~ubuntu-branches/ubuntu/saucy/rocs/saucy

« back to all changes in this revision

Viewing changes to App/Interface/SideDockWidget.cpp

  • Committer: Package Import Robot
  • Author(s): Rohan Garg, Rohan Garg, Philip Muškovac
  • Date: 2013-06-21 02:04:20 UTC
  • mfrom: (1.1.27)
  • Revision ID: package-import@ubuntu.com-20130621020420-lzlui9y7qc6w3xog
Tags: 4:4.10.80-0ubuntu1
[ Rohan Garg ]
* New upstream release

[ Philip Muškovac ]
* Build-depend on libgrantlee-dev and libx11-dev
* Update rocs.install and not-installed 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    This file is part of Rocs.
3
 
    Copyright 2012  Andreas Cord-Landwehr <cola@uni-paderborn.de>
4
 
 
5
 
    This program is free software; you can redistribute it and/or
6
 
    modify it under the terms of the GNU General Public License as
7
 
    published by the Free Software Foundation; either version 2 of
8
 
    the License, or (at your option) any later version.
9
 
 
10
 
    This program is distributed in the hope that it will be useful,
11
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
    GNU General Public License for more details.
14
 
 
15
 
    You should have received a copy of the GNU General Public License
16
 
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 
*/
18
 
 
19
 
 
20
 
#include "SideDockWidget.h"
21
 
 
22
 
#include <KAcceleratorManager>
23
 
#include <QStyle>
24
 
#include <QStyleOptionToolButton>
25
 
#include <QStylePainter>
26
 
#include <QToolBar>
27
 
#include <QLabel>
28
 
#include <QAction>
29
 
#include <QVBoxLayout>
30
 
#include <QLayout>
31
 
#include <QObject>
32
 
#include <QWidget>
33
 
#include <KIcon>
34
 
#include <KAction>
35
 
#include <KLocale>
36
 
#include <KDebug>
37
 
 
38
 
 
39
 
SideToolButton::SideToolButton(QWidget *parent)
40
 
    : QToolButton(parent)
41
 
{
42
 
    setFocusPolicy(Qt::NoFocus);
43
 
    KAcceleratorManager::setNoAccel(this);
44
 
    setCheckable(true);
45
 
    setAutoRaise(true);
46
 
    setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
47
 
}
48
 
 
49
 
Qt::Orientation SideToolButton::orientation() const
50
 
{
51
 
    return Qt::Vertical;
52
 
}
53
 
 
54
 
QSize SideToolButton::sizeHint() const
55
 
{
56
 
    ensurePolished();
57
 
 
58
 
    QStyleOptionToolButton option;
59
 
    initStyleOption(&option);
60
 
 
61
 
    QFontMetrics fm = fontMetrics();
62
 
 
63
 
    // calculate example character width
64
 
    const int charWidth = fm.width(QLatin1Char('x'));
65
 
 
66
 
    // compute text size
67
 
    QSize textSize;
68
 
    textSize = fm.size(Qt::TextShowMnemonic, option.text);
69
 
    textSize.rwidth() += 2 * charWidth;
70
 
 
71
 
    // compute icon size
72
 
    const int spacing = 2;
73
 
    int iconwidth = 0, iconheight = 0;
74
 
    iconwidth = option.iconSize.height();
75
 
    iconheight = option.iconSize.width();
76
 
 
77
 
    // compute final size
78
 
    int width = 4 + textSize.width() + iconwidth + spacing;
79
 
    int height = 4 + qMax(textSize.height(), iconheight) + spacing;
80
 
 
81
 
    return QSize(height, width);
82
 
}
83
 
 
84
 
void SideToolButton::paintEvent(QPaintEvent *event)
85
 
{
86
 
    Q_UNUSED(event);
87
 
 
88
 
    // rotated paint
89
 
    QStylePainter painter(this);
90
 
    QStyleOptionToolButton option;
91
 
    initStyleOption(&option);
92
 
 
93
 
    // first draw normal frame and not text/icon
94
 
    option.text.clear();
95
 
    option.icon = QIcon();
96
 
    painter.drawComplexControl(QStyle::CC_ToolButton, option);
97
 
 
98
 
    // rotate the options
99
 
    QSize size(option.rect.size());
100
 
    size.transpose();
101
 
    option.rect.setSize(size);
102
 
 
103
 
    // rotate the painter
104
 
    painter.translate(width(), 0);
105
 
    painter.rotate(90);
106
 
 
107
 
    // paint text and icon
108
 
    option.text = text();
109
 
    QIcon::Mode iconMode = (option.state & QStyle::State_MouseOver) ? QIcon::Active : QIcon::Normal;
110
 
    QPixmap ic = icon().pixmap(option.iconSize, iconMode, QIcon::On);
111
 
    QTransform transform;
112
 
    transform = transform.rotate(-90); // rotate right
113
 
    option.icon = ic.transformed(transform, Qt::SmoothTransformation);
114
 
    painter.drawControl(QStyle::CE_ToolButtonLabel, option);
115
 
    painter.end();
116
 
}
117
 
 
118
 
 
119
 
SideDockWidget::SideDockWidget(QWidget* parent)
120
 
    : QWidget(parent)
121
 
    , _showDock(false)
122
 
{
123
 
    Q_ASSERT(parent); // we need a parent widget
124
 
 
125
 
    _toolBar = new QToolBar(i18nc("@title", "Side Toolbar"), this);
126
 
    _toolBar->setFloatable(false);
127
 
    _toolBar->setMovable(false);
128
 
    _toolBar->setObjectName("sidebar");
129
 
    _toolBar->setIconSize(QSize(16,16));
130
 
    _toolBar->setContextMenuPolicy(Qt::PreventContextMenu);
131
 
    setLayout(new QVBoxLayout);
132
 
}
133
 
 
134
 
void SideDockWidget::addDock(QWidget* widget, const QString& title, const KIcon& icon)
135
 
{
136
 
    widget->setVisible(false);
137
 
    layout()->addWidget(widget);
138
 
 
139
 
    SideToolButton* button = new SideToolButton(this);
140
 
 
141
 
    button->setText(title);
142
 
    button->setToolTip(i18n("Toggle '%1' view.", title));
143
 
    button->setIcon(icon);
144
 
    button->setShortcut(QKeySequence());
145
 
    button->setChecked(false); // initially do not check
146
 
 
147
 
    // only request action on user set action
148
 
    connect(button, SIGNAL(clicked(bool)), this, SLOT(buttonToggled(bool)));
149
 
 
150
 
    // register and add to list
151
 
    _toolBar->addWidget(button);
152
 
    _widgets.insert(button, widget);
153
 
}
154
 
 
155
 
void SideDockWidget::buttonToggled(bool state)
156
 
{
157
 
    Q_UNUSED(state);
158
 
 
159
 
    SideToolButton* button = qobject_cast<SideToolButton*>(sender());
160
 
    if (!button) {
161
 
        kWarning() << "Wrong sender for side bar toggle action, aborting";
162
 
        return;
163
 
    }
164
 
 
165
 
    Q_ASSERT(button->isChecked() == state);
166
 
    Q_ASSERT(_widgets.contains(button));
167
 
 
168
 
    // if button is toggled off, close dock
169
 
    if (button->isChecked() == false) {
170
 
        showDock(false, _widgets[button]);
171
 
    } else {
172
 
        showDock(true, _widgets[button]);
173
 
    }
174
 
 
175
 
    // only one button is allowed to be toggled
176
 
    QHash<SideToolButton*, QWidget*>::iterator iter = _widgets.begin();
177
 
    while (iter != _widgets.end()) {
178
 
        if (iter.key() != button) {
179
 
            iter.key()->setChecked(false);
180
 
            (*iter)->setVisible(false);
181
 
        }
182
 
        ++iter;
183
 
    }
184
 
}
185
 
 
186
 
void SideDockWidget::showDock(bool show, QWidget* widget)
187
 
{
188
 
    _showDock = show;
189
 
    if (show == false) {
190
 
        widget->setVisible(false);
191
 
        parentWidget()->setVisible(false);
192
 
    }
193
 
 
194
 
    if (show == true) {
195
 
        widget->setVisible(true);
196
 
        parentWidget()->setVisible(true);;
197
 
    }
198
 
    emit visibilityChanged(show);
199
 
}
200
 
 
201
 
QToolBar* SideDockWidget::toolbar() const
202
 
{
203
 
    return _toolBar;
204
 
}