Add KDE status notifier icon

This commit is contained in:
Ximi1970
2020-06-03 17:54:47 +02:00
parent 3f8043e184
commit 7d216ef0df
6 changed files with 789 additions and 3 deletions

View File

@@ -1,5 +1,14 @@
#<script>
Notifier
https://stackoverflow.com/questions/52935526/kstatusnotifieritem-instantiation-crashes-qt
kstatusnotifieritem.h => knotifications-devel
Installers

View File

@@ -4,7 +4,7 @@
#
#-------------------------------------------------
QT += core gui
QT += core gui dbus KNotifications
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
@@ -154,7 +154,8 @@ SOURCES += \
debugwidget.cpp \
preferencesdialog.cpp \
preferences.cpp \
windowctrl.cpp
windowctrl.cpp \
systrayxstatusnotifier.cpp
unix: {
SOURCES += \
windowctrl-unix.cpp
@@ -171,7 +172,8 @@ HEADERS += \
debugwidget.h \
preferencesdialog.h \
preferences.h \
windowctrl.h
windowctrl.h \
systrayxstatusnotifier.h
unix: {
HEADERS += \
windowctrl-unix.h

View File

@@ -7,6 +7,7 @@
#include "preferencesdialog.h"
#include "systrayxlink.h"
#include "systrayxicon.h"
#include "systrayxstatusnotifier.h"
#include "windowctrl.h"
/*
@@ -32,6 +33,7 @@ SysTrayX::SysTrayX( QObject *parent ) : QObject( parent )
* Initialize
*/
m_tray_icon = nullptr;
m_tray_icon2 = nullptr;
/*
* Setup preferences storage
@@ -70,6 +72,7 @@ SysTrayX::SysTrayX( QObject *parent ) : QObject( parent )
*/
createMenu();
showTrayIcon();
showTrayIconKDE();
/*
* Setup debug window
@@ -285,6 +288,83 @@ void SysTrayX::hideTrayIcon()
}
/*
* Show / create tray icon
*/
void SysTrayX::showTrayIconKDE()
{
if( !m_tray_icon2 )
{
/*
* Create system tray icon
*/
m_tray_icon2 = new SysTrayXStatusNotifier( m_link, m_preferences );
m_tray_icon2->setStandardActionsEnabled( false );
m_tray_icon2->setContextMenu( m_tray_icon_menu );
/*
* Set default icon
*/
m_tray_icon2->setDefaultIconMime( m_preferences->getDefaultIconMime() );
m_tray_icon2->setDefaultIconData( m_preferences->getDefaultIconData() );
m_tray_icon2->setDefaultIconType( m_preferences->getDefaultIconType() );
/*
* Set icon
*/
m_tray_icon2->setIconMime( m_preferences->getIconMime() );
m_tray_icon2->setIconData( m_preferences->getIconData() );
m_tray_icon2->setIconType( m_preferences->getIconType() );
/*
* Connect the world
*/
connect( m_tray_icon2, &SysTrayXStatusNotifier::signalShowHide, m_win_ctrl, &WindowCtrl::slotShowHide );
connect( m_preferences, &Preferences::signalDefaultIconTypeChange, m_tray_icon2, &SysTrayXStatusNotifier::slotDefaultIconTypeChange );
connect( m_preferences, &Preferences::signalDefaultIconDataChange, m_tray_icon2, &SysTrayXStatusNotifier::slotDefaultIconDataChange );
connect( m_preferences, &Preferences::signalIconTypeChange, m_tray_icon2, &SysTrayXStatusNotifier::slotIconTypeChange );
connect( m_preferences, &Preferences::signalIconDataChange, m_tray_icon2, &SysTrayXStatusNotifier::slotIconDataChange );
connect( m_preferences, &Preferences::signalShowNumberChange, m_tray_icon2, &SysTrayXStatusNotifier::slotShowNumberChange );
connect( m_preferences, &Preferences::signalNumberColorChange, m_tray_icon2, &SysTrayXStatusNotifier::slotNumberColorChange );
connect( m_preferences, &Preferences::signalNumberSizeChange, m_tray_icon2, &SysTrayXStatusNotifier::slotNumberSizeChange );
connect( m_link, &SysTrayXLink::signalUnreadMail, m_tray_icon2, &SysTrayXStatusNotifier::slotSetUnreadMail );
}
}
/*
* Hide / remove tray icon
*/
void SysTrayX::hideTrayIconKDE()
{
if( m_tray_icon2 )
{
/*
* Disconnect all signals
*/
disconnect( m_tray_icon2, &SysTrayXStatusNotifier::signalShowHide, m_win_ctrl, &WindowCtrl::slotShowHide );
disconnect( m_preferences, &Preferences::signalDefaultIconTypeChange, m_tray_icon2, &SysTrayXStatusNotifier::slotDefaultIconTypeChange );
disconnect( m_preferences, &Preferences::signalDefaultIconDataChange, m_tray_icon2, &SysTrayXStatusNotifier::slotDefaultIconDataChange );
disconnect( m_preferences, &Preferences::signalIconTypeChange, m_tray_icon2, &SysTrayXStatusNotifier::slotIconTypeChange );
disconnect( m_preferences, &Preferences::signalIconDataChange, m_tray_icon2, &SysTrayXStatusNotifier::slotIconDataChange );
disconnect( m_preferences, &Preferences::signalShowNumberChange, m_tray_icon2, &SysTrayXStatusNotifier::slotShowNumberChange );
disconnect( m_preferences, &Preferences::signalNumberColorChange, m_tray_icon2, &SysTrayXStatusNotifier::slotNumberColorChange );
disconnect( m_preferences, &Preferences::signalNumberSizeChange, m_tray_icon2, &SysTrayXStatusNotifier::slotNumberSizeChange );
disconnect( m_link, &SysTrayXLink::signalUnreadMail, m_tray_icon2, &SysTrayXStatusNotifier::slotSetUnreadMail );
/*
* Remove the notifier icon
*/
delete m_tray_icon2;
m_tray_icon2 = nullptr;
}
}
/*
* Handle icon show signal
*/

View File

@@ -25,6 +25,8 @@ class SysTrayXIcon;
class SysTrayXLink;
class WindowCtrl;
class SysTrayXStatusNotifier;
/**
* @brief The SysTrayX class
*/
@@ -67,6 +69,16 @@ class SysTrayX : public QObject
*/
void hideTrayIcon();
/**
* @brief showTrayIconKDE. Create and show the KDE icon.
*/
void showTrayIconKDE();
/**
* @brief hideTrayIcon. Hide and destroy the KDE icon.
*/
void hideTrayIconKDE();
signals:
/**
@@ -152,6 +164,11 @@ class SysTrayX : public QObject
*/
SysTrayXIcon* m_tray_icon;
/**
* @brief m_tray_icon2. Pointer to the KDE system tray icon.
*/
SysTrayXStatusNotifier* m_tray_icon2;
/**
* @brief m_tray_icon_menu. Pointer to the tray icon menu.
*/

View File

@@ -0,0 +1,434 @@
#include "systrayxstatusnotifier.h"
/*
* Local includes
*/
#include "preferences.h"
/*
* System includes
*/
#include "systrayxlink.h"
/*
* Qt includes
*/
#include <QIcon>
#include <QPixmap>
#include <QPainter>
/*
* Constructor
*/
SysTrayXStatusNotifier::SysTrayXStatusNotifier( SysTrayXLink* link, Preferences* pref, QObject* parent )
: KStatusNotifierItem( parent )
{
/*
* Initialize
*/
m_link = link;
m_pref = pref;
m_unread_mail = 0;
m_show_number = m_pref->getShowNumber();
m_number_color = m_pref->getNumberColor();
m_number_size = m_pref->getNumberSize();
/*
* Setup notifier
*/
setCategory( KStatusNotifierItem::ApplicationStatus );
setIconByPixmap( QIcon( QPixmap( ":/files/icons/Thunderbird.png") ) );
setTitle("SysTray-X");
// setStatus(KStatusNotifierItem::ItemStatus::Passive);
setStatus(KStatusNotifierItem::ItemStatus::Active);
// setStatus(KStatusNotifierItem::ItemStatus::NeedsAttention);
connect( this, &KStatusNotifierItem::activateRequested, this, &SysTrayXStatusNotifier::slotActivateRequested );
connect( this, &KStatusNotifierItem::secondaryActivateRequested, this, &SysTrayXStatusNotifier::slotSecondaryActivateRequested );
}
/*
* Set the default icon type
*/
void SysTrayXStatusNotifier::setDefaultIconType( Preferences::DefaultIconType icon_type )
{
if( m_default_icon_type != icon_type )
{
/*
* Store the new value
*/
m_default_icon_type = icon_type;
/*
* Render and set a new icon in the tray
*/
renderIcon();
}
}
/*
* Set the default icon mime
*/
void SysTrayXStatusNotifier::setDefaultIconMime( const QString& icon_mime )
{
if( m_default_icon_mime != icon_mime )
{
/*
* Store the new value
*/
m_default_icon_mime = icon_mime;
}
}
/*
* Set the default icon data
*/
void SysTrayXStatusNotifier::setDefaultIconData( const QByteArray& icon_data )
{
if( m_default_icon_data != icon_data )
{
/*
* Store the new value
*/
m_default_icon_data = icon_data;
/*
* Render and set a new icon in the tray
*/
renderIcon();
}
}
/*
* Set the icon type
*/
void SysTrayXStatusNotifier::setIconType( Preferences::IconType icon_type )
{
if( icon_type != m_icon_type )
{
/*
* Store the new value
*/
m_icon_type = icon_type;
/*
* Render and set a new icon in the tray
*/
renderIcon();
}
}
/*
* Set the icon mime
*/
void SysTrayXStatusNotifier::setIconMime( const QString& icon_mime )
{
if( m_icon_mime != icon_mime )
{
/*
* Store the new value
*/
m_icon_mime = icon_mime;
}
}
/*
* Set the icon data
*/
void SysTrayXStatusNotifier::setIconData( const QByteArray& icon_data )
{
if( m_icon_data != icon_data )
{
/*
* Store the new value
*/
m_icon_data = icon_data;
/*
* Render and set a new icon in the tray
*/
renderIcon();
}
}
/*
* Enable/disable number
*/
void SysTrayXStatusNotifier::showNumber( bool state )
{
if( m_show_number != state )
{
/*
* Store the new value
*/
m_show_number = state;
/*
* Render and set a new icon in the tray
*/
renderIcon();
}
}
/*
* Set number color
*/
void SysTrayXStatusNotifier::setNumberColor( const QString& color )
{
if( m_number_color != color )
{
/*
* Store the new value
*/
m_number_color = color;
/*
* Render and set a new icon in the tray
*/
renderIcon();
}
}
/*
* Set number size
*/
void SysTrayXStatusNotifier::setNumberSize( int size )
{
if( m_number_size != size )
{
/*
* Store the new value
*/
m_number_size = size;
/*
* Render and set a new icon in the tray
*/
renderIcon();
}
}
/*
* Set the number of unread mails
*/
void SysTrayXStatusNotifier::setUnreadMail( int unread_mail )
{
if( unread_mail != m_unread_mail ) {
/*
* Store the new value
*/
m_unread_mail = unread_mail;
/*
* Render and set a new icon in the tray
*/
renderIcon();
}
}
/*
* Set and render the icon in the system tray
*/
void SysTrayXStatusNotifier::renderIcon()
{
QPixmap pixmap;
if( m_unread_mail > 0 )
{
switch( m_icon_type )
{
case Preferences::PREF_BLANK_ICON:
{
pixmap = QPixmap( ":/files/icons/blank-icon.png" );
break;
}
case Preferences::PREF_NEWMAIL_ICON:
{
QIcon new_mail = QIcon::fromTheme("mail-unread", QIcon(":/files/icons/mail-unread.png"));
pixmap = new_mail.pixmap( 256, 256 );
break;
}
case Preferences::PREF_CUSTOM_ICON:
{
pixmap.loadFromData( m_icon_data );
break;
}
case Preferences::PREF_NO_ICON:
{
QPixmap lookthrough( 256, 256 );
lookthrough.fill( Qt::transparent );
pixmap = lookthrough;
break;
}
}
}
else
{
switch( m_default_icon_type )
{
case Preferences::PREF_DEFAULT_ICON_DEFAULT:
{
pixmap = QPixmap( ":/files/icons/Thunderbird.png" );
break;
}
case Preferences::PREF_DEFAULT_ICON_HIDE:
{
pixmap = QPixmap();
break;
}
case Preferences::PREF_DEFAULT_ICON_CUSTOM:
{
pixmap.loadFromData( m_default_icon_data );
break;
}
}
}
if( m_show_number && ( m_unread_mail > 0 ) )
{
/*
* Paint the number
*/
QPainter painter( &pixmap );
painter.setFont( QFont("Sans") );
#if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
double factor = pixmap.width() / ( 3 * painter.fontMetrics().width( "0" ) );
#else
double factor = pixmap.width() / ( 3 * painter.fontMetrics().horizontalAdvance( "0" ) );
#endif
QFont font = painter.font();
font.setPointSizeF( font.pointSizeF() * ( factor * m_number_size / 10 ) );
font.setBold( true );
painter.setFont( font );
painter.setPen( QColor( m_number_color ) );
painter.drawText( pixmap.rect(), Qt::AlignCenter, QString::number( m_unread_mail ) );
}
/*
* Set the tray icon
*/
setIconByPixmap( QIcon( pixmap ) );
}
/*
* Handle unread mail signal
*/
void SysTrayXStatusNotifier::slotSetUnreadMail( int unread_mail )
{
setUnreadMail( unread_mail );
}
/*
* Handle the default icon type change signal
*/
void SysTrayXStatusNotifier::slotDefaultIconTypeChange()
{
setDefaultIconType( m_pref->getDefaultIconType() );
}
/*
* Handle the default icon data change signal
*/
void SysTrayXStatusNotifier::slotDefaultIconDataChange()
{
setDefaultIconMime( m_pref->getDefaultIconMime() );
setDefaultIconData( m_pref->getDefaultIconData() );
}
/*
* Handle the icon type change signal
*/
void SysTrayXStatusNotifier::slotIconTypeChange()
{
setIconType( m_pref->getIconType() );
}
/*
* Handle the icon data change signal
*/
void SysTrayXStatusNotifier::slotIconDataChange()
{
setIconMime( m_pref->getIconMime() );
setIconData( m_pref->getIconData() );
}
/*
* Handle the enable number state change signal
*/
void SysTrayXStatusNotifier::slotShowNumberChange()
{
showNumber( m_pref->getShowNumber() );
}
/*
* Handle the number color change signal
*/
void SysTrayXStatusNotifier::slotNumberColorChange()
{
setNumberColor( m_pref->getNumberColor() );
}
/*
* Handle the number size change signal
*/
void SysTrayXStatusNotifier::slotNumberSizeChange()
{
setNumberSize( m_pref->getNumberSize() );
}
/*
* Handle activate request of the notification icon
*/
void SysTrayXStatusNotifier::slotActivateRequested( bool active, const QPoint &pos )
{
Q_UNUSED( active )
Q_UNUSED( pos )
emit signalShowHide();
}
/*
* Handle secondary activate request of the notification icon
*/
void SysTrayXStatusNotifier::slotSecondaryActivateRequested( const QPoint &pos )
{
Q_UNUSED( pos )
emit signalShowHide();
}

View File

@@ -0,0 +1,244 @@
#ifndef SYSTRAYXSTATUSNOTIFIER_H
#define SYSTRAYXSTATUSNOTIFIER_H
/*
* Local includes
*/
#include "preferences.h"
/*
* Qt includes
*/
#include <KStatusNotifierItem>
/*
* Predefines
*/
class SysTrayXLink;
/**
* @brief The SysTrayXStatusNotifier class. The system tray icon.
*/
class SysTrayXStatusNotifier : public KStatusNotifierItem
{
Q_OBJECT
public:
/**
* @brief SysTrayXStatusNotifier. Constructor.
*
* @param parent My parent.
*/
SysTrayXStatusNotifier( SysTrayXLink* link, Preferences* pref, QObject* parent = nullptr );
/**
* @brief setDefaultIconType. Set the sytem tray default icon type.
*
* @param icon_type The icon type
*/
void setDefaultIconType( Preferences::DefaultIconType icon_type );
/**
* @brief setDefaultIconMime. Set the sytem tray icon mime.
*
* @param icon_mime The icon mime
*/
void setDefaultIconMime( const QString& icon_mime );
/**
* @brief setDefaultIconData. Set the custom icon data.
*
* @param icon_data The icon data.
*/
void setDefaultIconData( const QByteArray& icon_data );
/**
* @brief setIconType. Set the sytem tray icon type.
*
* @param icon_type The icon type
*/
void setIconType( Preferences::IconType icon_type );
/**
* @brief setIconMime. Set the sytem tray icon mime.
*
* @param icon_mime The icon mime
*/
void setIconMime( const QString& icon_mime );
/**
* @brief setIconData. Set the custom icon data.
*
* @param icon_data The icon data.
*/
void setIconData( const QByteArray& icon_data );
/**
* @brief showNumber. Set the show number state.
*
* @param state Show / hide.
*/
void showNumber( bool state );
/**
* @brief setNumberColor. Set the number color.
*
* @param color The color.
*/
void setNumberColor( const QString& color );
/**
* @brief setNumberSize. Set the number size.
*
* @param size The size.
*/
void setNumberSize( int size );
/**
* @brief setUnreadMail. Set the number of unread mails.
*
* @param unread_mail The number of unread mails.
*/
void setUnreadMail( int unread_mail );
private:
/**
* @brief setIcon. Set a new rendered icon.
*/
void renderIcon();
signals:
/**
* @brief signalShowHide. Signal show / hide window.
*/
void signalShowHide();
public slots:
/**
* @brief slotSetUnreadMail. Slot for handling unread mail signals.
*
* @param unread_mail The number of unread mails.
*/
void slotSetUnreadMail( int unread_mail );
/**
* @brief slotDefaultIconTypeChange. Slot for handling default icon type change signals.
*/
void slotDefaultIconTypeChange();
/**
* @brief slotDefaultIconDataChange. Slot for handling default icon data change signals.
*/
void slotDefaultIconDataChange();
/**
* @brief slotIconTypeChange. Slot for handling icon type change signals.
*/
void slotIconTypeChange();
/**
* @brief slotIconDataChange. Slot for handling icon data change signals.
*/
void slotIconDataChange();
/**
* @brief slotShowNumberChange. Slot for handling show number change signals.
*/
void slotShowNumberChange();
/**
* @brief slotNumberColorChange. Slot for handling number color change signals.
*/
void slotNumberColorChange();
/**
* @brief slotNumberSizeChange. Slot for handling number size change signals.
*/
void slotNumberSizeChange();
private slots:
/**
* @brief slotActivateRequested. Handle a activate request of the notifier.
*
* @param active Window request show / hide.
* @param pos Click position.
*/
void slotActivateRequested( bool active, const QPoint &pos );
/**
* @brief slotSecondaryActivateRequested. Handle a secondary activate request of the notifier.
*
* @param pos Click position.
*/
void slotSecondaryActivateRequested( const QPoint &pos );
private:
/**
* @brief m_link. Pointer to the link.
*/
SysTrayXLink* m_link;
/**
* @brief m_pref Pointer to the preferences storage.
*/
Preferences* m_pref;
/**
* @brief m_default_icon_type. Storage for the default icon type.
*/
Preferences::DefaultIconType m_default_icon_type;
/**
* @brief m_default_icon_mime. Storage for the default icon mime.
*/
QString m_default_icon_mime;
/**
* @brief m_default_icon_data. Storage for the default icon.
*/
QByteArray m_default_icon_data;
/**
* @brief m_icon_type. Storage for the icon type.
*/
Preferences::IconType m_icon_type;
/**
* @brief m_icon_mime. Storage for the icon mime.
*/
QString m_icon_mime;
/**
* @brief m_icon_data. Storage for the icon.
*/
QByteArray m_icon_data;
/**
* @brief m_show_number. Show the unread mail count.
*/
bool m_show_number;
/**
* @brief m_number_color. Color of the unread mail number.
*/
QString m_number_color;
/**
* @brief m_number_size. Size of the unread mail number.
*/
int m_number_size;
/**
* @brief m_unread_mail. Storage for the number of unread mails.
*/
int m_unread_mail;
};
#endif // SYSTRAYXSTATUSNOTIFIER_H