Falkon Develop
Cross-platform Qt-based web browser
navigationbartoolbutton.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2018 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
20
21#include <QLabel>
22#include <QMouseEvent>
23#include <QApplication>
24
26 : ToolButton(parent)
27 , m_button(button)
28{
29 setAutoRaise(true);
31 setFocusPolicy(Qt::NoFocus);
32
33 m_badgeLabel = new QLabel(this);
34 m_badgeLabel->setObjectName(QSL("navigation-toolbutton-badge"));
35 QFont f = m_badgeLabel->font();
36 f.setPixelSize(m_badgeLabel->height() / 2.5);
37 m_badgeLabel->setFont(f);
38 m_badgeLabel->hide();
39
40 setToolTip(button->toolTip());
41 updateIcon();
42 updateBadge();
43
44 connect(button, &AbstractButtonInterface::iconChanged, this, &NavigationBarToolButton::updateIcon);
45 connect(button, &AbstractButtonInterface::activeChanged, this, &NavigationBarToolButton::updateIcon);
46 connect(button, &AbstractButtonInterface::toolTipChanged, this, &NavigationBarToolButton::setToolTip);
47 connect(button, &AbstractButtonInterface::badgeTextChanged, this, &NavigationBarToolButton::updateBadge);
49}
50
52{
53 setVisible(m_button->isVisible());
54}
55
56void NavigationBarToolButton::clicked()
57{
59 c->visualParent = this;
60 c->popupPosition = [=](const QSize &size) {
61 QPoint pos = mapToGlobal(rect().bottomRight());
62 if (QApplication::isRightToLeft()) {
63 pos.setX(pos.x() - rect().width());
64 } else {
65 pos.setX(pos.x() - size.width());
66 }
67 c->popupOpened = true;
68 return pos;
69 };
70 c->popupClosed = [=]() {
71 setDown(false);
72 delete c;
73 };
74 Q_EMIT m_button->clicked(c);
75 if (c->popupOpened) {
76 setDown(true);
77 } else {
78 c->popupClosed();
79 }
80}
81
82void NavigationBarToolButton::updateIcon()
83{
84 const QIcon::Mode mode = m_button->isActive() ? QIcon::Normal : QIcon::Disabled;
85 const QImage img = m_button->icon().pixmap(iconSize(), mode).toImage();
86 setIcon(QPixmap::fromImage(img, Qt::MonoOnly));
87}
88
89void NavigationBarToolButton::updateBadge()
90{
91 if (m_button->badgeText().isEmpty()) {
92 m_badgeLabel->hide();
93 } else {
94 m_badgeLabel->setText(m_button->badgeText());
95 m_badgeLabel->resize(m_badgeLabel->sizeHint());
96 m_badgeLabel->move(width() - m_badgeLabel->width(), 0);
97 m_badgeLabel->show();
98 }
99}
100
101void NavigationBarToolButton::mouseReleaseEvent(QMouseEvent *e)
102{
103 // Prevent flickering due to mouse release event restoring Down state
104
105 bool popupOpened = false;
106
107 if (e->button() == Qt::LeftButton && rect().contains(e->pos())) {
108 clicked();
109 popupOpened = isDown();
110 }
111
112 if (popupOpened) {
113 setUpdatesEnabled(false);
114 }
115
117
118 if (popupOpened) {
119 setDown(true);
120 setUpdatesEnabled(true);
121 }
122}
void iconChanged(const QIcon &icon)
void clicked(AbstractButtonInterface::ClickController *controller)
void badgeTextChanged(const QString &badgeText)
void activeChanged(bool active)
void visibleChanged(bool visible)
void toolTipChanged(const QString &toolTip)
NavigationBarToolButton(AbstractButtonInterface *button, QWidget *parent=nullptr)
void mouseReleaseEvent(QMouseEvent *e) override
Definition: toolbutton.cpp:203
void setToolbarButtonLook(bool enable)
Definition: toolbutton.cpp:138
void setIcon(const QIcon &icon)
Definition: toolbutton.cpp:85
#define QSL(x)
Definition: qzcommon.h:40