libyui-qt  2.53.0
YQCheckBoxFrame.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: YQCheckBoxFrame.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 
26 #define YUILogComponent "qt-ui"
27 #include <yui/YUILog.h>
28 #include <qcheckbox.h>
29 #include <QDebug>
30 #include <QVBoxLayout>
31 #include <qevent.h>
32 #include "YQUI.h"
33 #include <yui/YEvent.h>
34 #include "utf8.h"
35 
36 #include "YQCheckBoxFrame.h"
37 
38 #define TOP_MARGIN 6
39 
40 using std::string;
41 using std::endl;
42 
43 
44 
46  const string & label,
47  bool checked )
48  : QGroupBox( (QWidget *) parent->widgetRep() )
49  , YCheckBoxFrame( parent, label, checked)
50 {
51  setWidgetRep ( this );
52  QGroupBox::setTitle( fromUTF8( label ) );
53  QGroupBox::setCheckable( true );
54  setValue( checked );
55 
56  connect( this, &pclass(this)::toggled,
57  this, &pclass(this)::stateChanged );
58 }
59 
60 
61 void YQCheckBoxFrame::setLabel( const string & newLabel )
62 {
63  YCheckBoxFrame::setLabel( newLabel );
64  QGroupBox::setTitle( fromUTF8( label() ) );
65 }
66 
67 
69 {
70  return QGroupBox::isChecked();
71 }
72 
73 
74 void YQCheckBoxFrame::setValue( bool newValue )
75 {
76  setChecked( newValue );
77  setEnabled( newValue );
78 }
79 
80 
81 void YQCheckBoxFrame::setEnabled( bool enabled )
82 {
83  if ( enabled )
84  {
85  QGroupBox::setEnabled( true );
86  handleChildrenEnablement( value() );
87  }
88  else
89  {
90  QGroupBox::setEnabled( true );
91  YWidget::setChildrenEnabled( false );
92  }
93 
94  YWidget::setEnabled( enabled );
95 }
96 
97 
98 void YQCheckBoxFrame::stateChanged( bool newState )
99 {
100  if ( notify() )
101  YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
102 }
103 
104 
105 bool YQCheckBoxFrame::event( QEvent *e )
106 {
107  bool oldChildEnabled = true;
108 
109  if ( YCheckBoxFrame::hasChildren() )
110  oldChildEnabled = YCheckBoxFrame::firstChild()->isEnabled();
111 
112  bool oldStatus = QGroupBox::isChecked();
113  bool ret = QGroupBox::event( e );
114  bool newStatus = QGroupBox::isChecked();
115 
116  if ( oldStatus != newStatus )
117  {
118  yuiDebug() << "Status change of " << this << " : now " << std::boolalpha << newStatus << endl;
119 
120  if ( autoEnable() )
121  {
122  handleChildrenEnablement( newStatus );
123  }
124  else
125  {
126  if ( YCheckBoxFrame::hasChildren() )
127  YCheckBoxFrame::firstChild()->setEnabled( oldChildEnabled );
128  }
129  }
130 
131  return ret;
132 }
133 
134 
135 void YQCheckBoxFrame::childEvent( QChildEvent * event )
136 {
137  if ( event->added() )
138  {
139  // yuiDebug() << "Child widget added" << endl;
140 
141  // Prevent parent class from disabling child widgets according to its
142  // own policy: YCheckBoxFrame is much more flexible than QGroupBox.
143  }
144  else
145  QGroupBox::childEvent( event );
146 }
147 
148 
149 void
150 YQCheckBoxFrame::setSize( int newWidth, int newHeight )
151 {
152  resize ( newWidth, newHeight );
153 
154  if ( hasChildren() )
155  {
156  QMargins margins = contentsMargins();
157  int newChildWidth = newWidth - margins.left() - margins.right();
158  int newChildHeight = newHeight - margins.bottom() - margins.top();
159 
160  firstChild()->setSize( newChildWidth, newChildHeight );
161 
162  QWidget * qChild = (QWidget *) firstChild()->widgetRep();
163  qChild->move( margins.left(), margins.top() );
164  }
165 }
166 
167 
169 {
170  int preferredWidth = hasChildren() ? firstChild()->preferredWidth() : 0;
171  QMargins margins = contentsMargins();
172 
173  return preferredWidth + margins.left() + margins.right();
174 }
175 
176 
178 {
179  int preferredHeight = hasChildren() ? firstChild()->preferredHeight() : 0;
180  QMargins margins = contentsMargins();
181 
182  return preferredHeight + margins.top() + margins.left();
183 }
184 
185 
187 {
188  setFocus();
189 
190  return true;
191 }
192 
193 
194 
195 
196 
virtual bool value()
Get the status of the CheckBoxFrame's check box.
virtual int preferredWidth()
Preferred width of the widget.
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
virtual void setEnabled(bool enabled)
Set enabled / disabled state.
virtual void childEvent(QChildEvent *)
Reimplemented from QGroupBox to prevent QGroupBox from disabling children according to the check box ...
virtual void setLabel(const std::string &label)
Change the label text on the CheckBoxFrame.
YQCheckBoxFrame(YWidget *parent, const std::string &label, bool checked)
Constructor.
virtual bool setKeyboardFocus()
Accept the keyboard focus.
virtual int preferredHeight()
Preferred height of the widget.
virtual void setValue(bool isChecked)
Check or uncheck the CheckBoxFrame's check box.
static YQUI * ui()
Access the global Qt-UI.
Definition: YQUI.h:83
void sendEvent(YEvent *event)
Widget event handlers (slots) call this when an event occured that should be the answer to a UserInpu...
Definition: YQUI.cc:480