]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/YQGenericButton.cc
don't create QObjects in ycp thread - make timeouts work
[duncan/yast2-qt4.git] / src / YQGenericButton.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQGenericButton.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17 /-*/
18
19
20 #include <qpushbutton.h>
21 #include <qsize.h>
22 #include <qevent.h>
23 //Added by qt3to4:
24 #include <qpixmap.h>
25 #include <qevent.h>
26 #define y2log_component "qt-ui"
27 #include <ycp/y2log.h>
28
29 #include "utf8.h"
30 #include "YQUI.h"
31 #include "YEvent.h"
32 #include "YQGenericButton.h"
33 #include "YQDialog.h"
34
35
36 YQGenericButton::YQGenericButton( YWidget *             parent,
37                                   const string &        label )
38     : QWidget( (QWidget *) parent->widgetRep() )
39     , YPushButton( parent, label )
40     , _dialog( 0 )
41     , _qPushButton( 0 )
42     , _setDefaultButtonRecursive( false )
43 {
44     setWidgetRep( 0 );
45 }
46
47
48 void YQGenericButton::setQPushButton( QPushButton * pb )
49 {
50     _qPushButton = pb;
51     _qPushButton->setAutoDefault( true );
52     _qPushButton->installEventFilter( this );
53     _qPushButton->setAutoDefault( true );
54
55     YPushButton::setLabel( toUTF8 ( _qPushButton->text() ) );
56 }
57
58
59 YQGenericButton::~YQGenericButton()
60 {
61     if ( _dialog ) // If we don't have one any more, don't bother
62     {
63         if ( _dialog->focusButton() == this )
64             _dialog->losingFocus( this );
65
66         if ( _dialog->defaultButton() == this )
67             _dialog->setDefaultButton(0);
68     }
69 }
70
71 void
72 YQGenericButton::forgetDialog()
73 {
74    _dialog = 0;
75 }
76
77 YQDialog *
78 YQGenericButton::dialog()
79 {
80     if ( ! _dialog )
81     {
82         YDialog * yDialog = findDialog();
83
84         if ( yDialog )
85             _dialog = dynamic_cast<YQDialog *> (yDialog);
86
87         YUI_CHECK_PTR( _dialog );
88     }
89
90     return _dialog;
91 }
92
93
94 void
95 YQGenericButton::setDefaultButton( bool def )
96 {
97     YPushButton::setDefaultButton( def );
98
99     if ( ! _setDefaultButtonRecursive )
100     {
101         _setDefaultButtonRecursive = true;
102         
103         if ( _dialog && def )
104             dialog()->setDefaultButton( this );
105         
106         _setDefaultButtonRecursive = true;
107     }
108 }
109
110
111 void YQGenericButton::setEnabled( bool enabled )
112 {
113     if ( _qPushButton )
114         _qPushButton->setEnabled( enabled );
115
116     YWidget::setEnabled( enabled );
117 }
118
119
120 bool YQGenericButton::isEnabled() const
121 {
122     return _qPushButton ? _qPushButton->isEnabled() : false;
123 }
124
125
126 void YQGenericButton::setIcon( const string & iconName )
127 {
128     if ( ! _qPushButton )
129     {
130         y2error( "NULL button (icon %s)", iconName.c_str() );
131         return;
132     }
133
134     QString qIconName = fromUTF8( iconName );
135
136     if ( qIconName.isEmpty() )
137     {
138         _qPushButton->setIcon( QIcon() );
139         return;
140     }
141
142     qIconName = QString( ICONDIR ) + "/" + qIconName;
143     QPixmap icon( qIconName );
144
145     if ( icon.isNull() )
146         y2warning( "Can't load icon '%s'", qPrintable(qIconName) );
147     else
148         _qPushButton->setIcon( icon );
149 }
150
151
152 void YQGenericButton::setLabel( const QString & label )
153 {
154     if ( _qPushButton )
155         _qPushButton->setText( label );
156     else
157         y2error( "NULL button '%s'", qPrintable(label) );
158
159     YPushButton::setLabel( toUTF8( label ) );
160 }
161
162
163 void YQGenericButton::setLabel( const string & label )
164 {
165     if ( _qPushButton )
166         _qPushButton->setText( fromUTF8( label ) );
167     else
168         y2error( "NULL button '%s'", label.c_str() );
169
170     YPushButton::setLabel( label );
171 }
172
173
174 void YQGenericButton::showAsDefault( bool show )
175 {
176     if ( _qPushButton )
177     {
178         _qPushButton->setDefault( show );
179         _qPushButton->update();
180     }
181 }
182
183
184 bool YQGenericButton::isShownAsDefault() const
185 {
186     return _qPushButton ? _qPushButton->isDefault() : false;
187 }
188
189
190 QString
191 YQGenericButton::text() const
192 {
193     return _qPushButton ? _qPushButton->text() : "";
194 }
195
196
197 void YQGenericButton::activate()
198 {
199     if ( _qPushButton )
200         _qPushButton->animateClick();
201 }
202
203
204 bool YQGenericButton::eventFilter( QObject * obj, QEvent * event )
205 {
206     if ( event )
207     {
208         if ( event->type() == QEvent::FocusIn )
209         {
210             dialog()->gettingFocus( this );
211             return false;       // event processed?
212         }
213         else if ( event->type() == QEvent::FocusOut )
214         {
215             dialog()->losingFocus( this );
216             return false;       // event processed?
217         }
218         else if ( event->type() == QEvent::MouseButtonRelease )
219         {
220             QMouseEvent * mouseEvent = dynamic_cast<QMouseEvent *> (event);
221
222             if ( mouseEvent && mouseEvent->button() == Qt::RightButton )
223             {
224                 y2milestone( "Right click on button detected" );
225                 YQUI::ui()->maybeLeftHandedUser();
226             }
227         }
228     }
229
230
231     return QObject::eventFilter( obj, event );
232 }
233
234
235 bool YQGenericButton::setKeyboardFocus()
236 {
237     if ( ! _qPushButton )
238         return false;
239
240     dialog()->gettingFocus( this );
241     _qPushButton->setFocus();
242
243     return true;
244 }
245
246
247 #include "YQGenericButton.moc"