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