1 #include "SUIT_FileDlg.h"
3 #include "SUIT_Tools.h"
4 #include "SUIT_Session.h"
5 #include "SUIT_Desktop.h"
6 #include "SUIT_MessageBox.h"
7 #include "SUIT_ResourceMgr.h"
8 #include "SUIT_FileValidator.h"
14 #include <qobjectlist.h>
15 #include <qpushbutton.h>
16 #include <qapplication.h>
18 #define MIN_COMBO_SIZE 100
20 QString SUIT_FileDlg::myLastVisitedPath;
25 SUIT_FileDlg::SUIT_FileDlg( QWidget* parent, bool open, bool showQuickDir, bool modal ) :
26 QFileDialog( parent, 0, modal ),
32 setIcon( *parent->icon() );
33 setSizeGripEnabled( true );
36 // inserting quick dir combo box
37 QLabel* lab = new QLabel(tr("Quick path:"), this);
38 myQuickCombo = new QComboBox(false, this);
39 myQuickCombo->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
40 myQuickCombo->setMinimumSize(MIN_COMBO_SIZE, 0);
42 // the following is a workaround for proper layouting of custom widgets ===========
43 QPushButton* btn = new QPushButton(this);
44 btn->setEnabled(false);
45 QPalette pal = btn->palette();
46 QColorGroup ca = pal.active();
47 ca.setColor(QColorGroup::Light, palette().active().background());
48 ca.setColor(QColorGroup::Midlight, palette().active().background());
49 ca.setColor(QColorGroup::Dark, palette().active().background());
50 ca.setColor(QColorGroup::Mid, palette().active().background());
51 ca.setColor(QColorGroup::Shadow, palette().active().background());
52 QColorGroup ci = pal.inactive();
53 ci.setColor(QColorGroup::Light, palette().inactive().background());
54 ci.setColor(QColorGroup::Midlight, palette().inactive().background());
55 ci.setColor(QColorGroup::Dark, palette().inactive().background());
56 ci.setColor(QColorGroup::Mid, palette().inactive().background());
57 ci.setColor(QColorGroup::Shadow, palette().inactive().background());
58 QColorGroup cd = pal.disabled();
59 cd.setColor(QColorGroup::Light, palette().disabled().background());
60 cd.setColor(QColorGroup::Midlight, palette().disabled().background());
61 cd.setColor(QColorGroup::Dark, palette().disabled().background());
62 cd.setColor(QColorGroup::Mid, palette().disabled().background());
63 cd.setColor(QColorGroup::Shadow, palette().disabled().background());
64 pal.setActive(ca); pal.setInactive(ci); pal.setDisabled(cd);
66 // ================================================================================
68 connect(myQuickCombo, SIGNAL(activated(const QString&)), this, SLOT(quickDir(const QString&)));
69 addWidgets(lab, myQuickCombo, btn);
71 // getting dir list from settings
73 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
75 dirs = resMgr->stringValue( "FileDlg", "QuickDirList" );
77 QStringList dirList = QStringList::split(';', dirs, false);
78 if (dirList.count() > 0) {
79 for (unsigned i = 0; i < dirList.count(); i++)
80 myQuickCombo->insertItem(dirList[i]);
83 myQuickCombo->insertItem(QDir::homeDirPath());
86 // the following is a workaround for proper layouting of custom widgets ===========
87 QValueList<QPushButton*> buttonList;
88 QValueList<QLabel*> labelList;
89 const QObjectList *list = children();
90 QObjectListIt it(*list);
91 int maxButWidth = lab->sizeHint().width();
92 int maxLabWidth = btn->sizeHint().width();
94 for (; it.current() ; ++it) {
95 if ( it.current()->isA( "QLabel" ) ) {
96 int tempW = ((QLabel*)it.current())->minimumWidth();
97 if ( maxLabWidth < tempW ) maxLabWidth = tempW;
98 labelList.append( (QLabel*)it.current() );
100 else if( it.current()->isA("QPushButton") ) {
101 int tempW = ((QPushButton*)it.current())->minimumWidth();
102 if ( maxButWidth < tempW ) maxButWidth = tempW;
103 buttonList.append( (QPushButton*)it.current() );
106 if (maxButWidth > 0) {
107 QValueList<QPushButton*>::Iterator bListIt;
108 for ( bListIt = buttonList.begin(); bListIt != buttonList.end(); ++bListIt )
109 (*bListIt)->setFixedWidth( maxButWidth );
111 if (maxLabWidth > 0) {
112 QValueList<QLabel*>::Iterator lListIt;
113 for ( lListIt = labelList.begin(); lListIt != labelList.end(); ++lListIt )
114 (*lListIt)->setFixedWidth( maxLabWidth );
116 // ================================================================================
118 setMode( myOpen ? ExistingFile : AnyFile );
119 setCaption( myOpen ? tr( "INF_DESK_DOC_OPEN" ) : tr( "INF_DESK_DOC_SAVE" ) );
120 if (myLastVisitedPath.isNull() || myLastVisitedPath.isEmpty()) {
121 // If no last visited path exists -> switch to the first preferred path
122 processPath(myQuickCombo->text(0));
124 else if ( !processPath(myLastVisitedPath) ) {
125 // If last visited path doesn't exist -> switch to the first preferred path
126 processPath(myQuickCombo->text(0));
128 myValidator = new SUIT_FileValidator(this);
135 SUIT_FileDlg::~SUIT_FileDlg()
140 Sets validator for file names to open/save
141 Deletes previous validator
143 void SUIT_FileDlg::setValidator( SUIT_FileValidator* v )
151 Returns the selected file
153 QString SUIT_FileDlg::selectedFile() const
155 return mySelectedFile;
159 Returns 'true' if this is 'Open File' dialog
160 and 'false' if 'Save File' dialog
162 bool SUIT_FileDlg::isOpenDlg() const
168 Closes this dialog and sets the return code to 'Accepted'
169 if the selected name is valid ( see 'acceptData()' )
171 void SUIT_FileDlg::accept()
173 // mySelectedFile = QFileDialog::selectedFile().simplifyWhiteSpace(); //VSR- 06/12/02
174 mySelectedFile = QFileDialog::selectedFile(); //VSR+ 06/12/02
176 // mySelectedFile = mySelectedFile.simplifyWhiteSpace(); //VSR- 06/12/02
178 /* Qt 2.2.2 BUG: accept() is called twice if you validate
179 the selected file name by pressing 'Return' key in file
180 name editor but this name is not acceptable for acceptData()
182 if ( acceptData() ) {
183 myLastVisitedPath = dirPath();
184 QFileDialog::accept();
189 Closes this dialog and sets the return code to 'Rejected'
191 void SUIT_FileDlg::reject()
193 mySelectedFile = QString::null;
194 QFileDialog::reject();
198 Returns 'true' if selected file is valid.
199 The validity is checked by a file validator,
200 if there is no validator the file is always
203 bool SUIT_FileDlg::acceptData()
208 return myValidator->canOpen( selectedFile() );
210 return myValidator->canSave( selectedFile() );
216 Adds an extension to the selected file name
217 if the file has not it.
218 The extension is extracted from the active filter.
220 void SUIT_FileDlg::addExtension()
222 // mySelectedFile.stripWhiteSpace();//VSR- 06/12/02
223 // if ( mySelectedFile.isEmpty() )//VSR- 06/12/02
224 if ( mySelectedFile.stripWhiteSpace().isEmpty() )//VSR+ 06/12/02
227 // if ( SUIT_Tools::getFileExtensionFromPath( mySelectedFile ).isEmpty() ) //VSR- 06/12/02
228 //ota : 16/12/03 if ( SUIT_Tools::getFileExtensionFromPath( mySelectedFile ).isEmpty() ) //VSR+ 06/12/02
231 #if QT_VERSION < 0x030000
232 QRegExp r( QString::fromLatin1("([a-zA-Z0-9.*? +;#]*)$") );
233 int len, index = r.match( selectedFilter(), 0, &len );
235 QRegExp r( QString::fromLatin1("\\([a-zA-Z0-9.*? +;#]*\\)$") );
236 int index = r.search(selectedFilter());
240 #if QT_VERSION < 0x030000
241 // QString wildcard = selectedFilter().mid( index + 1, len-2 ); //VSR- 06/12/02
242 QString wildcard = selectedFilter().mid( index + 1, len-2 ).stripWhiteSpace(); //VSR+ 06/12/02
244 // QString wildcard = selectedFilter().mid( index + 1, r.matchedLength()-2 ); //VSR- 06/12/02
245 QString wildcard = selectedFilter().mid( index + 1, r.matchedLength()-2 ).stripWhiteSpace(); //VSR+ 06/12/02
247 int aLen = mySelectedFile.length();
248 if ( mySelectedFile[aLen - 1] == '.')
249 //if the file name ends with the point remove it
250 mySelectedFile.truncate(aLen - 1);
251 QString anExt = "." + SUIT_Tools::extension( mySelectedFile ).stripWhiteSpace();
252 // From the filters list make a pattern to validate a file extension
253 // Due to transformations from the filter list (*.txt *.*xx *.c++ SUIT*.* ) we
254 // will have the pattern (\.txt|\..*xx|\.c\+\+|\..*) (as we validate extension only we remove
255 // stay extension mask only in the pattern
256 QString aPattern(wildcard);
257 QRegExp anExtRExp("("+aPattern.replace(QRegExp("(^| )[0-9a-zA-Z*_?]*\\."), " \\.").
258 stripWhiteSpace().replace(QRegExp("\\s+"), "|").
259 replace(QRegExp("[*]"),".*").replace(QRegExp("[+]"),"\\+") + ")");
261 if ( anExtRExp.match(anExt) == -1 ) //if a selected file extension does not match to filter's list
262 { //remove a point if it is at the word end
263 int aExtLen = anExt.length();
264 if (anExt[ aExtLen - 1 ] == '.') anExt.truncate( aExtLen - 1 );
265 index = wildcard.findRev( '.' );
267 mySelectedFile += wildcard.mid( index ); //add the extension
274 Processes selection : tries to set given path or filename as selection
276 bool SUIT_FileDlg::processPath( const QString& path )
278 if ( !path.isNull() ) {
279 QFileInfo fi( path );
282 setSelection( path );
283 else if ( fi.isDir() )
288 if ( QFileInfo( fi.dirPath() ).exists() ) {
289 setDir( fi.dirPath() );
297 Called when user selects item from "Quick Dir" combo box
299 void SUIT_FileDlg::quickDir(const QString& dirPath)
301 if ( !QDir(dirPath).exists() ) {
302 SUIT_MessageBox::error1(this,
304 tr("ERR_DIR_NOT_EXIST").arg(dirPath),
309 processPath(dirPath);
314 Returns the file name for Open/Save [ static ]
316 QString SUIT_FileDlg::getFileName( QWidget* parent,
317 const QString& initial,
318 const QStringList& filters,
319 const QString& caption,
322 SUIT_FileValidator* validator )
324 SUIT_FileDlg* fd = new SUIT_FileDlg( parent, open, showQuickDir, true );
325 if ( !caption.isEmpty() )
326 fd->setCaption( caption );
327 if ( !initial.isEmpty() ) {
328 fd->processPath( initial ); // VSR 24/03/03 check for existing of directory has been added to avoid QFileDialog's bug
330 fd->setFilters( filters );
332 fd->setValidator( validator );
334 QString filename = fd->selectedFile();
336 qApp->processEvents();
341 Existing directory selection dialog [ static ]
343 QString SUIT_FileDlg::getExistingDirectory ( QWidget* parent,
344 const QString& initial,
345 const QString& caption,
348 SUIT_FileDlg* fd = new SUIT_FileDlg( parent, true, showQuickDir, true);
349 if ( !caption.isEmpty() )
350 fd->setCaption( caption );
351 if ( !initial.isEmpty() ) {
352 fd->processPath( initial ); // VSR 24/03/03 check for existing of directory has been added to avoid QFileDialog's bug
354 fd->setMode( DirectoryOnly );
355 fd->setFilters(tr("DIRECTORIES_FILTER"));
357 QString dirname = fd->selectedFile();
359 qApp->processEvents();