File indexing completed on 2024-05-19 05:05:30
0001 /*************************************************************************** 0002 * SPDX-License-Identifier: GPL-2.0-or-later 0003 * * 0004 * SPDX-FileCopyrightText: 2004-2018 Thomas Fischer <fischer@unix-ag.uni-kl.de> 0005 * * 0006 * This program is free software; you can redistribute it and/or modify * 0007 * it under the terms of the GNU General Public License as published by * 0008 * the Free Software Foundation; either version 2 of the License, or * 0009 * (at your option) any later version. * 0010 * * 0011 * This program is distributed in the hope that it will be useful, * 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0014 * GNU General Public License for more details. * 0015 * * 0016 * You should have received a copy of the GNU General Public License * 0017 * along with this program; if not, see <https://www.gnu.org/licenses/>. * 0018 ***************************************************************************/ 0019 0020 #include "italictextitemmodel.h" 0021 0022 #include <QFont> 0023 0024 #include "logging_gui.h" 0025 0026 class ItalicTextItemModel::Private 0027 { 0028 public: 0029 QList<QPair<QString, QString> > data; 0030 0031 Private(ItalicTextItemModel *parent) 0032 { 0033 Q_UNUSED(parent) 0034 } 0035 }; 0036 0037 ItalicTextItemModel::ItalicTextItemModel(QObject *parent) 0038 : QAbstractItemModel(parent), d(new Private(this)) 0039 { 0040 /// nothing 0041 } 0042 0043 ItalicTextItemModel::~ItalicTextItemModel() 0044 { 0045 delete d; 0046 } 0047 0048 void ItalicTextItemModel::addItem(const QString &a, const QString &b) 0049 { 0050 /// Store both arguments in a pair of strings, 0051 /// added to the data structure 0052 d->data.append(QPair<QString, QString>(a, b)); 0053 } 0054 0055 QVariant ItalicTextItemModel::data(const QModelIndex &index, int role) const 0056 { 0057 /// Test and ignore for invalid rows 0058 if (index.row() < 0 || index.row() >= d->data.count()) 0059 return QVariant(); 0060 0061 if (role == Qt::FontRole) { 0062 QFont font; 0063 /// If the identifier for a data row is empty, 0064 /// set the row's text in italics 0065 if (d->data[index.row()].second.isEmpty()) 0066 font.setItalic(true); 0067 return font; 0068 } else if (role == Qt::DisplayRole) { 0069 /// Show text as passed as first parameter to function addItem 0070 return d->data[index.row()].first; 0071 } else if (role == Qt::UserRole) { 0072 qCWarning(LOG_KBIBTEX_GUI) << "Requesting data from Qt::UserRole is deprecated, should not happen"; 0073 } else if (role == IdentifierRole) { 0074 /// Return string as passed as second parameter to function addItem 0075 return d->data[index.row()].second; 0076 } 0077 0078 return QVariant(); 0079 } 0080 0081 QModelIndex ItalicTextItemModel::index(int row, int column, const QModelIndex &) const 0082 { 0083 return createIndex(row, column); 0084 } 0085 0086 QModelIndex ItalicTextItemModel::parent(const QModelIndex &) const 0087 { 0088 /// Flat, single-level model 0089 return QModelIndex(); 0090 } 0091 0092 int ItalicTextItemModel::rowCount(const QModelIndex &) const 0093 { 0094 /// As many rows as elements in data structure 0095 /// (should be as many calls to addItem were made) 0096 return d->data.count(); 0097 } 0098 0099 int ItalicTextItemModel::columnCount(const QModelIndex &) const 0100 { 0101 /// Just one column, thus suitable for combo boxes 0102 return 1; 0103 }