libyui-qt  2.46.13
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 
42 #define SPACING 8
43 
44 // +----+----------------------------------+----+
45 // | |(o) RadioButtonlabel | |
46 // +----+----------------------------------+----+
47 // <----> SPACING <---->
48 
49 
50 
51 YQRadioButton::YQRadioButton( YWidget * parent,
52  const std::string & label,
53  bool checked )
54  : QRadioButton( fromUTF8( label ), ( QWidget *) (parent->widgetRep() ) )
55  , YRadioButton( parent, label )
56 {
57  setWidgetRep( this );
58 
59  // QRadioButton uses its own logic by default to make sure that only one
60  // button of a radio box is checked at any time, but this interferes with
61  // YRadioButtonGroup. Let YRadioButtonGroup and YQRadioButton::changed()
62  // handle this.
63  setAutoExclusive( false );
64 
65  setChecked( checked );
66 
67  installEventFilter(this);
68 
69  connect ( this, &pclass(this)::toggled,
70  this, &pclass(this)::changed );
71 }
72 
73 
74 void
76 {
77  setFont( useBold ?
78  YQUI::yqApp()->boldFont() :
79  YQUI::yqApp()->currentFont() );
80 
81  YRadioButton::setUseBoldFont( useBold );
82 }
83 
84 
86 {
87  return sizeHint().width();
88 }
89 
90 
92 {
93  return sizeHint().height();
94 }
95 
96 
97 void YQRadioButton::setSize( int newWidth, int newHeight )
98 {
99  resize( newWidth, newHeight );
100 }
101 
102 
104 {
105  return isChecked();
106 }
107 
108 
109 void YQRadioButton::setValue( bool newValue )
110 {
111  YQSignalBlocker sigBlocker( this );
112 
113  // yuiDebug() << "Setting " << this << (newValue ? " on" : " off") << std::endl;
114  setChecked( newValue );
115 
116  if ( newValue )
117  {
118  YRadioButtonGroup * group = buttonGroup();
119 
120  if ( group )
121  group->uncheckOtherButtons( this );
122  }
123 }
124 
125 
126 void YQRadioButton::setLabel( const std::string & label )
127 {
128  setText( fromUTF8( label ) );
129  YRadioButton::setLabel( label );
130 }
131 
132 
133 void YQRadioButton::setEnabled( bool enabled )
134 {
135  QRadioButton::setEnabled( enabled );
136  YWidget::setEnabled( enabled );
137 }
138 
139 
141 {
142  setFocus();
143 
144  return true;
145 }
146 
147 
148 void YQRadioButton::changed( bool newState )
149 {
150  if ( newState )
151  {
152  yuiDebug() << "User set " << this << ( newState ? " on" : " off" ) << std::endl;
153  YRadioButtonGroup * group = buttonGroup();
154 
155  if ( group )
156  group->uncheckOtherButtons( this );
157 
158  if ( notify() )
159  YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
160  }
161  else
162  {
163  // prevent that the user deselects all items
164  setChecked( true );
165  }
166 }
167 
168 
169 bool YQRadioButton::eventFilter( QObject * obj, QEvent * event )
170 {
171  if ( event && event->type() == QEvent::MouseButtonRelease )
172  {
173  QMouseEvent * mouseEvent = dynamic_cast<QMouseEvent *> (event);
174 
175  if ( mouseEvent && mouseEvent->button() == Qt::RightButton )
176  {
177  yuiMilestone() << "Right click on button detected" << std::endl;
179  }
180  }
181 
182  return QObject::eventFilter( obj, event );
183 }
184 
185 
186 #include "YQRadioButton.moc"
virtual void setValue(bool checked)
Set the "checked" state of the RadioButton.
Helper class to block Qt signals for QWidgets or QObjects as long as this object exists.
virtual void setLabel(const std::string &label)
Change the label (the text) on the RadioButton.
YQRadioButton(YWidget *parent, const std::string &label, bool checked)
Constructor.
virtual void setUseBoldFont(bool bold=true)
Use a bold font.
static YQApplication * yqApp()
Return the global YApplication object as YQApplication.
Definition: YQUI.cc:284
virtual int preferredWidth()
Preferred width of the widget.
bool eventFilter(QObject *obj, QEvent *event)
Redirect events to this object.
void maybeLeftHandedUser()
A mouse click with the wrong mouse button was detected - e.g., a right click on a push button...
virtual bool setKeyboardFocus()
Accept the keyboard focus.
void changed(bool newState)
Triggered when the RadioButton is toggled.
virtual bool value()
Return the "checked" state of the RadioButton.
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:486
virtual int preferredHeight()
Preferred height of the widget.
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
virtual void setEnabled(bool enabled)
Set enabled / disabled state.
static YQUI * ui()
Access the global Qt-UI.
Definition: YQUI.h:81