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 <QStandardPaths>
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 QStringList docpaths = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);
00094 if (docpaths.isEmpty()) {
00095 path = "";
00096 } else {
00097 path = docpaths[0];
00098 }
00099 }
00100
00101 QString s;
00102 if (_mode == 1) {
00103
00104 s = QFileDialog::getSaveFileName(this, _caption, path, _filter, &_selectedfilter, options);
00105 } else if (_mode == 2) {
00106 s = QFileDialog::getExistingDirectory(this, _caption, path, 0);
00107 } else {
00108
00109 s = QFileDialog::getOpenFileName(this, _caption, path, _filter, &_selectedfilter);
00110 }
00111 if ( ! s.isEmpty() ) {
00112 setPath(s);
00113 if (_mode == 1 && _dlgconfirmoverwrite)
00114 _pathFromDialog = true;
00115 emit fileDialogPathChosen(s);
00116 }
00117 }
00118
00119
00120 void KLFPathChooser::setCaption(const QString& caption)
00121 {
00122 _caption = caption;
00123 }
00124
00125 void KLFPathChooser::setMode(int mode)
00126 {
00127 _mode = mode;
00128 _pathFromDialog = false;
00129 }
00130
00131 void KLFPathChooser::setFilter(const QString& filter)
00132 {
00133 _filter = filter;
00134 }
00135
00136 void KLFPathChooser::slotTextChanged()
00137 {
00138 _pathFromDialog = false;
00139 }
00140