1
//---------------------------------------------------------------------------
3
// Project: OpenWalnut ( http://www.openwalnut.org )
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6
// For more information see http://www.openwalnut.org/copying
8
// This file is part of OpenWalnut.
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.
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.
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/>.
23
//---------------------------------------------------------------------------
25
#include "../WQt4Gui.h"
26
#include "../WMainWindow.h"
28
#include "WQtDockWidget.h"
29
#include "WQtDockTitleWidget.h"
30
#include "WQtDockTitleWidget.moc"
32
WQtDockTitleWidget::WQtDockTitleWidget( WQtDockWidget* parent ):
39
void WQtDockTitleWidget::setupButton( QToolButton* btn )
41
btn->setToolButtonStyle( Qt::ToolButtonIconOnly );
42
btn->setAutoRaise( true );
45
void WQtDockTitleWidget::setupSizeConstraints( QWidget* widget )
47
widget->setContentsMargins( 0, 0, 0, 0 );
48
widget->setFixedHeight( 24 );
49
widget->setMinimumSize( 24, 24 );
50
widget->setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed ) );
53
void WQtDockTitleWidget::construct()
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 ) );
62
m_title = new WScaleLabel( " " + dockParent->windowTitle(), 3, this );
63
m_title->setTextInteractionFlags( Qt::NoTextInteraction );
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 );
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 );
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 );
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 );
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 );
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 );
126
void WQtDockTitleWidget::resizeEvent( QResizeEvent* event )
128
updateLayouts( event->size().width() );
131
void WQtDockTitleWidget::addTitleAction( QAction* action, bool instantPopup )
133
QToolButton* actionBtn = new QToolButton( this );
134
actionBtn->setDefaultAction( action );
135
setupButton( actionBtn );
136
setupSizeConstraints( actionBtn );
140
actionBtn->setPopupMode( QToolButton::InstantPopup );
141
actionBtn->setFixedWidth( 32 );
144
addTitleWidget( actionBtn );
147
void WQtDockTitleWidget::addTitleButton( QToolButton* button )
149
setupButton( button );
150
setupSizeConstraints( button );
152
addTitleWidget( button );
155
void WQtDockTitleWidget::addTitleWidget( QWidget* widget )
157
setupSizeConstraints( widget );
159
// we keep track of the widgets:
160
m_titleActionWidgets.push_back( widget );
162
// update the layouts
163
updateLayouts( width() );
166
void WQtDockTitleWidget::removeTitleWidget( QWidget* widget )
170
m_toolsLayout->removeWidget( widget );
171
m_toolsMenuLayout->removeWidget( widget );
172
m_titleActionWidgets.removeAll( widget );
173
// update the layouts
174
updateLayouts( width() );
178
void WQtDockTitleWidget::removeTitleAction( QAction* action )
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 )
184
QToolButton* btnCandidate = dynamic_cast< QToolButton* >( *i );
185
if( btnCandidate && ( btnCandidate->defaultAction() == action ) )
191
removeTitleWidget( btn );
194
void WQtDockTitleWidget::addTitleSeperator()
197
QFrame* line = new QFrame();
198
line->setFrameShape( QFrame::VLine );
199
line->setFrameShadow( QFrame::Sunken );
202
m_titleActionWidgets.push_back( line );
203
// update the layouts
204
updateLayouts( width() );
207
void WQtDockTitleWidget::updateLayouts( int width )
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();
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 )
222
if( curWidth > width )
224
// we reached the size limit.
225
currentList = &hidden;
227
currentList->push_back( *i );
228
curWidth += ( *i )->sizeHint().width();
231
// move all visible items to the m_toolsLayout
232
for( QList< QWidget* >::iterator i = visible.begin(); i != visible.end(); ++i )
234
m_toolsMenuLayout->removeWidget( *i );
235
m_toolsLayout->addWidget( *i );
238
// move all visible items to the m_toolsMenuLayout
239
for( QList< QWidget* >::iterator i = hidden.begin(); i != hidden.end(); ++i )
241
m_toolsLayout->removeWidget( *i );
242
m_toolsMenuLayout->addWidget( *i );
245
// hide more button if nothing needs to be hidden
246
m_moreBtn->setHidden( !( hidden.size() ) );
249
void WQtDockTitleWidget::updateHelp()
251
// hide button if no help is available.
252
if( dockParent->getHelpContext() == "" )
254
m_helpBtn->setVisible( false );
257
m_helpBtn->setVisible( true );