From: vsr Date: Wed, 18 Nov 2020 17:51:40 +0000 (+0300) Subject: InfoPanel introduction v4. X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=b28f5e6783166f081df129a95e436066edd34671;p=modules%2Fgui.git InfoPanel introduction v4. --- diff --git a/src/Qtx/QtxInfoPanel.cxx b/src/Qtx/QtxInfoPanel.cxx index 8f4ef80d0..e5d77db7d 100644 --- a/src/Qtx/QtxInfoPanel.cxx +++ b/src/Qtx/QtxInfoPanel.cxx @@ -26,128 +26,221 @@ #include "QtxInfoPanel.h" +#include #include #include -#include +#include +#include +#include -QtxInfoPanel::QtxInfoPanel( QWidget* parent ) - : QWidget( parent ) + +class QtxInfoPanel::Container: public QWidget { - tbar = new QToolBar(); - tbar->setOrientation(Qt::Vertical); - tbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); - QVBoxLayout* layout = new QVBoxLayout(this); - layout->setMargin( 0 ); - layout->addWidget(tbar); +public: + Container( QWidget* = 0 ); + Container( const QString&, QWidget* = 0 ); + + void addAction( QAction*, const int ); + void addLabel( const QString&, Qt::Alignment, const int ); + void addGroup( const QString&, const int ); + + QWidget* find( const int ) const; + + void remove( const int ); + void clear(); + + void put( QWidget* ); + +private: + QMap ids; + QGroupBox* group; +}; + +QtxInfoPanel::Container::Container( QWidget* parent ) + : QWidget( parent ), group( 0 ) +{ + QVBoxLayout* l = new QVBoxLayout( this ); + l->setContentsMargins( 0, 0, 0, 0 ); } -QtxInfoPanel::~QtxInfoPanel() +QtxInfoPanel::Container::Container( const QString& title, QWidget* parent ) + : Container( parent ) { + QVBoxLayout* l = dynamic_cast( layout() ); + group = new QGroupBox( title ); + group->setLayout( new QVBoxLayout() ); + l->addWidget( group ); } -int QtxInfoPanel::addLabel( const QString& text, Qt::Alignment alignment, const int groupId ) +void QtxInfoPanel::Container::put( QWidget* widget ) { - QLabel *label = new QLabel(text); - label->setAlignment(alignment); - label->setWordWrap(true); - - int id_new = generateId(); - QToolBar* tb = getToolBar(groupId); - QAction* q = tb->addWidget(label); - q->setProperty("id", id_new); - alignLeft(tb->layout()); - - return id_new; + QVBoxLayout* l = group ? dynamic_cast( group->layout() ) : dynamic_cast( layout() ); + l->addWidget( widget ); } -int QtxInfoPanel::addAction( QAction* action, const int groupId ) +void QtxInfoPanel::Container::addLabel( const QString& text, Qt::Alignment alignment, const int id ) { - int id_new = generateId(); - action->setProperty("id", id_new); - QToolBar* tb = getToolBar(groupId); - tb->addAction(action); - alignLeft(tb->layout()); + QLabel* label = new QLabel( text ); + label->setAlignment( alignment ); + label->setWordWrap( true ); + put( label ); + ids[ id ] = label; +} - return id_new; +void QtxInfoPanel::Container::addAction( QAction* action, const int id ) +{ + QToolButton* button = new QToolButton( this ); + button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); + button->setAutoRaise( true ); + button->setDefaultAction( action ); + put( button ); + ids[ id ] = button; } -int QtxInfoPanel::addGroup( const QString& text, const int groupId ) +void QtxInfoPanel::Container::addGroup( const QString& text, const int id ) +{ + Container* group = new Container( text, this ); + put( group ); + ids[ id ] = group; +} + +QWidget* QtxInfoPanel::Container::find( const int id ) const { - QGroupBox* box = new QGroupBox(text); - QVBoxLayout *vbox = new QVBoxLayout; - box->setLayout(vbox); - QToolBar* toolbar = new QToolBar(); - toolbar->setOrientation(Qt::Vertical); - toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); - vbox->addWidget(toolbar); - - int id_new = generateId(); - QToolBar* tb = getToolBar(groupId); - QAction* q = tb->addWidget(box); - box->setProperty("id", id_new); - alignLeft(tb->layout()); + if ( ids.contains( id ) ) + return ids[id]; + + QMap::ConstIterator it; + QWidget* widget = 0; + for( it = ids.begin(); it != ids.end() && !widget; ++it ) + { + Container* group = dynamic_cast( it.value() ); + if ( group ) + widget = group->find( id ); + } - return id_new; + return widget; } -void QtxInfoPanel::remove(const int actionId, const int groupId) +void QtxInfoPanel::Container::remove( const int id ) { - // TODO: Method don't delete QAction - QToolBar* tb = getToolBar(groupId); - tb->removeAction(getAction(actionId, groupId)); + if ( ids.contains( id ) ) + { + QVBoxLayout* l = group ? dynamic_cast( group->layout() ) : dynamic_cast( layout() ); + l->removeWidget( ids[id] ); + ids[id]->deleteLater(); + l->invalidate(); + ids.remove( id ); + } } -void QtxInfoPanel::clear(const int groupId) +void QtxInfoPanel::Container::clear() { - QToolBar* tb = getToolBar(groupId); - tb->clear(); + QVBoxLayout* l = group ? dynamic_cast( group->layout() ) : dynamic_cast( layout() ); + + QList widgets = ids.values(); + foreach( QWidget* widget, widgets ) + { + l->removeWidget( widget ); + widget->deleteLater(); + } + + l->invalidate(); + ids.clear(); } -void QtxInfoPanel::alignLeft( QLayout* lay ) + +QtxInfoPanel::QtxInfoPanel( QWidget* parent ) + : QWidget( parent ) +{ + container = new Container( this ); + QVBoxLayout* layout = new QVBoxLayout(this); + layout->setMargin( 0 ); + layout->addWidget( container ); + layout->addStretch(); +} + +QtxInfoPanel::~QtxInfoPanel() +{ +} + +int QtxInfoPanel::addLabel( const QString& text, const int groupId ) +{ + return addLabel( text, Qt::AlignLeft, groupId ); +} + +int QtxInfoPanel::addLabel( const QString& text, Qt::Alignment alignment, const int groupId ) { - for(int i = 0; i < lay->count(); ++i) - lay->itemAt(i)->setAlignment(Qt::AlignLeft); + int id = 0; + Container* c = dynamic_cast( find( groupId ) ); + if ( c ) + { + id = generateId(); + c->addLabel( text, alignment, id ); + } + return id; } -QToolBar* QtxInfoPanel::getToolBar( const int id ) +int QtxInfoPanel::addAction( QAction* action, const int groupId ) { - if (id == -1) - return this->tbar; - for(QAction* a : this->tbar->actions()) + int id = 0; + Container* c = dynamic_cast( find( groupId ) ); + if ( c ) { - QWidget* w= this->tbar->widgetForAction(a); - if(w != NULL) - { - if (w->property("id").value() == id) - { - QGroupBox *qgb = dynamic_cast(w); - if (qgb) - return dynamic_cast(qgb->layout()->itemAt(0)->widget()); - } - } + id = generateId(); + c->addAction( action, id ); } - return nullptr; + return id; } -QAction* QtxInfoPanel::getAction( const int actionId, const int groupId ) +int QtxInfoPanel::addGroup( const QString& text, const int groupId ) { - QToolBar* tb = getToolBar(groupId); - for(QAction* a : tb->actions()) + int id = 0; + Container* c = dynamic_cast( find( groupId ) ); + if ( c ) { - if(a->property("id").value() == actionId) - return a; + id = generateId(); + c->addGroup( text, id ); } - return nullptr; + return id; +} + +void QtxInfoPanel::remove( const int id ) +{ + QWidget* widget = find( id ); + if ( widget ) + { + Container* group = dynamic_cast( widget->parentWidget() ); + if ( group ) + group->remove( id ); + } +} + +void QtxInfoPanel::clear( const int groupId ) +{ + Container* c = dynamic_cast( find( groupId ) ); + if ( c ) + c->clear(); +} + +QWidget* QtxInfoPanel::find( const int id ) const +{ + if ( id == -1 ) + return container; + return container->find( id ); } -void QtxInfoPanel::setVisible( const int actionId, bool state, const int groupId ) +void QtxInfoPanel::setVisible( const int id, bool visible ) { - getAction(actionId, groupId)->setVisible(state); + QWidget* widget = find( id ); + if ( widget ) + widget->setVisible( visible ); } -void QtxInfoPanel::setEnabled( const int actionId, bool state, const int groupId ) +void QtxInfoPanel::setEnabled( const int id, bool enabled ) { - getAction(actionId, groupId)->setEnabled(state); + QWidget* widget = find( id ); + if ( widget ) + widget->setEnabled( enabled ); } int QtxInfoPanel::generateId() const diff --git a/src/Qtx/QtxInfoPanel.h b/src/Qtx/QtxInfoPanel.h index 3e72473c1..5fcde4cef 100644 --- a/src/Qtx/QtxInfoPanel.h +++ b/src/Qtx/QtxInfoPanel.h @@ -20,17 +20,11 @@ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -// File : InfoPanel.h -// Author : Viktor UZLOV, Open CASCADE S.A.S. (viktor.uzlov@opencascade.com) -// #ifndef QTXINFOPANEL_H #define QTXINFOPANEL_H #include "Qtx.h" -#include -#include -#include #include #ifdef WIN32 @@ -43,24 +37,29 @@ class QTX_EXPORT QtxInfoPanel : public QWidget { Q_OBJECT + class Container; + public: QtxInfoPanel( QWidget* = 0 ); ~QtxInfoPanel(); + + int addLabel( const QString&, const int = -1 ); int addLabel( const QString&, Qt::Alignment = Qt::AlignLeft, const int = -1 ); - int addAction( QAction* action, const int = -1 ); + int addAction( QAction*, const int = -1 ); int addGroup( const QString&, const int = -1 ); - void setVisible( const int , bool, const int = -1 ); - void setEnabled( const int , bool, const int = -1 ); - void remove( const int, const int = -1); - void clear(const int = -1); + + void remove( const int ); + void clear( const int = -1 ); + + void setVisible( const int, bool ); + void setEnabled( const int, bool ); private: - void alignLeft( QLayout* lay ); - QAction* getAction( const int, const int = -1 ); - QToolBar* getToolBar( const int ); int generateId() const; + QWidget* find( const int ) const; - QToolBar* tbar; +private: + Container* container; }; #ifdef WIN32