libyui-qt  2.49.16
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 
42  const std::string & label )
43  : QWidget( (QWidget *) parent->widgetRep() )
44  , YPushButton( parent, label )
45  , _dialog( 0 )
46  , _qPushButton( 0 )
47 {
48  setWidgetRep( 0 );
49 }
50 
51 
52 void YQGenericButton::setQPushButton( QPushButton * pb )
53 {
54  _qPushButton = pb;
55  _qPushButton->installEventFilter( this );
56  _qPushButton->setAutoDefault( true );
57 
58  YPushButton::setLabel( toUTF8 ( _qPushButton->text() ) );
59 }
60 
61 
63 {
64  if ( _dialog ) // If we don't have one any more, don't bother
65  {
66  if ( _dialog->focusButton() == this )
67  _dialog->losingFocus( this );
68 
69  if ( _dialog->defaultButton() == this )
70  _dialog->setDefaultButton(0);
71  }
72 }
73 
74 
75 void YQGenericButton::forgetDialog()
76 {
77  _dialog = 0;
78 }
79 
80 
81 YQDialog *
83 {
84  if ( ! _dialog )
85  {
86  YDialog * yDialog = findDialog();
87 
88  if ( yDialog )
89  _dialog = dynamic_cast<YQDialog *> (yDialog);
90 
91  YUI_CHECK_PTR( _dialog );
92  }
93 
94  return _dialog;
95 }
96 
97 
98 void YQGenericButton::setEnabled( bool enabled )
99 {
100  if ( _qPushButton )
101  _qPushButton->setEnabled( enabled );
102 
103  YWidget::setEnabled( enabled );
104 }
105 
106 
108 {
109  return _qPushButton ? _qPushButton->isEnabled() : false;
110 }
111 
112 
113 void YQGenericButton::setIcon( const std::string & iconName )
114 {
115  if ( ! _qPushButton )
116  {
117  yuiError() << "NULL button (icon " << iconName << ")" << std::endl;
118  return;
119  }
120 
121  QString qIconName = fromUTF8( iconName );
122 
123  if ( qIconName.isEmpty() )
124  {
125  _qPushButton->setIcon( QIcon() );
126  return;
127  }
128 
129  // Search for the icon - FaTE #306356
130  // qIconName = fromUTF8( YQUI::yqApp()->iconLoader()->findIcon( iconName ) );
131  // QPixmap icon( qIconName );
132  // Use method from Qt instead
133  QIcon icon = QIcon::fromTheme ( iconName.c_str() );
134 
135  if ( icon.isNull() )
136  yuiWarning() << "Can't load icon \"" << qIconName << "\"" << std::endl;
137  else
138  _qPushButton->setIcon( icon );
139 }
140 
141 
142 void YQGenericButton::setLabel( const QString & label )
143 {
144  if ( _qPushButton )
145  _qPushButton->setText( label );
146  else
147  yuiError() << "NULL button \"" << label << "\"" << std::endl;
148 
149  YPushButton::setLabel( toUTF8( label ) );
150 }
151 
152 
153 void YQGenericButton::setLabel( const std::string & label )
154 {
155  if ( _qPushButton )
156  _qPushButton->setText( fromUTF8( label ) );
157  else
158  yuiError() << "NULL button \"" << label << "\"" << std::endl;
159 
160  YPushButton::setLabel( label );
161 }
162 
163 
165 {
166  if ( _qPushButton )
167  {
168  _qPushButton->setAutoDefault( !show );
169  _qPushButton->setDefault( show );
170  _qPushButton->update();
171  }
172 }
173 
174 
176 {
177  return _qPushButton ? _qPushButton->isDefault() : false;
178 }
179 
180 
181 QString
183 {
184  return _qPushButton ? _qPushButton->text() : "";
185 }
186 
187 
189 {
190  if ( _qPushButton )
191  _qPushButton->animateClick();
192 }
193 
194 
195 bool YQGenericButton::eventFilter( QObject * obj, QEvent * event )
196 {
197  if ( event )
198  {
199  if ( event->type() == QEvent::FocusIn )
200  {
201  dialog()->gettingFocus( this );
202  return false; // event processed?
203  }
204  else if ( event->type() == QEvent::FocusOut )
205  {
206  dialog()->losingFocus( this );
207  return false; // event processed?
208  }
209  else if ( event->type() == QEvent::MouseButtonRelease )
210  {
211  QMouseEvent * mouseEvent = dynamic_cast<QMouseEvent *> (event);
212 
213  if ( mouseEvent && mouseEvent->button() == Qt::RightButton )
214  {
215  yuiMilestone() << "Right click on button detected" << std::endl;
217  }
218  }
219  }
220 
221 
222  return QObject::eventFilter( obj, event );
223 }
224 
225 
227 {
228  if ( ! _qPushButton )
229  return false;
230 
231  dialog()->gettingFocus( this );
232  _qPushButton->setFocus();
233 
234  return true;
235 }
236 
237 void YQGenericButton::setShortcut ( const QKeySequence & key )
238 {
239  _qPushButton->setShortcut (key );
240 }
241 
242 
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:257
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:591
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:496
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:575
void setLabel(const QString &label)
Changes the label (the text) of the button.
YQGenericButton(YWidget *parent, const std::string &label)
Constructor.