]> SALOME platform Git repositories - modules/gui.git/blob - src/SUIT/SUIT_FileDlg.cxx
Salome HOME
a3cb690f339196b6b9bf9aa4a7591ffdd56bfa49
[modules/gui.git] / src / SUIT / SUIT_FileDlg.cxx
1 //  Copyright (C) 2007-2008  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.
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 // File   : SUIT_FileDlg.cxx
23 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
24 //
25 /*!
26   \class SUIT_FileDlg
27   \brief An extension of the Qt Open/Save file dialog box.
28
29   The class SUIT_FileDlg provides a set of static methods which canbe used
30   for file or directories selection:
31   - getFileName() for single file opening or saving
32   - getOpenFileNames() for mulktiple files opening
33   - getExistingDirectory() for existing directory selection
34
35   Examples:
36   \code
37   // select file to dump contents of the view
38   QStringList filters;
39   filters << "Image files (*.bmp *.gif *.jpg )" << "All files (*)";
40   QString fileName = SUIT_FileDlg::getFileName( desktop(), 
41                                                 QString(), 
42                                                 filters, 
43                                                 "Dump view",
44                                                 false );
45   if ( !fileName.isEmpty() ) {
46     ... writing image to the file 
47   }
48
49   // select list of files to open in the editor windows
50   QStringList filters;
51   filters << "*.cpp | *.cxx | *.c++" << "*.h | *.hpp | *.hxx";
52   QStringList fileNames = SUIT_FileDlg::getOpenFileName( desktop(),
53                                                          QString(), 
54                                                          filters, 
55                                                          QString() );
56   if ( !fileNames.isEmpty() ) {
57     ... open files
58   }
59   \endcode
60
61   The class SUIT_FileDlg can be subclassed to implement custom file 
62   dialog boxes. The class provides a set of methods which can be used
63   in subclasses:
64   - setCheckPermissions() - to enable/disable check of files/directories
65     permissions
66   - setValidator() - to use custom file validator
67   - addWidgets() - to add custom widgets to the lower part of the 
68     dialog box
69   - getLastVisitedDirectory() - to get last visited directory
70   - acceptData() - can be used ti customize user selection validation
71
72   \sa SUIT_FileValidator class.
73 */
74
75 #include "SUIT_FileDlg.h"
76
77 #include "SUIT_Tools.h"   
78 #include "SUIT_Session.h"
79 #include "SUIT_MessageBox.h"
80 #include "SUIT_ResourceMgr.h"
81 #include "SUIT_FileValidator.h"
82
83 #include <QDir>
84 #include <QEvent>
85 #include <QRegExp>
86 #include <QLabel>
87 #include <QComboBox>
88 #include <QPushButton>
89 #include <QGridLayout>
90 #include <QApplication>
91 #include <QListView>
92 #include <QLineEdit>
93
94 /*!
95   \brief Defines extension behavior.
96
97   If the selected file name has extension which does not match the selected filter
98   and this variable is set to \c true, the file extension is ignored and new one
99   (from current file filter will be added.
100   \sa addExtension()
101 */
102 const bool IGNORE_NON_MATCHING_EXTENSION = true;
103
104 QString SUIT_FileDlg::myLastVisitedPath;
105
106 /*!
107   \brief Constructor.
108   \param parent parent widget
109   \param open if \c true dialog box is used for file opening, otherwise - for saving
110   \param showQuickDir if \c true the quick directory list widgets will be shown
111   \param modal if \c true the dialog box will be modal
112 */
113 SUIT_FileDlg::SUIT_FileDlg( QWidget* parent, bool open, bool showQuickDir, bool modal )
114 : QFileDialog( parent ),
115   myValidator( 0 ),
116   myQuickLab( 0 ),
117   myQuickCombo( 0 ),
118   myQuickButton( 0 ),
119   myCheckPermissions( true )
120 {
121   setModal( modal );
122   setSizeGripEnabled( true );
123   if ( parent )
124     setWindowIcon( parent->windowIcon() );
125
126   // add quick directories widgets
127   if ( showQuickDir ) {
128     myQuickLab    = new QLabel( tr( "LAB_QUICK_PATH" ), this );
129     myQuickCombo  = new QComboBox( this );
130     myQuickButton = new QPushButton( tr( "BUT_ADD_PATH" ), this );
131     
132     if ( addWidgets( myQuickLab, myQuickCombo, myQuickButton ) ) {
133       connect( myQuickCombo,  SIGNAL( activated( const QString& ) ), this, SLOT( quickDir( const QString& ) ) );
134       connect( myQuickButton, SIGNAL( clicked() ),                   this, SLOT( addQuickDir() ) );
135
136       // retrieve directories list from the resources
137       QStringList dirList;
138   
139       SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
140       if ( resMgr )
141         dirList = resMgr->stringValue( "FileDlg", QString( "QuickDirList" ) ).split( ';', QString::SkipEmptyParts );
142
143       if ( dirList.isEmpty() ) 
144         dirList << QDir::homePath();
145
146       for ( int i = 0; i < dirList.count(); i++ )
147         myQuickCombo->addItem( dirList[i] );
148     }
149     else {
150       delete myQuickLab;    myQuickLab = 0;
151       delete myQuickCombo;  myQuickCombo = 0;
152       delete myQuickButton; myQuickButton = 0;
153     }
154   }
155
156   setAcceptMode( open ? AcceptOpen: AcceptSave );
157   setWindowTitle( open ? tr( "INF_DESK_DOC_OPEN" ) : tr( "INF_DESK_DOC_SAVE" ) );
158
159   // If last visited path doesn't exist -> switch to the first preferred path
160   if ( !myLastVisitedPath.isEmpty() ) {
161     if ( !processPath( myLastVisitedPath ) && showQuickDir )
162       processPath( myQuickCombo->itemText( 0 ) );
163   }
164   else if ( showQuickDir ) {
165     processPath( myQuickCombo->itemText( 0 ) );
166   }
167
168   // set default file validator
169   myValidator = new SUIT_FileValidator( this );
170 }
171
172 /*!
173   \brief Destructor.
174 */
175 SUIT_FileDlg::~SUIT_FileDlg() 
176 {
177   setValidator( 0 );
178 }
179
180
181 /*! 
182   \brief Check if the dialog box is used for opening or saving the file.
183   \return \c true if dialog is used for file opening and \c false otherwise
184 */
185 bool SUIT_FileDlg::isOpenDlg() const
186 {
187   return acceptMode() == AcceptOpen;
188 }
189
190 /*!
191   \brief Get 'check file permissions' flag.
192   \return flag value
193   \sa setCheckPermissions()
194 */
195 bool SUIT_FileDlg::checkPermissions() const
196 {
197   return myCheckPermissions;
198 }
199
200 /*!
201   \brief Set 'check file permissions' flag.
202  
203   If this flag is set and file validator is not null,
204   the validator will check the file permissions also.
205
206   \param checkPerm new flag value
207   \sa checkPermissions()
208 */
209 void SUIT_FileDlg::setCheckPermissions( const bool checkPerm )
210 {
211   myCheckPermissions = checkPerm;
212 }
213
214 /*!
215   \brief Get file validator.
216   \return current file validator
217   \sa setValidator()
218 */
219 SUIT_FileValidator* SUIT_FileDlg::validator() const
220 {
221   return myValidator;
222 }
223
224 /*!
225   \brief Set file validator.
226  
227   Destroys previous validator if the dialog owns it.
228
229   \param v new file validator
230   \sa validator()
231 */
232 void SUIT_FileDlg::setValidator( SUIT_FileValidator* v )
233 {
234   if ( myValidator && myValidator->parent() == this )
235     delete myValidator;
236   myValidator = v;
237 }
238
239 /*!
240   \brief Adds the specified widgets to the bottom of the file dialog. 
241   
242   The first widget (usually label) \a l is placed underneath the "file name" 
243   and the "file types" labels. 
244   The widget \a w is placed underneath the file types combobox.
245   The last widget (usually button) \a b is placed underneath the Cancel push button. 
246
247   In general, the widgets can be arbitrary. This method is added to support 
248   the functionality provided by the Qt series 3.x.
249
250   If you don't want to have one of the widgets added, pass 0 in that widget's position. 
251   Every time this function is called, a new row of widgets is added to the bottom of the 
252   file dialog. 
253
254   \param l first widget (e.g. text label)
255   \param w second widget (e.g. combo box)
256   \param b third widget (e.g. push button)
257   \return \c true if widgets have been added successfully
258 */
259 bool SUIT_FileDlg::addWidgets( QWidget* l, QWidget* w, QWidget* b )
260 {
261   QGridLayout* grid = ::qobject_cast<QGridLayout*>( layout() );
262   if ( grid ) {
263     int row = grid->rowCount();
264     int columns = grid->columnCount();
265     if ( l ) 
266       grid->addWidget( l, row, 0 );
267     if ( w )
268       grid->addWidget( w, row, 1, 1, columns-2 );
269     if ( b )
270       grid->addWidget( b, row, columns-1 );
271     return true;
272   }
273   return false;
274 }
275
276 /*!
277   \brief Get list of selected files.
278   \return selected file names
279 */
280 QStringList SUIT_FileDlg::selectedFiles() const
281 {
282   QStringList files = QFileDialog::selectedFiles();
283   if ( fileMode() != DirectoryOnly && fileMode() != Directory ) {
284     QMutableListIterator<QString> it( files );
285     while ( it.hasNext() ) {
286       QString f = it.next();
287       QFileInfo finfo( f );
288       if ( !finfo.isDir() )
289         it.setValue( addExtension( f ) );
290     }
291   }
292   return files;
293 }
294
295 /*!
296   \brief Get selected file.
297   \return selected file name or null string if file is not selected
298 */
299 QString SUIT_FileDlg::selectedFile() const
300 {
301   QStringList files = selectedFiles();
302   return files.count() > 0 ? files[0] : QString();
303 }
304
305 /*!
306   \brief Get last visited directory.
307
308   Note, that last visited path is memorized only if the 
309   dialog box is accepted.
310
311   \return last visited directory
312 */
313 QString SUIT_FileDlg::getLastVisitedDirectory()
314 {
315   return myLastVisitedPath;
316 }
317
318 /*!
319   \brief Customize events processing.
320   \param e event
321   \return \c true if the event e was recognized and processed
322 */
323 bool SUIT_FileDlg::event( QEvent* e )
324 {
325   bool res = QFileDialog::event( e );
326
327   if ( e->type() == QEvent::Polish )
328     polish();
329
330   return res;
331 }
332
333 /*!
334   \brief Get line edit which is used to enter file name.
335   \return line edit widget or0 if it could not be found
336 */
337 QLineEdit* SUIT_FileDlg::lineEdit() const
338 {
339   QLineEdit* ebox = 0;
340   QList<QLineEdit*> editBoxes = findChildren<QLineEdit*>();
341   QGridLayout* grid = ::qobject_cast<QGridLayout*>( layout() );
342   if ( grid ) {
343     int idx = 10000;
344     for ( int i = 0; i < editBoxes.count(); i++ ) {
345       int widx = grid->indexOf( editBoxes[ i ] );
346       if ( widx >= 0 )
347         idx = qMin( idx, widx );
348     }
349     if ( grid->itemAt( idx )  )
350       ebox = qobject_cast<QLineEdit*>( grid->itemAt( idx )->widget() );
351   }
352   return ebox;
353 }
354
355 /*! 
356   \brief Validate user selection.
357
358   The validation is done by calling the corresponding methods
359   of the validator. If the validator is not set, this method
360   always returns \c true.
361
362   This method can be re-implemented in the subclasses to customize
363   the file dialog behavior.
364   Another solution could be implementing own file validator class.
365
366   \return \c true if user selection (file(s) or directory) is valid
367   \sa SUIT_FileValidator class, validator(), setValidator()
368 */
369 bool SUIT_FileDlg::acceptData()
370 {    
371   QStringList files = selectedFiles();
372   if ( files.isEmpty() )
373     return false;
374
375   // special case for ".."
376   if ( lineEdit() ) {
377     QString txt = lineEdit()->text();
378     if ( txt == ".." ) {
379       QDir dir = directory();
380       if ( dir.cdUp() ) {
381         setDirectory( dir );
382         bool block = lineEdit()->blockSignals( true );
383         lineEdit()->setText( ".." );
384         lineEdit()->selectAll();
385         lineEdit()->setFocus( Qt::OtherFocusReason );
386         lineEdit()->blockSignals( block );
387         return false;
388       }
389     }
390     else if ( fileMode() != DirectoryOnly ) {
391       QStringList fs = txt.split( " ", QString::SkipEmptyParts );
392       for ( int i = 0; i < fs.count(); i++ ) {
393         QString wc = fs.at( i );
394         if ( wc.startsWith( "\"" ) && wc.endsWith( "\"" ) )
395           wc = wc.mid( 1, wc.length()-2 );
396         if ( hasWildCards( wc ) ) {
397           addFilter( wc );
398           lineEdit()->clear();
399           return false;
400         }
401       }
402     }
403   }
404
405   // special case for wildcards
406   for ( int i = 0; i < files.count(); ++i ) {
407   }
408
409   bool bOk = true;
410
411   switch ( fileMode() ) {
412   case DirectoryOnly:
413   case Directory: 
414     {
415       QString fn = files.first();
416       if ( validator() ) {
417         bOk = isOpenDlg() ? validator()->canReadDir( fn, checkPermissions() ) : 
418                             validator()->canWriteDir( fn, checkPermissions() );
419       }
420       break;
421     }
422   case AnyFile: 
423     {
424       QString fn = files.first();
425       QFileInfo info( fn );
426       if ( info.isDir() ) {
427         setDirectory( info.absoluteFilePath() );
428         if ( lineEdit() ) {
429           lineEdit()->selectAll();
430           lineEdit()->setFocus( Qt::OtherFocusReason );
431         }
432         return false;
433       }
434       // validation is not required
435       if ( validator() ) {
436         bOk = isOpenDlg() ? validator()->canOpen( fn, checkPermissions() ) : 
437                             validator()->canSave( fn, checkPermissions() );
438       }
439       break;
440     }
441   case ExistingFile:
442   case ExistingFiles: 
443     {
444       for ( int i = 0; i < files.count(); ++i ) {
445         QFileInfo info( files.at( i ) );
446         if ( info.isDir() ) {
447           setDirectory( info.absoluteFilePath() );
448           if ( lineEdit() ) {
449             lineEdit()->selectAll();
450             lineEdit()->setFocus( Qt::OtherFocusReason );
451           }
452           return false;
453         }
454         if ( validator() ) {
455           bOk = isOpenDlg() ? validator()->canOpen( files.at( i ), checkPermissions() ) : 
456                               validator()->canSave( files.at( i ), checkPermissions() );
457         if ( !bOk )
458           return false;
459         }
460       }
461       break;
462     }
463   }
464
465   if ( bOk )
466     emit filesSelected( files );
467
468   return bOk;
469 }
470
471 /*!
472   \brief Add an extension to the specified file name.
473  
474   The extension is extracted from the active filter.
475
476   \param fileName file name to be processed
477   \return fileName with the extension added
478 */
479 QString SUIT_FileDlg::addExtension( const QString& fileName ) const
480 {
481   QString fname = fileName.trimmed();
482
483   // check if file name entered is empty
484   if ( fname.isEmpty() )
485     return fileName;
486
487   // current file extension
488   QString anExt = "." + SUIT_Tools::extension( fname ).trimmed();
489
490   // If the file already has extension and it does not match the filter there are two choices:
491   // - to leave it 'as is'
492   // - to ignore it
493   // The behavior is defined by IGNORE_NON_MATCHING_EXTENSION constant
494   if ( anExt != "." && !IGNORE_NON_MATCHING_EXTENSION )
495     return fileName;
496
497   QRegExp r( QString::fromLatin1("\\(?[a-zA-Z0-9.*? +;#|]*\\)?$") );
498   int index = r.indexIn( selectedFilter().trimmed() );
499
500   if ( index >= 0 ) {            
501     // Create wildcard regular expression basing on selected filter 
502     // in order to validate a file extension.
503     // Due to transformations from the filter list (*.txt *.*xx *.c++ SUIT*.* ) we 
504     // will have the pattern (\.txt|\..*xx|\.c\+\+|\..*) (as we validate extension only, 
505     // we remove everything except extension mask from the pattern
506     QString wildcard = selectedFilter().mid( index, r.matchedLength() ).trimmed();
507     // replace '|' and ';' separators by space symbol and also brackets if there are some
508     wildcard.replace( QRegExp( "[\\|;|(|)]" )," " ); 
509
510     QString aPattern = wildcard.replace( QRegExp( "(^| )(\\s*)[0-9a-zA-Z*_?]*\\."), " \\." ).trimmed().
511                                          replace( QRegExp( "\\s+" ), "|" ).replace( QRegExp( "[?]" ),".?" ).
512                                          replace( QRegExp( "[*]" ),".*" ).replace( QRegExp( "[+]" ),"\\+" );
513
514     // now we get the list of all extension masks and remove all which does not contain wildcard symbols
515     QStringList extList = aPattern.split( "|", QString::SkipEmptyParts );
516     for ( int i = extList.count() - 1; i >= 0; i-- ) {
517       if ( !extList[i].contains( "." ) )
518         extList.removeAt( i );
519     }
520     aPattern = extList.join( "|" );
521
522     // finalize pattern
523     QRegExp anExtRExp( "^("+ aPattern + ")$" );
524
525     // Check if the current file extension matches the pattern
526     if ( !anExtRExp.exactMatch( anExt ) ) {
527       // find first appropriate extension in the selected filter 
528       // (it should be without wildcard symbols)
529       for ( int i = 0; i < extList.count(); i++ ) {
530         QString newExt = extList[i].replace( QRegExp( "[\\\\][+]" ),"+" );
531         int res = newExt.lastIndexOf( '.' );
532         if ( res >= 0 )
533           newExt = newExt.mid( res + 1 );
534         if ( newExt.indexOf( QRegExp("[*|?]" ) ) < 0 ) {
535           fname += fname.endsWith( "." ) ? newExt : QString( "." ) + newExt;
536           return fname;
537         }
538       }
539     }
540   }
541   return fileName;
542 }
543
544 /*!
545   \brief Processes selection : tries to set specified sirectory or filename
546   as current file dialog selection.
547   \param path file or directory path
548   \return \c true if \a path is processed correctly and \c false otherwise
549 */
550 bool SUIT_FileDlg::processPath( const QString& path )
551 {
552   if ( !path.isNull() ) {
553     QFileInfo fi( path );
554     if ( fi.exists() ) {
555       if ( fi.isFile() )
556         selectFile( path );
557       else if ( fi.isDir() )
558         setDirectory( path );
559       return true;
560     }
561     else if ( QFileInfo( SUIT_Tools::dir( path ) ).exists() ) {
562       setDirectory( SUIT_Tools::dir( path ) );
563       selectFile( path );
564       return true;
565     }
566   }
567   return false;
568 }
569
570 /*!
571   \brief Add file filter and activates it.
572   \param filter new file filter
573 */
574 void SUIT_FileDlg::addFilter( const QString& filter )
575 {
576   QStringList flist = filters();
577   if ( !flist.contains( filter ) ) {
578     flist << filter;
579     setFilters( flist );
580   }
581   selectFilter( filter );
582 }
583
584 /*!
585   \brief Check if the string contains wildcard symbols.
586   \param s string to be checked (for example, file name)
587   \return \c true if string contains "*" or "?" symbols
588 */
589 bool SUIT_FileDlg::hasWildCards( const QString& s )
590 {
591   return s.contains( QRegExp("[*|?]") );
592 }
593
594 /*!
595   \brief Called when the user presses "Open"or "Save" button.
596
597   Verifies the user choice and closes dialog box, setting the return code to QDialog::Accepted
598
599   \sa acceptData()
600 */
601 void SUIT_FileDlg::accept()
602 {
603   if ( acceptData() ) {
604     myLastVisitedPath = directory().path();
605     QDialog::accept();        
606   }
607 }
608
609 /*!
610   \brief Called when user selects directory from the "Quick Dir" combo box.
611
612   Browses the file dialog to the specified directory (if it is valid).
613
614   \param dirPath selected directory
615 */
616 void SUIT_FileDlg::quickDir( const QString& dirPath )
617 {
618   if ( !QDir( dirPath ).exists() )
619     SUIT_MessageBox::critical( this, tr( "ERR_ERROR" ), tr( "ERR_DIR_NOT_EXIST" ).arg( dirPath ) );
620   else
621     processPath( dirPath );
622 }
623
624 /*!
625   \brief Called when user presses "Quick Dir Add" button.
626   
627   Adds current directory to the quick directories list and to the preferences.
628 */
629 void SUIT_FileDlg::addQuickDir()
630 {
631   QString dp = directory().path();
632   if ( !dp.isEmpty() ) {
633     QDir dir( dp );
634
635     QStringList dirList;
636
637     SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
638     if ( resMgr )
639       dirList = resMgr->stringValue( "FileDlg", QString( "QuickDirList" ) ).split( ';', QString::SkipEmptyParts );
640
641     bool found = false;
642     bool emptyAndHome = false;
643     if ( dirList.count() > 0 ) {
644       for ( int i = 0; i < dirList.count() && !found; i++ )  {
645         QDir aDir( dirList[i] );
646         if ( aDir.canonicalPath().isNull() && dirList[i] == dir.absolutePath() ||
647              !aDir.canonicalPath().isNull() && aDir.exists() && 
648              aDir.canonicalPath() == dir.canonicalPath() ) {
649           found = true;
650         }
651       }
652     }
653     else {
654       emptyAndHome = dir.canonicalPath() == QDir( QDir::homePath() ).canonicalPath();
655     }
656
657     if ( !found ) {
658       dirList.append( dp );
659       resMgr->setValue( "FileDlg", QString( "QuickDirList" ), dirList.join( ";" ) );
660       if ( !emptyAndHome )
661         myQuickCombo->addItem( dp );
662     }
663   }
664 }
665
666 /*!
667   \brief Polish the dialog box.
668 */
669 void SUIT_FileDlg::polish()
670 {
671   QList<QPushButton*> buttons = findChildren<QPushButton*>();
672
673   int maxBtnWidth = 0;
674
675   for ( QList<QPushButton*>::const_iterator it = buttons.begin(); 
676         it != buttons.end(); ++it )
677     maxBtnWidth = qMax( maxBtnWidth, (*it)->sizeHint().width() );
678
679   for ( QList<QPushButton*>::const_iterator it = buttons.begin(); 
680         it != buttons.end(); ++it ) {
681     (*it)->setDefault( false );
682     (*it)->setAutoDefault( false );
683     (*it)->setFixedWidth( maxBtnWidth );
684   }
685
686   QList<QListView*> views = findChildren<QListView*>();
687   for ( QList<QListView*>::const_iterator it = views.begin(); 
688         it != views.end(); ++it ) {
689     (*it)->setViewMode( QListView::ListMode );
690   }
691 }
692
693 /*!
694   \brief Show dialog box for the file opening/saving.
695
696   This method can be used to select the file for opening
697   or saving. The behavior is defined by the \a open parameter.
698   Note, that selection validation depends on the dialog mode used.
699
700   If \a initial parameter is not null string it is used as starting directory
701   or file at which dialog box is opened.
702   
703   The parameter \a filters defines file filters (wildcards) to be used.
704   If filters list is empty, "All files (*)" is used by default.
705   
706   The parameter \a caption is used as dialog box title. If it is
707   is empty, the default title is used.
708   
709   The parameter \a showQuickDir specifies if it is necessary to 
710   show additional quick directories list controls in the bottom part
711   of the dialog box.
712
713   The validation of the user selection is done with help of the file 
714   validator (SUIT_FileValidator class). The last parameter \a validator
715   can be used to pass the custom file validator to the dialog box.
716   
717   \param parent parent widget
718   \param initial initial file (or directory) dialog box to be opened on
719   \param filters file filters list
720   \param caption dialog box title
721   \param open if \c true dialog box is used for file opening, otherwise - for saving
722   \param showQuickDir if \c true the quick directory list widgets will be shown
723   \param validator custom file validator
724   \return selected file name or null string if dialog box is cancelled
725   \sa getOpenFileNames(), getExistingDirectory()
726 */
727 QString SUIT_FileDlg::getFileName( QWidget* parent, const QString& initial, 
728                                    const QStringList& filters, const QString& caption, 
729                                    const bool open, const bool showQuickDir,
730                                    SUIT_FileValidator* validator )
731 {            
732   SUIT_FileDlg fd( parent, open, showQuickDir, true );    
733
734   fd.setFileMode( open ? ExistingFile : AnyFile );
735
736   QString tmpfilename = initial;
737   tmpfilename = tmpfilename.simplified();
738   tmpfilename = tmpfilename.replace(QRegExp("\\*"), "" ).replace(QRegExp("\\?"), "" );
739
740   if ( filters.isEmpty() )
741     fd.setFilter( tr( "ALL_FILES_FILTER" ) ); // All files (*)
742   else
743     fd.setFilters( filters );
744
745   if ( !caption.isEmpty() )
746     fd.setWindowTitle( caption );
747
748   if ( !tmpfilename.isEmpty() )
749     fd.processPath( tmpfilename );
750
751   if ( validator )
752     fd.setValidator( validator );
753
754   QString filename;
755
756   if ( fd.exec() == QDialog::Accepted )
757     filename = fd.selectedFile();
758
759   QApplication::processEvents();
760
761   return filename;
762 }
763
764 /*!
765   \brief Show dialog box for the file opening/saving.
766   \overload
767
768   This method can be used to select the file for opening
769   or saving. The behavior is defined by the \a open parameter.
770   Note, that selection validation depends on the dialog mode used.
771
772   If \a initial parameter is not null string it is used as starting directory
773   or file at which dialog box is opened.
774   
775   The parameter \a filters defines file filters (wildcards) to be used.
776   This is the list of wildcards, separated by the ";;" symbols.
777   If filters list is empty, "All files (*)" is used by default.
778   
779   The parameter \a caption is used as dialog box title. If it is
780   is empty, the default title is used.
781   
782   The parameter \a showQuickDir specifies if it is necessary to 
783   show additional quick directories list controls in the bottom part
784   of the dialog box.
785
786   The validation of the user selection is done with help of the file 
787   validator (SUIT_FileValidator class). The last parameter \a validator
788   can be used to pass the custom file validator to the dialog box.
789   
790   \param parent parent widget
791   \param initial initial file (or directory) dialog box to be opened on
792   \param filters file filters separated by ";;"
793   \param caption dialog box title
794   \param open if \c true dialog box is used for file opening, otherwise - for saving
795   \param showQuickDir if \c true the quick directory list widgets will be shown
796   \param validator custom file validator
797   \return selected file name or null string if dialog box is cancelled
798   \sa getOpenFileNames(), getExistingDirectory()
799 */
800 QString SUIT_FileDlg::getFileName( QWidget* parent, const QString& initial, 
801                                    const QString& filters, const QString& caption, 
802                                    const bool open, const bool showQuickDir,
803                                    SUIT_FileValidator* validator )
804 {
805   return getFileName( parent, initial, filters.split( ";;", QString::SkipEmptyParts ), 
806                       caption, open, showQuickDir, validator );
807 }
808
809 /*!
810   \brief Show dialog box for the multiple files selection.
811
812   If \a initial parameter is not null string it is used as starting directory
813   or file at which dialog box is opened.
814   
815   The parameter \a filters defines file filters (wildcards) to be used.
816   If filters list is empty, "All files (*)" is used by default.
817   
818   The parameter \a caption is used as dialog box title. If it is
819   is empty, the default title is used.
820   
821   The parameter \a showQuickDir specifies if it is necessary to 
822   show additional quick directories list controls in the bottom part
823   of the dialog box.
824
825   The validation of the user selection is done with help of the file 
826   validator (SUIT_FileValidator class). The last parameter \a validator
827   can be used to pass the custom file validator to the dialog box.
828   
829   \param parent parent widget
830   \param initial initial file (or directory) dialog box to be opened on
831   \param filters file filters list
832   \param caption dialog box title
833   \param showQuickDir if \c true the quick directory list widgets will be shown
834   \param validator custom file validator
835   \return selected file names or empty list if dialog box is cancelled
836   \sa getFileName(), getExistingDirectory()
837 */
838 QStringList SUIT_FileDlg::getOpenFileNames( QWidget* parent, const QString& initial,
839                                             const QStringList& filters, const QString& caption,
840                                             const bool showQuickDir, 
841                                             SUIT_FileValidator* validator )
842 {            
843   SUIT_FileDlg fd( parent, true, showQuickDir, true );
844
845   fd.setFileMode( ExistingFiles );
846
847   if ( filters.isEmpty() )
848     fd.setFilter( tr( "ALL_FILES_FILTER" ) ); // All files (*)
849   else
850     fd.setFilters( filters );
851
852   if ( !caption.isEmpty() )
853     fd.setWindowTitle( caption );
854
855   if ( !initial.isEmpty() )
856     fd.processPath( initial );
857
858   if ( validator )
859     fd.setValidator( validator );
860
861   QStringList filenames;
862
863   if ( fd.exec() == QDialog::Accepted )
864     filenames = fd.selectedFiles();
865
866   QApplication::processEvents();
867
868   return filenames;
869 }
870
871 /*!
872   \brief Show dialog box for the multiple file opening.
873   \overload
874
875   If \a initial parameter is not null string it is used as starting directory
876   or file at which dialog box is opened.
877   
878   The parameter \a filters defines file filters (wildcards) to be used.
879   This is the list of wildcards, separated by the ";;" symbols.
880   If filters list is empty, "All files (*)" is used by default.
881   
882   The parameter \a caption is used as dialog box title. If it is
883   is empty, the default title is used.
884   
885   The parameter \a showQuickDir specifies if it is necessary to 
886   show additional quick directories list controls in the bottom part
887   of the dialog box.
888
889   The validation of the user selection is done with help of the file 
890   validator (SUIT_FileValidator class). The last parameter \a validator
891   can be used to pass the custom file validator to the dialog box.
892   
893   \param parent parent widget
894   \param initial initial file (or directory) dialog box to be opened on
895   \param filters file filters separated by ";;"
896   \param caption dialog box title
897   \param showQuickDir if \c true the quick directory list widgets will be shown
898   \param validator custom file validator
899   \return selected file names or empty list if dialog box is cancelled
900   \sa getFileName(), getExistingDirectory()
901 */
902 QStringList SUIT_FileDlg::getOpenFileNames( QWidget* parent, const QString& initial,
903                                             const QString& filters, const QString& caption,
904                                             const bool showQuickDir,
905                                             SUIT_FileValidator* validator )
906 {
907   return getOpenFileNames( parent, initial, filters.split( ";;", QString::SkipEmptyParts ), 
908                            caption, showQuickDir, validator );
909 }
910
911 /*!
912   \brief Show dialog box for the existing directory selection.
913
914   If \a initial parameter is not null string it is used as starting directory
915   at which dialog box is opened.
916   
917   The parameter \a caption is used as dialog box title. If it is
918   is empty, the default title is used.
919   
920   The parameter \a showQuickDir specifies if it is necessary to 
921   show additional quick directories list controls in the bottom part
922   of the dialog box.
923
924   The validation of the user selection is done with help of the file 
925   validator (SUIT_FileValidator class). The last parameter \a validator
926   can be used to pass the custom file validator to the dialog box.
927   
928   \param parent parent widget
929   \param initial initial directory dialog box to be opened on
930   \param caption dialog box title
931   \param showQuickDir if \c true the quick directory list widgets will be shown
932   \param validator custom file validator
933   \return selected directory name or null string if dialog box is cancelled
934   \sa getFileName(), getOpenFileNames()
935 */
936 QString SUIT_FileDlg::getExistingDirectory( QWidget* parent, const QString& initial,
937                                             const QString& caption, const bool showQuickDir,
938                                             SUIT_FileValidator* validator )
939 {
940   SUIT_FileDlg fd( parent, true, showQuickDir, true );
941
942   fd.setFileMode( DirectoryOnly );
943
944   if ( !caption.isEmpty() )
945     fd.setWindowTitle( caption );
946
947   if ( !initial.isEmpty() )
948     fd.processPath( initial );
949   
950   if ( validator )
951     fd.setValidator( validator );
952
953   QString dirname;
954
955   if ( fd.exec() == QDialog::Accepted )
956     dirname = fd.selectedFile();
957
958   QApplication::processEvents();
959
960   return dirname;
961 }
962
963 /*!
964   \brief Get last visited path
965   \return last visited path
966 */
967 QString SUIT_FileDlg::getLastVisitedPath()
968 {
969   return myLastVisitedPath;
970 }
971
972 /*!
973   \brief Selects current file
974
975   This version of selectFile() methods works similar to Qt version 3.x:
976   it selects the given file as current and it changes the current file dialog's directory
977   to the directory of the file
978   
979   \param f - new current file name 
980 */
981 void SUIT_FileDlg::selectFile( const QString& f )
982 {
983   QFileDialog::selectFile( QFileInfo( f ).baseName() );
984   setDirectory( QFileInfo( f ).absoluteDir() );
985 }