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