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