From 0e41fd0785b500ea3545721714c35810d2c50daa Mon Sep 17 00:00:00 2001 From: David Rosca Date: Mon, 8 Jan 2018 19:31:12 +0100 Subject: [PATCH] AbstractButtonInterface: Add support for hiding tool button --- src/lib/navigation/navigationbar.cpp | 38 ++++++++++++++----- .../navigation/navigationbartoolbutton.cpp | 8 ++++ src/lib/navigation/navigationbartoolbutton.h | 7 ++++ src/lib/tools/abstractbuttoninterface.cpp | 15 ++++++++ src/lib/tools/abstractbuttoninterface.h | 5 +++ 5 files changed, 64 insertions(+), 9 deletions(-) diff --git a/src/lib/navigation/navigationbar.cpp b/src/lib/navigation/navigationbar.cpp index 3160d3449..bc8d00309 100644 --- a/src/lib/navigation/navigationbar.cpp +++ b/src/lib/navigation/navigationbar.cpp @@ -307,10 +307,17 @@ void NavigationBar::addToolButton(AbstractButtonInterface *button) return; } + NavigationBarToolButton *toolButton = new NavigationBarToolButton(button, this); + connect(toolButton, &NavigationBarToolButton::visibilityChangeRequested, this, [=]() { + if (m_layout->indexOf(toolButton) != -1) { + toolButton->updateVisibility(); + } + }); + WidgetData data; data.id = button->id(); data.name = button->name(); - data.widget = new NavigationBarToolButton(button, this); + data.widget = toolButton; data.button = button; m_widgets[data.id] = data; @@ -406,7 +413,7 @@ void NavigationBar::aboutToShowToolsMenu() for (const WidgetData &data : qAsConst(m_widgets)) { AbstractButtonInterface *button = data.button; - if (button && !m_layoutIds.contains(data.id)) { + if (button && (!button->isVisible() || !m_layoutIds.contains(data.id))) { QString title = button->title(); if (!button->badgeText().isEmpty()) { title.append(QSL(" (%1)").arg(button->badgeText())); @@ -460,20 +467,28 @@ void NavigationBar::toolActionActivated() return; } - AbstractButtonInterface::ClickController c; - c.visualParent = buttonTools; - c.popupPosition = [=](const QSize &size) { + AbstractButtonInterface::ClickController *c = new AbstractButtonInterface::ClickController; + c->visualParent = buttonTools; + c->popupPosition = [=](const QSize &size) { QPoint pos = buttonTools->mapToGlobal(buttonTools->rect().bottomRight()); if (QApplication::isRightToLeft()) { pos.setX(pos.x() - buttonTools->rect().width()); } else { pos.setX(pos.x() - size.width()); } + c->popupOpened = true; return pos; }; - buttonTools->setDown(true); - emit data.button->clicked(&c); - buttonTools->setDown(false); + c->popupClosed = [=]() { + buttonTools->setDown(false); + delete c; + }; + emit data.button->clicked(c); + if (c->popupOpened) { + buttonTools->setDown(true); + } else { + c->popupClosed(); + } } void NavigationBar::loadSettings() @@ -531,7 +546,12 @@ void NavigationBar::reloadLayout() const WidgetData data = m_widgets.value(id); if (data.widget) { m_layout->addWidget(data.widget); - data.widget->show(); + NavigationBarToolButton *button = qobject_cast(data.widget); + if (button) { + button->updateVisibility(); + } else { + data.widget->show(); + } } } diff --git a/src/lib/navigation/navigationbartoolbutton.cpp b/src/lib/navigation/navigationbartoolbutton.cpp index dfe3c6258..6b89a7632 100644 --- a/src/lib/navigation/navigationbartoolbutton.cpp +++ b/src/lib/navigation/navigationbartoolbutton.cpp @@ -44,9 +44,15 @@ NavigationBarToolButton::NavigationBarToolButton(AbstractButtonInterface *button connect(button, &AbstractButtonInterface::activeChanged, this, &NavigationBarToolButton::updateIcon); connect(button, &AbstractButtonInterface::toolTipChanged, this, &NavigationBarToolButton::setToolTip); connect(button, &AbstractButtonInterface::badgeTextChanged, this, &NavigationBarToolButton::updateBadge); + connect(button, &AbstractButtonInterface::visibleChanged, this, &NavigationBarToolButton::visibilityChangeRequested); connect(this, &ToolButton::clicked, this, &NavigationBarToolButton::clicked); } +void NavigationBarToolButton::updateVisibility() +{ + setVisible(m_button->isVisible()); +} + void NavigationBarToolButton::clicked() { AbstractButtonInterface::ClickController *c = new AbstractButtonInterface::ClickController; @@ -68,6 +74,8 @@ void NavigationBarToolButton::clicked() emit m_button->clicked(c); if (c->popupOpened) { setDown(true); + } else { + c->popupClosed(); } } diff --git a/src/lib/navigation/navigationbartoolbutton.h b/src/lib/navigation/navigationbartoolbutton.h index 6c1642e3f..362429741 100644 --- a/src/lib/navigation/navigationbartoolbutton.h +++ b/src/lib/navigation/navigationbartoolbutton.h @@ -26,9 +26,16 @@ class AbstractButtonInterface; class QUPZILLA_EXPORT NavigationBarToolButton : public ToolButton { + Q_OBJECT + public: explicit NavigationBarToolButton(AbstractButtonInterface *button, QWidget *parent = nullptr); + void updateVisibility(); + +signals: + void visibilityChangeRequested(); + private: void clicked(); void updateIcon(); diff --git a/src/lib/tools/abstractbuttoninterface.cpp b/src/lib/tools/abstractbuttoninterface.cpp index a93ddb958..eb5c03c02 100644 --- a/src/lib/tools/abstractbuttoninterface.cpp +++ b/src/lib/tools/abstractbuttoninterface.cpp @@ -42,6 +42,21 @@ void AbstractButtonInterface::setActive(bool active) emit activeChanged(m_active); } +bool AbstractButtonInterface::isVisible() const +{ + return m_visible; +} + +void AbstractButtonInterface::setVisible(bool visible) +{ + if (m_visible == visible) { + return; + } + + m_visible = visible; + emit visibleChanged(m_visible); +} + QString AbstractButtonInterface::title() const { return m_title; diff --git a/src/lib/tools/abstractbuttoninterface.h b/src/lib/tools/abstractbuttoninterface.h index 107c8ef3d..6f4b39e01 100644 --- a/src/lib/tools/abstractbuttoninterface.h +++ b/src/lib/tools/abstractbuttoninterface.h @@ -48,6 +48,9 @@ public: bool isActive() const; void setActive(bool active); + bool isVisible() const; + void setVisible(bool visible); + QString title() const; void setTitle(const QString &text); @@ -65,6 +68,7 @@ public: signals: void activeChanged(bool active); + void visibleChanged(bool visible); void titleChanged(const QString &title); void toolTipChanged(const QString &toolTip); void iconChanged(const QIcon &icon); @@ -74,6 +78,7 @@ signals: private: bool m_active = true; + bool m_visible = true; QString m_title; QString m_toolTip; QIcon m_icon;