-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookmarkdialog.cpp
271 lines (241 loc) · 8.75 KB
/
bookmarkdialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// *****************************************************************************
// bookmarkdialog.cpp Tao3D project
// *****************************************************************************
//
// File description:
//
//
//
//
//
//
//
//
// *****************************************************************************
// This software is licensed under the GNU General Public License v3
// (C) 2013,2015,2019, Christophe de Dinechin <[email protected]>
// (C) 2011,2013, Jérôme Forissier <[email protected]>
// *****************************************************************************
// This file is part of Tao3D
//
// Tao3D is free software: you can r redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Tao3D is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Tao3D, in a file named COPYING.
// If not, see <https://www.gnu.org/licenses/>.
// *****************************************************************************
/*
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the Qt Assistant of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
*/
#include "bookmarkdialog.h"
#include "bookmarkfiltermodel.h"
#include "bookmarkitem.h"
#include "bookmarkmodel.h"
#include "helpenginewrapper.h"
#include "tracer.h"
#include <QKeyEvent>
#include <QMenu>
QT_BEGIN_NAMESPACE
BookmarkDialog::BookmarkDialog(BookmarkModel *sourceModel, const QString &title,
const QString &url, QWidget *parent)
: QDialog(parent)
, m_url(url)
, m_title(title)
, bookmarkModel(sourceModel)
{
TRACE_OBJ
ui.setupUi(this);
ui.bookmarkEdit->setText(m_title);
ui.newFolderButton->setVisible(false);
ui.buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
connect(ui.buttonBox, SIGNAL(accepted()), this, SLOT(accepted()));
connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(rejected()));
connect(ui.newFolderButton, SIGNAL(clicked()), this, SLOT(addFolder()));
connect(ui.toolButton, SIGNAL(clicked()), this, SLOT(toolButtonClicked()));
connect(ui.bookmarkEdit, SIGNAL(textChanged(QString)), this,
SLOT(textChanged(QString)));
bookmarkProxyModel = new BookmarkFilterModel(this);
bookmarkProxyModel->setSourceModel(bookmarkModel);
ui.bookmarkFolders->setModel(bookmarkProxyModel);
connect(ui.bookmarkFolders, SIGNAL(currentIndexChanged(int)), this,
SLOT(currentIndexChanged(int)));
bookmarkTreeModel = new BookmarkTreeModel(this);
bookmarkTreeModel->setSourceModel(bookmarkModel);
ui.treeView->setModel(bookmarkTreeModel);
ui.treeView->expandAll();
ui.treeView->setVisible(false);
ui.treeView->installEventFilter(this);
ui.treeView->viewport()->installEventFilter(this);
ui.treeView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui.treeView, SIGNAL(customContextMenuRequested(QPoint)), this,
SLOT(customContextMenuRequested(QPoint)));
connect(ui.treeView->selectionModel(), SIGNAL(currentChanged(QModelIndex,
QModelIndex)), this, SLOT(currentIndexChanged(QModelIndex)));
ui.bookmarkFolders->setCurrentIndex(0);
ui.treeView->setCurrentIndex(ui.treeView->indexAt(QPoint(2, 2)));
const HelpEngineWrapper &helpEngine = HelpEngineWrapper::instance();
if (helpEngine.usesAppFont())
setFont(helpEngine.appFont());
}
BookmarkDialog::~BookmarkDialog()
{
TRACE_OBJ
}
bool BookmarkDialog::isRootItem(const QModelIndex &index) const
{
return !bookmarkTreeModel->parent(index).isValid();
}
bool BookmarkDialog::eventFilter(QObject *object, QEvent *event)
{
TRACE_OBJ
if (object != ui.treeView && object != ui.treeView->viewport())
return QWidget::eventFilter(object, event);
if (event->type() == QEvent::KeyPress) {
QKeyEvent *ke = static_cast<QKeyEvent*>(event);
switch (ke->key()) {
case Qt::Key_F2: {
const QModelIndex &index = ui.treeView->currentIndex();
if (!isRootItem(index)) {
bookmarkModel->setItemsEditable(true);
ui.treeView->edit(index);
bookmarkModel->setItemsEditable(false);
}
} break;
default: break;
}
}
return QObject::eventFilter(object, event);
}
void BookmarkDialog::currentIndexChanged(int row)
{
TRACE_OBJ
QModelIndex next = bookmarkProxyModel->index(row, 0, QModelIndex());
if (next.isValid()) {
next = bookmarkProxyModel->mapToSource(next);
ui.treeView->setCurrentIndex(bookmarkTreeModel->mapFromSource(next));
}
}
void BookmarkDialog::currentIndexChanged(const QModelIndex &index)
{
TRACE_OBJ
const QModelIndex current = bookmarkTreeModel->mapToSource(index);
if (current.isValid()) {
const int row = bookmarkProxyModel->mapFromSource(current).row();
ui.bookmarkFolders->setCurrentIndex(row);
}
}
void BookmarkDialog::accepted()
{
TRACE_OBJ
QModelIndex index = ui.treeView->currentIndex();
if (index.isValid()) {
index = bookmarkModel->addItem(bookmarkTreeModel->mapToSource(index));
bookmarkModel->setData(index, DataVector() << m_title << m_url << false);
} else
rejected();
accept();
}
void BookmarkDialog::rejected()
{
TRACE_OBJ
foreach (const QPersistentModelIndex &index, cache)
bookmarkModel->removeItem(index);
reject();
}
void BookmarkDialog::addFolder()
{
TRACE_OBJ
QModelIndex index = ui.treeView->currentIndex();
if (index.isValid()) {
index = bookmarkModel->addItem(bookmarkTreeModel->mapToSource(index),
true);
cache.append(index);
index = bookmarkTreeModel->mapFromSource(index);
if (index.isValid()) {
bookmarkModel->setItemsEditable(true);
ui.treeView->edit(index);
ui.treeView->expand(index);
ui.treeView->setCurrentIndex(index);
bookmarkModel->setItemsEditable(false);
}
}
}
void BookmarkDialog::toolButtonClicked()
{
TRACE_OBJ
const bool visible = !ui.treeView->isVisible();
ui.treeView->setVisible(visible);
ui.newFolderButton->setVisible(visible);
if (visible) {
resize(QSize(width(), 400));
ui.toolButton->setText(QLatin1String("-"));
} else {
resize(width(), minimumHeight());
ui.toolButton->setText(QLatin1String("+"));
}
}
void BookmarkDialog::textChanged(const QString& text)
{
m_title = text;
}
void BookmarkDialog::customContextMenuRequested(const QPoint &point)
{
TRACE_OBJ
const QModelIndex &index = ui.treeView->currentIndex();
if (isRootItem(index))
return; // check if we go to rename the "Bookmarks Menu", bail
QMenu menu(QLatin1String(""), this);
QAction *renameItem = menu.addAction(tr("Rename Folder"));
QAction *picked = menu.exec(ui.treeView->mapToGlobal(point));
if (picked == renameItem) {
bookmarkModel->setItemsEditable(true);
ui.treeView->edit(index);
bookmarkModel->setItemsEditable(false);
}
}
QT_END_NAMESPACE