File: pTabBar.cpp

package info (click to toggle)
monkeystudio 1.9.0.4%2Bgit20161218-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 41,500 kB
  • ctags: 22,118
  • sloc: cpp: 144,671; ansic: 33,969; python: 2,922; makefile: 127; sh: 122; php: 73; cs: 69
file content (492 lines) | stat: -rw-r--r-- 14,598 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/****************************************************************************
    Copyright (C) 2005 - 2011  Filipe AZEVEDO & The Monkey Studio Team
    http://monkeystudio.org licensing under the GNU GPL.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
****************************************************************************/
#include "pTabBar.h"

#include <QMouseEvent>
#include <QApplication>
#include <QIcon>
#include <QPainter>
#include <QAction>
#include <QMimeData>
#include <QDrag>

/*!
    \details Create a new pTabBar object
    \param parent The parent widget
*/
pTabBar::pTabBar( QWidget* parent )
    : QTabBar( parent )
{
    // set default colors
    mTabsColor = Qt::black;
    mCurrentTabColor = Qt::blue;
    
#ifdef Q_CC_MSVC
#pragma message("Deprecated uncomment code if needed this class")
#else
#warning Deprecated, uncomment code if needed this class
#endif

    /*
    // close button
    aToggleTabsHaveCloseButton = new pAction( "aTabbedTabsHaveShortcut", tr( "Tabs Have &Close Button" ), QKeySequence(), tr( "Tabbed Workspace" ) );
    aToggleTabsHaveCloseButton->setCheckable( true );
    
    // create actions for right tab
    aToggleTabsHaveShortcut = new pAction( "aTabbedTabsHaveShortcut", tr( "Tabs Have &Shortcut" ), QKeySequence(), tr( "Tabbed Workspace" ) );
    aToggleTabsHaveShortcut->setCheckable( true );
    
    // elid
    aToggleTabsElided = new pAction( "aTabbedTabsElided", tr( "Tabs Are &Elided" ), QKeySequence(), tr( "Tabbed Workspace" ) );
    aToggleTabsElided->setCheckable( true );
    */
    
    // for accepting drop
    setAcceptDrops( true );
    
    // set mouse tracking
    setMouseTracking( true );
    
    // update tab text color on current changed
    connect( this, SIGNAL( currentChanged( int ) ), this, SLOT( resetTabsColor() ) );
    connect( this, SIGNAL( tabsColorChanged( const QColor& ) ), this, SLOT( resetTabsColor() ) );
    connect( this, SIGNAL( currentTabColorChanged( const QColor& ) ), this, SLOT( resetTabsColor() ) );
    connect( this, SIGNAL( tabsHaveCloseButtonChanged( bool ) ), this, SLOT( update() ) );
#ifdef Q_CC_MSVC
#pragma message("Uncomment")
#else
#warning Uncomment
#endif
    /*
    connect( aToggleTabsHaveCloseButton, SIGNAL( toggled( bool ) ), this, SLOT( setTabsHaveCloseButton( bool ) ) );
    connect( aToggleTabsHaveShortcut, SIGNAL( toggled( bool ) ), this, SLOT( setTabsHaveShortcut( bool ) ) );
    connect( aToggleTabsElided, SIGNAL( toggled( bool ) ), this, SLOT( setTabsElided( bool ) ) );
    */
}

void pTabBar::paintEvent( QPaintEvent* event )
{
    // draw tabs
    QTabBar::paintEvent( event );
    
    // update button close
    if ( !aToggleTabsHaveCloseButton->isChecked() )
        return;
    
    // get tab
    int i = tabAt( mapFromGlobal( QCursor::pos() ) );
    if ( i != -1 )
    {
        // get close button rect
        QRect ir = iconRectForTab( i );
        
        // if mouse in close button rect
        if ( ir.contains( mapFromGlobal( QCursor::pos() ) ) )
        {
            // draw button
            QPainter p( this );
            p.drawPixmap( ir.topLeft(), QIcon( ":/file/icons/file/closeall.png" ).pixmap( iconSize(), QIcon::Active, isTabEnabled( i ) ? QIcon::On : QIcon::Off ) );
        }
    }
}

void pTabBar::mousePressEvent( QMouseEvent* event )
{
    // reset drag position
    dragStartPosition = QPoint();
    
    // get tab under cursor
    int i = tabAt( event->pos() );
    
    // if tab
    if ( i != -1 )
    {
        // emit left button pressed
        if ( event->button() == Qt::LeftButton )
            emit leftButtonPressed( i, event->globalPos() );
        
        // emit mid button pressed
        if ( event->button() == Qt::MidButton )
            emit midButtonPressed( i, event->globalPos() );
        
        // emit right button pressed and drag position
        if ( event->button() == Qt::RightButton )
        {
            emit rightButtonPressed( i, event->globalPos() );
            dragStartPosition = event->pos();
        }
    }
    
    // default event
    QTabBar::mousePressEvent( event );
}

void pTabBar::mouseReleaseEvent( QMouseEvent* event )
{
    // check button close clicked
    if ( aToggleTabsHaveCloseButton->isChecked() )
    {
        // get tab under cursor
        int i = tabAt( event->pos() );
        
        // if tab and left button and  tab icon pressed
        if ( i != -1 )
            if ( event->button() == Qt::LeftButton && inCloseButtonRect( i, event->pos() ) )
                emit closeButtonClicked( i );
    }
    
    // default event
    QTabBar::mouseReleaseEvent( event );
}

void pTabBar::mouseMoveEvent( QMouseEvent* event )
{
    if ( aToggleTabsHaveCloseButton->isChecked() )
    {
        // update icon state
        update();
    
        // change cursor if over button
        if ( inCloseButtonRect( tabAt( event->pos() ), event->pos() ) )
            setCursor( Qt::PointingHandCursor );
        else
            unsetCursor();
    }
    
    // need left button
    if ( event->buttons() != Qt::LeftButton )
        return;
    
    // need target tab and minimum drag distance
    if ( tabAt( event->pos() ) == -1 || ( event->pos() -dragStartPosition ).manhattanLength() < QApplication::startDragDistance() )
        return;

    // create mime
    QMimeData* m = new QMimeData;
    m->setData( "x-tabindex", QByteArray::number( tabAt( event->pos() ) ) );
    m->setData( "x-tabbar", QByteArray::number( reinterpret_cast<quintptr>( this ) ) );
    
    // create drag and set mime
    QDrag* d = new QDrag( this );
    d->setMimeData( m );
    
    // execute drag
    d->exec( Qt::MoveAction );
    
    // default event
    QTabBar::mouseMoveEvent( event );
}

void pTabBar::dragEnterEvent( QDragEnterEvent* event )
{
    // if correct mime and same tabbar
    if ( ( event->mimeData()->hasFormat( "x-tabindex" ) && event->mimeData()->hasFormat( "x-tabbar" ) 
        && reinterpret_cast<pTabBar*>( QVariant( event->mimeData()->data( "x-tabbar" ) ).value<quintptr>() ) == this 
        && tabAt( event->pos() ) != -1 ) || event->mimeData()->hasUrls() )
    {
        // accept drag
        event->acceptProposedAction();
    }
    
    // default event
    QTabBar::dragEnterEvent( event );
}

void pTabBar::dropEvent( QDropEvent* event )
{
    if ( !event->mimeData()->hasUrls() )
    {
        // get drop tab
        int ni = tabAt( event->pos() );
        
        // if get it
        if ( ni != -1 )
        {
            // get original tab infos
            int oi = event->mimeData()->data( "x-tabindex" ).toInt();
            QVariant otd = tabData( oi );
            QIcon oti = tabIcon( oi );
            QString ott = tabText( oi );
            QColor ottc = tabTextColor( oi );
            QString ottt = tabToolTip( oi );
            QString otwt = tabWhatsThis( oi );
            
            // remove original tab
            removeTab( oi );
            
            // insert new one with correct infos
            int i = insertTab( ni, oti, ott );
            setTabData( i, otd );
            setTabTextColor( i, ottc );
            setTabToolTip( i, ottt );
            setTabWhatsThis( i, otwt );
            
            //accept
            event->acceptProposedAction();
            
            // emit signal
            emit tabDropped( oi, i );
            
            // set new current index
            setCurrentIndex( i );
        }
    }
    else
        emit urlsDropped( event->mimeData()->urls () );
    
    // default event
    QTabBar::dropEvent( event );
}

void pTabBar::tabInserted( int i )
{
    // set chortcut if needed
    if ( tabsHaveShortcut() )
        updateTabsNumber( i );
}

void pTabBar::tabRemoved( int i )
{
    // set chortcut if needed
    if ( tabsHaveShortcut() )
        updateTabsNumber( i );
}

QRect pTabBar::iconRectForTab( int i )
{
    // get tab infos
    int x = 0, y = 0;
    QRect tr = tabRect( i );
    QSize sh = tr.size();
    
    switch ( shape() )
    {
        case QTabBar::RoundedNorth:
        case QTabBar::TriangularNorth:
            // calcul positions
            x = sh.width() -iconSize().width();
            y = ( sh.height() -iconSize().height() ) / 2;
            if ( currentIndex() == i )
            {
                y++;
                x -= 2;
            }
            else if ( currentIndex() != i )
            {
                y += 2;
                x--;
            }
            break;
        case QTabBar::RoundedSouth:
        case QTabBar::TriangularSouth:
            // calcul positions
            x = sh.width() -iconSize().width();
            y = ( sh.height() -iconSize().height() ) / 2;
            if ( currentIndex() == i )
                x -= 2;
            else if ( currentIndex() != i )
                x--;
            break;
        case QTabBar::RoundedWest:
        case QTabBar::TriangularWest:
            // calcul positions
            x = ( sh.width() -iconSize().height() ) / 2;
            if ( currentIndex() == i )
            {
                x++;
                y += 2;
            }
            else if ( currentIndex() != i )
            {
                x += 2;
                y += 2;
            }
            break;
        case QTabBar::RoundedEast:
        case QTabBar::TriangularEast:
            // calcul positions
            x = ( sh.width() -iconSize().height() ) / 2;
            y = sh.height() -iconSize().width();
            if ( currentIndex() == i )
            {
                x++;
                y -= 2;
            }
            else if ( currentIndex() != i )
            {
                x--;
                y -= 2;
            }
            break;
    }
    
    // return icon rect
    return QRect( tr.topLeft() +QPoint( x, y ), iconSize() );
}

bool pTabBar::inCloseButtonRect( int i, const QPoint& p )
{ return iconRectForTab( i ).contains( p ); }

/*!
    \details Redo the tabs color
*/
void pTabBar::resetTabsColor()
{
    for ( int i = 0; i < count(); i++ )
        setTabTextColor( i, i == currentIndex() ? currentTabColor() : tabsColor() );
}

/*!
    \details Return the tabs text color
*/
QColor pTabBar::tabsColor() const
{ return mTabsColor; }

/*!
    \details Set the tabs text color
    \param color The text color
*/
void pTabBar::setTabsColor( const QColor& color )
{
    if ( mTabsColor == color )
        return;
    mTabsColor = color;
    emit tabsColorChanged( mTabsColor );
}

/*!
    \details Return the current tab text color
*/
QColor pTabBar::currentTabColor() const
{ return mCurrentTabColor; }

/*!
    \details Set the current tab text color
    \param color The text color
*/
void pTabBar::setCurrentTabColor( const QColor& color )
{
    if ( mCurrentTabColor == color )
        return;
    mCurrentTabColor = color;
    emit currentTabColorChanged( mCurrentTabColor );
}

/*!
    \details Return true if tabs have a small close button else false
*/
bool pTabBar::tabsHaveCloseButton() const
{ return aToggleTabsHaveCloseButton->isChecked(); }

/*!
    \details Set if tabs have or not a small close button
    \param buttons True for buttons, else false
*/
void pTabBar::setTabsHaveCloseButton( bool buttons )
{
    if ( aToggleTabsHaveCloseButton->isChecked() == buttons && sender() != aToggleTabsHaveCloseButton )
        return;
    aToggleTabsHaveCloseButton->setChecked( buttons );
    setTabText( 0, tabText( 0 ) ); // workaround for tabs update
    emit tabsHaveCloseButtonChanged( aToggleTabsHaveCloseButton->isChecked() );
}

/*!
    \details Return true if tabs have shortcut else false
*/
bool pTabBar::tabsHaveShortcut() const
{ return aToggleTabsHaveShortcut->isChecked(); }

/*!
    \details Set if tabs have or not shortcut
    \param shortcuts True for shortcuts, else false
*/
void pTabBar::setTabsHaveShortcut( bool shortcuts )
{
    if ( aToggleTabsHaveShortcut->isChecked() == shortcuts && sender() != aToggleTabsHaveShortcut )
        return;
    aToggleTabsHaveShortcut->setChecked( shortcuts );
    updateTabsNumber();
    emit tabsHaveShortcutChanged( aToggleTabsHaveShortcut->isChecked() );
}

/*!
    \details Return true if tabs text is elided else false
*/
bool pTabBar::tabsElided() const
{ return  aToggleTabsElided->isChecked(); }

/*!
    \details Set if tabs text is elided or not
    \param elided True for elided text, else false
*/
void pTabBar::setTabsElided( bool elided )
{
    if ( aToggleTabsElided->isChecked() == elided && sender() != aToggleTabsElided )
        return;
    aToggleTabsElided->setChecked( elided );
    setElideMode( elided ? Qt::ElideMiddle : Qt::ElideNone );
    setTabText( 0, tabText( 0 ) ); // workaround for tabs update
    emit tabsElidedChanged( aToggleTabsElided->isChecked() );
}

void pTabBar::updateTabsNumber( int i )
{
    // fill i if i = -1 for complete update
    if ( i == -1 )
        i = 0;

    // loop documents starting at id i
    for ( int j = i; j < count(); j++ )
    {
        // only 10 tabs can have shortcut
        if ( j > 9 )
            return;

        // got tab text
        QString s = tabText( j );

        // look index of cut part
        int k = s.indexOf( ":" );

        // set new tab caption
        if ( tabsHaveShortcut() )
            setTabText( j, QString( "&%1: %2" ).arg( j ).arg( s.mid( k != -1 ? k +2 : 0 ) ) );
        else
            setTabText( j, s.mid( k != -1 ? k +2 : 0 ) );
    }
}

/*!
    \details Return a togglable QAction for managing the "Have close button" property
*/
QAction* pTabBar::toggleTabsHaveCloseButtonAction() const
{ return aToggleTabsHaveCloseButton; }

/*!
    \details Return a togglable QAction for managing the "Have shortcut" property
*/
QAction* pTabBar::toggleTabsHaveShortcutAction() const
{ return aToggleTabsHaveShortcut; }

/*!
    \details Return a togglable QAction for managing the "Text elided" property
*/
QAction* pTabBar::toggleTabsElidedAction() const
{ return aToggleTabsElided; }