1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 01:22:10 +01:00

[ToolButton] Manually handle showing menu

ToolButtons now don't have menu indicators.
This fixes issue with Back/Forward buttons in Navigation Toolbar
being more wide than other buttons.
This commit is contained in:
David Rosca 2014-04-19 13:10:20 +02:00
parent 717585c6d6
commit b3a9b99ac7
6 changed files with 112 additions and 79 deletions

View File

@ -421,7 +421,7 @@ void BrowserWindow::loadSettings()
m_navigationToolbar->buttonReloadStop()->setVisible(showReloadButton);
m_navigationToolbar->buttonHome()->setVisible(showHomeButton);
m_navigationToolbar->buttonBack()->setVisible(showBackForwardButtons);
m_navigationToolbar->buttonNext()->setVisible(showBackForwardButtons);
m_navigationToolbar->buttonForward()->setVisible(showBackForwardButtons);
m_navigationToolbar->webSearchBar()->setVisible(showWebSearchBar);
m_navigationToolbar->buttonAddTab()->setVisible(showAddTabButton);

View File

@ -37,7 +37,7 @@
#include <QMouseEvent>
#include <QStyleOption>
static void setButtonIconSize(ToolButton* button)
static void setToolBarButtonIconSize(ToolButton* button)
{
QStyleOption opt;
opt.initFrom(button);
@ -63,26 +63,26 @@ NavigationBar::NavigationBar(BrowserWindow* window)
m_buttonBack->setAutoRaise(true);
m_buttonBack->setEnabled(false);
m_buttonBack->setFocusPolicy(Qt::NoFocus);
setButtonIconSize(m_buttonBack);
setToolBarButtonIconSize(m_buttonBack);
m_buttonNext = new ToolButton(this);
m_buttonNext->setObjectName("navigation-button-next");
m_buttonNext->setToolTip(tr("Forward"));
m_buttonNext->setToolButtonStyle(Qt::ToolButtonIconOnly);
m_buttonNext->setAutoRaise(true);
m_buttonNext->setEnabled(false);
m_buttonNext->setFocusPolicy(Qt::NoFocus);
setButtonIconSize(m_buttonNext);
m_buttonForward = new ToolButton(this);
m_buttonForward->setObjectName("navigation-button-next");
m_buttonForward->setToolTip(tr("Forward"));
m_buttonForward->setToolButtonStyle(Qt::ToolButtonIconOnly);
m_buttonForward->setAutoRaise(true);
m_buttonForward->setEnabled(false);
m_buttonForward->setFocusPolicy(Qt::NoFocus);
setToolBarButtonIconSize(m_buttonForward);
QHBoxLayout* backNextLayout = new QHBoxLayout();
backNextLayout->setContentsMargins(0, 0, 0, 0);
backNextLayout->setSpacing(0);
backNextLayout->addWidget(m_buttonBack);
backNextLayout->addWidget(m_buttonNext);
backNextLayout->addWidget(m_buttonForward);
m_reloadStop = new ReloadStopButton(this);
setButtonIconSize(m_reloadStop->buttonReload());
setButtonIconSize(m_reloadStop->buttonStop());
setToolBarButtonIconSize(m_reloadStop->buttonReload());
setToolBarButtonIconSize(m_reloadStop->buttonStop());
m_buttonHome = new ToolButton(this);
m_buttonHome->setObjectName("navigation-button-home");
@ -90,7 +90,7 @@ NavigationBar::NavigationBar(BrowserWindow* window)
m_buttonHome->setToolButtonStyle(Qt::ToolButtonIconOnly);
m_buttonHome->setAutoRaise(true);
m_buttonHome->setFocusPolicy(Qt::NoFocus);
setButtonIconSize(m_buttonHome);
setToolBarButtonIconSize(m_buttonHome);
m_buttonAddTab = new ToolButton(this);
m_buttonAddTab->setObjectName("navigation-button-addtab");
@ -98,14 +98,17 @@ NavigationBar::NavigationBar(BrowserWindow* window)
m_buttonAddTab->setToolButtonStyle(Qt::ToolButtonIconOnly);
m_buttonAddTab->setAutoRaise(true);
m_buttonAddTab->setFocusPolicy(Qt::NoFocus);
setButtonIconSize(m_buttonAddTab);
setToolBarButtonIconSize(m_buttonAddTab);
m_menuBack = new Menu(this);
m_menuBack->setCloseOnMiddleClick(true);
m_buttonBack->setMenu(m_menuBack);
connect(m_buttonBack, SIGNAL(aboutToShowMenu()), this, SLOT(aboutToShowHistoryBackMenu()));
m_menuForward = new Menu(this);
m_menuForward->setCloseOnMiddleClick(true);
m_buttonNext->setMenu(m_menuForward);
m_buttonForward->setMenu(m_menuForward);
connect(m_buttonForward, SIGNAL(aboutToShowMenu()), this, SLOT(aboutToShowHistoryNextMenu()));
#ifndef Q_OS_MAC
m_supMenu = new ToolButton(this);
@ -116,7 +119,7 @@ NavigationBar::NavigationBar(BrowserWindow* window)
m_supMenu->setFocusPolicy(Qt::NoFocus);
m_supMenu->setMenu(m_window->superMenu());
m_supMenu->setShowMenuInside(true);
setButtonIconSize(m_supMenu);
setToolBarButtonIconSize(m_supMenu);
#endif
m_searchLine = new WebSearchBar(m_window);
@ -135,7 +138,7 @@ NavigationBar::NavigationBar(BrowserWindow* window)
m_exitFullscreen->setFocusPolicy(Qt::NoFocus);
m_exitFullscreen->setAutoRaise(true);
m_exitFullscreen->setVisible(false);
setButtonIconSize(m_exitFullscreen);
setToolBarButtonIconSize(m_exitFullscreen);
m_layout->addLayout(backNextLayout);
m_layout->addWidget(m_reloadStop);
@ -150,17 +153,15 @@ NavigationBar::NavigationBar(BrowserWindow* window)
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuRequested(QPoint)));
connect(m_menuBack, SIGNAL(aboutToShow()), this, SLOT(aboutToShowHistoryBackMenu()));
connect(m_menuForward, SIGNAL(aboutToShow()), this, SLOT(aboutToShowHistoryNextMenu()));
connect(m_buttonBack, SIGNAL(clicked()), this, SLOT(goBack()));
connect(m_buttonBack, SIGNAL(middleMouseClicked()), this, SLOT(goBackInNewTab()));
connect(m_buttonBack, SIGNAL(controlClicked()), this, SLOT(goBackInNewTab()));
connect(m_buttonNext, SIGNAL(clicked()), this, SLOT(goForward()));
connect(m_buttonNext, SIGNAL(middleMouseClicked()), this, SLOT(goForwardInNewTab()));
connect(m_buttonNext, SIGNAL(controlClicked()), this, SLOT(goForwardInNewTab()));
connect(m_buttonForward, SIGNAL(clicked()), this, SLOT(goForward()));
connect(m_buttonForward, SIGNAL(middleMouseClicked()), this, SLOT(goForwardInNewTab()));
connect(m_buttonForward, SIGNAL(controlClicked()), this, SLOT(goForwardInNewTab()));
connect(m_reloadStop->buttonStop(), SIGNAL(clicked()), this, SLOT(stop()));
connect(m_reloadStop->buttonReload(), SIGNAL(clicked()), this, SLOT(reload()));
connect(m_reloadStop, SIGNAL(stopClicked()), this, SLOT(stop()));
connect(m_reloadStop, SIGNAL(reloadClicked()), this, SLOT(reload()));
connect(m_buttonHome, SIGNAL(clicked()), m_window, SLOT(goHome()));
connect(m_buttonHome, SIGNAL(middleMouseClicked()), m_window, SLOT(goHomeInNewTab()));
connect(m_buttonHome, SIGNAL(controlClicked()), m_window, SLOT(goHomeInNewTab()));
@ -337,7 +338,7 @@ void NavigationBar::refreshHistory()
QWebHistory* history = m_window->weView()->page()->history();
m_buttonBack->setEnabled(history->canGoBack());
m_buttonNext->setEnabled(history->canGoForward());
m_buttonForward->setEnabled(history->canGoForward());
}
void NavigationBar::stop()

View File

@ -48,7 +48,7 @@ public:
void showStopButton();
ToolButton* buttonBack() { return m_buttonBack; }
ToolButton* buttonNext() { return m_buttonNext; }
ToolButton* buttonForward() { return m_buttonForward; }
ToolButton* buttonHome() { return m_buttonHome; }
ToolButton* buttonAddTab() { return m_buttonAddTab; }
ToolButton* buttonExitFullscreen() { return m_exitFullscreen; }
@ -98,7 +98,7 @@ private:
QHBoxLayout* m_layout;
QSplitter* m_navigationSplitter;
ToolButton* m_buttonBack;
ToolButton* m_buttonNext;
ToolButton* m_buttonForward;
ToolButton* m_buttonHome;
ToolButton* m_buttonAddTab;
ToolButton* m_supMenu;

View File

@ -29,7 +29,7 @@ ButtonWithMenu::ButtonWithMenu(QWidget* parent)
setFocusPolicy(Qt::ClickFocus);
setMenu(m_menu);
connect(m_menu, SIGNAL(aboutToShow()), this, SLOT(generateMenu()));
connect(this, SIGNAL(aboutToShowMenu()), this, SLOT(generateMenu()));
}
void ButtonWithMenu::setCurrentItem()

View File

@ -17,21 +17,30 @@
* ============================================================ */
#include "toolbutton.h"
#include <QMouseEvent>
#include <QMenu>
#include <QStyle>
#include <QPainter>
#include <QMouseEvent>
#include <QApplication>
#include <QStyleOptionToolButton>
ToolButton::ToolButton(QWidget* parent)
: QToolButton(parent)
, m_menu(0)
, m_usingMultiIcon(false)
, m_showMenuInside(false)
{
setMinimumWidth(16);
QStyleOptionToolButton opt;
initStyleOption(&opt);
m_pressTimer.setSingleShot(true);
m_pressTimer.setInterval(QApplication::style()->styleHint(QStyle::SH_ToolButton_PopupDelay, &opt, this));
connect(&m_pressTimer, SIGNAL(timeout()), this, SLOT(showMenu()));
}
QPixmap ToolButton::pixmap() const
QPixmap ToolButton::multiIcon() const
{
return m_normalIcon;
}
@ -61,6 +70,11 @@ void ToolButton::setThemeIcon(const QString &icon)
setIcon(QIcon::fromTheme(m_themeIcon));
}
QIcon ToolButton::icon() const
{
return m_usingMultiIcon ? multiIcon() : QToolButton::icon();
}
void ToolButton::setIcon(const QIcon &icon)
{
if (m_usingMultiIcon)
@ -70,15 +84,20 @@ void ToolButton::setIcon(const QIcon &icon)
QToolButton::setIcon(icon);
}
QMenu* ToolButton::menu() const
{
return m_menu;
}
void ToolButton::setMenu(QMenu* menu)
{
Q_ASSERT(menu);
if (QToolButton::menu())
disconnect(QToolButton::menu(), SIGNAL(aboutToHide()), this, SLOT(menuAboutToHide()));
if (m_menu)
disconnect(m_menu, SIGNAL(aboutToHide()), this, SLOT(menuAboutToHide()));
connect(menu, SIGNAL(aboutToHide()), this, SLOT(menuAboutToHide()));
QToolButton::setMenu(menu);
m_menu = menu;
connect(m_menu, SIGNAL(aboutToHide()), this, SLOT(menuAboutToHide()));
}
bool ToolButton::showMenuInside() const
@ -94,46 +113,71 @@ void ToolButton::setShowMenuInside(bool inside)
void ToolButton::menuAboutToHide()
{
setDown(false);
emit aboutToHideMenu();
}
void ToolButton::showMenu()
{
if (!m_menu)
return;
emit aboutToShowMenu();
QPoint pos;
if (m_showMenuInside) {
pos = mapToGlobal(rect().bottomRight());
if (QApplication::layoutDirection() == Qt::RightToLeft)
pos.setX(pos.x() - rect().width());
else
pos.setX(pos.x() - m_menu->sizeHint().width());
}
else {
pos = mapToGlobal(rect().bottomLeft());
}
m_menu->popup(pos);
}
void ToolButton::mousePressEvent(QMouseEvent* e)
{
QToolButton::mousePressEvent(e);
if (popupMode() == QToolButton::DelayedPopup)
m_pressTimer.start();
if (e->buttons() == Qt::LeftButton && menu() && popupMode() == QToolButton::InstantPopup) {
setDown(true);
showMenu();
return;
}
if (e->buttons() == Qt::RightButton && menu()) {
else if (e->buttons() == Qt::RightButton && menu()) {
setDown(true);
showMenu();
return;
}
QToolButton::mousePressEvent(e);
}
void ToolButton::mouseReleaseEvent(QMouseEvent* e)
{
QToolButton::mouseReleaseEvent(e);
m_pressTimer.stop();
if (e->button() == Qt::MiddleButton && rect().contains(e->pos())) {
emit middleMouseClicked();
setDown(false);
return;
}
if (e->button() == Qt::LeftButton && rect().contains(e->pos()) && e->modifiers() == Qt::ControlModifier) {
else if (e->button() == Qt::LeftButton && rect().contains(e->pos()) && e->modifiers() == Qt::ControlModifier) {
emit controlClicked();
setDown(false);
return;
}
QToolButton::mouseReleaseEvent(e);
}
void ToolButton::mouseDoubleClickEvent(QMouseEvent* e)
{
QToolButton::mouseDoubleClickEvent(e);
m_pressTimer.stop();
if (e->buttons() == Qt::LeftButton) {
emit doubleClicked();
}
@ -157,24 +201,3 @@ void ToolButton::paintEvent(QPaintEvent* e)
else
p.drawPixmap(0, 0, m_normalIcon);
}
void ToolButton::showMenu()
{
if (!m_showMenuInside) {
QToolButton::showMenu();
return;
}
QMenu* m = menu();
if (!m)
return;
emit aboutToShowMenu();
QPoint pos = mapToGlobal(rect().bottomRight());
if (QApplication::layoutDirection() == Qt::RightToLeft)
pos.setX(pos.x() - rect().width());
else
pos.setX(pos.x() - m->sizeHint().width());
m->popup(pos);
}

View File

@ -19,7 +19,7 @@
#define TOOLBUTTON_H
#include <QToolButton>
#include <QVariant>
#include <QTimer>
#include "qzcommon.h"
@ -30,7 +30,7 @@ class QUPZILLA_EXPORT ToolButton : public QToolButton
Q_PROPERTY(QSize fixedsize READ size WRITE setFixedSize)
Q_PROPERTY(int fixedwidth READ width WRITE setFixedWidth)
Q_PROPERTY(int fixedheight READ height WRITE setFixedHeight)
Q_PROPERTY(QPixmap multiIcon READ pixmap WRITE setMultiIcon)
Q_PROPERTY(QPixmap multiIcon READ multiIcon WRITE setMultiIcon)
Q_PROPERTY(QIcon icon READ icon WRITE setIcon)
Q_PROPERTY(QString themeIcon READ themeIcon WRITE setThemeIcon)
@ -38,16 +38,24 @@ public:
explicit ToolButton(QWidget* parent = 0);
// MultiIcon - Image containing pixmaps for all button states
QPixmap pixmap() const;
QPixmap multiIcon() const;
void setMultiIcon(const QPixmap &icon);
// ThemeIcon - Standard QToolButton with theme icon
QString themeIcon() const;
void setThemeIcon(const QString &icon);
// Icon - Standard QToolButton with icon
QIcon icon() const;
void setIcon(const QIcon &icon);
// Menu - Menu is handled in ToolButton and is not passed to QToolButton
// There won't be menu indicator shown in the button
// QToolButton::MenuButtonPopup is not supported
QMenu* menu() const;
void setMenu(QMenu* menu);
// Align the right corner of menu to the right corner of button
bool showMenuInside() const;
void setShowMenuInside(bool inside);
@ -56,11 +64,13 @@ signals:
void controlClicked();
void doubleClicked();
// Emitted when showMenuInside is true
// It is needed to use these signals with showMenuInsied
void aboutToShowMenu();
void aboutToHideMenu();
private slots:
void menuAboutToHide();
void showMenu();
protected:
void mousePressEvent(QMouseEvent* e);
@ -69,17 +79,16 @@ protected:
void paintEvent(QPaintEvent* e);
private:
void showMenu();
bool m_usingMultiIcon;
bool m_showMenuInside;
QPixmap m_normalIcon;
QPixmap m_hoverIcon;
QPixmap m_activeIcon;
QPixmap m_disabledIcon;
QString m_themeIcon;
QTimer m_pressTimer;
QMenu* m_menu;
bool m_usingMultiIcon;
bool m_showMenuInside;
};
#endif // TOOLBUTTON_H