2014-02-08 18:14:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
|
|
|
* 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"
|
|
|
|
#include "bookmarksmodel.h"
|
|
|
|
#include "bookmarkitem.h"
|
|
|
|
#include "bookmarks.h"
|
|
|
|
#include "mainapplication.h"
|
|
|
|
|
|
|
|
#include <QHeaderView>
|
|
|
|
#include <QMouseEvent>
|
|
|
|
|
|
|
|
BookmarksTreeView::BookmarksTreeView(QWidget* parent)
|
|
|
|
: QTreeView(parent)
|
|
|
|
, m_bookmarks(mApp->bookmarks())
|
|
|
|
, m_model(m_bookmarks->model())
|
2014-02-09 11:02:51 +01:00
|
|
|
, m_type(BookmarksManagerViewType)
|
2014-02-08 18:14:20 +01:00
|
|
|
{
|
|
|
|
setModel(m_model);
|
|
|
|
setDragEnabled(true);
|
|
|
|
setAcceptDrops(true);
|
|
|
|
setDropIndicatorShown(true);
|
|
|
|
setSelectionMode(QAbstractItemView::ExtendedSelection);
|
|
|
|
setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
header()->resizeSections(QHeaderView::ResizeToContents);
|
|
|
|
|
|
|
|
connect(this, SIGNAL(expanded(QModelIndex)), this, SLOT(indexExpanded(QModelIndex)));
|
|
|
|
connect(this, SIGNAL(collapsed(QModelIndex)), this, SLOT(indexCollapsed(QModelIndex)));
|
|
|
|
connect(selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(selectionChanged()));
|
|
|
|
|
|
|
|
connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(createContextMenu(QPoint)));
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
break;
|
|
|
|
case BookmarksSidebarViewType:
|
|
|
|
setColumnHidden(1, true);
|
|
|
|
setHeaderHidden(true);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
restoreExpandedState(QModelIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
BookmarkItem* BookmarksTreeView::selectedBookmark() const
|
|
|
|
{
|
|
|
|
QList<BookmarkItem*> items = selectedBookmarks();
|
|
|
|
return items.count() == 1 ? items.first() : 0;
|
|
|
|
}
|
|
|
|
|
2014-02-08 18:14:20 +01:00
|
|
|
QList<BookmarkItem*> BookmarksTreeView::selectedBookmarks() const
|
|
|
|
{
|
|
|
|
QList<BookmarkItem*> items;
|
|
|
|
|
|
|
|
foreach (const QModelIndex &index, selectionModel()->selectedRows()) {
|
|
|
|
BookmarkItem* item = m_model->item(index);
|
|
|
|
items.append(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksTreeView::indexExpanded(const QModelIndex &parent)
|
|
|
|
{
|
|
|
|
BookmarkItem* item = m_model->item(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)
|
|
|
|
{
|
|
|
|
BookmarkItem* item = m_model->item(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::selectionChanged()
|
|
|
|
{
|
|
|
|
emit bookmarksSelected(selectedBookmarks());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksTreeView::createContextMenu(const QPoint &point)
|
|
|
|
{
|
2014-02-08 20:01:07 +01:00
|
|
|
emit contextMenuRequested(viewport()->mapToGlobal(point));
|
2014-02-08 18:14:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksTreeView::restoreExpandedState(const QModelIndex &parent)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < m_model->rowCount(parent); ++i) {
|
|
|
|
QModelIndex index = m_model->index(i, 0, parent);
|
|
|
|
BookmarkItem* item = m_model->item(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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-08 18:14:20 +01:00
|
|
|
|
2014-02-08 20:01:07 +01:00
|
|
|
if (index.isValid()) {
|
|
|
|
BookmarkItem* item = m_model->item(index);
|
|
|
|
Qt::MouseButtons buttons = event->buttons();
|
|
|
|
Qt::KeyboardModifiers modifiers = QApplication::keyboardModifiers();
|
|
|
|
|
|
|
|
if (buttons == Qt::LeftButton && modifiers == Qt::ShiftModifier) {
|
|
|
|
emit bookmarkShiftActivated(item);
|
|
|
|
}
|
|
|
|
else if (buttons == Qt::MiddleButton || modifiers == Qt::ControlModifier) {
|
|
|
|
emit bookmarkCtrlActivated(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()) {
|
|
|
|
BookmarkItem* item = m_model->item(index);
|
|
|
|
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) {
|
|
|
|
QModelIndex index = selectionModel()->selectedRows().first();
|
|
|
|
BookmarkItem* item = m_model->item(index);
|
|
|
|
|
|
|
|
switch (event->key()) {
|
|
|
|
case Qt::Key_Return:
|
|
|
|
case Qt::Key_Enter:
|
2014-02-08 18:28:01 +01:00
|
|
|
if (item->isFolder() && event->modifiers() == Qt::NoModifier) {
|
2014-02-08 18:14:20 +01:00
|
|
|
setExpanded(index, !isExpanded(index));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Qt::KeyboardModifiers modifiers = event->modifiers();
|
|
|
|
|
|
|
|
if (modifiers == Qt::NoModifier) {
|
|
|
|
emit bookmarkActivated(item);
|
|
|
|
}
|
|
|
|
else if (modifiers == Qt::ControlModifier) {
|
|
|
|
emit bookmarkCtrlActivated(item);
|
|
|
|
}
|
|
|
|
else if (modifiers == Qt::ShiftModifier) {
|
|
|
|
emit bookmarkShiftActivated(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|