1 // SALOME SALOMEGUI : implementation of desktop and GUI kernel
3 // Copyright (C) 2003 CEA/DEN, EDF R&D
7 // File : QAD_FileDlg.cxx
12 #include <qapplication.h>
15 #include <qobjectlist.h>
18 #include "QAD_Config.h"
19 #include "QAD_Desktop.h"
20 #include "QAD_FileDlg.h"
21 #include "QAD_MessageBox.h"
22 #include "QAD_Tools.h"
25 #define MIN_COMBO_SIZE 100
27 QString QAD_FileDlg::myLastVisitedPath;
32 QAD_FileDlg::QAD_FileDlg( QWidget* parent, bool open, bool showQuickDir, bool modal ) :
33 QFileDialogP( parent, 0, modal ),
39 setIcon( *parent->icon() );
40 setSizeGripEnabled( true );
43 // inserting quick dir combo box
44 QLabel* lab = new QLabel(tr("QUICK_PATH_LAB"), this);
45 myQuickCombo = new QComboBox(false, this);
46 myQuickCombo->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
47 myQuickCombo->setMinimumSize(MIN_COMBO_SIZE, 0);
49 myQuickButton = new QPushButton(tr("ADD_PATH_BTN"), this);
51 connect(myQuickCombo, SIGNAL(activated(const QString&)), this, SLOT(quickDir(const QString&)));
52 connect(myQuickButton, SIGNAL(clicked()), this, SLOT(addQuickDir()));
53 addWidgets(lab, myQuickCombo, myQuickButton);
55 // getting dir list from settings
56 QString dirs = QAD_CONFIG->getSetting("FileDlg:QuickDirList");
57 QStringList dirList = QStringList::split(';', dirs, false);
58 if (dirList.count() > 0) {
59 for (unsigned i = 0; i < dirList.count(); i++)
60 myQuickCombo->insertItem(dirList[i]);
63 myQuickCombo->insertItem(QDir::homeDirPath());
66 // the following is a workaround for proper layouting of custom widgets ===========
67 QValueList<QPushButton*> buttonList;
68 QValueList<QLabel*> labelList;
69 const QObjectList *list = children();
70 QObjectListIt it(*list);
71 int maxButWidth = lab->sizeHint().width();
72 int maxLabWidth = myQuickButton->sizeHint().width();
74 for (; it.current() ; ++it) {
75 if ( it.current()->isA( "QLabel" ) ) {
76 int tempW = ((QLabel*)it.current())->minimumWidth();
77 if ( maxLabWidth < tempW ) maxLabWidth = tempW;
78 labelList.append( (QLabel*)it.current() );
80 else if( it.current()->isA("QPushButton") ) {
81 int tempW = ((QPushButton*)it.current())->minimumWidth();
82 if ( maxButWidth < tempW ) maxButWidth = tempW;
83 buttonList.append( (QPushButton*)it.current() );
86 if (maxButWidth > 0) {
87 QValueList<QPushButton*>::Iterator bListIt;
88 for ( bListIt = buttonList.begin(); bListIt != buttonList.end(); ++bListIt )
89 (*bListIt)->setFixedWidth( maxButWidth );
91 if (maxLabWidth > 0) {
92 QValueList<QLabel*>::Iterator lListIt;
93 for ( lListIt = labelList.begin(); lListIt != labelList.end(); ++lListIt )
94 (*lListIt)->setFixedWidth( maxLabWidth );
96 // ================================================================================
98 setMode( myOpen ? ExistingFile : AnyFile );
99 setCaption( myOpen ? tr( "INF_DESK_DOC_OPEN" ) : tr( "INF_DESK_DOC_SAVE" ) );
100 if (myLastVisitedPath.isNull() || myLastVisitedPath.isEmpty()) {
101 // If no last visited path exists -> switch to the first preferred path
102 processPath(myQuickCombo->text(0));
104 else if ( !processPath(myLastVisitedPath) ) {
105 // If last visited path doesn't exist -> switch to the first preferred path
106 processPath(myQuickCombo->text(0));
108 myValidator = new QAD_FileValidator(this);
115 QAD_FileDlg::~QAD_FileDlg()
120 Sets validator for file names to open/save
121 Deletes previous validator
123 void QAD_FileDlg::setValidator( QAD_FileValidator* v )
131 Returns the selected file
133 QString QAD_FileDlg::selectedFile() const
135 return mySelectedFile;
139 Returns 'true' if this is 'Open File' dialog
140 and 'false' if 'Save File' dialog
142 bool QAD_FileDlg::isOpenDlg() const
148 Closes this dialog and sets the return code to 'Accepted'
149 if the selected name is valid ( see 'acceptData()' )
151 void QAD_FileDlg::accept()
153 // mySelectedFile = QFileDialog::selectedFile().simplifyWhiteSpace(); //VSR- 06/12/02
154 if ( mode() != ExistingFiles ) {
155 mySelectedFile = QFileDialogP::selectedFile(); //VSR+ 06/12/02
158 // mySelectedFile = mySelectedFile.simplifyWhiteSpace(); //VSR- 06/12/02
160 /* Qt 2.2.2 BUG: accept() is called twice if you validate
161 the selected file name by pressing 'Return' key in file
162 name editor but this name is not acceptable for acceptData()
164 if ( acceptData() ) {
165 myLastVisitedPath = dirPath();
166 QFileDialogP::accept();
171 Closes this dialog and sets the return code to 'Rejected'
173 void QAD_FileDlg::reject()
175 mySelectedFile = QString::null;
176 QFileDialogP::reject();
180 Returns 'true' if selected file is valid.
181 The validity is checked by a file validator,
182 if there is no validator the file is always
185 bool QAD_FileDlg::acceptData()
190 if ( mode() == ExistingFiles ) {
191 QStringList fileNames = selectedFiles();
192 for ( int i = 0; i < fileNames.count(); i++ ) {
193 if ( !myValidator->canOpen( fileNames[i] ) )
199 return myValidator->canOpen( selectedFile() );
202 return myValidator->canSave( selectedFile() );
208 Adds an extension to the selected file name
209 if the file has not it.
210 The extension is extracted from the active filter.
212 void QAD_FileDlg::addExtension()
214 // mySelectedFile.stripWhiteSpace();//VSR- 06/12/02
215 // if ( mySelectedFile.isEmpty() )//VSR- 06/12/02
216 if ( mySelectedFile.stripWhiteSpace().isEmpty() )//VSR+ 06/12/02
219 // if ( QAD_Tools::getFileExtensionFromPath( mySelectedFile ).isEmpty() ) //VSR- 06/12/02
220 //ota : 16/12/03 if ( QAD_Tools::getFileExtensionFromPath( mySelectedFile ).isEmpty() ) //VSR+ 06/12/02
223 #if QT_VERSION < 0x030000
224 QRegExp r( QString::fromLatin1("([a-zA-Z0-9.*? +;#]*)$") );
225 int len, index = r.match( selectedFilter(), 0, &len );
227 QRegExp r( QString::fromLatin1("\\([a-zA-Z0-9.*? +;#]*\\)$") );
228 int index = r.search(selectedFilter());
232 #if QT_VERSION < 0x030000
233 // QString wildcard = selectedFilter().mid( index + 1, len-2 ); //VSR- 06/12/02
234 QString wildcard = selectedFilter().mid( index + 1, len-2 ).stripWhiteSpace(); //VSR+ 06/12/02
236 // QString wildcard = selectedFilter().mid( index + 1, r.matchedLength()-2 ); //VSR- 06/12/02
237 QString wildcard = selectedFilter().mid( index + 1, r.matchedLength()-2 ).stripWhiteSpace(); //VSR+ 06/12/02
239 if ( mySelectedFile[mySelectedFile.length() - 1] == '.')
240 //if the file name ends with the point remove it
241 mySelectedFile.truncate(mySelectedFile.length() - 1);
242 QString anExt = "." + QAD_Tools::getFileExtensionFromPath( mySelectedFile ).stripWhiteSpace();
243 // From the filters list make a pattern to validate a file extension
244 // Due to transformations from the filter list (*.txt *.*xx *.c++ QAD*.* ) we
245 // will have the pattern (\.txt|\..*xx|\.c\+\+|\..*) (as we validate extension only we remove
246 // stay extension mask only in the pattern
247 QString aPattern(wildcard);
248 QRegExp anExtRExp("("+aPattern.replace(QRegExp("(^| )[0-9a-zA-Z*_?]*\\."), " \\.").
249 stripWhiteSpace().replace(QRegExp("\\s+"), "|").
250 replace(QRegExp("[*]"),".*").replace(QRegExp("[+]"),"\\+") + ")");
252 if ( anExtRExp.match(anExt) == -1 ) //if a selected file extension does not match to filter's list
253 { //remove a point if it is at the word end
254 if (anExt[ anExt.length() - 1 ] == '.') anExt.truncate( anExt.length() - 1 );
255 index = wildcard.findRev( '.' );
257 mySelectedFile += wildcard.mid( index ); //add the extension
264 Processes selection : tries to set given path or filename as selection
266 bool QAD_FileDlg::processPath( const QString& path )
268 if ( !path.isNull() ) {
269 QFileInfo fi( path );
272 setSelection( path );
273 else if ( fi.isDir() )
278 if ( QFileInfo( fi.dirPath() ).exists() ) {
279 setDir( fi.dirPath() );
280 setSelection( path );
288 Called when user selects item from "Quick Dir" combo box
290 void QAD_FileDlg::quickDir(const QString& dirPath)
292 if ( !QDir(dirPath).exists() ) {
293 QAD_MessageBox::error1(this,
295 tr("ERR_DIR_NOT_EXIST").arg(dirPath),
300 processPath(dirPath);
304 Called when user presses "Add" button - adds current directory to quick directory
305 list and to the preferences
307 void QAD_FileDlg::addQuickDir()
309 QString dp = dirPath();
310 if ( !dp.isEmpty() ) {
312 // getting dir list from settings
313 QString dirs = QAD_CONFIG->getSetting("FileDlg:QuickDirList");
314 QStringList dirList = QStringList::split(';', dirs, false);
316 bool emptyAndHome = false;
317 if ( dirList.count() > 0 ) {
318 for ( unsigned i = 0; i < dirList.count(); i++ ) {
319 QDir aDir( dirList[i] );
320 if ( aDir.canonicalPath().isNull() && dirList[i] == dir.absPath() ||
321 !aDir.canonicalPath().isNull() && aDir.exists() && aDir.canonicalPath() == dir.canonicalPath() ) {
328 emptyAndHome = dir.canonicalPath() == QDir(QDir::homeDirPath()).canonicalPath();
331 dirList.append( dp );
332 QAD_CONFIG->addSetting("FileDlg:QuickDirList", dirList.join(";"));
334 myQuickCombo->insertItem( dp );
339 Returns the file name for Open/Save [ static ]
341 QString QAD_FileDlg::getFileName( QWidget* parent,
342 const QString& initial,
343 const QStringList& filters,
344 const QString& caption,
347 QAD_FileValidator* validator )
349 QAD_FileDlg* fd = new QAD_FileDlg( parent, open, showQuickDir, true );
350 if ( !caption.isEmpty() )
351 fd->setCaption( caption );
352 if ( !initial.isEmpty() ) {
353 fd->processPath( initial ); // VSR 24/03/03 check for existing of directory has been added to avoid QFileDialog's bug
356 fd->setFilters( filters );
359 fd->setValidator( validator );
361 QString filename = fd->selectedFile();
363 qApp->processEvents();
369 Returns the list of files to be opened [ static ]
371 QStringList QAD_FileDlg::getOpenFileNames( QWidget* parent,
372 const QString& initial,
373 const QStringList& filters,
374 const QString& caption,
376 QAD_FileValidator* validator )
378 QAD_FileDlg* fd = new QAD_FileDlg( parent, true, showQuickDir, true );
379 fd->setMode( ExistingFiles );
380 if ( !caption.isEmpty() )
381 fd->setCaption( caption );
382 if ( !initial.isEmpty() ) {
383 fd->processPath( initial ); // VSR 24/03/03 check for existing of directory has been added to avoid QFileDialog's bug
385 fd->setFilters( filters );
387 fd->setValidator( validator );
389 QStringList filenames = fd->selectedFiles();
391 qApp->processEvents();
396 Existing directory selection dialog [ static ]
398 QString QAD_FileDlg::getExistingDirectory ( QWidget* parent,
399 const QString& initial,
400 const QString& caption,
403 QAD_FileDlg* fd = new QAD_FileDlg( parent, true, showQuickDir, true);
404 if ( !caption.isEmpty() )
405 fd->setCaption( caption );
406 if ( !initial.isEmpty() ) {
407 fd->processPath( initial ); // VSR 24/03/03 check for existing of directory has been added to avoid QFileDialog's bug
409 fd->setMode( DirectoryOnly );
410 fd->setFilters(tr("DIRECTORIES_FILTER"));
413 QString dirname = fd->selectedFile();
415 qApp->processEvents();