summaryrefslogtreecommitdiffstats
path: root/lib/koproperty/editors/combobox.cpp
blob: 1dd35a8d90b944601af74e6728fa1acebe23fc5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* This file is part of the KDE project
   Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
   Copyright (C) 2004  Alexander Dymo <cloudtemple@mskat.net>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/
#include "combobox.h"

#include <layout.h>
#include <tqmap.h>
#include <tqvariant.h>
#include <tqpainter.h>

#include <kcombobox.h>
#include <kdebug.h>

#include "property.h"

using namespace KoProperty;

ComboBox::ComboBox(Property *property, TQWidget *parent, const char *name)
 : Widget(property, parent, name)
 , m_setValueEnabled(true)
{
	TQHBoxLayout *l = new TQHBoxLayout(this, 0, 0);
	m_edit = new KComboBox(this);
	m_edit->setSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Expanding);
	m_edit->setMinimumHeight(5);
	l->addWidget(m_edit);

	m_edit->setEditable(false);
	m_edit->setInsertionPolicy(TQComboBox::NoInsertion);
	m_edit->setMinimumSize(10, 0); // to allow the combo to be resized to a small size
	m_edit->setAutoCompletion(true);
	m_edit->setContextMenuEnabled(false);

	if (this->property()->listData()) {
		fillBox();
	}
//not needed for combo	setLeavesTheSpaceForRevertButton(true);

	setFocusWidget(m_edit);
	connect(m_edit, TQT_SIGNAL(activated(int)), this, TQT_SLOT(slotValueChanged(int)));
}

ComboBox::~ComboBox()
{
}

TQVariant
ComboBox::value() const
{
	if (!property()->listData()) {
		kopropertywarn << "ComboBox::value(): propery listData not available!" << endl;
		return TQVariant();
	}
	const int idx = m_edit->currentItem();
	if (idx<0 || idx>=(int)property()->listData()->keys.count())
		return TQVariant();
	return TQVariant( property()->listData()->keys[idx] );
//	if(property()->listData() && property()->listData()->contains(m_edit->currentText()))
//		return (*(property()->valueList()))[m_edit->currentText()];
//	return TQVariant();
}

void
ComboBox::setValue(const TQVariant &value, bool emitChange)
{
	if (!property() || !property()->listData()) {
		kopropertywarn << "ComboBox::value(): propery listData not available!" << endl;
		return;
	}
	if (!m_setValueEnabled)
		return;
	int idx = property()->listData()->keys.findIndex( value );
	if (idx>=0 && idx<m_edit->count()) {
		m_edit->setCurrentItem(idx);
	}
	else {
		if (idx<0) {
			kopropertywarn << "ComboBox::setValue(): NO SUCH KEY '" << value.toString() 
				<< "' (property '" << property()->name() << "')" << endl;
		} else {
			TQStringList list;
			for (int i=0; i<m_edit->count(); i++)
				list += m_edit->text(i);
			kopropertywarn << "ComboBox::setValue(): NO SUCH INDEX WITHIN COMBOBOX: " << idx 
				<< " count=" << m_edit->count() << " value='" << value.toString() 
				<< "' (property '" << property()->name() << "')\nActual combobox contents: "
				<< list << endl;
		}
		m_edit->setCurrentText(TQString());
	}

	if(value.isNull())
		return;

//	m_edit->blockSignals(true);
//	m_edit->setCurrentText(keyForValue(value));
//	m_edit->blockSignals(false);
	if (emitChange)
		emit valueChanged(this);
}

void
ComboBox::drawViewer(TQPainter *p, const TQColorGroup &cg, const TQRect &r, const TQVariant &value)
{
	TQString txt;
	if (property()->listData()) {
		const int idx = property()->listData()->keys.findIndex( value );
		if (idx>=0)
			txt = property()->listData()->names[ idx ];
	}

	Widget::drawViewer(p, cg, r, txt); //keyForValue(value));
//	p->eraseRect(r);
//	p->drawText(r, TQt::AlignLeft | TQt::AlignVCenter | TQt::SingleLine, keyForValue(value));
}

void
ComboBox::fillBox()
{
	m_edit->clear();
	//m_edit->clearContents();

	if(!property())
		return;
	if (!property()->listData()) {
		kopropertywarn << "ComboBox::fillBox(): propery listData not available!" << endl;
		return;
	}

	m_edit->insertStringList(property()->listData()->names);
	KCompletion *comp = m_edit->completionObject();
	comp->insertItems(property()->listData()->names);
	comp->setCompletionMode(KGlobalSettings::CompletionShell);
}

void
ComboBox::setProperty(Property *prop)
{
	const bool b = (property() == prop);
	m_setValueEnabled = false; //setValue() couldn't be called before fillBox()
	Widget::setProperty(prop);
	m_setValueEnabled = true;
	if(!b)
		fillBox();
	if(prop)
		setValue(prop->value(), false); //now the value can be set
}

void
ComboBox::slotValueChanged(int)
{
	emit valueChanged(this);
}

void
ComboBox::setReadOnlyInternal(bool readOnly)
{
	setVisibleFlag(!readOnly);
}


/*TQString
ComboBox::keyForValue(const TQVariant &value)
{
	const TQMap<TQString, TQVariant> *list = property()->valueList();
	Property::ListData *list = property()->listData();

	if (!list)
		return TQString();
	int idx = listData->keys.findIndex( value );


	TQMap<TQString, TQVariant>::ConstIterator endIt = list->constEnd();
	for(TQMap<TQString, TQVariant>::ConstIterator it = list->constBegin(); it != endIt; ++it) {
		if(it.data() == value)
			return it.key();
	}
	return TQString();
}*/


#include "combobox.moc"