~ubuntu-branches/ubuntu/saucy/goldencheetah/saucy

« back to all changes in this revision

Viewing changes to src/GcSideBarItem.cpp

  • Committer: Package Import Robot
  • Author(s): KURASHIKI Satoru
  • Date: 2013-08-18 07:02:45 UTC
  • mfrom: (4.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20130818070245-zgdvb47e1k3mtgil
Tags: 3.0-3
debian/control: remove needless dependency. (Closes: #719571)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2013 Damien Grauser (Damien.Grauser@pev-geneve.ch)
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License as published by the Free
 
6
 * Software Foundation; either version 2 of the License, or (at your option)
 
7
 * any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful, but WITHOUT
 
10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 
12
 * more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License along
 
15
 * with this program; if not, write to the Free Software Foundation, Inc., 51
 
16
 * Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
17
 */
 
18
 
 
19
#include "GcSideBarItem.h"
 
20
#include "GcCalendar.h"
 
21
 
 
22
QIcon iconFromPNG(QString filename, bool emboss)
 
23
{
 
24
    QImage pngImage;
 
25
    pngImage.load(filename);
 
26
 
 
27
    // use muted dark gray color
 
28
    QImage gray8 = pngImage.convertToFormat(QImage::Format_Indexed8);
 
29
    QImage white8 = pngImage.convertToFormat(QImage::Format_Indexed8);
 
30
    gray8.setColor(0, QColor(80,80,80, 170).rgb());
 
31
    white8.setColor(0, QColor(255,255,255, 255).rgb());
 
32
 
 
33
    // now convert to a format we can paint with!
 
34
    QImage white = white8.convertToFormat(QImage::Format_ARGB32_Premultiplied);
 
35
    QImage gray = gray8.convertToFormat(QImage::Format_ARGB32_Premultiplied);
 
36
 
 
37
    QPainter painter;
 
38
    painter.begin(&white);
 
39
    painter.setBackgroundMode(Qt::TransparentMode);
 
40
    if (emboss) painter.drawImage(0,-1, gray);
 
41
    else painter.drawImage(0,0, gray);
 
42
    painter.end();
 
43
 
 
44
    QIcon icon(QPixmap::fromImage(white));
 
45
    return icon;
 
46
}
 
47
 
 
48
GcSplitter::GcSplitter(Qt::Orientation orientation, QWidget *parent) : QWidget(parent)
 
49
{
 
50
 
 
51
    setContentsMargins(0,0,0,0);
 
52
 
 
53
    control = new GcSplitterControl(this);
 
54
 
 
55
    splitter = new GcSubSplitter(orientation, control, this);
 
56
    splitter->setHandleWidth(23);
 
57
    splitter->setFrameStyle(QFrame::NoFrame);
 
58
    splitter->setContentsMargins(0,0,0,0);
 
59
    splitter->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
 
60
 
 
61
    QVBoxLayout *layout = new QVBoxLayout(this);
 
62
    layout->setSpacing(0);
 
63
    layout->setAlignment(Qt::AlignBottom|Qt::AlignHCenter);
 
64
    layout->setContentsMargins(0,0,0,0);
 
65
    layout->addWidget(splitter);
 
66
    layout->addWidget(control);
 
67
 
 
68
    connect(splitter,SIGNAL(splitterMoved(int,int)), this, SLOT(subSplitterMoved(int,int)));
 
69
}
 
70
 
 
71
void
 
72
GcSplitter::prepare(QString cyclist, QString name)
 
73
{
 
74
    this->name = name;
 
75
    this->cyclist = cyclist;
 
76
 
 
77
    QWidget *spacer = new QWidget(this);
 
78
    spacer->setAutoFillBackground(false);
 
79
    spacer->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
 
80
    control->addWidget(spacer);
 
81
 
 
82
    // get saved state
 
83
    QString statesetting = QString("splitter/%1/sizes").arg(name);
 
84
    QVariant sizes = appsettings->cvalue(cyclist, statesetting); 
 
85
    if (sizes != QVariant()) {
 
86
        splitter->restoreState(sizes.toByteArray());
 
87
        splitter->setOpaqueResize(true); // redraw when released, snappier UI
 
88
    }
 
89
 
 
90
    // should we hide / show each widget?
 
91
    for(int i=0; i<splitter->count(); i++) {
 
92
        QString hidesetting = QString("splitter/%1/hide/%2").arg(name).arg(i);
 
93
        QVariant hidden = appsettings->cvalue(cyclist, hidesetting);
 
94
        if (i && hidden != QVariant()) {
 
95
            if (hidden.toBool() == true) {
 
96
                splitter->widget(i)->hide();
 
97
            }
 
98
        }
 
99
    }
 
100
}
 
101
 
 
102
void
 
103
GcSplitter::saveSettings()
 
104
{
 
105
    // get saved state
 
106
    QString statesetting = QString("splitter/%1/sizes").arg(name);
 
107
    appsettings->setCValue(cyclist, statesetting, splitter->saveState()); 
 
108
 
 
109
    // should we hide / show each widget?
 
110
    for(int i=0; i<splitter->count(); i++) {
 
111
        QString hidesetting = QString("splitter/%1/hide/%2").arg(name).arg(i);
 
112
        appsettings->setCValue(cyclist, hidesetting, QVariant(splitter->widget(i)->isHidden()));
 
113
    }
 
114
}
 
115
 
 
116
void
 
117
GcSplitter::setOpaqueResize(bool opaque)
 
118
{
 
119
    return splitter->setOpaqueResize(opaque);
 
120
}
 
121
 
 
122
QList<int>
 
123
GcSplitter::sizes() const
 
124
{
 
125
    return splitter->sizes();
 
126
}
 
127
 
 
128
void
 
129
GcSplitter::setSizes(const QList<int> &list)
 
130
{
 
131
    return splitter->setSizes(list);
 
132
}
 
133
 
 
134
QByteArray
 
135
GcSplitter::saveState() const
 
136
{
 
137
    return splitter->saveState();
 
138
}
 
139
 
 
140
bool
 
141
GcSplitter::restoreState(const QByteArray &state)
 
142
{
 
143
    return splitter->restoreState(state);
 
144
}
 
145
 
 
146
void
 
147
GcSplitter::subSplitterMoved(int pos, int index)
 
148
{
 
149
   saveSettings();
 
150
   emit splitterMoved(pos, index);
 
151
}
 
152
 
 
153
void
 
154
GcSplitter::addWidget(QWidget *widget)
 
155
{
 
156
   splitter->addWidget(widget);
 
157
}
 
158
 
 
159
void
 
160
GcSplitter::insertWidget(int index, QWidget *widget)
 
161
{
 
162
    splitter->insertWidget(index, widget);
 
163
}
 
164
 
 
165
GcSubSplitter::GcSubSplitter(Qt::Orientation orientation, GcSplitterControl *control, GcSplitter *parent) : QSplitter(orientation, parent), control(control), gcSplitter (parent)
 
166
{
 
167
    _insertedWidget = NULL;
 
168
 
 
169
    // we add a fake widget to ensure the first real widget
 
170
    // that is added has a handle (even though it cannot be moved)
 
171
    QLabel *fake = new QLabel("fake");
 
172
    fake->setFixedHeight(0);
 
173
    setHandleWidth(0);
 
174
    QSplitter::addWidget(fake);
 
175
    //setStretchFactor(0,0);
 
176
}
 
177
 
 
178
void
 
179
GcSubSplitter::addWidget(QWidget *widget)
 
180
{
 
181
    _insertedWidget = widget;
 
182
    QSplitter::addWidget(widget);
 
183
    //setStretchFactor(indexOf(widget), 1);
 
184
}
 
185
 
 
186
void
 
187
GcSubSplitter::insertWidget(int index, QWidget *widget)
 
188
{
 
189
    _insertedWidget = widget;
 
190
    QSplitter::insertWidget(index, widget);
 
191
}
 
192
 
 
193
QSplitterHandle*
 
194
GcSubSplitter::createHandle()
 
195
{
 
196
    if(_insertedWidget != 0) {
 
197
        GcSplitterItem* _item = dynamic_cast<GcSplitterItem*>(_insertedWidget);
 
198
        if(_item != 0) {
 
199
            _item->splitterHandle = new GcSplitterHandle(_item->title, _item, orientation(), this);
 
200
            _item->splitterHandle->addActions(_item->actions());
 
201
            _item->controlAction = new QAction(_item->icon, _item->title, this);
 
202
            _item->controlAction->setStatusTip(_item->title);
 
203
            control->addAction(_item->controlAction);
 
204
 
 
205
            connect(_item->controlAction, SIGNAL(triggered(void)), _item, SLOT(selectHandle(void)));
 
206
            connect(_item->controlAction, SIGNAL(triggered(void)), gcSplitter, SLOT(saveSettings()));
 
207
            return _item->splitterHandle;
 
208
        }
 
209
    }
 
210
 
 
211
    return QSplitter::createHandle();
 
212
}
 
213
 
 
214
GcSplitterHandle::GcSplitterHandle(QString title, GcSplitterItem *widget, Qt::Orientation orientation, GcSubSplitter *parent) : QSplitterHandle(orientation, parent), widget(widget), title(title)
 
215
{
 
216
    setContentsMargins(0,0,0,0);
 
217
    setFixedHeight(23);
 
218
 
 
219
    gcSplitter = parent;
 
220
 
 
221
    titleLayout = new QHBoxLayout(this);
 
222
    titleLayout->setContentsMargins(0,0,0,0);
 
223
    titleLayout->setSpacing(2);
 
224
 
 
225
    titleLabel = new GcLabel(title, this);
 
226
    titleLabel->setXOff(0);
 
227
 
 
228
#ifdef Q_OS_MAC
 
229
    int shade = 178;
 
230
    int inshade = 225;
 
231
#else
 
232
    int shade = 200;
 
233
    int inshade = 250;
 
234
#endif
 
235
    active = QLinearGradient(0, 0, 0, 23);
 
236
    active.setColorAt(0.0, QColor(shade,shade,shade, 100));
 
237
    active.setColorAt(0.5, QColor(shade,shade,shade, 180));
 
238
    active.setColorAt(1.0, QColor(shade,shade,shade, 255));
 
239
    active.setSpread(QGradient::PadSpread);
 
240
    inactive = QLinearGradient(0, 0, 0, 23);
 
241
    inactive.setColorAt(0.0, QColor(inshade,inshade,inshade, 100));
 
242
    inactive.setColorAt(0.5, QColor(inshade,inshade,inshade, 180));
 
243
    inactive.setColorAt(1.0, QColor(inshade,inshade,inshade, 255));
 
244
    inactive.setSpread(QGradient::PadSpread);
 
245
 
 
246
    QFont font;
 
247
#ifdef Q_OS_MAC
 
248
    titleLabel->setFixedHeight(16);
 
249
    titleLabel->setYOff(1);
 
250
    font.setFamily("Lucida Grande");
 
251
    font.setPointSize(11);
 
252
#else
 
253
    titleLabel->setYOff(1);
 
254
    font.setFamily("Helvetica");
 
255
#ifdef WIN32
 
256
    font.setPointSize(8);
 
257
#else
 
258
    font.setPointSize(10);
 
259
#endif
 
260
#endif
 
261
    font.setWeight(QFont::Black);
 
262
    titleLabel->setFont(font);
 
263
 
 
264
    titleLayout->addSpacing(10);
 
265
    titleLayout->addWidget(titleLabel);
 
266
    titleLayout->addStretch();
 
267
 
 
268
    setCursor(Qt::ArrowCursor);
 
269
}
 
270
 
 
271
QSize
 
272
GcSplitterHandle::sizeHint() const
 
273
{
 
274
    return QSize(200, 23);
 
275
}
 
276
 
 
277
GcSubSplitter*
 
278
GcSplitterHandle::splitter() const
 
279
{
 
280
    return gcSplitter;
 
281
}
 
282
 
 
283
void
 
284
GcSplitterHandle::addAction(QAction *)
 
285
{
 
286
    //not used anyway titleToolbar->addAction(action);
 
287
}
 
288
 
 
289
void
 
290
GcSplitterHandle::addActions(QList<QAction*> actions)
 
291
{
 
292
    foreach(QAction *action, actions) {
 
293
        QToolButton *p = new QToolButton(this);
 
294
        p->setStyleSheet("QToolButton { border: none; padding: 0px; }");
 
295
        p->setAutoFillBackground(false);
 
296
        p->setFixedSize(20,20);
 
297
        p->setIcon(action->icon());
 
298
        p->setIconSize(QSize(10,10));
 
299
        p->setFocusPolicy(Qt::NoFocus);
 
300
        titleLayout->addWidget(p);
 
301
        connect(p, SIGNAL(clicked()), action, SLOT(trigger()));
 
302
    }
 
303
}
 
304
 
 
305
void
 
306
GcSplitterHandle::paintEvent(QPaintEvent *event)
 
307
{
 
308
    // paint the darn thing!
 
309
    paintBackground(event);
 
310
    QWidget::paintEvent(event);
 
311
}
 
312
 
 
313
void
 
314
GcSplitterHandle::paintBackground(QPaintEvent *)
 
315
{
 
316
    // setup a painter and the area to paint
 
317
    QPainter painter(this);
 
318
 
 
319
    painter.save();
 
320
    QRect all(0,0,width(),height());
 
321
 
 
322
    // fill with a linear gradient
 
323
    painter.setPen(Qt::NoPen);
 
324
    painter.fillRect(all, isActiveWindow() ? active : inactive);
 
325
 
 
326
    QPen black(QColor(100,100,100,200));
 
327
    painter.setPen(black);
 
328
    painter.drawLine(0,height()-1, width()-1, height()-1);
 
329
 
 
330
    QPen gray(QColor(230,230,230));
 
331
    painter.setPen(gray);
 
332
    painter.drawLine(0,0, width()-1, 0);
 
333
 
 
334
    painter.restore();
 
335
}
 
336
 
 
337
GcSplitterControl::GcSplitterControl(QWidget *parent) : QToolBar(parent)
 
338
{
 
339
    setContentsMargins(0,0,0,0);
 
340
    setFixedHeight(20);
 
341
    setIconSize(QSize(14,14));
 
342
    setToolButtonStyle(Qt::ToolButtonIconOnly);
 
343
    setAutoFillBackground(false);
 
344
 
 
345
#ifdef Q_OS_MAC
 
346
    int shade = 178;
 
347
    int inshade = 225;
 
348
#else
 
349
    int shade = 200;
 
350
    int inshade = 250;
 
351
#endif
 
352
    active = QLinearGradient(0, 0, 0, 20);
 
353
    active.setColorAt(0.0, QColor(shade,shade,shade, 100));
 
354
    active.setColorAt(0.5, QColor(shade,shade,shade, 180));
 
355
    active.setColorAt(1.0, QColor(shade,shade,shade, 255));
 
356
    active.setSpread(QGradient::PadSpread);
 
357
    inactive = QLinearGradient(0, 0, 0, 20);
 
358
    inactive.setColorAt(0.0, QColor(inshade,inshade,inshade, 100));
 
359
    inactive.setColorAt(0.5, QColor(inshade,inshade,inshade, 180));
 
360
    inactive.setColorAt(1.0, QColor(inshade,inshade,inshade, 255));
 
361
    inactive.setSpread(QGradient::PadSpread);
 
362
 
 
363
    QWidget *spacer = new QWidget(this);
 
364
    spacer->setAutoFillBackground(false);
 
365
    spacer->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
 
366
    addWidget(spacer);
 
367
}
 
368
 
 
369
void
 
370
GcSplitterControl::paintEvent(QPaintEvent *event)
 
371
{
 
372
    // paint the darn thing!
 
373
    paintBackground(event);
 
374
    //QToolBar::paintEvent(event);
 
375
}
 
376
 
 
377
void
 
378
GcSplitterControl::paintBackground(QPaintEvent *)
 
379
{
 
380
    QRect all(0,0,width(),height());
 
381
 
 
382
    // setup a painter and the area to paint
 
383
    QPainter painter(this);
 
384
 
 
385
    // fill with a linear gradient
 
386
    painter.setPen(Qt::NoPen);
 
387
    painter.fillRect(all, isActiveWindow() ? active : inactive);
 
388
    QPen gray(QColor(230,230,230));
 
389
    painter.setPen(gray);
 
390
    painter.drawLine(0,0, width()-1, 0);
 
391
 
 
392
}
 
393
 
 
394
void
 
395
GcSplitterControl::selectAction()
 
396
{
 
397
    this->setVisible(!this->isVisible());
 
398
 
 
399
    /*this->setBaseSize(width(), parentWidget()->height());
 
400
    this->setMaximumSize(QWIDGETSIZE_MAX,QWIDGETSIZE_MAX);*/
 
401
}
 
402
 
 
403
GcSplitterItem::GcSplitterItem(QString title, QIcon icon, QWidget *parent) : QWidget(parent), title(title), icon(icon)
 
404
{
 
405
    setContentsMargins(0,0,0,0);
 
406
    layout = new QVBoxLayout(this);
 
407
    layout->setContentsMargins(0,0,0,0);
 
408
    layout->setSpacing(0);
 
409
    content = NULL;
 
410
    //titleBar = new GcSideBarTitle(title, this);
 
411
    //layout->addWidget(titleBar);
 
412
}
 
413
 
 
414
void
 
415
GcSplitterItem::addWidget(QWidget *p)
 
416
{
 
417
    content = p;
 
418
    //p->setContentsMargins(0,0,0,0);
 
419
    layout->addWidget(p);
 
420
}
 
421
 
 
422
void
 
423
GcSplitterItem::selectHandle()
 
424
{
 
425
    //splitterHandle->gcSplitter->setStretchFactor(splitterHandle->index, this->isVisible() ? 0 : 1);
 
426
    this->setVisible(!this->isVisible());
 
427
    controlAction->setChecked(this->isVisible());
 
428
    /*this->setBaseSize(width(), parentWidget()->height());
 
429
    this->setMaximumSize(QWIDGETSIZE_MAX,QWIDGETSIZE_MAX);*/
 
430
}
 
431
 
 
432
GcSplitterItem::~GcSplitterItem()
 
433
{
 
434
}
 
435