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