• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

rocs/App

  • sources
  • kde-4.14
  • kdeedu
  • rocs
  • App
  • Ui
SideDockWidget.cpp
Go to the documentation of this file.
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 }
QWidget::layout
QLayout * layout() const
QStylePainter
QTransform
QWidget
QHash::insert
iterator insert(const Key &key, const T &value)
SideDockWidget::toolbar
QToolBar * toolbar() const
Definition: SideDockWidget.cpp:201
QSize::width
int width() const
QPainter::end
bool end()
QHash::key
const Key key(const T &value) const
QWidget::setFocusPolicy
void setFocusPolicy(Qt::FocusPolicy policy)
QObject::sender
QObject * sender() const
SideDockWidget.h
QSize::rwidth
int & rwidth()
QWidget::setVisible
virtual void setVisible(bool visible)
QToolBar::addWidget
QAction * addWidget(QWidget *widget)
QWidget::ensurePolished
void ensurePolished() const
QSize::transpose
void transpose()
QStyleOptionToolButton
QFontMetrics
QPainter::rotate
void rotate(qreal angle)
QAbstractButton::icon
QIcon icon() const
QToolButton::initStyleOption
void initStyleOption(QStyleOptionToolButton *option) const
QStylePainter::drawControl
void drawControl(QStyle::ControlElement ce, const QStyleOption &option)
QIcon::pixmap
QPixmap pixmap(const QSize &size, Mode mode, State state) const
QWidget::width
int width() const
SideToolButton
Used for vertical tool button bars.
Definition: SideDockWidget.h:36
QWidget::size
QSize size() const
SideDockWidget::addDock
void addDock(QWidget *dock, const QString &title, const KIcon &icon)
Definition: SideDockWidget.cpp:134
SideDockWidget::visibilityChanged
void visibilityChanged(bool visible)
QWidget::setLayout
void setLayout(QLayout *layout)
QHash
QObject::setObjectName
void setObjectName(const QString &name)
QToolBar::setMovable
void setMovable(bool movable)
QHash::begin
iterator begin()
QVBoxLayout
QAbstractButton::setShortcut
void setShortcut(const QKeySequence &key)
QAbstractButton::setCheckable
void setCheckable(bool)
SideDockWidget::buttonToggled
void buttonToggled(bool state)
Definition: SideDockWidget.cpp:155
QString
QToolButton::setAutoRaise
void setAutoRaise(bool enable)
QLayout::addWidget
void addWidget(QWidget *w)
QPixmap
SideToolButton::paintEvent
virtual void paintEvent(QPaintEvent *event)
Definition: SideDockWidget.cpp:84
QToolButton
QSize
QLatin1Char
QWidget::setContextMenuPolicy
void setContextMenuPolicy(Qt::ContextMenuPolicy policy)
QFontMetrics::width
int width(const QString &text, int len) const
SideDockWidget::SideDockWidget
SideDockWidget(QWidget *parent)
Definition: SideDockWidget.cpp:119
QAbstractButton::setChecked
void setChecked(bool)
QTransform::rotate
QTransform & rotate(qreal angle, Qt::Axis axis)
QWidget::fontMetrics
QFontMetrics fontMetrics() const
QPixmap::transformed
QPixmap transformed(const QMatrix &matrix, Qt::TransformationMode mode) const
SideDockWidget::showDock
void showDock(bool show, QWidget *widget)
Definition: SideDockWidget.cpp:186
QKeySequence
QWidget::parentWidget
QWidget * parentWidget() const
QToolBar
QSize::height
int height() const
QPainter::translate
void translate(const QPointF &offset)
QStylePainter::drawComplexControl
void drawComplexControl(QStyle::ComplexControl cc, const QStyleOptionComplex &option)
QAbstractButton::text
QString text() const
QHash::contains
bool contains(const Key &key) const
QFontMetrics::size
QSize size(int flags, const QString &text, int tabStops, int *tabArray) const
QToolBar::setIconSize
void setIconSize(const QSize &iconSize)
QWidget::show
void show()
QWidget::setToolTip
void setToolTip(const QString &)
QPaintEvent
QHash::end
iterator end()
SideToolButton::SideToolButton
SideToolButton(QWidget *parent=0)
Definition: SideDockWidget.cpp:39
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QToolButton::setToolButtonStyle
void setToolButtonStyle(Qt::ToolButtonStyle style)
QWidget::height
int height() const
SideToolButton::sizeHint
virtual QSize sizeHint() const
Definition: SideDockWidget.cpp:54
QIcon
SideToolButton::orientation
Qt::Orientation orientation() const
Definition: SideDockWidget.cpp:49
QToolBar::setFloatable
void setFloatable(bool floatable)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:16:13 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

rocs/App

Skip menu "rocs/App"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal