Merge pull request #18104 from sledgehammer999/remove_dead_code
[qBittorrent.git] / src / gui / fspathedit.cpp
blob94ac6741d5630a2a0cfef8e631478d4746e2a6cc
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 #include "fspathedit.h"
31 #include <memory>
32 #include <stdexcept>
34 #include <QAction>
35 #include <QApplication>
36 #include <QCoreApplication>
37 #include <QFileDialog>
38 #include <QHBoxLayout>
39 #include <QStyle>
40 #include <QToolButton>
42 #include "base/utils/fs.h"
43 #include "fspathedit_p.h"
45 namespace
47 struct TrStringWithComment
49 const char *source = nullptr;
50 const char *comment = nullptr;
52 QString tr() const
54 return QCoreApplication::translate("FileSystemPathEdit", source, comment);
58 constexpr TrStringWithComment browseButtonBriefText =
59 QT_TRANSLATE_NOOP3("FileSystemPathEdit", "...", "Launch file dialog button text (brief)");
60 constexpr TrStringWithComment browseButtonFullText =
61 QT_TRANSLATE_NOOP3("FileSystemPathEdit", "&Browse...", "Launch file dialog button text (full)");
62 constexpr TrStringWithComment defaultDialogCaptionForFile =
63 QT_TRANSLATE_NOOP3("FileSystemPathEdit", "Choose a file", "Caption for file open/save dialog");
64 constexpr TrStringWithComment defaultDialogCaptionForDirectory =
65 QT_TRANSLATE_NOOP3("FileSystemPathEdit", "Choose a folder", "Caption for directory open dialog");
68 class FileSystemPathEdit::FileSystemPathEditPrivate
70 Q_DECLARE_PUBLIC(FileSystemPathEdit)
71 Q_DISABLE_COPY_MOVE(FileSystemPathEditPrivate)
73 private:
74 FileSystemPathEditPrivate(FileSystemPathEdit *q, Private::IFileEditorWithCompletion *editor);
76 void modeChanged();
77 void browseActionTriggered();
78 QString dialogCaptionOrDefault() const;
80 FileSystemPathEdit *q_ptr = nullptr;
81 std::unique_ptr<Private::IFileEditorWithCompletion> m_editor;
82 QAction *m_browseAction = nullptr;
83 QToolButton *m_browseBtn = nullptr;
84 QString m_fileNameFilter;
85 Mode m_mode = FileSystemPathEdit::Mode::FileOpen;
86 Path m_lastSignaledPath;
87 QString m_dialogCaption;
88 Private::FileSystemPathValidator *m_validator = nullptr;
91 FileSystemPathEdit::FileSystemPathEditPrivate::FileSystemPathEditPrivate(
92 FileSystemPathEdit *q, Private::IFileEditorWithCompletion *editor)
93 : q_ptr {q}
94 , m_editor {editor}
95 , m_browseAction {new QAction(QApplication::style()->standardIcon(QStyle::SP_DirOpenIcon), browseButtonFullText.tr(), q)}
96 , m_browseBtn {new QToolButton(q)}
97 , m_fileNameFilter {tr("Any file") + u" (*)"}
98 , m_validator {new Private::FileSystemPathValidator(q)}
100 m_browseAction->setIconText(browseButtonBriefText.tr());
101 m_browseAction->setShortcut(Qt::CTRL + Qt::Key_B);
102 m_browseAction->setToolTip(browseButtonFullText.tr().remove(u'&'));
104 m_browseBtn->setDefaultAction(m_browseAction);
106 m_validator->setStrictMode(false);
108 m_editor->setBrowseAction(m_browseAction);
109 m_editor->setValidator(m_validator);
111 modeChanged();
114 void FileSystemPathEdit::FileSystemPathEditPrivate::browseActionTriggered()
116 Q_Q(FileSystemPathEdit);
118 const Path currentDirectory = (m_mode == FileSystemPathEdit::Mode::DirectoryOpen) || (m_mode == FileSystemPathEdit::Mode::DirectorySave)
119 ? q->selectedPath()
120 : q->selectedPath().parentPath();
121 const Path initialDirectory = currentDirectory.isAbsolute() ? currentDirectory : (Utils::Fs::homePath() / currentDirectory);
123 QString filter = q->fileNameFilter();
124 QString newPath;
125 switch (m_mode)
127 case FileSystemPathEdit::Mode::FileOpen:
128 newPath = QFileDialog::getOpenFileName(q, dialogCaptionOrDefault(), initialDirectory.data(), filter);
129 break;
130 case FileSystemPathEdit::Mode::FileSave:
131 newPath = QFileDialog::getSaveFileName(q, dialogCaptionOrDefault(), initialDirectory.data(), filter, &filter);
132 break;
133 case FileSystemPathEdit::Mode::DirectoryOpen:
134 case FileSystemPathEdit::Mode::DirectorySave:
135 newPath = QFileDialog::getExistingDirectory(q, dialogCaptionOrDefault(),
136 initialDirectory.data(), QFileDialog::ShowDirsOnly);
137 break;
138 default:
139 throw std::logic_error("Unknown FileSystemPathEdit mode");
142 if (!newPath.isEmpty())
143 q->setSelectedPath(Path(newPath));
146 QString FileSystemPathEdit::FileSystemPathEditPrivate::dialogCaptionOrDefault() const
148 if (!m_dialogCaption.isEmpty())
149 return m_dialogCaption;
151 switch (m_mode)
153 case FileSystemPathEdit::Mode::FileOpen:
154 case FileSystemPathEdit::Mode::FileSave:
155 return defaultDialogCaptionForFile.tr();
156 case FileSystemPathEdit::Mode::DirectoryOpen:
157 case FileSystemPathEdit::Mode::DirectorySave:
158 return defaultDialogCaptionForDirectory.tr();
159 default:
160 throw std::logic_error("Unknown FileSystemPathEdit mode");
164 void FileSystemPathEdit::FileSystemPathEditPrivate::modeChanged()
166 m_editor->completeDirectoriesOnly((m_mode == FileSystemPathEdit::Mode::DirectoryOpen) || (m_mode == FileSystemPathEdit::Mode::DirectorySave));
168 m_validator->setExistingOnly(m_mode != FileSystemPathEdit::Mode::FileSave);
169 m_validator->setDirectoriesOnly((m_mode == FileSystemPathEdit::Mode::DirectoryOpen) || (m_mode == FileSystemPathEdit::Mode::DirectorySave));
170 m_validator->setCheckReadPermission((m_mode == FileSystemPathEdit::Mode::FileOpen) || (m_mode == FileSystemPathEdit::Mode::DirectoryOpen));
171 m_validator->setCheckWritePermission((m_mode == FileSystemPathEdit::Mode::FileSave) || (m_mode == FileSystemPathEdit::Mode::DirectorySave));
174 FileSystemPathEdit::FileSystemPathEdit(Private::IFileEditorWithCompletion *editor, QWidget *parent)
175 : QWidget(parent)
176 , d_ptr(new FileSystemPathEditPrivate(this, editor))
178 Q_D(FileSystemPathEdit);
179 editor->widget()->setParent(this);
181 setFocusProxy(editor->widget());
182 // required, otherwise the button cannot be selected via keyboard tab
183 setTabOrder(editor->widget(), d_ptr->m_browseBtn);
185 auto *layout = new QHBoxLayout(this);
186 layout->setContentsMargins(0, 0, 0, 0);
187 layout->addWidget(editor->widget());
188 layout->addWidget(d->m_browseBtn);
190 connect(d->m_browseAction, &QAction::triggered, this, [this]() { this->d_func()->browseActionTriggered(); });
193 FileSystemPathEdit::~FileSystemPathEdit()
195 delete d_ptr;
198 Path FileSystemPathEdit::selectedPath() const
200 return Path(editWidgetText());
203 void FileSystemPathEdit::setSelectedPath(const Path &val)
205 Q_D(FileSystemPathEdit);
207 const QString nativePath = val.toString();
208 setEditWidgetText(nativePath);
209 d->m_editor->widget()->setToolTip(nativePath);
212 QString FileSystemPathEdit::fileNameFilter() const
214 Q_D(const FileSystemPathEdit);
215 return d->m_fileNameFilter;
218 void FileSystemPathEdit::setFileNameFilter(const QString &val)
220 Q_D(FileSystemPathEdit);
221 d->m_fileNameFilter = val;
223 #if 0
224 // QFileSystemModel applies name filters to directories too.
225 // To use the filters we have to subclass QFileSystemModel and skip directories while filtering
226 // extract file masks
227 const int openBracePos = val.indexOf(u'(');
228 const int closeBracePos = val.indexOf(u')', (openBracePos + 1));
229 if ((openBracePos > 0) && (closeBracePos > 0) && (closeBracePos > openBracePos + 2))
231 QString filterString = val.mid(openBracePos + 1, closeBracePos - openBracePos - 1);
232 if (filterString == u"*")
233 { // no filters
234 d->m_editor->setFilenameFilters({});
236 else
238 QStringList filters = filterString.split(u' ', Qt::SkipEmptyParts);
239 d->m_editor->setFilenameFilters(filters);
242 else
244 d->m_editor->setFilenameFilters({});
246 #endif
249 Path FileSystemPathEdit::placeholder() const
251 Q_D(const FileSystemPathEdit);
252 return d->m_editor->placeholder();
255 void FileSystemPathEdit::setPlaceholder(const Path &val)
257 Q_D(FileSystemPathEdit);
258 d->m_editor->setPlaceholder(val);
261 bool FileSystemPathEdit::briefBrowseButtonCaption() const
263 Q_D(const FileSystemPathEdit);
264 return d->m_browseBtn->text() == browseButtonBriefText.tr();
267 void FileSystemPathEdit::setBriefBrowseButtonCaption(bool brief)
269 Q_D(FileSystemPathEdit);
270 d->m_browseBtn->setText(brief ? browseButtonBriefText.tr() : browseButtonFullText.tr());
273 void FileSystemPathEdit::onPathEdited()
275 Q_D(FileSystemPathEdit);
277 const Path newPath = selectedPath();
278 if (newPath != d->m_lastSignaledPath)
280 emit selectedPathChanged(newPath);
281 d->m_lastSignaledPath = newPath;
282 d->m_editor->widget()->setToolTip(editWidgetText());
286 FileSystemPathEdit::Mode FileSystemPathEdit::mode() const
288 Q_D(const FileSystemPathEdit);
289 return d->m_mode;
292 void FileSystemPathEdit::setMode(const Mode mode)
294 Q_D(FileSystemPathEdit);
295 d->m_mode = mode;
296 d->modeChanged();
299 QString FileSystemPathEdit::dialogCaption() const
301 Q_D(const FileSystemPathEdit);
302 return d->m_dialogCaption;
305 void FileSystemPathEdit::setDialogCaption(const QString &caption)
307 Q_D(FileSystemPathEdit);
308 d->m_dialogCaption = caption;
311 QWidget *FileSystemPathEdit::editWidgetImpl() const
313 Q_D(const FileSystemPathEdit);
314 return d->m_editor->widget();
317 // ------------------------- FileSystemPathLineEdit ----------------------
318 FileSystemPathLineEdit::FileSystemPathLineEdit(QWidget *parent)
319 : FileSystemPathEdit(new WidgetType(), parent)
321 connect(editWidget<WidgetType>(), &QLineEdit::editingFinished, this, &FileSystemPathLineEdit::onPathEdited);
322 connect(editWidget<WidgetType>(), &QLineEdit::textChanged, this, &FileSystemPathLineEdit::onPathEdited);
325 QString FileSystemPathLineEdit::editWidgetText() const
327 return editWidget<WidgetType>()->text();
330 void FileSystemPathLineEdit::clear()
332 editWidget<WidgetType>()->clear();
335 void FileSystemPathLineEdit::setEditWidgetText(const QString &text)
337 editWidget<WidgetType>()->setText(text);
340 // ----------------------- FileSystemPathComboEdit -----------------------
341 FileSystemPathComboEdit::FileSystemPathComboEdit(QWidget *parent)
342 : FileSystemPathEdit(new WidgetType(), parent)
344 editWidget<WidgetType>()->setEditable(true);
345 connect(editWidget<WidgetType>(), &QComboBox::currentTextChanged, this, &FileSystemPathComboEdit::onPathEdited);
346 connect(editWidget<WidgetType>()->lineEdit(), &QLineEdit::editingFinished, this, &FileSystemPathComboEdit::onPathEdited);
349 void FileSystemPathComboEdit::clear()
351 editWidget<WidgetType>()->clear();
354 int FileSystemPathComboEdit::count() const
356 return editWidget<WidgetType>()->count();
359 Path FileSystemPathComboEdit::item(int index) const
361 return Path(editWidget<WidgetType>()->itemText(index));
364 void FileSystemPathComboEdit::addItem(const Path &path)
366 editWidget<WidgetType>()->addItem(path.toString());
369 void FileSystemPathComboEdit::insertItem(int index, const Path &path)
371 editWidget<WidgetType>()->insertItem(index, path.toString());
374 int FileSystemPathComboEdit::currentIndex() const
376 return editWidget<WidgetType>()->currentIndex();
379 void FileSystemPathComboEdit::setCurrentIndex(int index)
381 editWidget<WidgetType>()->setCurrentIndex(index);
384 int FileSystemPathComboEdit::maxVisibleItems() const
386 return editWidget<WidgetType>()->maxVisibleItems();
389 void FileSystemPathComboEdit::setMaxVisibleItems(int maxItems)
391 editWidget<WidgetType>()->setMaxVisibleItems(maxItems);
394 QString FileSystemPathComboEdit::editWidgetText() const
396 return editWidget<WidgetType>()->currentText();
399 void FileSystemPathComboEdit::setEditWidgetText(const QString &text)
401 editWidget<WidgetType>()->setCurrentText(text);