libyui-qt  2.49.11
YQComboBox.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YQComboBox.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 
26 #define SEND_SELECTION_CHANGED_EVENT 0
27 
28 #include <qstring.h>
29 #include <qlabel.h>
30 #include <qcombobox.h>
31 #include <qlineedit.h>
32 #define YUILogComponent "qt-ui"
33 #include <yui/YUILog.h>
34 
35 #include "utf8.h"
36 #include "YQUI.h"
37 #include <yui/YEvent.h>
38 #include "QY2CharValidator.h"
39 #include "YQComboBox.h"
40 #include "YQSignalBlocker.h"
41 #include "YQWidgetCaption.h"
42 #include <QVBoxLayout>
43 #include <QDebug>
44 
45 YQComboBox::YQComboBox( YWidget * parent,
46  const std::string & label,
47  bool editable )
48  : QFrame( (QWidget *) parent->widgetRep() )
49  , YComboBox( parent, label, editable )
50  , _validator(0)
51 {
52  QVBoxLayout* layout = new QVBoxLayout( this );
53  setLayout( layout );
54 
55  setWidgetRep( this );
56  layout->setSpacing( YQWidgetSpacing );
57  layout->setMargin ( YQWidgetMargin );
58 
59  _caption = new YQWidgetCaption( this, label );
60  YUI_CHECK_NEW( _caption );
61  layout->addWidget( _caption );
62 
63  _qt_comboBox = new QComboBox(this);
64  _qt_comboBox->setEditable(editable);
65  YUI_CHECK_NEW( _caption );
66  layout->addWidget( _qt_comboBox );
67 
68  _caption->setBuddy( _qt_comboBox );
69 
70 #if SEND_SELECTION_CHANGED_EVENT
71  connect( _qt_comboBox, &pclass(_qt_comboBox)::highlighted,
72  this, &pclass(this)::slotSelected );
73 #endif
74 
75  connect( _qt_comboBox, static_cast<void (QComboBox::*)(const QString&)>(&QComboBox::activated),
76  this, &pclass(this)::textChanged );
77 
78  connect( _qt_comboBox, &pclass(_qt_comboBox)::editTextChanged,
79  this, &pclass(this)::textChanged );
80 }
81 
82 
84 {
85  // NOP
86 }
87 
88 
90 {
91  return toUTF8( _qt_comboBox->currentText() );
92 }
93 
94 
95 void YQComboBox::setText( const std::string & newValue )
96 {
97  QString text = fromUTF8( newValue );
98 
99  if ( isValidText( text ) )
100  {
101  YQSignalBlocker sigBlocker( _qt_comboBox );
102  int index = _qt_comboBox->findText( text );
103  if ( index < 0 )
104  _qt_comboBox->setEditText( text );
105  else {
106  _qt_comboBox->setCurrentIndex( index );
107  _qt_comboBox->setItemText(index, text );
108  }
109  }
110  else
111  {
112  yuiError() << this << ": Rejecting invalid value \"" << newValue << "\"" << std::endl;
113  }
114 }
115 
116 
117 void YQComboBox::addItem( YItem * item )
118 {
119  YComboBox::addItem( item );
120  QIcon icon;
121 
122  if ( item->hasIconName() )
123  {
124  icon = YQUI::ui()->loadIcon( item->iconName() );
125  }
126 
127  if ( icon.isNull() )
128  _qt_comboBox->addItem( fromUTF8( item->label() ) );
129  else
130  _qt_comboBox->addItem( icon, fromUTF8( item->label() ) );
131 
132  if ( item->selected() )
133  {
134  YQSignalBlocker sigBlocker( _qt_comboBox );
135  setText( item->label() );
136  }
137 }
138 
139 
141 {
142  YQSignalBlocker sigBlocker( _qt_comboBox );
143 
144  _qt_comboBox->clear();
145  YComboBox::deleteAllItems();
146 }
147 
148 
149 void YQComboBox::setLabel( const std::string & label )
150 {
151  _caption->setText( label );
152  YComboBox::setLabel( label );
153 }
154 
155 
156 void YQComboBox::setValidChars( const std::string & newValidChars )
157 {
158  if ( ! _qt_comboBox->isEditable() )
159  {
160  yuiWarning() << this << ": Setting ValidChars is useless on a combo box that isn't editable!" << std::endl;
161  return;
162  }
163 
164  if ( _validator )
165  {
166  _validator->setValidChars( fromUTF8( newValidChars ) );
167  }
168  else
169  {
170  _validator = new QY2CharValidator( fromUTF8( newValidChars ), this );
171  _qt_comboBox->setValidator( _validator );
172 
173  // No need to delete the validator in the destructor - Qt will take
174  // care of that since it's a QObject with a parent!
175  }
176 
177  if ( ! isValidText( _qt_comboBox->currentText() ) )
178  {
179  yuiError() << this << ": Old value \"" << _qt_comboBox->currentText()
180  << " \" invalid according to new ValidChars \""<< newValidChars << "\" - deleting"
181  << std::endl;
182  _qt_comboBox->setItemText(_qt_comboBox->currentIndex(), "");
183  }
184 
185  YComboBox::setValidChars( newValidChars );
186 }
187 
188 
189 bool YQComboBox::isValidText( const QString & txt ) const
190 {
191  if ( ! _validator )
192  return true;
193 
194  int pos = 0;
195  QString text( txt ); // need a non-const QString &
196 
197  return _validator->validate( text, pos ) == QValidator::Acceptable;
198 }
199 
200 
202 {
203  if ( notify() )
204  {
205  if ( ! YQUI::ui()->eventPendingFor( this ) )
206  {
207  // Avoid overwriting a (more important) ValueChanged event with a SelectionChanged event
208 
209  YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::SelectionChanged ) );
210  }
211  }
212 }
213 
214 
215 void YQComboBox::textChanged( QString )
216 {
217  if ( notify() )
218  YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
219 }
220 
221 
223 {
224  _qt_comboBox->lineEdit()->setMaxLength( len );
225  YComboBox::setInputMaxLength( len );
226 }
227 
228 
230 {
231  return sizeHint().width();
232 }
233 
234 
236 {
237  return sizeHint().height();
238 }
239 
240 
241 void YQComboBox::setSize( int newWidth, int newHeight )
242 {
243  resize( newWidth, newHeight );
244 }
245 
246 
247 void YQComboBox::setEnabled( bool enabled )
248 {
249  _caption->setEnabled( enabled );
250  _qt_comboBox->setEnabled( enabled );
251  YWidget::setEnabled( enabled );
252 }
253 
254 
256 {
257  _qt_comboBox->setFocus();
258 
259  return true;
260 }
261 
262 
263 
virtual void setValidChars(const std::string &validChars)
Change the valid input characters.
Definition: YQComboBox.cc:156
Helper class to block Qt signals for QWidgets or QObjects as long as this object exists.
virtual std::string text()
Return this ComboBox's current value as text.
Definition: YQComboBox.cc:89
virtual void setLabel(const std::string &label)
Change the label text.
Definition: YQComboBox.cc:149
void textChanged(QString)
Tells the ui that the user has edited the text ( if the 'editable' option is set ).
Definition: YQComboBox.cc:215
virtual void setEnabled(bool enabled)
Set enabled / disabled state.
Definition: YQComboBox.cc:247
virtual void setText(const std::string &newText)
Change the text and handle visibility: If the new text is empty, hide this widget.
~YQComboBox()
Destructor.
Definition: YQComboBox.cc:83
YQComboBox(YWidget *parent, const std::string &label, bool editable)
Constructor.
Definition: YQComboBox.cc:45
void sendEvent(YEvent *event)
Widget event handlers (slots) call this when an event occured that should be the answer to a UserInpu...
Definition: YQUI.cc:469
virtual void addItem(YItem *item)
Add one item.
Definition: YQComboBox.cc:117
virtual void deleteAllItems()
Delete all items.
Definition: YQComboBox.cc:140
void slotSelected(int i)
Tells the ui that an item has been selected.
Definition: YQComboBox.cc:201
void setValidChars(const QString &newValidChars)
Set the valid input characters.
virtual bool setKeyboardFocus()
Accept the keyboard focus.
Definition: YQComboBox.cc:255
virtual int preferredWidth()
Preferred width of the widget.
Definition: YQComboBox.cc:229
Helper class for captions (labels) above a widget: Takes care of hiding itself when its text is empty...
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
Definition: YQComboBox.cc:241
virtual int preferredHeight()
Preferred height of the widget.
Definition: YQComboBox.cc:235
virtual void setInputMaxLength(int numberOfChars)
Specify the amount of characters which can be inserted.
Definition: YQComboBox.cc:222
virtual void setText(const std::string &newText)
Set this ComboBox's current value as text.
Definition: YQComboBox.cc:95
bool isValidText(const QString &txt) const
Returns 'true' if the given text is valid according to the current setting of ValidChars.
Definition: YQComboBox.cc:189
virtual State validate(QString &input, int &pos) const
Check user input.
static YQUI * ui()
Access the global Qt-UI.
Definition: YQUI.h:81