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