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 |
3 | #include "bookmarkdialog.h" |
4 | #include "bookmarkfiltermodel.h" |
5 | #include "bookmarkitem.h" |
6 | #include "bookmarkmodel.h" |
7 | #include "helpenginewrapper.h" |
8 | #include "tracer.h" |
9 | |
10 | #include <QtGui/QKeyEvent> |
11 | #include <QtWidgets/QMenu> |
12 | |
13 | QT_BEGIN_NAMESPACE |
14 | |
15 | BookmarkDialog::BookmarkDialog(BookmarkModel *sourceModel, const QString &title, |
16 | const QString &url, QWidget *parent) |
17 | : QDialog(parent) |
18 | , m_url(url) |
19 | , m_title(title) |
20 | , bookmarkModel(sourceModel) |
21 | { |
22 | TRACE_OBJ |
23 | ui.setupUi(this); |
24 | |
25 | ui.bookmarkEdit->setText(m_title); |
26 | ui.newFolderButton->setVisible(false); |
27 | ui.buttonBox->button(QDialogButtonBox::Ok)->setDefault(true); |
28 | |
29 | connect(ui.buttonBox, &QDialogButtonBox::accepted, this, &BookmarkDialog::accepted); |
30 | connect(ui.buttonBox, &QDialogButtonBox::rejected, this, &BookmarkDialog::rejected); |
31 | connect(ui.newFolderButton, &QAbstractButton::clicked, this, &BookmarkDialog::addFolder); |
32 | connect(ui.toolButton, &QAbstractButton::clicked, this, &BookmarkDialog::toolButtonClicked); |
33 | connect(ui.bookmarkEdit, &QLineEdit::textChanged, this, &BookmarkDialog::textChanged); |
34 | |
35 | bookmarkProxyModel = new BookmarkFilterModel(this); |
36 | bookmarkProxyModel->setSourceModel(bookmarkModel); |
37 | ui.bookmarkFolders->setModel(bookmarkProxyModel); |
38 | connect(ui.bookmarkFolders, &QComboBox::currentIndexChanged, |
39 | this, QOverload<int>::of(&BookmarkDialog::currentIndexChanged)); |
40 | |
41 | bookmarkTreeModel = new BookmarkTreeModel(this); |
42 | bookmarkTreeModel->setSourceModel(bookmarkModel); |
43 | ui.treeView->setModel(bookmarkTreeModel); |
44 | |
45 | ui.treeView->expandAll(); |
46 | ui.treeView->setVisible(false); |
47 | ui.treeView->installEventFilter(this); |
48 | ui.treeView->viewport()->installEventFilter(this); |
49 | ui.treeView->setContextMenuPolicy(Qt::CustomContextMenu); |
50 | |
51 | connect(ui.treeView, &QWidget::customContextMenuRequested, |
52 | this, &BookmarkDialog::customContextMenuRequested); |
53 | connect(ui.treeView->selectionModel(), &QItemSelectionModel::currentChanged, |
54 | this, QOverload<const QModelIndex &>::of(&BookmarkDialog::currentIndexChanged)); |
55 | |
56 | ui.bookmarkFolders->setCurrentIndex(ui.bookmarkFolders->count() > 1 ? 1 : 0); |
57 | |
58 | const HelpEngineWrapper &helpEngine = HelpEngineWrapper::instance(); |
59 | if (helpEngine.usesAppFont()) |
60 | setFont(helpEngine.appFont()); |
61 | } |
62 | |
63 | BookmarkDialog::~BookmarkDialog() |
64 | { |
65 | TRACE_OBJ |
66 | } |
67 | |
68 | bool BookmarkDialog::isRootItem(const QModelIndex &index) const |
69 | { |
70 | return !bookmarkTreeModel->parent(child: index).isValid(); |
71 | } |
72 | |
73 | bool BookmarkDialog::eventFilter(QObject *object, QEvent *event) |
74 | { |
75 | TRACE_OBJ |
76 | if (object != ui.treeView && object != ui.treeView->viewport()) |
77 | return QWidget::eventFilter(object, event); |
78 | |
79 | if (event->type() == QEvent::KeyPress) { |
80 | QKeyEvent *ke = static_cast<QKeyEvent*>(event); |
81 | switch (ke->key()) { |
82 | case Qt::Key_F2: { |
83 | const QModelIndex &index = ui.treeView->currentIndex(); |
84 | if (!isRootItem(index)) { |
85 | bookmarkModel->setItemsEditable(true); |
86 | ui.treeView->edit(index); |
87 | bookmarkModel->setItemsEditable(false); |
88 | } |
89 | } break; |
90 | default: break; |
91 | } |
92 | } |
93 | |
94 | return QObject::eventFilter(object, event); |
95 | } |
96 | |
97 | void BookmarkDialog::currentIndexChanged(int row) |
98 | { |
99 | TRACE_OBJ |
100 | QModelIndex next = bookmarkProxyModel->index(row, column: 0, parent: QModelIndex()); |
101 | if (next.isValid()) { |
102 | next = bookmarkProxyModel->mapToSource(proxyIndex: next); |
103 | ui.treeView->setCurrentIndex(bookmarkTreeModel->mapFromSource(next)); |
104 | } |
105 | } |
106 | |
107 | void BookmarkDialog::currentIndexChanged(const QModelIndex &index) |
108 | { |
109 | TRACE_OBJ |
110 | const QModelIndex current = bookmarkTreeModel->mapToSource(proxyIndex: index); |
111 | if (current.isValid()) { |
112 | const int row = bookmarkProxyModel->mapFromSource(sourceIndex: current).row(); |
113 | ui.bookmarkFolders->setCurrentIndex(row); |
114 | } |
115 | } |
116 | |
117 | void BookmarkDialog::accepted() |
118 | { |
119 | TRACE_OBJ |
120 | QModelIndex index = ui.treeView->currentIndex(); |
121 | if (index.isValid()) { |
122 | index = bookmarkModel->addItem(parent: bookmarkTreeModel->mapToSource(proxyIndex: index)); |
123 | bookmarkModel->setData(index, DataVector() << m_title << m_url << false); |
124 | } else |
125 | rejected(); |
126 | |
127 | accept(); |
128 | } |
129 | |
130 | void BookmarkDialog::rejected() |
131 | { |
132 | TRACE_OBJ |
133 | for (const QPersistentModelIndex &index : std::as_const(cache)) |
134 | bookmarkModel->removeItem(index); |
135 | reject(); |
136 | } |
137 | |
138 | void BookmarkDialog::addFolder() |
139 | { |
140 | TRACE_OBJ |
141 | QModelIndex index = ui.treeView->currentIndex(); |
142 | if (index.isValid()) { |
143 | index = bookmarkModel->addItem(parent: bookmarkTreeModel->mapToSource(proxyIndex: index), |
144 | isFolder: true); |
145 | cache.append(index); |
146 | |
147 | index = bookmarkTreeModel->mapFromSource(sourceIndex: index); |
148 | if (index.isValid()) { |
149 | bookmarkModel->setItemsEditable(true); |
150 | ui.treeView->edit(index); |
151 | ui.treeView->expand(index); |
152 | ui.treeView->setCurrentIndex(index); |
153 | bookmarkModel->setItemsEditable(false); |
154 | } |
155 | } |
156 | } |
157 | |
158 | void BookmarkDialog::toolButtonClicked() |
159 | { |
160 | TRACE_OBJ |
161 | const bool visible = !ui.treeView->isVisible(); |
162 | ui.treeView->setVisible(visible); |
163 | ui.newFolderButton->setVisible(visible); |
164 | |
165 | if (visible) { |
166 | resize(QSize(width(), 400)); |
167 | ui.toolButton->setText(QLatin1String("-" )); |
168 | } else { |
169 | resize(width(), minimumHeight()); |
170 | ui.toolButton->setText(QLatin1String("+" )); |
171 | } |
172 | } |
173 | |
174 | void BookmarkDialog::textChanged(const QString& text) |
175 | { |
176 | m_title = text; |
177 | } |
178 | |
179 | void BookmarkDialog::(const QPoint &point) |
180 | { |
181 | TRACE_OBJ |
182 | const QModelIndex &index = ui.treeView->currentIndex(); |
183 | if (isRootItem(index)) |
184 | return; // check if we go to rename the "Bookmarks Menu", bail |
185 | |
186 | QMenu (QString(), this); |
187 | QAction *renameItem = menu.addAction(tr("Rename Folder" )); |
188 | |
189 | QAction *picked = menu.exec(ui.treeView->mapToGlobal(point)); |
190 | if (picked == renameItem) { |
191 | bookmarkModel->setItemsEditable(true); |
192 | ui.treeView->edit(index); |
193 | bookmarkModel->setItemsEditable(false); |
194 | } |
195 | } |
196 | |
197 | QT_END_NAMESPACE |
198 | |