]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/YQSlider.cc
restart qt4 porting
[duncan/yast2-qt4.git] / src / YQSlider.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQSlider.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17 /-*/
18
19
20 #include <qslider.h>
21 #include <qspinbox.h>
22 #include <qlabel.h>
23 #include <qvbox.h>
24
25 #define y2log_component "qt-ui"
26 #include <ycp/y2log.h>
27
28 #include "utf8.h"
29 #include "YQUI.h"
30 #include "YEvent.h"
31 #include "YQSlider.h"
32 #include "YQSignalBlocker.h"
33 #include "YQWidgetCaption.h"
34
35
36 YQSlider::YQSlider( YWidget *           parent,
37                     const string &      label,
38                     int                 minValue,
39                     int                 maxValue,
40                     int                 initialValue,
41                     bool                reverseLayout )
42
43     : QVBox( (QWidget *) parent->widgetRep() )
44     , YSlider( parent, label, minValue, maxValue )
45 {
46     setWidgetRep( this );
47
48     setSpacing( YQWidgetSpacing );
49     setMargin ( YQWidgetMargin );
50
51     _caption = new YQWidgetCaption( this, toUTF8( label ) );
52     YUI_CHECK_NEW( _caption );
53     
54     _hbox = new QHBox( this );
55     YUI_CHECK_NEW( _hbox );
56     
57     _hbox->setSpacing( YQWidgetSpacing );
58
59     if ( reverseLayout )
60     {
61         _qt_spinBox = new QSpinBox( minValue, maxValue,
62                                     1, // step
63                                     _hbox );
64     }
65     else
66     {
67         _caption->setAlignment( Qt::AlignRight );
68     }
69     
70     _qt_slider = new QSlider( minValue, maxValue,
71                               1, // pageStep
72                               initialValue,
73                               QSlider::Horizontal, _hbox );
74     YUI_CHECK_NEW( _qt_slider );
75
76     if ( ! reverseLayout )
77     {
78         _qt_spinBox = new QSpinBox( minValue, maxValue,
79                                     1, // step
80                                     _hbox );
81     }
82     YUI_CHECK_NEW( _qt_spinBox );
83     
84     _qt_spinBox->setValue( initialValue );
85     _caption->setBuddy( _qt_spinBox );
86
87     setValue( initialValue );
88
89     connect( _qt_spinBox, SIGNAL( valueChanged(int) ),
90              _qt_slider,  SLOT  ( setValue    (int) ) );
91
92     connect( _qt_slider,  SIGNAL( valueChanged(int) ),
93              _qt_spinBox, SLOT  ( setValue    (int) ) );
94
95     connect( _qt_spinBox, SIGNAL( valueChanged    (int) ),
96              this,        SLOT  ( valueChangedSlot(int) ) );
97 }
98
99
100 YQSlider::~YQSlider()
101 {
102     // NOP
103 }
104
105
106 int
107 YQSlider::value()
108 {
109     return _qt_spinBox->value();
110 }
111
112
113 void
114 YQSlider::setValueInternal( int newValue )
115 {
116     YQSignalBlocker sigBlocker1( _qt_spinBox );
117     YQSignalBlocker sigBlocker2( _qt_slider  );
118     _qt_slider->setValue ( newValue );
119     _qt_spinBox->setValue( newValue );
120 }
121
122
123 void
124 YQSlider::valueChangedSlot( int newValue )
125 {
126     if ( notify() )
127         YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
128     
129     emit valueChanged( newValue );
130 }
131
132
133 void
134 YQSlider::setEnabled( bool enabled )
135 {
136     _caption->setEnabled  ( enabled );
137     _qt_slider->setEnabled ( enabled );
138     _qt_spinBox->setEnabled( enabled );
139     YWidget::setEnabled( enabled );
140 }
141
142
143 int
144 YQSlider::preferredWidth()
145 {
146     int hintWidth = _caption->isShown() ? _caption->sizeHint().width() : 0;
147     
148     // Arbitrary value - there is no really good default
149     return max( 200, hintWidth );
150 }
151
152
153 int
154 YQSlider::preferredHeight()
155 {
156     return sizeHint().height();
157 }
158
159
160 void
161 YQSlider::setSize( int newWidth, int newHeight )
162 {
163     resize( newWidth, newHeight );
164 }
165
166
167 void
168 YQSlider::setLabel( const string & newLabel )
169 {
170     _caption->setText( newLabel );
171     YSlider::setLabel( newLabel );
172 }
173
174
175 bool
176 YQSlider::setKeyboardFocus()
177 {
178     _qt_spinBox->setFocus();
179
180     return true;
181 }
182
183
184 #include "YQSlider.moc"