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