Skip to content

Commit

Permalink
Tray: Refresh icon if theme changed
Browse files Browse the repository at this point in the history
If the icon theme changes, we should check if there is more suitable icon
name in the new theme.
  • Loading branch information
palinek committed May 20, 2016
1 parent d0311b3 commit abbe586
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/tray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ COPYRIGHT_HEADER*/
#include <QMessageBox>
#include <QApplication>
#include <QPersistentModelIndex>
#include <QDebug>

#include <NetworkManagerQt/Manager>
#include <NetworkManagerQt/WirelessDevice>

#include "icons.h"
#include "nmmodel.h"
#include "nmproxy.h"
#include "log.h"

#include "nmlist.h"
#include "connectioninfo.h"
Expand All @@ -49,6 +49,7 @@ class TrayPrivate
void primaryConnectionUpdate();
void setShown(QPersistentModelIndex const & index);
void updateIcon();
void refreshIcon();
void openCloseDialog(QDialog * dialog);

public:
Expand Down Expand Up @@ -141,7 +142,13 @@ void TrayPrivate::updateIcon()
return;

mIconCurrent = mIcon2Show;
QStringList icon_names;
refreshIcon();
}

void TrayPrivate::refreshIcon()
{
//Note: the icons::getIcon chooses the right icon from list of possible candidates
// -> we need to refresh the icon in case of icon theme change
mTrayIcon.setIcon(icons::getIcon(mIconCurrent));
}

Expand Down Expand Up @@ -186,6 +193,8 @@ Tray::Tray(QObject *parent/* = nullptr*/)
, this, &Tray::onAboutTriggered);
connect(d->mContextMenu.addAction(QIcon::fromTheme(QStringLiteral("application-exit")), Tray::tr("Quit")), &QAction::triggered
, this, &Tray::onQuitTriggered);
//for listening on the QEvent::ThemeChange (is delivered only to QWidget objects)
d->mContextMenu.installEventFilter(this);

d->mActEnableNetwork->setCheckable(true);
d->mActEnableWifi->setCheckable(true);
Expand Down Expand Up @@ -241,6 +250,14 @@ Tray::~Tray()
{
}

bool Tray::eventFilter(QObject * object, QEvent * event)
{
Q_ASSERT(&d->mContextMenu == object);
if (QEvent::ThemeChange == event->type())
d->refreshIcon();
return false;
}

void Tray::onAboutTriggered()
{
QMessageBox::about(nullptr, Tray::tr("%1 about").arg(QStringLiteral("nm-tray"))
Expand Down
3 changes: 3 additions & 0 deletions src/tray.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ Q_OBJECT
Tray(QObject *parent = nullptr);
~Tray();

protected:
virtual bool eventFilter(QObject * object, QEvent * event) override;

private Q_SLOTS:
//menu
void onAboutTriggered();
Expand Down

0 comments on commit abbe586

Please sign in to comment.