Salome HOME
0d3ea02cb9df426d6dab01e14d6fdd16a78da620
[modules/gui.git] / src / SalomeApp / SalomeApp_LoadStudiesDlg.cxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "SalomeApp_LoadStudiesDlg.h"
21
22 #include <QLabel>
23 #include <QHBoxLayout>
24 #include <QVBoxLayout>
25 #include <QListWidget>
26 #include <QPushButton>
27 #include <QStringList>
28
29 #define SPACING_SIZE             6
30 #define MARGIN_SIZE             11
31 #define MIN_LISTBOX_WIDTH      150
32 #define MIN_LISTBOX_HEIGHT     100
33
34 /*!
35   \class SalomeApp_LoadStudiesDlg
36   \brief Dialog box which allows selecting study to be loaded 
37   from the list.
38 */
39
40 /*!
41   \brief Constructor
42   \param parent a parent widget
43   \param studies list of study names
44 */
45 SalomeApp_LoadStudiesDlg::SalomeApp_LoadStudiesDlg( QWidget* parent, const QStringList& studies )
46 : QDialog( parent )
47 {
48   setModal( true );
49
50   setWindowTitle( tr("DLG_LOAD_STUDY_CAPTION") );
51   setSizeGripEnabled( true );
52
53   QVBoxLayout* topLayout = new QVBoxLayout( this );
54   topLayout->setMargin( MARGIN_SIZE );
55   topLayout->setSpacing( SPACING_SIZE );
56   
57   QLabel* lab = new QLabel( tr( "MEN_STUDIES_CHOICE" ), this );
58   
59   myButtonOk = new QPushButton( tr( "BUT_OK" ), this );
60   myButtonOk->setAutoDefault( true );
61   myButtonOk->setDefault( true );
62     
63   QPushButton* buttonCancel = new QPushButton( tr( "BUT_CANCEL" ), this );
64   
65   QHBoxLayout* btnLayout = new QHBoxLayout;
66   btnLayout->setSpacing( SPACING_SIZE );
67   btnLayout->setMargin( 0 );
68   btnLayout->addWidget( myButtonOk );
69   btnLayout->addStretch();
70   btnLayout->addWidget( buttonCancel );
71
72   myList = new QListWidget( this );
73   myList->setMinimumSize( MIN_LISTBOX_WIDTH, MIN_LISTBOX_HEIGHT );
74   myList->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, 
75                                                QSizePolicy::Expanding ) );
76   myList->setSelectionMode( QAbstractItemView::SingleSelection );
77
78   topLayout->addWidget( lab );
79   topLayout->addWidget( myList );
80   topLayout->addLayout( btnLayout );
81   
82   connect( myButtonOk,   SIGNAL( clicked() ), this, SLOT( accept() ) );
83   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
84   connect( myList,       SIGNAL( itemDoubleClicked( QListWidgetItem* ) ),
85                                               this, SLOT( accept() ) );
86   connect( myList,       SIGNAL( itemSelectionChanged() ),
87                                               this, SLOT( updateState() ) );
88   myList->addItems( studies );
89
90   updateState();
91 }
92
93 /*!
94   \brief Destructor
95 */
96 SalomeApp_LoadStudiesDlg::~SalomeApp_LoadStudiesDlg()
97 {
98 }
99
100 /*!
101   \brief Updates buttons state.
102 */
103 void SalomeApp_LoadStudiesDlg::updateState()
104 {
105   myButtonOk->setEnabled( myList->currentItem() != 0 );
106 }
107
108 /*!
109   \brief Get selected study name
110   \return selected study name or null string if study is not selected
111 */
112 QString SalomeApp_LoadStudiesDlg::selectedStudy()
113 {
114   QString study;
115   if ( myList->currentItem() )
116     study = myList->currentItem()->text();
117   return study;
118 }
119
120 /*!
121   \brief Executes dialog box to select study from the list 
122          and returns the study selected.
123   \param parent parent widget
124   \param studies list of study names
125   \return select study (or null string if dialog box is rejected)
126 */
127 QString SalomeApp_LoadStudiesDlg::selectStudy( QWidget* parent, const QStringList& studies )
128 {
129   SalomeApp_LoadStudiesDlg dlg( parent, studies );
130   QString study;
131   if ( dlg.exec() == QDialog::Accepted )
132     study = dlg.selectedStudy();
133   return study;
134 }