Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
bookmarkdialog.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
5#include "bookmarkitem.h"
8#include "tracer.h"
9
10#include <QtGui/QKeyEvent>
11#include <QtWidgets/QMenu>
12
14
15using namespace Qt::StringLiterals;
16
17BookmarkDialog::BookmarkDialog(BookmarkModel *sourceModel, const QString &title,
18 const QString &url, QWidget *parent)
19 : QDialog(parent)
20 , m_url(url)
21 , m_title(title)
22 , bookmarkModel(sourceModel)
23{
25 ui.setupUi(this);
26
27 ui.bookmarkEdit->setText(m_title);
28 ui.newFolderButton->setVisible(false);
29 ui.buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
30
31 connect(ui.buttonBox, &QDialogButtonBox::accepted, this, &BookmarkDialog::accepted);
32 connect(ui.buttonBox, &QDialogButtonBox::rejected, this, &BookmarkDialog::rejected);
33 connect(ui.newFolderButton, &QAbstractButton::clicked, this, &BookmarkDialog::addFolder);
34 connect(ui.toolButton, &QAbstractButton::clicked, this, &BookmarkDialog::toolButtonClicked);
35 connect(ui.bookmarkEdit, &QLineEdit::textChanged, this, &BookmarkDialog::textChanged);
36
37 bookmarkProxyModel = new BookmarkFilterModel(this);
38 bookmarkProxyModel->setSourceModel(bookmarkModel);
39 ui.bookmarkFolders->setModel(bookmarkProxyModel);
40 connect(ui.bookmarkFolders, &QComboBox::currentIndexChanged,
41 this, QOverload<int>::of(&BookmarkDialog::currentIndexChanged));
42
43 bookmarkTreeModel = new BookmarkTreeModel(this);
44 bookmarkTreeModel->setSourceModel(bookmarkModel);
45 ui.treeView->setModel(bookmarkTreeModel);
46
47 ui.treeView->expandAll();
48 ui.treeView->setVisible(false);
49 ui.treeView->installEventFilter(this);
50 ui.treeView->viewport()->installEventFilter(this);
51 ui.treeView->setContextMenuPolicy(Qt::CustomContextMenu);
52
53 connect(ui.treeView, &QWidget::customContextMenuRequested,
54 this, &BookmarkDialog::customContextMenuRequested);
55 connect(ui.treeView->selectionModel(), &QItemSelectionModel::currentChanged,
56 this, QOverload<const QModelIndex &>::of(&BookmarkDialog::currentIndexChanged));
57
58 ui.bookmarkFolders->setCurrentIndex(ui.bookmarkFolders->count() > 1 ? 1 : 0);
59
61 if (helpEngine.usesAppFont())
62 setFont(helpEngine.appFont());
63}
64
69
70bool BookmarkDialog::isRootItem(const QModelIndex &index) const
71{
72 return !bookmarkTreeModel->parent(index).isValid();
73}
74
75bool BookmarkDialog::eventFilter(QObject *object, QEvent *event)
76{
78 if (object != ui.treeView && object != ui.treeView->viewport())
79 return QWidget::eventFilter(object, event);
80
81 if (event->type() == QEvent::KeyPress) {
82 QKeyEvent *ke = static_cast<QKeyEvent*>(event);
83 switch (ke->key()) {
84 case Qt::Key_F2: {
85 const QModelIndex &index = ui.treeView->currentIndex();
86 if (!isRootItem(index)) {
87 bookmarkModel->setItemsEditable(true);
88 ui.treeView->edit(index);
89 bookmarkModel->setItemsEditable(false);
90 }
91 } break;
92 default: break;
93 }
94 }
95
96 return QObject::eventFilter(object, event);
97}
98
99void BookmarkDialog::currentIndexChanged(int row)
100{
102 QModelIndex next = bookmarkProxyModel->index(row, 0, QModelIndex());
103 if (next.isValid()) {
104 next = bookmarkProxyModel->mapToSource(next);
105 ui.treeView->setCurrentIndex(bookmarkTreeModel->mapFromSource(next));
106 }
107}
108
109void BookmarkDialog::currentIndexChanged(const QModelIndex &index)
110{
112 const QModelIndex current = bookmarkTreeModel->mapToSource(index);
113 if (current.isValid()) {
114 const int row = bookmarkProxyModel->mapFromSource(current).row();
115 ui.bookmarkFolders->setCurrentIndex(row);
116 }
117}
118
119void BookmarkDialog::accepted()
120{
122 QModelIndex index = ui.treeView->currentIndex();
123 if (index.isValid()) {
124 index = bookmarkModel->addItem(bookmarkTreeModel->mapToSource(index));
125 bookmarkModel->setData(index, DataVector() << m_title << m_url << false);
126 } else
127 rejected();
128
129 accept();
130}
131
132void BookmarkDialog::rejected()
133{
135 for (const QPersistentModelIndex &index : std::as_const(cache))
136 bookmarkModel->removeItem(index);
137 reject();
138}
139
140void BookmarkDialog::addFolder()
141{
143 QModelIndex index = ui.treeView->currentIndex();
144 if (index.isValid()) {
145 index = bookmarkModel->addItem(bookmarkTreeModel->mapToSource(index),
146 true);
147 cache.append(index);
148
149 index = bookmarkTreeModel->mapFromSource(index);
150 if (index.isValid()) {
151 bookmarkModel->setItemsEditable(true);
152 ui.treeView->edit(index);
153 ui.treeView->expand(index);
154 ui.treeView->setCurrentIndex(index);
155 bookmarkModel->setItemsEditable(false);
156 }
157 }
158}
159
160void BookmarkDialog::toolButtonClicked()
161{
163 const bool visible = !ui.treeView->isVisible();
164 ui.treeView->setVisible(visible);
165 ui.newFolderButton->setVisible(visible);
166
167 if (visible) {
168 resize(QSize(width(), 400));
169 ui.toolButton->setText("-"_L1);
170 } else {
171 resize(width(), minimumHeight());
172 ui.toolButton->setText("+"_L1);
173 }
174}
175
176void BookmarkDialog::textChanged(const QString& text)
177{
178 m_title = text;
179}
180
181void BookmarkDialog::customContextMenuRequested(const QPoint &point)
182{
184 const QModelIndex &index = ui.treeView->currentIndex();
185 if (isRootItem(index))
186 return; // check if we go to rename the "Bookmarks Menu", bail
187
188 QMenu menu(QString(), this);
189 QAction *renameItem = menu.addAction(tr("Rename Folder"));
190
191 QAction *picked = menu.exec(ui.treeView->mapToGlobal(point));
192 if (picked == renameItem) {
193 bookmarkModel->setItemsEditable(true);
194 ui.treeView->edit(index);
195 bookmarkModel->setItemsEditable(false);
196 }
197}
198
199QT_END_NAMESPACE
bool eventFilter(QObject *object, QEvent *event) override
Filters events if this object has been installed as an event filter for the watched object.
~BookmarkDialog() override
void setItemsEditable(bool editable)
static HelpEngineWrapper & instance()
Combined button and popup list for selecting options.
#define TRACE_OBJ
Definition tracer.h:34