Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / SalomeApp / SalomeApp_LoadStudiesDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include "SalomeApp_LoadStudiesDlg.h"
23
24 #include <QLabel>
25 #include <QHBoxLayout>
26 #include <QVBoxLayout>
27 #include <QListWidget>
28 #include <QPushButton>
29 #include <QStringList>
30
31 #define SPACING_SIZE             6
32 #define MARGIN_SIZE             11
33 #define MIN_LISTBOX_WIDTH      150
34 #define MIN_LISTBOX_HEIGHT     100
35
36 /*!
37   \class SalomeApp_LoadStudiesDlg
38   \brief Dialog box which allows selecting study to be loaded 
39   from the list.
40 */
41
42 /*!
43   \brief Constructor
44   \param parent a parent widget
45   \param studies list of study names
46 */
47 SalomeApp_LoadStudiesDlg::SalomeApp_LoadStudiesDlg( QWidget* parent, const QStringList& studies )
48 : QDialog( parent )
49 {
50   setModal( true );
51
52   setWindowTitle( tr("DLG_LOAD_STUDY_CAPTION") );
53   setSizeGripEnabled( true );
54
55   QVBoxLayout* topLayout = new QVBoxLayout( this );
56   topLayout->setMargin( MARGIN_SIZE );
57   topLayout->setSpacing( SPACING_SIZE );
58   
59   QLabel* lab = new QLabel( tr( "MEN_STUDIES_CHOICE" ), this );
60   
61   myButtonOk = new QPushButton( tr( "BUT_OK" ), this );
62   myButtonOk->setAutoDefault( true );
63   myButtonOk->setDefault( true );
64     
65   QPushButton* buttonCancel = new QPushButton( tr( "BUT_CANCEL" ), this );
66   
67   QHBoxLayout* btnLayout = new QHBoxLayout;
68   btnLayout->setSpacing( SPACING_SIZE );
69   btnLayout->setMargin( 0 );
70   btnLayout->addWidget( myButtonOk );
71   btnLayout->addStretch();
72   btnLayout->addWidget( buttonCancel );
73
74   myList = new QListWidget( this );
75   myList->setMinimumSize( MIN_LISTBOX_WIDTH, MIN_LISTBOX_HEIGHT );
76   myList->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, 
77                                                QSizePolicy::Expanding ) );
78   myList->setSelectionMode( QAbstractItemView::SingleSelection );
79
80   topLayout->addWidget( lab );
81   topLayout->addWidget( myList );
82   topLayout->addLayout( btnLayout );
83   
84   connect( myButtonOk,   SIGNAL( clicked() ), this, SLOT( accept() ) );
85   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
86   connect( myList,       SIGNAL( itemDoubleClicked( QListWidgetItem* ) ),
87                                               this, SLOT( accept() ) );
88   connect( myList,       SIGNAL( itemSelectionChanged() ),
89                                               this, SLOT( updateState() ) );
90   myList->addItems( studies );
91
92   updateState();
93 }
94
95 /*!
96   \brief Destructor
97 */
98 SalomeApp_LoadStudiesDlg::~SalomeApp_LoadStudiesDlg()
99 {
100 }
101
102 /*!
103   \brief Updates buttons state.
104 */
105 void SalomeApp_LoadStudiesDlg::updateState()
106 {
107   myButtonOk->setEnabled( myList->currentItem() != 0 );
108 }
109
110 /*!
111   \brief Get selected study name
112   \return selected study name or null string if study is not selected
113 */
114 QString SalomeApp_LoadStudiesDlg::selectedStudy()
115 {
116   QString study;
117   if ( myList->currentItem() )
118     study = myList->currentItem()->text();
119   return study;
120 }
121
122 /*!
123   \brief Executes dialog box to select study from the list 
124          and returns the study selected.
125   \param parent parent widget
126   \param studies list of study names
127   \return select study (or null string if dialog box is rejected)
128 */
129 QString SalomeApp_LoadStudiesDlg::selectStudy( QWidget* parent, const QStringList& studies )
130 {
131   SalomeApp_LoadStudiesDlg dlg( parent, studies );
132   QString study;
133   if ( dlg.exec() == QDialog::Accepted )
134     study = dlg.selectedStudy();
135   return study;
136 }