picking up branches/tmp/sh/qt4-port/, merging it with trunk
[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 <QVBoxLayout>
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     : QFrame( (QWidget *) parent->widgetRep() )
44     , YSlider( parent, label, minValue, maxValue )
45 {
46     setWidgetRep( this );
47
48     QVBoxLayout* layout = new QVBoxLayout( this );
49     setLayout( layout );
50
51     layout->setSpacing( YQWidgetSpacing );
52     layout->setMargin ( YQWidgetMargin );
53
54     _caption = new YQWidgetCaption( this, label );
55     YUI_CHECK_NEW( _caption );
56     layout->addWidget( _caption );
57     
58     _hbox = new QFrame( this );
59     YUI_CHECK_NEW( _hbox );
60     layout->addWidget( _hbox );
61
62     layout = new QVBoxLayout( _hbox );
63     _hbox->setLayout( layout );
64
65     layout->setMargin ( YQWidgetMargin );
66     layout->setSpacing( YQWidgetSpacing );
67
68     if ( reverseLayout )
69     {
70         _qt_spinBox = new QSpinBox( minValue, maxValue,
71                                     1, // step
72                                     _hbox );
73         layout->addWidget( _qt_spinBox );
74     }
75     else
76     {
77         _caption->setAlignment( Qt::AlignRight );
78     }
79     
80     _qt_slider = new QSlider( minValue, maxValue,
81                               1, // pageStep
82                               initialValue,
83                               Qt::Horizontal, _hbox );
84     YUI_CHECK_NEW( _qt_slider );
85     layout->addWidget( _qt_slider );
86
87     if ( ! reverseLayout )
88     {
89         _qt_spinBox = new QSpinBox( minValue, maxValue,
90                                     1, // step
91                                     _hbox );
92         layout->addWidget( _qt_spinBox );
93     }
94     YUI_CHECK_NEW( _qt_spinBox );
95     
96     _qt_spinBox->setValue( initialValue );
97     _caption->setBuddy( _qt_spinBox );
98
99     setValue( initialValue );
100
101     connect( _qt_spinBox, SIGNAL( valueChanged(int) ),
102              _qt_slider,  SLOT  ( setValue    (int) ) );
103
104     connect( _qt_slider,  SIGNAL( valueChanged(int) ),
105              _qt_spinBox, SLOT  ( setValue    (int) ) );
106
107     connect( _qt_spinBox, SIGNAL( valueChanged    (int) ),
108              this,        SLOT  ( valueChangedSlot(int) ) );
109 }
110
111
112 YQSlider::~YQSlider()
113 {
114     // NOP
115 }
116
117
118 int
119 YQSlider::value()
120 {
121     return _qt_spinBox->value();
122 }
123
124
125 void
126 YQSlider::setValueInternal( int newValue )
127 {
128     YQSignalBlocker sigBlocker1( _qt_spinBox );
129     YQSignalBlocker sigBlocker2( _qt_slider  );
130     _qt_slider->setValue ( newValue );
131     _qt_spinBox->setValue( newValue );
132 }
133
134
135 void
136 YQSlider::valueChangedSlot( int newValue )
137 {
138     if ( notify() )
139         YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
140     
141     emit valueChanged( newValue );
142 }
143
144
145 void
146 YQSlider::setEnabled( bool enabled )
147 {
148     _caption->setEnabled  ( enabled );
149     _qt_slider->setEnabled ( enabled );
150     _qt_spinBox->setEnabled( enabled );
151     YWidget::setEnabled( enabled );
152 }
153
154
155 int
156 YQSlider::preferredWidth()
157 {
158     int hintWidth = _caption->isShown() ? _caption->sizeHint().width() : 0;
159     
160     // Arbitrary value - there is no really good default
161     return max( 200, hintWidth );
162 }
163
164
165 int
166 YQSlider::preferredHeight()
167 {
168     return sizeHint().height();
169 }
170
171
172 void
173 YQSlider::setSize( int newWidth, int newHeight )
174 {
175     resize( newWidth, newHeight );
176 }
177
178
179 void
180 YQSlider::setLabel( const string & newLabel )
181 {
182     _caption->setText( newLabel );
183     YSlider::setLabel( newLabel );
184 }
185
186
187 bool
188 YQSlider::setKeyboardFocus()
189 {
190     _qt_spinBox->setFocus();
191
192     return true;
193 }
194
195
196 #include "YQSlider.moc"