libyui-qt  2.47.1.1
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 #include <qpixmap.h>
33 #define YUILogComponent "qt-ui"
34 #include <yui/YUILog.h>
35 
36 #include "utf8.h"
37 #include "YQUI.h"
38 #include <yui/YEvent.h>
39 #include "QY2CharValidator.h"
40 #include "YQComboBox.h"
41 #include "YQSignalBlocker.h"
42 #include "YQWidgetCaption.h"
43 #include <QVBoxLayout>
44 #include <QDebug>
45 
46 YQComboBox::YQComboBox( YWidget * parent,
47  const std::string & label,
48  bool editable )
49  : QFrame( (QWidget *) parent->widgetRep() )
50  , YComboBox( parent, label, editable )
51  , _validator(0)
52 {
53  QVBoxLayout* layout = new QVBoxLayout( this );
54  setLayout( layout );
55 
56  setWidgetRep( this );
57  layout->setSpacing( YQWidgetSpacing );
58  layout->setMargin ( YQWidgetMargin );
59 
60  _caption = new YQWidgetCaption( this, label );
61  YUI_CHECK_NEW( _caption );
62  layout->addWidget( _caption );
63 
64  _qt_comboBox = new QComboBox(this);
65  _qt_comboBox->setEditable(editable);
66  YUI_CHECK_NEW( _caption );
67  layout->addWidget( _qt_comboBox );
68 
69  _caption->setBuddy( _qt_comboBox );
70 
71 #if SEND_SELECTION_CHANGED_EVENT
72  connect( _qt_comboBox, &pclass(_qt_comboBox)::highlighted,
73  this, &pclass(this)::slotSelected );
74 #endif
75 
76  connect( _qt_comboBox, static_cast<void (QComboBox::*)(const QString&)>(&QComboBox::activated),
77  this, &pclass(this)::textChanged );
78 
79  connect( _qt_comboBox, &pclass(_qt_comboBox)::editTextChanged,
80  this, &pclass(this)::textChanged );
81 }
82 
83 
85 {
86  // NOP
87 }
88 
89 
91 {
92  return toUTF8( _qt_comboBox->currentText() );
93 }
94 
95 
96 void YQComboBox::setText( const std::string & newValue )
97 {
98  QString text = fromUTF8( newValue );
99 
100  if ( isValidText( text ) )
101  {
102  YQSignalBlocker sigBlocker( _qt_comboBox );
103  int index = _qt_comboBox->findText( text );
104  if ( index < 0 )
105  _qt_comboBox->setEditText( text );
106  else {
107  _qt_comboBox->setCurrentIndex( index );
108  _qt_comboBox->setItemText(index, text );
109  }
110  }
111  else
112  {
113  yuiError() << this << ": Rejecting invalid value \"" << newValue << "\"" << std::endl;
114  }
115 }
116 
117 
118 void YQComboBox::addItem( YItem * item )
119 {
120  YComboBox::addItem( item );
121  QIcon icon;
122 
123  if ( item->hasIconName() )
124  {
125  string iconName = iconFullPath( item );
126  icon = QIcon( iconName.c_str() );
127 
128  if ( icon.isNull() )
129  yuiWarning() << "Can't load icon \"" << iconName << "\"" << std::endl;
130  }
131 
132  if ( icon.isNull() )
133  _qt_comboBox->addItem( fromUTF8( item->label() ) );
134  else
135  _qt_comboBox->addItem( icon, fromUTF8( item->label() ) );
136 
137  if ( item->selected() )
138  {
139  YQSignalBlocker sigBlocker( _qt_comboBox );
140  setText( item->label() );
141  }
142 }
143 
144 
146 {
147  YQSignalBlocker sigBlocker( _qt_comboBox );
148 
149  _qt_comboBox->clear();
150  YComboBox::deleteAllItems();
151 }
152 
153 
154 void YQComboBox::setLabel( const std::string & label )
155 {
156  _caption->setText( label );
157  YComboBox::setLabel( label );
158 }
159 
160 
161 void YQComboBox::setValidChars( const std::string & newValidChars )
162 {
163  if ( ! _qt_comboBox->isEditable() )
164  {
165  yuiWarning() << this << ": Setting ValidChars is useless on a combo box that isn't editable!" << std::endl;
166  return;
167  }
168 
169  if ( _validator )
170  {
171  _validator->setValidChars( fromUTF8( newValidChars ) );
172  }
173  else
174  {
175  _validator = new QY2CharValidator( fromUTF8( newValidChars ), this );
176  _qt_comboBox->setValidator( _validator );
177 
178  // No need to delete the validator in the destructor - Qt will take
179  // care of that since it's a QObject with a parent!
180  }
181 
182  if ( ! isValidText( _qt_comboBox->currentText() ) )
183  {
184  yuiError() << this << ": Old value \"" << _qt_comboBox->currentText()
185  << " \" invalid according to new ValidChars \""<< newValidChars << "\" - deleting"
186  << std::endl;
187  _qt_comboBox->setItemText(_qt_comboBox->currentIndex(), "");
188  }
189 
190  YComboBox::setValidChars( newValidChars );
191 }
192 
193 
194 bool YQComboBox::isValidText( const QString & txt ) const
195 {
196  if ( ! _validator )
197  return true;
198 
199  int pos = 0;
200  QString text( txt ); // need a non-const QString &
201 
202  return _validator->validate( text, pos ) == QValidator::Acceptable;
203 }
204 
205 
207 {
208  if ( notify() )
209  {
210  if ( ! YQUI::ui()->eventPendingFor( this ) )
211  {
212  // Avoid overwriting a (more important) ValueChanged event with a SelectionChanged event
213 
214  YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::SelectionChanged ) );
215  }
216  }
217 }
218 
219 
220 void YQComboBox::textChanged( QString )
221 {
222  if ( notify() )
223  YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
224 }
225 
226 
228 {
229  _qt_comboBox->lineEdit()->setMaxLength( len );
230  YComboBox::setInputMaxLength( len );
231 }
232 
233 
235 {
236  return sizeHint().width();
237 }
238 
239 
241 {
242  return sizeHint().height();
243 }
244 
245 
246 void YQComboBox::setSize( int newWidth, int newHeight )
247 {
248  resize( newWidth, newHeight );
249 }
250 
251 
252 void YQComboBox::setEnabled( bool enabled )
253 {
254  _caption->setEnabled( enabled );
255  _qt_comboBox->setEnabled( enabled );
256  YWidget::setEnabled( enabled );
257 }
258 
259 
261 {
262  _qt_comboBox->setFocus();
263 
264  return true;
265 }
266 
267 
268 #include "YQComboBox.moc"
virtual void setValidChars(const std::string &validChars)
Change the valid input characters.
Definition: YQComboBox.cc:161
Helper class to block Qt signals for QWidgets or QObjects as long as this object exists.
virtual std::string text()
Return this ComboBox&#39;s current value as text.
Definition: YQComboBox.cc:90
virtual void setLabel(const std::string &label)
Change the label text.
Definition: YQComboBox.cc:154
void textChanged(QString)
Tells the ui that the user has edited the text ( if the &#39;editable&#39; option is set ).
Definition: YQComboBox.cc:220
virtual void setEnabled(bool enabled)
Set enabled / disabled state.
Definition: YQComboBox.cc:252
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:84
virtual State validate(QString &input, int &pos) const
Check user input.
YQComboBox(YWidget *parent, const std::string &label, bool editable)
Constructor.
Definition: YQComboBox.cc:46
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:494
virtual void addItem(YItem *item)
Add one item.
Definition: YQComboBox.cc:118
virtual void deleteAllItems()
Delete all items.
Definition: YQComboBox.cc:145
void slotSelected(int i)
Tells the ui that an item has been selected.
Definition: YQComboBox.cc:206
bool isValidText(const QString &txt) const
Returns &#39;true&#39; if the given text is valid according to the current setting of ValidChars.
Definition: YQComboBox.cc:194
void setValidChars(const QString &newValidChars)
Set the valid input characters.
virtual bool setKeyboardFocus()
Accept the keyboard focus.
Definition: YQComboBox.cc:260
virtual int preferredWidth()
Preferred width of the widget.
Definition: YQComboBox.cc:234
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:246
virtual int preferredHeight()
Preferred height of the widget.
Definition: YQComboBox.cc:240
virtual void setInputMaxLength(int numberOfChars)
Specify the amount of characters which can be inserted.
Definition: YQComboBox.cc:227
virtual void setText(const std::string &newText)
Set this ComboBox&#39;s current value as text.
Definition: YQComboBox.cc:96
static YQUI * ui()
Access the global Qt-UI.
Definition: YQUI.h:80