OpenWalnut  1.5.0dev
WQtDockTitleWidget.cpp
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 "../WQtGui.h"
26 #include "../WMainWindow.h"
27 
28 #include "core/common/WLogger.h"
29 
30 #include "WQtDockWidget.h"
31 #include "WQtDockTitleWidget.h"
32 
33 #define MagicWidgetMinSize 24
34 
36  QWidget( parent ),
37  m_dockParent( parent )
38 {
39  construct();
40 }
41 
42 void WQtDockTitleWidget::setupButton( QToolButton* btn )
43 {
44  btn->setToolButtonStyle( Qt::ToolButtonIconOnly );
45  btn->setAutoRaise( true );
46 }
47 
49 {
50  widget->setContentsMargins( 0, 0, 0, 0 );
51  widget->setFixedHeight( MagicWidgetMinSize );
52  widget->setMinimumSize( MagicWidgetMinSize, MagicWidgetMinSize );
53  widget->setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed ) );
54 }
55 
57 {
58  // create titlebar widget and its layout
59  QHBoxLayout* titleWidgetLayout = new QHBoxLayout( this );
60  titleWidgetLayout->setMargin( 0 );
61  titleWidgetLayout->setSpacing( 0 );
62  setSizePolicy( QSizePolicy( QSizePolicy::Ignored, QSizePolicy::Fixed ) );
63 
64  // title
65  m_title = new WScaleLabel( " " + m_dockParent->windowTitle(), 5, this );
66  m_title->setTextInteractionFlags( Qt::NoTextInteraction );
67 
68  // close Btn
69  m_closeBtn = new QToolButton( this );
70  QAction* closeAction = new QAction( WQtGui::getMainWindow()->getIconManager()->getIcon( "popup_close" ), "Close", this );
71  connect( closeAction, SIGNAL( triggered( bool ) ), m_dockParent, SLOT( close() ) );
72  m_closeBtn->setDefaultAction( closeAction );
75  m_closeBtn->setMinimumSize( MagicWidgetMinSize, MagicWidgetMinSize );
76  m_closeBtn->setMaximumSize( MagicWidgetMinSize, MagicWidgetMinSize );
77 
78  // create the container for the actions shown in the titlebar directly
79  m_tools = new QWidget( this );
80  m_toolsLayout = new QHBoxLayout( m_tools );
81  m_tools->setLayout( m_toolsLayout );
82  m_tools->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
83  m_toolsLayout->setMargin( 0 );
84  m_toolsLayout->setSpacing( 0 );
85  m_tools->setContentsMargins( 0, 0, 0, 0 );
86  m_tools->setMinimumSize( 1, MagicWidgetMinSize );
87 
88  // create the container for the actions dropped out the titlebar to save some space
89  m_toolsMenu = new QWidget( this );
90  m_toolsMenuLayout = new QHBoxLayout( m_toolsMenu );
91  m_toolsMenu->setLayout( m_toolsMenuLayout );
92  m_toolsMenuLayout->setMargin( 0 );
93  m_toolsMenuLayout->setSpacing( 0 );
94  m_toolsMenu->setContentsMargins( 0, 0, 0, 0 );
95 
96  // create a button that shows the m_toolsMenu container widget
97  m_moreBtn = new QToolButton( this );
100  m_moreBtn->setFixedWidth( 32 );
101  m_moreBtn->setPopupMode( QToolButton::InstantPopup );
102  m_moreBtn->setIcon( WQtGui::getMainWindow()->getIconManager()->getIcon( "popup_more" ) );
103  QMenu* moreMenu = new QMenu();
104  QWidgetAction* moreAction = new QWidgetAction( m_toolsMenu );
105  moreAction->setDefaultWidget( m_toolsMenu );
106  moreMenu->addAction( moreAction );
107  m_moreBtn->setMenu( moreMenu );
108 
109  // help button
110  m_helpBtn = new QToolButton( this );
111  QAction* helpAction = new QAction( WQtGui::getMainWindow()->getIconManager()->getIcon( "questionmark" ), "Help", this );
112  connect( helpAction, SIGNAL( triggered( bool ) ), m_dockParent, SLOT( showHelp() ) );
113  m_helpBtn->setDefaultAction( helpAction );
116  m_helpBtn->setMinimumSize( MagicWidgetMinSize, MagicWidgetMinSize );
117  m_helpBtn->setMaximumSize( MagicWidgetMinSize, MagicWidgetMinSize );
118  m_helpBtn->setVisible( false );
119 
120  // fill layout
121  titleWidgetLayout->addWidget( m_title );
122  titleWidgetLayout->addStretch( 1 );
123  titleWidgetLayout->addWidget( m_tools );
124  titleWidgetLayout->addWidget( m_moreBtn );
125  titleWidgetLayout->addWidget( m_helpBtn );
126  titleWidgetLayout->addWidget( m_closeBtn );
127 }
128 
129 void WQtDockTitleWidget::resizeEvent( QResizeEvent* event )
130 {
131  updateLayouts( event->size().width() );
132  QWidget::resizeEvent( event );
133 }
134 
135 void WQtDockTitleWidget::addTitleAction( QAction* action, bool instantPopup )
136 {
137  QToolButton* actionBtn = new QToolButton( this );
138  actionBtn->setDefaultAction( action );
139  setupButton( actionBtn );
140  setupSizeConstraints( actionBtn );
141 
142  if( instantPopup )
143  {
144  actionBtn->setPopupMode( QToolButton::InstantPopup );
145  }
146 
147  // we keep track of the widgets:
148  m_titleActionWidgets.push_back( actionBtn );
149 
150  // update the layouts
151  updateLayouts( width() );
152 }
153 
154 void WQtDockTitleWidget::addTitleButton( QToolButton* button )
155 {
156  setupButton( button );
157  setupSizeConstraints( button );
158 
159  addTitleWidget( button );
160 }
161 
162 void WQtDockTitleWidget::addTitleWidget( QWidget* widget )
163 {
164  setupSizeConstraints( widget );
165 
166  // we keep track of the widgets:
167  m_titleActionWidgets.push_back( widget );
168 
169  // update the layouts
170  updateLayouts( width() );
171 }
172 
174 {
175  if( widget )
176  {
177  m_toolsLayout->removeWidget( widget );
178  m_toolsMenuLayout->removeWidget( widget );
179  m_titleActionWidgets.removeAll( widget );
180  // update the layouts
181  updateLayouts( width() );
182  }
183 }
184 
186 {
187  // find the widget for this action
188  QToolButton* btn = NULL;
189  for( QList< QWidget* >::iterator i = m_titleActionWidgets.begin(); i != m_titleActionWidgets.end(); ++i )
190  {
191  QToolButton* btnCandidate = dynamic_cast< QToolButton* >( *i );
192  if( btnCandidate && ( btnCandidate->defaultAction() == action ) )
193  {
194  btn = btnCandidate;
195  }
196  }
197 
198  removeTitleWidget( btn );
199 }
200 
202 {
203  // use a qframe
204  QFrame* line = new QFrame();
205  line->setFrameShape( QFrame::VLine );
206  line->setFrameShadow( QFrame::Sunken );
207  line->setFixedWidth( 5 );
208 
209  // add it
210  m_titleActionWidgets.push_back( line );
211  // update the layouts
212  updateLayouts( width() );
213 }
214 
216 {
217  // calculate the size of widgets and the title and the mandatory close button
218  int minRequired = m_title->calculateSize( m_title->text().length() ) +
219  m_moreBtn->minimumSize().width() +
220  m_helpBtn->isVisible() * m_helpBtn->minimumSize().width() +
221  m_closeBtn->minimumSize().width();
222 
223  // check and move items
224  int curWidth = minRequired;
225  QList< QWidget* > visible;
226  QList< QWidget* > hidden;
227  QList< QWidget* >* currentList = &visible;
228  for( QList< QWidget* >::iterator i = m_titleActionWidgets.begin(); i != m_titleActionWidgets.end(); ++i )
229  {
230  curWidth += ( *i )->sizeHint().width();
231  if( curWidth >= width )
232  {
233  // we reached the size limit.
234  currentList = &hidden;
235  }
236  currentList->push_back( *i );
237  }
238 
239  // move all visible items to the m_toolsLayout
240  for( QList< QWidget* >::iterator i = visible.begin(); i != visible.end(); ++i )
241  {
242  m_toolsMenuLayout->removeWidget( *i );
243  m_toolsLayout->addWidget( *i );
244  }
245 
246  // move all visible items to the m_toolsMenuLayout
247  for( QList< QWidget* >::iterator i = hidden.begin(); i != hidden.end(); ++i )
248  {
249  m_toolsLayout->removeWidget( *i );
250  m_toolsMenuLayout->addWidget( *i );
251  }
252 
253  // hide more button if nothing needs to be hidden
254  m_moreBtn->setHidden( !( hidden.size() ) );
255 
256  updateGeometry();
257 }
258 
260 {
261  // hide button if no help is available.
262  if( m_dockParent->getHelpContext() == "" )
263  {
264  m_helpBtn->setVisible( false );
265  return;
266  }
267  m_helpBtn->setVisible( true );
268 }
269 
271 {
272  m_closeBtn->setDisabled( disable );
273  m_closeBtn->setVisible( !disable );
274 }
WQtDockWidget * m_dockParent
The parent as dock pointer.
virtual void removeTitleAction(QAction *action)
Remove the given action from the list.
QList< QWidget * > m_titleActionWidgets
We keep track of the widgets that we add.
void updateLayouts(int width)
Updates the layouts according to the new width.
QToolButton * m_helpBtn
Help button.
virtual void addTitleButton(QToolButton *button)
Add the given tool button to the titlebar.
void disableCloseButton(bool disable=true)
Disable close button? Might come in handy when embedding these widgets into others.
QToolButton * m_moreBtn
The tool button used when shrinking the title bar too much.
virtual void removeTitleWidget(QWidget *widget)
Remove the specified widget from the title bar.
WQtDockTitleWidget(WQtDockWidget *parent)
Constructor.
QWidget * m_tools
The tools buttons.
virtual void setupButton(QToolButton *btn)
Apply default settings for dock widget title buttons.
QToolButton * m_closeBtn
Close button.
virtual void addTitleAction(QAction *action, bool instantPopup=false)
Add the given action to the titlebar.
virtual void updateHelp()
Update help button.
virtual void resizeEvent(QResizeEvent *event)
Called upon resize.
virtual void addTitleSeperator()
Add a separator.
QWidget * m_toolsMenu
The tool inside the menu.
void construct()
Construct the title and configure the widget.
virtual void addTitleWidget(QWidget *widget)
Add an arbitrary widget.
QHBoxLayout * m_toolsMenuLayout
LAyout of the items in the moreBtn menu.
WScaleLabel * m_title
Title label.
virtual void setupSizeConstraints(QWidget *widget)
Apply size setup to a given widget.
QHBoxLayout * m_toolsLayout
Layout containing the tools.
Advanced QDockWidget.
Definition: WQtDockWidget.h:50
virtual const QString & getHelpContext()
Return the help context id.
static WMainWindow * getMainWindow()
Returns the current main window instance or NULL if not existent.
Definition: WQtGui.cpp:88
Special Label that can shrink and expand in a layout.
Definition: WScaleLabel.h:37
virtual size_t calculateSize(size_t chars) const
Calculate the size that is needed for the given number of chars.
Definition: WScaleLabel.cpp:88