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