8#include "kmimetypechooser.h"
10#include "kmimetypeeditor.h"
11#include <qmimedatabase.h>
13#include <QDialogButtonBox>
17#include <QSortFilterProxyModel>
18#include <QStandardItemModel>
19#include <QStandardPaths>
24class KMimeTypeChooserPrivate
27 KMimeTypeChooserPrivate(KMimeTypeChooser *parent)
32 void loadMimeTypes(
const QStringList &selected = QStringList());
33 QList<const QStandardItem *> getCheckedItems();
36 void slotCurrentChanged(
const QModelIndex &index);
37 void slotSycocaDatabaseChanged(
const QStringList &);
39 KMimeTypeChooser *
const q;
40 QTreeView *mimeTypeTree =
nullptr;
41 QStandardItemModel *m_model =
nullptr;
42 QSortFilterProxyModel *m_proxyModel =
nullptr;
43 QLineEdit *m_filterLineEdit =
nullptr;
44 QPushButton *btnEditMimeType =
nullptr;
52static const char s_keditfiletypeExecutable[] =
"keditfiletype";
62 , d(new KMimeTypeChooserPrivate(this))
64 d->defaultgroup = defaultGroup;
65 d->groups = groupsToShow;
82 d->m_proxyModel->setRecursiveFilteringEnabled(
true);
83 d->m_proxyModel->setFilterKeyColumn(-1);
84 d->m_proxyModel->setSourceModel(d->m_model);
85 d->mimeTypeTree->setModel(d->m_proxyModel);
87 d->m_filterLineEdit =
new QLineEdit(
this);
88 d->m_filterLineEdit->setPlaceholderText(
tr(
"Search for file type or filename pattern…",
"@info:placeholder"));
89 QLabel *filterLabel =
new QLabel(
tr(
"&Filter:",
"@label:textbox"));
90 filterLabel->
setBuddy(d->m_filterLineEdit);
92 d->m_proxyModel->setFilterRegularExpression(
93 QRegularExpression(text, QRegularExpression::CaseInsensitiveOption | QRegularExpression::UseUnicodePropertiesOption));
98 filterLayout->
addWidget(d->m_filterLineEdit);
100 d->m_filterLineEdit->setFocus();
103 QStringList headerLabels({
tr(
"MIME Type",
"@title:column")});
106 headerLabels.append(
tr(
"Comment",
"@title:column"));
110 headerLabels.append(
tr(
"Patterns",
"@title:column"));
113 d->m_model->setColumnCount(headerLabels.count());
114 d->m_model->setHorizontalHeaderLabels(headerLabels);
118 d->mimeTypeTree->setColumnWidth(0, optWidth);
119 d->mimeTypeTree->setColumnWidth(1, optWidth);
121 d->loadMimeTypes(selMimeTypes);
126 d->btnEditMimeType =
new QPushButton(
tr(
"&Edit…",
"@action:button"),
this);
127 buttonLayout->
addWidget(d->btnEditMimeType);
128 d->btnEditMimeType->setEnabled(
false);
138 d->slotCurrentChanged(index);
141 d->btnEditMimeType->setToolTip(
tr(
"Launch the MIME type editor",
"@info:tooltip"));
147KMimeTypeChooser::~KMimeTypeChooser() =
default;
149void KMimeTypeChooserPrivate::loadMimeTypes(
const QStringList &_selectedMimeTypes)
153 if (!_selectedMimeTypes.
isEmpty()) {
154 selMimeTypes = _selectedMimeTypes;
156 selMimeTypes = q->mimeTypes();
159 std::vector<QStandardItem *> parentGroups;
163 bool agroupisopen =
false;
164 QStandardItem *idefault =
nullptr;
165 QStandardItem *firstChecked =
nullptr;
167 for (
const QMimeType &mt : mimetypes) {
169 const int index =
mimetype.indexOf(QLatin1Char(
'/'));
171 const QString maj =
mimetype.left(index);
173 if (!groups.isEmpty() && !groups.contains(maj)) {
177 QStandardItem *groupItem;
179 auto it = std::find_if(parentGroups.cbegin(), parentGroups.cend(), [maj](
const QStandardItem *item) {
180 return maj == item->text();
183 if (it == parentGroups.cend()) {
184 groupItem =
new QStandardItem(maj);
187 QStandardItem *secondColumn =
new QStandardItem();
189 QStandardItem *thirdColumn =
new QStandardItem();
191 m_model->appendRow({groupItem, secondColumn, thirdColumn});
192 parentGroups.push_back(groupItem);
193 if (maj == defaultgroup) {
194 idefault = groupItem;
201 const QString min =
mimetype.mid(index + 1);
202 QStandardItem *mime =
new QStandardItem(
QIcon::fromTheme(mt.iconName()), min);
207 comments =
new QStandardItem(mt.comment());
211 QStandardItem *patterns =
nullptr;
214 patterns =
new QStandardItem(mt.globPatterns().join(QLatin1String(
"; ")));
220 if (selMimeTypes.
contains(mimetype)) {
222 const QModelIndex index = m_proxyModel->mapFromSource(m_model->indexFromItem(groupItem));
223 mimeTypeTree->expand(index);
236 const QModelIndex index = m_proxyModel->mapFromSource(m_model->indexFromItem(firstChecked));
237 mimeTypeTree->scrollTo(index);
240 if (!agroupisopen && idefault) {
241 const QModelIndex index = m_proxyModel->mapFromSource(m_model->indexFromItem(idefault));
242 mimeTypeTree->expand(index);
243 mimeTypeTree->scrollTo(index);
247void KMimeTypeChooserPrivate::editMimeType()
249 QModelIndex mimeIndex = m_proxyModel->mapToSource(mimeTypeTree->currentIndex());
252 if (m_model->hasChildren(mimeIndex)) {
256 if (mimeIndex.
column() > 0) {
257 mimeIndex = m_model->
sibling(mimeIndex.
row(), 0, mimeIndex);
260 const QStandardItem *item = m_model->itemFromIndex(mimeIndex);
261 const QString mt = (item->
parent())->text() + QLatin1Char(
'/') + item->
text();
271void KMimeTypeChooserPrivate::slotCurrentChanged(
const QModelIndex &index)
273 if (btnEditMimeType) {
274 const QModelIndex srcIndex = m_proxyModel->mapToSource(index);
275 const QStandardItem *currentItem = m_model->itemFromIndex(srcIndex);
276 btnEditMimeType->setEnabled(currentItem && currentItem->
parent());
281void KMimeTypeChooserPrivate::slotSycocaDatabaseChanged(
const QStringList &changedResources)
283 if (changedResources.
contains(QLatin1String(
"xdgdata-mime"))) {
290 QList<const QStandardItem *> lst;
291 const int rowCount = m_model->rowCount();
292 for (
int i = 0; i < rowCount; ++i) {
293 const QStandardItem *groupItem = m_model->item(i);
294 const int childCount = groupItem->
rowCount();
295 for (
int j = 0; j < childCount; ++j) {
296 const QStandardItem *child = groupItem->
child(j);
332class KMimeTypeChooserDialogPrivate
342 KMimeTypeChooserDialog *q;
343 KMimeTypeChooser *m_chooser;
357 , d(new KMimeTypeChooserDialogPrivate(this))
361 d->m_chooser =
new KMimeTypeChooser(text, selMimeTypes, defaultGroup, groupsToShow, visuals,
this);
371 , d(new KMimeTypeChooserDialogPrivate(this))
389void KMimeTypeChooserDialogPrivate::init()
402KMimeTypeChooserDialog::~KMimeTypeChooserDialog() =
default;
404QSize KMimeTypeChooserDialog::sizeHint()
const
407 const int viewableSize = fm.averageCharWidth() * 60;
408 return QSize(viewableSize, viewableSize);
413#include "moc_kmimetypechooser.cpp"
A dialog to select MIME types from the list of available ones on the system.
KMimeTypeChooser * chooser()
KMimeTypeChooserDialog(const QString &title=QString(), const QString &text=QString(), const QStringList &selectedMimeTypes=QStringList(), const QString &defaultGroup=QString(), const QStringList &groupsToShow=QStringList(), int visuals=KMimeTypeChooser::Comments|KMimeTypeChooser::Patterns|KMimeTypeChooser::EditButton, QWidget *parent=nullptr)
Create a KMimeTypeChooser dialog.
This widget provides a checkable list of all available MIME types, presented as a treeview,...
KMimeTypeChooser(const QString &text=QString(), const QStringList &selectedMimeTypes=QStringList(), const QString &defaultGroup=QString(), const QStringList &groupsToShow=QStringList(), int visuals=Comments|Patterns|EditButton, QWidget *parent=nullptr)
Create a new KMimeTypeChooser.
@ Patterns
Show the MIME types glob patterns (e.g. "*.html;*.htm") in a column.
@ EditButton
Show the "Edit" button, allowing to edit the selected type.
@ Comments
Show the MIME type comment (e.g. "HTML Document") in a column.
QStringList mimeTypes() const
QStringList patterns() const
KIOCORE_EXPORT MimetypeJob * mimetype(const QUrl &url, JobFlags flags=DefaultFlags)
void editMimeType(const QString &mimeType, QWidget *widget)
Starts the file types editor for a given MIME type.
KEDUVOCDOCUMENT_EXPORT QStringList comments(const QString &language=QString())
void activated(const QModelIndex &index)
void doubleClicked(const QModelIndex &index)
void addLayout(QLayout *layout, int stretch)
void addStretch(int stretch)
QDialog(QWidget *parent, Qt::WindowFlags f)
int averageCharWidth() const const
QIcon fromTheme(const QString &name)
void setBuddy(QWidget *buddy)
void setContentsMargins(const QMargins &margins)
void textChanged(const QString &text)
void append(QList< T > &&value)
bool isEmpty() const const
void reserve(qsizetype size)
qsizetype size() const const
QList< QMimeType > allMimeTypes() const const
QMimeType mimeTypeForName(const QString &nameOrAlias) const const
bool isValid() const const
QModelIndex sibling(int row, int column) const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
QString tr(const char *sourceText, const char *disambiguation, int n)
void appendRow(QStandardItem *item)
Qt::CheckState checkState() const const
QStandardItem * child(int row, int column) const const
QStandardItem * parent() const const
int rowCount() const const
void setCheckState(Qt::CheckState state)
void setFlags(Qt::ItemFlags flags)
QString text() const const
QString findExecutable(const QString &executableName, const QStringList &paths)
QString fromLatin1(QByteArrayView str)
bool isEmpty() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const