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 button.setIcon(QApplication::style()->standardIcon(QStyle::SP_DirIcon)); |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
88 lineedit.setCompleter(&completer); |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
89 lineedit.setClearButtonEnabled(true); |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
90 hlay.setMargin(0); |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
91 hlay.addWidget(&lineedit); |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
92 hlay.addWidget(&button); |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
93 |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
94 connect(&button, SIGNAL(clicked()), q, SLOT(slotButtonClicked())); |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
95 connect(&lineedit, &QLineEdit::textChanged, q, &FileNameRequester::fileNameChanged); |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
96 } |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
97 |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
98 FileNameRequester::Private::~Private() {} |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
99 |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
100 FileNameRequester::FileNameRequester(QWidget *p) |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
101 : QWidget(p), d(new Private(this)) |
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 } |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
105 |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
106 FileNameRequester::FileNameRequester(QDir::Filters f, 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 d->dirmodel.setFilter(f); |
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() |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
113 { |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
114 delete d; |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
115 } |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
116 |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
117 void FileNameRequester::setFileName(const QString &file) |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
118 { |
53
a43d8cf2fa95
Various fixes and improvements. Fix windows packaging.
Andre Heinecke <andre.heinecke@intevation.de>
diff
changeset
|
119 const QString converted = QDir::toNativeSeparators(file); |
a43d8cf2fa95
Various fixes and improvements. Fix windows packaging.
Andre Heinecke <andre.heinecke@intevation.de>
diff
changeset
|
120 d->lineedit.setText(converted); |
50
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 QString FileNameRequester::fileName() const |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
124 { |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
125 return d->lineedit.text(); |
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 void FileNameRequester::setExistingOnly(bool on) |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
129 { |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
130 d->existingOnly = on; |
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 bool FileNameRequester::existingOnly() const |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
134 { |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
135 return d->existingOnly; |
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 void FileNameRequester::setFilter(QDir::Filters f) |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
139 { |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
140 d->dirmodel.setFilter(f); |
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 QDir::Filters FileNameRequester::filter() const |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
144 { |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
145 return d->dirmodel.filter(); |
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 void FileNameRequester::setNameFilter(const QString &nameFilter) |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
149 { |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
150 d->nameFilter = nameFilter; |
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 QString FileNameRequester::nameFilter() const |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
154 { |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
155 return d->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 void FileNameRequester::Private::slotButtonClicked() |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
159 { |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
160 const QString fileName = q->requestFileName(); |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
161 if (!fileName.isEmpty()) { |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
162 q->setFileName(fileName); |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
163 } |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
164 } |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
165 |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
166 QString FileNameRequester::requestFileName() |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
167 { |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
168 const QDir::Filters filters = filter(); |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
169 if ((filters & QDir::Dirs) && !(filters & QDir::Files)) { |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
170 return QFileDialog::getExistingDirectory(this); |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
171 } else if (d->existingOnly) { |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
172 return QFileDialog::getOpenFileName(this, QString(), QString(), d->nameFilter); |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
173 } else { |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
174 return QFileDialog::getSaveFileName(this, QString(), QString(), d->nameFilter); |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
175 } |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
176 } |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
177 |
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff
changeset
|
178 #include "moc_filenamerequester.cpp" |