[Digikam-devel] extragear/graphics/digikam/digikam

Tom Albers tomalbers at kde.nl
Sat Jul 23 20:15:27 BST 2005


SVN commit 437971 by toma:

Added a new class which adds a unsqueezed tooltip to the squeezed combobox items, example at 
http://www.digikam.org/users/tom/t.png

CCMAIL: digikam-devel at kde.org



 M  +30 -14    squeezedcombobox.cpp  
 M  +49 -5     squeezedcombobox.h  


--- trunk/extragear/graphics/digikam/digikam/squeezedcombobox.cpp #437970:437971
@@ -32,11 +32,31 @@
 
 #include "squeezedcombobox.h"
 
+UnSqueezedTip::UnSqueezedTip( QWidget * parent, SqueezedComboBox* name )
+    : QToolTip( parent )
+{
+    originalWidget = name;
+}
+
+void UnSqueezedTip::maybeTip( const QPoint &pos )
+{
+    QListBox* listBox = ((SqueezedComboBox*)originalWidget)->listBox();
+    QListBoxItem* selectedItem = listBox->itemAt( pos );
+    QRect positionToolTip = listBox->itemRect( selectedItem );
+
+    QString toolTipText =
+            ((SqueezedComboBox*)originalWidget)->itemHighlighted();
+
+    tip(positionToolTip, toolTipText);
+}
+
 SqueezedComboBox::SqueezedComboBox( QWidget *parent, const char *name )
     : QComboBox( parent, name )
 {
     setMinimumWidth(100);
     m_timer = new QTimer(this);
+    t = new UnSqueezedTip( this->listBox()->viewport(), this );
+
     connect(m_timer, SIGNAL(timeout()),
             SLOT(slotTimeOut()));
     connect(this, SIGNAL(activated( int )),
@@ -45,6 +65,7 @@
 
 SqueezedComboBox::~SqueezedComboBox()
 {
+    delete t;
     delete m_timer;
 }
 
@@ -63,7 +84,7 @@
 
 void SqueezedComboBox::insertSqueezedItem(const QString& newItem, int index)
 {
-    m_OriginalItems.append( qMakePair(index, newItem) );
+    m_OriginalItems[index] = newItem;
     insertItem( squeezeText(newItem), index );
 
     // if this is the first item, set the tooltip.
@@ -78,11 +99,11 @@
 
 void SqueezedComboBox::slotTimeOut()
 {
-    QValueList< QPair<int, QString> >::iterator it;
+    QMapIterator<int,QString> it;
     for (it = m_OriginalItems.begin() ; it != m_OriginalItems.end();
          ++it)
     {
-        changeItem( squeezeText((*it).second), (*it).first );
+        changeItem( squeezeText( it.data() ), it.key() );
     }
 }
 
@@ -114,18 +135,13 @@
 void SqueezedComboBox::slotUpdateToolTip( int index )
 {
     QToolTip::remove(this);
+    QToolTip::add(this, m_OriginalItems[index]);
+}
 
-    QValueList< QPair<int, QString> >::iterator it;
-    for (it = m_OriginalItems.begin() ; it != m_OriginalItems.end();
-         ++it)
-    {
-        if ((*it).first == index)
-        {
-            QToolTip::add(this, (*it).second);
-            break;
-        }
-    }
-
+QString SqueezedComboBox::itemHighlighted()
+{
+    int curItem = this->listBox()->currentItem();
+    return m_OriginalItems[curItem];
 }
 
 #include "squeezedcombobox.moc"
--- trunk/extragear/graphics/digikam/digikam/squeezedcombobox.h #437970:437971
@@ -23,12 +23,48 @@
 #ifndef SQUEEZEDCOMBOBOX_H
 #define SQUEEZEDCOMBOBOX_H
 
-class QListBox;
 class QTimer;
+class SqueezedComboBox;
 
 #include <qcombobox.h>
-#include <qstring.h>
+#include <qtooltip.h>
 
+/** @class UnSqueezedTip
+ * This class shows a tooltip for a SqueezedComboBox
+ * the tooltip will contain the full text and helps
+ * the user find the correct entry. It is automatically
+ * activated when starting a SqueezedComboBox. This is
+ * inherited from QToolTip
+ * 
+ * @author Tom Albers
+ */
+class UnSqueezedTip : public QToolTip
+{
+public:
+    /**
+     * Constructor. An example call (as done in
+     * SqueezedComboBox::SqueezedComboBox):
+     * @code
+     * t = new UnSqueezedTip( this->listBox()->viewport(), this );
+     * @endcode
+     * 
+     * @param parent parent widget (viewport)
+     * @param name parent widget
+     */
+    UnSqueezedTip( QWidget *parent, SqueezedComboBox *name );
+
+protected:
+    /**
+     * Reimplemented version from QToolTip which shows the
+     * tooltip when needed.
+     * @param  pos the point where the mouse currently is
+     */
+    void maybeTip( const QPoint& pos );
+
+private:
+    QWidget*        originalWidget;
+};
+
 /** @class SqueezedComboBox
  *
  * This widget is a QComboBox, but then a little bit
@@ -66,8 +102,14 @@
      */
     void insertSqueezedItem(const QString& newItem, int index);
 
-    
     /**
+     * This method returns the full text (not squeezed) of the currently
+     * highlighted item.
+     * @return full text of the highlighted item
+     */
+    QString itemHighlighted( );
+
+    /**
      * Sets the sizeHint() of this widget.
      */
     virtual QSize sizeHint() const;
@@ -80,8 +122,10 @@
     void resizeEvent ( QResizeEvent * );
     QString squeezeText( const QString& original);
 
-    QValueList< QPair<int,QString> >  m_OriginalItems;
-    QTimer*                           m_timer;
+    QMap<int,QString>   m_OriginalItems;
+    QTimer*             m_timer;
+    UnSqueezedTip*      t;
 };
 
+
 #endif



More information about the Digikam-devel mailing list