~ubuntu-branches/ubuntu/vivid/openwalnut/vivid

« back to all changes in this revision

Viewing changes to src/qt4gui/guiElements/WQtDockTitleWidget.cpp

  • Committer: Package Import Robot
  • Author(s): Sebastian Eichelbaum
  • Date: 2014-03-19 17:46:12 UTC
  • mfrom: (3.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20140319174612-e4mgtr1avbq3f7ph
Tags: 1.4.0~rc1+hg3a3147463ee2-1
* Major functionality and stability improvements.
* Several bug fixes
* Changed ttf-liberation dependency to fonts-liberation (Closes: #722405)
* OpenWalnut now works properly with OpenSceneGraph 3.2 (Closes: #718381)
* See http://www.openwalnut.org/versions/2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//---------------------------------------------------------------------------
 
2
//
 
3
// Project: OpenWalnut ( http://www.openwalnut.org )
 
4
//
 
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
 
6
// For more information see http://www.openwalnut.org/copying
 
7
//
 
8
// This file is part of OpenWalnut.
 
9
//
 
10
// OpenWalnut is free software: you can redistribute it and/or modify
 
11
// it under the terms of the GNU Lesser General Public License as published by
 
12
// the Free Software Foundation, either version 3 of the License, or
 
13
// (at your option) any later version.
 
14
//
 
15
// OpenWalnut is distributed in the hope that it will be useful,
 
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
// GNU Lesser General Public License for more details.
 
19
//
 
20
// You should have received a copy of the GNU Lesser General Public License
 
21
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
 
22
//
 
23
//---------------------------------------------------------------------------
 
24
 
 
25
#include "../WQt4Gui.h"
 
26
#include "../WMainWindow.h"
 
27
 
 
28
#include "WQtDockWidget.h"
 
29
#include "WQtDockTitleWidget.h"
 
30
#include "WQtDockTitleWidget.moc"
 
31
 
 
32
WQtDockTitleWidget::WQtDockTitleWidget( WQtDockWidget* parent ):
 
33
    QWidget( parent ),
 
34
    dockParent( parent )
 
35
{
 
36
    construct();
 
37
}
 
38
 
 
39
void WQtDockTitleWidget::setupButton( QToolButton* btn )
 
40
{
 
41
    btn->setToolButtonStyle( Qt::ToolButtonIconOnly );
 
42
    btn->setAutoRaise( true );
 
43
}
 
44
 
 
45
void WQtDockTitleWidget::setupSizeConstraints( QWidget* widget )
 
46
{
 
47
    widget->setContentsMargins( 0, 0, 0, 0 );
 
48
    widget->setFixedHeight( 24 );
 
49
    widget->setMinimumSize( 24, 24 );
 
50
    widget->setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed ) );
 
51
}
 
52
 
 
53
void WQtDockTitleWidget::construct()
 
54
{
 
55
    // create titlebar widget and its layout
 
56
    QHBoxLayout* titleWidgetLayout = new QHBoxLayout( this );
 
57
    titleWidgetLayout->setMargin( 0 );
 
58
    titleWidgetLayout->setSpacing( 0 );
 
59
    setSizePolicy( QSizePolicy( QSizePolicy::Ignored, QSizePolicy::Fixed ) );
 
60
 
 
61
    // title
 
62
    m_title = new WScaleLabel( " " + dockParent->windowTitle(), 3, this );
 
63
    m_title->setTextInteractionFlags( Qt::NoTextInteraction );
 
64
 
 
65
    // close Btn
 
66
    m_closeBtn = new QToolButton( this );
 
67
    QAction* closeAction = new QAction( WQt4Gui::getMainWindow()->getIconManager()->getIcon( "popup_close" ), "Close", this );
 
68
    connect( closeAction, SIGNAL( triggered( bool ) ), dockParent, SLOT( close() ) );
 
69
    m_closeBtn->setDefaultAction( closeAction );
 
70
    setupButton( m_closeBtn );
 
71
    setupSizeConstraints( m_closeBtn );
 
72
    m_closeBtn->setMinimumSize( 24, 24 );
 
73
    m_closeBtn->setMaximumSize( 24, 24 );
 
74
 
 
75
    // create the container for the actions shown in the titlebar directly
 
76
    m_tools = new QWidget( this );
 
77
    m_toolsLayout = new QHBoxLayout( m_tools );
 
78
    m_tools->setLayout( m_toolsLayout );
 
79
    m_tools->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
 
80
    m_toolsLayout->setMargin( 0 );
 
81
    m_toolsLayout->setSpacing( 0 );
 
82
    m_tools->setContentsMargins( 0, 0, 0, 0 );
 
83
    m_tools->setMinimumSize( 1, 24 );
 
84
 
 
85
    // create the container for the actions dropped out the titlebar to save some space
 
86
    m_toolsMenu = new QWidget( this );
 
87
    m_toolsMenuLayout = new QHBoxLayout( m_toolsMenu );
 
88
    m_toolsMenu->setLayout( m_toolsMenuLayout );
 
89
    m_toolsMenuLayout->setMargin( 0 );
 
90
    m_toolsMenuLayout->setSpacing( 0 );
 
91
    m_toolsMenu->setContentsMargins( 0, 0, 0, 0 );
 
92
 
 
93
    // create a button that shows the m_toolsMenu container widget
 
94
    m_moreBtn = new QToolButton( this );
 
95
    setupButton( m_moreBtn );
 
96
    setupSizeConstraints( m_moreBtn );
 
97
    m_moreBtn->setFixedWidth( 32 );
 
98
    m_moreBtn->setPopupMode( QToolButton::InstantPopup );
 
99
    m_moreBtn->setIcon(  WQt4Gui::getMainWindow()->getIconManager()->getIcon( "popup_more" ) );
 
100
    QMenu* moreMenu = new QMenu();
 
101
    QWidgetAction* moreAction = new QWidgetAction( m_toolsMenu );
 
102
    moreAction->setDefaultWidget( m_toolsMenu );
 
103
    moreMenu->addAction( moreAction );
 
104
    m_moreBtn->setMenu( moreMenu );
 
105
 
 
106
    // help button
 
107
    m_helpBtn = new QToolButton( this );
 
108
    QAction* helpAction = new QAction( WQt4Gui::getMainWindow()->getIconManager()->getIcon( "questionmark" ), "Help", this );
 
109
    connect( helpAction, SIGNAL( triggered( bool ) ), dockParent, SLOT( showHelp() ) );
 
110
    m_helpBtn->setDefaultAction( helpAction );
 
111
    setupButton( m_helpBtn );
 
112
    setupSizeConstraints( m_helpBtn );
 
113
    m_helpBtn->setMinimumSize( 24, 24 );
 
114
    m_helpBtn->setMaximumSize( 24, 24 );
 
115
    m_helpBtn->setVisible( false );
 
116
 
 
117
    // fill layout
 
118
    titleWidgetLayout->addWidget( m_title );
 
119
    titleWidgetLayout->addStretch( 100000 );
 
120
    titleWidgetLayout->addWidget( m_tools );
 
121
    titleWidgetLayout->addWidget( m_moreBtn );
 
122
    titleWidgetLayout->addWidget( m_helpBtn );
 
123
    titleWidgetLayout->addWidget( m_closeBtn );
 
124
}
 
125
 
 
126
void WQtDockTitleWidget::resizeEvent( QResizeEvent* event )
 
127
{
 
128
    updateLayouts( event->size().width() );
 
129
}
 
130
 
 
131
void WQtDockTitleWidget::addTitleAction( QAction* action, bool instantPopup )
 
132
{
 
133
    QToolButton* actionBtn = new QToolButton( this );
 
134
    actionBtn->setDefaultAction( action );
 
135
    setupButton( actionBtn );
 
136
    setupSizeConstraints( actionBtn );
 
137
 
 
138
    if( instantPopup )
 
139
    {
 
140
        actionBtn->setPopupMode( QToolButton::InstantPopup );
 
141
        actionBtn->setFixedWidth( 32 );
 
142
    }
 
143
 
 
144
    addTitleWidget( actionBtn );
 
145
}
 
146
 
 
147
void WQtDockTitleWidget::addTitleButton( QToolButton* button )
 
148
{
 
149
    setupButton( button );
 
150
    setupSizeConstraints( button );
 
151
 
 
152
    addTitleWidget( button );
 
153
}
 
154
 
 
155
void WQtDockTitleWidget::addTitleWidget( QWidget* widget )
 
156
{
 
157
    setupSizeConstraints( widget );
 
158
 
 
159
    // we keep track of the widgets:
 
160
    m_titleActionWidgets.push_back( widget );
 
161
 
 
162
    // update the layouts
 
163
    updateLayouts( width() );
 
164
}
 
165
 
 
166
void WQtDockTitleWidget::removeTitleWidget( QWidget* widget )
 
167
{
 
168
    if( widget )
 
169
    {
 
170
        m_toolsLayout->removeWidget( widget );
 
171
        m_toolsMenuLayout->removeWidget( widget );
 
172
        m_titleActionWidgets.removeAll( widget );
 
173
        // update the layouts
 
174
        updateLayouts( width() );
 
175
    }
 
176
}
 
177
 
 
178
void WQtDockTitleWidget::removeTitleAction( QAction* action )
 
179
{
 
180
    // find the widget for this action
 
181
    QToolButton* btn = NULL;
 
182
    for( QList< QWidget* >::iterator i = m_titleActionWidgets.begin(); i != m_titleActionWidgets.end(); ++i )
 
183
    {
 
184
        QToolButton* btnCandidate = dynamic_cast< QToolButton* >( *i );
 
185
        if( btnCandidate && ( btnCandidate->defaultAction() == action ) )
 
186
        {
 
187
            btn = btnCandidate;
 
188
        }
 
189
    }
 
190
 
 
191
    removeTitleWidget( btn );
 
192
}
 
193
 
 
194
void WQtDockTitleWidget::addTitleSeperator()
 
195
{
 
196
    // use a qframe
 
197
    QFrame* line = new QFrame();
 
198
    line->setFrameShape( QFrame::VLine );
 
199
    line->setFrameShadow( QFrame::Sunken );
 
200
 
 
201
    // add it
 
202
    m_titleActionWidgets.push_back( line );
 
203
    // update the layouts
 
204
    updateLayouts( width() );
 
205
}
 
206
 
 
207
void WQtDockTitleWidget::updateLayouts( int width )
 
208
{
 
209
    // calculate the size of widgets and the title and the mandatory close button
 
210
    int minRequired = m_title->calculateSize( m_title->text().length() ) +
 
211
                      m_moreBtn->sizeHint().width() +
 
212
                      m_helpBtn->isVisible() * m_helpBtn->sizeHint().width() +
 
213
                      m_closeBtn->sizeHint().width();
 
214
 
 
215
    // check and move items
 
216
    int curWidth = minRequired;
 
217
    QList< QWidget* > visible;
 
218
    QList< QWidget* > hidden;
 
219
    QList< QWidget* >* currentList = &visible;
 
220
    for( QList< QWidget* >::iterator i = m_titleActionWidgets.begin(); i != m_titleActionWidgets.end(); ++i )
 
221
    {
 
222
        if( curWidth > width )
 
223
        {
 
224
            // we reached the size limit.
 
225
            currentList = &hidden;
 
226
        }
 
227
        currentList->push_back( *i );
 
228
        curWidth += ( *i )->sizeHint().width();
 
229
    }
 
230
 
 
231
    // move all visible items to the m_toolsLayout
 
232
    for( QList< QWidget* >::iterator i = visible.begin(); i != visible.end(); ++i )
 
233
    {
 
234
        m_toolsMenuLayout->removeWidget( *i );
 
235
        m_toolsLayout->addWidget( *i );
 
236
    }
 
237
 
 
238
    // move all visible items to the m_toolsMenuLayout
 
239
    for( QList< QWidget* >::iterator i = hidden.begin(); i != hidden.end(); ++i )
 
240
    {
 
241
        m_toolsLayout->removeWidget( *i );
 
242
        m_toolsMenuLayout->addWidget( *i );
 
243
    }
 
244
 
 
245
    // hide more button if nothing needs to be hidden
 
246
    m_moreBtn->setHidden( !( hidden.size() ) );
 
247
}
 
248
 
 
249
void WQtDockTitleWidget::updateHelp()
 
250
{
 
251
    // hide button if no help is available.
 
252
    if( dockParent->getHelpContext() == "" )
 
253
    {
 
254
        m_helpBtn->setVisible( false );
 
255
        return;
 
256
    }
 
257
    m_helpBtn->setVisible( true );
 
258
}