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