krita

squeezedcombobox.cpp

Go to the documentation of this file.
00001 /* ============================================================
00002  * Author: Tom Albers <tomalbers@kde.nl>
00003  * Date  : 2005-01-01
00004  * Description : 
00005  * 
00006  * Copyright 2005 by Tom Albers
00007  *
00008  * This program is free software; you can redistribute it
00009  * and/or modify it under the terms of the GNU General
00010  * Public License as published by the Free Software Foundation;
00011  * either version 2, or (at your option)
00012  * any later version.
00013  * 
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  * 
00019  * ============================================================ */
00020 
00023 // Qt includes.
00024 
00025 #include <qlistbox.h>
00026 #include <qcombobox.h>
00027 #include <qpair.h>
00028 #include <qtimer.h>
00029 #include <qvaluelist.h>
00030 #include <qstyle.h>
00031 #include <qapplication.h>
00032 #include <qtooltip.h>
00033 
00034 // Local includes.
00035 
00036 #include "squeezedcombobox.h"
00037 
00038 SqueezedComboBoxTip::SqueezedComboBoxTip( QWidget * parent, SqueezedComboBox* name )
00039     : QToolTip( parent )
00040 {
00041     m_originalWidget = name;
00042 }
00043 
00044 void SqueezedComboBoxTip::maybeTip( const QPoint &pos )
00045 {
00046     QListBox* listBox = m_originalWidget->listBox();
00047     if (!listBox)
00048         return;
00049 
00050     QListBoxItem* selectedItem = listBox->itemAt( pos );
00051     if (selectedItem)
00052     {
00053         QRect positionToolTip = listBox->itemRect( selectedItem );
00054         QString toolTipText = m_originalWidget->itemHighlighted();
00055         if (!toolTipText.isNull())
00056             tip(positionToolTip, toolTipText);
00057     }
00058 }
00059 
00060 SqueezedComboBox::SqueezedComboBox( QWidget *parent, const char *name )
00061     : QComboBox( parent, name )
00062 {
00063     setMinimumWidth(100);
00064     m_timer = new QTimer(this);
00065     m_tooltip = new SqueezedComboBoxTip( listBox()->viewport(), this );
00066 
00067     connect(m_timer, SIGNAL(timeout()),
00068             SLOT(slotTimeOut()));
00069     connect(this, SIGNAL(activated( int )),
00070             SLOT(slotUpdateToolTip( int )));
00071 }
00072 
00073 SqueezedComboBox::~SqueezedComboBox()
00074 {
00075     delete m_tooltip;
00076     delete m_timer;
00077 }
00078 
00079 bool SqueezedComboBox::contains( const QString& _text ) const
00080 {
00081     if ( _text.isEmpty() )
00082         return false;
00083 
00084     const int itemCount = count();
00085     for (int i = 0; i < itemCount; ++i )
00086     {
00087         if ( text(i) == _text )
00088             return true;
00089     }
00090     return false;
00091 }
00092 
00093 QSize SqueezedComboBox::sizeHint() const
00094 {
00095     constPolish();
00096     QFontMetrics fm = fontMetrics();
00097 
00098     int maxW = count() ? 18 : 7 * fm.width(QChar('x')) + 18;
00099     int maxH = QMAX( fm.lineSpacing(), 14 ) + 2;
00100 
00101     return style().sizeFromContents(QStyle::CT_ComboBox, this,
00102     QSize(maxW, maxH)).
00103             expandedTo(QApplication::globalStrut());
00104 }
00105 
00106 void SqueezedComboBox::insertSqueezedItem(const QString& newItem, int index)
00107 {
00108     m_originalItems[index] = newItem;
00109     insertItem( squeezeText(newItem), index );
00110 
00111     // if this is the first item, set the tooltip.
00112     if (index == 0)
00113         slotUpdateToolTip(0);
00114 }
00115 
00116 void SqueezedComboBox::resizeEvent ( QResizeEvent * )
00117 {
00118     m_timer->start(200, true);
00119 }
00120 
00121 void SqueezedComboBox::slotTimeOut()
00122 {
00123     QMapIterator<int,QString> it;
00124     for (it = m_originalItems.begin() ; it != m_originalItems.end();
00125          ++it)
00126     {
00127         changeItem( squeezeText( it.data() ), it.key() );
00128     }
00129 }
00130 
00131 QString SqueezedComboBox::squeezeText( const QString& original)
00132 {
00133     // not the complete widgetSize is usable. Need to compensate for that.
00134     int widgetSize = width()-30;
00135     QFontMetrics fm( fontMetrics() );
00136 
00137     // If we can fit the full text, return that.
00138     if (fm.width(original) < widgetSize)
00139         return(original);
00140 
00141     // We need to squeeze.
00142     QString sqItem = original; // prevent empty return value;
00143     widgetSize = widgetSize-fm.width("...");
00144     for (uint i = 0 ; i != original.length(); ++i)
00145     {
00146         if ( (int)fm.width(original.right(i)) > widgetSize)
00147         {
00148             sqItem = QString("..." + original.right(--i));
00149             break;
00150         }
00151     }
00152     return sqItem;
00153 }
00154 
00155 void SqueezedComboBox::slotUpdateToolTip( int index )
00156 {
00157     QToolTip::remove(this);
00158     QToolTip::add(this, m_originalItems[index]);
00159 }
00160 
00161 QString SqueezedComboBox::itemHighlighted()
00162 {
00163     int curItem = this->listBox()->currentItem();
00164     return m_originalItems[curItem];
00165 }
00166 
00167 #include "squeezedcombobox.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys