~ubuntu-branches/ubuntu/oneiric/libkdcraw/oneiric-proposed

« back to all changes in this revision

Viewing changes to libkdcraw/squeezedcombobox.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Philip Muškovac
  • Date: 2011-07-08 09:30:26 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20110708093026-deas159t23p3w3gq
Tags: 4:4.6.90+repack1-0ubuntu1
* New upstream release candidate 
* Update for split packaging

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** ===========================================================
 
2
 * @file
 
3
 *
 
4
 * This file is a part of digiKam project
 
5
 * <a href="http://www.digikam.org">http://www.digikam.org</a>
 
6
 *
 
7
 * @date   2008-08-21
 
8
 * @brief  a combo box with a width not depending of text
 
9
 *         content size
 
10
 *
 
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>
 
17
 *
 
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)
 
22
 * any later version.
 
23
 *
 
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.
 
28
 *
 
29
 * ============================================================ */
 
30
 
 
31
#include "squeezedcombobox.moc"
 
32
 
 
33
// Qt includes.
 
34
 
 
35
#include <QComboBox>
 
36
#include <QPair>
 
37
#include <QTimer>
 
38
#include <QStyle>
 
39
#include <QApplication>
 
40
#include <QToolTip>
 
41
#include <QResizeEvent>
 
42
 
 
43
namespace KDcrawIface
 
44
{
 
45
 
 
46
class SqueezedComboBox::SqueezedComboBoxPriv
 
47
{
 
48
public:
 
49
 
 
50
    SqueezedComboBoxPriv()
 
51
    {
 
52
        timer = 0;
 
53
    }
 
54
 
 
55
    QMap<int, QString> originalItems;
 
56
 
 
57
    QTimer*            timer;
 
58
};
 
59
 
 
60
SqueezedComboBox::SqueezedComboBox(QWidget* parent, const char* name)
 
61
                : QComboBox(parent), d(new SqueezedComboBoxPriv)
 
62
{
 
63
    setObjectName(name);
 
64
    setMinimumWidth(100);
 
65
    d->timer = new QTimer(this);
 
66
    d->timer->setSingleShot(true);
 
67
 
 
68
    connect(d->timer, SIGNAL(timeout()),
 
69
            this, SLOT(slotTimeOut()));
 
70
 
 
71
    connect(this, SIGNAL(activated(int)),
 
72
            SLOT(slotUpdateToolTip(int)));
 
73
}
 
74
 
 
75
SqueezedComboBox::~SqueezedComboBox()
 
76
{
 
77
    delete d->timer;
 
78
    delete d;
 
79
}
 
80
 
 
81
bool SqueezedComboBox::contains(const QString& text) const
 
82
{
 
83
    if (text.isEmpty())
 
84
        return false;
 
85
 
 
86
    for (QMap<int, QString>::const_iterator it = d->originalItems.constBegin() ; it != d->originalItems.constEnd();
 
87
         ++it)
 
88
    {
 
89
        if (it.value() == text)
 
90
            return true;
 
91
    }
 
92
 
 
93
    return false;
 
94
}
 
95
 
 
96
QSize SqueezedComboBox::sizeHint() const
 
97
{
 
98
    ensurePolished();
 
99
    QFontMetrics fm = fontMetrics();
 
100
 
 
101
    int maxW = count() ? 18 : 7 * fm.width(QChar('x')) + 18;
 
102
    int maxH = qMax( fm.lineSpacing(), 14 ) + 2;
 
103
 
 
104
    QStyleOptionComboBox options;
 
105
    options.initFrom(this);
 
106
 
 
107
    return style()->sizeFromContents(QStyle::CT_ComboBox, &options,
 
108
                                     QSize(maxW, maxH), this).expandedTo(QApplication::globalStrut());
 
109
}
 
110
 
 
111
void SqueezedComboBox::insertSqueezedItem(const QString& newItem, int index,
 
112
                                          const QVariant& userData)
 
113
{
 
114
    d->originalItems[index] = newItem;
 
115
    QComboBox::insertItem(index, squeezeText(newItem), userData);
 
116
 
 
117
    // if this is the first item, set the tooltip.
 
118
    if (index == 0)
 
119
        slotUpdateToolTip(0);
 
120
}
 
121
 
 
122
void SqueezedComboBox::insertSqueezedList(const QStringList& newItems, int index)
 
123
{
 
124
    for(QStringList::const_iterator it = newItems.constBegin() ; it != newItems.constEnd() ; ++it)
 
125
    {
 
126
        insertSqueezedItem(*it, index);
 
127
        index++;
 
128
    }
 
129
}
 
130
 
 
131
void SqueezedComboBox::addSqueezedItem(const QString& newItem,
 
132
                                       const QVariant& userData)
 
133
{
 
134
    insertSqueezedItem(newItem, count(), userData);
 
135
}
 
136
 
 
137
void SqueezedComboBox::setCurrent(const QString& itemText)
 
138
{
 
139
    QString squeezedText = squeezeText(itemText);
 
140
    qint32 itemIndex     = findText(squeezedText);
 
141
    if (itemIndex >= 0)
 
142
        setCurrentIndex(itemIndex);
 
143
}
 
144
 
 
145
void SqueezedComboBox::resizeEvent(QResizeEvent *)
 
146
{
 
147
    d->timer->start(200);
 
148
}
 
149
 
 
150
void SqueezedComboBox::slotTimeOut()
 
151
{
 
152
    for (QMap<int, QString>::iterator it = d->originalItems.begin() ;
 
153
         it != d->originalItems.end(); ++it)
 
154
    {
 
155
        setItemText( it.key(), squeezeText( it.value() ) );
 
156
    }
 
157
}
 
158
 
 
159
QString SqueezedComboBox::squeezeText(const QString& original)
 
160
{
 
161
    // not the complete widgetSize is usable. Need to compensate for that.
 
162
    int widgetSize = width()-30;
 
163
    QFontMetrics fm( fontMetrics() );
 
164
 
 
165
    // If we can fit the full text, return that.
 
166
    if (fm.width(original) < widgetSize)
 
167
        return(original);
 
168
 
 
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)
 
173
    {
 
174
        if ( (int)fm.width(original.right(i)) > widgetSize)
 
175
        {
 
176
            sqItem = QString(original.left(i) + "...");
 
177
            break;
 
178
        }
 
179
    }
 
180
    return sqItem;
 
181
}
 
182
 
 
183
void SqueezedComboBox::slotUpdateToolTip(int index)
 
184
{
 
185
     setToolTip(d->originalItems[index]);
 
186
}
 
187
 
 
188
QString SqueezedComboBox::itemHighlighted()
 
189
{
 
190
    int curItem = currentIndex();
 
191
    return d->originalItems[curItem];
 
192
}
 
193
 
 
194
QString SqueezedComboBox::item(int index)
 
195
{
 
196
    return d->originalItems[index];
 
197
}
 
198
 
 
199
}  // namespace KDcrawIface