#include "QtxPathDialog.h"
+#include "QtxGridBox.h"
#include "QtxGroupBox.h"
#include <qdir.h>
#include <qlayout.h>
#include <qlineedit.h>
#include <qfileinfo.h>
-#include <qobjectlist.h>
+
+#include <QObjectList>
+
#include <qstringlist.h>
#include <qfiledialog.h>
#include <qmessagebox.h>
/*!
Constructor.
*/
-QtxPathDialog::QtxPathDialog( const bool import, QWidget* parent, const bool modal, const bool resize, const int buttons, WFlags f )
-: QtxDialog( parent, 0, modal, resize, buttons, f ),
+QtxPathDialog::QtxPathDialog( const bool import, QWidget* parent, const bool modal,
+ const bool resize, const int buttons, Qt::WindowFlags f )
+: QtxDialog( parent, modal, resize, buttons, f ),
myDefault( -1 ),
myEntriesFrame( 0 ),
myOptionsFrame( 0 )
{
initialize();
- setCaption( tr( import ? "Open file" : "Save file" ) );
+ setWindowTitle( tr( import ? "Open file" : "Save file" ) );
setDefaultEntry( createFileEntry( tr( "File name" ), import ? OpenFile : SaveFile ) );
QLineEdit* le = fileEntry( defaultEntry() );
validate();
setFocusProxy( le );
+
+ updateVisibility();
}
/*!
Constructor.
*/
-QtxPathDialog::QtxPathDialog( QWidget* parent, const bool modal, const bool resize, const int buttons, WFlags f )
-: QtxDialog( parent, 0, modal, resize, buttons, f ),
+QtxPathDialog::QtxPathDialog( QWidget* parent, const bool modal,
+ const bool resize, const int buttons, Qt::WindowFlags f )
+: QtxDialog( parent, modal, resize, buttons, f ),
myDefault( -1 ),
myEntriesFrame( 0 ),
myOptionsFrame( 0 )
{
initialize();
+
+ updateVisibility();
}
/*!
*/
QString QtxPathDialog::filter() const
{
- return myFilter;
+ return filter( defaultEntry() );
}
/*!
*/
void QtxPathDialog::setFilter( const QString& fltr )
{
- myFilter = fltr;
+ setFilter( defaultEntry(), fltr );
}
/*!
- Shows path dialog
+ Shows/hides path dialog
*/
-void QtxPathDialog::show()
+void QtxPathDialog::setVisible( bool on )
{
- if ( hasVisibleChildren( myEntriesFrame ) )
- myEntriesFrame->show();
- else
- myEntriesFrame->hide();
+ if ( on )
+ updateVisibility();
- if ( hasVisibleChildren( myOptionsFrame ) )
- myOptionsFrame->show();
- else
- myOptionsFrame->hide();
-
- QtxDialog::show();
+ QtxDialog::setVisible( on );
}
/*!
int id = -1;
for ( FileEntryMap::Iterator it = myEntries.begin(); it != myEntries.end() && id == -1; ++it )
- if ( it.data().btn == obj )
+ {
+ if ( it.value().btn == obj )
id = it.key();
+ }
if ( id == -1 )
return;
if ( !entry.dlg )
{
- entry.dlg = new QFileDialog( QDir::current().path(), QString::null, this, 0, true );
- entry.dlg->setCaption( caption() );
+ entry.dlg = new QFileDialog( this, windowTitle(), QDir::current().path() );
switch ( entry.mode )
{
case NewDir:
case OpenDir:
case SaveDir:
isDir = true;
- entry.dlg->setMode( QFileDialog::DirectoryOnly );
+ entry.dlg->setFileMode( QFileDialog::DirectoryOnly );
break;
case SaveFile:
- entry.dlg->setMode( QFileDialog::AnyFile );
+ entry.dlg->setFileMode( QFileDialog::AnyFile );
break;
case OpenFile:
default:
- entry.dlg->setMode( QFileDialog::ExistingFile );
+ entry.dlg->setFileMode( QFileDialog::ExistingFiles );
break;
}
}
if ( !isDir )
- entry.dlg->setFilters( prepareFilters() );
- entry.dlg->setSelection( fileName( id ) );
+ {
+ QStringList fList = prepareFilters( entry.filter );
+ if ( !fList.isEmpty() )
+ entry.dlg->setFilters( fList );
+ }
+ entry.dlg->selectFile( fileName( id ) );
if ( entry.dlg->exec() != Accepted )
return;
- QString fName = entry.dlg->selectedFile();
+ QStringList fileList = entry.dlg->selectedFiles();
+ QString fName = !fileList.isEmpty() ? fileList.first() : QString();
if ( fName.isEmpty() )
return;
- if ( QFileInfo( fName ).extension().isEmpty() && !isDir )
+ if ( Qtx::extension( fName ).isEmpty() && !isDir )
fName = autoExtension( fName, entry.dlg->selectedFilter() );
fName = QDir::convertSeparators( fName );
int id = -1;
for ( FileEntryMap::Iterator it = myEntries.begin(); it != myEntries.end() && id == -1; ++it )
- if ( it.data().edit == obj )
+ {
+ if ( it.value().edit == obj )
id = it.key();
+ }
if ( id == -1 )
return;
{
bool ok = true;
for ( FileEntryMap::Iterator it = myEntries.begin(); it != myEntries.end() && ok; ++it )
- if ( it.data().edit->isEnabled() )
- ok = !it.data().edit->text().stripWhiteSpace().isEmpty();
+ {
+ if ( it.value().edit->isEnabled() )
+ ok = !it.value().edit->text().trimmed().isEmpty();
+ }
return ok;
}
FileEntryMap::ConstIterator it;
for ( it = myEntries.begin(); it != myEntries.end() && ok; ++it )
{
- const FileEntry& entry = it.data();
+ const FileEntry& entry = it.value();
QFileInfo fileInfo( entry.edit->text() );
if ( entry.edit->text().isEmpty() )
{
- QMessageBox::critical( parent, caption(), tr( "File name not specified" ),
+ QMessageBox::critical( parent, windowTitle(), tr( "File name not specified" ),
QMessageBox::Ok, QMessageBox::NoButton );
ok = false;
}
case OpenFile:
if ( !fileInfo.exists() )
{
- QMessageBox::critical( parent, caption(), tr( "File \"%1\" does not exist" ).arg( fileInfo.filePath() ),
+ QMessageBox::critical( parent, windowTitle(), tr( "File \"%1\" does not exist" ).arg( fileInfo.filePath() ),
QMessageBox::Ok, QMessageBox::NoButton );
ok = false;
}
break;
case SaveFile:
if ( fileInfo.exists() )
- ok = QMessageBox::warning( parent, caption(), tr( "File \"%1\" already exist. Do you want to overwrite it?" ).arg( fileInfo.filePath() ),
+ ok = QMessageBox::warning( parent, windowTitle(), tr( "File \"%1\" already exist. Do you want to overwrite it?" ).arg( fileInfo.filePath() ),
QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes;
break;
case OpenDir:
if ( !fileInfo.exists() || !fileInfo.isDir() )
{
- QMessageBox::critical( parent, caption(), tr( "Directory \"%1\" does not exist" ).arg( fileInfo.filePath() ),
+ QMessageBox::critical( parent, windowTitle(), tr( "Directory \"%1\" does not exist" ).arg( fileInfo.filePath() ),
QMessageBox::Ok, QMessageBox::NoButton );
ok = false;
}
case SaveDir:
if ( fileInfo.exists() && !fileInfo.isDir() )
{
- QMessageBox::critical( parent, caption(), tr( "Directory \"%1\" can't be created because file with the same name exist" ).arg( fileInfo.filePath() ),
+ QMessageBox::critical( parent, windowTitle(), tr( "Directory \"%1\" can't be created because file with the same name exist" ).arg( fileInfo.filePath() ),
QMessageBox::Ok, QMessageBox::NoButton );
ok = false;
}
{
if ( !fileInfo.isDir() )
{
- QMessageBox::critical( parent, caption(), tr( "Directory \"%1\" can't be created because file with the same name exist" ).arg( fileInfo.filePath() ),
+ QMessageBox::critical( parent, windowTitle(), tr( "Directory \"%1\" can't be created because file with the same name exist" ).arg( fileInfo.filePath() ),
QMessageBox::Ok, QMessageBox::NoButton );
ok = false;
}
else if ( QDir( fileInfo.filePath() ).count() > 2 )
- ok = QMessageBox::warning( parent, caption(), tr( "Directory \"%1\" not empty. Do you want to remove all files in this directory?" ).arg( fileInfo.filePath() ),
+ ok = QMessageBox::warning( parent, windowTitle(), tr( "Directory \"%1\" not empty. Do you want to remove all files in this directory?" ).arg( fileInfo.filePath() ),
QMessageBox::Yes, QMessageBox::No ) == QMessageBox::Yes;
}
break;
QString res;
if ( myEntries.contains( id ) )
res = myEntries[id].edit->text();
-
return res;
}
if ( le )
{
if ( autoExt && ( mode == OpenFile || mode == SaveFile ) )
- le->setText( autoExtension( txt ) );
+ le->setText( autoExtension( txt, filter( id ) ) );
else
le->setText( txt );
}
}
+/*!
+ \return filter
+ \param id - id of file entry
+*/
+QString QtxPathDialog::filter( const int id ) const
+{
+ QString res;
+ if ( myEntries.contains( id ) )
+ res = myEntries[id].filter;
+ return res;
+}
+
+void QtxPathDialog::setFilter( const int id, const QString& filter )
+{
+ if ( myEntries.contains( id ) )
+ myEntries[id].filter = filter;
+}
+
/*!
\return line edit of file entry
\param id - id of file entry
\param mode - mode of entry
\param id - proposed id (if it is -1, then id will be chosen automatically)
*/
-int QtxPathDialog::createFileEntry( const QString& lab, const int mode, const int id )
+int QtxPathDialog::createFileEntry( const QString& lab, const int mode, const QString& filter, const int id )
{
int num = id;
if ( num == -1 )
FileEntry entry;
entry.dlg = 0;
entry.mode = mode;
+ entry.filter = filter;
new QLabel( lab, myEntriesFrame );
entry.edit = new QLineEdit( myEntriesFrame );
entry.btn = new QPushButton( myEntriesFrame );
entry.btn->setAutoDefault( false );
- entry.btn->setPixmap( QPixmap( open_icon ) );
+ entry.btn->setIcon( QPixmap( open_icon ) );
connect( entry.btn, SIGNAL( clicked() ), this, SLOT( onBrowse() ) );
connect( entry.edit, SIGNAL( returnPressed() ), this, SLOT( onReturnPressed() ) );
*/
void QtxPathDialog::initialize()
{
- setCaption( tr( "File dialog" ) );
+ setWindowTitle( tr( "File dialog" ) );
QVBoxLayout* main = new QVBoxLayout( mainFrame() );
- QtxGroupBox* mainGroup = new QtxGroupBox( 1, Qt::Horizontal, "", mainFrame() );
- mainGroup->setFrameStyle( QFrame::NoFrame );
- mainGroup->setInsideMargin( 0 );
- main->addWidget( mainGroup );
+ main->setMargin( 0 );
- myEntriesFrame = new QGroupBox( 3, Qt::Horizontal, "", mainGroup );
+ QtxGroupBox* base = new QtxGroupBox( "", mainFrame() );
+ main->addWidget( base );
+
+ QtxGridBox* mainGroup = new QtxGridBox( 1, Qt::Horizontal, base, 0 );
+ base->setWidget( mainGroup );
+
+ myEntriesFrame = new QtxGridBox( 3, Qt::Horizontal, mainGroup );
myOptionsFrame = new QFrame( mainGroup );
}
/*!
\return list of filters
*/
-QStringList QtxPathDialog::prepareFilters() const
+QStringList QtxPathDialog::prepareFilters( const QString& filter ) const
{
QStringList res;
- if ( !myFilter.isEmpty() )
+ bool allFilter = false;
+ if ( !filter.isEmpty() )
{
- res = QStringList::split( ";;", myFilter );
- bool allFilter = false;
+ res = filter.split( ";;" );
for ( QStringList::ConstIterator it = res.begin(); it != res.end() && !allFilter; ++it )
{
QStringList wildCards = filterWildCards( *it );
- allFilter = wildCards.findIndex( "*.*" ) != -1;
+ allFilter = wildCards.indexOf( "*.*" ) != -1;
}
-
- if ( !allFilter )
- res.append( tr( "All files (*.*)" ) );
}
+ if ( !allFilter )
+ res.append( tr( "All files (*.*)" ) );
+
return res;
}
{
QStringList res;
- int b = theFilter.findRev( "(" );
- int e = theFilter.findRev( ")" );
+ int b = theFilter.lastIndexOf( "(" );
+ int e = theFilter.lastIndexOf( ")" );
if ( b != -1 && e != -1 )
{
- QString content = theFilter.mid( b + 1, e - b - 1 ).stripWhiteSpace();
- QStringList lst = QStringList::split( " ", content );
+ QString content = theFilter.mid( b + 1, e - b - 1 ).trimmed();
+ QStringList lst = content.split( " " );
for ( QStringList::ConstIterator it = lst.begin(); it != lst.end(); ++it )
- if ( (*it).find( "." ) != -1 )
- res.append( (*it).stripWhiteSpace() );
+ {
+ if ( (*it).indexOf( "." ) != -1 )
+ res.append( (*it).trimmed() );
+ }
}
return res;
}
if ( fName.isEmpty() )
return fName;
- QString filter = theFilter;
- if ( filter.isEmpty() )
- {
- QStringList filters = prepareFilters();
- if ( !filters.isEmpty() )
- filter = filters.first();
- }
+ QString filter;
+ QStringList filters = prepareFilters( theFilter );
+ if ( !filters.isEmpty() )
+ filter = filters.first();
QStringList wildCards = filterWildCards( filter );
if ( !wildCards.isEmpty() )
{
QString ext = wildCards.first();
- if ( ext.find( "." ) != -1 )
- ext = ext.mid( ext.find( "." ) + 1 );
+ if ( ext.indexOf( "." ) != -1 )
+ ext = ext.mid( ext.indexOf( "." ) + 1 );
if ( !ext.isEmpty() && !ext.contains( "*" ) )
fName = QDir::convertSeparators( fName ) + QString( "." ) + ext;
bool res = false;
if ( wid )
{
- const QObjectList* aChildren = wid->children();
- if ( aChildren )
+ const QObjectList& aChildren = wid->children();
+ for ( QObjectList::const_iterator it = aChildren.begin(); it != aChildren.end() && !res; ++it )
{
- for ( QObjectListIt it( *aChildren ); it.current() && !res; ++it )
- {
- if ( it.current()->isWidgetType() )
- res = ((QWidget*)it.current())->isVisibleTo( wid );
- }
- }
+ if ( (*it)->isWidgetType() )
+ res = ((QWidget*)(*it))->isVisibleTo( wid );
+ }
}
return res;
}
+
+void QtxPathDialog::updateVisibility()
+{
+ if ( hasVisibleChildren( myEntriesFrame ) )
+ myEntriesFrame->show();
+ else
+ myEntriesFrame->hide();
+
+ if ( hasVisibleChildren( myOptionsFrame ) )
+ myOptionsFrame->show();
+ else
+ myOptionsFrame->hide();
+}