libyui-qt  2.49.2
YQSlider.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: YQSlider.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 #include <QSlider>
26 #include <QSpinBox>
27 #include <QLabel>
28 #include <QVBoxLayout>
29 
30 #define YUILogComponent "qt-ui"
31 #include <yui/YUILog.h>
32 
33 #include "utf8.h"
34 #include "YQUI.h"
35 #include <yui/YEvent.h>
36 #include "YQSlider.h"
37 #include "YQSignalBlocker.h"
38 #include "YQWidgetCaption.h"
39 
40 
41 YQSlider::YQSlider( YWidget * parent,
42  const std::string & label,
43  int minValue,
44  int maxValue,
45  int initialValue,
46  bool reverseLayout )
47 
48  : QFrame( (QWidget *) parent->widgetRep() )
49  , YSlider( parent, label, minValue, maxValue )
50 {
51  setWidgetRep( this );
52 
53  QVBoxLayout* toplayout = new QVBoxLayout( this );
54  setLayout( toplayout );
55 
56  toplayout->setSpacing( YQWidgetSpacing );
57  toplayout->setMargin ( YQWidgetMargin );
58 
59  _caption = new YQWidgetCaption( this, label );
60  YUI_CHECK_NEW( _caption );
61  toplayout->addWidget( _caption );
62 
63  _hbox = new QFrame( this );
64  YUI_CHECK_NEW( _hbox );
65  toplayout->addWidget( _hbox );
66 
67  QHBoxLayout *layout = new QHBoxLayout( _hbox );
68  _hbox->setLayout( layout );
69 
70  layout->setMargin ( YQWidgetMargin );
71  layout->setSpacing( YQWidgetSpacing );
72 
73  if ( reverseLayout )
74  {
75  _qt_spinBox = new QSpinBox( _hbox );
76  _qt_spinBox->setMinimum(minValue);
77  _qt_spinBox->setMaximum(maxValue);
78  _qt_spinBox->setSingleStep(1);
79  layout->addWidget( _qt_spinBox );
80  }
81  else
82  {
83  _caption->setAlignment( Qt::AlignRight );
84  }
85 
86  _qt_slider = new QSlider( Qt::Horizontal, _hbox );
87  _qt_slider->setMinimum(minValue);
88  _qt_slider->setMaximum(maxValue);
89  _qt_slider->setPageStep(1);
90  YUI_CHECK_NEW( _qt_slider );
91  layout->addWidget( _qt_slider );
92 
93  if ( ! reverseLayout )
94  {
95  _qt_spinBox = new QSpinBox( _hbox );
96  _qt_spinBox->setMinimum(minValue);
97  _qt_spinBox->setMaximum(maxValue);
98  _qt_spinBox->setSingleStep(1);
99 
100  layout->addWidget( _qt_spinBox );
101  }
102  YUI_CHECK_NEW( _qt_spinBox );
103 
104  _qt_spinBox->setValue( initialValue );
105  _caption->setBuddy( _qt_spinBox );
106 
107  setValue( initialValue );
108 
109  connect( _qt_spinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
110  _qt_slider, &pclass(_qt_slider)::setValue );
111 
112  connect( _qt_slider, &pclass(_qt_slider)::valueChanged,
113  _qt_spinBox, &pclass(_qt_spinBox)::setValue );
114 
115  connect( _qt_spinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
116  this, &pclass(this)::valueChangedSlot );
117 }
118 
119 
121 {
122  // NOP
123 }
124 
125 
126 int
128 {
129  return _qt_spinBox->value();
130 }
131 
132 
133 void
135 {
136  YQSignalBlocker sigBlocker1( _qt_spinBox );
137  YQSignalBlocker sigBlocker2( _qt_slider );
138  _qt_slider->setValue ( newValue );
139  _qt_spinBox->setValue( newValue );
140 }
141 
142 
143 void
145 {
146  if ( notify() )
147  YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
148 
149  emit valueChanged( newValue );
150 }
151 
152 
153 void
154 YQSlider::setEnabled( bool enabled )
155 {
156  _caption->setEnabled ( enabled );
157  _qt_slider->setEnabled ( enabled );
158  _qt_spinBox->setEnabled( enabled );
159  YWidget::setEnabled( enabled );
160 }
161 
162 
163 int
165 {
166  int hintWidth = !_caption->isHidden() ? _caption->sizeHint().width() : 0;
167 
168  // Arbitrary value - there is no really good default
169  return std::max( 200, hintWidth );
170 }
171 
172 
173 int
175 {
176  return sizeHint().height();
177 }
178 
179 
180 void
181 YQSlider::setSize( int newWidth, int newHeight )
182 {
183  resize( newWidth, newHeight );
184 }
185 
186 
187 void
188 YQSlider::setLabel( const std::string & newLabel )
189 {
190  _caption->setText( newLabel );
191  YSlider::setLabel( newLabel );
192 }
193 
194 
195 bool
197 {
198  _qt_spinBox->setFocus();
199 
200  return true;
201 }
202 
203 
204 #include "YQSlider.moc"
Helper class to block Qt signals for QWidgets or QObjects as long as this object exists.
virtual ~YQSlider()
Destructor.
Definition: YQSlider.cc:120
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
Definition: YQSlider.cc:181
virtual void setText(const std::string &newText)
Change the text and handle visibility: If the new text is empty, hide this widget.
void valueChangedSlot(int newValue)
Slot for "value changed".
Definition: YQSlider.cc:144
virtual int preferredWidth()
Preferred width of the widget.
Definition: YQSlider.cc:164
virtual int preferredHeight()
Preferred height of the widget.
Definition: YQSlider.cc:174
YQSlider(YWidget *parent, const std::string &label, int minValue, int maxValue, int initialValue, bool reverseLayout=false)
Constructor.
Definition: YQSlider.cc:41
virtual int value()
Get the current value (the number entered by the user or set from the outside) of this slider...
Definition: YQSlider.cc:127
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:468
void valueChanged(int newValue)
Emitted when the value changes (regardless of the notify flag).
virtual bool setKeyboardFocus()
Accept the keyboard focus.
Definition: YQSlider.cc:196
virtual void setLabel(const std::string &label)
Set the label (the caption above the input field).
Definition: YQSlider.cc:188
virtual void setValueInternal(int val)
Set the current value (the number entered by the user or set from the outside) of this slider...
Definition: YQSlider.cc:134
Helper class for captions (labels) above a widget: Takes care of hiding itself when its text is empty...
virtual void setEnabled(bool enabled)
Set enabled/disabled state.
Definition: YQSlider.cc:154
static YQUI * ui()
Access the global Qt-UI.
Definition: YQUI.h:80