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

KDEUI

  • sources
  • kde-4.12
  • kdelibs
  • kdeui
  • widgets
ktabbar.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2003 Stephan Binner <binner@kde.org>
3  Copyright (C) 2003 Zack Rusin <zack@kde.org>
4  Copyright (C) 2009 Urs Wolfer <uwolfer @ kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library 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 GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "ktabbar.h"
23 
24 #include <QtCore/QTimer>
25 #include <QtGui/QApplication>
26 #include <QtGui/QCursor>
27 #include <QtGui/QMouseEvent>
28 
29 #include <kglobalsettings.h>
30 
31 class KTabBar::Private
32 {
33  public:
34  Private()
35  : mReorderStartTab( -1 ),
36  mReorderPreviousTab( -1 ),
37  mDragSwitchTab( -1 ),
38  mActivateDragSwitchTabTimer( 0 ),
39  mTabReorderingEnabled( false ),
40  mMiddleMouseTabMoveInProgress( false)
41  {
42  }
43 
44  QPoint mDragStart;
45  int mReorderStartTab;
46  int mReorderPreviousTab;
47  int mDragSwitchTab;
48  QTimer *mActivateDragSwitchTabTimer;
49 
50  bool mTabReorderingEnabled : 1;
51  bool mMiddleMouseTabMoveInProgress : 1;
52 
53 };
54 
55 KTabBar::KTabBar( QWidget *parent )
56  : QTabBar( parent ),
57  d( new Private )
58 {
59  setAcceptDrops( true );
60  setMouseTracking( true );
61 
62  d->mActivateDragSwitchTabTimer = new QTimer( this );
63  d->mActivateDragSwitchTabTimer->setSingleShot( true );
64  connect( d->mActivateDragSwitchTabTimer, SIGNAL(timeout()), SLOT(activateDragSwitchTab()) );
65 #ifndef KDE_NO_DEPRECATED
66  connect( this, SIGNAL(tabCloseRequested(int)), this, SIGNAL(closeRequest(int))); // just for backward compatibility, KDE5 remove
67 #endif
68 
69  //connect( this, SIGNAL(layoutChanged()), SLOT(onLayoutChange()) );
70 }
71 
72 KTabBar::~KTabBar()
73 {
74  delete d;
75 }
76 
77 void KTabBar::mouseDoubleClickEvent( QMouseEvent *event )
78 {
79  if ( event->button() != Qt::LeftButton )
80  return;
81 
82  int tab = selectTab( event->pos() );
83 
84  if(tab == -1) {
85  emit newTabRequest();
86  } else {
87 #ifndef KDE_NO_DEPRECATED
88  emit mouseDoubleClick( tab ); //deprecated
89 #endif
90  emit tabDoubleClicked( tab );
91  }
92 
93  QTabBar::mouseDoubleClickEvent( event );
94 }
95 
96 void KTabBar::mousePressEvent( QMouseEvent *event )
97 {
98  if ( event->button() == Qt::LeftButton ) {
99  d->mDragStart = event->pos();
100  } else if( event->button() == Qt::RightButton ) {
101  int tab = selectTab( event->pos() );
102  if ( tab != -1 ) {
103  emit contextMenu( tab, mapToGlobal( event->pos() ) );
104  } else {
105  emit emptyAreaContextMenu( mapToGlobal( event->pos() ) );
106  }
107  return;
108  } else if (QTabBar::isMovable() && event->button() == Qt::MidButton) {
109  // compatibility feature for old middle mouse tab moving
110  event->accept();
111  QMouseEvent fakedMouseEvent(event->type(), event->pos(), Qt::LeftButton, Qt::LeftButton, event->modifiers());
112  QCoreApplication::sendEvent(this, &fakedMouseEvent);
113  }
114 
115  QTabBar::mousePressEvent( event );
116 }
117 
118 void KTabBar::mouseMoveEvent( QMouseEvent *event )
119 {
120  if ( event->buttons() == Qt::LeftButton && !isMovable() ) {
121  int tab = selectTab( event->pos() );
122  if ( d->mDragSwitchTab && tab != d->mDragSwitchTab ) {
123  d->mActivateDragSwitchTabTimer->stop();
124  d->mDragSwitchTab = 0;
125  }
126 
127  int delay = KGlobalSettings::dndEventDelay();
128  QPoint newPos = event->pos();
129  if ( newPos.x() > d->mDragStart.x() + delay || newPos.x() < d->mDragStart.x() - delay ||
130  newPos.y() > d->mDragStart.y() + delay || newPos.y() < d->mDragStart.y() - delay ) {
131  if ( tab != -1 ) {
132  emit initiateDrag( tab );
133  return;
134  }
135  }
136  } else if ( event->buttons() == Qt::MidButton && !isMovable() ) {
137  if ( d->mReorderStartTab == -1 ) {
138  int delay = KGlobalSettings::dndEventDelay();
139  QPoint newPos = event->pos();
140 
141  if ( newPos.x() > d->mDragStart.x() + delay || newPos.x() < d->mDragStart.x() - delay ||
142  newPos.y() > d->mDragStart.y() + delay || newPos.y() < d->mDragStart.y() - delay ) {
143  int tab = selectTab( event->pos() );
144  if ( tab != -1 && d->mTabReorderingEnabled ) {
145  d->mReorderStartTab = tab;
146  grabMouse( Qt::SizeAllCursor );
147  return;
148  }
149  }
150  } else {
151  int tab = selectTab( event->pos() );
152  if ( tab != -1 ) {
153  int reorderStopTab = tab;
154  if ( d->mReorderStartTab != reorderStopTab && d->mReorderPreviousTab != reorderStopTab ) {
155  emit moveTab( d->mReorderStartTab, reorderStopTab );
156 
157  d->mReorderPreviousTab = d->mReorderStartTab;
158  d->mReorderStartTab = reorderStopTab;
159 
160  return;
161  }
162  }
163  }
164  } else if ( event->button() == Qt::NoButton && event->buttons() == Qt::MidButton && isMovable() ) {
165  // compatibility feature for old middle mouse tab moving
166  d->mMiddleMouseTabMoveInProgress = true;
167  event->accept();
168  QMouseEvent fakedMouseEvent(event->type(), event->pos(), event->button(), Qt::LeftButton, event->modifiers());
169  QCoreApplication::sendEvent(this, &fakedMouseEvent);
170  return;
171  }
172 
173  QTabBar::mouseMoveEvent( event );
174 }
175 
176 
177 #ifndef KDE_NO_DEPRECATED
178 void KTabBar::closeButtonClicked()
179 {
180  // deprecated
181 }
182 #endif
183 
184 
185 #ifndef KDE_NO_DEPRECATED
186 void KTabBar::enableCloseButton()
187 {
188  // deprecated
189 }
190 #endif
191 
192 
193 void KTabBar::activateDragSwitchTab()
194 {
195  int tab = selectTab( mapFromGlobal( QCursor::pos() ) );
196  if ( tab != -1 && d->mDragSwitchTab == tab )
197  setCurrentIndex( d->mDragSwitchTab );
198 
199  d->mDragSwitchTab = 0;
200 }
201 
202 void KTabBar::mouseReleaseEvent( QMouseEvent *event )
203 {
204  switch ( event->button() ) {
205  case Qt::LeftButton:
206  break;
207 
208  case Qt::MidButton:
209  if (d->mMiddleMouseTabMoveInProgress && QTabBar::isMovable()) {
210  // compatibility feature for old middle mouse tab moving
211  d->mMiddleMouseTabMoveInProgress = false;
212  event->accept();
213  QMouseEvent fakedMouseEvent(event->type(), event->pos(), Qt::LeftButton, Qt::LeftButton, event->modifiers());
214  QCoreApplication::sendEvent(this, &fakedMouseEvent);
215  return;
216  }
217  if ( d->mReorderStartTab == -1 ) {
218  int tab = selectTab( event->pos() );
219  if ( tab != -1 ) {
220  event->accept();
221  if (QTabBar::isMovable()) {
222  QMouseEvent fakedMouseEvent(event->type(), event->pos(), Qt::LeftButton, Qt::LeftButton, event->modifiers());
223  QCoreApplication::sendEvent(this, &fakedMouseEvent);
224  }
225  emit mouseMiddleClick( tab );
226  return;
227  }
228  } else {
229  releaseMouse();
230  setCursor( Qt::ArrowCursor );
231  d->mReorderStartTab = -1;
232  d->mReorderPreviousTab = -1;
233  }
234  break;
235 
236  default:
237  break;
238  }
239 
240  QTabBar::mouseReleaseEvent( event );
241 }
242 
243 void KTabBar::dragEnterEvent( QDragEnterEvent *event )
244 {
245  int tab = selectTab( event->pos() );
246  if ( tab != -1 ) {
247  bool accept = false;
248  // The receivers of the testCanDecode() signal has to adjust
249  // 'accept' accordingly.
250  emit testCanDecode( event, accept );
251  if ( accept && tab != currentIndex() ) {
252  d->mDragSwitchTab = tab;
253  d->mActivateDragSwitchTabTimer->start( QApplication::doubleClickInterval() * 2 );
254  }
255 
256  event->setAccepted( accept );
257  return;
258  }
259 
260  QTabBar::dragEnterEvent( event );
261 }
262 
263 void KTabBar::dragMoveEvent( QDragMoveEvent *event )
264 {
265  int tab = selectTab( event->pos() );
266  if ( tab != -1 ) {
267  bool accept = false;
268  // The receivers of the testCanDecode() signal has to adjust
269  // 'accept' accordingly.
270  emit testCanDecode( event, accept );
271  if ( accept && tab != currentIndex() ) {
272  d->mDragSwitchTab = tab;
273  d->mActivateDragSwitchTabTimer->start( QApplication::doubleClickInterval() * 2 );
274  }
275 
276  event->setAccepted( accept );
277  return;
278  }
279 
280  QTabBar::dragMoveEvent( event );
281 }
282 
283 void KTabBar::dropEvent( QDropEvent *event )
284 {
285  int tab = selectTab( event->pos() );
286  if ( tab != -1 ) {
287  d->mActivateDragSwitchTabTimer->stop();
288  d->mDragSwitchTab = 0;
289  emit receivedDropEvent( tab , event );
290  return;
291  }
292 
293  QTabBar::dropEvent( event );
294 }
295 
296 void KTabBar::paintEvent( QPaintEvent *event )
297 {
298  QTabBar::paintEvent( event );
299 }
300 
301 void KTabBar::leaveEvent( QEvent *event )
302 {
303  QTabBar::leaveEvent( event );
304 }
305 
306 QSize KTabBar::tabSizeHint( int index ) const
307 {
308  QSize size = QTabBar::tabSizeHint( index );
309 
310  return size;
311 }
312 
313 #ifndef QT_NO_WHEELEVENT
314 void KTabBar::wheelEvent( QWheelEvent *event )
315 {
316  if ( !( event->orientation() == Qt::Horizontal ) ) {
317  if ( receivers( SIGNAL(wheelDelta(int)) ) ) {
318  emit( wheelDelta( event->delta() ) );
319  return;
320  }
321  int lastIndex = count() - 1;
322  //Set an invalid index as base case
323  int targetIndex = -1;
324  bool forward = event->delta() < 0;
325  if ( forward && lastIndex == currentIndex() ) {
326  targetIndex = 0;
327  }
328  else if ( !forward && 0 == currentIndex() ) {
329  targetIndex = lastIndex;
330  }
331  //Will not move when targetIndex is invalid
332  setCurrentIndex( targetIndex );
333  //If it has not moved yet (targetIndex == -1), or if it moved but current tab is disabled
334  if ( targetIndex != currentIndex() || !isTabEnabled( targetIndex ) ) {
335  QTabBar::wheelEvent( event );
336  }
337  event->accept();
338  } else {
339  event->ignore();
340  }
341 }
342 #endif
343 
344 #ifndef KDE_NO_DEPRECATED
345 bool KTabBar::isTabReorderingEnabled() const
346 {
347  return d->mTabReorderingEnabled;
348 }
349 #endif
350 
351 #ifndef KDE_NO_DEPRECATED
352 void KTabBar::setTabReorderingEnabled( bool on )
353 {
354  d->mTabReorderingEnabled = on;
355 }
356 #endif
357 
358 #ifndef KDE_NO_DEPRECATED
359 bool KTabBar::tabCloseActivatePrevious() const
360 {
361  return selectionBehaviorOnRemove() == QTabBar::SelectPreviousTab;
362 }
363 #endif
364 
365 #ifndef KDE_NO_DEPRECATED
366 void KTabBar::setTabCloseActivatePrevious( bool on )
367 {
368  setSelectionBehaviorOnRemove(on ? QTabBar::SelectPreviousTab : QTabBar::SelectRightTab);
369 }
370 #endif
371 
372 
373 #ifndef KDE_NO_DEPRECATED
374 void KTabBar::setHoverCloseButton( bool button )
375 {
376  // deprecated
377  setTabsClosable(button);
378 }
379 #endif
380 
381 #ifndef KDE_NO_DEPRECATED
382 bool KTabBar::hoverCloseButton() const
383 {
384  // deprecated
385  return tabsClosable();
386 }
387 #endif
388 
389 #ifndef KDE_NO_DEPRECATED
390 void KTabBar::setHoverCloseButtonDelayed( bool delayed )
391 {
392  // deprecated
393  Q_UNUSED( delayed );
394 }
395 #endif
396 
397 #ifndef KDE_NO_DEPRECATED
398 bool KTabBar::hoverCloseButtonDelayed() const
399 {
400  // deprecated
401  return false;
402 }
403 #endif
404 
405 #ifndef KDE_NO_DEPRECATED
406 void KTabBar::setCloseButtonEnabled( bool enable )
407 {
408  QTabBar::setTabsClosable(enable);
409 }
410 #endif
411 
412 #ifndef KDE_NO_DEPRECATED
413 bool KTabBar::isCloseButtonEnabled() const
414 {
415  return QTabBar::tabsClosable();
416 }
417 #endif
418 
419 void KTabBar::tabLayoutChange()
420 {
421  d->mActivateDragSwitchTabTimer->stop();
422  d->mDragSwitchTab = 0;
423 }
424 
425 int KTabBar::selectTab( const QPoint &pos ) const
426 {
427  const int tabCount = count();
428  for ( int i = 0; i < tabCount; ++i )
429  if ( tabRect( i ).contains( pos ) )
430  return i;
431 
432  return -1;
433 }
434 
435 QPoint KTabBar::closeButtonPos( int tabIndex ) const
436 {
437  Q_UNUSED(tabIndex);
438  return QPoint();
439 }
440 
441 QRect KTabBar::closeButtonRect( int tabIndex ) const
442 {
443  Q_UNUSED(tabIndex);
444  return QRect();
445 }
446 
447 #include "ktabbar.moc"
KTabBar::setHoverCloseButtonDelayed
void setHoverCloseButtonDelayed(bool)
If enabled, the close button cannot get clicked until a minor delay has been passed.
Definition: ktabbar.cpp:390
KTabBar::activateDragSwitchTab
virtual void activateDragSwitchTab()
Definition: ktabbar.cpp:193
KTabBar::testCanDecode
void testCanDecode(const QDragMoveEvent *, bool &)
KTabBar::tabSizeHint
virtual QSize tabSizeHint(int index) const
Definition: ktabbar.cpp:306
ktabbar.h
KTabBar::closeButtonClicked
QT_MOC_COMPAT void closeButtonClicked()
Definition: ktabbar.cpp:178
kglobalsettings.h
timeout
int timeout
KTabBar::isCloseButtonEnabled
bool isCloseButtonEnabled() const
Returns true if the close button is shown on tabs.
Definition: ktabbar.cpp:413
KTabBar::mouseReleaseEvent
virtual void mouseReleaseEvent(QMouseEvent *event)
Definition: ktabbar.cpp:202
QWidget
KTabBar::initiateDrag
void initiateDrag(int)
QTabBar
KTabBar::hoverCloseButton
bool hoverCloseButton() const
Definition: ktabbar.cpp:382
KTabBar::paintEvent
virtual void paintEvent(QPaintEvent *event)
Definition: ktabbar.cpp:296
KTabBar::mouseMiddleClick
void mouseMiddleClick(int index)
A double middle mouse button click was performed over the tab with the.
KTabBar::contextMenu
void contextMenu(int index, const QPoint &globalPos)
A right mouse button click was performed over the tab with the.
KTabBar::selectTab
int selectTab(const QPoint &position) const
Selects the tab which has a tab header at given.
Definition: ktabbar.cpp:425
KTabBar::KTabBar
KTabBar(QWidget *parent=0)
Creates a new tab bar.
Definition: ktabbar.cpp:55
KTabBar::setCloseButtonEnabled
void setCloseButtonEnabled(bool)
If enabled, a close button is available for each tab.
Definition: ktabbar.cpp:406
KTabBar::setTabReorderingEnabled
void setTabReorderingEnabled(bool enable)
Sets the tab reordering enabled or disabled.
Definition: ktabbar.cpp:352
KTabBar::dragEnterEvent
virtual void dragEnterEvent(QDragEnterEvent *event)
Definition: ktabbar.cpp:243
KTabBar::enableCloseButton
QT_MOC_COMPAT void enableCloseButton()
Definition: ktabbar.cpp:186
KTabBar::leaveEvent
virtual void leaveEvent(QEvent *event)
Definition: ktabbar.cpp:301
KTabBar::tabCloseActivatePrevious
bool tabCloseActivatePrevious() const
Returns whether the 'activate previous tab on close' feature is enabled.
Definition: ktabbar.cpp:359
KTabBar::moveTab
void moveTab(int, int)
Used internally by KTabBar's/KTabWidget's middle-click tab moving mechanism.
KTabBar::isTabReorderingEnabled
bool isTabReorderingEnabled() const
Returns whether tab reordering is enabled.
Definition: ktabbar.cpp:345
KTabBar::mouseMoveEvent
virtual void mouseMoveEvent(QMouseEvent *event)
Definition: ktabbar.cpp:118
KTabBar::closeRequest
QT_MOC_COMPAT void closeRequest(int)
KStandardAction::forward
KAction * forward(const QObject *recvr, const char *slot, QObject *parent)
Move forward (web style menu).
Definition: kstandardaction.cpp:399
KTabBar::hoverCloseButtonDelayed
bool hoverCloseButtonDelayed() const
Definition: ktabbar.cpp:398
KTabBar::tabLayoutChange
virtual void tabLayoutChange()
Definition: ktabbar.cpp:419
KTabBar::wheelEvent
virtual void wheelEvent(QWheelEvent *event)
Definition: ktabbar.cpp:314
KTabBar::mouseDoubleClick
QT_MOC_COMPAT void mouseDoubleClick(int)
QPoint
QRect
KTabBar::~KTabBar
virtual ~KTabBar()
Destroys the tab bar.
Definition: ktabbar.cpp:72
KTabBar::dropEvent
virtual void dropEvent(QDropEvent *event)
Definition: ktabbar.cpp:283
KTabBar::tabDoubleClicked
void tabDoubleClicked(int index)
A double left mouse button click was performed over the tab with the.
KTabBar::mousePressEvent
virtual void mousePressEvent(QMouseEvent *event)
Definition: ktabbar.cpp:96
QSize
KTabBar::dragMoveEvent
virtual void dragMoveEvent(QDragMoveEvent *event)
Definition: ktabbar.cpp:263
KTabBar::emptyAreaContextMenu
void emptyAreaContextMenu(const QPoint &globalPos)
A right mouse button click was performed over the empty area on the tab bar.
KTabBar::mouseDoubleClickEvent
virtual void mouseDoubleClickEvent(QMouseEvent *event)
Definition: ktabbar.cpp:77
KTabBar::setHoverCloseButton
void setHoverCloseButton(bool)
If enabled, a close button is shown above the tab icon.
Definition: ktabbar.cpp:374
KTabBar::setTabCloseActivatePrevious
void setTabCloseActivatePrevious(bool)
Sets the 'activate previous tab on close' feature enabled or disabled.
Definition: ktabbar.cpp:366
KGlobalSettings::dndEventDelay
static int dndEventDelay()
Returns a threshold in pixels for drag & drop operations.
Definition: kglobalsettings.cpp:227
KTabBar::receivedDropEvent
void receivedDropEvent(int, QDropEvent *)
KTabBar::newTabRequest
void newTabRequest()
A double left mouse button click was performed over the empty area on the tab bar.
KTabBar::wheelDelta
void wheelDelta(int)
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:15 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

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

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

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