[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
klfpathchooser.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * file klfpathchooser.cpp
3  * This file is part of the KLatexFormula Project.
4  * Copyright (C) 2011 by Philippe Faist
5  * philippe.faist at bluewin.ch
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 /* $Id: klfpathchooser.cpp 952 2016-12-26 07:36:43Z phfaist $ */
23 
24 #include <QLineEdit>
25 #include <QHBoxLayout>
26 #include <QPushButton>
27 #include <QFrame>
28 #include <QFileDialog>
29 #include <QStandardPaths>
30 #include <QDirModel>
31 #include <QCompleter>
32 
33 #include "klfpathchooser.h"
34 
35 
37  : QFrame(parent), _mode(0), _caption(), _filter(), _dlgconfirmoverwrite(true),
38  _pathFromDialog(false)
39 {
40  // setFrameShape(QFrame::Box);
41  // setFrameShadow(QFrame::Raised);
42  setFrameStyle(QFrame::NoFrame|QFrame::Plain);
43 
44  QHBoxLayout *lyt = new QHBoxLayout(this);
45  // lyt->setContentsMargins(2,2,2,2);
46  lyt->setContentsMargins(0,0,0,0);
47  lyt->setSpacing(2);
48  txtPath = new QLineEdit(this);
49  lyt->addWidget(txtPath);
50  btnBrowse = new QPushButton(tr("Browse"), this);
51  btnBrowse->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
52  lyt->addWidget(btnBrowse);
53 
54  // set a filename completer for txtPath
55  QDirModel *dirModel = new QDirModel(QStringList(),
56  QDir::AllEntries|QDir::AllDirs|QDir::NoDotAndDotDot,
57  QDir::DirsFirst|QDir::IgnoreCase, this);
58  QCompleter *fileNameCompleter = new QCompleter(this);
59  fileNameCompleter->setModel(dirModel);
60  txtPath->setCompleter(fileNameCompleter);
61 
62  // connect signals
63  connect(txtPath, SIGNAL(textChanged(const QString&)), this, SLOT(slotTextChanged()));
64  connect(btnBrowse, SIGNAL(clicked()), this, SLOT(requestBrowse()));
65 }
66 
68 {
69 }
70 
71 
73 {
74  return txtPath->text();
75 }
76 
78 {
79  txtPath->setText(path);
80  _pathFromDialog = false;
81 }
82 
84 {
85  QFileDialog::Options options = 0;
86  if (_mode == 1 && !_dlgconfirmoverwrite)
87  options |= QFileDialog::DontConfirmOverwrite;
88 
89  QString path;
90  if (!txtPath->text().isEmpty())
91  path = txtPath->text();
92  else {
93  QStringList docpaths = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);
94  if (docpaths.isEmpty()) {
95  path = "";
96  } else {
97  path = docpaths[0];
98  }
99  }
100 
101  QString s;
102  if (_mode == 1) {
103  // save
104  s = QFileDialog::getSaveFileName(this, _caption, path, _filter, &_selectedfilter, options);
105  } else if (_mode == 2) {
106  s = QFileDialog::getExistingDirectory(this, _caption, path, 0/*options*/);
107  } else {
108  // open
109  s = QFileDialog::getOpenFileName(this, _caption, path, _filter, &_selectedfilter);
110  }
111  if ( ! s.isEmpty() ) {
112  setPath(s);
113  if (_mode == 1 && _dlgconfirmoverwrite)
114  _pathFromDialog = true;
115  emit fileDialogPathChosen(s);
116  }
117 }
118 
119 
121 {
122  _caption = caption;
123 }
124 
126 {
127  _mode = mode;
128  _pathFromDialog = false; // no overwrite confirmed yet
129 }
130 
132 {
133  _filter = filter;
134 }
135 
136 void KLFPathChooser::slotTextChanged()
137 {
138  _pathFromDialog = false;
139 }
140 
virtual void setMode(int mode)
virtual void setFilter(const QString &filter)
KLFPathChooser(QWidget *parent)
QStringList standardLocations(StandardLocation type)
bool isEmpty() const
bool isEmpty() const
virtual void setCaption(const QString &caption)
virtual int mode() const
virtual QString filter() const
virtual QString caption() const
virtual ~KLFPathChooser()
virtual void setPath(const QString &path)
virtual QString path() const
void fileDialogPathChosen(const QString &fname)
virtual void requestBrowse()

Generated by doxygen 1.8.13