MIME preferences: Add globs and current handler name to filter

It is most useful if a user can type a file extension in the filter to find
the corresponding MIME type(s).

Also fix resetting the filter model when reopening the preferences.

Change-Id: I583e021dcdd2791288f913bc64d2be07e06ebfd3
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2023-06-08 14:14:23 +02:00
parent c72c2cbf19
commit 7f2ad8079d

View File

@@ -83,9 +83,7 @@ public:
class MimeTypeSettingsModel : public QAbstractTableModel
{
public:
enum class Role {
DefaultHandler = Qt::UserRole
};
enum class Role { DefaultHandler = Qt::UserRole, MimeType };
MimeTypeSettingsModel(QObject *parent = nullptr)
: QAbstractTableModel(parent) {}
@@ -158,6 +156,8 @@ QVariant MimeTypeSettingsModel::data(const QModelIndex &modelIndex, int role) co
}
}
return QVariant();
} else if (role == int(Role::MimeType)) {
return QVariant::fromValue(m_mimeTypes.at(modelIndex.row()));
}
return QVariant();
}
@@ -222,6 +222,37 @@ void MimeTypeSettingsModel::resetUserDefaults()
endResetModel();
}
class MimeFilterModel : public QSortFilterProxyModel
{
public:
explicit MimeFilterModel(QObject *parent = nullptr);
protected:
bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override;
};
MimeFilterModel::MimeFilterModel(QObject *parent)
: QSortFilterProxyModel(parent)
{
}
bool MimeFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
const QModelIndex &index = sourceModel()->index(source_row, 0, source_parent);
const MimeType mt
= sourceModel()->data(index, int(MimeTypeSettingsModel::Role::MimeType)).value<MimeType>();
const QModelIndex &handlerIndex = sourceModel()->index(source_row, 1, source_parent);
const QString handlerText = sourceModel()->data(handlerIndex, Qt::DisplayRole).toString();
const QStringList matchStrings = mt.globPatterns() << mt.name() << handlerText;
const QRegularExpression regex = filterRegularExpression();
for (const QString &str : matchStrings) {
if (regex.match(str).hasMatch())
return true;
}
return false;
}
// MimeTypeSettingsPrivate
class MimeTypeSettingsPrivate : public QObject
{
@@ -256,7 +287,7 @@ public:
static UserMimeTypeHash m_userModifiedMimeTypes; // these are already in mime database
MimeTypeSettingsModel *m_model;
QSortFilterProxyModel *m_filterModel;
MimeFilterModel *m_filterModel;
UserMimeTypeHash m_pendingModifiedMimeTypes; // currently edited in the options page
QString m_filterPattern;
QPointer<QWidget> m_widget;
@@ -277,10 +308,11 @@ MimeTypeSettingsPrivate::UserMimeTypeHash MimeTypeSettingsPrivate::m_userModifie
MimeTypeSettingsPrivate::MimeTypeSettingsPrivate()
: m_model(new MimeTypeSettingsModel(this))
, m_filterModel(new QSortFilterProxyModel(this))
, m_filterModel(new MimeFilterModel(this))
{
m_filterModel->setSourceModel(m_model);
m_filterModel->setFilterKeyColumn(-1);
m_filterModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
connect(ICore::instance(), &ICore::saveSettingsRequested,
this, &MimeTypeSettingsPrivate::writeUserModifiedMimeTypes);
}
@@ -317,6 +349,7 @@ void MimeTypeSettingsPrivate::configureUi(QWidget *w)
auto filterLineEdit = new FancyLineEdit;
filterLineEdit->setObjectName("filterLineEdit");
filterLineEdit->setFiltering(true);
m_filterModel->setFilterWildcard({});
m_mimeTypesTreeView = new QTreeView;
m_mimeTypesTreeView->setObjectName("mimeTypesTreeView");