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

Konsole

  • kde-4.14
  • applications
  • konsole
  • src
ViewContainerTabBar.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Konsole Terminal.
3 
4  Copyright 2006-2008 Robert Knight <robertknight@gmail.com>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301 USA.
20 */
21 
22 // Own
23 #include "ViewContainerTabBar.h"
24 #include "ViewContainer.h"
25 
26 // Qt
27 #include <QtCore/QMimeData>
28 #include <QLabel>
29 #include <QtGui/QPainter>
30 #include <QtGui/QDragMoveEvent>
31 
32 // KDE
33 #include <KLocalizedString>
34 #include <KIcon>
35 
36 using Konsole::ViewContainerTabBar;
37 using Konsole::TabbedViewContainer;
38 
39 ViewContainerTabBar::ViewContainerTabBar(QWidget* parent, TabbedViewContainer* container)
40  : KTabBar(parent)
41  , _dropIndicator(0)
42  , _dropIndicatorIndex(-1)
43  , _drawIndicatorDisabled(false)
44  , _connectedContainer(container)
45 {
46  setDrawBase(true);
47  setDocumentMode(true);
48  setFocusPolicy(Qt::NoFocus);
49  setSelectionBehaviorOnRemove(QTabBar::SelectPreviousTab);
50  setElideMode(Qt::ElideLeft);
51 
52  setWhatsThis(i18nc("@info:whatsthis",
53  "<title>Tab Bar</title>"
54  "<para>The tab bar allows you to switch and move tabs. You can double-click a tab to change its name.</para>"));
55 }
56 
57 void ViewContainerTabBar::dragEnterEvent(QDragEnterEvent* event)
58 {
59  if (event->mimeData()->hasFormat(_supportedMimeType) &&
60  event->source() != 0)
61  event->acceptProposedAction();
62 }
63 
64 void ViewContainerTabBar::dragLeaveEvent(QDragLeaveEvent*)
65 {
66  setDropIndicator(-1);
67 }
68 
69 void ViewContainerTabBar::dragMoveEvent(QDragMoveEvent* event)
70 {
71  if (event->mimeData()->hasFormat(_supportedMimeType)
72  && event->source() != 0) {
73  int index = dropIndex(event->pos());
74  if (index == -1)
75  index = count();
76 
77  setDropIndicator(index, proposedDropIsSameTab(event));
78 
79  event->acceptProposedAction();
80  }
81 }
82 
83 void ViewContainerTabBar::dropEvent(QDropEvent* event)
84 {
85  setDropIndicator(-1);
86 
87  if (!event->mimeData()->hasFormat(_supportedMimeType)) {
88  event->ignore();
89  return;
90  }
91 
92  if (proposedDropIsSameTab(event)) {
93  event->setDropAction(Qt::IgnoreAction);
94  event->accept();
95  return;
96  }
97 
98  const int index = dropIndex(event->pos());
99  bool success = false;
100 
101  ViewContainerTabBar* sourceContainerTabBar = static_cast<ViewContainerTabBar*>(event->source());
102 
103  // check if the moved tab is the last of source view.
104  if (sourceContainerTabBar->count() == 1) {
105  TabbedViewContainer* sourceTabbedContainer = sourceContainerTabBar->connectedTabbedViewContainer();
106  emit moveViewRequest(index, event, success, sourceTabbedContainer);
107  } else {
108  emit moveViewRequest(index, event, success, NULL);
109  }
110 
111  if (success)
112  event->accept();
113  else
114  event->ignore();
115 }
116 
117 TabbedViewContainer* ViewContainerTabBar::connectedTabbedViewContainer()
118 {
119  return _connectedContainer;
120 }
121 
122 void ViewContainerTabBar::setDropIndicator(int index, bool drawDisabled)
123 {
124  if (!parentWidget() || _dropIndicatorIndex == index)
125  return;
126 
127  _dropIndicatorIndex = index;
128  const int ARROW_SIZE = 32;
129  const bool north = shape() == KTabBar::RoundedNorth || shape() == KTabBar::TriangularNorth;
130 
131  if (!_dropIndicator || _drawIndicatorDisabled != drawDisabled) {
132  if (!_dropIndicator) {
133  _dropIndicator = new QLabel(parentWidget());
134  _dropIndicator->resize(ARROW_SIZE, ARROW_SIZE);
135  }
136 
137  QIcon::Mode drawMode = drawDisabled ? QIcon::Disabled : QIcon::Normal;
138  const QString iconName = north ? "arrow-up" : "arrow-down";
139  _dropIndicator->setPixmap(KIcon(iconName).pixmap(ARROW_SIZE, ARROW_SIZE, drawMode));
140  _drawIndicatorDisabled = drawDisabled;
141  }
142 
143  if (index < 0) {
144  _dropIndicator->hide();
145  return;
146  }
147 
148  const QRect rect = tabRect(index < count() ? index : index - 1);
149 
150  QPoint pos;
151  if (index < count())
152  pos = rect.topLeft();
153  else
154  pos = rect.topRight();
155 
156  if (north)
157  pos.ry() += ARROW_SIZE;
158  else
159  pos.ry() -= ARROW_SIZE;
160 
161  pos.rx() -= ARROW_SIZE / 2;
162 
163  _dropIndicator->move(mapTo(parentWidget(), pos));
164  _dropIndicator->show();
165 }
166 
167 void ViewContainerTabBar::setSupportedMimeType(const QString& mimeType)
168 {
169  _supportedMimeType = mimeType;
170 }
171 
172 int ViewContainerTabBar::dropIndex(const QPoint& pos) const
173 {
174  int tab = tabAt(pos);
175  if (tab < 0)
176  return tab;
177 
178  // pick the closest tab boundary
179  QRect rect = tabRect(tab);
180  if ((pos.x() - rect.left()) > (rect.width() / 2))
181  tab++;
182 
183  if (tab == count())
184  return -1;
185 
186  return tab;
187 }
188 
189 bool ViewContainerTabBar::proposedDropIsSameTab(const QDropEvent* event) const
190 {
191  const bool sameTabBar = event->source() == this;
192  if (!sameTabBar)
193  return false;
194 
195  const int index = dropIndex(event->pos());
196  int sourceIndex = -1;
197  emit querySourceIndex(event, sourceIndex);
198 
199  const bool sourceAndDropAreLast = sourceIndex == count() - 1 && index == -1;
200  if (sourceIndex == index || sourceIndex == index - 1 || sourceAndDropAreLast)
201  return true;
202  else
203  return false;
204 }
205 
206 
207 QPixmap ViewContainerTabBar::dragDropPixmap(int tab)
208 {
209  Q_ASSERT(tab >= 0 && tab < count());
210 
211  // TODO - grabWidget() works except that it includes part
212  // of the tab bar outside the tab itself if the tab has
213  // curved corners
214  const QRect rect = tabRect(tab);
215  const int borderWidth = 1;
216 
217  QPixmap tabPixmap(rect.width() + borderWidth,
218  rect.height() + borderWidth);
219  QPainter painter(&tabPixmap);
220  painter.drawPixmap(0, 0, QPixmap::grabWidget(this, rect));
221  QPen borderPen;
222  borderPen.setBrush(palette().dark());
223  borderPen.setWidth(borderWidth);
224  painter.setPen(borderPen);
225  painter.drawRect(0, 0, rect.width(), rect.height());
226  painter.end();
227 
228  return tabPixmap;
229 }
QWidget
QPoint::rx
int & rx()
QPoint::ry
int & ry()
QRect::topRight
QPoint topRight() const
QDropEvent::mimeData
const QMimeData * mimeData() const
Konsole::TabbedViewContainer
An alternative tabbed view container which uses a QTabBar and QStackedWidget combination for navigati...
Definition: ViewContainer.h:361
QDragMoveEvent
QMimeData::hasFormat
virtual bool hasFormat(const QString &mimeType) const
QLabel::setPixmap
void setPixmap(const QPixmap &)
QDropEvent::pos
const QPoint & pos() const
QRect::height
int height() const
QPoint
QDropEvent::acceptProposedAction
void acceptProposedAction()
QPen::setBrush
void setBrush(const QBrush &brush)
Konsole::ViewContainerTabBar::setSupportedMimeType
void setSupportedMimeType(const QString &mimeType)
Definition: ViewContainerTabBar.cpp:167
QPoint::x
int x() const
Konsole::ViewContainerTabBar::dragDropPixmap
QPixmap dragDropPixmap(int tab)
Definition: ViewContainerTabBar.cpp:207
Konsole::ViewContainerTabBar::dragEnterEvent
virtual void dragEnterEvent(QDragEnterEvent *event)
Definition: ViewContainerTabBar.cpp:57
QWidget::resize
void resize(int w, int h)
QRect
Konsole::ViewContainerTabBar::connectedTabbedViewContainer
TabbedViewContainer * connectedTabbedViewContainer()
Definition: ViewContainerTabBar.cpp:117
QRect::left
int left() const
QDropEvent
QPainter
QWidget::move
void move(int x, int y)
KTabBar
Konsole::ViewContainerTabBar::dragLeaveEvent
virtual void dragLeaveEvent(QDragLeaveEvent *event)
Definition: ViewContainerTabBar.cpp:64
QString
QWidget::hide
void hide()
Konsole::ViewContainerTabBar
Definition: ViewContainerTabBar.h:34
Konsole::ViewContainerTabBar::dragMoveEvent
virtual void dragMoveEvent(QDragMoveEvent *event)
Definition: ViewContainerTabBar.cpp:69
QPixmap
Konsole::ViewContainerTabBar::moveViewRequest
void moveViewRequest(int index, const QDropEvent *event, bool &success, TabbedViewContainer *sourceTabbedContainer)
ViewContainerTabBar.h
QDragLeaveEvent
QRect::width
int width() const
QDragEnterEvent
QPen::setWidth
void setWidth(int width)
QPixmap::grabWidget
QPixmap grabWidget(QWidget *widget, const QRect &rectangle)
QRect::topLeft
QPoint topLeft() const
ViewContainer.h
QPen
QWidget::show
void show()
Konsole::ViewContainerTabBar::querySourceIndex
void querySourceIndex(const QDropEvent *event, int &sourceIndex) const
QLabel
Konsole::ViewContainerTabBar::dropEvent
virtual void dropEvent(QDropEvent *event)
Definition: ViewContainerTabBar.cpp:83
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Konsole

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

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

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