libyui-qt  2.44.0
 All Classes Functions Variables
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, SIGNAL( toggled ( bool ) ),
70  this, SLOT ( changed ( bool ) ) );
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)
virtual void setLabel(const std::string &label)
YQRadioButton(YWidget *parent, const std::string &label, bool checked)
virtual void setUseBoldFont(bool bold=true)
static YQApplication * yqApp()
Definition: YQUI.cc:283
virtual int preferredWidth()
bool eventFilter(QObject *obj, QEvent *event)
void maybeLeftHandedUser()
virtual bool setKeyboardFocus()
void changed(bool newState)
virtual bool value()
void sendEvent(YEvent *event)
Definition: YQUI.cc:476
virtual int preferredHeight()
virtual void setSize(int newWidth, int newHeight)
virtual void setEnabled(bool enabled)
static YQUI * ui()
Definition: YQUI.h:74