Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <QLineEdit>
00025 #include <QHBoxLayout>
00026 #include <QPushButton>
00027 #include <QFrame>
00028 #include <QFileDialog>
00029 #include <QDesktopServices>
00030 #include <QDirModel>
00031 #include <QCompleter>
00032
00033 #include "klfpathchooser.h"
00034
00035
00036 KLFPathChooser::KLFPathChooser(QWidget *parent)
00037 : QFrame(parent), _mode(0), _caption(), _filter(), _dlgconfirmoverwrite(true),
00038 _pathFromDialog(false)
00039 {
00040
00041
00042 setFrameStyle(QFrame::NoFrame|QFrame::Plain);
00043
00044 QHBoxLayout *lyt = new QHBoxLayout(this);
00045
00046 lyt->setContentsMargins(0,0,0,0);
00047 lyt->setSpacing(2);
00048 txtPath = new QLineEdit(this);
00049 lyt->addWidget(txtPath);
00050 btnBrowse = new QPushButton(tr("Browse"), this);
00051 btnBrowse->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
00052 lyt->addWidget(btnBrowse);
00053
00054
00055 QDirModel *dirModel = new QDirModel(QStringList(),
00056 QDir::AllEntries|QDir::AllDirs|QDir::NoDotAndDotDot,
00057 QDir::DirsFirst|QDir::IgnoreCase, this);
00058 QCompleter *fileNameCompleter = new QCompleter(this);
00059 fileNameCompleter->setModel(dirModel);
00060 txtPath->setCompleter(fileNameCompleter);
00061
00062
00063 connect(txtPath, SIGNAL(textChanged(const QString&)), this, SLOT(slotTextChanged()));
00064 connect(btnBrowse, SIGNAL(clicked()), this, SLOT(requestBrowse()));
00065 }
00066
00067 KLFPathChooser::~KLFPathChooser()
00068 {
00069 }
00070
00071
00072 QString KLFPathChooser::path() const
00073 {
00074 return txtPath->text();
00075 }
00076
00077 void KLFPathChooser::setPath(const QString& path)
00078 {
00079 txtPath->setText(path);
00080 _pathFromDialog = false;
00081 }
00082
00083 void KLFPathChooser::requestBrowse()
00084 {
00085 QFileDialog::Options options = 0;
00086 if (_mode == 1 && !_dlgconfirmoverwrite)
00087 options |= QFileDialog::DontConfirmOverwrite;
00088
00089 QString path;
00090 if (!txtPath->text().isEmpty())
00091 path = txtPath->text();
00092 else
00093 path = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
00094
00095 QString s;
00096 if (_mode == 1) {
00097
00098 s = QFileDialog::getSaveFileName(this, _caption, path, _filter, &_selectedfilter, options);
00099 } else if (_mode == 2) {
00100 s = QFileDialog::getExistingDirectory(this, _caption, path, 0);
00101 } else {
00102
00103 s = QFileDialog::getOpenFileName(this, _caption, path, _filter, &_selectedfilter);
00104 }
00105 if ( ! s.isEmpty() ) {
00106 setPath(s);
00107 if (_mode == 1 && _dlgconfirmoverwrite)
00108 _pathFromDialog = true;
00109 emit fileDialogPathChosen(s);
00110 }
00111 }
00112
00113
00114 void KLFPathChooser::setCaption(const QString& caption)
00115 {
00116 _caption = caption;
00117 }
00118
00119 void KLFPathChooser::setMode(int mode)
00120 {
00121 _mode = mode;
00122 _pathFromDialog = false;
00123 }
00124
00125 void KLFPathChooser::setFilter(const QString& filter)
00126 {
00127 _filter = filter;
00128 }
00129
00130 void KLFPathChooser::slotTextChanged()
00131 {
00132 _pathFromDialog = false;
00133 }
00134