Visually validate input path in torrent creator dialog
[qBittorrent.git] / src / gui / fspathedit_p.h
blob69592281b6e1294052e82a087513eb4d316a8c31
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2016 Eugene Shalygin
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program 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 General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #pragma once
31 #include <QComboBox>
32 #include <QFileIconProvider>
33 #include <QLineEdit>
34 #include <QtContainerFwd>
35 #include <QValidator>
37 #include "base/pathfwd.h"
39 class QAction;
40 class QCompleter;
41 class QContextMenuEvent;
42 class QFileSystemModel;
43 class QKeyEvent;
45 namespace Private
47 class FileSystemPathValidator final : public QValidator
49 Q_OBJECT
50 Q_DISABLE_COPY_MOVE(FileSystemPathValidator)
52 public:
53 enum class TestResult
55 OK,
56 DoesNotExist,
57 NotADir,
58 NotAFile,
59 CantRead,
60 CantWrite
63 FileSystemPathValidator(QObject *parent = nullptr);
65 bool strictMode() const;
66 void setStrictMode(bool value);
68 bool existingOnly() const;
69 void setExistingOnly(bool value);
71 bool filesOnly() const;
72 void setFilesOnly(bool value);
74 bool directoriesOnly() const;
75 void setDirectoriesOnly(bool value);
77 bool checkReadPermission() const;
78 void setCheckReadPermission(bool value);
80 bool checkWritePermission() const;
81 void setCheckWritePermission(bool value);
83 TestResult lastTestResult() const;
84 QValidator::State lastValidationState() const;
86 QValidator::State validate(QString &input, int &pos) const override;
88 private:
89 TestResult testPath(const Path &path) const;
91 bool m_strictMode = false;
92 bool m_existingOnly = false;
93 bool m_filesOnly = false;
94 bool m_directoriesOnly = false;
95 bool m_checkReadPermission = false;
96 bool m_checkWritePermission = false;
98 mutable TestResult m_lastTestResult = TestResult::DoesNotExist;
99 mutable QValidator::State m_lastValidationState = QValidator::Invalid;
102 class IFileEditorWithCompletion
104 public:
105 virtual ~IFileEditorWithCompletion() = default;
106 virtual void completeDirectoriesOnly(bool completeDirsOnly) = 0;
107 virtual void setFilenameFilters(const QStringList &filters) = 0;
108 virtual void setBrowseAction(QAction *action) = 0;
109 virtual void setValidator(QValidator *validator) = 0;
110 virtual Path placeholder() const = 0;
111 virtual void setPlaceholder(const Path &val) = 0;
112 virtual QWidget *widget() = 0;
115 class FileLineEdit final : public QLineEdit, public IFileEditorWithCompletion
117 Q_OBJECT
118 Q_DISABLE_COPY_MOVE(FileLineEdit)
120 public:
121 FileLineEdit(QWidget *parent = nullptr);
122 ~FileLineEdit();
124 void completeDirectoriesOnly(bool completeDirsOnly) override;
125 void setFilenameFilters(const QStringList &filters) override;
126 void setBrowseAction(QAction *action) override;
127 void setValidator(QValidator *validator) override;
128 Path placeholder() const override;
129 void setPlaceholder(const Path &val) override;
130 QWidget *widget() override;
132 protected:
133 void keyPressEvent(QKeyEvent *event) override;
134 void contextMenuEvent(QContextMenuEvent *event) override;
136 private:
137 void showCompletionPopup();
138 void validateText();
140 static QString warningText(FileSystemPathValidator::TestResult result);
142 QFileSystemModel *m_completerModel = nullptr;
143 QCompleter *m_completer = nullptr;
144 QAction *m_browseAction = nullptr;
145 QAction *m_warningAction = nullptr;
146 QFileIconProvider m_iconProvider;
149 class FileComboEdit final : public QComboBox, public IFileEditorWithCompletion
151 Q_OBJECT
152 Q_DISABLE_COPY_MOVE(FileComboEdit)
154 public:
155 FileComboEdit(QWidget *parent = nullptr);
157 void completeDirectoriesOnly(bool completeDirsOnly) override;
158 void setFilenameFilters(const QStringList &filters) override;
159 void setBrowseAction(QAction *action) override;
160 void setValidator(QValidator *validator) override;
161 Path placeholder() const override;
162 void setPlaceholder(const Path &val) override;
163 QWidget *widget() override;
165 protected:
166 QString text() const;