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