libyui-qt  2.53.0
YQRadioButton.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YQRadioButton.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 #include <qradiobutton.h>
26 #include <QMouseEvent>
27 #include <QBoxLayout>
28 
29 #define YUILogComponent "qt-ui"
30 #include <yui/YUILog.h>
31 
32 #include "utf8.h"
33 #include "YQUI.h"
34 #include "YQApplication.h"
35 #include <yui/YEvent.h>
36 #include "YQRadioButton.h"
37 #include <yui/YRadioButtonGroup.h>
38 #include "YQSignalBlocker.h"
39 
40 using std::string;
41 using std::endl;
42 
43 #define SPACING 8
44 
45 // +----+----------------------------------+----+
46 // | |(o) RadioButtonlabel | |
47 // +----+----------------------------------+----+
48 // <----> SPACING <---->
49 
50 
51 
52 YQRadioButton::YQRadioButton( YWidget * parent,
53  const string & label,
54  bool checked )
55  : QRadioButton( fromUTF8( label ), ( QWidget *) (parent->widgetRep() ) )
56  , YRadioButton( parent, label )
57 {
58  setWidgetRep( this );
59 
60  // QRadioButton uses its own logic by default to make sure that only one
61  // button of a radio box is checked at any time, but this interferes with
62  // YRadioButtonGroup. Let YRadioButtonGroup and YQRadioButton::changed()
63  // handle this.
64  setAutoExclusive( false );
65 
66  setChecked( checked );
67 
68  installEventFilter(this);
69 
70  connect ( this, &pclass(this)::toggled,
71  this, &pclass(this)::changed );
72 }
73 
74 
75 void
77 {
78  setFont( useBold ?
79  YQUI::yqApp()->boldFont() :
80  YQUI::yqApp()->currentFont() );
81 
82  YRadioButton::setUseBoldFont( useBold );
83 }
84 
85 
87 {
88  return sizeHint().width();
89 }
90 
91 
93 {
94  return sizeHint().height();
95 }
96 
97 
98 void YQRadioButton::setSize( int newWidth, int newHeight )
99 {
100  resize( newWidth, newHeight );
101 }
102 
103 
105 {
106  return isChecked();
107 }
108 
109 
110 void YQRadioButton::setValue( bool newValue )
111 {
112  YQSignalBlocker sigBlocker( this );
113 
114  // yuiDebug() << "Setting " << this << (newValue ? " on" : " off") << endl;
115  setChecked( newValue );
116 
117  if ( newValue )
118  {
119  YRadioButtonGroup * group = buttonGroup();
120 
121  if ( group )
122  group->uncheckOtherButtons( this );
123  }
124 }
125 
126 
127 void YQRadioButton::setLabel( const string & label )
128 {
129  setText( fromUTF8( label ) );
130  YRadioButton::setLabel( label );
131 }
132 
133 
134 void YQRadioButton::setEnabled( bool enabled )
135 {
136  QRadioButton::setEnabled( enabled );
137  YWidget::setEnabled( enabled );
138 }
139 
140 
142 {
143  setFocus();
144 
145  return true;
146 }
147 
148 
149 void YQRadioButton::changed( bool newState )
150 {
151  if ( newState )
152  {
153  yuiDebug() << "User set " << this << ( newState ? " on" : " off" ) << endl;
154  YRadioButtonGroup * group = buttonGroup();
155 
156  if ( group )
157  group->uncheckOtherButtons( this );
158 
159  if ( notify() )
160  YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
161  }
162  else
163  {
164  // prevent that the user deselects all items
165  setChecked( true );
166  }
167 }
168 
169 
170 bool YQRadioButton::eventFilter( QObject * obj, QEvent * event )
171 {
172  if ( event && event->type() == QEvent::MouseButtonRelease )
173  {
174  QMouseEvent * mouseEvent = dynamic_cast<QMouseEvent *> (event);
175 
176  if ( mouseEvent && mouseEvent->button() == Qt::RightButton )
177  {
178  yuiMilestone() << "Right click on button detected" << endl;
180  }
181  }
182 
183  return QObject::eventFilter( obj, event );
184 }
YQSignalBlocker
Helper class to block Qt signals for QWidgets or QObjects as long as this object exists.
Definition: YQSignalBlocker.h:37
YQRadioButton::setKeyboardFocus
virtual bool setKeyboardFocus()
Accept the keyboard focus.
Definition: YQRadioButton.cc:141
YQRadioButton::setSize
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
Definition: YQRadioButton.cc:98
YQUI::sendEvent
void sendEvent(YEvent *event)
Widget event handlers (slots) call this when an event occured that should be the answer to a UserInpu...
Definition: YQUI.cc:480
YQRadioButton::value
virtual bool value()
Return the "checked" state of the RadioButton.
Definition: YQRadioButton.cc:104
YQApplication::maybeLeftHandedUser
void maybeLeftHandedUser()
A mouse click with the wrong mouse button was detected - e.g., a right click on a push button.
Definition: YQApplication.cc:642
YQRadioButton::preferredWidth
virtual int preferredWidth()
Preferred width of the widget.
Definition: YQRadioButton.cc:86
YQRadioButton::preferredHeight
virtual int preferredHeight()
Preferred height of the widget.
Definition: YQRadioButton.cc:92
YQRadioButton::YQRadioButton
YQRadioButton(YWidget *parent, const std::string &label, bool checked)
Constructor.
Definition: YQRadioButton.cc:52
YQRadioButton::eventFilter
bool eventFilter(QObject *obj, QEvent *event)
Redirect events to this object.
Definition: YQRadioButton.cc:170
YQRadioButton::setLabel
virtual void setLabel(const std::string &label)
Change the label (the text) on the RadioButton.
Definition: YQRadioButton.cc:127
YQUI::ui
static YQUI * ui()
Access the global Qt-UI.
Definition: YQUI.h:83
YQRadioButton::changed
void changed(bool newState)
Triggered when the RadioButton is toggled.
Definition: YQRadioButton.cc:149
YQUI::yqApp
static YQApplication * yqApp()
Return the global YApplication object as YQApplication.
Definition: YQUI.cc:268
YQRadioButton::setUseBoldFont
virtual void setUseBoldFont(bool bold=true)
Use a bold font.
Definition: YQRadioButton.cc:76
YQRadioButton::setEnabled
virtual void setEnabled(bool enabled)
Set enabled / disabled state.
Definition: YQRadioButton.cc:134
YQRadioButton::setValue
virtual void setValue(bool checked)
Set the "checked" state of the RadioButton.
Definition: YQRadioButton.cc:110