ATLAS Offline Software
Loading...
Searching...
No Matches
VP1TabBar.cxx
Go to the documentation of this file.
1
2// //
3// File adopted from KDE4 libraries by //
4// T. Kittelmann <Thomas.Kittelmann@cern.ch>, March 2007. //
5// //
6// Main thing is to remove dependence on KDE for length of title text //
7// settings, delay on drag settings and title eliding. Also, //
8// hoverbuttons were removed (since these had not been properly //
9// implemented in KDE4 at the time the code was copied). //
10// //
11// Notice about Copyrights and GPL license from the orignal file is //
12// left untouched below. //
13// //
15
16/* This file is part of the KDE libraries
17 Copyright (C) 2003 Stephan Binner <binner@kde.org>
18 Copyright (C) 2003 Zack Rusin <zack@kde.org>
19
20 This library is free software; you can redistribute it and/or
21 modify it under the terms of the GNU Library General Public
22 License as published by the Free Software Foundation; either
23 version 2 of the License, or (at your option) any later version.
24
25 This library is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 Library General Public License for more details.
29
30 You should have received a copy of the GNU Library General Public License
31 along with this library; see the file COPYING.LIB. If not, write to
32 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
33 Boston, MA 02110-1301, USA.
34*/
35
36#include <QTimer>
37#include <QApplication>
38#include <QMouseEvent>
39
40#include "VP1Base/VP1TabBar.h"
41
65
66VP1TabBar::VP1TabBar( QWidget *parent )
67 : QTabBar( parent ),
68 m_d( new Private )
69{
70 setAcceptDrops( true );
71 setMouseTracking( true );
72
73 //m_d->mEnableCloseButtonTimer = new QTimer( this );
74 //connect( m_d->mEnableCloseButtonTimer, SIGNAL( timeout() ), SLOT( enableCloseButton() ) );
75
76 m_d->mActivateDragSwitchTabTimer = new QTimer( this );
77 m_d->mActivateDragSwitchTabTimer->setSingleShot( true );
78 connect( m_d->mActivateDragSwitchTabTimer, &QTimer::timeout , this, &VP1TabBar::activateDragSwitchTab );
79
80 //connect( this, SIGNAL( layoutChanged() ), SLOT( onLayoutChange() ) );
81}
82
84{
85 delete m_d;
86}
87
88void VP1TabBar::mouseDoubleClickEvent( QMouseEvent *event )
89{
90 if ( event->button() != Qt::LeftButton )
91 return;
92
93 int tab = selectTab( event->pos() );
94 if ( tab != -1 ) {
95 emit mouseDoubleClick( tab );
96 return;
97 }
98
99 QTabBar::mouseDoubleClickEvent( event );
100}
101
102void VP1TabBar::mousePressEvent( QMouseEvent *event )
103{
104 if ( event->button() == Qt::LeftButton ) {
105 //m_d->mEnableCloseButtonTimer->stop();
106 m_d->mDragStart = event->pos();
107 } else if( event->button() == Qt::RightButton ) {
108 int tab = selectTab( event->pos() );
109 if ( tab != -1 ) {
110 emit contextMenu( tab, mapToGlobal( event->pos() ) );
111 return;
112 }
113 }
114
115 QTabBar::mousePressEvent( event );
116}
117
118void VP1TabBar::mouseMoveEvent( QMouseEvent *event )
119{
120 if ( event->buttons() == Qt::LeftButton ) {
121 int tab = selectTab( event->pos() );
122 if ( m_d->mDragSwitchTab && tab != m_d->mDragSwitchTab ) {
123 m_d->mActivateDragSwitchTabTimer->stop();
124 m_d->mDragSwitchTab = 0;
125 }
126
127 int delay = 5;//TK fixme KGlobalSettings::dndEventDelay();
128 QPoint newPos = event->pos();
129 if ( newPos.x() > m_d->mDragStart.x() + delay || newPos.x() < m_d->mDragStart.x() - delay ||
130 newPos.y() > m_d->mDragStart.y() + delay || newPos.y() < m_d->mDragStart.y() - delay ) {
131 if ( tab != -1 ) {
132 emit initiateDrag( tab );
133 return;
134 }
135 }
136 } else if ( event->buttons() == Qt::MiddleButton ) {
137 if ( m_d->mReorderStartTab == -1 ) {
138 int delay = 5;//TK fixme KGlobalSettings::dndEventDelay();
139 QPoint newPos = event->pos();
140
141 if ( newPos.x() > m_d->mDragStart.x() + delay || newPos.x() < m_d->mDragStart.x() - delay ||
142 newPos.y() > m_d->mDragStart.y() + delay || newPos.y() < m_d->mDragStart.y() - delay ) {
143 int tab = selectTab( event->pos() );
144 if ( tab != -1 && m_d->mTabReorderingEnabled ) {
145 m_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 ( m_d->mReorderStartTab != reorderStopTab && m_d->mReorderPreviousTab != reorderStopTab ) {
155 emit moveTab( m_d->mReorderStartTab, reorderStopTab );
156
157 m_d->mReorderPreviousTab = m_d->mReorderStartTab;
158 m_d->mReorderStartTab = reorderStopTab;
159
160 return;
161 }
162 }
163 }
164 }
165
166 QTabBar::mouseMoveEvent( event );
167}
168
169
171{
172 int tab = selectTab( mapFromGlobal( QCursor::pos() ) );
173 if ( tab != -1 && m_d->mDragSwitchTab == tab )
174 setCurrentIndex( m_d->mDragSwitchTab );
175
176 m_d->mDragSwitchTab = 0;
177}
178
179void VP1TabBar::mouseReleaseEvent( QMouseEvent *event )
180{
181 if ( event->button() == Qt::MiddleButton ) {
182 if ( m_d->mReorderStartTab == -1 ) {
183 int tab = selectTab( event->pos() );
184 if ( tab != -1 ) {
185 emit mouseMiddleClick( tab );
186 return;
187 }
188 } else {
189 releaseMouse();
190 setCursor( Qt::ArrowCursor );
191 m_d->mReorderStartTab = -1;
192 m_d->mReorderPreviousTab = -1;
193 }
194 }
195
196 QTabBar::mouseReleaseEvent( event );
197}
198
199void VP1TabBar::dragEnterEvent( QDragEnterEvent *event )
200{
201 event->setAccepted( true );
202 QTabBar::dragEnterEvent( event );
203}
204
205void VP1TabBar::dragMoveEvent( QDragMoveEvent *event )
206{
207 int tab = selectTab( event->pos() );
208 if ( tab != -1 ) {
209 bool accept = false;
210 // The receivers of the testCanDecode() signal has to adjust
211 // 'accept' accordingly.
212 emit testCanDecode( event, accept );
213 if ( accept && tab != currentIndex() ) {
214 m_d->mDragSwitchTab = tab;
215 m_d->mActivateDragSwitchTabTimer->start( QApplication::doubleClickInterval() * 2 );
216 }
217
218 event->setAccepted( accept );
219 return;
220 }
221
222 event->setAccepted( false );
223 QTabBar::dragMoveEvent( event );
224}
225
226void VP1TabBar::dropEvent( QDropEvent *event )
227{
228 int tab = selectTab( event->pos() );
229 if ( tab != -1 ) {
230 m_d->mActivateDragSwitchTabTimer->stop();
231 m_d->mDragSwitchTab = 0;
232 emit receivedDropEvent( tab , event );
233 return;
234 }
235
236 QTabBar::dropEvent( event );
237}
238
239#ifndef QT_NO_WHEELEVENT
240void VP1TabBar::wheelEvent( QWheelEvent *event )
241{
242 emit( wheelDelta( event->angleDelta().y() ) );
243}
244#endif
245
247{
248 return m_d->mTabReorderingEnabled;
249}
250
252{
253 m_d->mTabReorderingEnabled = on;
254}
255
257{
258 return m_d->mTabCloseActivatePrevious;
259}
260
262{
263 m_d->mTabCloseActivatePrevious = on;
264}
265
266
268{
269 m_d->mActivateDragSwitchTabTimer->stop();
270 m_d->mDragSwitchTab = 0;
271}
272
273int VP1TabBar::selectTab( const QPoint &pos ) const
274{
275 for ( int i = 0; i < count(); ++i )
276 if ( tabRect( i ).contains( pos ) )
277 return i;
278
279 return -1;
280}
281
double delay(std::size_t d)
bool mTabCloseActivatePrevious
Definition VP1TabBar.cxx:62
QTimer * mActivateDragSwitchTabTimer
Definition VP1TabBar.cxx:59
void mouseMiddleClick(int)
virtual void tabLayoutChange()
virtual void dragMoveEvent(QDragMoveEvent *event)
bool isTabReorderingEnabled() const
void mouseDoubleClick(int)
virtual void mouseReleaseEvent(QMouseEvent *event)
virtual void dropEvent(QDropEvent *event)
virtual void mouseMoveEvent(QMouseEvent *event)
void testCanDecode(const QDragMoveEvent *, bool &)
void setTabCloseActivatePrevious(bool)
virtual void mousePressEvent(QMouseEvent *event)
virtual void activateDragSwitchTab()
Private *const m_d
Definition VP1TabBar.h:94
virtual void dragEnterEvent(QDragEnterEvent *event)
virtual ~VP1TabBar()
Definition VP1TabBar.cxx:83
void moveTab(int, int)
bool tabCloseActivatePrevious() const
virtual void mouseDoubleClickEvent(QMouseEvent *event)
Definition VP1TabBar.cxx:88
virtual void wheelEvent(QWheelEvent *event)
void wheelDelta(int)
void contextMenu(int, const QPoint &)
void initiateDrag(int)
void receivedDropEvent(int, QDropEvent *)
VP1TabBar(QWidget *parent=0)
Definition VP1TabBar.cxx:66
void setTabReorderingEnabled(bool enable)
int selectTab(const QPoint &position) const
bool contains(const std::string &s, const std::string &regx)
does a string contain the substring
Definition hcg.cxx:114
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146