cmake-gui: Conditionally switch between QDirModel and QFileSystemModel

Uses QT_VERSION_CHECK to determine Qt version. The code switches to
QFileSystemModel for Qt versions >= 6
This commit is contained in:
Sankhesh Jhaveri
2020-12-23 10:28:15 -05:00
committed by Brad King
parent 020b2766f3
commit b3ee09290b

View File

@@ -1,16 +1,23 @@
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#define QT_DEPRECATED_WARNINGS_SINCE QT_VERSION_CHECK(5, 14, 0)
#include "QCMakeWidgets.h"
#include <utility>
#include <QFileDialog>
#include <QFileInfo>
#include <QFileSystemModel>
#include <QResizeEvent>
#include <QToolButton>
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
# include <QFileSystemModel>
#else
# include <QDirModel>
#endif
QCMakeFileEditor::QCMakeFileEditor(QWidget* p, QString var)
: QLineEdit(p)
, Variable(std::move(var))
@@ -89,8 +96,10 @@ void QCMakePathEditor::chooseFile()
}
}
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
// use same QFileSystemModel for all completers
static QFileSystemModel* fileDirModel()
{
static QFileSystemModel* m = nullptr;
if (!m) {
@@ -107,13 +116,39 @@ static QFileSystemModel* pathDirModel()
}
return m;
}
#else
// use same QDirModel for all completers
static QDirModel* fileDirModel()
{
static QDirModel* m = nullptr;
if (!m) {
m = new QDirModel();
}
return m;
}
static QDirModel* pathDirModel()
{
static QDirModel* m = nullptr;
if (!m) {
m = new QDirModel();
m->setFilter(QDir::AllDirs | QDir::Drives | QDir::NoDotAndDotDot);
}
return m;
}
#endif
QCMakeFileCompleter::QCMakeFileCompleter(QObject* o, bool dirs)
: QCompleter(o)
{
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
QFileSystemModel* m = dirs ? pathDirModel() : fileDirModel();
this->setModel(m);
m->setRootPath(QString());
#else
QDirModel* m = dirs ? pathDirModel() : fileDirModel();
this->setModel(m);
#endif
}
QString QCMakeFileCompleter::pathFromIndex(const QModelIndex& idx) const