~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kcontrol/randr/collapsiblewidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   This file is part of the KDE libraries
 
3
   Copyright (C) 2005 Daniel Molkentin <molkentin@kde.org>
 
4
 
 
5
   This library is free software; you can redistribute it and/or
 
6
   modify it under the terms of the GNU Library General Public
 
7
   License version 2 as published by the Free Software Foundation.
 
8
 
 
9
   This library is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
   Library General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU Library General Public License
 
15
   along with this library; see the file COPYING.LIB.  If not, write to
 
16
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
   Boston, MA 02110-1301, USA.
 
18
*/
 
19
 
 
20
#include <QtGui/QtGui>
 
21
#include <QTimeLine>
 
22
#include <collapsiblewidget.h>
 
23
 
 
24
/******************************************************************
 
25
 * Helper classes
 
26
 *****************************************************************/
 
27
 
 
28
ClickableLabel::ClickableLabel( QWidget* parent )
 
29
    : QLabel( parent )
 
30
{
 
31
}
 
32
 
 
33
ClickableLabel::~ClickableLabel()
 
34
{
 
35
}
 
36
 
 
37
void ClickableLabel::mouseReleaseEvent( QMouseEvent *e )
 
38
{
 
39
    Q_UNUSED( e );
 
40
    emit clicked();
 
41
}
 
42
 
 
43
ArrowButton::ArrowButton( QWidget *parent )
 
44
: QAbstractButton( parent )
 
45
{
 
46
}
 
47
 
 
48
 
 
49
ArrowButton::~ArrowButton()
 
50
{
 
51
}
 
52
 
 
53
void ArrowButton::paintEvent( QPaintEvent *event )
 
54
{
 
55
  Q_UNUSED( event );
 
56
  QPainter p( this );
 
57
  QStyleOption opt;
 
58
  int h = sizeHint().height();
 
59
  opt.rect = QRect(0,( height()- h )/2, h, h);
 
60
  opt.palette = palette();
 
61
  opt.state = QStyle::State_Children;
 
62
  if (isChecked())
 
63
    opt.state |= QStyle::State_Open;
 
64
 
 
65
  style()->drawPrimitive(QStyle::PE_IndicatorBranch, &opt, &p);
 
66
  p.end();
 
67
}
 
68
 
 
69
 
 
70
/******************************************************************
 
71
 * Private classes
 
72
 *****************************************************************/
 
73
 
 
74
class CollapsibleWidget::Private
 
75
{
 
76
  public:
 
77
    QGridLayout    *gridLayout;
 
78
    QWidget        *innerWidget;
 
79
    ClickableLabel *label;
 
80
    ArrowButton    *colButton;
 
81
    QTimeLine      *timeline;
 
82
    QWidget        *expander;
 
83
    QVBoxLayout    *expanderLayout;
 
84
};
 
85
 
 
86
class SettingsContainer::Private
 
87
{
 
88
  public:
 
89
    QVBoxLayout *layout;
 
90
};
 
91
 
 
92
/******************************************************************
 
93
 * Implementation
 
94
 *****************************************************************/
 
95
 
 
96
SettingsContainer::SettingsContainer(QWidget *parent)
 
97
 : QScrollArea( parent ), d(new SettingsContainer::Private)
 
98
{
 
99
  QWidget *w = new QWidget;
 
100
  QVBoxLayout *helperLay = new QVBoxLayout(w);
 
101
  d->layout = new QVBoxLayout;
 
102
  helperLay->addLayout( d->layout );
 
103
  helperLay->addStretch(1);
 
104
  setWidget(w);
 
105
  setWidgetResizable(true);
 
106
}
 
107
 
 
108
SettingsContainer::~SettingsContainer()
 
109
{
 
110
  delete d;
 
111
}
 
112
 
 
113
CollapsibleWidget* SettingsContainer::insertWidget( QWidget *w, const QString& name )
 
114
{
 
115
   if (w && w->layout()) {
 
116
     QLayout *lay = w->layout();
 
117
     lay->setMargin(2);
 
118
     lay->setSpacing(0);
 
119
   }
 
120
 
 
121
   CollapsibleWidget *cw = new CollapsibleWidget( name );
 
122
   d->layout->addWidget( cw );
 
123
   cw->setInnerWidget( w );
 
124
   return cw;
 
125
}
 
126
 
 
127
CollapsibleWidget::CollapsibleWidget(QWidget *parent)
 
128
 : QWidget(parent), d(new CollapsibleWidget::Private)
 
129
{
 
130
  init();
 
131
}
 
132
CollapsibleWidget::CollapsibleWidget(const QString& caption, QWidget *parent)
 
133
 : QWidget(parent), d(new CollapsibleWidget::Private)
 
134
{
 
135
  init();
 
136
  setCaption(caption);
 
137
}
 
138
 
 
139
void CollapsibleWidget::init()
 
140
{
 
141
  d->expander = 0;
 
142
  d->expanderLayout = 0;
 
143
  d->timeline = new QTimeLine( 150, this );
 
144
  d->timeline->setCurveShape( QTimeLine::EaseInOutCurve );
 
145
  connect( d->timeline, SIGNAL(valueChanged(qreal)),
 
146
           this, SLOT(animateCollapse(qreal)) );
 
147
 
 
148
  d->innerWidget = 0;
 
149
  d->gridLayout = new QGridLayout( this );
 
150
  d->gridLayout->setMargin(0);
 
151
 
 
152
  d->colButton = new ArrowButton;
 
153
  d->colButton->setCheckable(true);
 
154
 
 
155
  d->label = new ClickableLabel;
 
156
  d->label->setSizePolicy(QSizePolicy::MinimumExpanding, 
 
157
                        QSizePolicy::Preferred);
 
158
 
 
159
  d->gridLayout->addWidget(d->colButton, 1, 1);
 
160
  d->gridLayout->addWidget(d->label, 1, 2);
 
161
 
 
162
 
 
163
  connect(d->label, SIGNAL(clicked()), 
 
164
          d->colButton, SLOT(click()));
 
165
 
 
166
  connect(d->colButton, SIGNAL(toggled(bool)), 
 
167
          SLOT(setExpanded(bool)));
 
168
 
 
169
  setExpanded(false);
 
170
  setEnabled(false);
 
171
}
 
172
 
 
173
CollapsibleWidget::~CollapsibleWidget()
 
174
{
 
175
  delete d;
 
176
}
 
177
 
 
178
QWidget* CollapsibleWidget::innerWidget() const
 
179
{
 
180
  return d->innerWidget;
 
181
}
 
182
 
 
183
#define SIMPLE
 
184
 
 
185
void CollapsibleWidget::setInnerWidget(QWidget *w)
 
186
{
 
187
  if (!w) {
 
188
    return;
 
189
  }
 
190
 
 
191
  d->innerWidget = w;
 
192
 
 
193
#ifdef SIMPLE
 
194
  if ( !isExpanded() ) {
 
195
      d->innerWidget->hide();
 
196
  }
 
197
  d->gridLayout->addWidget( d->innerWidget, 2, 2 );
 
198
  d->gridLayout->setRowStretch( 2, 1 );
 
199
#else
 
200
  if ( !d->expander ) {
 
201
      d->expander = new QWidget( this );
 
202
      d->gridLayout->addWidget( d->expander, 2, 2 );
 
203
      d->gridLayout->setRowStretch( 2, 1 );
 
204
      d->expanderLayout = new QVBoxLayout( d->expander );
 
205
      d->expanderLayout->setMargin( 0 );
 
206
      d->expanderLayout->setSpacing( 0 );
 
207
      d->expander->setFixedHeight( 0 );
 
208
  }
 
209
 
 
210
  d->innerWidget->setParent( d->expander );
 
211
  d->innerWidget->show();
 
212
  d->expanderLayout->addWidget( d->innerWidget );
 
213
#endif
 
214
 
 
215
  setEnabled( true );
 
216
 
 
217
  if ( isExpanded() ) {
 
218
    setExpanded( true );
 
219
  }
 
220
}
 
221
 
 
222
void CollapsibleWidget::setCaption(const QString& caption)
 
223
{
 
224
  d->label->setText(QString("<b>%1</b>").arg(caption));
 
225
}
 
226
 
 
227
QString CollapsibleWidget::caption() const
 
228
{
 
229
  return d->label->text();
 
230
}
 
231
 
 
232
 
 
233
void CollapsibleWidget::setExpanded(bool expanded)
 
234
{
 
235
  if ( !d->innerWidget ) {
 
236
    return;
 
237
  }
 
238
 
 
239
#ifdef SIMPLE
 
240
  if ( !expanded ) {
 
241
    d->innerWidget->setVisible( false );
 
242
  }
 
243
#else
 
244
  if ( expanded ) {
 
245
      d->expander->setVisible( true );
 
246
  }
 
247
  d->innerWidget->setVisible( expanded );
 
248
#endif
 
249
  d->colButton->setChecked( expanded );
 
250
  d->timeline->setDirection( expanded ? QTimeLine::Forward
 
251
                                      : QTimeLine::Backward );
 
252
  if (d->timeline->state() != QTimeLine::Running)
 
253
      d->timeline->start();
 
254
}
 
255
 
 
256
void CollapsibleWidget::animateCollapse( qreal showAmount )
 
257
{
 
258
  int pixels = d->innerWidget->sizeHint().height() * showAmount;
 
259
  d->gridLayout->setRowMinimumHeight( 2, pixels );
 
260
 
 
261
#ifdef SIMPLE
 
262
  d->gridLayout->setRowMinimumHeight( 2, pixels );
 
263
 
 
264
  if ( showAmount == 1 ) {
 
265
      d->innerWidget->setVisible( true );
 
266
  }
 
267
#else
 
268
  d->expander->setFixedHeight( pixels );
 
269
#endif
 
270
}
 
271
 
 
272
bool CollapsibleWidget::isExpanded() const
 
273
{
 
274
  return d->colButton->isChecked();
 
275
}
 
276
 
 
277
 
 
278
#include "collapsiblewidget.moc"