1
/** ===========================================================
4
* This file is a part of digiKam project
5
* <a href="http://www.digikam.org">http://www.digikam.org</a>
8
* @brief a combo box with a width not depending of text
11
* @author Copyright (C) 2006-2011 by Gilles Caulier
12
* <a href="mailto:caulier dot gilles at gmail dot com">caulier dot gilles at gmail dot com</a>
13
* @author Copyright (C) 2008 by Andi Clemens
14
* <a href="mailto:andi dot clemens at gmx dot net">andi dot clemens at gmx dot net</a>
15
* @author Copyright (C) 2005 by Tom Albers
16
* <a href="mailto:tomalbers at kde dot nl">tomalbers at kde dot nl</a>
18
* This program is free software; you can redistribute it
19
* and/or modify it under the terms of the GNU General
20
* Public License as published by the Free Software Foundation;
21
* either version 2, or (at your option)
24
* This program is distributed in the hope that it will be useful,
25
* but WITHOUT ANY WARRANTY; without even the implied warranty of
26
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
* GNU General Public License for more details.
29
* ============================================================ */
31
#include "squeezedcombobox.moc"
39
#include <QApplication>
41
#include <QResizeEvent>
46
class SqueezedComboBox::SqueezedComboBoxPriv
50
SqueezedComboBoxPriv()
55
QMap<int, QString> originalItems;
60
SqueezedComboBox::SqueezedComboBox(QWidget* parent, const char* name)
61
: QComboBox(parent), d(new SqueezedComboBoxPriv)
65
d->timer = new QTimer(this);
66
d->timer->setSingleShot(true);
68
connect(d->timer, SIGNAL(timeout()),
69
this, SLOT(slotTimeOut()));
71
connect(this, SIGNAL(activated(int)),
72
SLOT(slotUpdateToolTip(int)));
75
SqueezedComboBox::~SqueezedComboBox()
81
bool SqueezedComboBox::contains(const QString& text) const
86
for (QMap<int, QString>::const_iterator it = d->originalItems.constBegin() ; it != d->originalItems.constEnd();
89
if (it.value() == text)
96
QSize SqueezedComboBox::sizeHint() const
99
QFontMetrics fm = fontMetrics();
101
int maxW = count() ? 18 : 7 * fm.width(QChar('x')) + 18;
102
int maxH = qMax( fm.lineSpacing(), 14 ) + 2;
104
QStyleOptionComboBox options;
105
options.initFrom(this);
107
return style()->sizeFromContents(QStyle::CT_ComboBox, &options,
108
QSize(maxW, maxH), this).expandedTo(QApplication::globalStrut());
111
void SqueezedComboBox::insertSqueezedItem(const QString& newItem, int index,
112
const QVariant& userData)
114
d->originalItems[index] = newItem;
115
QComboBox::insertItem(index, squeezeText(newItem), userData);
117
// if this is the first item, set the tooltip.
119
slotUpdateToolTip(0);
122
void SqueezedComboBox::insertSqueezedList(const QStringList& newItems, int index)
124
for(QStringList::const_iterator it = newItems.constBegin() ; it != newItems.constEnd() ; ++it)
126
insertSqueezedItem(*it, index);
131
void SqueezedComboBox::addSqueezedItem(const QString& newItem,
132
const QVariant& userData)
134
insertSqueezedItem(newItem, count(), userData);
137
void SqueezedComboBox::setCurrent(const QString& itemText)
139
QString squeezedText = squeezeText(itemText);
140
qint32 itemIndex = findText(squeezedText);
142
setCurrentIndex(itemIndex);
145
void SqueezedComboBox::resizeEvent(QResizeEvent *)
147
d->timer->start(200);
150
void SqueezedComboBox::slotTimeOut()
152
for (QMap<int, QString>::iterator it = d->originalItems.begin() ;
153
it != d->originalItems.end(); ++it)
155
setItemText( it.key(), squeezeText( it.value() ) );
159
QString SqueezedComboBox::squeezeText(const QString& original)
161
// not the complete widgetSize is usable. Need to compensate for that.
162
int widgetSize = width()-30;
163
QFontMetrics fm( fontMetrics() );
165
// If we can fit the full text, return that.
166
if (fm.width(original) < widgetSize)
169
// We need to squeeze.
170
QString sqItem = original; // prevent empty return value;
171
widgetSize = widgetSize-fm.width("...");
172
for (int i = 0 ; i != original.length(); ++i)
174
if ( (int)fm.width(original.right(i)) > widgetSize)
176
sqItem = QString(original.left(i) + "...");
183
void SqueezedComboBox::slotUpdateToolTip(int index)
185
setToolTip(d->originalItems[index]);
188
QString SqueezedComboBox::itemHighlighted()
190
int curItem = currentIndex();
191
return d->originalItems[curItem];
194
QString SqueezedComboBox::item(int index)
196
return d->originalItems[index];
199
} // namespace KDcrawIface