Initialize completer for file system path widget on demand

PR #20586.
This commit is contained in:
Vladimir Golovnev 2024-03-23 08:18:36 +03:00 committed by Vladimir Golovnev (Glassez)
parent daaaa11f93
commit 753cdfdb1a
No known key found for this signature in database
GPG key ID: 52A2C7DEE2DFA6F7
2 changed files with 40 additions and 20 deletions

View file

@ -1,7 +1,8 @@
/* /*
* Bittorrent Client using Qt and libtorrent. * Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2022 Mike Tzou (Chocobo1) * Copyright (C) 2024 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2016 Eugene Shalygin * Copyright (C) 2022 Mike Tzou (Chocobo1)
* Copyright (C) 2016 Eugene Shalygin
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -32,6 +33,7 @@
#include <QCompleter> #include <QCompleter>
#include <QContextMenuEvent> #include <QContextMenuEvent>
#include <QDir> #include <QDir>
#include <QFileIconProvider>
#include <QFileInfo> #include <QFileInfo>
#include <QFileSystemModel> #include <QFileSystemModel>
#include <QMenu> #include <QMenu>
@ -160,36 +162,33 @@ QValidator::State Private::FileSystemPathValidator::validate(QString &input, int
} }
Private::FileLineEdit::FileLineEdit(QWidget *parent) Private::FileLineEdit::FileLineEdit(QWidget *parent)
: QLineEdit {parent} : QLineEdit(parent)
, m_completerModel {new QFileSystemModel(this)}
, m_completer {new QCompleter(this)}
{ {
m_iconProvider.setOptions(QFileIconProvider::DontUseCustomDirectoryIcons);
m_completerModel->setIconProvider(&m_iconProvider);
m_completerModel->setOptions(QFileSystemModel::DontWatchForChanges);
m_completer->setModel(m_completerModel);
setCompleter(m_completer);
connect(this, &QLineEdit::textChanged, this, &FileLineEdit::validateText); connect(this, &QLineEdit::textChanged, this, &FileLineEdit::validateText);
} }
Private::FileLineEdit::~FileLineEdit() Private::FileLineEdit::~FileLineEdit()
{ {
delete m_completerModel; // has to be deleted before deleting the m_iconProvider object delete m_completerModel; // has to be deleted before deleting the m_iconProvider object
delete m_iconProvider;
} }
void Private::FileLineEdit::completeDirectoriesOnly(const bool completeDirsOnly) void Private::FileLineEdit::completeDirectoriesOnly(const bool completeDirsOnly)
{ {
const QDir::Filters filters = QDir::NoDotAndDotDot m_completeDirectoriesOnly = completeDirsOnly;
| (completeDirsOnly ? QDir::Dirs : QDir::AllEntries); if (m_completerModel)
m_completerModel->setFilter(filters); {
const QDir::Filters filters = QDir::NoDotAndDotDot
| (completeDirsOnly ? QDir::Dirs : QDir::AllEntries);
m_completerModel->setFilter(filters);
}
} }
void Private::FileLineEdit::setFilenameFilters(const QStringList &filters) void Private::FileLineEdit::setFilenameFilters(const QStringList &filters)
{ {
m_completerModel->setNameFilters(filters); m_filenameFilters = filters;
if (m_completerModel)
m_completerModel->setNameFilters(m_filenameFilters);
} }
void Private::FileLineEdit::setBrowseAction(QAction *action) void Private::FileLineEdit::setBrowseAction(QAction *action)
@ -223,6 +222,24 @@ void Private::FileLineEdit::keyPressEvent(QKeyEvent *e)
if ((e->key() == Qt::Key_Space) && (e->modifiers() == Qt::CTRL)) if ((e->key() == Qt::Key_Space) && (e->modifiers() == Qt::CTRL))
{ {
if (!m_completer)
{
m_iconProvider = new QFileIconProvider;
m_iconProvider->setOptions(QFileIconProvider::DontUseCustomDirectoryIcons);
m_completerModel = new QFileSystemModel(this);
m_completerModel->setIconProvider(m_iconProvider);
m_completerModel->setOptions(QFileSystemModel::DontWatchForChanges);
m_completerModel->setNameFilters(m_filenameFilters);
const QDir::Filters filters = QDir::NoDotAndDotDot
| (m_completeDirectoriesOnly ? QDir::Dirs : QDir::AllEntries);
m_completerModel->setFilter(filters);
m_completer = new QCompleter(this);
m_completer->setModel(m_completerModel);
setCompleter(m_completer);
}
m_completerModel->setRootPath(Path(text()).data()); m_completerModel->setRootPath(Path(text()).data());
showCompletionPopup(); showCompletionPopup();
} }

View file

@ -1,6 +1,7 @@
/* /*
* Bittorrent Client using Qt and libtorrent. * Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2016 Eugene Shalygin * Copyright (C) 2024 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2016 Eugene Shalygin
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -29,7 +30,6 @@
#pragma once #pragma once
#include <QComboBox> #include <QComboBox>
#include <QFileIconProvider>
#include <QLineEdit> #include <QLineEdit>
#include <QtContainerFwd> #include <QtContainerFwd>
#include <QValidator> #include <QValidator>
@ -39,6 +39,7 @@
class QAction; class QAction;
class QCompleter; class QCompleter;
class QContextMenuEvent; class QContextMenuEvent;
class QFileIconProvider;
class QFileSystemModel; class QFileSystemModel;
class QKeyEvent; class QKeyEvent;
@ -143,7 +144,9 @@ namespace Private
QCompleter *m_completer = nullptr; QCompleter *m_completer = nullptr;
QAction *m_browseAction = nullptr; QAction *m_browseAction = nullptr;
QAction *m_warningAction = nullptr; QAction *m_warningAction = nullptr;
QFileIconProvider m_iconProvider; QFileIconProvider *m_iconProvider = nullptr;
bool m_completeDirectoriesOnly = false;
QStringList m_filenameFilters;
}; };
class FileComboEdit final : public QComboBox, public IFileEditorWithCompletion class FileComboEdit final : public QComboBox, public IFileEditorWithCompletion