comparison src/filenamerequester.cpp @ 50:36ee5dd46fd3

Add GUI New Mainwindow that allows to set output formats and input files through a GUI.
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 19 Jul 2016 12:19:45 +0200
parents
children a43d8cf2fa95
comparison
equal deleted inserted replaced
49:a849b1de248f 50:36ee5dd46fd3
1 /* Copyright (C) 2016 by ETH Zürich
2 * Software engineering by Intevation GmbH
3 *
4 * This file is Free Software under the GNU GPL (v>=2)
5 * and comes with ABSOLUTELY NO WARRANTY!
6 * See LICENSE.txt for details.
7 */
8
9 /* This file was originally taken from Libkleo where the license was:
10 ui/filenamerequester.cpp
11
12 This file is part of Kleopatra, the KDE keymanager
13 Copyright (c) 2007 Klarälvdalens Datakonsult AB
14
15 Kleopatra is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
19
20 Kleopatra is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28
29 In addition, as a special exception, the copyright holders give
30 permission to link the code of this program with any edition of
31 the Qt library by Trolltech AS, Norway (or with modified versions
32 of Qt that use the same license as Qt), and distribute linked
33 combinations including the two. You must obey the GNU General
34 Public License in all respects for all of the code used other than
35 Qt. If you modify this file, you may extend this exception to
36 your version of the file, but you are not obligated to do so. If
37 you do not wish to do so, delete this exception statement from
38 your version.
39 */
40
41 #include "filenamerequester.h"
42
43 #include <QLineEdit>
44
45 #include <QHBoxLayout>
46 #include <QToolButton>
47 #include <QCompleter>
48 #include <QDirModel>
49 #include <QString>
50 #include <QFileDialog>
51 #include <QApplication>
52 #include <QStyle>
53
54 class FileNameRequester::Private
55 {
56 friend class FileNameRequester;
57 FileNameRequester *const q;
58 public:
59 explicit Private(FileNameRequester *qq);
60 ~Private();
61
62 private:
63 void slotButtonClicked();
64
65 private:
66 QDirModel dirmodel;
67 QCompleter completer;
68
69 QLineEdit lineedit;
70 QToolButton button;
71 QHBoxLayout hlay;
72
73 QString nameFilter;
74 bool existingOnly;
75 };
76
77 FileNameRequester::Private::Private(FileNameRequester *qq)
78 : q(qq),
79 dirmodel(),
80 completer(&dirmodel),
81 lineedit(q),
82 button(q),
83 hlay(q),
84 nameFilter(),
85 existingOnly(true)
86 {
87 dirmodel.setObjectName(QStringLiteral("dirmodel"));
88 completer.setObjectName(QStringLiteral("completer"));
89 lineedit.setObjectName(QStringLiteral("lineedit"));
90 button.setObjectName(QStringLiteral("button"));
91 hlay.setObjectName(QStringLiteral("hlay"));
92
93 button.setIcon(QApplication::style()->standardIcon(QStyle::SP_DirIcon));
94 lineedit.setCompleter(&completer);
95 lineedit.setClearButtonEnabled(true);
96 hlay.setMargin(0);
97 hlay.addWidget(&lineedit);
98 hlay.addWidget(&button);
99
100 connect(&button, SIGNAL(clicked()), q, SLOT(slotButtonClicked()));
101 connect(&lineedit, &QLineEdit::textChanged, q, &FileNameRequester::fileNameChanged);
102 }
103
104 FileNameRequester::Private::~Private() {}
105
106 FileNameRequester::FileNameRequester(QWidget *p)
107 : QWidget(p), d(new Private(this))
108 {
109
110 }
111
112 FileNameRequester::FileNameRequester(QDir::Filters f, QWidget *p)
113 : QWidget(p), d(new Private(this))
114 {
115 d->dirmodel.setFilter(f);
116 }
117
118 FileNameRequester::~FileNameRequester()
119 {
120 delete d;
121 }
122
123 void FileNameRequester::setFileName(const QString &file)
124 {
125 d->lineedit.setText(file);
126 }
127
128 QString FileNameRequester::fileName() const
129 {
130 return d->lineedit.text();
131 }
132
133 void FileNameRequester::setExistingOnly(bool on)
134 {
135 d->existingOnly = on;
136 }
137
138 bool FileNameRequester::existingOnly() const
139 {
140 return d->existingOnly;
141 }
142
143 void FileNameRequester::setFilter(QDir::Filters f)
144 {
145 d->dirmodel.setFilter(f);
146 }
147
148 QDir::Filters FileNameRequester::filter() const
149 {
150 return d->dirmodel.filter();
151 }
152
153 void FileNameRequester::setNameFilter(const QString &nameFilter)
154 {
155 d->nameFilter = nameFilter;
156 }
157
158 QString FileNameRequester::nameFilter() const
159 {
160 return d->nameFilter;
161 }
162
163 void FileNameRequester::Private::slotButtonClicked()
164 {
165 const QString fileName = q->requestFileName();
166 if (!fileName.isEmpty()) {
167 q->setFileName(fileName);
168 }
169 }
170
171 QString FileNameRequester::requestFileName()
172 {
173 const QDir::Filters filters = filter();
174 if ((filters & QDir::Dirs) && !(filters & QDir::Files)) {
175 return QFileDialog::getExistingDirectory(this);
176 } else if (d->existingOnly) {
177 return QFileDialog::getOpenFileName(this, QString(), QString(), d->nameFilter);
178 } else {
179 return QFileDialog::getSaveFileName(this, QString(), QString(), d->nameFilter);
180 }
181 }
182
183 #include "moc_filenamerequester.cpp"
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)