-
Notifications
You must be signed in to change notification settings - Fork 19
/
YQComboBox.cc
284 lines (216 loc) · 6.61 KB
/
YQComboBox.cc
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
Copyright (C) 2000-2012 Novell, Inc
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) version 3.0 of the License. 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 Lesser General Public
License for more details. You should have received a copy of the GNU
Lesser General Public License along with this library; if not, write
to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
Floor, Boston, MA 02110-1301 USA
*/
/*-/
File: YQComboBox.cc
Author: Stefan Hundhammer <shundhammer@suse.de>
/-*/
#define SEND_SELECTION_CHANGED_EVENT 0
#include <qstring.h>
#include <qlabel.h>
#include <qcombobox.h>
#include <qlineedit.h>
#define YUILogComponent "qt-ui"
#include <yui/YUILog.h>
#include "utf8.h"
#include "YQUI.h"
#include <yui/YEvent.h>
#include "QY2CharValidator.h"
#include "YQComboBox.h"
#include "YQSignalBlocker.h"
#include "YQWidgetCaption.h"
#include <QVBoxLayout>
#include <QDebug>
using std::string;
using std::endl;
YQComboBox::YQComboBox( YWidget * parent,
const string & label,
bool editable )
: QFrame( (QWidget *) parent->widgetRep() )
, YComboBox( parent, label, editable )
, _validator(0)
{
QVBoxLayout* layout = new QVBoxLayout( this );
setLayout( layout );
setWidgetRep( this );
layout->setSpacing( YQWidgetSpacing );
layout->setMargin ( YQWidgetMargin );
_caption = new YQWidgetCaption( this, label );
YUI_CHECK_NEW( _caption );
layout->addWidget( _caption );
_qt_comboBox = new QComboBox(this);
_qt_comboBox->setEditable(editable);
YUI_CHECK_NEW( _caption );
layout->addWidget( _qt_comboBox );
_caption->setBuddy( _qt_comboBox );
#if SEND_SELECTION_CHANGED_EVENT
connect( _qt_comboBox, &pclass(_qt_comboBox)::highlighted,
this, &pclass(this)::slotSelected );
#endif
#if (QT_VERSION < QT_VERSION_CHECK( 5, 15, 0 ))
// The Trolls introduced a bunch of SOURCE CODE INCOMPATIBILITIES in a MINOR RELEASE!
// With Qt 5.15, they deprecated a ton of functions, causing a ton of warnings.
// For us, this means a build failure because we always compile with -Werror,
// promoting all warnings to errors.
//
// Seriously, Trolls, WTF?!?
//
// And now we have to live with ugly stuff like this #if QT_VERSION. Thanks a lot.
//
// -- 2020-05-20 HuHa
connect( _qt_comboBox, static_cast<void (QComboBox::*)(const QString&)>(&QComboBox::activated),
this, &pclass(this)::textChanged );
#else
connect( _qt_comboBox, static_cast<void (QComboBox::*)(const QString&)>(&QComboBox::textActivated),
this, &pclass(this)::textChanged );
#endif
connect( _qt_comboBox, &pclass(_qt_comboBox)::editTextChanged,
this, &pclass(this)::textChanged );
}
YQComboBox::~YQComboBox()
{
// NOP
}
string YQComboBox::text()
{
return toUTF8( _qt_comboBox->currentText() );
}
void YQComboBox::setText( const string & newValue )
{
QString text = fromUTF8( newValue );
if ( isValidText( text ) )
{
YQSignalBlocker sigBlocker( _qt_comboBox );
int index = _qt_comboBox->findText( text );
if ( index < 0 )
_qt_comboBox->setEditText( text );
else
{
_qt_comboBox->setCurrentIndex( index );
_qt_comboBox->setItemText(index, text );
}
}
else
{
yuiError() << this << ": Rejecting invalid value \"" << newValue << "\"" << endl;
}
}
void YQComboBox::addItem( YItem * item )
{
YComboBox::addItem( item );
QIcon icon;
if ( item->hasIconName() )
{
icon = YQUI::ui()->loadIcon( item->iconName() );
}
if ( icon.isNull() )
_qt_comboBox->addItem( fromUTF8( item->label() ) );
else
_qt_comboBox->addItem( icon, fromUTF8( item->label() ) );
if ( item->selected() )
{
YQSignalBlocker sigBlocker( _qt_comboBox );
setText( item->label() );
}
}
void YQComboBox::deleteAllItems()
{
YQSignalBlocker sigBlocker( _qt_comboBox );
_qt_comboBox->clear();
YComboBox::deleteAllItems();
}
void YQComboBox::setLabel( const string & label )
{
_caption->setText( label );
YComboBox::setLabel( label );
}
void YQComboBox::setValidChars( const string & newValidChars )
{
if ( ! _qt_comboBox->isEditable() )
{
yuiWarning() << this << ": Setting ValidChars is useless on a combo box that isn't editable!" << endl;
return;
}
if ( _validator )
{
_validator->setValidChars( fromUTF8( newValidChars ) );
}
else
{
_validator = new QY2CharValidator( fromUTF8( newValidChars ), this );
_qt_comboBox->setValidator( _validator );
// No need to delete the validator in the destructor - Qt will take
// care of that since it's a QObject with a parent!
}
if ( ! isValidText( _qt_comboBox->currentText() ) )
{
yuiError() << this << ": Old value \"" << _qt_comboBox->currentText()
<< " \" invalid according to new ValidChars \""<< newValidChars << "\" - deleting"
<< endl;
_qt_comboBox->setItemText(_qt_comboBox->currentIndex(), "");
}
YComboBox::setValidChars( newValidChars );
}
bool YQComboBox::isValidText( const QString & txt ) const
{
if ( ! _validator )
return true;
int pos = 0;
QString text( txt ); // need a non-const QString &
return _validator->validate( text, pos ) == QValidator::Acceptable;
}
void YQComboBox::slotSelected( int i )
{
if ( notify() )
{
if ( ! YQUI::ui()->eventPendingFor( this ) )
{
// Avoid overwriting a (more important) ValueChanged event with a SelectionChanged event
YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::SelectionChanged ) );
}
}
}
void YQComboBox::textChanged( QString )
{
if ( notify() )
YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
}
void YQComboBox::setInputMaxLength( int len )
{
_qt_comboBox->lineEdit()->setMaxLength( len );
YComboBox::setInputMaxLength( len );
}
int YQComboBox::preferredWidth()
{
return sizeHint().width();
}
int YQComboBox::preferredHeight()
{
return sizeHint().height();
}
void YQComboBox::setSize( int newWidth, int newHeight )
{
resize( newWidth, newHeight );
}
void YQComboBox::setEnabled( bool enabled )
{
_caption->setEnabled( enabled );
_qt_comboBox->setEnabled( enabled );
YWidget::setEnabled( enabled );
}
bool YQComboBox::setKeyboardFocus()
{
_qt_comboBox->setFocus();
return true;
}