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