chalk

squeezedcombobox.cpp
Go to the documentation of this file.
1/* ============================================================
2 * Author: Tom Albers <tomalbers@kde.nl>
3 * Date : 2005-01-01
4 * Description :
5 *
6 * Copyright 2005 by Tom Albers
7 *
8 * This program is free software; you can redistribute it
9 * and/or modify it under the terms of the GNU General
10 * Public License as published by the Free Software Foundation;
11 * either version 2, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * ============================================================ */
20
23// TQt includes.
24
25#include <tqlistbox.h>
26#include <tqcombobox.h>
27#include <tqpair.h>
28#include <tqtimer.h>
29#include <tqvaluelist.h>
30#include <tqstyle.h>
31#include <tqapplication.h>
32#include <tqtooltip.h>
33
34// Local includes.
35
36#include "squeezedcombobox.h"
37
39 : TQToolTip( parent )
40{
41 m_originalWidget = name;
42}
43
44void SqueezedComboBoxTip::maybeTip( const TQPoint &pos )
45{
46 TQListBox* listBox = m_originalWidget->listBox();
47 if (!listBox)
48 return;
49
50 TQListBoxItem* selectedItem = listBox->itemAt( pos );
51 if (selectedItem)
52 {
53 TQRect positionToolTip = listBox->itemRect( selectedItem );
54 TQString toolTipText = m_originalWidget->itemHighlighted();
55 if (!toolTipText.isNull())
56 tip(positionToolTip, toolTipText);
57 }
58}
59
60SqueezedComboBox::SqueezedComboBox( TQWidget *parent, const char *name )
61 : TQComboBox( parent, name )
62{
63 setMinimumWidth(100);
64 m_timer = new TQTimer(this);
65 m_tooltip = new SqueezedComboBoxTip( listBox()->viewport(), this );
66
67 connect(m_timer, TQ_SIGNAL(timeout()),
68 TQ_SLOT(slotTimeOut()));
69 connect(this, TQ_SIGNAL(activated( int )),
70 TQ_SLOT(slotUpdateToolTip( int )));
71}
72
74{
75 delete m_tooltip;
76 delete m_timer;
77}
78
79bool SqueezedComboBox::contains( const TQString& _text ) const
80{
81 if ( _text.isEmpty() )
82 return false;
83
84 const int itemCount = count();
85 for (int i = 0; i < itemCount; ++i )
86 {
87 if ( text(i) == _text )
88 return true;
89 }
90 return false;
91}
92
94{
95 constPolish();
96 TQFontMetrics fm = fontMetrics();
97
98 int maxW = count() ? 18 : 7 * fm.width(TQChar('x')) + 18;
99 int maxH = TQMAX( fm.lineSpacing(), 14 ) + 2;
100
101 return style().sizeFromContents(TQStyle::CT_ComboBox, this,
102 TQSize(maxW, maxH)).
103 expandedTo(TQApplication::globalStrut());
104}
105
106void SqueezedComboBox::insertSqueezedItem(const TQString& newItem, int index)
107{
108 m_originalItems[index] = newItem;
109 insertItem( squeezeText(newItem), index );
110
111 // if this is the first item, set the tooltip.
112 if (index == 0)
113 slotUpdateToolTip(0);
114}
115
116void SqueezedComboBox::resizeEvent ( TQResizeEvent * )
117{
118 m_timer->start(200, true);
119}
120
121void SqueezedComboBox::slotTimeOut()
122{
123 TQMapIterator<int,TQString> it;
124 for (it = m_originalItems.begin() ; it != m_originalItems.end();
125 ++it)
126 {
127 changeItem( squeezeText( it.data() ), it.key() );
128 }
129}
130
131TQString SqueezedComboBox::squeezeText( const TQString& original)
132{
133 // not the complete widgetSize is usable. Need to compensate for that.
134 int widgetSize = width()-30;
135 TQFontMetrics fm( fontMetrics() );
136
137 // If we can fit the full text, return that.
138 if (fm.width(original) < widgetSize)
139 return(original);
140
141 // We need to squeeze.
142 TQString sqItem = original; // prevent empty return value;
143 widgetSize = widgetSize-fm.width("...");
144 for (uint i = 0 ; i != original.length(); ++i)
145 {
146 if ( (int)fm.width(original.right(i)) > widgetSize)
147 {
148 sqItem = TQString("..." + original.right(--i));
149 break;
150 }
151 }
152 return sqItem;
153}
154
155void SqueezedComboBox::slotUpdateToolTip( int index )
156{
157 TQToolTip::remove(this);
158 TQToolTip::add(this, m_originalItems[index]);
159}
160
162{
163 int curItem = this->listBox()->currentItem();
164 return m_originalItems[curItem];
165}
166
167#include "squeezedcombobox.moc"
This class shows a tooltip for a SqueezedComboBox the tooltip will contain the full text and helps th...
SqueezedComboBoxTip(TQWidget *parent, SqueezedComboBox *name)
Constructor.
void maybeTip(const TQPoint &pos)
Reimplemented version from TQToolTip which shows the tooltip when needed.
This widget is a TQComboBox, but then a little bit different.
void insertSqueezedItem(const TQString &newItem, int index)
This inserts a item to the list.
virtual TQSize sizeHint() const
Sets the sizeHint() of this widget.
SqueezedComboBox(TQWidget *parent=0, const char *name=0)
Constructor.
TQString itemHighlighted()
This method returns the full text (not squeezed) of the currently highlighted item.
virtual ~SqueezedComboBox()
destructor