File indexing completed on 2024-05-19 05:05:31

0001 /***************************************************************************
0002  *   SPDX-License-Identifier: GPL-2.0-or-later
0003  *                                                                         *
0004  *   SPDX-FileCopyrightText: 2004-2023 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 #ifndef KBIBTEX_GUI_VALUELISTMODEL_H
0021 #define KBIBTEX_GUI_VALUELISTMODEL_H
0022 
0023 #include <QAbstractTableModel>
0024 #include <QTreeView>
0025 #include <QStyledItemDelegate>
0026 
0027 #include <NotificationHub>
0028 #include <Value>
0029 #include <models/FileModel>
0030 
0031 #include "kbibtexgui_export.h"
0032 
0033 class KBIBTEXGUI_EXPORT ValueListDelegate : public QStyledItemDelegate
0034 {
0035     Q_OBJECT
0036 
0037 public:
0038     explicit ValueListDelegate(QTreeView *parent = nullptr);
0039     ~ValueListDelegate();
0040 
0041     QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const override;
0042     void setEditorData(QWidget *editor, const QModelIndex &index) const override;
0043     void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
0044     QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
0045     void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override;
0046     void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
0047 
0048     void setFieldName(const QString &fieldName);
0049 
0050 private:
0051     class Private;
0052     Private *const d;
0053 };
0054 
0055 class KBIBTEXGUI_EXPORT ValueListModel : public QAbstractTableModel, private NotificationListener
0056 {
0057     Q_OBJECT
0058 
0059 public:
0060     enum ValueListModelRole {
0061         /// How many occurrences a value has
0062         CountRole = Qt::UserRole + 112,
0063         /// Role to sort values by
0064         SortRole = Qt::UserRole + 113,
0065         /// Role to get text to filter for
0066         SearchTextRole = Qt::UserRole + 114
0067     };
0068 
0069     enum class SortBy { Text, Count };
0070 
0071 private:
0072     struct ValueLine {
0073         QString text;
0074         QString sortBy;
0075         Value value;
0076         int count;
0077     };
0078 
0079     typedef QVector<ValueLine> ValueLineList;
0080 
0081     const File *file;
0082     const QString fName;
0083     ValueLineList values;
0084     QMap<QString, QString> colorToLabel;
0085     bool showCountColumn;
0086     SortBy sortBy;
0087 
0088 public:
0089     ValueListModel(const File *bibtexFile, const QString &fieldName, QObject *parent);
0090 
0091     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0092     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0093     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0094     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
0095     Qt::ItemFlags flags(const QModelIndex &index) const override;
0096     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0097     void removeValue(const QModelIndex &index);
0098 
0099     void setShowCountColumn(bool showCountColumn);
0100     void setSortBy(SortBy sortBy);
0101 
0102     void notificationEvent(int eventId) override;
0103 
0104 private:
0105     void readConfiguration();
0106     void updateValues();
0107     void insertValue(const Value &value);
0108     void insertText(const QString &text, const QSharedPointer<ValueItem> &item, const QString &sortBy = QString());
0109     int indexOf(const QString &text);
0110     QString htmlize(const QString &text) const;
0111 
0112     bool searchAndReplaceValueInEntries(const QModelIndex &index, const Value &newValue);
0113     bool searchAndReplaceValueInModel(const QModelIndex &index, const Value &newValue);
0114     void removeValueFromEntries(const QModelIndex &index);
0115     void removeValueFromModel(const QModelIndex &index);
0116 };
0117 
0118 
0119 #endif // KBIBTEX_GUI_VALUELISTMODEL_H