void STD_Application::onOpenDoc()
{
// It is preferrable to use OS-specific file dialog box here !!!
- QString aName = QFileDialog::getOpenFileName( QString::null, getFileFilter(), desktop() );
+ QString aName = getFileName( true );
if ( aName.isNull() )
return;
if ( !study )
return false;
- QString aName;
- QString aUsedFilter;
- QString anOldPath = study->studyName();
-
- bool isOk = false;
- while ( !isOk )
- {
- // It is preferrable to use OS-specific file dialog box here !!!
- aName = QFileDialog::getSaveFileName( anOldPath, getFileFilter(), desktop(),
- 0, QString::null, &aUsedFilter);
-
- if ( aName.isNull() )
- isOk = true;
- else
- {
- if ( !getFileFilter().isNull() ) // check exstension and add if it is necessary
- {
- int aStart = aUsedFilter.find( '*' );
- int aEnd = aUsedFilter.find( ')', aStart + 1 );
- QString aExt = aUsedFilter.mid( aStart + 1, aEnd - aStart - 1 );
- if ( aExt.contains( '*' ) == 0 ) // if it is not *.*
- {
- // Check that there is an extension at the end of the name
- QString aNameTail = aName.right( aExt.length() );
- if ( aNameTail != aExt )
- aName += aExt;
- }
- }
- if ( QFileInfo( aName ).exists() )
- {
- int aAnswer = SUIT_MessageBox::warn2( desktop(), tr( "TIT_FILE_SAVEAS" ),
- tr( "MSG_FILE_EXISTS" ).arg( aName ),
- tr( "BUT_YES" ), tr( "BUT_NO" ), 1, 2, 2 );
- if ( aAnswer != 2 )
- isOk = true;
- else
- anOldPath = aName; // Not to return to the same initial dir at each "while" step
- }
- else
- isOk = true;
-
- if ( isOk )
- isOk = study->saveDocumentAs( aName );
- }
- }
+ QString aName = getFileName( false );
if ( aName.isNull() )
return false;
+ bool isOk = study->saveDocumentAs( aName );
updateDesktopTitle();
updateCommandsStatus();
- return true;
+ return isOk;
}
void STD_Application::onExit()
popup->exec( e->globalPos() );
delete popup;
}
+
+QString STD_Application::getFileName( bool open )
+{
+ if ( open )
+ {
+ return QFileDialog::getOpenFileName( QString::null, getFileFilter(), desktop() );
+ }
+ else
+ {
+ SUIT_Study* study = activeStudy();
+ QString aName;
+ QString aUsedFilter;
+ QString anOldPath = study->studyName();
+
+ bool isOk = false;
+ while ( !isOk )
+ {
+ // It is preferrable to use OS-specific file dialog box here !!!
+ aName = QFileDialog::getSaveFileName( anOldPath, getFileFilter(), desktop(),
+ 0, QString::null, &aUsedFilter);
+
+ if ( aName.isNull() )
+ isOk = true;
+ else
+ {
+ if ( !getFileFilter().isNull() ) // check extension and add if it is necessary
+ {
+ int aStart = aUsedFilter.find( '*' );
+ int aEnd = aUsedFilter.find( ')', aStart + 1 );
+ QString aExt = aUsedFilter.mid( aStart + 1, aEnd - aStart - 1 );
+ if ( aExt.contains( '*' ) == 0 ) // if it is not *.*
+ {
+ // Check that there is an extension at the end of the name
+ QString aNameTail = aName.right( aExt.length() );
+ if ( aNameTail != aExt )
+ aName += aExt;
+ }
+ }
+ if ( QFileInfo( aName ).exists() )
+ {
+ int aAnswer = SUIT_MessageBox::warn3( desktop(), tr( "TIT_FILE_SAVEAS" ),
+ tr( "MSG_FILE_EXISTS" ).arg( aName ),
+ tr( "BUT_YES" ), tr( "BUT_NO" ), tr( "BUT_CANCEL" ), 1, 2, 3, 1 );
+ if ( aAnswer == 3 ) { // cancelled
+ aName = QString::null;
+ isOk = true;
+ }
+ else if ( aAnswer == 2 ) // not save to this file
+ anOldPath = aName; // not to return to the same initial dir at each "while" step
+ else // overwrite the existing file
+ isOk = true;
+ }
+ else
+ isOk = true;
+ }
+ }
+ return aName;
+ }
+}
#include <qregexp.h>
#include <qpalette.h>
#include <qobjectlist.h>
+#include <qcombobox.h>
#include <qpushbutton.h>
#include <qapplication.h>
SUIT_FileDlg::SUIT_FileDlg( QWidget* parent, bool open, bool showQuickDir, bool modal ) :
QFileDialog( parent, 0, modal ),
myValidator( 0 ),
-myQuickCombo( 0 ),
+myQuickCombo( 0 ), myQuickButton( 0 ), myQuickLab( 0 ),
myOpen( open )
{
if ( parent->icon() )
setIcon( *parent->icon() );
setSizeGripEnabled( true );
- if (showQuickDir) {
+ if ( showQuickDir ) {
// inserting quick dir combo box
- QLabel* lab = new QLabel(tr("Quick path:"), this);
+ myQuickLab = new QLabel(tr("LAB_QUICK_PATH"), this);
myQuickCombo = new QComboBox(false, this);
myQuickCombo->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
myQuickCombo->setMinimumSize(MIN_COMBO_SIZE, 0);
- // the following is a workaround for proper layouting of custom widgets ===========
- QPushButton* btn = new QPushButton(this);
- btn->setEnabled(false);
- QPalette pal = btn->palette();
- QColorGroup ca = pal.active();
- ca.setColor(QColorGroup::Light, palette().active().background());
- ca.setColor(QColorGroup::Midlight, palette().active().background());
- ca.setColor(QColorGroup::Dark, palette().active().background());
- ca.setColor(QColorGroup::Mid, palette().active().background());
- ca.setColor(QColorGroup::Shadow, palette().active().background());
- QColorGroup ci = pal.inactive();
- ci.setColor(QColorGroup::Light, palette().inactive().background());
- ci.setColor(QColorGroup::Midlight, palette().inactive().background());
- ci.setColor(QColorGroup::Dark, palette().inactive().background());
- ci.setColor(QColorGroup::Mid, palette().inactive().background());
- ci.setColor(QColorGroup::Shadow, palette().inactive().background());
- QColorGroup cd = pal.disabled();
- cd.setColor(QColorGroup::Light, palette().disabled().background());
- cd.setColor(QColorGroup::Midlight, palette().disabled().background());
- cd.setColor(QColorGroup::Dark, palette().disabled().background());
- cd.setColor(QColorGroup::Mid, palette().disabled().background());
- cd.setColor(QColorGroup::Shadow, palette().disabled().background());
- pal.setActive(ca); pal.setInactive(ci); pal.setDisabled(cd);
- btn->setPalette(pal);
- // ================================================================================
+ myQuickButton = new QPushButton(tr("BUT_ADD_PATH"), this);
- connect(myQuickCombo, SIGNAL(activated(const QString&)), this, SLOT(quickDir(const QString&)));
- addWidgets(lab, myQuickCombo, btn);
+ connect(myQuickCombo, SIGNAL(activated(const QString&)), this, SLOT(quickDir(const QString&)));
+ connect(myQuickButton, SIGNAL(clicked()), this, SLOT(addQuickDir()));
+ addWidgets(myQuickLab, myQuickCombo, myQuickButton);
// getting dir list from settings
QString dirs;
SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
if ( resMgr )
- dirs = resMgr->stringValue( "FileDlg", "QuickDirList" );
+ dirs = resMgr->stringValue( "FileDlg", QString( "QuickDirList" ) );
QStringList dirList = QStringList::split(';', dirs, false);
if (dirList.count() > 0) {
for (unsigned i = 0; i < dirList.count(); i++)
- myQuickCombo->insertItem(dirList[i]);
+ myQuickCombo->insertItem(dirList[i]);
}
else {
myQuickCombo->insertItem(QDir::homeDirPath());
}
+ }
+ setMode( myOpen ? ExistingFile : AnyFile );
+ setCaption( myOpen ? tr( "INF_DESK_DOC_OPEN" ) : tr( "INF_DESK_DOC_SAVE" ) );
+
+ // If last visited path doesn't exist -> switch to the first preferred path
+ if ( !myLastVisitedPath.isEmpty() ) {
+ if ( !processPath( myLastVisitedPath ) && showQuickDir )
+ processPath( myQuickCombo->text( 0 ) );
+ }
+ else {
+ if ( showQuickDir )
+ processPath(myQuickCombo->text( 0 ) );
+ }
- // the following is a workaround for proper layouting of custom widgets ===========
+ // set default file validator
+ myValidator = new SUIT_FileValidator(this);
+}
+
+/*!
+Destructor
+*/
+SUIT_FileDlg::~SUIT_FileDlg()
+{
+ setValidator( 0 );
+}
+
+/*!
+ Redefined from QFileDialog.
+*/
+void SUIT_FileDlg::polish()
+{
+ QFileDialog::polish();
+ if ( myQuickButton && myQuickLab ) {
+ // the following is a workaround for proper layouting of custom widgets
QValueList<QPushButton*> buttonList;
QValueList<QLabel*> labelList;
const QObjectList *list = children();
QObjectListIt it(*list);
- int maxButWidth = lab->sizeHint().width();
- int maxLabWidth = btn->sizeHint().width();
+ int maxButWidth = myQuickLab->sizeHint().width();
+ int maxLabWidth = myQuickButton->sizeHint().width();
for (; it.current() ; ++it) {
if ( it.current()->isA( "QLabel" ) ) {
for ( lListIt = labelList.begin(); lListIt != labelList.end(); ++lListIt )
(*lListIt)->setFixedWidth( maxLabWidth );
}
- // ================================================================================
}
- setMode( myOpen ? ExistingFile : AnyFile );
- setCaption( myOpen ? tr( "INF_DESK_DOC_OPEN" ) : tr( "INF_DESK_DOC_SAVE" ) );
-
- if (showQuickDir) {
- if (myLastVisitedPath.isNull() || myLastVisitedPath.isEmpty()) {
- // If no last visited path exists -> switch to the first preferred path
- processPath(myQuickCombo->text(0));
- }
- else if ( !processPath(myLastVisitedPath) ) {
- // If last visited path doesn't exist -> switch to the first preferred path
- processPath(myQuickCombo->text(0));
- }
- }
-
- myValidator = new SUIT_FileValidator(this);
-
-}
-
-/*!
-Destructor
-*/
-SUIT_FileDlg::~SUIT_FileDlg()
-{
}
/*!
Sets validator for file names to open/save
-Deletes previous validator
+Deletes previous validator if the dialog owns it.
*/
void SUIT_FileDlg::setValidator( SUIT_FileValidator* v )
{
- if (myValidator)
+ if ( myValidator && myValidator->parent() == this )
delete myValidator;
myValidator = v;
}
void SUIT_FileDlg::accept()
{
// mySelectedFile = QFileDialog::selectedFile().simplifyWhiteSpace(); //VSR- 06/12/02
- mySelectedFile = QFileDialog::selectedFile(); //VSR+ 06/12/02
- addExtension();
+ if ( mode() != ExistingFiles ) {
+ mySelectedFile = QFileDialog::selectedFile(); //VSR+ 06/12/02
+ addExtension();
+ }
// mySelectedFile = mySelectedFile.simplifyWhiteSpace(); //VSR- 06/12/02
/* Qt 2.2.2 BUG: accept() is called twice if you validate
if ( myValidator )
{
if ( isOpenDlg() )
- return myValidator->canOpen( selectedFile() );
+ if ( mode() == ExistingFiles ) {
+ QStringList fileNames = selectedFiles();
+ for ( int i = 0; i < fileNames.count(); i++ ) {
+ if ( !myValidator->canOpen( fileNames[i] ) )
+ return false;
+ }
+ return true;
+ }
+ else {
+ return myValidator->canOpen( selectedFile() );
+ }
else
return myValidator->canSave( selectedFile() );
}
if ( anExtRExp.match(anExt) == -1 ) //if a selected file extension does not match to filter's list
{ //remove a point if it is at the word end
- int aExtLen = anExt.length();
+ int aExtLen = anExt.length();
if (anExt[ aExtLen - 1 ] == '.') anExt.truncate( aExtLen - 1 );
index = wildcard.findRev( '.' );
if ( index >= 0 )
else {
if ( QFileInfo( fi.dirPath() ).exists() ) {
setDir( fi.dirPath() );
+ setSelection( path );
return true;
}
}
processPath(dirPath);
}
}
-
+/*!
+ Called when user presses "Add" button - adds current directory to quick directory
+ list and to the preferences
+*/
+void SUIT_FileDlg::addQuickDir()
+{
+ QString dp = dirPath();
+ if ( !dp.isEmpty() ) {
+ QDir dir( dp );
+ // getting dir list from settings
+ QString dirs;
+ SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
+ if ( resMgr )
+ dirs = resMgr->stringValue( "FileDlg", QString( "QuickDirList" ) );
+ QStringList dirList = QStringList::split(';', dirs, false);
+ bool found = false;
+ bool emptyAndHome = false;
+ if ( dirList.count() > 0 ) {
+ for ( unsigned i = 0; i < dirList.count(); i++ ) {
+ QDir aDir( dirList[i] );
+ if ( aDir.canonicalPath().isNull() && dirList[i] == dir.absPath() ||
+ !aDir.canonicalPath().isNull() && aDir.exists() && aDir.canonicalPath() == dir.canonicalPath() ) {
+ found = true;
+ break;
+ }
+ }
+ }
+ else {
+ emptyAndHome = dir.canonicalPath() == QDir(QDir::homeDirPath()).canonicalPath();
+ }
+ if ( !found ) {
+ dirList.append( dp );
+ resMgr->setValue( "FileDlg", QString( "QuickDirList" ), dirList.join(";") );
+ if ( !emptyAndHome )
+ myQuickCombo->insertItem( dp );
+ }
+ }
+}
/*!
Returns the file name for Open/Save [ static ]
*/
-QString SUIT_FileDlg::getFileName( QWidget* parent,
- const QString& initial,
- const QStringList& filters,
- const QString& caption,
- bool open,
- bool showQuickDir,
- SUIT_FileValidator* validator )
+QString SUIT_FileDlg::getFileName( QWidget* parent,
+ const QString& initial,
+ const QStringList& filters,
+ const QString& caption,
+ bool open,
+ bool showQuickDir,
+ SUIT_FileValidator* validator )
{
SUIT_FileDlg* fd = new SUIT_FileDlg( parent, open, showQuickDir, true );
if ( !caption.isEmpty() )
return filename;
}
+
+/*!
+ Returns the list of files to be opened [ static ]
+*/
+QStringList SUIT_FileDlg::getOpenFileNames( QWidget* parent,
+ const QString& initial,
+ const QStringList& filters,
+ const QString& caption,
+ bool showQuickDir,
+ SUIT_FileValidator* validator )
+{
+ SUIT_FileDlg* fd = new SUIT_FileDlg( parent, true, showQuickDir, true );
+ fd->setMode( ExistingFiles );
+ if ( !caption.isEmpty() )
+ fd->setCaption( caption );
+ if ( !initial.isEmpty() ) {
+ fd->processPath( initial ); // VSR 24/03/03 check for existing of directory has been added to avoid QFileDialog's bug
+ }
+ fd->setFilters( filters );
+ if ( validator )
+ fd->setValidator( validator );
+ fd->exec();
+ QStringList filenames = fd->selectedFiles();
+ delete fd;
+ qApp->processEvents();
+ return filenames;
+}
+
/*!
Existing directory selection dialog [ static ]
*/
-QString SUIT_FileDlg::getExistingDirectory ( QWidget* parent,
+QString SUIT_FileDlg::getExistingDirectory( QWidget* parent,
const QString& initial,
const QString& caption,
bool showQuickDir )
fd->processPath( initial ); // VSR 24/03/03 check for existing of directory has been added to avoid QFileDialog's bug
}
fd->setMode( DirectoryOnly );
- fd->setFilters(tr("DIRECTORIES_FILTER"));
+ fd->setFilters(tr("INF_DIRECTORIES_FILTER"));
fd->exec();
QString dirname = fd->selectedFile();
delete fd;