Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / LightApp / LightApp_ModuleDlg.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 // File   : LightApp_ModuleDlg.cxx
23 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
24 //
25 #include "LightApp_ModuleDlg.h"
26
27 #include <QLabel>
28 #include <QPushButton>
29 #include <QGridLayout>
30 #include <QHBoxLayout>
31
32 /*!
33   \brief Pixmap used as default icon for the module.
34   \internal
35 */
36 static const char* const default_icon[] = { 
37 "48 48 17 1",
38 ". c None",
39 "# c #161e4c",
40 "b c #1d3638",
41 "e c #2f585b",
42 "i c #345b5e",
43 "c c #386266",
44 "g c #3f7477",
45 "d c #4d8589",
46 "m c #519099",
47 "o c #6abbc1",
48 "a c #70c9d3",
49 "f c #79ddea",
50 "n c #7adff2",
51 "k c #7ce2f4",
52 "j c #993550",
53 "h c #d84b71",
54 "l c #ef537d",
55 "................................................",
56 "................................................",
57 "................................................",
58 "................................................",
59 "................................................",
60 "................########.########.########......",
61 "...............#aaaaaa###aaaaaa###aaaaaa##......",
62 "..............#aaaaaa#b#aaaaaa#b#aaaaaa#c#......",
63 ".............########b########b########cc#......",
64 ".............#dddddd#b#dddddd#b#dddddd#cc#......",
65 "...........########d########d########d#cc#......",
66 "..........#aaaaaa###aaaaaa###aaaaaa##d#cc#......",
67 ".........#aaaaaa#b#aaaaaa#b#aaaaaa#c#d#cc#......",
68 "........########b########e########cc#d#c#.......",
69 "........#dddddd#b#dddddd#e#ffffff#cc#d####......",
70 "......########d########d########f#cc###g##......",
71 ".....#aaaaaa###aaaaaa###hhhhhh##f#cc#gg#c#......",
72 "....#aaaaaa#b#aaaaaa#i#hhhhhh#j#f#cc###cc#......",
73 "...########b########i########jj#f#c#gg#cc#......",
74 "...#kkkkkk#b#kkkkkk#i#llllll#jj#f####g#cc#......",
75 "...#kkkkkk#b#kkkkkk#i#llllll#jj###m##g#cc#......",
76 "...#knnkkk#b#kkkkkk#i#llllll#jj#mm#c#g#cc#......",
77 "...#knnkkk#b#kkkkkk#i#llllll#jj###cc#g#c#.......",
78 "...#kkkkkk#b#kkkkkk#i#llllll#j#dd#cc#g####......",
79 "...#kkkkkk###kkkkkk###llllll####d#cc###g##......",
80 "...########g########g########o##d#cc#gg#c#......",
81 "....#gggggg#b#gggggg#b#oooooo#c#d#cc###cc#......",
82 "...########b########b########cc#d#c#gg#cc#......",
83 "...#kkkkkk#b#kkkkkk#b#kkkkkk#cc#d####g#cc#......",
84 "...#kkkkkk#b#kkkkkk#b#kkkkkk#cc###g##g#cc#......",
85 "...#kkkkkk#b#kkkkkk#b#kkkkkk#cc#gg#c#g#cc#......",
86 "...#kkkkkk#b#kkkkkk#b#kkkkkk#cc###cc#g#c#.......",
87 "...#kkkkkk#b#kkkkkk#b#kkkkkk#c#gg#cc#g##........",
88 "...#kkkkkk###kkkkkk###kkkkkk####g#cc###.........",
89 "...########g########g########g##g#cc#...........",
90 "....#gggggg#b#gggggg#b#gggggg#c#g#cc#...........",
91 "...########b########b########cc#g#c#............",
92 "...#kkkkkk#b#kkkkkk#b#kkkkkk#cc#g##.............",
93 "...#kkkkkk#b#kkkkkk#b#kkkkkk#cc###..............",
94 "...#kkkkkk#b#kkkkkk#b#kkkkkk#cc#................",
95 "...#kkkkkk#b#kkkkkk#b#kkkkkk#cc#................",
96 "...#kkkkkk#b#kkkkkk#b#kkkkkk#c#.................",
97 "...#kkkkkk###kkkkkk###kkkkkk##..................",
98 "...########.########.########...................",
99 "................................................",
100 "................................................",
101 "................................................",
102 "................................................"};
103
104 /*!
105   \class LightApp_ModuleDlg
106   \brief A dialog box allowing to select study operation to be performed
107   on the module activating.
108
109   The dialog box is shown when the user tries to activate any module
110   while there is no opened study. The dialog box proposes user to select
111   one of the possible operations which should be done before module activating,
112   for example, create new study or open study from the file.
113   The available operations are assigned by adding the buttons with the unique
114   identifier to the dialog box. When the user clicks any operation button,
115   the dialog box sets its identifier as the return code and closes.
116
117   The typical usage of the dialog box:
118   \code
119   LightApp_ModuleDlg dlg( desktop() );
120   dlg.addButton( "New study", NewStudyId );
121   dlg.addButton( "Open study...", OpenStudyId );
122   int ret = dlg.exec();
123   switch( ret ) {
124   case NewStudyId:
125     // create new study
126     createNewStudy();
127     break;
128   case OpenStudyId:
129     // open study from the file
130     // ... show dialog box to choose the file
131     QString fileName = QFileDialog::getOpenFileName( desktop(), "Open File" );
132     if ( !fileName.isEmpty() )
133       openStudy( fileName );
134     break;
135   default:
136     // operation is cancelled
137     break;
138   }
139   \endcode
140
141   \sa addButton()
142 */
143
144 /*!
145   \brief Constructor.
146   \param parent parent widget
147   \param component module name
148   \param icon module icon
149 */
150 LightApp_ModuleDlg::LightApp_ModuleDlg( QWidget*       parent, 
151                                         const QString& component, 
152                                         const QPixmap& icon )
153 : QDialog ( parent )
154 {
155   setModal( true );
156
157   QPixmap defaultIcon( default_icon );
158   setWindowTitle( tr( "CAPTION" ) );
159   
160   // icon
161   QLabel* iconLab = new QLabel( this );
162   iconLab->setFrameStyle( QFrame::Box | QFrame::Sunken );
163   iconLab->setMinimumSize( 70, 70 );
164   iconLab->setSizePolicy( QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) );
165   iconLab->setPixmap( !icon.isNull() ? icon : defaultIcon );
166   iconLab->setScaledContents( false );
167   iconLab->setAlignment( Qt::AlignCenter );
168
169   // info message
170   QLabel* infoLab = new QLabel( tr ( "DESCRIPTION" ).arg( component ), this );
171   infoLab->setTextFormat( Qt::RichText );
172   infoLab->setAlignment( Qt::AlignCenter );
173   
174   // Buttons
175   myButtonLayout = new QHBoxLayout(); 
176   myButtonLayout->setMargin( 0 ); 
177   myButtonLayout->setSpacing( 6 );
178
179   // <Cancel>
180   QPushButton* cancelBtn = new QPushButton( tr( "CANCEL" ), this );
181   myButtonLayout->addSpacing( 70 );
182   myButtonLayout->addStretch();
183   myButtonLayout->addWidget( cancelBtn );
184
185   QGridLayout* layout = new QGridLayout( this ); 
186   layout->setMargin( 11 );
187   layout->setSpacing( 6 );
188
189   layout->addWidget( iconLab, 0, 0 );
190   layout->addWidget( infoLab, 0, 1 );
191   layout->addLayout( myButtonLayout, 1, 0, 1, 2 );
192
193   // signals and slots connections
194   connect( cancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
195 }
196
197 /*!
198   \brief Destructor.
199 */
200 LightApp_ModuleDlg::~LightApp_ModuleDlg()
201 {
202 }
203
204 /*!
205   \brief Add operation button to the dialog box.
206
207   If the parameter \a id is equal to -1, then the 
208   button identifier is generated automatically.
209
210   \param button button text
211   \param id button identifier
212   \return button identifier
213 */
214 int LightApp_ModuleDlg::addButton( const QString& button, const int id )
215 {
216   static int lastId = 0;
217   int bid = id == -1 ? --lastId : id;
218
219   QPushButton* b = findButton( bid );
220   if ( b ) {
221     myButtons.remove( b );
222     delete b;
223   }
224
225   QPushButton* newButton = new QPushButton( button, this );
226
227   if ( myButtons.empty() ) newButton->setDefault( true );
228
229   myButtons.insert( newButton, bid );
230   myButtonLayout->insertWidget( myButtonLayout->count()-3, newButton );
231   connect( newButton, SIGNAL( clicked() ), this, SLOT( accept() ) );
232
233   return bid;
234 }
235
236 /*!
237   \brief Search button with the specified identifier.
238   \param id button identifier
239   \return button or 0 if \a id is invalid
240 */
241 QPushButton* LightApp_ModuleDlg::findButton( const int id ) const
242 {
243   QPushButton* btn = 0;
244   for ( ButtonMap::ConstIterator it = myButtons.begin(); 
245         it != myButtons.end() && !btn; ++it ) {
246     if ( it.value() == id )
247       btn = it.key();
248   }
249   return btn;
250 }
251
252 /*!
253   \brief Called when any dialog button (except \c Cancel) 
254   is clicked.
255   
256   Closes the dialog and sets its result code to the identifier
257   of the button clicked by the user.
258 */
259 void LightApp_ModuleDlg::accept()
260 {
261   QPushButton* btn = ( QPushButton* )sender();
262   done( myButtons[ btn ] );
263 }