]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/YQPartitionSplitter.cc
follow the widget state - for reference: you don't
[duncan/yast2-qt4.git] / src / YQPartitionSplitter.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQPartitionSplitter.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17 /-*/
18
19
20 #define y2log_component "qt-ui"
21 #include <ycp/y2log.h>
22
23 #include "utf8.h"
24 #include "YQUI.h"
25 #include "YEvent.h"
26 #include "YQWidgetFactory.h"
27 #include "YQOptionalWidgetFactory.h"
28 #include "YQPartitionSplitter.h"
29 #include "YQLayoutBox.h"
30 #include "YQBarGraph.h"
31 #include "YQIntField.h"
32 #include "YQSlider.h"
33 #include "YQSignalBlocker.h"
34
35
36 YQPartitionSplitter::YQPartitionSplitter( YWidget *             parent,
37                                           int                   usedSize,
38                                           int                   totalFreeSize,
39                                           int                   newPartSize,
40                                           int                   minNewSize,
41                                           int                   minFreeSize,
42                                           const string &        usedLabel,
43                                           const string &        freeLabel,
44                                           const string &        newPartLabel,
45                                           const string &        freeFieldLabel,
46                                           const string &        newPartFieldLabel )
47     : QWidget( (QWidget *) parent->widgetRep() )
48     , YPartitionSplitter( parent,
49                           usedSize,
50                           totalFreeSize,
51                           newPartSize,
52                           minNewSize,
53                           minFreeSize,
54                           usedLabel,
55                           freeLabel,
56                           newPartLabel,
57                           freeFieldLabel,
58                           newPartFieldLabel )
59       , _vbox( 0 )
60       , _barGraph( 0 )
61       , _hbox( 0 )
62       , _freeSizeSlider( 0 )
63       , _newPartField( 0 )
64 {
65     setWidgetRep( this );
66
67     // Replace children manager so it will accept one single direct child (the outer vbox)
68     setChildrenManager( new YSingleWidgetChildManager( this ) );
69
70     //
71     // Create internal widgets
72     //
73     
74     _vbox       = YUI::widgetFactory()->createVBox( this );
75     _barGraph   = dynamic_cast<YQBarGraph *> ( YUI::optionalWidgetFactory()->createBarGraph( _vbox ) );
76     YUI_CHECK_PTR( _barGraph );
77
78     int freeSize = totalFreeSize - newPartSize;
79     
80     {
81         YBarGraphMultiUpdate multiUpdate( _barGraph );
82
83         _barGraph->addSegment( YBarGraphSegment( usedSize,    usedLabel ) );
84         _barGraph->addSegment( YBarGraphSegment( freeSize,    freeLabel ) );
85         _barGraph->addSegment( YBarGraphSegment( newPartSize, newPartLabel) );
86     }
87
88     _hbox          = YUI::widgetFactory()->createHBox( _vbox );
89
90     _freeSizeSlider = new YQSlider( _hbox, freeFieldLabel,
91                                     minFreeSize, maxFreeSize(), freeSize,
92                                     true ); // reverseLayout (put QSpinBox left of QSlider)
93     YUI_CHECK_PTR( _freeSizeSlider );
94     _freeSizeSlider->setStretchable( YD_HORIZ, true );
95
96     _newPartField = new YQIntField( _hbox, newPartFieldLabel,
97                                     minNewSize, maxNewPartSize(), newPartSize );
98     YUI_CHECK_PTR( _newPartField );
99     _newPartField->setStretchable( YD_HORIZ, false );
100
101
102     // Connect signals
103
104     connect( _newPartField,     SIGNAL( valueChanged      (int) ),
105              this,              SLOT  ( setNewPartSizeSlot(int) ) );
106
107     connect( _freeSizeSlider,   SIGNAL( valueChanged   (int) ),
108              this,              SLOT  ( setFreeSizeSlot(int) ) );
109 }
110
111
112 YQPartitionSplitter::~YQPartitionSplitter()
113 {
114     // NOP
115 }
116
117
118 void YQPartitionSplitter::setEnabled( bool enabled )
119 {
120     _freeSizeSlider->setEnabled( enabled );
121     _newPartField->setEnabled  ( enabled );
122
123     YWidget::setEnabled( enabled );
124 }
125
126
127 int YQPartitionSplitter::preferredWidth()
128 {
129     return _vbox->preferredWidth();
130 }
131
132
133 int YQPartitionSplitter::preferredHeight()
134 {
135     return _vbox->preferredHeight();
136 }
137
138
139 void YQPartitionSplitter::setSize( int newWidth, int newHeight )
140 {
141     QWidget::resize( newWidth, newHeight );
142     _vbox->setSize ( newWidth, newHeight );
143 }
144
145
146 int YQPartitionSplitter::value()
147 {
148     YUI_CHECK_PTR( _newPartField );
149     
150     return _newPartField->value();
151 }
152
153
154 void YQPartitionSplitter::setValue( int newValue )
155 {
156     YUI_CHECK_PTR( _barGraph       );
157     YUI_CHECK_PTR( _freeSizeSlider );
158     YUI_CHECK_PTR( _newPartField   );
159                   
160     YQSignalBlocker sigBlocker1( _barGraph       );
161     YQSignalBlocker sigBlocker2( _freeSizeSlider );
162     YQSignalBlocker sigBlocker3( _newPartField   );
163
164     _newPartField->setValue( newValue );
165
166     int freeSize = totalFreeSize() - newValue;
167     _freeSizeSlider->setValue( freeSize );
168
169     YBarGraphMultiUpdate multiUpdate( _barGraph );
170     {
171         _barGraph->setValue( freeSegment,    freeSize );
172         _barGraph->setValue( newPartSegment, newValue );
173     }
174 }
175
176
177 void YQPartitionSplitter::setFreeSizeSlot( int newFreeSize )
178 {
179     int newPartSize = totalFreeSize() - newFreeSize;
180
181     setValue( newPartSize );
182
183     if ( notify() )
184         YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
185 }
186
187
188 void YQPartitionSplitter::setNewPartSizeSlot( int newPartSize )
189 {
190     setValue( newPartSize );
191
192     if ( notify() )
193         YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
194 }
195
196
197 bool YQPartitionSplitter::setKeyboardFocus()
198 {
199     _newPartField->setKeyboardFocus();
200
201     return true;
202 }
203
204
205 #include "YQPartitionSplitter.moc"