combobox.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
3 Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20#include "combobox.h"
21
22#include <tqlayout.h>
23#include <tqmap.h>
24#include <tqvariant.h>
25#include <tqpainter.h>
26
27#include <kcombobox.h>
28#include <kdebug.h>
29
30#include "property.h"
31
32using namespace KoProperty;
33
34ComboBox::ComboBox(Property *property, TQWidget *parent, const char *name)
35 : Widget(property, parent, name)
36 , m_setValueEnabled(true)
37{
38 TQHBoxLayout *l = new TQHBoxLayout(this, 0, 0);
39 m_edit = new KComboBox(this);
40 m_edit->setSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Expanding);
41 m_edit->setMinimumHeight(5);
42 l->addWidget(m_edit);
43
44 m_edit->setEditable(false);
45 m_edit->setInsertionPolicy(TQComboBox::NoInsertion);
46 m_edit->setMinimumSize(10, 0); // to allow the combo to be resized to a small size
47 m_edit->setAutoCompletion(true);
48 m_edit->setContextMenuEnabled(false);
49
50 if (this->property()->listData()) {
51 fillBox();
52 }
53//not needed for combo setLeavesTheSpaceForRevertButton(true);
54
55 setFocusWidget(m_edit);
56 connect(m_edit, TQ_SIGNAL(activated(int)), this, TQ_SLOT(slotValueChanged(int)));
57}
58
59ComboBox::~ComboBox()
60{
61}
62
63TQVariant
64ComboBox::value() const
65{
66 if (!property()->listData()) {
67 kopropertywarn << "ComboBox::value(): propery listData not available!" << endl;
68 return TQVariant();
69 }
70 const int idx = m_edit->currentItem();
71 if (idx<0 || idx>=(int)property()->listData()->keys.count())
72 return TQVariant();
73 return TQVariant( property()->listData()->keys[idx] );
74// if(property()->listData() && property()->listData()->contains(m_edit->currentText()))
75// return (*(property()->valueList()))[m_edit->currentText()];
76// return TQVariant();
77}
78
79void
80ComboBox::setValue(const TQVariant &value, bool emitChange)
81{
82 if (!property() || !property()->listData()) {
83 kopropertywarn << "ComboBox::value(): propery listData not available!" << endl;
84 return;
85 }
86 if (!m_setValueEnabled)
87 return;
88 int idx = property()->listData()->keys.findIndex( value );
89 if (idx>=0 && idx<m_edit->count()) {
90 m_edit->setCurrentItem(idx);
91 }
92 else {
93 if (idx<0) {
94 kopropertywarn << "ComboBox::setValue(): NO SUCH KEY '" << value.toString()
95 << "' (property '" << property()->name() << "')" << endl;
96 } else {
97 TQStringList list;
98 for (int i=0; i<m_edit->count(); i++)
99 list += m_edit->text(i);
100 kopropertywarn << "ComboBox::setValue(): NO SUCH INDEX WITHIN COMBOBOX: " << idx
101 << " count=" << m_edit->count() << " value='" << value.toString()
102 << "' (property '" << property()->name() << "')\nActual combobox contents: "
103 << list << endl;
104 }
105 m_edit->setCurrentText(TQString());
106 }
107
108 if(value.isNull())
109 return;
110
111// m_edit->blockSignals(true);
112// m_edit->setCurrentText(keyForValue(value));
113// m_edit->blockSignals(false);
114 if (emitChange)
115 emit valueChanged(this);
116}
117
118void
119ComboBox::drawViewer(TQPainter *p, const TQColorGroup &cg, const TQRect &r, const TQVariant &value)
120{
121 TQString txt;
122 if (property()->listData()) {
123 const int idx = property()->listData()->keys.findIndex( value );
124 if (idx>=0)
125 txt = property()->listData()->names[ idx ];
126 }
127
128 Widget::drawViewer(p, cg, r, txt); //keyForValue(value));
129// p->eraseRect(r);
130// p->drawText(r, TQt::AlignLeft | TQt::AlignVCenter | TQt::SingleLine, keyForValue(value));
131}
132
133void
134ComboBox::fillBox()
135{
136 m_edit->clear();
137 //m_edit->clearContents();
138
139 if(!property())
140 return;
141 if (!property()->listData()) {
142 kopropertywarn << "ComboBox::fillBox(): propery listData not available!" << endl;
143 return;
144 }
145
146 m_edit->insertStringList(property()->listData()->names);
147 TDECompletion *comp = m_edit->completionObject();
148 comp->insertItems(property()->listData()->names);
149 comp->setCompletionMode(TDEGlobalSettings::CompletionShell);
150}
151
152void
153ComboBox::setProperty(Property *prop)
154{
155 const bool b = (property() == prop);
156 m_setValueEnabled = false; //setValue() couldn't be called before fillBox()
158 m_setValueEnabled = true;
159 if(!b)
160 fillBox();
161 if(prop)
162 setValue(prop->value(), false); //now the value can be set
163}
164
165void
166ComboBox::slotValueChanged(int)
167{
168 emit valueChanged(this);
169}
170
171void
172ComboBox::setReadOnlyInternal(bool readOnly)
173{
174 setVisibleFlag(!readOnly);
175}
176
177
178/*TQString
179ComboBox::keyForValue(const TQVariant &value)
180{
181 const TQStringVariantMap *list = property()->valueList();
182 Property::ListData *list = property()->listData();
183
184 if (!list)
185 return TQString();
186 int idx = listData->keys.findIndex( value );
187
188
189 TQStringVariantMap::ConstIterator endIt = list->constEnd();
190 for(TQStringVariantMap::ConstIterator it = list->constBegin(); it != endIt; ++it) {
191 if(it.data() == value)
192 return it.key();
193 }
194 return TQString();
195}*/
196
197
198#include "combobox.moc"
199
TQValueList< TQVariant > keys
Definition property.h:203
The base class representing a single property.
Definition property.h:177
TQCString name() const
Definition property.cpp:248
ListData * listData() const
Definition property.cpp:428
TQVariant value() const
Definition property.cpp:314
The base class for all item editors used in Editor.
Definition widget.h:37
virtual void drawViewer(TQPainter *p, const TQColorGroup &cg, const TQRect &r, const TQVariant &value)
Definition widget.cpp:88
virtual void setProperty(Property *property)
Definition widget.cpp:78
const char * name(StdAction id)
Namespace for a set of classes implementing generic properties framework.