picking up branches/tmp/sh/qt4-port/, merging it with trunk
[duncan/yast2-qt4.git] / src / YQCheckBox.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQCheckBox.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17 /-*/
18
19
20 #include <qcheckbox.h>
21 #include <q3boxlayout.h>
22 #define y2log_component "qt-ui"
23 #include <ycp/y2log.h>
24
25 #include "utf8.h"
26 #include "YQApplication.h"
27 #include "YQUI.h"
28 #include "YEvent.h"
29 #include "YQCheckBox.h"
30
31
32 #define SPACING 8
33
34
35 YQCheckBox::YQCheckBox( YWidget *       parent,
36                         const string &  label,
37                         bool            checked )
38     : Q3GroupBox( (QWidget *) parent->widgetRep() )
39     , YCheckBox( parent, label )
40 {
41     setWidgetRep( this );
42     setFrameStyle( NoFrame );
43
44     QBoxLayout * layout = new QBoxLayout( this, QBoxLayout::LeftToRight );
45
46     _qt_checkBox = new QCheckBox( fromUTF8( label ), this );
47     YUI_CHECK_NEW( _qt_checkBox );
48
49     layout->addSpacing( SPACING );
50     layout->addWidget( _qt_checkBox );
51     layout->addSpacing( SPACING );
52
53     _qt_checkBox->setChecked( checked );
54
55     connect( _qt_checkBox,      SIGNAL( stateChanged( int ) ),
56              this,              SLOT  ( stateChanged( int ) ) );
57 }
58
59
60 YQCheckBox::~YQCheckBox()
61 {
62     // NOP
63 }
64
65
66 YCheckBoxState
67 YQCheckBox::value()
68 {
69     switch ( _qt_checkBox->checkState() )
70     {
71         case Qt::Checked:          return YCheckBox_on;
72         case Qt::Unchecked:        return YCheckBox_off;
73         case Qt::PartiallyChecked: return YCheckBox_dont_care;
74     }
75
76     return YCheckBox_off;
77 }
78
79
80 void
81 YQCheckBox::setValue( YCheckBoxState newValue )
82 {
83     switch ( newValue )
84     {
85         case YCheckBox_on:
86             _qt_checkBox->setChecked( true );
87             _qt_checkBox->setTristate( false );
88             break;
89
90         case YCheckBox_off:
91             _qt_checkBox->setChecked( false );
92             _qt_checkBox->setTristate( false );
93             break;
94
95         case YCheckBox_dont_care:
96             _qt_checkBox->setTristate( true );
97             _qt_checkBox->setNoChange();
98             break;
99     }
100 }
101
102
103 void YQCheckBox::setLabel( const string & label )
104 {
105     _qt_checkBox->setText( fromUTF8( label ) );
106     YCheckBox::setLabel( label );
107 }
108
109
110 void YQCheckBox::setUseBoldFont( bool useBold )
111 {
112     _qt_checkBox->setFont( useBold ?
113                            YQUI::yqApp()->boldFont() :
114                            YQUI::yqApp()->currentFont() );
115
116     YCheckBox::setUseBoldFont( useBold );
117 }
118
119
120 void YQCheckBox::setEnabled( bool enabled )
121 {
122     _qt_checkBox->setEnabled( enabled );
123     YWidget::setEnabled( enabled );
124 }
125
126
127 int YQCheckBox::preferredWidth()
128 {
129     return 2*SPACING + _qt_checkBox->sizeHint().width();
130 }
131
132
133 int YQCheckBox::preferredHeight()
134 {
135     return _qt_checkBox->sizeHint().height();
136 }
137
138
139 void YQCheckBox::setSize( int newWidth, int newHeight )
140 {
141     _qt_checkBox->resize( newWidth - 2*SPACING, newHeight );
142     resize( newWidth, newHeight );
143 }
144
145
146 bool YQCheckBox::setKeyboardFocus()
147 {
148     _qt_checkBox->setFocus();
149
150     return true;
151 }
152
153
154 void YQCheckBox::stateChanged( int newState )
155 {
156     // y2milestone( "new state: %d", newState );
157
158     if ( notify() )
159         YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
160 }
161
162
163 #include "YQCheckBox.moc"