Salome HOME
PR: mergefrom_PAL_OCC_21Oct04
[modules/kernel.git] / src / TOOLSGUI / ToolsGUI_CatalogGeneratorDlg.cxx
1 //  SALOME TOOLSGUI : implementation of desktop "Tools" optioins
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : ToolsGUI_CatalogGeneratorDlg.cxx
25 //  Author : Nicolas REJNERI
26 //  Modified : Marc TAJCHMAN
27 //  Module : SALOME
28 //  $Header$
29
30 #include "ToolsGUI_CatalogGeneratorDlg.h"
31
32 #include "QAD_Application.h"
33 #include "QAD_Desktop.h"
34 #include "QAD_FileDlg.h"
35 #include "QAD_MessageBox.h"
36 #include "QAD_Tools.h"
37 #include <stdlib.h>
38 #include <qlabel.h>
39 #include <qlineedit.h>
40 #include <qpushbutton.h>
41 #include <qlayout.h>
42 #include <qgroupbox.h>
43 #include <qstringlist.h>
44 #include <qregexp.h>
45 #include <qvalidator.h>
46
47 #include <OSD_Process.hxx>
48 #include <OSD_Path.hxx>
49 #include <TCollection_AsciiString.hxx>
50 #include <Standard_CString.hxx>
51
52 #include "utilities.h"
53
54 using namespace std;
55
56 #define SPACING_SIZE             6
57 #define MARGIN_SIZE             11
58 #define MIN_EDIT_SIZE          250
59
60 //=================================================================================
61 // class    : ToolsGUI_CatalogGeneratorDlg()
62 // purpose  : Constructor
63 //=================================================================================
64 ToolsGUI_CatalogGeneratorDlg::ToolsGUI_CatalogGeneratorDlg( QWidget* parent, const char* name )
65     : QDialog( parent, name, TRUE, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu )
66 {
67   if ( !name )
68     setName( "ToolsGUI_CatalogGeneratorDlg" );
69   resize( 322, 120 ); 
70   setCaption( tr( "TOOLS_CATALOG_GENERATOR" ) );
71   setSizeGripEnabled( TRUE );
72
73   QGridLayout* aTopLayout = new QGridLayout(this);
74   aTopLayout->setMargin(MARGIN_SIZE);
75   aTopLayout->setSpacing(SPACING_SIZE);
76
77   QGroupBox* filesGrp = new QGroupBox( tr( "TOOLS_FILES") , this, "filesGrp" );
78   filesGrp->setColumnLayout( 0, Qt::Vertical );
79   filesGrp->layout()->setSpacing( 0 );
80   filesGrp->layout()->setMargin( 0 );
81   QGridLayout* filesGrpLayout = new QGridLayout( filesGrp->layout() );
82   filesGrpLayout->setAlignment( Qt::AlignTop );
83   filesGrpLayout->setSpacing( SPACING_SIZE );
84   filesGrpLayout->setMargin( MARGIN_SIZE  );
85
86   myIdlEdit = new QLineEdit( filesGrp, "myIdlEdit" );
87   myIdlEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
88   myIdlEdit->setMinimumSize( MIN_EDIT_SIZE, 0 );
89   myXmlEdit = new QLineEdit( filesGrp, "myXmlEdit" );
90   myXmlEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
91   myXmlEdit->setMinimumSize( MIN_EDIT_SIZE, 0 );
92
93   myBrowseIdlBtn = new QPushButton( tr( "TOOLS_BUT_BROWSE" ), filesGrp, "myBrowseIdlBtn" );
94   myBrowseXmlBtn = new QPushButton( tr( "TOOLS_BUT_BROWSE" ), filesGrp, "myBrowseXmlBtn" );
95 //  QFontMetrics fm(myBrowseIdlBtn->font());
96 //  myBrowseIdlBtn->setFixedWidth(fm.width(myBrowseIdlBtn->text()) + 10);
97 //  myBrowseXmlBtn->setFixedWidth(fm.width(myBrowseXmlBtn->text()) + 10);
98
99   filesGrpLayout->addWidget( new QLabel( tr( "TOOLS_IDL_FILE" ), filesGrp ), 0, 0);
100   filesGrpLayout->addWidget( myIdlEdit, 0, 1 );
101   filesGrpLayout->addWidget( myBrowseIdlBtn, 0, 2 );
102   filesGrpLayout->addWidget( new QLabel( tr( "TOOLS_XML_FILE" ), filesGrp ), 1, 0);
103   filesGrpLayout->addWidget( myXmlEdit, 1, 1 );
104   filesGrpLayout->addWidget( myBrowseXmlBtn, 1, 2 );
105
106   QGroupBox* supplGrp = new QGroupBox(tr( "TOOLS_SUPPLEMENT" )  , this, "SupplGrp" );
107   supplGrp->setColumnLayout( 0, Qt::Vertical );
108   supplGrp->layout()->setSpacing( 0 );
109   supplGrp->layout()->setMargin( 0 );
110   QGridLayout* supplGrpLayout = new QGridLayout( supplGrp->layout() );
111   supplGrpLayout->setAlignment( Qt::AlignTop );
112   supplGrpLayout->setSpacing( SPACING_SIZE );
113   supplGrpLayout->setMargin( MARGIN_SIZE  );
114
115   QSize myMinimumSize(int(MIN_EDIT_SIZE*0.3), 0);
116
117   myAuthorEdit = new QLineEdit( supplGrp , "myAuthorEdit" );
118   myAuthorEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
119   myAuthorEdit->setMinimumSize( myMinimumSize );
120
121   OSD_Process aProcess;
122   myAuthorEdit->setText(aProcess.UserName().ToCString());
123
124   myVersionEdit = new QLineEdit(supplGrp , "myVersion" );
125   myVersionEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
126   myVersionEdit->setMinimumSize( myMinimumSize );
127   QStringList aList = QStringList::split(QRegExp("\\s+"),tr( "INF_VERSION" ));
128   myVersionEdit->setText(aList.last());
129
130   myPngEdit = new QLineEdit(supplGrp , "myCompIcon" );
131   myPngEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
132   myPngEdit->setMinimumSize( MIN_EDIT_SIZE, 0 );
133
134   myBrowsePngBtn = new QPushButton( tr( "TOOLS_BUT_BROWSE" ), supplGrp, "myBrowsePngBtn" );
135
136   myCompName = new QLineEdit(supplGrp , "myCompName");
137   myCompName->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
138   myCompName->setMinimumSize( myMinimumSize );
139
140   myCompUserName = new QLineEdit(supplGrp , "myCompUserName");
141   myCompUserName->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
142   myCompUserName->setMinimumSize( MIN_EDIT_SIZE*0.3, 0 );
143
144   myCompType = new QLineEdit(supplGrp , "myCompType");
145   myCompType->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
146   myCompType->setMinimumSize( myMinimumSize );
147   myCompType->setText("OTHER");
148
149   myCompMultiStd = new QLineEdit(supplGrp , "myCompMultiStd");
150   myCompMultiStd->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) );
151   myCompMultiStd->setMinimumSize( myMinimumSize );
152   myCompMultiStd->setText("1");
153   QIntValidator *ivalidator = new QIntValidator(myVersionEdit);
154   myCompMultiStd->setValidator(ivalidator);
155   
156   supplGrpLayout->addWidget( new QLabel( tr( "TOOLS_AUTHOR" ), supplGrp ), 0, 0);
157   supplGrpLayout->addWidget( myAuthorEdit, 0, 1 );
158   supplGrpLayout->addWidget( new QLabel( tr( "TOOLS_COMP_NAME" ), supplGrp ), 0, 2);
159   supplGrpLayout->addWidget(myCompName,0,3);
160   supplGrpLayout->addWidget( new QLabel( tr( "TOOLS_COMP_USERNAME" ), supplGrp ), 0, 4);
161   supplGrpLayout->addWidget(myCompUserName,0,5);
162   supplGrpLayout->addWidget( new QLabel( tr( "TOOLS_VERSION" ), supplGrp ), 1, 0);
163   supplGrpLayout->addWidget( myVersionEdit, 1, 1);
164   supplGrpLayout->addWidget( new QLabel( tr( "TOOLS_COMP_MULTISTD" ), supplGrp ), 1, 2);
165   supplGrpLayout->addWidget(myCompMultiStd,1,3);
166   supplGrpLayout->addWidget( new QLabel( tr( "TOOLS_COMP_TYPE" ), supplGrp ), 1, 4);
167   supplGrpLayout->addWidget(myCompType,1,5);
168   supplGrpLayout->addWidget( new QLabel( tr( "TOOLS_PNG_FILE" ), supplGrp ), 2, 0);
169   supplGrpLayout->addMultiCellWidget( myPngEdit, 2,2,1,4 );
170   supplGrpLayout->addWidget( myBrowsePngBtn, 2, 5 );
171   
172
173   QHBoxLayout* aBtnLayout = new QHBoxLayout;
174   aBtnLayout->setSpacing( SPACING_SIZE );
175   aBtnLayout->setMargin( 0 );
176
177   myApplyBtn = new QPushButton( tr( "TOOLS_BUT_APPLY"  ), this, "myApplyBtn" );
178   myApplyBtn->setAutoDefault( true );
179   myApplyBtn->setDefault( true );
180   myCloseBtn = new QPushButton( tr( "TOOLS_BUT_CLOSE" ), this, "myCloseBtn" );
181   myCloseBtn->setAutoDefault( true );
182   
183   aBtnLayout->addWidget( myApplyBtn );
184   aBtnLayout->addItem( new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
185   aBtnLayout->addWidget( myCloseBtn );
186
187   aTopLayout->addWidget( filesGrp, 0, 0 );
188   aTopLayout->addWidget( supplGrp, 1, 0 );
189   aTopLayout->addLayout( aBtnLayout, 2, 0 ); 
190
191   /* signals and slots connections */
192   connect( myApplyBtn,     SIGNAL( clicked() ), this, SLOT( onApply() ) );
193   connect( myCloseBtn,     SIGNAL( clicked() ), this, SLOT( reject() ) );
194   connect( myBrowseIdlBtn, SIGNAL( clicked() ), this, SLOT( onBrowseBtnClicked() ) );
195   connect( myBrowseXmlBtn, SIGNAL( clicked() ), this, SLOT( onBrowseBtnClicked() ) );
196   connect( myBrowsePngBtn, SIGNAL( clicked() ), this, SLOT( onBrowseBtnClicked() ) );
197   connect( myIdlEdit,      SIGNAL( textChanged( const QString& ) ), this, SLOT( updateButtonState() ) );
198   connect( myXmlEdit,      SIGNAL( textChanged( const QString& ) ), this, SLOT( updateButtonState() ) );
199
200   updateButtonState();
201 }
202
203 //=================================================================================
204 // function : ~ToolsGUI_CatalogGeneratorDlg()
205 // purpose  : destructor
206 //=================================================================================
207 ToolsGUI_CatalogGeneratorDlg::~ToolsGUI_CatalogGeneratorDlg()
208 {  
209 }
210
211 //=================================================================================
212 // function : getIdlFile()
213 // purpose  : gets IDL file name entered
214 //=================================================================================
215 QString ToolsGUI_CatalogGeneratorDlg::getIdlFile()
216 {
217   return myIdlEdit->text().stripWhiteSpace();
218 }
219
220 //=================================================================================
221 // function : getXmlFile()
222 // purpose  : gets XML file name entered
223 //=================================================================================
224 QString ToolsGUI_CatalogGeneratorDlg::getXmlFile()
225 {
226   return myXmlEdit->text().stripWhiteSpace();
227 }
228
229 //=================================================================================
230 // function : getPngFile()
231 // purpose  : gets PNG file name entered
232 //=================================================================================
233 QString ToolsGUI_CatalogGeneratorDlg::getPngFile()
234 {
235   return myPngEdit->text().stripWhiteSpace();
236 }
237
238 //=================================================================================
239 // function : getAuthor()
240 // purpose  : gets author 
241 //=================================================================================
242 QString ToolsGUI_CatalogGeneratorDlg::getAuthor()
243 {
244   return myAuthorEdit->text().stripWhiteSpace();
245 }
246
247 //=================================================================================
248 // function : getVersion()
249 // purpose  : gets version number 
250 //=================================================================================
251 QString ToolsGUI_CatalogGeneratorDlg::getVersion()
252 {
253   return myVersionEdit->text().stripWhiteSpace();
254 }
255
256 //=================================================================================
257 // function : getCompName()
258 // purpose  : gets name of the component
259 //=================================================================================
260 QString ToolsGUI_CatalogGeneratorDlg::getCompName()
261 {
262   return myCompName->text().stripWhiteSpace();
263 }
264
265 //=================================================================================
266 // function : getCompUserName()
267 // purpose  : gets username of the component
268 //=================================================================================
269 QString ToolsGUI_CatalogGeneratorDlg::getCompUserName()
270 {
271   return myCompUserName->text().stripWhiteSpace();
272 }
273
274 //=================================================================================
275 // function : getCompType()
276 // purpose  : gets type of the component
277 //=================================================================================
278 QString ToolsGUI_CatalogGeneratorDlg::getCompMultiStd()
279 {
280   return myCompMultiStd->text().stripWhiteSpace();
281 }
282 //=================================================================================
283 // function : getComptype()
284 // purpose  : gets type of the component
285 //=================================================================================
286 QString ToolsGUI_CatalogGeneratorDlg::getCompType()
287 {
288   return myCompType->text().stripWhiteSpace();
289 }
290
291 //=================================================================================
292 // function : getIdlPath()
293 // purpose  : gets IDL path of modules
294 //=================================================================================
295 QString ToolsGUI_CatalogGeneratorDlg::getIdlPath()
296 {
297   SALOME_ModuleCatalog::ModuleCatalog_var aCatalog = 
298     SALOME_ModuleCatalog::ModuleCatalog::_narrow( QAD_Application::getDesktop()->getCatalogue());
299
300   SALOME_ModuleCatalog::ListOfIAPP_Affich_var list_composants =
301     aCatalog->GetComponentIconeList();
302
303   QString IDLpath = "";
304
305   for (unsigned int ind = 0; ind < list_composants->length();ind++) {
306     QString modulename = CORBA::string_dup(list_composants[ind].modulename) ;
307     
308     QCString dir;
309     if (dir = getenv( modulename + "_ROOT_DIR")) {
310       IDLpath = IDLpath + "-I" + QAD_Tools::addSlash( QAD_Tools::addSlash(dir) + 
311                                                       QAD_Tools::addSlash("idl") + 
312                                                       QAD_Tools::addSlash("salome")) + " ";
313     }
314   }
315
316   //  MESSAGE ( " IDLpath = " << IDLpath);
317
318   return IDLpath;
319 }
320
321 //=================================================================================
322 // function : onBrowseBtnClicked()
323 // purpose  : <...> (Browse) buttons slot
324 //=================================================================================
325 void ToolsGUI_CatalogGeneratorDlg::onBrowseBtnClicked()
326 {
327   QPushButton* send = (QPushButton*)sender();
328  
329   if ( send == myBrowseIdlBtn ) {
330     QString file = myIdlEdit->text().stripWhiteSpace();
331     file = QAD_FileDlg::getFileName(QAD_Application::getDesktop(), 
332                                     file,
333                                     tr("TOOLS_MEN_IMPORT_IDL"),
334                                     tr("TOOLS_MEN_IMPORT"),
335                                     true);
336     if ( !file.isEmpty() ) {
337       myIdlEdit->setText(file);
338     }
339   } 
340   else if ( send == myBrowseXmlBtn ) {
341     QString file = QAD_FileDlg::getFileName(QAD_Application::getDesktop(), 
342                                             myXmlEdit->text().stripWhiteSpace(),
343                                             tr("TOOLS_MEN_EXPORT_XML"),
344                                             tr("TOOLS_MEN_EXPORT"),
345                                             false);
346     if ( !file.isEmpty() ) {
347       myXmlEdit->setText(file);
348     }
349   } else if ( send == myBrowsePngBtn ) {
350     QString file = QAD_FileDlg::getFileName(QAD_Application::getDesktop(), 
351                                             myXmlEdit->text().stripWhiteSpace(),
352                                             tr("TOOLS_MEN_IMPORT_PNG"),
353                                             tr("TOOLS_MEN_IMPORT"),
354                                             true);
355     if ( !file.isEmpty() ) {
356       myPngEdit->setText(file);
357     
358     }
359   }
360   updateButtonState();
361 }
362
363 //=================================================================================
364 // function : updateButtonState()
365 // purpose  : Updates <OK> button's state
366 //=================================================================================
367 void ToolsGUI_CatalogGeneratorDlg::updateButtonState()
368 {
369   myApplyBtn->setEnabled( !myIdlEdit->text().stripWhiteSpace().isEmpty() && 
370                           !myXmlEdit->text().stripWhiteSpace().isEmpty() );
371 }
372
373 //=================================================================================
374 // function : onApply()
375 // purpose  : <Apply> button slot, performs IDL->XML conversion
376 //=================================================================================
377 void ToolsGUI_CatalogGeneratorDlg::onApply()
378 {
379   QString IDLpath = getIdlPath();
380   QString XmlFile = getXmlFile();
381   QString IdlFile = getIdlFile();
382   QString Author  = getAuthor();
383   QString Version = getVersion();
384   QString PngFile = getPngFile();
385   QString CompName = getCompName(); //gets component name 
386   QString CompUserName = getCompUserName(); //gets component username 
387   QString CompType = getCompType(); //gets component type
388   QString CompMultiStd = getCompMultiStd();
389
390   if ( !XmlFile.isEmpty() && !IdlFile.isEmpty() ) {
391     if ( !QFile::exists( IdlFile ) ) {
392       QAD_MessageBox::error1( this, 
393                               tr("TOOLS_ERR_ERROR"), 
394                               tr("TOOLS_ERR_FILE_NOT_EXIST").arg(IdlFile), 
395                               tr ("TOOLS_BUT_OK") );
396     }
397     else {
398       QString command = "";
399       if ( getenv("KERNEL_ROOT_DIR")  )
400         command = QString( getenv( "KERNEL_ROOT_DIR" ) ) + "/bin/salome/runIDLparser -K " + IDLpath + " -Wbcatalog=" + XmlFile;
401       else {
402         QAD_MessageBox::error1( this, 
403                                 tr("TOOLS_ERR_ERROR"), 
404                                 tr("KERNEL_ROOT_DIR variable is not defined"), 
405                                 tr("TOOLS_BUT_OK") );
406       }
407
408       if (!Author.isEmpty()) command += ",author=" + Author; 
409       if (!Version.isEmpty()) command += ",version=" + Version;
410       if (!PngFile.isEmpty()) {
411         OSD_Path aPath((Standard_CString)PngFile.latin1()); 
412         TCollection_AsciiString aFile = aPath.Name() + aPath.Extension();
413         command += QString(",icon=") + QString(aFile.ToCString());
414       }
415       if (!CompName.isEmpty()) command += ",name=" + CompName;
416       if (!CompUserName.isEmpty()) command += ",username=" + CompUserName;
417       if (!CompType.isEmpty()) command += ",type=" + CompType;
418       if (!CompMultiStd.isEmpty()) command += ",multistudy=" + CompMultiStd;
419       command += " " + IdlFile;
420       MESSAGE( "shell command is : " << command );
421       int res;
422       res = system( ( char* )( command.latin1() ) );
423       if ( res == -1 ) {
424         MESSAGE( "work failed (system command result = " << res );
425       } else if (res == 217) {
426         MESSAGE( "shell exec failed (system command result = " << res );
427       }
428     }
429   }
430 }