Combine all the column filter related widgets
[qBittorrent.git] / src / gui / fspathedit_p.h
blobdeb1fde2a3be6aa1333727dd04e6533fcb5ea8a1
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 directoriesOnly() const;
72 void setDirectoriesOnly(bool value);
74 bool checkReadPermission() const;
75 void setCheckReadPermission(bool value);
77 bool checkWritePermission() const;
78 void setCheckWritePermission(bool value);
80 TestResult lastTestResult() const;
81 QValidator::State lastValidationState() const;
83 QValidator::State validate(QString &input, int &pos) const override;
85 private:
86 TestResult testPath(const Path &path) const;
88 bool m_strictMode = false;
89 bool m_existingOnly = false;
90 bool m_directoriesOnly = false;
91 bool m_checkReadPermission = false;
92 bool m_checkWritePermission = false;
94 mutable TestResult m_lastTestResult = TestResult::DoesNotExist;
95 mutable QValidator::State m_lastValidationState = QValidator::Invalid;
98 class IFileEditorWithCompletion
100 public:
101 virtual ~IFileEditorWithCompletion() = default;
102 virtual void completeDirectoriesOnly(bool completeDirsOnly) = 0;
103 virtual void setFilenameFilters(const QStringList &filters) = 0;
104 virtual void setBrowseAction(QAction *action) = 0;
105 virtual void setValidator(QValidator *validator) = 0;
106 virtual Path placeholder() const = 0;
107 virtual void setPlaceholder(const Path &val) = 0;
108 virtual QWidget *widget() = 0;
111 class FileLineEdit final : public QLineEdit, public IFileEditorWithCompletion
113 Q_OBJECT
114 Q_DISABLE_COPY_MOVE(FileLineEdit)
116 public:
117 FileLineEdit(QWidget *parent = nullptr);
118 ~FileLineEdit();
120 void completeDirectoriesOnly(bool completeDirsOnly) override;
121 void setFilenameFilters(const QStringList &filters) override;
122 void setBrowseAction(QAction *action) override;
123 void setValidator(QValidator *validator) override;
124 Path placeholder() const override;
125 void setPlaceholder(const Path &val) override;
126 QWidget *widget() override;
128 protected:
129 void keyPressEvent(QKeyEvent *event) override;
130 void contextMenuEvent(QContextMenuEvent *event) override;
132 private:
133 void showCompletionPopup();
134 void validateText();
136 static QString warningText(FileSystemPathValidator::TestResult result);
138 QFileSystemModel *m_completerModel = nullptr;
139 QCompleter *m_completer = nullptr;
140 QAction *m_browseAction = nullptr;
141 QAction *m_warningAction = nullptr;
142 QFileIconProvider m_iconProvider;
145 class FileComboEdit final : public QComboBox, public IFileEditorWithCompletion
147 Q_OBJECT
148 Q_DISABLE_COPY_MOVE(FileComboEdit)
150 public:
151 FileComboEdit(QWidget *parent = nullptr);
153 void completeDirectoriesOnly(bool completeDirsOnly) override;
154 void setFilenameFilters(const QStringList &filters) override;
155 void setBrowseAction(QAction *action) override;
156 void setValidator(QValidator *validator) override;
157 Path placeholder() const override;
158 void setPlaceholder(const Path &val) override;
159 QWidget *widget() override;
161 protected:
162 QString text() const;