File: pDockToolBar.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 (595 lines) | stat: -rw-r--r-- 16,515 bytes parent folder | download | duplicates (3)
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/****************************************************************************
    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 "pDockToolBar.h"
#include "pDockToolBarManager.h"
#include "pTabbedWorkspaceCornerButton.h"

#include <QButtonGroup>
#include <QFrame>
#include <QBoxLayout>
#include <QEvent>
#include <QDockWidget>
#include <QAction>
#include <QMainWindow>
#include <QKeyEvent>

/*!
    \details Create a new object
    \param manager The pDockToolBarManager manager
    \param orientation The toolbar orientation
    \param window The main window
*/
pDockToolBar::pDockToolBar( pDockToolBarManager* manager, Qt::Orientation orientation, QMainWindow* window )
    : QToolBar( window ), mManager( manager ), mUniqueId( 0 )
{
    // need docktoolbar manager
    Q_ASSERT( manager != 0 );
    Q_UNUSED( manager );
    
    toggleViewAction()->setEnabled( false );
    toggleViewAction()->setVisible( false );
    
    // always show button text
    mTextAlwaysVisible = true;

    // toolbar is not movable
    setMovable( false );
    
    // toggle exclusive action
    aToggleExclusive = new QAction( this );
    aToggleExclusive->setCheckable( true );
    aToggleExclusive->setChecked( true );

    // change font
    QFont f( font() );
    f.setPixelSize( 11 );
    setFont( f );

    // set icon size
    setIconSize( QSize( 16, 16 ) );

    // create button frame
    mFrame = new QFrame;

    // create buttons layout
    mLayout = new QBoxLayout( QBoxLayout::LeftToRight, mFrame );
    mLayout->setMargin( 0 );
    mLayout->setSpacing( 0 );

    // add frame to toolbar
    aDockFrame = addWidget( mFrame );

    // connect orientation change
    connect( this, SIGNAL( orientationChanged( Qt::Orientation ) ), this, SLOT( internal_orientationChanged( Qt::Orientation ) ) );

    // set toolbar/layout orientation
    setOrientation( orientation );
}

bool pDockToolBar::eventFilter( QObject* object, QEvent* event )
{
    // get event type
    QEvent::Type type = event->type();
    // try casting object to dockwidget
    QDockWidget* dock = qobject_cast<QDockWidget*>( object );

    // if it s a dock widget
    if ( dock )
    {
        if ( type == QEvent::Show || type == QEvent::Hide )
        {
            // if exclusive, hide all except this one
            if ( type == QEvent::Show && aToggleExclusive->isChecked() )
            {
                foreach ( QDockWidget* dw, docks() )
                {
                    if ( dw != dock && dw->isVisible() )
                        dw->hide();
                }
            }

            // get dock button
            QAbstractButton* btn = button( dock );

            // check the dock button
            btn->setChecked( type == QEvent::Show );

            // check button text
            internal_checkButtonText( btn );

            // check toolbar visibility
            internal_checkVisibility();
        }
        else if ( type == QEvent::KeyPress )
        {
            if ( static_cast<QKeyEvent*>( event )->key() == Qt::Key_Escape )
                dock->hide();
        }
    }

    // deturn default event filter
    return QToolBar::eventFilter( object, event );
}

/*!
    \details Add an action into this pDockToolBar
    \param action The action to add, if null create a new separator action
    \param insert If true the action is inserted before the dock buttons, else it's append
    \return The added action
*/
QAction* pDockToolBar::addAction( QAction* action, bool insert )
{
    // create a separator if no action
    if ( !action )
    {
        action = new QAction( this );
        action->setSeparator( true );
    }

    if ( insert )
        QToolBar::insertAction( aDockFrame, action );
    else
        QToolBar::addAction( action );

    internal_checkVisibility();
    
    return action;
}

/*!
    \details Add actions into this pDockToolBar
    \param actions The actions to add
    \param insert If true the actions are inserted before the dock buttons, else they are append
*/
void pDockToolBar::addActions( QList<QAction*> actions, bool insert )
{
    if ( insert )
        QToolBar::insertActions( aDockFrame, actions );
    else
        QToolBar::addActions( actions );

    internal_checkVisibility();
}

/*!
    \details Add a dock in this toolbar, the dock is moved area if needed
    \param dock The dock to add
    \param title The dock button title
    \param icon The dock button icon
    \return Return the unique id that identify the dock
*/
int pDockToolBar::addDock( QDockWidget* dock, const QString& title, const QIcon& icon )
{   
    // cancel if no dock or dock already managed
    if ( !dock || id( dock ) != -1 )
        return -1;

    // check if this dock is already in another bar, and remove it
    pDockToolBar* tb = mManager->bar( mManager->dockWidgetAreaToToolBarArea( mManager->mainWindow()->dockWidgetArea( dock ) ) );
    if ( tb && tb->id( dock ) != -1 )
        tb->removeDock( dock );

    // set dock title
    if ( !title.isEmpty() )
        dock->setWindowTitle( title );

    // set object name if needed
    if ( dock->objectName().isEmpty() )
        dock->setObjectName( QString( "QDockWidget_%1" ).arg( title ).replace( " ", "_" ).trimmed() );

    // set dock icon
    if ( !icon.isNull() )
        dock->setWindowIcon( icon );

    // create button
    pTabbedWorkspaceCornerButton* pb = new pTabbedWorkspaceCornerButton( this, mManager->toolBarAreaToBoxLayoutDirection( mManager->mainWindow()->toolBarArea( this ) ) );
    pb->setCheckable( true );
    pb->setFont( font() );
    pb->setText( dock->windowTitle() );
    pb->setToolTip( pb->text() );
    pb->setProperty( "Caption", pb->text() );
    pb->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
    pb->setIconSize( iconSize() );
    pb->setIcon( icon );

    // add button to layout
    mLayout->addWidget( pb );

    // add dock to correct area
    Qt::DockWidgetArea da = mManager->mainWindow()->dockWidgetArea( dock );
    Qt::DockWidgetArea ta = mManager->toolBarAreaToDockWidgetArea( mManager->mainWindow()->toolBarArea( this ) );
    if ( da != ta )
        mManager->mainWindow()->addDockWidget( ta, dock, orientation() );

    // if exclusive, hide all dock except this one
    if ( aToggleExclusive->isChecked() && count() )
    {
        foreach ( QDockWidget* dw, docks() )
        {
            if ( dw->isVisible() )
                dw->hide();
        }
    }

    // check button according to dock visibility
    pb->setChecked( dock->isVisible() );

    // check button text
    internal_checkButtonText( pb );

    // add dock/button to list
    mButtons[mUniqueId] = pb;
    mDocks[mUniqueId] = dock;

    // filter the dock
    dock->installEventFilter( this );

    // connect
    connect( dock->toggleViewAction(), SIGNAL( changed() ), this, SLOT( internal_dockChanged() ) );
    connect( dock, SIGNAL( destroyed( QObject* ) ), this, SLOT( internal_dockDestroyed( QObject* ) ) );
    connect( pb, SIGNAL( clicked( bool ) ), this, SLOT( internal_buttonClicked( bool ) ) );

    // check if we need to hide/show the toolbar
    internal_checkVisibility();

    // return unique auto increment id of the dock
    return mUniqueId++;
}

/*!
    \details Unmanage a dock
    \param id The dock id
*/
void pDockToolBar::removeDock( int id )
{
    removeDock( dock( id ) );
}

/*!
    \details Unmanage a dock
    \param dock The dock to unmanage
*/
void pDockToolBar::removeDock( QDockWidget* dock )
{
    // get dock id
    int i = id( dock );

    // cancel if dock is not acutally own by this toolbar
    if ( i == -1 )
        return;
    
    // remove filter event
    dock->removeEventFilter( this );

    // disconnect
    disconnect( dock->toggleViewAction(), SIGNAL( changed() ), this, SLOT( internal_dockChanged() ) );
    disconnect( dock, SIGNAL( destroyed( QObject* ) ), this, SLOT( internal_dockDestroyed( QObject* ) ) );

    // delete button
    QAbstractButton* btn = button( dock );
    mButtons.remove( i );
    btn->deleteLater();

    // remove dock from list
    mDocks.remove( i );

    // check if we need to hide/show the toolbar
    internal_checkVisibility();
}

/*!
    \details Check a dock visibility
    \param id The dock id to check
*/
bool pDockToolBar::isDockVisible( int id ) const
{
    return isDockVisible( dock( id ) );
}

/*!
    \details Check a dock visibility
    \param dock The dock to check
*/
bool pDockToolBar::isDockVisible( QDockWidget* dock ) const
{
    // if dock is in internal list
    if ( id( dock ) != -1 )
        return button( dock )->isChecked();

    // else return widget visibility
    return dock ? dock->isVisible() : false;
}

/*!
    \details Set a dock visibility
    \param dock The dock
    \param visible The visible state
*/
void pDockToolBar::setDockVisible( QDockWidget* dock, bool visible )
{
    dock->setVisible( visible );
}

/*!
    \details Return true if the pDockToolBar buttons are exclusive, else false
*/
bool pDockToolBar::exclusive() const
{
    return aToggleExclusive->isChecked();
}

/*!
    \details Set this pDockToolBar exclusive, ie each dock button are exclusive meaning activating one will deactivate all others
    \param exclusive exclusive or not
*/
void pDockToolBar::setExclusive( bool exclusive )
{
    if ( aToggleExclusive->isChecked() == exclusive )
        return;
    aToggleExclusive->setChecked( exclusive );

    // if exclusive, hide all
    if ( aToggleExclusive->isChecked() && count() )
    {
        foreach ( QDockWidget* dw, docks() )
        {
            if ( dw->isVisible() )
                dw->hide();
        }
    }
}

/*!
    \details If parameter is yes, the text on button will always be shown, else they will be shwn only when they are active
    \param visible Text visibility
*/
void pDockToolBar::setTextAlwaysVisible( bool visible )
{
    mTextAlwaysVisible = visible;
    foreach ( QAbstractButton* ab, mButtons )
    {
        if ( mTextAlwaysVisible )
        {
            if ( ab->text().isEmpty() )
                ab->setText( ab->property( "Caption" ).toString() );
        }
        else
        {
            if ( ab->isChecked() && ab->text().isEmpty() )
                ab->setText( ab->property( "Caption" ).toString() );
            else if ( !ab->isChecked() && !ab->text().isEmpty() )
                ab->setText( QString() );
        }
    }
}

/*!
    \details Return true if button text is always shown, else false
*/
bool pDockToolBar::textAlwaysVisible() const
{
    return mTextAlwaysVisible;
}

/*!
    \details Return the id of the dock given in parameter
    \param dock The dock to get id from
*/
int pDockToolBar::id( QDockWidget* dock ) const
{
    return mDocks.values().contains( dock ) ? mDocks.key( dock ) : -1;
}

/*!
    \details Return the id of the button given in parameter
    \param button The button to get id from
*/
int pDockToolBar::id( QAbstractButton* button ) const
{
    return mButtons.values().contains( button ) ? mButtons.key( button ) : -1;
}

/*!
    \details Return the dock associated with the id given in parameter
    \param id The id of the dock to get
*/
QDockWidget* pDockToolBar::dock( int id ) const
{
    return mDocks.value( id );
}

/*!
    \details Return the dock associated with the button given in parameter
    \param button The button of the dock to get
*/
QDockWidget* pDockToolBar::dock( QAbstractButton* button ) const
{
    return dock( id( button ) );
}

/*!
    \details Return the button associated with the id given in parameter
    \param id The id of the button to get
*/
QAbstractButton* pDockToolBar::button( int id ) const
{
    return mButtons.value( id );
}

/*!
    \details Return the button associated with the dock given in parameter
    \param dock The dock of the button to get
*/
QAbstractButton* pDockToolBar::button( QDockWidget* dock ) const
{
    return button( id( dock ) );
}

/*!
    \details Return the docks list
*/
QList<QDockWidget*> pDockToolBar::docks() const
{
    return mDocks.values();
}

/*!
    \details Return the buttons list
*/
QList<QAbstractButton*> pDockToolBar::buttons() const
{
    return mButtons.values();
}

/*!
    \details Return the number of docks managed by this pDockToolBar
*/
int pDockToolBar::count() const
{
    return docks().count();
}

/*!
    \details Return the togglabe exclusive action associated with this pDockToolBar
*/
QAction* pDockToolBar::toggleExclusiveAction() const 
{
    // set action text
    aToggleExclusive->setText( tr( "%1 exclusive" ).arg( windowTitle() ) );
    return aToggleExclusive;
}

void pDockToolBar::internal_checkVisibility()
{
    // count toolbar actions, if 1 it s dockframe
    int i = actions().count();

    // need show ?!
    if ( !isVisible() && ( i > 1 || ( i == 1 && count() ) ) )
        show();
    // need hide ?!
    else if ( isVisible() && ( i == 1 && !count() ) )
        hide();
}

void pDockToolBar::internal_checkButtonText( QAbstractButton* b )
{
    // cancel if no button
    if ( !b )
        return;
    // show text when checked, else not
    if ( !mTextAlwaysVisible )
    {
        if ( b->isChecked() && b->text().isEmpty() )
            b->setText( b->property( "Caption" ).toString() );
        else if ( !b->isChecked() && !b->text().isEmpty() )
            b->setText( QString() );
    }
}

void pDockToolBar::internal_orientationChanged( Qt::Orientation o )
{
    // change layout direction
    switch ( o )
    {
        case Qt::Horizontal:
            mLayout->setDirection( QBoxLayout::LeftToRight );
            break;
        case Qt::Vertical:
            mLayout->setDirection( QBoxLayout::TopToBottom );
            break;
    }

    // layout docks
    foreach ( QDockWidget* d, mDocks )
        mManager->mainWindow()->addDockWidget( mManager->mainWindow()->dockWidgetArea( d ), d, o );
}

void pDockToolBar::internal_dockChanged()
{
    // get dock action
    QAction* a = qobject_cast<QAction*>( sender() );

    // get dock
    QDockWidget* d = qobject_cast<QDockWidget*>( a->parent() );

    // no position changed, return
    if ( !d || d->isFloating() || pDockToolBarManager::dockWidgetAreaToToolBarArea( mManager->mainWindow()->dockWidgetArea( d ) ) == mManager->mainWindow()->toolBarArea( this ) )
        return;
    else
        emit dockWidgetAreaChanged( d, this );
}

void pDockToolBar::internal_dockDestroyed( QObject* o )
{
    QDockWidget* d = reinterpret_cast<QDockWidget*>( o ); // qobject_cast<QDockWidget*>( o ); qobject_cast don't work with QDockWidget in destroyed emits :|
    
    // get dock id
    int i = id( d );

    // cancel if dock is not acutally own by this toolbar
    if ( i == -1 )
        return;
    
    // remove filter event
    d->removeEventFilter( this );

    // delete button
    QAbstractButton* b = button( d );
    mButtons.remove( i );
    b->deleteLater();

    // remove dock from list
    mDocks.remove( i );

    // check if we need to hide/show the toolbar
    internal_checkVisibility();
}

void pDockToolBar::internal_buttonClicked( bool b )
{
    // get button
    QAbstractButton* ab = qobject_cast<QAbstractButton*>( sender() );

    // get dock
    QDockWidget* d = dock( ab );

    // return if no dock
    if ( !d )
        return;

    if ( aToggleExclusive->isChecked() )
    {
        foreach ( QDockWidget* dw, docks() )
        {
            if ( dw != d && dw->isVisible() )
                dw->hide();
        }
    }

    // update button text
    internal_checkButtonText( ab );

    // show/hide dock according to b
    if ( d->isVisible() != b )
        d->setVisible( b );

    // emit button clicked
    emit buttonClicked( id( dock( ab ) ) );
}