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