1 // Copyright (C) 2007-2023 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "QtxInfoPanel.h"
31 #include <QSizePolicy>
32 #include <QToolButton>
33 #include <QVBoxLayout>
34 #include <QScrollArea>
38 \class QtxInfoPanel::Container
39 \brief Container to store widgets within info panel
41 class QtxInfoPanel::Container: public QWidget
44 Container( QWidget* = 0 );
45 Container( const QString&, QWidget* = 0 );
47 void addAction( QAction*, const int );
48 void addLabel( const QString&, Qt::Alignment, const int );
49 void addGroup( const QString&, const int );
51 QWidget* find( const int ) const;
53 void remove( const int );
59 QMap<int, QWidget*> ids;
63 QtxInfoPanel::Container::Container( QWidget* parent )
64 : QWidget( parent ), group( 0 )
66 QVBoxLayout* l = new QVBoxLayout( this );
67 l->setContentsMargins( 0, 0, 0, 0 );
70 QtxInfoPanel::Container::Container( const QString& title, QWidget* parent )
73 QVBoxLayout* l = dynamic_cast<QVBoxLayout*>( layout() );
74 group = new QGroupBox( title );
75 group->setLayout( new QVBoxLayout() );
76 l->addWidget( group );
79 void QtxInfoPanel::Container::put( QWidget* widget )
81 QVBoxLayout* l = group ? dynamic_cast<QVBoxLayout*>( group->layout() ) : dynamic_cast<QVBoxLayout*>( layout() );
82 l->addWidget( widget );
85 void QtxInfoPanel::Container::addLabel( const QString& text, Qt::Alignment alignment, const int id )
87 QLabel* label = new QLabel( text );
88 QFont f = label->font();
91 label->setAlignment( alignment );
92 label->setWordWrap( true );
97 void QtxInfoPanel::Container::addAction( QAction* action, const int id )
99 static const char* empty_xpm[] = {"16 16 1 1",
117 QToolButton* button = new QToolButton( this );
118 button->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
119 button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
120 button->setAutoRaise( true );
121 if ( action->icon().isNull() )
122 action->setIcon( QPixmap(empty_xpm) );
123 button->setDefaultAction( action );
128 void QtxInfoPanel::Container::addGroup( const QString& text, const int id )
130 Container* group = new Container( text, this );
135 QWidget* QtxInfoPanel::Container::find( const int id ) const
137 if ( ids.contains( id ) )
140 QMap<int, QWidget*>::ConstIterator it;
142 for( it = ids.begin(); it != ids.end() && !widget; ++it )
144 Container* group = dynamic_cast<Container*>( it.value() );
146 widget = group->find( id );
152 void QtxInfoPanel::Container::remove( const int id )
154 if ( ids.contains( id ) )
156 QVBoxLayout* l = group ? dynamic_cast<QVBoxLayout*>( group->layout() ) : dynamic_cast<QVBoxLayout*>( layout() );
157 l->removeWidget( ids[id] );
158 ids[id]->deleteLater();
164 void QtxInfoPanel::Container::clear()
166 QVBoxLayout* l = group ? dynamic_cast<QVBoxLayout*>( group->layout() ) : dynamic_cast<QVBoxLayout*>( layout() );
168 QList<QWidget*> widgets = ids.values();
169 foreach( QWidget* widget, widgets )
171 l->removeWidget( widget );
172 widget->deleteLater();
182 \class QtxInfoPanel::Title
183 \brief Info panel's title widget
185 class QtxInfoPanel::Title: public QLabel
188 Title( QWidget* parent = 0 );
191 QtxInfoPanel::Title::Title( QWidget* parent )
194 setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
195 QString bg = palette().color( QPalette::Highlight ).name();
196 QString fg = palette().color( QPalette::HighlightedText ).name();
197 setStyleSheet( QString( "QLabel { background:%1; color:%2; }" ).arg( bg ).arg( fg ) );
198 setTextFormat( Qt::PlainText );
202 setContentsMargins( 2, 5, 2, 5 );
208 \brief Info panel which allows presenting welcome, useful hints
209 and other information dynamically, e.g. in the dock panel of main
210 application's window.
212 The *Info panel* normally has a title (aimed to shortly present the
213 current application's context) and a set of buttons and text labels
214 combined into the groups (which may be nested).
216 Buttons normally represent some quick actions which are applicable in
217 the current context. Text labels can be used to show additional information
218 like hints, proposed actions, etc.
220 To set the title to the panel, use method setTitle(). Text label can be
221 added to the panel with addLabel() method, action (button) is added via
224 By default, items are added to the top level, untitled group. Additionally,
225 panel allows arranging items into groups; new group can be added with the
228 Each of addAction(), addLabel(), addGroup() methods return item's unique
229 identifier. This identifier can be used, for instance, to enable/disable
230 item with setEnabled() method, hide/show with setVisible() method, remove
231 from panel with remove() method. The same action can be added to the panel
232 several times, e.g. to the different groups - the corresponding buttons will
233 have different unique ids.
235 To remove all contents of panel, use clear() method.
240 \param parent Parent widget.
242 QtxInfoPanel::QtxInfoPanel( QWidget* parent )
245 title = new Title( this );
246 container = new Container( this );
248 QVBoxLayout* layout = new QVBoxLayout( this );
249 layout->setContentsMargins( 0, 0, 0, 0 );
251 QWidget* wg = new QWidget();
252 QVBoxLayout* wg_layout = new QVBoxLayout( wg );
253 wg_layout->setContentsMargins( 0, 0, 0, 0 );
254 wg_layout->addWidget( container );
255 wg_layout->addStretch();
257 QScrollArea* scroll = new QScrollArea();
258 scroll->setWidgetResizable( true );
259 scroll->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
260 scroll->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Expanding );
261 scroll->setSizeAdjustPolicy( QScrollArea::AdjustToContents );
262 scroll->setFrameStyle( QScrollArea::NoFrame );
263 scroll->setContentsMargins( 0, 0, 0, 0 );
264 scroll->setWidget( wg );
266 layout->addWidget( title );
267 layout->addWidget( scroll );
274 QtxInfoPanel::~QtxInfoPanel()
279 \brief Add left-aligned text label to the given group.
280 \param text Label's text.
281 \param groupId Group's identifier. Value -1 (default) is used
282 to add label to the top-level (untitled) group.
283 \return Label's unique identifier.
285 int QtxInfoPanel::addLabel( const QString& text, const int groupId )
287 return addLabel( text, Qt::AlignLeft, groupId );
291 \brief Add text label to the given group.
292 \param text Label's text.
293 \param alignment Label's alignment.
294 \param groupId Group's identifier. Value -1 (default) is used
295 to add label to the top-level (untitled) group.
296 \return Label's unique identifier.
298 int QtxInfoPanel::addLabel( const QString& text, Qt::Alignment alignment, const int groupId )
301 Container* c = dynamic_cast<Container*>( find( groupId ) );
305 c->addLabel( text, alignment, id );
311 \brief Add action button to the given group.
312 \param action Action being added (note: parent is not changed).
313 \param groupId Group's identifier. Value -1 (default) is used
314 to add button to the top-level (untitled) group.
315 \return Button's unique identifier.
317 int QtxInfoPanel::addAction( QAction* action, const int groupId )
320 Container* c = dynamic_cast<Container*>( find( groupId ) );
324 c->addAction( action, id );
330 \brief Add (sub-)group to the given group.
331 \param text Group's title.
332 \param groupId Parent group's identifier. Value -1 (default) is used
333 to add (sub-)group to the top-level (untitled) group (i.e. panel itself).
334 \return Group's unique identifier.
336 int QtxInfoPanel::addGroup( const QString& text, const int groupId )
339 Container* c = dynamic_cast<Container*>( find( groupId ) );
343 c->addGroup( text, id );
349 \brief Set panel's title.
350 \param text %Title text (empty string removes title).
352 void QtxInfoPanel::setTitle( const QString& text )
354 title->setText( text );
355 title->setVisible( !title->text().isEmpty() );
359 \brief Remove given item from panel.
360 \note If a group is removed, all its contents (recursively) is removed
362 \param id Item's (label's, button's, group's) identifier.
364 void QtxInfoPanel::remove( const int id )
366 QWidget* widget = find( id );
369 Container* group = dynamic_cast<Container*>( widget->parentWidget() );
371 group = dynamic_cast<Container*>( widget->parentWidget()->parentWidget() );
378 \brief Clear contents of panel of group.
379 \param groupId Group's identifier. Value -1 (default) is used
380 to clear all contents of panel.
382 void QtxInfoPanel::clear( const int groupId )
384 Container* c = dynamic_cast<Container*>( find( groupId ) );
391 \brief Find widget that represents given item.
392 \param id Item's (label's, button's, group's) identifier.
393 \return Item's widget (0 if not found).
395 QWidget* QtxInfoPanel::find( const int id ) const
399 return container->find( id );
403 \brief Change item's visibility.
404 \param id Item's (label's, button's, group's) identifier.
405 \param visible \c true to show item, \c false to hide it.
407 void QtxInfoPanel::setVisible( const int id, bool visible )
409 QWidget* widget = find( id );
411 widget->setVisible( visible );
415 \brief Enable / disable item.
416 \param id Item's (label's, button's, group's) identifier.
417 \param enabled \c true to enable item, \c false to disable it.
419 void QtxInfoPanel::setEnabled( const int id, bool enabled )
421 QWidget* widget = find( id );
423 widget->setEnabled( enabled );
428 \brief Generate new unique identifier.
429 \return Unique identifier.
431 int QtxInfoPanel::generateId() const
433 static int id = -100;