QtBase  v6.3.1
mimetypemodel.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 #include "mimetypemodel.h"
52 
53 #include <QDebug>
54 #include <QIcon>
55 #include <QMimeDatabase>
56 #include <QTextStream>
57 #include <QVariant>
58 
59 #include <algorithm>
60 
62 
64 
66 
68 bool operator<(const QMimeType &t1, const QMimeType &t2)
69 {
70  return t1.name() < t2.name();
71 }
73 
74 static StandardItemList createRow(const QMimeType &t)
75 {
77  QStandardItem *nameItem = new QStandardItem(t.name());
78  const Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
79  nameItem->setData(v, mimeTypeRole);
80  nameItem->setData(QVariant(false), iconQueriedRole);
81  nameItem->setFlags(flags);
82  nameItem->setToolTip(t.comment());
83  return StandardItemList{nameItem};
84 }
85 
88 {
90  populate();
91 }
92 
94 {
95  if (role != Qt::DecorationRole || !index.isValid() || index.data(iconQueriedRole).toBool())
96  return QStandardItemModel::data(index, role);
98  const QString iconName = qvariant_cast<QMimeType>(item->data(mimeTypeRole)).iconName();
99  if (!iconName.isEmpty())
100  item->setIcon(QIcon::fromTheme(iconName));
102  return item->icon();
103 }
104 
106 {
107  return qvariant_cast<QMimeType>(index.data(mimeTypeRole));
108 }
109 
110 void MimetypeModel::populate()
111 {
112  typedef QList<QMimeType>::Iterator Iterator;
113 
114  QMimeDatabase mimeDatabase;
115  QList<QMimeType> allTypes = mimeDatabase.allMimeTypes();
116 
117  // Move top level types to rear end of list, sort this partition,
118  // create top level items and truncate the list.
119  Iterator end = allTypes.end();
120  const Iterator topLevelStart =
121  std::stable_partition(allTypes.begin(), end,
122  [](const QMimeType &t) { return !t.parentMimeTypes().isEmpty(); });
123  std::stable_sort(topLevelStart, end);
124  for (Iterator it = topLevelStart; it != end; ++it) {
125  const StandardItemList row = createRow(*it);
126  appendRow(row);
127  m_nameIndexHash.insert(it->name(), indexFromItem(row.constFirst()));
128  }
129  allTypes.erase(topLevelStart, end);
130 
131  while (!allTypes.isEmpty()) {
132  // Find a type inheriting one that is already in the model.
133  end = allTypes.end();
134  auto nameIndexIt = m_nameIndexHash.constEnd();
135  for (Iterator it = allTypes.begin(); it != end; ++it) {
136  nameIndexIt = m_nameIndexHash.constFind(it->parentMimeTypes().constFirst());
137  if (nameIndexIt != m_nameIndexHash.constEnd())
138  break;
139  }
140  if (nameIndexIt == m_nameIndexHash.constEnd()) {
141  qWarning() << "Orphaned mime types:" << allTypes;
142  break;
143  }
144 
145  // Move types inheriting the parent type to rear end of list, sort this partition,
146  // append the items to parent and truncate the list.
147  const QString &parentName = nameIndexIt.key();
148  const Iterator start =
149  std::stable_partition(allTypes.begin(), end, [parentName](const QMimeType &t)
150  { return !t.parentMimeTypes().contains(parentName); });
151  std::stable_sort(start, end);
152  QStandardItem *parentItem = itemFromIndex(nameIndexIt.value());
153  for (Iterator it = start; it != end; ++it) {
154  const StandardItemList row = createRow(*it);
155  parentItem->appendRow(row);
156  m_nameIndexHash.insert(it->name(), indexFromItem(row.constFirst()));
157  }
158  allTypes.erase(start, end);
159  }
160 }
161 
163 {
164  for (int i = 0, size = list.size(); i < size; ++i) {
165  if (i)
166  stream << ", ";
167  stream << list.at(i);
168  }
169  return stream;
170 }
171 
173 {
174  QString result;
176  str << "<html><head/><body><h3><center>" << t.name() << "</center></h3><br><table>";
177 
178  const QStringList &aliases = t.aliases();
179  if (!aliases.isEmpty())
180  str << "<tr><td>Aliases:</td><td>" << " (" << aliases << ')';
181 
182  str << "</td></tr>"
183  << "<tr><td>Comment:</td><td>" << t.comment() << "</td></tr>"
184  << "<tr><td>Icon name:</td><td>" << t.iconName() << "</td></tr>"
185  << "<tr><td>Generic icon name</td><td>" << t.genericIconName() << "</td></tr>";
186 
187  const QString &filter = t.filterString();
188  if (!filter.isEmpty())
189  str << "<tr><td>Filter:</td><td>" << t.filterString() << "</td></tr>";
190 
191  const QStringList &patterns = t.globPatterns();
192  if (!patterns.isEmpty())
193  str << "<tr><td>Glob patterns:</td><td>" << patterns << "</td></tr>";
194 
195  const QStringList &parentMimeTypes = t.parentMimeTypes();
196  if (!parentMimeTypes.isEmpty())
197  str << "<tr><td>Parent types:</td><td>" << t.parentMimeTypes() << "</td></tr>";
198 
199  QStringList suffixes = t.suffixes();
200  if (!suffixes.isEmpty()) {
201  str << "<tr><td>Suffixes:</td><td>";
202  const QString &preferredSuffix = t.preferredSuffix();
203  if (!preferredSuffix.isEmpty()) {
204  suffixes.removeOne(preferredSuffix);
205  str << "<b>" << preferredSuffix << "</b> ";
206  }
207  str << suffixes << "</td></tr>";
208  }
209  str << "</table></body></html>";
210  return result;
211 }
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
QVariant data(const QModelIndex &index, int role) const override
QMimeType mimeType(const QModelIndex &) const
static QString formatMimeTypeInfo(const QMimeType &)
MimetypeModel(QObject *parent=nullptr)
operator<<(QDataStream &ds, qfloat16 f)
Definition: qfloat16.cpp:327
bool operator<(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
const_iterator constFind(const Key &key) const noexcept
Definition: qhash.h:1224
const_iterator constEnd() const noexcept
Definition: qhash.h:1162
iterator insert(const Key &key, const T &value)
Definition: qhash.h:1228
static QIcon fromTheme(const QString &name)
Definition: qicon.cpp:1311
Definition: qlist.h:108
bool isEmpty() const noexcept
Definition: qlist.h:418
iterator erase(const_iterator begin, const_iterator end)
Definition: qlist.h:893
iterator end()
Definition: qlist.h:624
iterator begin()
Definition: qlist.h:623
The QMimeDatabase class maintains a database of MIME types.
Definition: qmimedatabase.h:59
QList< QMimeType > allMimeTypes() const
The QMimeType class describes types of file or data, represented by a MIME type string.
Definition: qmimetype.h:61
The QModelIndex class is used to locate data in a data model.
The QObject class is the base class of all Qt objects.
Definition: qobject.h:125
The QStandardItem class provides an item for use with the QStandardItemModel class.
virtual void setData(const QVariant &value, int role=Qt::UserRole+1)
void setToolTip(const QString &toolTip)
void setIcon(const QIcon &icon)
virtual QVariant data(int role=Qt::UserRole+1) const
QIcon icon() const
void setFlags(Qt::ItemFlags flags)
void appendRow(const QList< QStandardItem * > &items)
The QStandardItemModel class provides a generic model for storing custom data.\inmodule QtGui.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
void setHorizontalHeaderLabels(const QStringList &labels)
QStandardItem * item(int row, int column=0) const
void appendRow(const QList< QStandardItem * > &items)
QModelIndex indexFromItem(const QStandardItem *item) const
QStandardItem * itemFromIndex(const QModelIndex &index) const
The QString class provides a Unicode character string.
Definition: qstring.h:388
bool isEmpty() const
Definition: qstring.h:1216
The QStringList class provides a list of strings.
The QTextStream class provides a convenient interface for reading and writing text.
Definition: qtextstream.h:62
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:95
static auto fromValue(const T &value) -> std::enable_if_t< std::is_copy_constructible_v< T >, QVariant >
Definition: qvariant.h:391
QString str
[2]
@ iconQueriedRole
@ mimeTypeRole
@ UserRole
Definition: qnamespace.h:1527
@ DecorationRole
Definition: qnamespace.h:1503
@ ItemIsSelectable
Definition: qnamespace.h:1532
@ ItemIsEnabled
Definition: qnamespace.h:1537
EGLStreamKHR stream
@ ColumnCount
#define qWarning
Definition: qlogging.h:179
#define Q_DECLARE_METATYPE(TYPE)
Definition: qmetatype.h:1417
GLsizei const GLfloat * v
[13]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint index
[2]
GLuint GLuint end
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
[4]
GLbitfield flags
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
GLuint start
GLdouble GLdouble t
[9]
Definition: qopenglext.h:243
GLenum GLenum GLsizei void * row
Definition: qopenglext.h:2747
GLuint64EXT * result
[6]
Definition: qopenglext.h:10932
#define tr(X)
QSize t2(10, 12)
QStringList::Iterator it
QStringList list
[0]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent