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>
17 #include <qpushbutton.h>
19 #include "QAD_Config.h"
20 #include "QAD_Desktop.h"
21 #include "QAD_FileDlg.h"
22 #include "QAD_MessageBox.h"
23 #include "QAD_Tools.h"
26 #define MIN_COMBO_SIZE 100
28 QString QAD_FileDlg::myLastVisitedPath;
33 QAD_FileDlg::QAD_FileDlg( QWidget* parent, bool open, bool showQuickDir, bool modal ) :
34 QFileDialogP( parent, 0, modal ),
40 setIcon( *parent->icon() );
41 setSizeGripEnabled( true );
44 // inserting quick dir combo box
45 QLabel* lab = new QLabel(tr("Quick path:"), this);
46 myQuickCombo = new QComboBox(false, this);
47 myQuickCombo->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
48 myQuickCombo->setMinimumSize(MIN_COMBO_SIZE, 0);
50 // the following is a workaround for proper layouting of custom widgets ===========
51 QPushButton* btn = new QPushButton(this);
52 btn->setEnabled(false);
53 QPalette pal = btn->palette();
54 QColorGroup ca = pal.active();
55 ca.setColor(QColorGroup::Light, palette().active().background());
56 ca.setColor(QColorGroup::Midlight, palette().active().background());
57 ca.setColor(QColorGroup::Dark, palette().active().background());
58 ca.setColor(QColorGroup::Mid, palette().active().background());
59 ca.setColor(QColorGroup::Shadow, palette().active().background());
60 QColorGroup ci = pal.inactive();
61 ci.setColor(QColorGroup::Light, palette().inactive().background());
62 ci.setColor(QColorGroup::Midlight, palette().inactive().background());
63 ci.setColor(QColorGroup::Dark, palette().inactive().background());
64 ci.setColor(QColorGroup::Mid, palette().inactive().background());
65 ci.setColor(QColorGroup::Shadow, palette().inactive().background());
66 QColorGroup cd = pal.disabled();
67 cd.setColor(QColorGroup::Light, palette().disabled().background());
68 cd.setColor(QColorGroup::Midlight, palette().disabled().background());
69 cd.setColor(QColorGroup::Dark, palette().disabled().background());
70 cd.setColor(QColorGroup::Mid, palette().disabled().background());
71 cd.setColor(QColorGroup::Shadow, palette().disabled().background());
72 pal.setActive(ca); pal.setInactive(ci); pal.setDisabled(cd);
74 // ================================================================================
76 connect(myQuickCombo, SIGNAL(activated(const QString&)), this, SLOT(quickDir(const QString&)));
77 addWidgets(lab, myQuickCombo, btn);
79 // getting dir list from settings
80 QString dirs = QAD_CONFIG->getSetting("FileDlg:QuickDirList");
81 QStringList dirList = QStringList::split(';', dirs, false);
82 if (dirList.count() > 0) {
83 for (unsigned i = 0; i < dirList.count(); i++)
84 myQuickCombo->insertItem(dirList[i]);
87 myQuickCombo->insertItem(QDir::homeDirPath());
90 // the following is a workaround for proper layouting of custom widgets ===========
91 QValueList<QPushButton*> buttonList;
92 QValueList<QLabel*> labelList;
93 const QObjectList *list = children();
94 QObjectListIt it(*list);
95 int maxButWidth = lab->sizeHint().width();
96 int maxLabWidth = btn->sizeHint().width();
98 for (; it.current() ; ++it) {
99 if ( it.current()->isA( "QLabel" ) ) {
100 int tempW = ((QLabel*)it.current())->minimumWidth();
101 if ( maxLabWidth < tempW ) maxLabWidth = tempW;
102 labelList.append( (QLabel*)it.current() );
104 else if( it.current()->isA("QPushButton") ) {
105 int tempW = ((QPushButton*)it.current())->minimumWidth();
106 if ( maxButWidth < tempW ) maxButWidth = tempW;
107 buttonList.append( (QPushButton*)it.current() );
110 if (maxButWidth > 0) {
111 QValueList<QPushButton*>::Iterator bListIt;
112 for ( bListIt = buttonList.begin(); bListIt != buttonList.end(); ++bListIt )
113 (*bListIt)->setFixedWidth( maxButWidth );
115 if (maxLabWidth > 0) {
116 QValueList<QLabel*>::Iterator lListIt;
117 for ( lListIt = labelList.begin(); lListIt != labelList.end(); ++lListIt )
118 (*lListIt)->setFixedWidth( maxLabWidth );
120 // ================================================================================
122 setMode( myOpen ? ExistingFile : AnyFile );
123 setCaption( myOpen ? tr( "INF_DESK_DOC_OPEN" ) : tr( "INF_DESK_DOC_SAVE" ) );
124 if (myLastVisitedPath.isNull() || myLastVisitedPath.isEmpty()) {
125 // If no last visited path exists -> switch to the first preferred path
126 processPath(myQuickCombo->text(0));
128 else if ( !processPath(myLastVisitedPath) ) {
129 // If last visited path doesn't exist -> switch to the first preferred path
130 processPath(myQuickCombo->text(0));
132 myValidator = new QAD_FileValidator(this);
139 QAD_FileDlg::~QAD_FileDlg()
144 Sets validator for file names to open/save
145 Deletes previous validator
147 void QAD_FileDlg::setValidator( QAD_FileValidator* v )
155 Returns the selected file
157 QString QAD_FileDlg::selectedFile() const
159 return mySelectedFile;
163 Returns 'true' if this is 'Open File' dialog
164 and 'false' if 'Save File' dialog
166 bool QAD_FileDlg::isOpenDlg() const
172 Closes this dialog and sets the return code to 'Accepted'
173 if the selected name is valid ( see 'acceptData()' )
175 void QAD_FileDlg::accept()
177 // mySelectedFile = QFileDialog::selectedFile().simplifyWhiteSpace(); //VSR- 06/12/02
178 if ( mode() != ExistingFiles ) {
179 mySelectedFile = QFileDialogP::selectedFile(); //VSR+ 06/12/02
182 // mySelectedFile = mySelectedFile.simplifyWhiteSpace(); //VSR- 06/12/02
184 /* Qt 2.2.2 BUG: accept() is called twice if you validate
185 the selected file name by pressing 'Return' key in file
186 name editor but this name is not acceptable for acceptData()
188 if ( acceptData() ) {
189 myLastVisitedPath = dirPath();
190 QFileDialogP::accept();
195 Closes this dialog and sets the return code to 'Rejected'
197 void QAD_FileDlg::reject()
199 mySelectedFile = QString::null;
200 QFileDialogP::reject();
204 Returns 'true' if selected file is valid.
205 The validity is checked by a file validator,
206 if there is no validator the file is always
209 bool QAD_FileDlg::acceptData()
214 if ( mode() == ExistingFiles ) {
215 QStringList fileNames = selectedFiles();
216 for ( int i = 0; i < fileNames.count(); i++ ) {
217 if ( !myValidator->canOpen( fileNames[i] ) )
223 return myValidator->canOpen( selectedFile() );
226 return myValidator->canSave( selectedFile() );
232 Adds an extension to the selected file name
233 if the file has not it.
234 The extension is extracted from the active filter.
236 void QAD_FileDlg::addExtension()
238 // mySelectedFile.stripWhiteSpace();//VSR- 06/12/02
239 // if ( mySelectedFile.isEmpty() )//VSR- 06/12/02
240 if ( mySelectedFile.stripWhiteSpace().isEmpty() )//VSR+ 06/12/02
243 // if ( QAD_Tools::getFileExtensionFromPath( mySelectedFile ).isEmpty() ) //VSR- 06/12/02
244 //ota : 16/12/03 if ( QAD_Tools::getFileExtensionFromPath( mySelectedFile ).isEmpty() ) //VSR+ 06/12/02
247 #if QT_VERSION < 0x030000
248 QRegExp r( QString::fromLatin1("([a-zA-Z0-9.*? +;#]*)$") );
249 int len, index = r.match( selectedFilter(), 0, &len );
251 QRegExp r( QString::fromLatin1("\\([a-zA-Z0-9.*? +;#]*\\)$") );
252 int index = r.search(selectedFilter());
256 #if QT_VERSION < 0x030000
257 // QString wildcard = selectedFilter().mid( index + 1, len-2 ); //VSR- 06/12/02
258 QString wildcard = selectedFilter().mid( index + 1, len-2 ).stripWhiteSpace(); //VSR+ 06/12/02
260 // QString wildcard = selectedFilter().mid( index + 1, r.matchedLength()-2 ); //VSR- 06/12/02
261 QString wildcard = selectedFilter().mid( index + 1, r.matchedLength()-2 ).stripWhiteSpace(); //VSR+ 06/12/02
263 if ( mySelectedFile[mySelectedFile.length() - 1] == '.')
264 //if the file name ends with the point remove it
265 mySelectedFile.truncate(mySelectedFile.length() - 1);
266 QString anExt = "." + QAD_Tools::getFileExtensionFromPath( mySelectedFile ).stripWhiteSpace();
267 // From the filters list make a pattern to validate a file extension
268 // Due to transformations from the filter list (*.txt *.*xx *.c++ QAD*.* ) we
269 // will have the pattern (\.txt|\..*xx|\.c\+\+|\..*) (as we validate extension only we remove
270 // stay extension mask only in the pattern
271 QString aPattern(wildcard);
272 QRegExp anExtRExp("("+aPattern.replace(QRegExp("(^| )[0-9a-zA-Z*_?]*\\."), " \\.").
273 stripWhiteSpace().replace(QRegExp("\\s+"), "|").
274 replace(QRegExp("[*]"),".*").replace(QRegExp("[+]"),"\\+") + ")");
276 if ( anExtRExp.match(anExt) == -1 ) //if a selected file extension does not match to filter's list
277 { //remove a point if it is at the word end
278 if (anExt[ anExt.length() - 1 ] == '.') anExt.truncate( anExt.length() - 1 );
279 index = wildcard.findRev( '.' );
281 mySelectedFile += wildcard.mid( index ); //add the extension
288 Processes selection : tries to set given path or filename as selection
290 bool QAD_FileDlg::processPath( const QString& path )
292 if ( !path.isNull() ) {
293 QFileInfo fi( path );
296 setSelection( path );
297 else if ( fi.isDir() )
302 if ( QFileInfo( fi.dirPath() ).exists() ) {
303 setDir( fi.dirPath() );
304 setSelection( path );
312 Called when user selects item from "Quick Dir" combo box
314 void QAD_FileDlg::quickDir(const QString& dirPath)
316 if ( !QDir(dirPath).exists() ) {
317 QAD_MessageBox::error1(this,
319 tr("ERR_DIR_NOT_EXIST").arg(dirPath),
324 processPath(dirPath);
329 Returns the file name for Open/Save [ static ]
331 QString QAD_FileDlg::getFileName( QWidget* parent,
332 const QString& initial,
333 const QStringList& filters,
334 const QString& caption,
337 QAD_FileValidator* validator )
339 QAD_FileDlg* fd = new QAD_FileDlg( parent, open, showQuickDir, true );
340 if ( !caption.isEmpty() )
341 fd->setCaption( caption );
342 if ( !initial.isEmpty() ) {
343 fd->processPath( initial ); // VSR 24/03/03 check for existing of directory has been added to avoid QFileDialog's bug
346 fd->setFilters( filters );
349 fd->setValidator( validator );
351 QString filename = fd->selectedFile();
353 qApp->processEvents();
359 Returns the list of files to be opened [ static ]
361 QStringList QAD_FileDlg::getOpenFileNames( QWidget* parent,
362 const QString& initial,
363 const QStringList& filters,
364 const QString& caption,
366 QAD_FileValidator* validator )
368 QAD_FileDlg* fd = new QAD_FileDlg( parent, true, showQuickDir, true );
369 fd->setMode( ExistingFiles );
370 if ( !caption.isEmpty() )
371 fd->setCaption( caption );
372 if ( !initial.isEmpty() ) {
373 fd->processPath( initial ); // VSR 24/03/03 check for existing of directory has been added to avoid QFileDialog's bug
375 fd->setFilters( filters );
377 fd->setValidator( validator );
379 QStringList filenames = fd->selectedFiles();
381 qApp->processEvents();
386 Existing directory selection dialog [ static ]
388 QString QAD_FileDlg::getExistingDirectory ( QWidget* parent,
389 const QString& initial,
390 const QString& caption,
393 QAD_FileDlg* fd = new QAD_FileDlg( parent, true, showQuickDir, true);
394 if ( !caption.isEmpty() )
395 fd->setCaption( caption );
396 if ( !initial.isEmpty() ) {
397 fd->processPath( initial ); // VSR 24/03/03 check for existing of directory has been added to avoid QFileDialog's bug
399 fd->setMode( DirectoryOnly );
400 fd->setFilters(tr("DIRECTORIES_FILTER"));
403 QString dirname = fd->selectedFile();
405 qApp->processEvents();