2014-02-08 18:14:20 +01:00
|
|
|
/* ============================================================
|
2017-08-25 17:11:29 +02:00
|
|
|
* Falkon - Qt web browser
|
2014-02-08 18:14:20 +01:00
|
|
|
* Copyright (C) 2014 David Rosca <nowrep@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can 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.
|
|
|
|
*
|
|
|
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
|
|
|
#include "bookmarkstreeview.h"
|
2014-02-09 17:27:55 +01:00
|
|
|
#include "bookmarksitemdelegate.h"
|
2014-02-08 18:14:20 +01:00
|
|
|
#include "bookmarksmodel.h"
|
|
|
|
#include "bookmarkitem.h"
|
|
|
|
#include "bookmarks.h"
|
|
|
|
#include "mainapplication.h"
|
2016-12-11 10:24:10 +01:00
|
|
|
#include "iconprovider.h"
|
2014-02-08 18:14:20 +01:00
|
|
|
|
|
|
|
#include <QHeaderView>
|
|
|
|
#include <QMouseEvent>
|
|
|
|
|
|
|
|
BookmarksTreeView::BookmarksTreeView(QWidget* parent)
|
|
|
|
: QTreeView(parent)
|
|
|
|
, m_bookmarks(mApp->bookmarks())
|
|
|
|
, m_model(m_bookmarks->model())
|
2014-02-09 15:44:38 +01:00
|
|
|
, m_filter(new BookmarksFilterModel(m_model))
|
2014-02-09 11:02:51 +01:00
|
|
|
, m_type(BookmarksManagerViewType)
|
2014-02-08 18:14:20 +01:00
|
|
|
{
|
2014-02-09 15:44:38 +01:00
|
|
|
setModel(m_filter);
|
2014-02-08 18:14:20 +01:00
|
|
|
setDragEnabled(true);
|
|
|
|
setAcceptDrops(true);
|
2014-05-03 15:08:47 +02:00
|
|
|
setUniformRowHeights(true);
|
2014-02-08 18:14:20 +01:00
|
|
|
setDropIndicatorShown(true);
|
2014-02-11 14:34:02 +01:00
|
|
|
setAllColumnsShowFocus(true);
|
2014-02-09 17:27:55 +01:00
|
|
|
setItemDelegate(new BookmarksItemDelegate(this));
|
2014-02-08 18:14:20 +01:00
|
|
|
header()->resizeSections(QHeaderView::ResizeToContents);
|
|
|
|
|
2015-08-31 22:26:13 +02:00
|
|
|
connect(this, &QTreeView::expanded, this, &BookmarksTreeView::indexExpanded);
|
|
|
|
connect(this, &QTreeView::collapsed, this, &BookmarksTreeView::indexCollapsed);
|
2014-02-08 18:14:20 +01:00
|
|
|
}
|
|
|
|
|
2014-02-09 11:02:51 +01:00
|
|
|
BookmarksTreeView::ViewType BookmarksTreeView::viewType() const
|
|
|
|
{
|
|
|
|
return m_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksTreeView::setViewType(BookmarksTreeView::ViewType type)
|
|
|
|
{
|
|
|
|
m_type = type;
|
|
|
|
|
|
|
|
switch (m_type) {
|
|
|
|
case BookmarksManagerViewType:
|
|
|
|
setColumnHidden(1, false);
|
|
|
|
setHeaderHidden(false);
|
2014-02-09 18:53:21 +01:00
|
|
|
setMouseTracking(false);
|
2014-03-01 14:06:44 +01:00
|
|
|
setSelectionMode(QAbstractItemView::ExtendedSelection);
|
2014-02-09 11:02:51 +01:00
|
|
|
break;
|
|
|
|
case BookmarksSidebarViewType:
|
|
|
|
setColumnHidden(1, true);
|
|
|
|
setHeaderHidden(true);
|
2014-02-09 18:53:21 +01:00
|
|
|
setMouseTracking(true);
|
2014-03-01 14:06:44 +01:00
|
|
|
setSelectionMode(QAbstractItemView::SingleSelection);
|
2014-02-09 11:02:51 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
restoreExpandedState(QModelIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
BookmarkItem* BookmarksTreeView::selectedBookmark() const
|
|
|
|
{
|
|
|
|
QList<BookmarkItem*> items = selectedBookmarks();
|
2015-10-23 11:55:14 +02:00
|
|
|
return items.count() == 1 ? items.at(0) : 0;
|
2014-02-09 11:02:51 +01:00
|
|
|
}
|
|
|
|
|
2014-02-08 18:14:20 +01:00
|
|
|
QList<BookmarkItem*> BookmarksTreeView::selectedBookmarks() const
|
|
|
|
{
|
|
|
|
QList<BookmarkItem*> items;
|
|
|
|
|
|
|
|
foreach (const QModelIndex &index, selectionModel()->selectedRows()) {
|
2014-02-09 15:44:38 +01:00
|
|
|
BookmarkItem* item = m_model->item(m_filter->mapToSource(index));
|
2014-02-08 18:14:20 +01:00
|
|
|
items.append(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2014-02-09 15:07:19 +01:00
|
|
|
void BookmarksTreeView::selectBookmark(BookmarkItem* item)
|
|
|
|
{
|
2014-02-09 15:44:38 +01:00
|
|
|
QModelIndex col0 = m_filter->mapFromSource(m_model->index(item, 0));
|
|
|
|
QModelIndex col1 = m_filter->mapFromSource(m_model->index(item, 1));
|
2014-02-09 15:07:19 +01:00
|
|
|
|
|
|
|
selectionModel()->clearSelection();
|
|
|
|
selectionModel()->select(col0, QItemSelectionModel::Select);
|
|
|
|
selectionModel()->select(col1, QItemSelectionModel::Select);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksTreeView::ensureBookmarkVisible(BookmarkItem* item)
|
|
|
|
{
|
2014-02-09 15:44:38 +01:00
|
|
|
QModelIndex index = m_filter->mapFromSource(m_model->index(item));
|
|
|
|
QModelIndex parent = m_filter->parent(index);
|
2014-02-09 15:07:19 +01:00
|
|
|
|
|
|
|
while (parent.isValid()) {
|
|
|
|
setExpanded(parent, true);
|
2014-02-09 15:44:38 +01:00
|
|
|
parent = m_filter->parent(parent);
|
2014-02-09 15:07:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-09 15:44:38 +01:00
|
|
|
void BookmarksTreeView::search(const QString &string)
|
|
|
|
{
|
|
|
|
m_filter->setFilterFixedString(string);
|
|
|
|
}
|
|
|
|
|
2014-02-08 18:14:20 +01:00
|
|
|
void BookmarksTreeView::indexExpanded(const QModelIndex &parent)
|
|
|
|
{
|
2014-02-09 15:44:38 +01:00
|
|
|
BookmarkItem* item = m_model->item(m_filter->mapToSource(parent));
|
2014-02-09 11:02:51 +01:00
|
|
|
|
|
|
|
switch (m_type) {
|
|
|
|
case BookmarksManagerViewType:
|
|
|
|
item->setExpanded(true);
|
|
|
|
break;
|
|
|
|
case BookmarksSidebarViewType:
|
|
|
|
item->setSidebarExpanded(true);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-02-08 18:14:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksTreeView::indexCollapsed(const QModelIndex &parent)
|
|
|
|
{
|
2014-02-09 15:44:38 +01:00
|
|
|
BookmarkItem* item = m_model->item(m_filter->mapToSource(parent));
|
2014-02-09 11:02:51 +01:00
|
|
|
|
|
|
|
switch (m_type) {
|
|
|
|
case BookmarksManagerViewType:
|
|
|
|
item->setExpanded(false);
|
|
|
|
break;
|
|
|
|
case BookmarksSidebarViewType:
|
|
|
|
item->setSidebarExpanded(false);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-02-08 18:14:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksTreeView::restoreExpandedState(const QModelIndex &parent)
|
|
|
|
{
|
2014-02-09 15:44:38 +01:00
|
|
|
for (int i = 0; i < m_filter->rowCount(parent); ++i) {
|
|
|
|
QModelIndex index = m_filter->index(i, 0, parent);
|
|
|
|
BookmarkItem* item = m_model->item(m_filter->mapToSource(index));
|
2014-02-09 11:02:51 +01:00
|
|
|
setExpanded(index, m_type == BookmarksManagerViewType ? item->isExpanded() : item->isSidebarExpanded());
|
2014-02-08 18:14:20 +01:00
|
|
|
restoreExpandedState(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksTreeView::rowsInserted(const QModelIndex &parent, int start, int end)
|
|
|
|
{
|
|
|
|
restoreExpandedState(parent);
|
|
|
|
QTreeView::rowsInserted(parent, start, end);
|
|
|
|
}
|
|
|
|
|
2015-08-31 22:26:13 +02:00
|
|
|
void BookmarksTreeView::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
|
|
|
|
{
|
|
|
|
Q_UNUSED(selected)
|
|
|
|
Q_UNUSED(deselected)
|
|
|
|
|
|
|
|
emit bookmarksSelected(selectedBookmarks());
|
|
|
|
}
|
|
|
|
|
2014-05-03 15:08:47 +02:00
|
|
|
void BookmarksTreeView::contextMenuEvent(QContextMenuEvent* event)
|
|
|
|
{
|
|
|
|
emit contextMenuRequested(viewport()->mapToGlobal(event->pos()));
|
|
|
|
}
|
|
|
|
|
2014-02-09 18:53:21 +01:00
|
|
|
void BookmarksTreeView::mouseMoveEvent(QMouseEvent* event)
|
|
|
|
{
|
2014-02-10 15:09:51 +01:00
|
|
|
QTreeView::mouseMoveEvent(event);
|
|
|
|
|
2014-02-09 18:53:21 +01:00
|
|
|
if (m_type == BookmarksSidebarViewType) {
|
|
|
|
QCursor cursor = Qt::ArrowCursor;
|
|
|
|
if (event->buttons() == Qt::NoButton) {
|
|
|
|
QModelIndex index = indexAt(event->pos());
|
|
|
|
if (index.isValid() && index.data(BookmarksModel::TypeRole).toInt() == BookmarkItem::Url) {
|
|
|
|
cursor = Qt::PointingHandCursor;
|
|
|
|
}
|
|
|
|
}
|
2014-05-03 15:23:07 +02:00
|
|
|
viewport()->setCursor(cursor);
|
2014-02-09 18:53:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-08 18:14:20 +01:00
|
|
|
void BookmarksTreeView::mousePressEvent(QMouseEvent* event)
|
|
|
|
{
|
2014-02-08 20:01:07 +01:00
|
|
|
QTreeView::mousePressEvent(event);
|
2014-02-08 18:14:20 +01:00
|
|
|
|
2014-02-08 20:01:07 +01:00
|
|
|
if (selectionModel()->selectedRows().count() == 1) {
|
|
|
|
QModelIndex index = indexAt(event->pos());
|
2014-03-01 14:06:44 +01:00
|
|
|
Qt::MouseButtons buttons = event->buttons();
|
|
|
|
Qt::KeyboardModifiers modifiers = event->modifiers();
|
2014-02-08 18:14:20 +01:00
|
|
|
|
2014-02-08 20:01:07 +01:00
|
|
|
if (index.isValid()) {
|
2014-02-09 15:44:38 +01:00
|
|
|
BookmarkItem* item = m_model->item(m_filter->mapToSource(index));
|
2014-02-08 20:01:07 +01:00
|
|
|
|
|
|
|
if (buttons == Qt::LeftButton && modifiers == Qt::ShiftModifier) {
|
|
|
|
emit bookmarkShiftActivated(item);
|
|
|
|
}
|
2014-03-01 14:06:44 +01:00
|
|
|
else if (buttons == Qt::MiddleButton || (buttons == Qt::LeftButton && modifiers == Qt::ControlModifier)) {
|
2014-02-08 20:01:07 +01:00
|
|
|
emit bookmarkCtrlActivated(item);
|
|
|
|
}
|
2014-02-10 15:09:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksTreeView::mouseReleaseEvent(QMouseEvent* event)
|
|
|
|
{
|
|
|
|
QTreeView::mouseReleaseEvent(event);
|
|
|
|
|
|
|
|
if (selectionModel()->selectedRows().count() == 1) {
|
|
|
|
QModelIndex index = indexAt(event->pos());
|
|
|
|
|
|
|
|
if (index.isValid()) {
|
|
|
|
BookmarkItem* item = m_model->item(m_filter->mapToSource(index));
|
2014-02-09 18:53:21 +01:00
|
|
|
|
|
|
|
// Activate bookmarks with single mouse click in Sidebar
|
2014-03-01 14:06:44 +01:00
|
|
|
if (m_type == BookmarksSidebarViewType && event->button() == Qt::LeftButton && event->modifiers() == Qt::NoModifier) {
|
2014-02-09 18:53:21 +01:00
|
|
|
emit bookmarkActivated(item);
|
|
|
|
}
|
2014-02-08 18:14:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksTreeView::mouseDoubleClickEvent(QMouseEvent* event)
|
|
|
|
{
|
2014-02-08 20:01:07 +01:00
|
|
|
QTreeView::mouseDoubleClickEvent(event);
|
2014-02-08 18:14:20 +01:00
|
|
|
|
2014-02-08 20:01:07 +01:00
|
|
|
if (selectionModel()->selectedRows().count() == 1) {
|
|
|
|
QModelIndex index = indexAt(event->pos());
|
2014-02-08 18:14:20 +01:00
|
|
|
|
2014-02-08 20:01:07 +01:00
|
|
|
if (index.isValid()) {
|
2014-02-09 15:44:38 +01:00
|
|
|
BookmarkItem* item = m_model->item(m_filter->mapToSource(index));
|
2014-02-08 20:01:07 +01:00
|
|
|
Qt::MouseButtons buttons = event->buttons();
|
|
|
|
Qt::KeyboardModifiers modifiers = QApplication::keyboardModifiers();
|
|
|
|
|
|
|
|
if (buttons == Qt::LeftButton && modifiers == Qt::NoModifier) {
|
|
|
|
emit bookmarkActivated(item);
|
|
|
|
}
|
|
|
|
else if (buttons == Qt::LeftButton && modifiers == Qt::ShiftModifier) {
|
|
|
|
emit bookmarkShiftActivated(item);
|
|
|
|
}
|
2014-02-08 18:14:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksTreeView::keyPressEvent(QKeyEvent* event)
|
|
|
|
{
|
2014-02-08 20:01:07 +01:00
|
|
|
QTreeView::keyPressEvent(event);
|
|
|
|
|
2014-02-08 18:14:20 +01:00
|
|
|
if (selectionModel()->selectedRows().count() == 1) {
|
2015-10-23 11:55:14 +02:00
|
|
|
QModelIndex index = selectionModel()->selectedRows().at(0);
|
2014-02-09 15:44:38 +01:00
|
|
|
BookmarkItem* item = m_model->item(m_filter->mapToSource(index));
|
2014-02-08 18:14:20 +01:00
|
|
|
|
|
|
|
switch (event->key()) {
|
|
|
|
case Qt::Key_Return:
|
|
|
|
case Qt::Key_Enter:
|
2016-01-02 19:58:54 +01:00
|
|
|
if (item->isFolder() && (event->modifiers() == Qt::NoModifier || event->modifiers() == Qt::KeypadModifier)) {
|
2014-02-08 18:14:20 +01:00
|
|
|
setExpanded(index, !isExpanded(index));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Qt::KeyboardModifiers modifiers = event->modifiers();
|
|
|
|
|
2016-01-02 19:58:54 +01:00
|
|
|
if (modifiers == Qt::NoModifier || modifiers == Qt::KeypadModifier) {
|
2014-02-08 18:14:20 +01:00
|
|
|
emit bookmarkActivated(item);
|
|
|
|
}
|
|
|
|
else if (modifiers == Qt::ControlModifier) {
|
|
|
|
emit bookmarkCtrlActivated(item);
|
|
|
|
}
|
|
|
|
else if (modifiers == Qt::ShiftModifier) {
|
|
|
|
emit bookmarkShiftActivated(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|