qBittorrent
fspathedit_p.h
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2016 Eugene Shalygin
4  *
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.
9  *
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.
14  *
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.
18  *
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.
27  */
28 
29 #pragma once
30 
31 #include <QComboBox>
32 #include <QFileIconProvider>
33 #include <QLineEdit>
34 #include <QtContainerFwd>
35 #include <QValidator>
36 
37 class QAction;
38 class QCompleter;
39 class QContextMenuEvent;
40 class QFileSystemModel;
41 class QKeyEvent;
42 
43 namespace Private
44 {
45  class FileSystemPathValidator final : public QValidator
46  {
47  Q_OBJECT
48 
49  public:
50  FileSystemPathValidator(QObject *parent = nullptr);
51 
52  bool strictMode() const;
53  void setStrictMode(bool v);
54 
55  bool existingOnly() const;
56  void setExistingOnly(bool v);
57 
58  bool directoriesOnly() const;
59  void setDirectoriesOnly(bool v);
60 
61  bool checkReadPermission() const;
62  void setCheckReadPermission(bool v);
63 
64  bool checkWritePermission() const;
65  void setCheckWritePermission(bool v);
66 
67  QValidator::State validate(QString &input, int &pos) const override;
68 
69  enum class TestResult
70  {
71  OK,
73  NotADir,
74  NotAFile,
75  CantRead,
76  CantWrite
77  };
78 
79  TestResult lastTestResult() const;
80  QValidator::State lastValidationState() const;
81  QString lastTestedPath() const;
82 
83  private:
84  QValidator::State validate(const QList<QStringView> &pathComponents, bool strict,
85  int firstComponentToTest, int lastComponentToTest) const;
86 
87  TestResult testPath(QStringView path, bool pathIsComplete) const;
88 
94 
96  mutable QValidator::State m_lastValidationState;
97  mutable QString m_lastTestedPath;
98  };
99 
101  {
102  public:
103  virtual ~FileEditorWithCompletion() = default;
104  virtual void completeDirectoriesOnly(bool completeDirsOnly) = 0;
105  virtual void setFilenameFilters(const QStringList &filters) = 0;
106  virtual void setBrowseAction(QAction *action) = 0;
107  virtual void setValidator(QValidator *validator) = 0;
108  virtual QString placeholder() const = 0;
109  virtual void setPlaceholder(const QString &val) = 0;
110  virtual QWidget *widget() = 0;
111  };
112 
113  class FileLineEdit final : public QLineEdit, public FileEditorWithCompletion
114  {
115  Q_OBJECT
116  Q_DISABLE_COPY_MOVE(FileLineEdit)
117 
118  public:
119  FileLineEdit(QWidget *parent = nullptr);
120  ~FileLineEdit();
121 
122  void completeDirectoriesOnly(bool completeDirsOnly) override;
123  void setFilenameFilters(const QStringList &filters) override;
124  void setBrowseAction(QAction *action) override;
125  void setValidator(QValidator *validator) override;
126  QString placeholder() const override;
127  void setPlaceholder(const QString &val) override;
128  QWidget *widget() override;
129 
130  protected:
131  void keyPressEvent(QKeyEvent *event) override;
132  void contextMenuEvent(QContextMenuEvent *event) override;
133 
134  private:
136  void showCompletionPopup();
137 
138  QFileSystemModel *m_completerModel;
139  QCompleter *m_completer;
140  QAction *m_browseAction;
141  QFileIconProvider m_iconProvider;
142  QAction *m_warningAction;
143  };
144 
145  class FileComboEdit final : public QComboBox, public FileEditorWithCompletion
146  {
147  Q_OBJECT
148 
149  public:
150  FileComboEdit(QWidget *parent = nullptr);
151 
152  void completeDirectoriesOnly(bool completeDirsOnly) override;
153  void setFilenameFilters(const QStringList &filters) override;
154  void setBrowseAction(QAction *action) override;
155  void setValidator(QValidator *validator) override;
156  QString placeholder() const override;
157  void setPlaceholder(const QString &val) override;
158  QWidget *widget() override;
159 
160  protected:
161  QString text() const;
162  };
163 }
void completeDirectoriesOnly(bool completeDirsOnly) override
QString text() const
void setValidator(QValidator *validator) override
void setPlaceholder(const QString &val) override
FileComboEdit(QWidget *parent=nullptr)
void setBrowseAction(QAction *action) override
QWidget * widget() override
void setFilenameFilters(const QStringList &filters) override
QString placeholder() const override
virtual void setValidator(QValidator *validator)=0
virtual void setFilenameFilters(const QStringList &filters)=0
virtual QWidget * widget()=0
virtual ~FileEditorWithCompletion()=default
virtual void setBrowseAction(QAction *action)=0
virtual void setPlaceholder(const QString &val)=0
virtual void completeDirectoriesOnly(bool completeDirsOnly)=0
virtual QString placeholder() const =0
void keyPressEvent(QKeyEvent *event) override
static QString warningText(FileSystemPathValidator::TestResult r)
QFileSystemModel * m_completerModel
Definition: fspathedit_p.h:138
QCompleter * m_completer
Definition: fspathedit_p.h:139
FileLineEdit(QWidget *parent=nullptr)
void setBrowseAction(QAction *action) override
QFileIconProvider m_iconProvider
Definition: fspathedit_p.h:141
void setValidator(QValidator *validator) override
QString placeholder() const override
void contextMenuEvent(QContextMenuEvent *event) override
void completeDirectoriesOnly(bool completeDirsOnly) override
void setFilenameFilters(const QStringList &filters) override
QWidget * widget() override
void setPlaceholder(const QString &val) override
QValidator::State validate(QString &input, int &pos) const override
TestResult testPath(QStringView path, bool pathIsComplete) const
QValidator::State m_lastValidationState
Definition: fspathedit_p.h:96
FileSystemPathValidator(QObject *parent=nullptr)
QValidator::State lastValidationState() const
action
Definition: tstool.py:143