From 2baa9ec45a3e7e169e73ee74c74c0954702ab882 Mon Sep 17 00:00:00 2001 From: Peter Penz Date: Fri, 8 Apr 2011 19:37:11 +0200 Subject: [PATCH] Optimize alignment of toolbar menu Try to align the toolbar menu within the Dolphin window to reduce the cases where the sub-menus overlap with the right screen border. --- src/dolphinmainwindow.cpp | 91 ++++++++++++++++++++++++++++++--------- src/dolphinmainwindow.h | 2 - 2 files changed, 71 insertions(+), 22 deletions(-) diff --git a/src/dolphinmainwindow.cpp b/src/dolphinmainwindow.cpp index 467373829..dbb46a239 100644 --- a/src/dolphinmainwindow.cpp +++ b/src/dolphinmainwindow.cpp @@ -90,12 +90,25 @@ #include #include +#include #include #include #include #include #include +/* + * Menu shown when pressing the configure-button in the toolbar. + */ +class ToolBarMenu : public KMenu +{ +public: + ToolBarMenu(QWidget* parent); + virtual ~ToolBarMenu(); +protected: + virtual void showEvent(QShowEvent* event); +}; + /* * Remembers the tab configuration if a tab has been closed. * Each closed tab can be restored by the menu @@ -123,7 +136,6 @@ DolphinMainWindow::DolphinMainWindow(int id) : m_settingsDialog(0), m_toolBarSpacer(0), m_openToolBarMenuButton(0), - m_toolBarMenu(), m_updateToolBarTimer(0), m_lastHandleUrlStatJob(0), m_searchDockIsTemporaryVisible(false) @@ -1312,24 +1324,14 @@ void DolphinMainWindow::openContextMenu(const KFileItem& item, delete contextMenu; } -void DolphinMainWindow::openToolBarMenu() -{ - const int height = m_openToolBarMenuButton->height(); - const QPoint pos = m_openToolBarMenuButton->mapToGlobal(QPoint(0, height)); - - m_toolBarMenu = new KMenu(m_openToolBarMenuButton); - m_toolBarMenu.data()->setAttribute(Qt::WA_DeleteOnClose); - connect(m_toolBarMenu.data(), SIGNAL(aboutToShow()), this, SLOT(updateToolBarMenu())); - - m_toolBarMenu.data()->exec(pos); -} - void DolphinMainWindow::updateToolBarMenu() { - KMenu* menu = m_toolBarMenu.data(); - if (!menu) { - return; - } + KMenu* menu = qobject_cast(sender()); + Q_ASSERT(menu); + + // All actions get cleared by KMenu::clear(). The sub-menus are deleted + // by connecting to the aboutToHide() signal from the parent-menu. + menu->clear(); const GeneralSettings* generalSettings = DolphinSettings::instance().generalSettings(); @@ -1380,6 +1382,7 @@ void DolphinMainWindow::updateToolBarMenu() // Add "Go" menu KMenu* goMenu = new KMenu(i18nc("@action:inmenu", "Go"), menu); + connect(menu, SIGNAL(aboutToHide()), goMenu, SLOT(deleteLater())); goMenu->addAction(ac->action(KStandardAction::name(KStandardAction::Back))); goMenu->addAction(ac->action(KStandardAction::name(KStandardAction::Forward))); goMenu->addAction(ac->action(KStandardAction::name(KStandardAction::Up))); @@ -1389,6 +1392,7 @@ void DolphinMainWindow::updateToolBarMenu() // Add "Tool" menu KMenu* toolsMenu = new KMenu(i18nc("@action:inmenu", "Tools"), menu); + connect(menu, SIGNAL(aboutToHide()), toolsMenu, SLOT(deleteLater())); toolsMenu->addAction(ac->action("show_filter_bar")); toolsMenu->addAction(ac->action("compare_files")); toolsMenu->addAction(ac->action("open_terminal")); @@ -1402,6 +1406,7 @@ void DolphinMainWindow::updateToolBarMenu() // Add "Help" menu KMenu* helpMenu = new KMenu(i18nc("@action:inmenu", "Help"), menu); + connect(menu, SIGNAL(aboutToHide()), helpMenu, SLOT(deleteLater())); helpMenu->addAction(ac->action(KStandardAction::name(KStandardAction::HelpContents))); helpMenu->addAction(ac->action(KStandardAction::name(KStandardAction::WhatsThis))); helpMenu->addAction(ac->action(KStandardAction::name(KStandardAction::AboutApp))); @@ -1977,10 +1982,13 @@ void DolphinMainWindow::createToolBarMenuButton() m_openToolBarMenuButton = new QToolButton(this); m_openToolBarMenuButton->setIcon(KIcon("configure")); + m_openToolBarMenuButton->setPopupMode(QToolButton::InstantPopup); + m_openToolBarMenuButton->setToolTip(i18nc("@info:tooltip", "Configure and control Dolphin")); - // Instead of using QPushButton::setMenu() the opening of the menu is done manually - // to prevent the "clutter" of the down-arrow drawn by the style. - connect(m_openToolBarMenuButton, SIGNAL(clicked()), this, SLOT(openToolBarMenu())); + KMenu* toolBarMenu = new ToolBarMenu(m_openToolBarMenuButton); + connect(toolBarMenu, SIGNAL(aboutToShow()), this, SLOT(updateToolBarMenu())); + + m_openToolBarMenuButton->setMenu(toolBarMenu); toolBar()->addWidget(m_toolBarSpacer); toolBar()->addWidget(m_openToolBarMenuButton); @@ -2223,4 +2231,47 @@ void DolphinMainWindow::UndoUiInterface::jobError(KIO::Job* job) } } +ToolBarMenu::ToolBarMenu(QWidget* parent) : + KMenu(parent) +{ +} + +ToolBarMenu::~ToolBarMenu() +{ +} + +void ToolBarMenu::showEvent(QShowEvent* event) +{ + KMenu::showEvent(event); + + // Adjust the position of the menu to be shown within the + // Dolphin window to reduce the cases that sub-menus might overlap + // the right screen border. + QPoint pos; + QWidget* button = parentWidget(); + if (layoutDirection() == Qt::RightToLeft) { + pos = button->mapToGlobal(QPoint(0, button->height())); + } else { + pos = button->mapToGlobal(QPoint(button->width(), button->height())); + pos.rx() -= width(); + } + + // Assure that the menu is not shown outside the screen boundaries and + // that it does not overlap with the parent button. + const QRect screen = QApplication::desktop()->screenGeometry(QCursor::pos()); + if (pos.x() < 0) { + pos.rx() = 0; + } else if (pos.x() + width() >= screen.width()) { + pos.rx() = screen.width() - width(); + } + + if (pos.y() < 0) { + pos.ry() = 0; + } else if (pos.y() + height() >= screen.height()) { + pos.ry() = button->mapToGlobal(QPoint(0, 0)).y() - height(); + } + + move(pos); +} + #include "dolphinmainwindow.moc" diff --git a/src/dolphinmainwindow.h b/src/dolphinmainwindow.h index 87f29e865..a85b1c87a 100644 --- a/src/dolphinmainwindow.h +++ b/src/dolphinmainwindow.h @@ -441,7 +441,6 @@ private slots: const KUrl& url, const QList& customActions); - void openToolBarMenu(); void updateToolBarMenu(); void updateToolBar(); void slotToolBarSpacerDeleted(); @@ -573,7 +572,6 @@ private: // Members for the toolbar menu that is shown when the menubar is hidden: QWidget* m_toolBarSpacer; QToolButton* m_openToolBarMenuButton; - QWeakPointer m_toolBarMenu; QTimer* m_updateToolBarTimer; KJob* m_lastHandleUrlStatJob; -- 2.47.3