Add mime type browser example.
Add an example showing the types in QMimeDatabase in a QTreeView with detailed information on click. The example has menu options to detect a file type and search by name. Change-Id: If6f1a6661edd100a54460a49562a8a8667245ddb Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
parent
14b79ae2b3
commit
9a96eb00c4
@ -4,5 +4,6 @@ CONFIG += no_docs_target
|
||||
SUBDIRS = \
|
||||
ipc \
|
||||
json \
|
||||
mimetypes \
|
||||
threads \
|
||||
tools
|
||||
|
69
examples/corelib/mimetypes/mimetypebrowser/main.cpp
Normal file
69
examples/corelib/mimetypes/mimetypebrowser/main.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2016 The Qt Company Ltd.
|
||||
** Contact: http://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the examples of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:BSD$
|
||||
** You may use this file under the terms of the BSD license as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of The Qt Company Ltd nor the names of its
|
||||
** contributors may be used to endorse or promote products derived
|
||||
** from this software without specific prior written permission.
|
||||
**
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "mainwindow.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QDesktopWidget>
|
||||
|
||||
#include <QCommandLineParser>
|
||||
#include <QCommandLineOption>
|
||||
#include <QDebug>
|
||||
#include <QMimeDatabase>
|
||||
#include <QMimeType>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
QCoreApplication::setApplicationVersion(QT_VERSION_STR);
|
||||
|
||||
QCommandLineParser parser;
|
||||
parser.addHelpOption();
|
||||
parser.addVersionOption();
|
||||
|
||||
parser.process(app);
|
||||
|
||||
MainWindow mainWindow;
|
||||
const QRect availableGeometry = QApplication::desktop()->availableGeometry(&mainWindow);
|
||||
mainWindow.resize(availableGeometry.width() / 3, availableGeometry.height() / 2);
|
||||
mainWindow.show();
|
||||
|
||||
return app.exec();
|
||||
}
|
185
examples/corelib/mimetypes/mimetypebrowser/mainwindow.cpp
Normal file
185
examples/corelib/mimetypes/mimetypebrowser/mainwindow.cpp
Normal file
@ -0,0 +1,185 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2016 The Qt Company Ltd.
|
||||
** Contact: http://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the examples of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:BSD$
|
||||
** You may use this file under the terms of the BSD license as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of The Qt Company Ltd nor the names of its
|
||||
** contributors may be used to endorse or promote products derived
|
||||
** from this software without specific prior written permission.
|
||||
**
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "mainwindow.h"
|
||||
#include "mimetypemodel.h"
|
||||
|
||||
#include <QAction>
|
||||
#include <QApplication>
|
||||
#include <QFileDialog>
|
||||
#include <QInputDialog>
|
||||
#include <QMenu>
|
||||
#include <QMenuBar>
|
||||
#include <QMessageBox>
|
||||
#include <QPlainTextEdit>
|
||||
#include <QSplitter>
|
||||
#include <QStatusBar>
|
||||
#include <QTextEdit>
|
||||
#include <QTreeView>
|
||||
|
||||
#include <QFileInfo>
|
||||
#include <QItemSelectionModel>
|
||||
#include <QMimeDatabase>
|
||||
#include <QMimeType>
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent)
|
||||
, m_model(new MimetypeModel(this))
|
||||
, m_treeView(new QTreeView)
|
||||
, m_detailsText(new QTextEdit)
|
||||
, m_findIndex(0)
|
||||
{
|
||||
setWindowTitle(tr("Qt Mime Database Browser"));
|
||||
|
||||
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
|
||||
QAction *detectFileAction =
|
||||
fileMenu->addAction(tr("&Detect File Type..."), this, &MainWindow::detectFile);
|
||||
detectFileAction->setShortcuts(QKeySequence::Open);
|
||||
|
||||
QAction *exitAction = fileMenu->addAction(tr("E&xit"), qApp, &QApplication::closeAllWindows);
|
||||
exitAction->setShortcuts(QKeySequence::Quit);
|
||||
|
||||
QMenu *findMenu = menuBar()->addMenu(tr("&Edit"));
|
||||
QAction *findAction =
|
||||
findMenu->addAction(tr("&Find..."), this, &MainWindow::find);
|
||||
findAction->setShortcuts(QKeySequence::Find);
|
||||
m_findNextAction = findMenu->addAction(tr("Find &Next"), this, &MainWindow::findNext);
|
||||
m_findNextAction->setShortcuts(QKeySequence::FindNext);
|
||||
m_findPreviousAction = findMenu->addAction(tr("Find &Previous"), this, &MainWindow::findPrevious);
|
||||
m_findPreviousAction->setShortcuts(QKeySequence::FindPrevious);
|
||||
|
||||
menuBar()->addMenu(tr("&About"))->addAction(tr("&About Qt"), qApp, &QApplication::aboutQt);
|
||||
|
||||
QSplitter *centralSplitter = new QSplitter;
|
||||
setCentralWidget(centralSplitter);
|
||||
m_treeView = new QTreeView;
|
||||
m_treeView->setUniformRowHeights(true);
|
||||
m_treeView->setModel(m_model);
|
||||
|
||||
const auto items = m_model->findItems("application/octet-stream", Qt::MatchContains | Qt::MatchFixedString | Qt::MatchRecursive);
|
||||
if (!items.isEmpty())
|
||||
m_treeView->expand(m_model->indexFromItem(items.constFirst()));
|
||||
|
||||
connect(m_treeView->selectionModel(), &QItemSelectionModel::currentChanged,
|
||||
this, &MainWindow::currentChanged);
|
||||
centralSplitter->addWidget(m_treeView);
|
||||
m_detailsText->setReadOnly(true);
|
||||
centralSplitter->addWidget(m_detailsText);
|
||||
|
||||
updateFindActions();
|
||||
}
|
||||
|
||||
void MainWindow::currentChanged(const QModelIndex &index)
|
||||
{
|
||||
if (index.isValid())
|
||||
m_detailsText->setText(MimetypeModel::formatMimeTypeInfo(m_model->mimeType(index)));
|
||||
else
|
||||
m_detailsText->clear();
|
||||
}
|
||||
|
||||
void MainWindow::selectAndGoTo(const QModelIndex &index)
|
||||
{
|
||||
m_treeView->scrollTo(index, QAbstractItemView::PositionAtCenter);
|
||||
m_treeView->setCurrentIndex(index);
|
||||
}
|
||||
|
||||
void MainWindow::detectFile()
|
||||
{
|
||||
const QString fileName = QFileDialog::getOpenFileName(this, tr("Choose File"));
|
||||
if (fileName.isEmpty())
|
||||
return;
|
||||
QMimeDatabase mimeDatabase;
|
||||
const QFileInfo fi(fileName);
|
||||
const QMimeType mimeType = mimeDatabase.mimeTypeForFile(fi);
|
||||
const QModelIndex index = mimeType.isValid()
|
||||
? m_model->indexForMimeType(mimeType.name()) : QModelIndex();
|
||||
if (index.isValid()) {
|
||||
statusBar()->showMessage(tr("\"%1\" is of type \"%2\"").arg(fi.fileName(), mimeType.name()));
|
||||
selectAndGoTo(index);
|
||||
} else {
|
||||
QMessageBox::information(this, tr("Unknown File Type"),
|
||||
tr("The type of %1 could not be determined.")
|
||||
.arg(QDir::toNativeSeparators(fileName)));
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::updateFindActions()
|
||||
{
|
||||
const bool findNextPreviousEnabled = m_findMatches.size() > 1;
|
||||
m_findNextAction->setEnabled(findNextPreviousEnabled);
|
||||
m_findPreviousAction->setEnabled(findNextPreviousEnabled);
|
||||
}
|
||||
|
||||
void MainWindow::findPrevious()
|
||||
{
|
||||
if (--m_findIndex < 0)
|
||||
m_findIndex = m_findMatches.size() - 1;
|
||||
if (m_findIndex >= 0)
|
||||
selectAndGoTo(m_findMatches.at(m_findIndex));
|
||||
}
|
||||
|
||||
void MainWindow::findNext()
|
||||
{
|
||||
if (++m_findIndex >= m_findMatches.size())
|
||||
m_findIndex = 0;
|
||||
if (m_findIndex < m_findMatches.size())
|
||||
selectAndGoTo(m_findMatches.at(m_findIndex));
|
||||
}
|
||||
|
||||
void MainWindow::find()
|
||||
{
|
||||
QInputDialog inputDialog(this);
|
||||
inputDialog.setWindowTitle(tr("Find"));
|
||||
inputDialog.setLabelText(tr("Text:"));
|
||||
if (inputDialog.exec() != QDialog::Accepted)
|
||||
return;
|
||||
const QString value = inputDialog.textValue().trimmed();
|
||||
if (value.isEmpty())
|
||||
return;
|
||||
|
||||
m_findMatches.clear();
|
||||
m_findIndex = 0;
|
||||
foreach (const QStandardItem *item, m_model->findItems(value, Qt::MatchContains | Qt::MatchFixedString | Qt::MatchRecursive))
|
||||
m_findMatches.append(m_model->indexFromItem(item));
|
||||
statusBar()->showMessage(tr("%n mime types match \"%1\".", 0, m_findMatches.size()).arg(value));
|
||||
updateFindActions();
|
||||
if (!m_findMatches.isEmpty())
|
||||
selectAndGoTo(m_findMatches.constFirst());
|
||||
}
|
79
examples/corelib/mimetypes/mimetypebrowser/mainwindow.h
Normal file
79
examples/corelib/mimetypes/mimetypebrowser/mainwindow.h
Normal file
@ -0,0 +1,79 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2016 The Qt Company Ltd.
|
||||
** Contact: http://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the examples of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:BSD$
|
||||
** You may use this file under the terms of the BSD license as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of The Qt Company Ltd nor the names of its
|
||||
** contributors may be used to endorse or promote products derived
|
||||
** from this software without specific prior written permission.
|
||||
**
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef MAINWINDOW_H
|
||||
#define MAINWINDOW_H
|
||||
|
||||
#include <QMainWindow>
|
||||
#include <QModelIndexList>
|
||||
|
||||
QT_FORWARD_DECLARE_CLASS(QAction)
|
||||
QT_FORWARD_DECLARE_CLASS(QTextEdit)
|
||||
QT_FORWARD_DECLARE_CLASS(QTreeView)
|
||||
|
||||
class MimetypeModel;
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit MainWindow(QWidget *parent = nullptr);
|
||||
|
||||
private slots:
|
||||
void currentChanged(const QModelIndex &);
|
||||
void detectFile();
|
||||
void find();
|
||||
void findNext();
|
||||
void findPrevious();
|
||||
|
||||
private:
|
||||
void selectAndGoTo(const QModelIndex &index);
|
||||
void updateFindActions();
|
||||
|
||||
MimetypeModel *m_model;
|
||||
QTreeView *m_treeView;
|
||||
QTextEdit *m_detailsText;
|
||||
QAction *m_findNextAction;
|
||||
QAction *m_findPreviousAction;
|
||||
QModelIndexList m_findMatches;
|
||||
int m_findIndex;
|
||||
};
|
||||
|
||||
#endif // MAINWINDOW_H
|
@ -0,0 +1,13 @@
|
||||
TEMPLATE = app
|
||||
QT += widgets
|
||||
CONFIG -= app_bundle
|
||||
CONFIG += c++11
|
||||
|
||||
SOURCES += \
|
||||
main.cpp \
|
||||
mimetypemodel.cpp \
|
||||
mainwindow.cpp
|
||||
|
||||
HEADERS += \
|
||||
mimetypemodel.h \
|
||||
mainwindow.h
|
199
examples/corelib/mimetypes/mimetypebrowser/mimetypemodel.cpp
Normal file
199
examples/corelib/mimetypes/mimetypebrowser/mimetypemodel.cpp
Normal file
@ -0,0 +1,199 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2016 The Qt Company Ltd.
|
||||
** Contact: http://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the examples of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:BSD$
|
||||
** You may use this file under the terms of the BSD license as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of The Qt Company Ltd nor the names of its
|
||||
** contributors may be used to endorse or promote products derived
|
||||
** from this software without specific prior written permission.
|
||||
**
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "mimetypemodel.h"
|
||||
|
||||
#include <QIcon>
|
||||
|
||||
#include <QDebug>
|
||||
#include <QMimeDatabase>
|
||||
#include <QTextStream>
|
||||
#include <QVariant>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
Q_DECLARE_METATYPE(QMimeType)
|
||||
|
||||
typedef QList<QStandardItem *> StandardItemList;
|
||||
|
||||
enum { mimeTypeRole = Qt::UserRole + 1, iconQueriedRole = Qt::UserRole + 2 };
|
||||
|
||||
bool operator<(const QMimeType &t1, const QMimeType &t2)
|
||||
{
|
||||
return t1.name() < t2.name();
|
||||
}
|
||||
|
||||
static StandardItemList createRow(const QMimeType &t)
|
||||
{
|
||||
const QVariant v = QVariant::fromValue(t);
|
||||
QStandardItem *nameItem = new QStandardItem(t.name());
|
||||
const Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
|
||||
nameItem->setData(v, mimeTypeRole);
|
||||
nameItem->setData(QVariant(false), iconQueriedRole);
|
||||
nameItem->setFlags(flags);
|
||||
nameItem->setToolTip(t.comment());
|
||||
return StandardItemList{nameItem};
|
||||
}
|
||||
|
||||
MimetypeModel::MimetypeModel(QObject *parent)
|
||||
: QStandardItemModel(0, ColumnCount, parent)
|
||||
{
|
||||
setHorizontalHeaderLabels(QStringList{tr("Name")});
|
||||
populate();
|
||||
}
|
||||
|
||||
QVariant MimetypeModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (role != Qt::DecorationRole || !index.isValid() || index.data(iconQueriedRole).toBool())
|
||||
return QStandardItemModel::data(index, role);
|
||||
QStandardItem *item = itemFromIndex(index);
|
||||
const QString iconName = item->data(mimeTypeRole).value<QMimeType>().iconName();
|
||||
if (!iconName.isEmpty())
|
||||
item->setIcon(QIcon::fromTheme(iconName));
|
||||
item->setData(QVariant(true), iconQueriedRole);
|
||||
return item->icon();
|
||||
}
|
||||
|
||||
QMimeType MimetypeModel::mimeType(const QModelIndex &index) const
|
||||
{
|
||||
return index.data(mimeTypeRole).value<QMimeType>();
|
||||
}
|
||||
|
||||
void MimetypeModel::populate()
|
||||
{
|
||||
typedef QList<QMimeType>::Iterator Iterator;
|
||||
|
||||
QMimeDatabase mimeDatabase;
|
||||
QList<QMimeType> allTypes = mimeDatabase.allMimeTypes();
|
||||
|
||||
// Move top level types to rear end of list, sort this partition,
|
||||
// create top level items and truncate the list.
|
||||
Iterator end = allTypes.end();
|
||||
const Iterator topLevelStart =
|
||||
std::stable_partition(allTypes.begin(), end,
|
||||
[](const QMimeType &t) { return !t.parentMimeTypes().isEmpty(); });
|
||||
std::stable_sort(topLevelStart, end);
|
||||
for (Iterator it = topLevelStart; it != end; ++it) {
|
||||
const StandardItemList row = createRow(*it);
|
||||
appendRow(row);
|
||||
m_nameIndexHash.insert(it->name(), indexFromItem(row.constFirst()));
|
||||
}
|
||||
allTypes.erase(topLevelStart, end);
|
||||
|
||||
while (!allTypes.isEmpty()) {
|
||||
// Find a type inheriting one that is already in the model.
|
||||
end = allTypes.end();
|
||||
auto nameIndexIt = m_nameIndexHash.constEnd();
|
||||
for (Iterator it = allTypes.begin(); it != end; ++it) {
|
||||
nameIndexIt = m_nameIndexHash.constFind(it->parentMimeTypes().constFirst());
|
||||
if (nameIndexIt != m_nameIndexHash.constEnd())
|
||||
break;
|
||||
}
|
||||
if (nameIndexIt == m_nameIndexHash.constEnd()) {
|
||||
qWarning() << "Orphaned mime types:" << allTypes;
|
||||
break;
|
||||
}
|
||||
|
||||
// Move types inheriting the parent type to rear end of list, sort this partition,
|
||||
// append the items to parent and truncate the list.
|
||||
const QString &parentName = nameIndexIt.key();
|
||||
const Iterator start =
|
||||
std::stable_partition(allTypes.begin(), end,
|
||||
[parentName](const QMimeType &t) { return !t.inherits(parentName); });
|
||||
std::stable_sort(start, end);
|
||||
QStandardItem *parentItem = itemFromIndex(nameIndexIt.value());
|
||||
for (Iterator it = start; it != end; ++it) {
|
||||
const StandardItemList row = createRow(*it);
|
||||
parentItem->appendRow(row);
|
||||
m_nameIndexHash.insert(it->name(), indexFromItem(row.constFirst()));
|
||||
}
|
||||
allTypes.erase(start, end);
|
||||
}
|
||||
}
|
||||
|
||||
QTextStream &operator<<(QTextStream &stream, const QStringList &list)
|
||||
{
|
||||
for (int i = 0, size = list.size(); i < size; ++i) {
|
||||
if (i)
|
||||
stream << ", ";
|
||||
stream << list.at(i);
|
||||
}
|
||||
return stream;
|
||||
}
|
||||
|
||||
QString MimetypeModel::formatMimeTypeInfo(const QMimeType &t)
|
||||
{
|
||||
QString result;
|
||||
QTextStream str(&result);
|
||||
str << "<html><head/><body><h3><center>" << t.name() << "</center></h3><br><table>";
|
||||
|
||||
const QStringList &aliases = t.aliases();
|
||||
if (!aliases.isEmpty())
|
||||
str << "<tr><td>Aliases:</td><td>" << " (" << aliases << ')';
|
||||
|
||||
str << "</td></tr>"
|
||||
<< "<tr><td>Comment:</td><td>" << t.comment() << "</td></tr>"
|
||||
<< "<tr><td>Icon name:</td><td>" << t.iconName() << "</td></tr>";
|
||||
|
||||
const QString &filter = t.filterString();
|
||||
if (!filter.isEmpty())
|
||||
str << "<tr><td>Filter:</td><td>" << t.filterString() << "</td></tr>";
|
||||
|
||||
const QStringList &patterns = t.globPatterns();
|
||||
if (!patterns.isEmpty())
|
||||
str << "<tr><td>Glob patterns:</td><td>" << patterns << "</td></tr>";
|
||||
|
||||
const QStringList &parentMimeTypes = t.parentMimeTypes();
|
||||
if (!parentMimeTypes.isEmpty())
|
||||
str << "<tr><td>Parent types:</td><td>" << t.parentMimeTypes() << "</td></tr>";
|
||||
|
||||
QStringList suffixes = t.suffixes();
|
||||
if (!suffixes.isEmpty()) {
|
||||
str << "<tr><td>Suffixes:</td><td>";
|
||||
const QString &preferredSuffix = t.preferredSuffix();
|
||||
if (!preferredSuffix.isEmpty()) {
|
||||
suffixes.removeOne(preferredSuffix);
|
||||
str << "<b>" << preferredSuffix << "</b> ";
|
||||
}
|
||||
str << suffixes << "</td></tr>";
|
||||
}
|
||||
str << "</table></body></html>";
|
||||
return result;
|
||||
}
|
74
examples/corelib/mimetypes/mimetypebrowser/mimetypemodel.h
Normal file
74
examples/corelib/mimetypes/mimetypebrowser/mimetypemodel.h
Normal file
@ -0,0 +1,74 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2016 The Qt Company Ltd.
|
||||
** Contact: http://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of the examples of the Qt Toolkit.
|
||||
**
|
||||
** $QT_BEGIN_LICENSE:BSD$
|
||||
** You may use this file under the terms of the BSD license as follows:
|
||||
**
|
||||
** "Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions are
|
||||
** met:
|
||||
** * Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
** * Redistributions in binary form must reproduce the above copyright
|
||||
** notice, this list of conditions and the following disclaimer in
|
||||
** the documentation and/or other materials provided with the
|
||||
** distribution.
|
||||
** * Neither the name of The Qt Company Ltd nor the names of its
|
||||
** contributors may be used to endorse or promote products derived
|
||||
** from this software without specific prior written permission.
|
||||
**
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
||||
**
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef MIMETYPEMODEL_H
|
||||
#define MIMETYPEMODEL_H
|
||||
|
||||
#include <QStandardItemModel>
|
||||
#include <QHash>
|
||||
|
||||
QT_FORWARD_DECLARE_CLASS(QMimeType)
|
||||
|
||||
class MimetypeModel : public QStandardItemModel
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum Columns { NameColumn, ColumnCount };
|
||||
|
||||
explicit MimetypeModel(QObject *parent = nullptr);
|
||||
|
||||
QVariant data(const QModelIndex &index, int role) const override;
|
||||
|
||||
QMimeType mimeType(const QModelIndex &) const;
|
||||
|
||||
QModelIndex indexForMimeType(const QString &name) const
|
||||
{ return m_nameIndexHash.value(name); }
|
||||
|
||||
static QString formatMimeTypeInfo(const QMimeType &);
|
||||
|
||||
private:
|
||||
typedef QHash<QString, QModelIndex> NameIndexHash;
|
||||
|
||||
void populate();
|
||||
|
||||
NameIndexHash m_nameIndexHash;
|
||||
};
|
||||
|
||||
#endif // MIMETYPEMODEL_H
|
3
examples/corelib/mimetypes/mimetypes.pro
Normal file
3
examples/corelib/mimetypes/mimetypes.pro
Normal file
@ -0,0 +1,3 @@
|
||||
TEMPLATE = subdirs
|
||||
|
||||
SUBDIRS = mimetypebrowser
|
Loading…
Reference in New Issue
Block a user