1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +01:00

StatusBar: Add support for adding new buttons

This commit is contained in:
David Rosca 2018-01-24 19:44:36 +01:00
parent 444884bee9
commit 24fa384d23
2 changed files with 113 additions and 0 deletions

View File

@ -24,6 +24,8 @@
#include "webpage.h"
#include "proxystyle.h"
#include "qztools.h"
#include "abstractbuttoninterface.h"
#include "clickablelabel.h"
#include <QStyleOptionFrame>
#include <QStatusBar>
@ -31,6 +33,78 @@
#include <QStylePainter>
#include <QTimer>
class StatusBarButton : public ClickableLabel
{
public:
explicit StatusBarButton(AbstractButtonInterface *button, QWidget *parent = nullptr);
private:
void clicked();
void updateIcon();
void updateToolTip();
AbstractButtonInterface *m_button;
};
StatusBarButton::StatusBarButton(AbstractButtonInterface *button, QWidget *parent)
: ClickableLabel(parent)
, m_button(button)
{
setFixedSize(16, 16);
setCursor(Qt::PointingHandCursor);
updateIcon();
updateToolTip();
setVisible(m_button->isVisible());
connect(this, &ClickableLabel::clicked, this, &StatusBarButton::clicked);
connect(m_button, &AbstractButtonInterface::iconChanged, this, &StatusBarButton::updateIcon);
connect(m_button, &AbstractButtonInterface::activeChanged, this, &StatusBarButton::updateIcon);
connect(m_button, &AbstractButtonInterface::toolTipChanged, this, &StatusBarButton::updateToolTip);
connect(m_button, &AbstractButtonInterface::badgeTextChanged, this, &StatusBarButton::updateToolTip);
connect(m_button, &AbstractButtonInterface::visibleChanged, this, &StatusBarButton::setVisible);
}
void StatusBarButton::clicked()
{
AbstractButtonInterface::ClickController *c = new AbstractButtonInterface::ClickController;
c->visualParent = this;
c->popupPosition = [=](const QSize &size) {
QPoint pos = mapToGlobal(rect().topRight());
if (QApplication::isRightToLeft()) {
pos.setX(pos.x() - rect().width());
} else {
pos.setX(pos.x() - size.width());
}
pos.setY(pos.y() - size.height());
c->popupOpened = true;
return pos;
};
c->popupClosed = [=]() {
delete c;
};
emit m_button->clicked(c);
if (!c->popupOpened) {
c->popupClosed();
}
}
void StatusBarButton::updateIcon()
{
const QIcon::Mode mode = m_button->isActive() ? QIcon::Normal : QIcon::Disabled;
const QImage img = m_button->icon().pixmap(size(), mode).toImage();
setPixmap(QPixmap::fromImage(img, Qt::MonoOnly));
}
void StatusBarButton::updateToolTip()
{
QString text = m_button->toolTip();
if (!m_button->badgeText().isEmpty()) {
text.append(QSL(" (%1)").arg(m_button->badgeText()));
}
setToolTip(text);
}
TipLabel::TipLabel(QWidget* parent)
: SqueezeLabelV1(parent)
{
@ -150,3 +224,30 @@ void StatusBar::clearMessage()
QStatusBar::clearMessage();
m_statusBarText->hideDelayed();
}
void StatusBar::addButton(AbstractButtonInterface *button)
{
if (!button || !button->isValid()) {
return;
}
StatusBarButton *widget = new StatusBarButton(button, this);
widget->setProperty("button-id", button->id());
WidgetData data;
data.id = button->id();
data.widget = widget;
data.button = button;
m_widgets[data.id] = data;
addPermanentWidget(widget);
}
void StatusBar::removeButton(AbstractButtonInterface *button)
{
if (!button || !m_widgets.contains(button->id())) {
return;
}
delete m_widgets.take(button->id()).widget;
}

View File

@ -26,6 +26,7 @@
class QTimer;
class BrowserWindow;
class AbstractButtonInterface;
class FALKON_EXPORT TipLabel : public SqueezeLabelV1
{
@ -52,7 +53,18 @@ public:
void showMessage(const QString &message, int timeout = 0);
void clearMessage();
void addButton(AbstractButtonInterface *button);
void removeButton(AbstractButtonInterface *button);
private:
BrowserWindow *m_window;
TipLabel *m_statusBarText;
struct WidgetData {
QString id;
QWidget *widget = nullptr;
AbstractButtonInterface *button = nullptr;
};
QHash<QString, WidgetData> m_widgets;
};