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