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

liblancelot

  • sources
  • kde-4.14
  • workspace
  • kdeplasma-addons
  • libs
  • lancelot
  • widgets
TabBar.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 Ivan Cukic <ivan.cukic(at)kde.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser/Library General Public License version 2,
6  * or (at your option) any later version, as published by the Free
7  * Software Foundation
8  *
9  * This program 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
12  * GNU Lesser/Library General Public License for more details
13  *
14  * You should have received a copy of the GNU Lesser/Library General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #include <lancelot/widgets/TabBar.h>
21 
22 #include <QPainter>
23 #include <QSignalMapper>
24 #include <QGraphicsSceneMoveEvent>
25 
26 #include <KDebug>
27 
28 #include <Plasma/FrameSvg>
29 
30 #include <lancelot/widgets/ExtenderButton.h>
31 #include <lancelot/widgets/CustomItemBackground_p.h>
32 #include <lancelot/Global.h>
33 
34 namespace Lancelot
35 {
36 
37 class TabBar::Private {
38 public:
39  Private(TabBar * parent);
40 
41  Qt::Orientation orientation;
42  Qt::Orientation textDirection;
43  Plasma::Flip flip;
44 
45  QString currentTab;
46  QString groupName;
47  QSize tabIconSize;
48 
49  QMap < QString, ExtenderButton * > tabs;
50  QList < ExtenderButton * > tabButtons;
51 
52  QMap < ExtenderButton *, QPair < QString, QString > > mimes;
53 
54  QSignalMapper mapper;
55  CustomItemBackground * background;
56 
57  void relayout();
58  void updateOrientation();
59  bool isRotated() const;
60 
61  TabBar * const q;
62 };
63 
64 TabBar::Private::Private(TabBar * parent)
65  : q(parent)
66 {
67  orientation = Qt::Horizontal;
68  textDirection = Qt::Horizontal;
69  groupName = QLatin1String("TabBarButton");
70  tabIconSize = QSize(32, 32);
71 
72  connect(&mapper, SIGNAL(mapped(QString)),
73  parent, SIGNAL(currentTabChanged(QString)));
74  connect(&mapper, SIGNAL(mapped(QString)),
75  parent, SLOT(setCurrentTab(QString)));
76 }
77 
78 bool TabBar::Private::isRotated() const
79 {
80  return (orientation == Qt::Vertical && textDirection == Qt::Horizontal);
81 }
82 
83 void TabBar::Private::relayout()
84 {
85  int diff;
86  QPointF cursor = QPointF(0, 0);
87  QSizeF size = q->size();
88 
89  if (q->size().isNull() || tabs.size() == 0) {
90  return;
91  }
92 
93  if (orientation == Qt::Horizontal) {
94  diff = q->size().width() / tabs.size();
95  size.setWidth(diff);
96  } else {
97  diff = q->size().height() / tabs.size();
98  size.setHeight(diff);
99  }
100 
101  if (isRotated()) {
102  cursor.ry() += diff;
103  size = QSizeF(size.height(), size.width());
104  }
105 
106  bool shouldFlip =
107  (orientation == Qt::Vertical && (flip & Plasma::VerticalFlip))
108  ||
109  (orientation == Qt::Horizontal && (flip & Plasma::HorizontalFlip));
110 
111  QListIterator < ExtenderButton * > i (tabButtons);
112  if (shouldFlip) {
113  i.toBack();
114  }
115 
116  while (shouldFlip ? i.hasPrevious() : i.hasNext()) {
117  ExtenderButton * button =
118  shouldFlip ? i.previous() : i.next();
119 
120  if (isRotated()) {
121  button->setRotation(-90);
122  } else {
123  button->setRotation(0);
124  }
125 
126  button->setMaximumSize(size);
127  button->setGeometry(QRectF(cursor, size));
128 
129  if (orientation == Qt::Horizontal) {
130  cursor.rx() += diff;
131  } else {
132  cursor.ry() += diff;
133  }
134  }
135 
136  q->setCurrentTab(currentTab);
137 }
138 
139 void TabBar::Private::updateOrientation()
140 {
141  foreach(Lancelot::ExtenderButton * button, tabs) {
142  button->setInnerOrientation(
143  (orientation == Qt::Vertical && textDirection == Qt::Horizontal)
144  ? Qt::Horizontal : Qt::Vertical
145  );
146  }
147 }
148 
149 TabBar::TabBar(QGraphicsWidget * parent)
150  : QGraphicsWidget(parent), d(new Private(this))
151 {
152  d->background = new CustomItemBackground(this);
153  d->background->hide();
154 }
155 
156 TabBar::~TabBar()
157 {
158  delete d->background;
159  delete d;
160 }
161 
162 Qt::Orientation TabBar::orientation() const
163 {
164  return d->orientation;
165 }
166 
167 void TabBar::setOrientation(Qt::Orientation value)
168 {
169  d->orientation = value;
170  d->updateOrientation();
171 }
172 
173 Qt::Orientation TabBar::textDirection() const
174 {
175  return d->textDirection;
176 }
177 
178 void TabBar::setTextDirection(Qt::Orientation value)
179 {
180  d->textDirection = value;
181  d->updateOrientation();
182 }
183 
184 QString TabBar::currentTab() const
185 {
186  return d->currentTab;
187 }
188 
189 void TabBar::setCurrentTab(const QString & current)
190 {
191  if (!d->tabs.contains(current)) {
192  d->background->hide();
193  return;
194  }
195 
196  d->currentTab = current;
197 
198  if (d->isRotated()) {
199  QRectF g = d->tabs[current]->geometry();
200 
201  g.setSize(QSizeF(g.size().height(), g.size().width()));
202  g.moveTop(g.top() - g.height());
203 
204  d->background->setTarget(g);
205 
206  } else {
207  d->background->setTarget(d->tabs[current]->geometry());
208 
209  }
210  d->background->show();
211 
212  emit currentTabChanged(current);
213 }
214 
215 void TabBar::addTab(const QString & id, const QIcon & icon, const QString & title,
216  const QString & mimeType, const QString & mimeData)
217 {
218  if (d->tabs.contains(id)) {
219  return;
220  }
221 
222  Lancelot::ExtenderButton * button = new ExtenderButton(
223  icon, title, QString::null, this);
224  d->tabs[id] = button;
225  d->tabButtons.append(button);
226 
227  button->installSceneEventFilter(this);
228 
229  button->setIconSize(d->tabIconSize);
230  button->setGroupByName(d->groupName);
231 
232  if (!mimeType.isEmpty() && !mimeData.isEmpty()) {
233  d->mimes[button] = QPair < QString, QString > (mimeType, mimeData);
234  }
235 
236  connect(
237  button, SIGNAL(activated()),
238  &(d->mapper), SLOT(map()));
239  d->mapper.setMapping(button, id);
240 
241 
242  d->relayout();
243 }
244 
245 void TabBar::removeTab(const QString & id)
246 {
247  if (d->tabs.contains(id)) {
248  return;
249  }
250 
251  d->tabButtons.removeAll(d->tabs[id]);
252  d->mimes.remove(d->tabs[id]);
253 
254  delete d->tabs[id];
255  d->tabs.remove(id);
256 
257  d->relayout();
258 }
259 
260 void TabBar::setTabsGroupName(const QString & groupName)
261 {
262  Group * group = Global::self()->group(groupName);
263 
264  if (!group) return;
265 
266  foreach (ExtenderButton * button, d->tabs) {
267  button->setGroup(group);
268  }
269 
270  // d->background->setSvg(group->backgroundSvg(), "down");
271  d->background->setGroup(group);
272  d->background->setSvgElementPrefix("down");
273 
274  d->groupName = groupName;
275 }
276 
277 void TabBar::resizeEvent(QGraphicsSceneResizeEvent * event)
278 {
279  Q_UNUSED(event);
280  d->relayout();
281 }
282 
283 void TabBar::setFlip(Plasma::Flip flip)
284 {
285  d->flip = flip;
286  d->relayout();
287 }
288 
289 Plasma::Flip TabBar::flip() const
290 {
291  return d->flip;
292 }
293 
294 
295 void TabBar::setTabIconSize(const QSize & size)
296 {
297  d->tabIconSize = size;
298  foreach (ExtenderButton * button, d->tabButtons) {
299  button->setIconSize(size);
300  }
301 }
302 
303 QSize TabBar::tabIconSize() const
304 {
305  return d->tabIconSize;
306 }
307 
308 bool TabBar::sceneEventFilter(QGraphicsItem * object, QEvent * event)
309 {
310  if (Global::self()->immutability() != Plasma::Mutable
311  || event->type() != QEvent::GraphicsSceneMouseMove) {
312  return false;
313  }
314 
315  ExtenderButton * button = static_cast < ExtenderButton * > (object);
316 
317  if (!button
318  || !d->mimes.contains(button)
319  ) {
320  return false;
321  }
322 
323  QMimeData * data = new QMimeData();
324  data->setData(
325  d->mimes[button].first,
326  d->mimes[button].second.toAscii());
327 
328  QDrag * drag = new QDrag(
329  static_cast < QGraphicsSceneMoveEvent * > (event)->widget());
330  drag->setMimeData(data);
331  drag->exec();
332 
333  return false;
334 }
335 
336 } // namespace Lancelot
337 
Lancelot::TabBar::sceneEventFilter
L_Override bool sceneEventFilter(QGraphicsItem *watched, QEvent *event)
Definition: TabBar.cpp:308
Lancelot::Widget::setGroupByName
virtual void setGroupByName(const QString &groupName)
Sets this widget's group by group name.
Definition: Widget.cpp:134
QEvent
QEvent::type
Type type() const
Lancelot::TabBar::currentTabChanged
void currentTabChanged(const QString &current)
This signal is emitted when the currently selected tab is changed.
Lancelot::TabBar::~TabBar
~TabBar()
Destroys this Lancelot::TabBar.
Definition: TabBar.cpp:156
QDrag::setMimeData
void setMimeData(QMimeData *data)
Lancelot::Global::self
static Global * self()
Definition: Global.cpp:307
QRectF::size
QSizeF size() const
QMap
Lancelot::TabBar::flip
Plasma::Flip flip() const
Definition: TabBar.cpp:289
QRectF::top
qreal top() const
QGraphicsItem
Lancelot::TabBar::setTabIconSize
void setTabIconSize(const QSize &size)
Sets the icon size for tab buttons.
Definition: TabBar.cpp:295
Lancelot::TabBar::setTextDirection
void setTextDirection(Qt::Orientation value)
Sets the inner layout of tab buttons - that is whether the icon is above or beside the text...
Definition: TabBar.cpp:178
Lancelot::BasicWidget::setInnerOrientation
void setInnerOrientation(Qt::Orientation orientation)
Sets the inner orientation of this Lancelot::BasicWidget.
Definition: BasicWidget.cpp:485
Lancelot::Global::group
Group * group(const QString &name)
Definition: Global.cpp:442
QMimeData
QPointF
QGraphicsWidget::size
QSizeF size() const
QDrag::exec
Qt::DropAction exec(QFlags< Qt::DropAction > supportedActions)
Lancelot::TabBar::currentTab
QString currentTab() const
QGraphicsItem::group
QGraphicsItemGroup * group() const
QSizeF::setWidth
void setWidth(qreal width)
Lancelot::TabBar::setTabsGroupName
void setTabsGroupName(const QString &groupName)
Sets the Lancelot::Group for the tab buttons.
Definition: TabBar.cpp:260
QDrag
Lancelot::Group
Represents a group of object.
Definition: Global.h:63
QString::isEmpty
bool isEmpty() const
Lancelot::StandardActionTreeModel::size
L_Override int size() const
Definition: StandardActionTreeModel.cpp:160
QGraphicsWidget
Global.h
QString
QList
QGraphicsSceneResizeEvent
Lancelot::BasicWidget::setIconSize
void setIconSize(QSize size)
Sets icon size of this Lancelot::BasicWidget.
Definition: BasicWidget.cpp:413
QPair
QSize
TabBar.h
Lancelot::ExtenderButton::setGroup
L_Override void setGroup(Group *group=NULL)
Sets this widget's group.
Definition: ExtenderButton.cpp:340
Lancelot::CustomItemBackground
Definition: CustomItemBackground_p.h:37
QGraphicsItem::data
QVariant data(int key) const
Lancelot::TabBar::setCurrentTab
void setCurrentTab(const QString &current)
Sets the current tab.
Definition: TabBar.cpp:189
Lancelot::TabBar::textDirection
Qt::Orientation textDirection() const
QSizeF
QLatin1String
Lancelot::TabBar::addTab
void addTab(const QString &id, const QIcon &icon, const QString &title, const QString &mimeType=QString::null, const QString &mimeData=QString::null)
Adds a new tab.
Definition: TabBar.cpp:215
QRectF
ExtenderButton.h
Lancelot::TabBar::setOrientation
void setOrientation(Qt::Orientation value)
Sets the tab bar orientation.
Definition: TabBar.cpp:167
QPointF::rx
qreal & rx()
QPointF::ry
qreal & ry()
QGraphicsItem::installSceneEventFilter
void installSceneEventFilter(QGraphicsItem *filterItem)
QRectF::height
qreal height() const
Lancelot::TabBar::tabIconSize
QSize tabIconSize() const
QListIterator
QRectF::setSize
void setSize(const QSizeF &size)
CustomItemBackground_p.h
QMimeData::setData
void setData(const QString &mimeType, const QByteArray &data)
Lancelot::ExtenderButton
Button widget with special activation options beside clicking - hover and extender activation...
Definition: ExtenderButton.h:39
QSizeF::height
qreal height() const
QRectF::moveTop
void moveTop(qreal y)
Lancelot::TabBar::resizeEvent
L_Override void resizeEvent(QGraphicsSceneResizeEvent *event)
Definition: TabBar.cpp:277
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject::parent
QObject * parent() const
Lancelot::TabBar::removeTab
void removeTab(const QString &id)
Removes the specified tab.
Definition: TabBar.cpp:245
QSizeF::setHeight
void setHeight(qreal height)
QSignalMapper
Lancelot::TabBar::setFlip
void setFlip(Plasma::Flip flip)
Sets the layout flip.
Definition: TabBar.cpp:283
QSizeF::width
qreal width() const
Lancelot::TabBar::TabBar
TabBar(QGraphicsWidget *parent=0)
Creates a new Lancelot::TabBar.
Definition: TabBar.cpp:149
QtConcurrent::mapped
QFuture< T > mapped(const Sequence &sequence, MapFunction function)
QIcon
Lancelot::TabBar::orientation
Qt::Orientation orientation() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:43:01 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

liblancelot

Skip menu "liblancelot"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

workspace API Reference

Skip menu "workspace API Reference"
  • kdeplasma-addons
  •       GroupingDesktop
  •     liblancelot

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