From 6551c000fc68134932cdc21f2cc7086b34bff30b Mon Sep 17 00:00:00 2001 From: Peter Penz Date: Sat, 8 Oct 2011 21:40:34 +0200 Subject: Context menu cleanups - Open the context menu on the mouse-press event, not on the mouse-release event. - Provide an explicit position-information and don't use QCursor::pos(). This fixes the issue that opening a context-menu by the keyboard opens below the cursor. - Provide different signals in the KItemListController for the different context-menu types (item vs. view vs. header). - Implement turning on/off roles by the header-context-menu. --- src/views/dolphinview.cpp | 97 +++++++++++++++++++++++++++-------------------- 1 file changed, 55 insertions(+), 42 deletions(-) (limited to 'src/views/dolphinview.cpp') diff --git a/src/views/dolphinview.cpp b/src/views/dolphinview.cpp index a82008d25..83bf3a678 100644 --- a/src/views/dolphinview.cpp +++ b/src/views/dolphinview.cpp @@ -170,7 +170,9 @@ DolphinView::DolphinView(const KUrl& url, QWidget* parent) : connect(controller, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int))); connect(controller, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int))); - connect(controller, SIGNAL(contextMenuRequested(int,QPointF)), this, SLOT(slotContextMenuRequested(int,QPointF))); + connect(controller, SIGNAL(itemContextMenuRequested(int,QPointF)), this, SLOT(slotItemContextMenuRequested(int,QPointF))); + connect(controller, SIGNAL(viewContextMenuRequested(QPointF)), this, SLOT(slotViewContextMenuRequested(QPointF))); + connect(controller, SIGNAL(headerContextMenuRequested(QPointF)), this, SLOT(slotHeaderContextMenuRequested(QPointF))); connect(controller, SIGNAL(itemExpansionToggleClicked(int)), this, SLOT(slotItemExpansionToggleClicked(int))); connect(controller, SIGNAL(itemHovered(int)), this, SLOT(slotItemHovered(int))); connect(controller, SIGNAL(itemUnhovered(int)), this, SLOT(slotItemUnhovered(int))); @@ -699,32 +701,6 @@ void DolphinView::mouseReleaseEvent(QMouseEvent* event) setActive(true); } -void DolphinView::contextMenuEvent(QContextMenuEvent* event) -{ - Q_UNUSED(event); - - const QPoint pos = m_container->mapFromGlobal(QCursor::pos()); - const KItemListView* view = m_container->controller()->view(); - if (view->itemAt(pos) < 0) { - // Only open the context-menu if the cursor is above the viewport - // (the context-menu for items is handled in slotContextMenuRequested()) - requestContextMenu(KFileItem(), url(), QList()); - } -} - -void DolphinView::wheelEvent(QWheelEvent* event) -{ - if (event->modifiers().testFlag(Qt::ControlModifier)) { - const int numDegrees = event->delta() / 8; - const int numSteps = numDegrees / 15; - - setZoomLevel(zoomLevel() + numSteps); - event->accept(); - } else { - event->ignore(); - } -} - void DolphinView::activate() { setActive(true); @@ -760,14 +736,63 @@ void DolphinView::slotItemMiddleClicked(int index) } } -void DolphinView::slotContextMenuRequested(int index, const QPointF& pos) +void DolphinView::slotItemContextMenuRequested(int index, const QPointF& pos) { - Q_UNUSED(pos); if (GeneralSettings::showToolTips()) { m_toolTipManager->hideToolTip(); } const KFileItem item = fileItemModel()->fileItem(index); - emit requestContextMenu(item, url(), QList()); + emit requestContextMenu(pos.toPoint(), item, url(), QList()); +} + +void DolphinView::slotViewContextMenuRequested(const QPointF& pos) +{ + if (GeneralSettings::showToolTips()) { + m_toolTipManager->hideToolTip(); + } + emit requestContextMenu(pos.toPoint(), KFileItem(), url(), QList()); +} + +void DolphinView::slotHeaderContextMenuRequested(const QPointF& pos) +{ + QWeakPointer menu = new KMenu(QApplication::activeWindow()); + + KItemListView* view = m_container->controller()->view(); + const QSet visibleRolesSet = view->visibleRoles().toSet(); + + // Add all roles to the menu that can be shown or hidden by the user + const AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance(); + const QList keys = infoAccessor.keys(); + foreach (const DolphinView::AdditionalInfo info, keys) { + const QByteArray& role = infoAccessor.role(info); + if (role != "name") { + const QString text = fileItemModel()->roleDescription(role); + + QAction* action = menu.data()->addAction(text); + action->setCheckable(true); + action->setChecked(visibleRolesSet.contains(role)); + action->setData(info); + } + } + + QAction* action = menu.data()->exec(pos.toPoint()); + if (action) { + // Show or hide the selected role + const DolphinView::AdditionalInfo info = + static_cast(action->data().toInt()); + + const QByteArray selectedRole = infoAccessor.role(info); + QList visibleRoles = view->visibleRoles(); + if (action->isChecked()) { + const int index = keys.indexOf(info); + visibleRoles.insert(index + 1, selectedRole); + } else { + visibleRoles.removeOne(selectedRole); + } + view->setVisibleRoles(visibleRoles); + } + + delete menu.data(); } void DolphinView::slotItemExpansionToggleClicked(int index) @@ -851,18 +876,6 @@ void DolphinView::emitSelectionChangedSignal() emit selectionChanged(selectedItems()); } -void DolphinView::openContextMenu(const QPoint& pos, - const QList& customActions) -{ - KFileItem item; - const int index = m_container->controller()->view()->itemAt(pos); - if (index >= 0) { - item = fileItemModel()->fileItem(index); - } - - emit requestContextMenu(item, url(), customActions); -} - void DolphinView::dropUrls(const KFileItem& destItem, const KUrl& destPath, QDropEvent* event) -- cgit v1.3