]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/YQRadioButton.cc
follow the widget state - for reference: you don't
[duncan/yast2-qt4.git] / src / YQRadioButton.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQRadioButton.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17 /-*/
18
19
20 #include <qradiobutton.h>
21 #include <QMouseEvent>
22 #include <q3boxlayout.h>
23 #define y2log_component "qt-ui"
24 #include <ycp/y2log.h>
25
26 #include "utf8.h"
27 #include "YQUI.h"
28 #include "YQApplication.h"
29 #include "YEvent.h"
30 #include "YQRadioButton.h"
31 #include "YRadioButtonGroup.h"
32 #include "YQSignalBlocker.h"
33
34 using std::string;
35
36 #define SPACING 8
37
38 // +----+----------------------------------+----+
39 // |    |(o) RadioButtonlabel              |    |
40 // +----+----------------------------------+----+
41 // <----> SPACING                          <---->
42
43
44
45 YQRadioButton::YQRadioButton( YWidget *         parent,
46                               const string &    label,
47                               bool              checked )
48     : Q3GroupBox( (QWidget *) (parent->widgetRep() ) )
49     , YRadioButton( parent, label )
50 {
51     setWidgetRep( this );
52     setFrameStyle( NoFrame );
53
54     QBoxLayout * layout = new QBoxLayout( this, QBoxLayout::LeftToRight );
55
56     _qt_radioButton = new QRadioButton( fromUTF8( label ), this );
57     
58     layout->addSpacing( SPACING );
59     layout->addWidget( _qt_radioButton );
60     layout->addSpacing( SPACING );
61     _qt_radioButton->setChecked( checked );
62
63     installEventFilter(this);
64
65     connect ( _qt_radioButton,  SIGNAL( toggled ( bool ) ),
66               this,             SLOT  ( changed ( bool ) ) );
67 }
68
69
70 void
71 YQRadioButton::setUseBoldFont( bool useBold )
72 {
73     _qt_radioButton->setFont( useBold ?
74                               YQUI::yqApp()->boldFont() :
75                               YQUI::yqApp()->currentFont() );
76     
77     YRadioButton::setUseBoldFont( useBold );
78 }
79
80
81 int YQRadioButton::preferredWidth()
82 {
83     return 2 * SPACING + _qt_radioButton->sizeHint().width();
84 }
85
86
87 int YQRadioButton::preferredHeight()
88 {
89     return _qt_radioButton->sizeHint().height();
90 }
91
92
93 void YQRadioButton::setSize( int newWidth, int newHeight )
94 {
95     _qt_radioButton->resize( newWidth - 2*SPACING, newHeight );
96     resize( newWidth, newHeight );
97 }
98
99
100 bool YQRadioButton::value()
101 {
102     return _qt_radioButton->isChecked();
103 }
104
105
106 void YQRadioButton::setValue( bool newValue )
107 {
108     YQSignalBlocker sigBlocker( _qt_radioButton );
109
110     _qt_radioButton->setChecked( newValue );
111
112     if ( newValue )
113     {
114         YRadioButtonGroup * group = buttonGroup();
115
116         if ( group )
117             group->uncheckOtherButtons( this );
118     }
119 }
120
121
122 void YQRadioButton::setLabel( const string & label )
123 {
124     _qt_radioButton->setText( fromUTF8( label ) );
125     YRadioButton::setLabel( label );
126 }
127
128
129 void YQRadioButton::setEnabled( bool enabled )
130 {
131     _qt_radioButton->setEnabled( enabled );
132     YWidget::setEnabled( enabled );
133 }
134
135
136 QRadioButton * YQRadioButton::getQtButton()
137 {
138     return _qt_radioButton;
139 }
140
141
142 bool YQRadioButton::setKeyboardFocus()
143 {
144     _qt_radioButton->setFocus();
145
146     return true;
147 }
148
149
150 // slots
151
152 void YQRadioButton::changed( bool newState )
153 {
154     if ( notify() && newState )
155         YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
156 }
157
158
159 bool YQRadioButton::eventFilter( QObject * obj, QEvent * event )
160 {
161     if ( event && event->type() == QEvent::MouseButtonRelease )
162     {
163         QMouseEvent * mouseEvent = dynamic_cast<QMouseEvent *> (event);
164
165         if ( mouseEvent && mouseEvent->button() == Qt::RightButton )
166         {
167              y2milestone( "Right click on button detected" );
168              YQUI::ui()->maybeLeftHandedUser();
169         }
170     }
171
172     return QObject::eventFilter( obj, event );
173 }
174
175
176 #include "YQRadioButton.moc"