From 719de3bcf0e136f80982a3ce5dd8bc6e84ec45bf Mon Sep 17 00:00:00 2001 From: Ingo Klcker Date: Mon, 22 Sep 2003 22:13:34 +0000 Subject: [PATCH] Make the background of the system tray icon with the overlayed number of unread messages transparent. Man, this was more difficult than I had anticipated. svn path=/trunk/kdepim/; revision=253211 --- kmsystemtray.cpp | 50 ++++++++++++++++++++++++++++++++++-------------- kmsystemtray.h | 3 ++- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/kmsystemtray.cpp b/kmsystemtray.cpp index 3882de34c..1cb26b838 100644 --- a/kmsystemtray.cpp +++ b/kmsystemtray.cpp @@ -33,6 +33,7 @@ #include #include +#include #include #include #include @@ -65,8 +66,7 @@ KMSystemTray::KMSystemTray(QWidget *parent, const char *name) kdDebug(5006) << "Initting systray" << endl; mDefaultIcon = loadIcon( "kmail" ); - mTransparentIcon = loadIcon( "kmail" ); - KIconEffect::semiTransparent( mTransparentIcon ); + mLightIconImage = loadIcon( "kmaillight" ).convertToImage(); setPixmap(mDefaultIcon); @@ -163,20 +163,42 @@ void KMSystemTray::updateCount() countFont.setPointSizeFloat( countFontSize ); } - QPixmap bg(oldPixmapWidth, oldPixmapHeight); - bg.fill(this, 0, 0); - - /** Overlay count on transparent icon */ - QPainter p(&bg); - p.drawPixmap(0, 0, mTransparentIcon); - p.setFont(countFont); - p.setPen(Qt::blue); - p.drawText(pixmap()->rect(), Qt::AlignCenter, countString); - - setPixmap(bg); + // Create an image which represents the number of unread messages + // and which has a transparent background. + // Unfortunately this required the following twisted code because for some + // reason text that is drawn on a transparent pixmap is invisible + // (apparently the alpha channel isn't changed when the text is drawn). + // Therefore I have to draw the text on a solid background and then remove + // the background by making it transparent with QPixmap::setMask. This + // involves the slow createHeuristicMask() function (from the API docs: + // "This function is slow because it involves transformation to a QImage, + // non-trivial computations and a transformation back to a QBitmap."). Then + // I have to convert the resulting QPixmap to a QImage in order to overlay + // the light KMail icon with the number (because KIconEffect::overlay only + // works with QImage). Finally the resulting QImage has to be converted + // back to a QPixmap. + // That's a lot of work for overlaying the KMail icon with the number of + // unread messages, but every other approach I tried failed miserably. + // IK, 2003-09-22 + QPixmap numberPixmap( oldPixmapWidth, oldPixmapHeight ); + numberPixmap.fill( Qt::white ); + QPainter p( &numberPixmap ); + p.setFont( countFont ); + p.setPen( Qt::blue ); + p.drawText( numberPixmap.rect(), Qt::AlignCenter, countString ); + numberPixmap.setMask( numberPixmap.createHeuristicMask() ); + QImage numberImage = numberPixmap.convertToImage(); + + // Overlay the light KMail icon with the number image + QImage iconWithNumberImage = mLightIconImage.copy(); + KIconEffect::overlay( iconWithNumberImage, numberImage ); + + QPixmap iconWithNumber; + iconWithNumber.convertFromImage( iconWithNumberImage ); + setPixmap( iconWithNumber ); } else { - setPixmap(mDefaultIcon); + setPixmap( mDefaultIcon ); } } diff --git a/kmsystemtray.h b/kmsystemtray.h index a8f5df8e8..7b681cbc7 100644 --- a/kmsystemtray.h +++ b/kmsystemtray.h @@ -24,6 +24,7 @@ #include #include #include +#include class KMFolder; class KMMainWidget; @@ -79,7 +80,7 @@ private: KPopupMenu * mPopupMenu; QPixmap mDefaultIcon; - QPixmap mTransparentIcon; + QImage mLightIconImage; QPtrVector mPopupFolders; QMap, int> mFoldersWithUnread;