From 9a96eb00c4ef5c3025eb852e9bba6b51706db286 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Fri, 18 Mar 2016 14:40:19 +0100 Subject: 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 --- examples/corelib/corelib.pro | 1 + .../corelib/mimetypes/mimetypebrowser/main.cpp | 69 +++++++ .../mimetypes/mimetypebrowser/mainwindow.cpp | 185 +++++++++++++++++++ .../corelib/mimetypes/mimetypebrowser/mainwindow.h | 79 ++++++++ .../mimetypes/mimetypebrowser/mimetypebrowser.pro | 13 ++ .../mimetypes/mimetypebrowser/mimetypemodel.cpp | 199 +++++++++++++++++++++ .../mimetypes/mimetypebrowser/mimetypemodel.h | 74 ++++++++ examples/corelib/mimetypes/mimetypes.pro | 3 + 8 files changed, 623 insertions(+) create mode 100644 examples/corelib/mimetypes/mimetypebrowser/main.cpp create mode 100644 examples/corelib/mimetypes/mimetypebrowser/mainwindow.cpp create mode 100644 examples/corelib/mimetypes/mimetypebrowser/mainwindow.h create mode 100644 examples/corelib/mimetypes/mimetypebrowser/mimetypebrowser.pro create mode 100644 examples/corelib/mimetypes/mimetypebrowser/mimetypemodel.cpp create mode 100644 examples/corelib/mimetypes/mimetypebrowser/mimetypemodel.h create mode 100644 examples/corelib/mimetypes/mimetypes.pro diff --git a/examples/corelib/corelib.pro b/examples/corelib/corelib.pro index ec6e19c2b5..a32b8ba51a 100644 --- a/examples/corelib/corelib.pro +++ b/examples/corelib/corelib.pro @@ -4,5 +4,6 @@ CONFIG += no_docs_target SUBDIRS = \ ipc \ json \ + mimetypes \ threads \ tools diff --git a/examples/corelib/mimetypes/mimetypebrowser/main.cpp b/examples/corelib/mimetypes/mimetypebrowser/main.cpp new file mode 100644 index 0000000000..daa92a2344 --- /dev/null +++ b/examples/corelib/mimetypes/mimetypebrowser/main.cpp @@ -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 +#include + +#include +#include +#include +#include +#include + +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(); +} diff --git a/examples/corelib/mimetypes/mimetypebrowser/mainwindow.cpp b/examples/corelib/mimetypes/mimetypebrowser/mainwindow.cpp new file mode 100644 index 0000000000..edd48021f3 --- /dev/null +++ b/examples/corelib/mimetypes/mimetypebrowser/mainwindow.cpp @@ -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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +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()); +} diff --git a/examples/corelib/mimetypes/mimetypebrowser/mainwindow.h b/examples/corelib/mimetypes/mimetypebrowser/mainwindow.h new file mode 100644 index 0000000000..11b1f004a0 --- /dev/null +++ b/examples/corelib/mimetypes/mimetypebrowser/mainwindow.h @@ -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 +#include + +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 diff --git a/examples/corelib/mimetypes/mimetypebrowser/mimetypebrowser.pro b/examples/corelib/mimetypes/mimetypebrowser/mimetypebrowser.pro new file mode 100644 index 0000000000..3788632c30 --- /dev/null +++ b/examples/corelib/mimetypes/mimetypebrowser/mimetypebrowser.pro @@ -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 diff --git a/examples/corelib/mimetypes/mimetypebrowser/mimetypemodel.cpp b/examples/corelib/mimetypes/mimetypebrowser/mimetypemodel.cpp new file mode 100644 index 0000000000..81e4eb10cd --- /dev/null +++ b/examples/corelib/mimetypes/mimetypebrowser/mimetypemodel.cpp @@ -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 + +#include +#include +#include +#include + +#include + +Q_DECLARE_METATYPE(QMimeType) + +typedef QList 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().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(); +} + +void MimetypeModel::populate() +{ + typedef QList::Iterator Iterator; + + QMimeDatabase mimeDatabase; + QList 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 << "

" << t.name() << "


"; + + const QStringList &aliases = t.aliases(); + if (!aliases.isEmpty()) + str << "" + << "" + << ""; + + const QString &filter = t.filterString(); + if (!filter.isEmpty()) + str << ""; + + const QStringList &patterns = t.globPatterns(); + if (!patterns.isEmpty()) + str << ""; + + const QStringList &parentMimeTypes = t.parentMimeTypes(); + if (!parentMimeTypes.isEmpty()) + str << ""; + + QStringList suffixes = t.suffixes(); + if (!suffixes.isEmpty()) { + str << ""; + } + str << "
Aliases:" << " (" << aliases << ')'; + + str << "
Comment:" << t.comment() << "
Icon name:" << t.iconName() << "
Filter:" << t.filterString() << "
Glob patterns:" << patterns << "
Parent types:" << t.parentMimeTypes() << "
Suffixes:"; + const QString &preferredSuffix = t.preferredSuffix(); + if (!preferredSuffix.isEmpty()) { + suffixes.removeOne(preferredSuffix); + str << "" << preferredSuffix << " "; + } + str << suffixes << "
"; + return result; +} diff --git a/examples/corelib/mimetypes/mimetypebrowser/mimetypemodel.h b/examples/corelib/mimetypes/mimetypebrowser/mimetypemodel.h new file mode 100644 index 0000000000..0656e64e8f --- /dev/null +++ b/examples/corelib/mimetypes/mimetypebrowser/mimetypemodel.h @@ -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 +#include + +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 NameIndexHash; + + void populate(); + + NameIndexHash m_nameIndexHash; +}; + +#endif // MIMETYPEMODEL_H diff --git a/examples/corelib/mimetypes/mimetypes.pro b/examples/corelib/mimetypes/mimetypes.pro new file mode 100644 index 0000000000..5eaa816871 --- /dev/null +++ b/examples/corelib/mimetypes/mimetypes.pro @@ -0,0 +1,3 @@ +TEMPLATE = subdirs + +SUBDIRS = mimetypebrowser -- cgit v1.2.3