From 1a33586cf96a825035898c318b65581e1c5a9c4b Mon Sep 17 00:00:00 2001 From: Jean-Paul Chaput Date: Thu, 16 Sep 2010 20:41:39 +0000 Subject: [PATCH] * ./vslsisapd/src/configuration: - Bug: In FilePathEdit, do not sets the file/path if the Dialog has been cancelled. - Bug: In FilePathEdit, do not uses the native dialog (called by static methods) when under Linux/GtkStyle as the native widget seems to be badly linked (bug in GtkStyle?). --- .../src/configuration/src/ConfEditorMain.cpp | 1 + .../src/configuration/src/FilePathEdit.cpp | 59 ++++++++++++++----- .../src/configuration/src/ParameterWidget.cpp | 4 +- .../src/vlsisapd/configuration/FilePathEdit.h | 6 +- 4 files changed, 51 insertions(+), 19 deletions(-) diff --git a/vlsisapd/src/configuration/src/ConfEditorMain.cpp b/vlsisapd/src/configuration/src/ConfEditorMain.cpp index ca923dc6..a02dba7f 100644 --- a/vlsisapd/src/configuration/src/ConfEditorMain.cpp +++ b/vlsisapd/src/configuration/src/ConfEditorMain.cpp @@ -94,6 +94,7 @@ int main ( int argc, char* argv[] ) ConfEditorWidget* editor = new ConfEditorWidget (); editor->show (); + //QFileDialog::getOpenFileName(NULL, "Choose file", "", ""); returnCode = qa->exec (); } diff --git a/vlsisapd/src/configuration/src/FilePathEdit.cpp b/vlsisapd/src/configuration/src/FilePathEdit.cpp index 33c6b435..55531b1d 100644 --- a/vlsisapd/src/configuration/src/FilePathEdit.cpp +++ b/vlsisapd/src/configuration/src/FilePathEdit.cpp @@ -35,38 +35,67 @@ namespace Cfg { FilePathEdit::FilePathEdit ( Type type, std::string filter, QWidget* parent ) - : QLineEdit(parent) - , _type (type) - , _filter (filter) + : QLineEdit (parent) + , _chooseButton (new QToolButton(this)) + , _filePathDialog(NULL) + , _type (type) + , _filter (filter) { - _chooseButton = new QToolButton(this); QPixmap pixmap(":/images/choose.png"); - _chooseButton->setIcon(QIcon(pixmap)); - _chooseButton->setIconSize(pixmap.size()); - _chooseButton->setCursor(Qt::ArrowCursor); + + _chooseButton->setIcon (QIcon(pixmap)); + _chooseButton->setIconSize (pixmap.size()); + _chooseButton->setCursor (Qt::ArrowCursor); _chooseButton->setStyleSheet("QToolButton { border: none; padding: 0px; }"); - _chooseButton->setToolTip(tr( (type&Filename)?"Choose file ...":"Choose directory ..." )); - connect(_chooseButton, SIGNAL(clicked()), this, SLOT(updateLineEdit())); + _chooseButton->setToolTip (tr( (type&FileName)?"Choose file ...":"Choose directory ..." )); + + connect ( _chooseButton, SIGNAL(clicked()), this, SLOT(updateLineEdit()) ); + setStyleSheet(QString("QLineEdit { padding-right: %1px; } ").arg(_chooseButton->sizeHint().width() + 1)); } void FilePathEdit::resizeEvent(QResizeEvent *) { - QSize sz = _chooseButton->sizeHint(); + QSize sz = _chooseButton->sizeHint (); int l, t, r, b; - getContentsMargins(&l, &t, &r, &b); - _chooseButton->move(rect().right() - r - sz.width(), (rect().bottom() + 2 - sz.height())/2); + getContentsMargins (&l, &t, &r, &b ); + _chooseButton->move ( rect().right() - r - sz.width(), (rect().bottom() + 2 - sz.height())/2 ); } void FilePathEdit::updateLineEdit() { - if (_type == FilePathEdit::Filename) { - setText(QFileDialog::getOpenFileName(this, tr("Choose file"), "", tr(_filter.c_str()))); + QString result; + +#if defined(__APPLE__) + if (_type == FileName) { + result = QFileDialog::getOpenFileName ( this, tr("Choose File"), "", tr(_filter.c_str()) ); } else { - setText(QFileDialog::getExistingDirectory(this, tr("Choose directory"), "", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks)); + result = QFileDialog::getExistingDirectory ( this + , tr("Choose Directory") + , "" + , QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks + ); } +#else + if ( _filePathDialog == NULL ) { + _filePathDialog = new QFileDialog ( this, tr((_type&FileName)?"Choose File":"Choose Directory") ); + if (_type == FileName) { + _filePathDialog->setFileMode ( QFileDialog::ExistingFile ); + } else { + _filePathDialog->setOption ( QFileDialog::ShowDirsOnly ); + _filePathDialog->setOption ( QFileDialog::DontResolveSymlinks ); + } + _filePathDialog->setViewMode ( QFileDialog::Detail ); + _filePathDialog->selectNameFilter ( tr(_filter.c_str()) ); + } + + if ( _filePathDialog->exec() == QDialog::Accepted ) + result = _filePathDialog->selectedFiles().at(0); +#endif + + if ( not result.isEmpty() ) setText ( result ); } diff --git a/vlsisapd/src/configuration/src/ParameterWidget.cpp b/vlsisapd/src/configuration/src/ParameterWidget.cpp index 80d5a604..ea003a97 100644 --- a/vlsisapd/src/configuration/src/ParameterWidget.cpp +++ b/vlsisapd/src/configuration/src/ParameterWidget.cpp @@ -64,8 +64,8 @@ namespace Cfg { case Parameter::String: { QLineEdit* lineEdit = NULL; - if ( hasFlags(IsFileName) ) lineEdit = new FilePathEdit ( FilePathEdit::Filename ); - else if ( hasFlags(IsPathName) ) lineEdit = new FilePathEdit ( FilePathEdit::Pathname ); + if ( hasFlags(IsFileName) ) lineEdit = new FilePathEdit ( FilePathEdit::FileName ); + else if ( hasFlags(IsPathName) ) lineEdit = new FilePathEdit ( FilePathEdit::PathName ); else lineEdit = new QLineEdit(); lineEdit->setSizePolicy ( QSizePolicy::Expanding, QSizePolicy::Fixed ); diff --git a/vlsisapd/src/configuration/src/vlsisapd/configuration/FilePathEdit.h b/vlsisapd/src/configuration/src/vlsisapd/configuration/FilePathEdit.h index c6c540d6..abf60174 100644 --- a/vlsisapd/src/configuration/src/vlsisapd/configuration/FilePathEdit.h +++ b/vlsisapd/src/configuration/src/vlsisapd/configuration/FilePathEdit.h @@ -29,6 +29,7 @@ #include #include class QToolButton; +class QFileDialog; namespace Cfg { @@ -38,15 +39,16 @@ namespace Cfg { { Q_OBJECT; public: - enum Type { Filename=0x1, Pathname=0x2 }; + enum Type { FileName=0x1, PathName=0x2 }; public: - FilePathEdit ( Type type=Filename, std::string filter="", QWidget* parent=NULL ); + FilePathEdit ( Type type=FileName, std::string filter="", QWidget* parent=NULL ); protected: void resizeEvent ( QResizeEvent* ); private slots: void updateLineEdit (); private: QToolButton* _chooseButton; + QFileDialog* _filePathDialog; Type _type; std::string _filter; };