tuchulcha  0.10.1
Graphical Workflow Configuration Editor
QDirEdit.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2009 Jens-Malte Gottfried
2 
3  This file is part of Tuchulcha.
4 
5  Tuchulcha is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  Tuchulcha is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with Tuchulcha. If not, see <http://www.gnu.org/licenses/>.
17 */
24 #include "QDirEdit.h"
25 #include <QCompleter>
26 #include <QDirModel>
27 #include <QFileDialog>
28 #include <QToolButton>
29 #include <QtEvents>
30 
32  // initial values
33  _files = false;
34  _writeFile = false;
35 
36  // tool button to open browse dialog
37  _browseButton = new QToolButton(this);
38  _browseButton->setText(tr("..."));
39  _browseButton->setCursor(QCursor(Qt::ArrowCursor));
40  _browseButton->setFocusPolicy(Qt::NoFocus);
41  QObject::connect(
42  _browseButton, SIGNAL(clicked()),this, SLOT(fileDialog()));
43 
44  // completer
45  QCompleter* complt = new QCompleter(this);
46  complt->setModel(new QDirModel(
47  QStringList(),
48  QDir::AllDirs | QDir::NoDotAndDotDot,
49  QDir::Name, complt));
50  setCompleter(complt);
51 }
52 
53 QDirEdit::QDirEdit(QWidget* p) :
54  QLineEdit(p) {
55  _init();
56 }
57 
58 QDirEdit::QDirEdit(const QString& content, QWidget* p) :
59  QLineEdit(content, p) {
60  _init();
61 }
62 
64  emit dialogOpen(true);
65  QString newVal;
66  if (_files) {
67  if (_writeFile)
68  newVal = QFileDialog::getSaveFileName(
69  this,tr("select file to save to"), text());
70  else {
71  QStringList list = QFileDialog::getOpenFileNames(
72  this, tr("select one or more file(s) to open"), text());
73  if (list.size() > 0) {
74  newVal = list.join(";");
75  }
76  }
77  }
78  else
79  newVal = QFileDialog::getExistingDirectory(
80  this, tr("select directory"),text());
81  emit dialogOpen(false);
82  if(!newVal.isEmpty())
83  setText(newVal);
84 }
85 
86 void QDirEdit::acceptFiles(bool files, bool write) {
87  _files = files;
88  _writeFile = write;
89  QDirModel* dirModel = qobject_cast<QDirModel*>(completer()->model());
90  Q_ASSERT(dirModel);
91  if (_files)
92  dirModel->setFilter(
93  QDir::AllDirs | QDir::Files | QDir::NoDotAndDotDot);
94  else
95  dirModel->setFilter(
96  QDir::AllDirs | QDir::NoDotAndDotDot);
97 }
98 
99 void QDirEdit::resizeEvent(QResizeEvent* e) {
100  // make the tool button fit on the right side
101  int h = e->size().height();
102  // move the line edit to make room for the tool button
103  setContentsMargins(0, 0, h, 0);
104  // put the tool button in its place
105  _browseButton->resize(h, h);
106  _browseButton->move(width() - h, 0);
107 }
Declaration of class DirEdit.
bool _files
look for files instead of directories
Definition: QDirEdit.h:38
QDirEdit(QWidget *parent=0)
inherited constructor
Definition: QDirEdit.cpp:53
void resizeEvent(QResizeEvent *e)
handle resize events
Definition: QDirEdit.cpp:99
bool _writeFile
use saveFile dialog instead of openFile dialog
Definition: QDirEdit.h:40
void acceptFiles(bool files=true, bool write=true)
allow files instead of directories
Definition: QDirEdit.cpp:86
void _init()
set initial values
Definition: QDirEdit.cpp:31
void dialogOpen(bool status)
emit status of file dialog
QToolButton * _browseButton
browse button
Definition: QDirEdit.h:36
void fileDialog()
show open dialog and use the selected directory as entry
Definition: QDirEdit.cpp:63