[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    [RFC] Using KPassivePopup from KSystrayIcon
From:       Alessandro Praduroux <pradu () pradu ! it>
Date:       2007-03-29 9:21:43
Message-ID: 200703291121.43934.pradu () pradu ! it
[Download RAW message or body]

Since QSystemTrayIcon does not derive from QWidget, it is not possible to call 
the static ::message methods of KPassivePopup from a tray icon handler and 
have the passive popup displayed near the tray area.

since Qt 4.3 introduced the geometry() method to QSystemTrayIcon, it is now 
possible to correctly place the KPassivePopup.

I implemented the needed new static methods in KPassivePopup accepting a 
QSystemTrayIcon * instead of a QWidget.

Patch attached, AFAICT doesn't even break BC, is it ok to commit?

-- 
http://www.pradu.it/
pradu@pradu.it

["kpassivepopup.patch" (text/x-diff)]

Index: kpassivepopup.h
===================================================================
--- kpassivepopup.h	(revisione 647545)
+++ kpassivepopup.h	(copia locale)
@@ -16,6 +16,7 @@
 #include <kdelibs_export.h>
 
 class KVBox;
+class QSystemTrayIcon;
 
 /**
  * @short A dialog-like popup that displays messages without interupting the user.
@@ -206,6 +207,14 @@
     static KPassivePopup *message( const QString &text, QWidget *parent );
 
     /**
+     * Convenience method that displays popup with the specified  message  beside the
+     * icon of the specified QSystemTrayIcon.
+     * Note that the returned object is destroyed when it is hidden.
+     * @see setAutoDelete
+     */
+    static KPassivePopup *message( const QString &text, QSystemTrayIcon *parent );
+
+    /**
      * Convenience method that displays popup with the specified caption and message
      * beside the icon of the specified widget.
      * Note that the returned object is destroyed when it is hidden.
@@ -215,6 +224,15 @@
 				   QWidget *parent );
 
     /**
+     * Convenience method that displays popup with the specified caption and message
+     * beside the icon of the specified QSystemTrayIcon.
+     * Note that the returned object is destroyed when it is hidden.
+     * @see setAutoDelete
+     */
+    static KPassivePopup *message( const QString &caption, const QString &text,
+                   QSystemTrayIcon *parent );
+
+    /**
      * Convenience method that displays popup with the specified icon, caption and
      * message beside the icon of the specified widget.
      * Note that the returned object is destroyed when it is hidden.
@@ -226,6 +244,16 @@
 
     /**
      * Convenience method that displays popup with the specified icon, caption and
+     * message beside the icon of the specified QSystemTrayIcon.
+     * Note that the returned object is destroyed when it is hidden.
+     * @see setAutoDelete
+     */
+    static KPassivePopup *message( const QString &caption, const QString &text,
+                   const QPixmap &icon,
+                   QSystemTrayIcon *parent, int timeout = -1 );
+
+    /**
+     * Convenience method that displays popup with the specified icon, caption and
      * message beside the icon of the specified window.
      * Note that the returned object is destroyed when it is hidden.
      * @see setAutoDelete
@@ -243,13 +271,30 @@
     static KPassivePopup *message( int popupStyle, const QString &text, QWidget *parent );
 
     /**
+     * Convenience method that displays popup with the specified popup-style and message beside the
+     * icon of the specified QSystemTrayIcon.
+     * Note that the returned object is destroyed when it is hidden.
+     * @see setAutoDelete
+     */
+    static KPassivePopup *message( int popupStyle, const QString &text, QSystemTrayIcon *parent );
+
+    /**
      * Convenience method that displays popup with the specified popup-style, caption and message
+     * beside the icon of the specified QSystemTrayIcon.
+     * Note that the returned object is destroyed when it is hidden.
+     * @see setAutoDelete
+     */
+    static KPassivePopup *message( int popupStyle, const QString &caption, const QString &text,
+				   QSystemTrayIcon *parent );
+
+    /**
+     * Convenience method that displays popup with the specified popup-style, caption and message
      * beside the icon of the specified widget.
      * Note that the returned object is destroyed when it is hidden.
      * @see setAutoDelete
      */
     static KPassivePopup *message( int popupStyle, const QString &caption, const QString &text,
-				   QWidget *parent );
+                   QWidget *parent );
 
     /**
      * Convenience method that displays popup with the specified popup-style, icon, caption and
@@ -263,6 +308,16 @@
 
     /**
      * Convenience method that displays popup with the specified popup-style, icon, caption and
+     * message beside the icon of the specified QSystemTrayIcon.
+     * Note that the returned object is destroyed when it is hidden.
+     * @see setAutoDelete
+     */
+    static KPassivePopup *message( int popupStyle, const QString &caption, const QString &text,
+                   const QPixmap &icon,
+                   QSystemTrayIcon *parent, int timeout = -1 );
+
+    /**
+     * Convenience method that displays popup with the specified popup-style, icon, caption and
      * message beside the icon of the specified window.
      * Note that the returned object is destroyed when it is hidden.
      * @see setAutoDelete
@@ -298,7 +353,7 @@
      * Shows the popup in the given point
      */
     void show(const QPoint &p);
-    
+
     virtual void setVisible(bool visible);
 
 Q_SIGNALS:
@@ -330,6 +385,11 @@
     void moveNear( const QRect &target );
 
     /**
+     * Calculates the position to place the popup near the specified rectangle.
+     */
+    QPoint calculateNearbyPoint( const QRect &target);  
+
+    /**
      * Reimplemented to detect mouse clicks.
      */
     virtual void mouseReleaseEvent( QMouseEvent *e );
Index: kpassivepopup.cpp
===================================================================
--- kpassivepopup.cpp	(revisione 647545)
+++ kpassivepopup.cpp	(copia locale)
@@ -18,6 +18,7 @@
 #include <QPolygonF>
 #include <QTimer>
 #include <QToolTip>
+#include <QSystemTrayIcon>
 
 #include <kvbox.h>
 #include <kdebug.h>
@@ -264,7 +265,7 @@
         QFrame::setVisible( visible );
         return;
     }
-  
+
     if ( size() != sizeHint() )
         resize( sizeHint() );
 
@@ -369,11 +370,19 @@
 
 void KPassivePopup::moveNear( const QRect &target )
 {
+    QPoint pos = calculateNearbyPoint(target);
+    if( d->popupStyle == Balloon )
+        setAnchor( pos );
+    else
+        move( pos.x(), pos.y() );
+}
+
+QPoint KPassivePopup::calculateNearbyPoint( const QRect &target) {
     QPoint pos = target.topLeft();
     int x = pos.x();
     int y = pos.y();
-    int w = width();
-    int h = height();
+    int w = minimumSizeHint().width();
+    int h = minimumSizeHint().height();
 
     QRect r = KGlobalSettings::desktopGeometry(QPoint(x+w/2,y+h/2));
 
@@ -407,10 +416,7 @@
     if ( x < r.left() )
         x = r.left();
 
-    if( d->popupStyle == Balloon )
-        setAnchor( QPoint( x, y ) );
-    else
-        move( x, y );
+    return QPoint( x, y );
 }
 
 QPoint KPassivePopup::anchor() const
@@ -540,6 +546,25 @@
     return message( DEFAULT_POPUP_TYPE, caption, text, icon, parent, timeout );
 }
 
+KPassivePopup *KPassivePopup::message( const QString &caption, const QString &text,
+                       const QPixmap &icon,
+                       QSystemTrayIcon *parent, int timeout )
+{
+    return message( DEFAULT_POPUP_TYPE, caption, text, icon, parent, timeout );
+}
+
+KPassivePopup *KPassivePopup::message( const QString &text, QSystemTrayIcon *parent )
+{
+    return message( DEFAULT_POPUP_TYPE, QString(), text, QPixmap(), parent );
+}
+
+KPassivePopup *KPassivePopup::message( const QString &caption, const QString &text,
+                       QSystemTrayIcon *parent )
+{
+    return message( DEFAULT_POPUP_TYPE, caption, text, QPixmap(), parent );
+}
+
+
 KPassivePopup *KPassivePopup::message( int popupStyle, const QString &caption, const QString &text,
 				       const QPixmap &icon,
 				       QWidget *parent, int timeout )
@@ -578,6 +603,34 @@
     return pop;
 }
 
+KPassivePopup *KPassivePopup::message( int popupStyle, const QString &caption, const QString &text,
+                       const QPixmap &icon,
+                       QSystemTrayIcon *parent, int timeout )
+{
+    KPassivePopup *pop = new KPassivePopup( );
+    pop->setPopupStyle( popupStyle );
+    pop->setAutoDelete( true );
+    pop->setView( caption, text, icon );
+    pop->d->hideDelay = timeout;
+    QPoint pos = pop->calculateNearbyPoint(parent->geometry());
+    pop->show(pos);
+    pop->moveNear(parent->geometry());
+
+    return pop;
+}
+
+KPassivePopup *KPassivePopup::message( int popupStyle, const QString &text, QSystemTrayIcon *parent )
+{
+    return message( popupStyle, QString(), text, QPixmap(), parent );
+}
+
+KPassivePopup *KPassivePopup::message( int popupStyle, const QString &caption, const QString &text,
+                       QSystemTrayIcon *parent )
+{
+    return message( popupStyle, caption, text, QPixmap(), parent );
+}
+
+
 // Local Variables:
 // c-basic-offset: 4
 // End:


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic