File: BaseMainWindow.cpp

package info (click to toggle)
qtop 2.3.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,976 kB
  • sloc: cpp: 40,477; makefile: 7
file content (510 lines) | stat: -rw-r--r-- 17,099 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
/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This 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 software 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, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/

#include "BaseMainWindow.h"

#include "BaseIconNames.h"
#include "DockWidget.h"
#include "IconEngine.h"
#include "IconSizeMenu.h"
#include "CustomToolBar.h"
#include "CustomToolButton.h"
#include "QtUtil.h"
#include "Singleton.h"
#include "ToolBarMenu.h"
#include "ToolButtonStyleMenu.h"
#include "Util.h"

#include <QMenuBar>
#include <QTextStream>
#include <QToolBar>
#include <QWindowStateChangeEvent>

//____________________________________________________________
BaseMainWindow::BaseMainWindow( QWidget *parent, Qt::WindowFlags WindowFlags):
    QMainWindow( parent, WindowFlags ),
    monitor_( this )
{
    Debug::Throw( "BaseMainWindow::BaseMainWindow.\n" );

    // lock toolbars action
    addAction( lockToolBarsAction_ = new QAction( IconEngine::get( IconNames::Lock ), tr( "Lock Toolbar Positions" ), this ) );
    lockToolBarsAction_->setCheckable( true );
    lockToolBarsAction_->setChecked( true );
    connect( lockToolBarsAction_, SIGNAL(toggled(bool)), SLOT(_lockToolBars(bool)) );

    // lock panels action
    addAction( lockPanelsAction_ = new QAction( IconEngine::get( IconNames::Lock ), tr( "Lock Panel Positions" ), this ) );
    lockPanelsAction_->setCheckable( true );
    lockPanelsAction_->setChecked( true );
    connect( lockPanelsAction_, SIGNAL(toggled(bool)), SLOT(_lockPanels(bool)) );

    // show menu action
    addAction( showMenuBarAction_ = new QAction( IconEngine::get( IconNames::ShowMenu ), tr( "Show Menu Bar" ), this ) );
    showMenuBarAction_->setCheckable( true );
    showMenuBarAction_->setChecked( true );
    showMenuBarAction_->setShortcut( Qt::CTRL + Qt::Key_M );
    showMenuBarAction_->setEnabled( false );
    connect( showMenuBarAction_, SIGNAL(toggled(bool)), SLOT(_toggleMenuBar(bool)) );

    // show statusbar
    addAction( showStatusBarAction_ = new QAction( tr( "Show Status Bar" ), this ) );
    showStatusBarAction_->setCheckable( true );
    showStatusBarAction_->setChecked( true );
    showStatusBarAction_->setEnabled( false );
    connect( showStatusBarAction_, SIGNAL(toggled(bool)), SLOT(_toggleStatusBar(bool)) );

    connect( Base::Singleton::get().application(), SIGNAL(configurationChanged()), SLOT(_updateConfiguration()) );
    connect( this, SIGNAL(toolbarConfigurationChanged()), Base::Singleton::get().application(), SIGNAL(configurationChanged()) );

    _updateConfiguration();

}

//__________________________________________________
void BaseMainWindow::setOptionName( const QString& name )
{

    Debug::Throw( "BaseMainWindow::setOptionName.\n" );
    monitor_.setOptionName( name );

    if( name.isEmpty() ) {

        lockToolBarsOptionName_.clear();
        lockPanelsOptionName_.clear();
        showMenuBarOptionName_.clear();
        showStatusBarOptionName_.clear();

    } else {

        lockToolBarsOptionName_ = name+"_LOCK_TOOLBARS";
        if( !XmlOptions::get().contains( lockToolBarsOptionName_ ) ) XmlOptions::get().set<bool>( lockToolBarsOptionName_, lockToolBarsAction_->isChecked() );
        else { lockToolBarsAction_->setChecked( XmlOptions::get().get<bool>( lockToolBarsOptionName_ ) ); }

        lockPanelsOptionName_ = name+"_LOCK_PANELS";
        if( !XmlOptions::get().contains( lockPanelsOptionName_ ) ) XmlOptions::get().set<bool>( lockPanelsOptionName_, lockPanelsAction_->isChecked() );
        else { lockPanelsAction_->setChecked( XmlOptions::get().get<bool>( lockPanelsOptionName_ ) ); }

        showMenuBarOptionName_ = name+"_SHOW_MENU";
        if( !XmlOptions::get().contains( showMenuBarOptionName_ ) ) XmlOptions::get().set<bool>( showMenuBarOptionName_, showMenuBarAction_->isChecked() );
        else showMenuBarAction_->setChecked( XmlOptions::get().get<bool>( showMenuBarOptionName_ ) );

        showStatusBarOptionName_ = name+"_SHOW_STATUS";
        if( !XmlOptions::get().contains( showStatusBarOptionName_ ) ) XmlOptions::get().set<bool>( showStatusBarOptionName_, showStatusBarAction_->isChecked() );
        else showStatusBarAction_->setChecked( XmlOptions::get().get<bool>( showStatusBarOptionName_ ) );

    }

}

//__________________________________________________
void BaseMainWindow::setWindowTitle( const QString& title )
{

    Debug::Throw( "BaseMainWindow::setWindowTitle.\n" );
    QMainWindow::setWindowTitle( Util::windowTitle( title ) );

}

//__________________________________________________
void BaseMainWindow::setMenuBar( QMenuBar* menu )
{

    Debug::Throw( "BaseMainWindow::setMenuBar.\n" );
    QMainWindow::setMenuBar( menu );
    if( !menuBar() ) return;
    menuBar()->setVisible( showMenuBarAction_->isChecked() );
    showMenuBarAction_->setEnabled( true );
}

//__________________________________________________
void BaseMainWindow::setStatusBar( QStatusBar* widget )
{

    Debug::Throw( "BaseMainWindow::setStatusBar.\n" );
    QMainWindow::setStatusBar( widget );
    if( !statusBar() ) return;
    statusBar()->setVisible( showStatusBarAction_->isChecked() );
    showStatusBarAction_->setEnabled( true );
}

//__________________________________________________
QSize BaseMainWindow::minimumSizeHint() const
{
    QSize out( monitor_.sizeHint() );
    return out.isValid() ? out:QMainWindow::minimumSizeHint();
}

//__________________________________________________
QSize BaseMainWindow::sizeHint() const
{
    QSize out( monitor_.sizeHint() );
    return out.isValid() ? out:QMainWindow::sizeHint();
}

//________________________________________________________________
void BaseMainWindow::centerOnDesktop()
{

    Debug::Throw( "BaseMainWindow::centerOnDesktop.\n" );
    move( QtUtil::centerOnDesktop( sizeHint() ) );

}

//________________________________________________________________
void BaseMainWindow::centerOnWidget( QWidget* parent )
{

    Debug::Throw( "BaseMainWindow::centerOnWidget.\n" );
    move( QtUtil::centerOnWidget( sizeHint(), parent ) );

}

//________________________________________________________________
QMenu* BaseMainWindow::createPopupMenu()
{
    Debug::Throw( "BaseMainWindow::createPopupMenu.\n" );
    if( !_hasToolBars() )
    {

        QMenu* menu = new QMenu( this );
        if( _hasPanels() ) menu->addAction( lockPanelsAction_ );
        if( _hasMenuBar() ) menu->addAction( showMenuBarAction_ );
        if( _hasStatusBar() ) menu->addAction( showStatusBarAction_ );
        return menu;

    } else {

        ToolBarMenu& menu = toolBarMenu( this );
        menu.toolButtonStyleMenu().select( XmlOptions::get().get<int>( "TOOLBUTTON_TEXT_POSITION" ) );
        menu.iconSizeMenu().select( (IconSize::Size) XmlOptions::get().get<int>( "TOOLBUTTON_ICON_SIZE" ) );
        connect( &menu.toolButtonStyleMenu(), SIGNAL(styleSelected(int)), SLOT(_updateToolButtonStyle(int)) );
        connect( &menu.iconSizeMenu(), SIGNAL(iconSizeSelected(IconSize::Size)), SLOT(_updateToolButtonIconSize(IconSize::Size)) );
        return &menu;

    }

}

//________________________________________________________________
ToolBarMenu& BaseMainWindow::toolBarMenu( QWidget* parent )
{

    Debug::Throw( "BaseMainWindow::toolBarMenu.\n" );
    ToolBarMenu* menu = new ToolBarMenu( parent );

    const ActionList actions( _toolBarsActions( menu ) );
    if( actions.size() > 1 )
    {
        QMenu* toolbarsMenu( menu->addMenu( tr( "Toolbars" ) ) );
        for( const auto& action:actions )
        { toolbarsMenu->addAction( action ); }
    }

    const bool hasLockableToolbars( _hasLockableToolBars() );
    const bool needSeparator( hasLockableToolbars || _hasPanels() || _hasMenuBar() || _hasStatusBar() || actions.size() == 1 );
    if( needSeparator ) menu->addSeparator();
    if( hasLockableToolbars ) menu->addAction( lockToolBarsAction_ );

    if( _hasPanels() ) menu->addAction( lockPanelsAction_ );
    if( _hasMenuBar() ) menu->addAction( showMenuBarAction_ );
    if( actions.size() == 1 )
    {
        actions.front()->setText( tr( "Show Main Toolbar" ) );
        menu->addAction( actions.front() );
    }

    if( _hasStatusBar() ) menu->addAction( showStatusBarAction_ );

    return *menu;

}

//________________________________________________________________
void BaseMainWindow::uniconify()
{

    Debug::Throw( "BaseMainWindow::uniconify" );
    if( isMinimized() )
    {

        if( _wasMaximized() ) showMaximized();
        else showNormal();

    } else if( isHidden() ) show();

    activateWindow();
    raise();

}

//_______________________________________________________
bool BaseMainWindow::event( QEvent* event )
{

    // check that all needed widgets/actions are valid and checked.
    switch (event->type())
    {

        case QEvent::WindowStateChange:
        {
            // cast
            QWindowStateChangeEvent *state_event( static_cast<QWindowStateChangeEvent*>(event) );

            if( windowState() & Qt::WindowMinimized )
            { _setWasMaximized( state_event->oldState() & Qt::WindowMaximized ); }

        }
        break;

        default: break;
    }

    return QMainWindow::event( event );

}

//________________________________________________________________
bool BaseMainWindow::_hasMenuBar() const
{
    Debug::Throw( "BaseMainWindow::_hasMenuBar.\n" );
    return (bool) findChild<QMenuBar*>();
}

//________________________________________________________________
bool BaseMainWindow::_hasStatusBar() const
{
    Debug::Throw( "BaseMainWindow::_hasStatusBar.\n" );
    return (bool) findChild<QStatusBar*>();
}

//________________________________________________________________
bool BaseMainWindow::_hasToolBars() const
{
    Debug::Throw( "BaseMainWindow::_hasToolBars.\n" );
    QList<QToolBar*> toolbars( findChildren<QToolBar*>() );
    for( const auto& toolbar:toolbars )
    {

        // skip toolbars with no names
        if( toolbar->windowTitle().isEmpty() ) continue;

        // cast to custom
        CustomToolBar* customToolbar( qobject_cast<CustomToolBar*>( toolbar ) );

        // skip toolbars that are not direct children
        if( toolbar->parentWidget() != this && !(customToolbar && customToolbar->appearsInMenu() ))
        { continue; }

        return true;
    }

    return false;

}

//________________________________________________________________
bool BaseMainWindow::_hasLockableToolBars() const
{
    Debug::Throw( "BaseMainWindow::_hasLockableToolBars.\n" );

    QList<QToolBar*> toolbars( findChildren<QToolBar*>() );
    for( const auto& toolbar:toolbars )
    {

        // try cast to custom
        CustomToolBar* customToolbar( qobject_cast<CustomToolBar*>( toolbar ) );

        // skip toolbars that are not direct children
        // or should not appear in menu
        if( toolbar->parentWidget() != this && !(customToolbar && customToolbar->appearsInMenu() ))
        { continue; }

        if( customToolbar && customToolbar->lockFromOptions() )
        { continue; }

        return true;
    }

    return false;

}

//________________________________________________________________
bool BaseMainWindow::_hasPanels() const
{ return (bool) findChild<DockWidget*>(); }

//________________________________________________________________
BaseMainWindow::ActionList BaseMainWindow::_toolBarsActions( QMenu* menu )
{
    Debug::Throw( "BaseMainWindow::_toolBarsActions.\n" );

    ActionList actions;

    QList<QToolBar*> toolbars( findChildren<QToolBar*>() );
    for( const auto& toolbar:toolbars )
    {

        // try cast to custom
        CustomToolBar* customToolbar( qobject_cast<CustomToolBar*>( toolbar ) );

        // skip toolbars that are not direct children
        // or should not appear in menu
        if( toolbar->parentWidget() != this && !(customToolbar && customToolbar->appearsInMenu() ))
        { continue; }

        if( customToolbar ) {

            actions.append( &customToolbar->visibilityAction() );

        } else if( !toolbar->windowTitle().isEmpty() ) {

            // add visibility action
            QAction* action = new QAction( toolbar->windowTitle(), menu );
            action->setCheckable( true );
            action->setChecked( toolbar->isVisible() );
            connect( action, SIGNAL(toggled(bool)), toolbar, SLOT(setVisible(bool)) );
            actions.append( action );

        }

    }

    return actions;

}

//____________________________________________________________
void BaseMainWindow::_updateConfiguration()
{

    Debug::Throw( "BaseMainWindow::_updateConfiguration.\n" );

    // icon size
    int iconSize( XmlOptions::get().get<int>( "TOOLBUTTON_ICON_SIZE" ) );
    if( iconSize <= 0 ) iconSize = style()->pixelMetric( QStyle::PM_ToolBarIconSize );
    setIconSize( QSize( iconSize, iconSize ) );

    // text label for toolbars
    const int toolButtonTextPosition( XmlOptions::get().get<int>( "TOOLBUTTON_TEXT_POSITION" ) );
    if( toolButtonTextPosition < 0 ) setToolButtonStyle(  (Qt::ToolButtonStyle) style()->styleHint( QStyle::SH_ToolButtonStyle ) );
    else setToolButtonStyle(  (Qt::ToolButtonStyle) toolButtonTextPosition );

    // toolbars locked
    if( hasOptionName() )
    {

        // toolbars locked
        if( XmlOptions::get().contains( lockToolBarsOptionName_ ) )
        { lockToolBarsAction_->setChecked( XmlOptions::get().get<bool>( lockToolBarsOptionName_ ) ); }

        // toolbars locked
        if( XmlOptions::get().contains( lockPanelsOptionName_ ) )
        { lockPanelsAction_->setChecked( XmlOptions::get().get<bool>( lockPanelsOptionName_ ) ); }

        // menu visibility
        if( XmlOptions::get().contains( showMenuBarOptionName_ ) )
        { showMenuBarAction_->setChecked( XmlOptions::get().get<bool>( showMenuBarOptionName_ ) ); }

        // menu visibility
        if( XmlOptions::get().contains( showStatusBarOptionName_ ) )
        { showStatusBarAction_->setChecked( XmlOptions::get().get<bool>( showStatusBarOptionName_ ) ); }

    }
}

//____________________________________________________________
void BaseMainWindow::_updateToolButtonStyle( int style )
{

    Debug::Throw( "BaseMainWindow::_updateToolButtonStyle.\n" );
    XmlOptions::get().set<int>( "TOOLBUTTON_TEXT_POSITION", style );
    emit toolbarConfigurationChanged();

}

//____________________________________________________________
void BaseMainWindow::_updateToolButtonIconSize( IconSize::Size size )
{

    Debug::Throw( "BaseMainWindow::_updateToolButtonIconSize.\n" );
    XmlOptions::get().set<int>( "TOOLBUTTON_ICON_SIZE", size );
    emit toolbarConfigurationChanged();

}

//____________________________________________________________
void BaseMainWindow::_lockToolBars( bool value )
{
    Debug::Throw( "BaseMainWindow::_lockToolBars.\n" );
    for( const auto& toolbar:findChildren<QToolBar*>() )
    {

        // skip if parent is not this
        if( !(toolbar->window() == this) ) continue;

        // try cast to CustomToolBar and check for 'lock from options'
        CustomToolBar* customtoolbar( qobject_cast<CustomToolBar*>( toolbar ) );
        if( customtoolbar && customtoolbar->lockFromOptions() ) continue;

        // update movability
        toolbar->setMovable( !value );

    }

    if( hasOptionName() ) XmlOptions::get().set<bool>( lockToolBarsOptionName_, value );
    return;
}

//____________________________________________________________
void BaseMainWindow::_lockPanels( bool value )
{
    Debug::Throw( "BaseMainWindow::_lockPanels.\n" );
    for( const auto& panel:findChildren<DockWidget*>() )
    { panel->setLocked( value ); }

    if( hasOptionName() ) XmlOptions::get().set<bool>( lockPanelsOptionName_, value );
    return;
}

//____________________________________________________________
void BaseMainWindow::_toggleMenuBar( bool value )
{
    Debug::Throw( "BaseMainWindow::_toggleMenuBar.\n" );

    if( !menuWidget() ) return;
    menuWidget()->setVisible( value );

    // save option
    if( hasOptionName() ) XmlOptions::get().set<bool>( showMenuBarOptionName_, value );

}

//____________________________________________________________
void BaseMainWindow::_toggleStatusBar( bool value )
{
    Debug::Throw( "BaseMainWindow::_toggleStatusBar.\n" );

    if( !statusBar() ) return;
    statusBar()->setVisible( value );

    // save option
    if( hasOptionName() ) XmlOptions::get().set<bool>( showStatusBarOptionName_, value );

}