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