]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/YQGenericButton.cc
follow the widget state - for reference: you don't
[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
72 YQDialog *
73 YQGenericButton::dialog()
74 {
75     if ( ! _dialog )
76     {
77         YDialog * yDialog = findDialog();
78
79         if ( yDialog )
80             _dialog = dynamic_cast<YQDialog *> (yDialog);
81
82         YUI_CHECK_PTR( _dialog );
83     }
84
85     return _dialog;
86 }
87
88
89 void
90 YQGenericButton::setDefaultButton( bool def )
91 {
92     YPushButton::setDefaultButton( def );
93
94     if ( ! _setDefaultButtonRecursive )
95     {
96         _setDefaultButtonRecursive = true;
97         
98         if ( _dialog && def )
99             dialog()->setDefaultButton( this );
100         
101         _setDefaultButtonRecursive = true;
102     }
103 }
104
105
106 void YQGenericButton::setEnabled( bool enabled )
107 {
108     if ( _qPushButton )
109         _qPushButton->setEnabled( enabled );
110
111     YWidget::setEnabled( enabled );
112 }
113
114
115 bool YQGenericButton::isEnabled() const
116 {
117     return _qPushButton ? _qPushButton->isEnabled() : false;
118 }
119
120
121 void YQGenericButton::setIcon( const string & iconName )
122 {
123     if ( ! _qPushButton )
124     {
125         y2error( "NULL button (icon %s)", iconName.c_str() );
126         return;
127     }
128
129     QString qIconName = fromUTF8( iconName );
130
131     if ( qIconName.isEmpty() )
132     {
133         _qPushButton->setIconSet( QIcon() );
134         return;
135     }
136
137     qIconName = QString( ICONDIR ) + "/" + qIconName;
138     QPixmap icon( qIconName );
139
140     if ( icon.isNull() )
141         y2warning( "Can't load icon '%s'", (const char *) qIconName );
142     else
143         _qPushButton->setIconSet( icon );
144 }
145
146
147 void YQGenericButton::setLabel( const QString & label )
148 {
149     if ( _qPushButton )
150         _qPushButton->setText( label );
151     else
152         y2error( "NULL button '%s'", (const char *) label );
153
154     YPushButton::setLabel( toUTF8( label ) );
155 }
156
157
158 void YQGenericButton::setLabel( const string & label )
159 {
160     if ( _qPushButton )
161         _qPushButton->setText( fromUTF8( label ) );
162     else
163         y2error( "NULL button '%s'", label.c_str() );
164
165     YPushButton::setLabel( label );
166 }
167
168
169 void YQGenericButton::showAsDefault( bool show )
170 {
171     if ( _qPushButton )
172     {
173         _qPushButton->setDefault( show );
174         _qPushButton->update();
175     }
176 }
177
178
179 bool YQGenericButton::isShownAsDefault() const
180 {
181     return _qPushButton ? _qPushButton->isDefault() : false;
182 }
183
184
185 QString
186 YQGenericButton::text() const
187 {
188     return _qPushButton ? _qPushButton->text() : "";
189 }
190
191
192 void YQGenericButton::activate()
193 {
194     if ( _qPushButton )
195         _qPushButton->animateClick();
196 }
197
198
199 bool YQGenericButton::eventFilter( QObject * obj, QEvent * event )
200 {
201     if ( event )
202     {
203         if ( event->type() == QEvent::FocusIn )
204         {
205             dialog()->gettingFocus( this );
206             return false;       // event processed?
207         }
208         else if ( event->type() == QEvent::FocusOut )
209         {
210             dialog()->losingFocus( this );
211             return false;       // event processed?
212         }
213         else if ( event->type() == QEvent::MouseButtonRelease )
214         {
215             QMouseEvent * mouseEvent = dynamic_cast<QMouseEvent *> (event);
216
217             if ( mouseEvent && mouseEvent->button() == Qt::RightButton )
218             {
219                 y2milestone( "Right click on button detected" );
220                 YQUI::ui()->maybeLeftHandedUser();
221             }
222         }
223     }
224
225
226     return QObject::eventFilter( obj, event );
227 }
228
229
230 bool YQGenericButton::setKeyboardFocus()
231 {
232     if ( ! _qPushButton )
233         return false;
234
235     dialog()->gettingFocus( this );
236     _qPushButton->setFocus();
237
238     return true;
239 }
240
241
242 #include "YQGenericButton.moc"