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