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