Libkleo

filenamerequester.cpp
1/* -*- mode: c++; c-basic-offset:4 -*-
2 ui/filenamerequester.cpp
3
4 This file is part of Kleopatra, the KDE keymanager
5 SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8*/
9
10#include <config-libkleo.h>
11
12#include "filenamerequester.h"
13
14#include <KLocalizedString>
15
16#include <QCompleter>
17#include <QEvent>
18#include <QFileDialog>
19#include <QFileSystemModel>
20#include <QHBoxLayout>
21#include <QLineEdit>
22#include <QString>
23#include <QToolButton>
24
25using namespace Kleo;
26
27class Q_DECL_HIDDEN FileNameRequester::FileNameRequesterPrivate
28{
29 friend class ::Kleo::FileNameRequester;
30 FileNameRequester *const q;
31
32public:
33 explicit FileNameRequesterPrivate(FileNameRequester *qq);
34 ~FileNameRequesterPrivate();
35
36private:
37 void slotButtonClicked();
38
39private:
40#ifndef QT_NO_DIRMODEL
41 QFileSystemModel dirmodel;
42 QCompleter completer;
43#else
45#endif
46
47 QLineEdit lineedit;
48 QToolButton button;
49 QHBoxLayout hlay;
50
51 QString nameFilter;
52 bool existingOnly;
53};
54
55FileNameRequester::FileNameRequesterPrivate::FileNameRequesterPrivate(FileNameRequester *qq)
56 : q(qq)
57 ,
58#ifndef QT_NO_DIRMODEL
59 dirmodel()
60 , completer(&dirmodel)
61 ,
62#else
63 filter()
64 ,
65#endif
66 lineedit(q)
67 , button(q)
68 , hlay(q)
69 , nameFilter()
70 , existingOnly(true)
71{
72#ifndef QT_NO_DIRMODEL
73 dirmodel.setObjectName(QLatin1StringView("dirmodel"));
74 completer.setObjectName(QLatin1StringView("completer"));
75#endif
76 lineedit.setObjectName(QLatin1StringView("lineedit"));
77 button.setObjectName(QLatin1StringView("button"));
78 hlay.setObjectName(QLatin1StringView("hlay"));
79
80 button.setIcon(QIcon::fromTheme(QStringLiteral("document-open")));
81 button.setToolTip(i18nc("@info:tooltip", "Open file dialog"));
82 button.setAccessibleName(i18n("Open file dialog"));
83#ifndef QT_NO_DIRMODEL
84 lineedit.setCompleter(&completer);
85#endif
86 lineedit.setClearButtonEnabled(true);
87 hlay.setContentsMargins(0, 0, 0, 0);
88 hlay.addWidget(&lineedit);
89 hlay.addWidget(&button);
90 q->setFocusPolicy(lineedit.focusPolicy());
91 q->setFocusProxy(&lineedit);
92
93 connect(&button, &QToolButton::clicked, q, [this]() {
94 slotButtonClicked();
95 });
96 connect(&lineedit, &QLineEdit::textChanged, q, &FileNameRequester::fileNameChanged);
97}
98
99FileNameRequester::FileNameRequesterPrivate::~FileNameRequesterPrivate()
100{
101}
102
103FileNameRequester::FileNameRequester(QWidget *p)
104 : QWidget(p)
105 , d(new FileNameRequesterPrivate(this))
106{
107}
108
109FileNameRequester::FileNameRequester(QDir::Filters f, QWidget *p)
110 : QWidget(p)
111 , d(new FileNameRequesterPrivate(this))
112{
113#ifndef QT_NO_DIRMODEL
114 d->dirmodel.setFilter(f);
115#else
116 d->filter = f;
117#endif
118}
119
120FileNameRequester::~FileNameRequester() = default;
121
122void FileNameRequester::setFileName(const QString &file)
123{
124 d->lineedit.setText(file);
125}
126
127QString FileNameRequester::fileName() const
128{
129 return d->lineedit.text();
130}
131
132void FileNameRequester::setExistingOnly(bool on)
133{
134 d->existingOnly = on;
135}
136
137bool FileNameRequester::existingOnly() const
138{
139 return d->existingOnly;
140}
141
142void FileNameRequester::setFilter(QDir::Filters f)
143{
144#ifndef QT_NO_DIRMODEL
145 d->dirmodel.setFilter(f);
146#else
147 d->filter = f;
148#endif
149}
150
151QDir::Filters FileNameRequester::filter() const
152{
153#ifndef QT_NO_DIRMODEL
154 return d->dirmodel.filter();
155#else
156 return d->filter;
157#endif
158}
159
160void FileNameRequester::setNameFilter(const QString &nameFilter)
161{
162 d->nameFilter = nameFilter;
163}
164
165QString FileNameRequester::nameFilter() const
166{
167 return d->nameFilter;
168}
169
170void FileNameRequester::setAccessibleNameOfLineEdit(const QString &name)
171{
172 d->lineedit.setAccessibleName(name);
173}
174
175void FileNameRequester::FileNameRequesterPrivate::slotButtonClicked()
176{
177 const QString fileName = q->requestFileName();
178 if (!fileName.isEmpty()) {
179 q->setFileName(fileName);
180 }
181}
182
183bool FileNameRequester::event(QEvent *e)
184{
185 if (e->type() == QEvent::ToolTipChange) {
186 d->lineedit.setToolTip(toolTip());
187 }
188 return QWidget::event(e);
189}
190
191QString FileNameRequester::requestFileName()
192{
193#ifndef QT_NO_FILEDIALOG
194 const QDir::Filters filters = filter();
195 if ((filters & QDir::Dirs) && !(filters & QDir::Files)) {
197 } else if (d->existingOnly) {
198 return QFileDialog::getOpenFileName(this, QString(), QString(), d->nameFilter);
199 } else {
200 return QFileDialog::getSaveFileName(this, QString(), fileName(), d->nameFilter);
201 }
202#else
203 return QString();
204#endif
205}
206
207void FileNameRequester::setButtonHint(const QString &text)
208{
209 d->button.setToolTip(text);
210 d->button.setAccessibleName(text);
211}
212
213#include "moc_filenamerequester.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void clicked(bool checked)
Type type() const const
QString getExistingDirectory(QWidget *parent, const QString &caption, const QString &dir, Options options)
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, Options options)
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, Options options)
QIcon fromTheme(const QString &name)
void textChanged(const QString &text)
bool isEmpty() const const
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
virtual bool event(QEvent *event) override
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:46:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.