]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/YQGenericButton.cc
- installation summary looks nice
[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 {
43     setWidgetRep( 0 );
44 }
45
46
47 void YQGenericButton::setQPushButton( QPushButton * pb )
48 {
49     _qPushButton = pb;
50     _qPushButton->setAutoDefault( true );
51     _qPushButton->installEventFilter( this );
52     _qPushButton->setAutoDefault( true );
53
54     YPushButton::setLabel( toUTF8 ( _qPushButton->text() ) );
55 }
56
57
58 YQGenericButton::~YQGenericButton()
59 {
60     if ( _dialog ) // If we don't have one any more, don't bother
61     {
62         if ( _dialog->focusButton() == this )
63             _dialog->losingFocus( this );
64
65         if ( _dialog->defaultButton() == this )
66             _dialog->setDefaultButton(0);
67     }
68 }
69
70 void
71 YQGenericButton::forgetDialog()
72 {
73    _dialog = 0;
74 }
75
76 YQDialog *
77 YQGenericButton::dialog()
78 {
79     if ( ! _dialog )
80     {
81         YDialog * yDialog = findDialog();
82
83         if ( yDialog )
84             _dialog = dynamic_cast<YQDialog *> (yDialog);
85
86         YUI_CHECK_PTR( _dialog );
87     }
88
89     return _dialog;
90 }
91
92
93 void YQGenericButton::setEnabled( bool enabled )
94 {
95     if ( _qPushButton )
96         _qPushButton->setEnabled( enabled );
97
98     YWidget::setEnabled( enabled );
99 }
100
101
102 bool YQGenericButton::isEnabled() const
103 {
104     return _qPushButton ? _qPushButton->isEnabled() : false;
105 }
106
107
108 void YQGenericButton::setIcon( const string & iconName )
109 {
110     if ( ! _qPushButton )
111     {
112         y2error( "NULL button (icon %s)", iconName.c_str() );
113         return;
114     }
115
116     QString qIconName = fromUTF8( iconName );
117
118     if ( qIconName.isEmpty() )
119     {
120         _qPushButton->setIcon( QIcon() );
121         return;
122     }
123
124     qIconName = QString( ICONDIR ) + "/" + qIconName;
125     QPixmap icon( qIconName );
126
127     if ( icon.isNull() )
128         y2warning( "Can't load icon '%s'", qPrintable(qIconName) );
129     else
130         _qPushButton->setIcon( icon );
131 }
132
133
134 void YQGenericButton::setLabel( const QString & label )
135 {
136     if ( _qPushButton )
137         _qPushButton->setText( label );
138     else
139         y2error( "NULL button '%s'", qPrintable(label) );
140
141     YPushButton::setLabel( toUTF8( label ) );
142 }
143
144
145 void YQGenericButton::setLabel( const string & label )
146 {
147     if ( _qPushButton )
148         _qPushButton->setText( fromUTF8( label ) );
149     else
150         y2error( "NULL button '%s'", label.c_str() );
151
152     YPushButton::setLabel( label );
153 }
154
155
156 void YQGenericButton::showAsDefault( bool show )
157 {
158     if ( _qPushButton )
159     {
160         _qPushButton->setDefault( show );
161         _qPushButton->update();
162     }
163 }
164
165
166 bool YQGenericButton::isShownAsDefault() const
167 {
168     return _qPushButton ? _qPushButton->isDefault() : false;
169 }
170
171
172 QString
173 YQGenericButton::text() const
174 {
175     return _qPushButton ? _qPushButton->text() : "";
176 }
177
178
179 void YQGenericButton::activate()
180 {
181     if ( _qPushButton )
182         _qPushButton->animateClick();
183 }
184
185
186 bool YQGenericButton::eventFilter( QObject * obj, QEvent * event )
187 {
188     if ( event )
189     {
190         if ( event->type() == QEvent::FocusIn )
191         {
192             dialog()->gettingFocus( this );
193             return false;       // event processed?
194         }
195         else if ( event->type() == QEvent::FocusOut )
196         {
197             dialog()->losingFocus( this );
198             return false;       // event processed?
199         }
200         else if ( event->type() == QEvent::MouseButtonRelease )
201         {
202             QMouseEvent * mouseEvent = dynamic_cast<QMouseEvent *> (event);
203
204             if ( mouseEvent && mouseEvent->button() == Qt::RightButton )
205             {
206                 y2milestone( "Right click on button detected" );
207                 YQUI::ui()->maybeLeftHandedUser();
208             }
209         }
210     }
211
212
213     return QObject::eventFilter( obj, event );
214 }
215
216
217 bool YQGenericButton::setKeyboardFocus()
218 {
219     if ( ! _qPushButton )
220         return false;
221
222     dialog()->gettingFocus( this );
223     _qPushButton->setFocus();
224
225     return true;
226 }
227
228
229 #include "YQGenericButton.moc"