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