Falkon Develop
Cross-platform Qt-based web browser
bookmarkstreeview.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2014 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "bookmarkstreeview.h"
20#include "bookmarksmodel.h"
21#include "bookmarkitem.h"
22#include "bookmarks.h"
23#include "mainapplication.h"
24#include "iconprovider.h"
25
26#include <QHeaderView>
27#include <QMouseEvent>
28
30 : QTreeView(parent)
31 , m_bookmarks(mApp->bookmarks())
32 , m_model(m_bookmarks->model())
33 , m_filter(new BookmarksFilterModel(m_model))
34 , m_type(BookmarksManagerViewType)
35{
36 setModel(m_filter);
37 setDragEnabled(true);
38 setAcceptDrops(true);
39 setUniformRowHeights(true);
40 setDropIndicatorShown(true);
41 setAllColumnsShowFocus(true);
42 setItemDelegate(new BookmarksItemDelegate(this));
43 header()->resizeSections(QHeaderView::ResizeToContents);
44
45 connect(this, &QTreeView::expanded, this, &BookmarksTreeView::indexExpanded);
46 connect(this, &QTreeView::collapsed, this, &BookmarksTreeView::indexCollapsed);
47}
48
50{
51 return m_type;
52}
53
55{
56 m_type = type;
57
58 switch (m_type) {
60 setColumnHidden(1, false);
61 setHeaderHidden(false);
62 setMouseTracking(false);
63 setSelectionMode(QAbstractItemView::ExtendedSelection);
64 break;
66 setColumnHidden(1, true);
67 setHeaderHidden(true);
68 setMouseTracking(true);
69 setSelectionMode(QAbstractItemView::SingleSelection);
70 break;
71 default:
72 break;
73 }
74
75 restoreExpandedState(QModelIndex());
76}
77
79{
80 QList<BookmarkItem*> items = selectedBookmarks();
81 return items.count() == 1 ? items.at(0) : nullptr;
82}
83
84QList<BookmarkItem*> BookmarksTreeView::selectedBookmarks() const
85{
86 QList<BookmarkItem*> items;
87
88 const auto indexes = selectionModel()->selectedRows();
89 for (const QModelIndex &index : indexes) {
90 BookmarkItem* item = m_model->item(m_filter->mapToSource(index));
91 items.append(item);
92 }
93
94 return items;
95}
96
98{
99 QModelIndex col0 = m_filter->mapFromSource(m_model->index(item, 0));
100 QModelIndex col1 = m_filter->mapFromSource(m_model->index(item, 1));
101
102 selectionModel()->clearSelection();
103 selectionModel()->select(col0, QItemSelectionModel::Select);
104 selectionModel()->select(col1, QItemSelectionModel::Select);
105}
106
108{
109 QModelIndex index = m_filter->mapFromSource(m_model->index(item));
110 QModelIndex parent = m_filter->parent(index);
111
112 while (parent.isValid()) {
113 setExpanded(parent, true);
114 parent = m_filter->parent(parent);
115 }
116}
117
118void BookmarksTreeView::search(const QString &string)
119{
120 m_filter->setFilterFixedString(string);
121}
122
123void BookmarksTreeView::indexExpanded(const QModelIndex &parent)
124{
125 BookmarkItem* item = m_model->item(m_filter->mapToSource(parent));
126
127 switch (m_type) {
129 item->setExpanded(true);
130 break;
132 item->setSidebarExpanded(true);
133 break;
134 default:
135 break;
136 }
137}
138
139void BookmarksTreeView::indexCollapsed(const QModelIndex &parent)
140{
141 BookmarkItem* item = m_model->item(m_filter->mapToSource(parent));
142
143 switch (m_type) {
145 item->setExpanded(false);
146 break;
148 item->setSidebarExpanded(false);
149 break;
150 default:
151 break;
152 }
153}
154
155void BookmarksTreeView::restoreExpandedState(const QModelIndex &parent)
156{
157 for (int i = 0; i < m_filter->rowCount(parent); ++i) {
158 QModelIndex index = m_filter->index(i, 0, parent);
159 BookmarkItem* item = m_model->item(m_filter->mapToSource(index));
160 setExpanded(index, m_type == BookmarksManagerViewType ? item->isExpanded() : item->isSidebarExpanded());
161 restoreExpandedState(index);
162 }
163}
164
165void BookmarksTreeView::rowsInserted(const QModelIndex &parent, int start, int end)
166{
167 restoreExpandedState(parent);
168 QTreeView::rowsInserted(parent, start, end);
169}
170
171void BookmarksTreeView::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
172{
173 Q_UNUSED(selected)
174 Q_UNUSED(deselected)
175
177}
178
179void BookmarksTreeView::contextMenuEvent(QContextMenuEvent* event)
180{
181 Q_EMIT contextMenuRequested(viewport()->mapToGlobal(event->pos()));
182}
183
184void BookmarksTreeView::mouseMoveEvent(QMouseEvent* event)
185{
186 QTreeView::mouseMoveEvent(event);
187
188 if (m_type == BookmarksSidebarViewType) {
189 QCursor cursor = Qt::ArrowCursor;
190 if (event->buttons() == Qt::NoButton) {
191 QModelIndex index = indexAt(event->position().toPoint());
192 if (index.isValid() && index.data(BookmarksModel::TypeRole).toInt() == BookmarkItem::Url) {
193 cursor = Qt::PointingHandCursor;
194 }
195 }
196 viewport()->setCursor(cursor);
197 }
198}
199
200void BookmarksTreeView::mousePressEvent(QMouseEvent* event)
201{
202 QTreeView::mousePressEvent(event);
203
204 if (selectionModel()->selectedRows().count() == 1) {
205 QModelIndex index = indexAt(event->position().toPoint());
206 Qt::MouseButtons buttons = event->buttons();
207 Qt::KeyboardModifiers modifiers = event->modifiers();
208
209 if (index.isValid()) {
210 BookmarkItem* item = m_model->item(m_filter->mapToSource(index));
211
212 if (buttons == Qt::LeftButton && modifiers == Qt::ShiftModifier) {
213 Q_EMIT bookmarkShiftActivated(item);
214 }
215 else if (buttons == Qt::MiddleButton || (buttons == Qt::LeftButton && modifiers == Qt::ControlModifier)) {
216 Q_EMIT bookmarkCtrlActivated(item);
217 }
218 }
219 }
220}
221
222void BookmarksTreeView::mouseReleaseEvent(QMouseEvent* event)
223{
224 QTreeView::mouseReleaseEvent(event);
225
226 if (selectionModel()->selectedRows().count() == 1) {
227 QModelIndex index = indexAt(event->position().toPoint());
228
229 if (index.isValid()) {
230 BookmarkItem* item = m_model->item(m_filter->mapToSource(index));
231
232 // Activate bookmarks with single mouse click in Sidebar
233 if (m_type == BookmarksSidebarViewType && event->button() == Qt::LeftButton && event->modifiers() == Qt::NoModifier) {
234 Q_EMIT bookmarkActivated(item);
235 }
236 }
237 }
238}
239
240void BookmarksTreeView::mouseDoubleClickEvent(QMouseEvent* event)
241{
242 QTreeView::mouseDoubleClickEvent(event);
243
244 if (selectionModel()->selectedRows().count() == 1) {
245 QModelIndex index = indexAt(event->position().toPoint());
246
247 if (index.isValid()) {
248 BookmarkItem* item = m_model->item(m_filter->mapToSource(index));
249 Qt::MouseButtons buttons = event->buttons();
250 Qt::KeyboardModifiers modifiers = QApplication::keyboardModifiers();
251
252 if (buttons == Qt::LeftButton && modifiers == Qt::NoModifier) {
253 Q_EMIT bookmarkActivated(item);
254 }
255 else if (buttons == Qt::LeftButton && modifiers == Qt::ShiftModifier) {
256 Q_EMIT bookmarkShiftActivated(item);
257 }
258 }
259 }
260}
261
262void BookmarksTreeView::keyPressEvent(QKeyEvent* event)
263{
264 QTreeView::keyPressEvent(event);
265
266 if (selectionModel()->selectedRows().count() == 1) {
267 QModelIndex index = selectionModel()->selectedRows().at(0);
268 BookmarkItem* item = m_model->item(m_filter->mapToSource(index));
269
270 switch (event->key()) {
271 case Qt::Key_Return:
272 case Qt::Key_Enter:
273 if (item->isFolder() && (event->modifiers() == Qt::NoModifier || event->modifiers() == Qt::KeypadModifier)) {
274 setExpanded(index, !isExpanded(index));
275 }
276 else {
277 Qt::KeyboardModifiers modifiers = event->modifiers();
278
279 if (modifiers == Qt::NoModifier || modifiers == Qt::KeypadModifier) {
280 Q_EMIT bookmarkActivated(item);
281 }
282 else if (modifiers == Qt::ControlModifier) {
283 Q_EMIT bookmarkCtrlActivated(item);
284 }
285 else if (modifiers == Qt::ShiftModifier) {
286 Q_EMIT bookmarkShiftActivated(item);
287 }
288 }
289 break;
290 }
291 }
292}
bool isExpanded() const
bool isFolder() const
void setSidebarExpanded(bool expanded)
void setExpanded(bool expanded)
bool isSidebarExpanded() const
void setFilterFixedString(const QString &pattern)
BookmarkItem * item(const QModelIndex &index) const
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
QList< BookmarkItem * > selectedBookmarks() const
ViewType viewType() const
BookmarksTreeView(QWidget *parent=nullptr)
void bookmarkShiftActivated(BookmarkItem *item)
void bookmarkCtrlActivated(BookmarkItem *item)
void ensureBookmarkVisible(BookmarkItem *item)
void setViewType(ViewType type)
void bookmarkActivated(BookmarkItem *item)
void bookmarksSelected(const QList< BookmarkItem * > &items)
BookmarkItem * selectedBookmark() const
void search(const QString &string)
void contextMenuRequested(const QPoint &point)
void selectBookmark(BookmarkItem *item)
#define mApp
i
Definition: i18n.py:23