libyui-qt  2.53.0
YQGenericButton.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: YQGenericButton.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 
26 #include <qpushbutton.h>
27 #include <qsize.h>
28 #include <qevent.h>
29 #include <qevent.h>
30 #define YUILogComponent "qt-ui"
31 #include <yui/YUILog.h>
32 
33 #include "utf8.h"
34 #include "YQUI.h"
35 #include "YQApplication.h"
36 #include <yui/YEvent.h>
37 #include "YQGenericButton.h"
38 #include "YQDialog.h"
39 
40 using std::string;
41 using std::endl;
42 
43 
45  const string & label )
46  : QWidget( (QWidget *) parent->widgetRep() )
47  , YPushButton( parent, label )
48  , _dialog( 0 )
49  , _qPushButton( 0 )
50 {
51  setWidgetRep( 0 );
52 }
53 
54 
55 void YQGenericButton::setQPushButton( QPushButton * pb )
56 {
57  _qPushButton = pb;
58  _qPushButton->installEventFilter( this );
59  _qPushButton->setAutoDefault( true );
60 
61  YPushButton::setLabel( toUTF8 ( _qPushButton->text() ) );
62 }
63 
64 
66 {
67  if ( _dialog ) // If we don't have one any more, don't bother
68  {
69  if ( _dialog->focusButton() == this )
70  _dialog->losingFocus( this );
71 
72  if ( _dialog->defaultButton() == this )
73  _dialog->setDefaultButton(0);
74  }
75 }
76 
77 
78 void YQGenericButton::forgetDialog()
79 {
80  _dialog = 0;
81 }
82 
83 
84 YQDialog *
86 {
87  if ( ! _dialog )
88  {
89  YDialog * yDialog = findDialog();
90 
91  if ( yDialog )
92  _dialog = dynamic_cast<YQDialog *> (yDialog);
93 
94  YUI_CHECK_PTR( _dialog );
95  }
96 
97  return _dialog;
98 }
99 
100 
101 void YQGenericButton::setEnabled( bool enabled )
102 {
103  if ( _qPushButton )
104  _qPushButton->setEnabled( enabled );
105 
106  YWidget::setEnabled( enabled );
107 }
108 
109 
111 {
112  return _qPushButton ? _qPushButton->isEnabled() : false;
113 }
114 
115 
116 void YQGenericButton::setIcon( const string & iconName )
117 {
118  if ( ! _qPushButton )
119  {
120  yuiError() << "NULL button (icon " << iconName << ")" << endl;
121  return;
122  }
123 
124  QString qIconName = fromUTF8( iconName );
125 
126  if ( qIconName.isEmpty() )
127  {
128  _qPushButton->setIcon( QIcon() );
129  return;
130  }
131 
132  // Search for the icon - FaTE #306356
133  // qIconName = fromUTF8( YQUI::yqApp()->iconLoader()->findIcon( iconName ) );
134  // QPixmap icon( qIconName );
135  // Use method from Qt instead
136  QIcon icon = QIcon::fromTheme ( iconName.c_str() );
137 
138  if ( icon.isNull() )
139  yuiWarning() << "Can't load icon \"" << qIconName << "\"" << endl;
140  else
141  _qPushButton->setIcon( icon );
142 }
143 
144 
145 void YQGenericButton::setLabel( const QString & label )
146 {
147  if ( _qPushButton )
148  _qPushButton->setText( label );
149  else
150  yuiError() << "NULL button \"" << label << "\"" << endl;
151 
152  YPushButton::setLabel( toUTF8( label ) );
153 }
154 
155 
156 void YQGenericButton::setLabel( const string & label )
157 {
158  if ( _qPushButton )
159  _qPushButton->setText( fromUTF8( label ) );
160  else
161  yuiError() << "NULL button \"" << label << "\"" << endl;
162 
163  YPushButton::setLabel( label );
164 }
165 
166 
168 {
169  if ( _qPushButton )
170  {
171  _qPushButton->setAutoDefault( !show );
172  _qPushButton->setDefault( show );
173  _qPushButton->update();
174  }
175 }
176 
177 
179 {
180  return _qPushButton ? _qPushButton->isDefault() : false;
181 }
182 
183 
184 QString
186 {
187  return _qPushButton ? _qPushButton->text() : "";
188 }
189 
190 
192 {
193  if ( _qPushButton )
194  _qPushButton->animateClick();
195 }
196 
197 
198 bool YQGenericButton::eventFilter( QObject * obj, QEvent * event )
199 {
200  if ( event )
201  {
202  if ( event->type() == QEvent::FocusIn )
203  {
204  dialog()->gettingFocus( this );
205  return false; // event processed?
206  }
207  else if ( event->type() == QEvent::FocusOut )
208  {
209  dialog()->losingFocus( this );
210  return false; // event processed?
211  }
212  else if ( event->type() == QEvent::MouseButtonRelease )
213  {
214  QMouseEvent * mouseEvent = dynamic_cast<QMouseEvent *> (event);
215 
216  if ( mouseEvent && mouseEvent->button() == Qt::RightButton )
217  {
218  yuiMilestone() << "Right click on button detected" << endl;
220  }
221  }
222  }
223 
224 
225  return QObject::eventFilter( obj, event );
226 }
227 
228 
230 {
231  if ( ! _qPushButton )
232  return false;
233 
234  dialog()->gettingFocus( this );
235  _qPushButton->setFocus();
236 
237  return true;
238 }
239 
240 void YQGenericButton::setShortcut ( const QKeySequence & key )
241 {
242  _qPushButton->setShortcut (key );
243 }
244 
245 
void maybeLeftHandedUser()
A mouse click with the wrong mouse button was detected - e.g., a right click on a push button.
YQGenericButton * defaultButton() const
Returns the dialog's default button - the button that is activated with [Return] if no button has the...
Definition: YQDialog.h:128
void gettingFocus(YQGenericButton *button)
Notification that a button gets the keyboard focus.
Definition: YQDialog.cc:589
YQGenericButton * focusButton() const
Returns the button that has the keyboard focus or 0 if no button has the keyboard focus.
Definition: YQDialog.h:122
void setDefaultButton(YPushButton *newDefaultButton)
Set the dialog's default button - the button that is activated with [Return] if no other button has t...
Definition: YQDialog.cc:494
void losingFocus(YQGenericButton *button)
Notification that a button loses the keyboard focus.
Definition: YQDialog.cc:573
YQDialog * dialog()
Returns the corresponding YQDialog.
void setLabel(const QString &label)
Changes the label (the text) of the button.
void setShortcut(const QKeySequence &key)
Set the keyboard shortcut (e.g.
void showAsDefault(bool show=true)
Show this button as the dialog's default button.
bool isEnabled() const
Returns 'true' if this button is enabled, 'false' otherwise.
virtual bool setKeyboardFocus()
Accept the keyboard focus.
QString text() const
Returns the button's text (label) - useful for log messages etc.
virtual void setEnabled(bool enabled)
Set enabled/disabled state.
virtual void setIcon(const std::string &iconName)
Set this button's icon.
void setQPushButton(QPushButton *pb)
Set the corresponding QPushButton.
void activate()
Activate (animated) this button.
virtual ~YQGenericButton()
Destructor.
bool isShownAsDefault() const
Returns 'true' if this button is shown as a default button - which may mean that this really is the d...
YQGenericButton(YWidget *parent, const std::string &label)
Constructor.
bool eventFilter(QObject *obj, QEvent *event)
Redirect events from the _qPushButton member to this object.
static YQApplication * yqApp()
Return the global YApplication object as YQApplication.
Definition: YQUI.cc:268