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