2
This file is part of Rocs.
3
Copyright 2012 Andreas Cord-Landwehr <cola@uni-paderborn.de>
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.
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.
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/>.
20
#include "SideDockWidget.h"
22
#include <KAcceleratorManager>
24
#include <QStyleOptionToolButton>
25
#include <QStylePainter>
29
#include <QVBoxLayout>
39
SideToolButton::SideToolButton(QWidget *parent)
42
setFocusPolicy(Qt::NoFocus);
43
KAcceleratorManager::setNoAccel(this);
46
setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
49
Qt::Orientation SideToolButton::orientation() const
54
QSize SideToolButton::sizeHint() const
58
QStyleOptionToolButton option;
59
initStyleOption(&option);
61
QFontMetrics fm = fontMetrics();
63
// calculate example character width
64
const int charWidth = fm.width(QLatin1Char('x'));
68
textSize = fm.size(Qt::TextShowMnemonic, option.text);
69
textSize.rwidth() += 2 * charWidth;
72
const int spacing = 2;
73
int iconwidth = 0, iconheight = 0;
74
iconwidth = option.iconSize.height();
75
iconheight = option.iconSize.width();
78
int width = 4 + textSize.width() + iconwidth + spacing;
79
int height = 4 + qMax(textSize.height(), iconheight) + spacing;
81
return QSize(height, width);
84
void SideToolButton::paintEvent(QPaintEvent *event)
89
QStylePainter painter(this);
90
QStyleOptionToolButton option;
91
initStyleOption(&option);
93
// first draw normal frame and not text/icon
95
option.icon = QIcon();
96
painter.drawComplexControl(QStyle::CC_ToolButton, option);
99
QSize size(option.rect.size());
101
option.rect.setSize(size);
103
// rotate the painter
104
painter.translate(width(), 0);
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);
119
SideDockWidget::SideDockWidget(QWidget* parent)
123
Q_ASSERT(parent); // we need a parent widget
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);
134
void SideDockWidget::addDock(QWidget* widget, const QString& title, const KIcon& icon)
136
widget->setVisible(false);
137
layout()->addWidget(widget);
139
SideToolButton* button = new SideToolButton(this);
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
147
// only request action on user set action
148
connect(button, SIGNAL(clicked(bool)), this, SLOT(buttonToggled(bool)));
150
// register and add to list
151
_toolBar->addWidget(button);
152
_widgets.insert(button, widget);
155
void SideDockWidget::buttonToggled(bool state)
159
SideToolButton* button = qobject_cast<SideToolButton*>(sender());
161
kWarning() << "Wrong sender for side bar toggle action, aborting";
165
Q_ASSERT(button->isChecked() == state);
166
Q_ASSERT(_widgets.contains(button));
168
// if button is toggled off, close dock
169
if (button->isChecked() == false) {
170
showDock(false, _widgets[button]);
172
showDock(true, _widgets[button]);
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);
186
void SideDockWidget::showDock(bool show, QWidget* widget)
190
widget->setVisible(false);
191
parentWidget()->setVisible(false);
195
widget->setVisible(true);
196
parentWidget()->setVisible(true);;
198
emit visibilityChanged(show);
201
QToolBar* SideDockWidget::toolbar() const