1 /***************************************************************************
2  *   Copyright (C) 2004-2017 by Thomas Fischer <fischer@unix-ag.uni-kl.de> *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, see <https://www.gnu.org/licenses/>. *
16  ***************************************************************************/
17 
18 #ifndef KBIBTEX_GUI_VALUELISTMODEL_H
19 #define KBIBTEX_GUI_VALUELISTMODEL_H
20 
21 #include "kbibtexgui_export.h"
22 
23 #include <QAbstractTableModel>
24 #include <QTreeView>
25 #include <QStyledItemDelegate>
26 
27 #include "notificationhub.h"
28 #include "models/filemodel.h"
29 
30 class KBIBTEXGUI_EXPORT ValueListDelegate : public QStyledItemDelegate
31 {
32     Q_OBJECT
33 
34 private:
35     QString m_fieldName;
36     QTreeView *m_parent;
37 
38 public:
39     explicit ValueListDelegate(QTreeView *parent = nullptr)
QStyledItemDelegate(parent)40             : QStyledItemDelegate(parent), m_fieldName(QString()), m_parent(parent) {}
41 
42     QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const override;
43     void setEditorData(QWidget *editor, const QModelIndex &index) const override;
44     void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
45     QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
46     void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override;
47     void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
48 
setFieldName(const QString & fieldName)49     void setFieldName(const QString &fieldName) {
50         m_fieldName = fieldName;
51     }
52 
53 private slots:
54     void commitAndCloseEditor();
55 };
56 
57 class KBIBTEXGUI_EXPORT ValueListModel : public QAbstractTableModel, private NotificationListener
58 {
59     Q_OBJECT
60 
61 public:
62     enum ValueListModelRole {
63         /// How many occurrences a value has
64         CountRole = Qt::UserRole + 112,
65         /// Role to sort values by
66         SortRole = Qt::UserRole + 113,
67         /// Role to get text to filter for
68         SearchTextRole = Qt::UserRole + 114
69     };
70 
71     enum SortBy { SortByText, SortByCount };
72 
73 private:
74     struct ValueLine {
75         QString text;
76         QString sortBy;
77         Value value;
78         int count;
79     };
80 
81     typedef QVector<ValueLine> ValueLineList;
82 
83     const File *file;
84     const QString fName;
85     ValueLineList values;
86     QMap<QString, QString> colorToLabel;
87     bool showCountColumn;
88     SortBy sortBy;
89 
90 public:
91     ValueListModel(const File *bibtexFile, const QString &fieldName, QObject *parent);
92 
93     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
94     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
95     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
96     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
97     Qt::ItemFlags flags(const QModelIndex &index) const override;
98     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
99     void removeValue(const QModelIndex &index);
100 
101     void setShowCountColumn(bool showCountColumn);
102     void setSortBy(SortBy sortBy);
103 
104     void notificationEvent(int eventId) override;
105 
106 private:
107     void readConfiguration();
108     void updateValues();
109     void insertValue(const Value &value);
110     int indexOf(const QString &text);
111     QString htmlize(const QString &text) const;
112 
113     bool searchAndReplaceValueInEntries(const QModelIndex &index, const Value &newValue);
114     bool searchAndReplaceValueInModel(const QModelIndex &index, const Value &newValue);
115     void removeValueFromEntries(const QModelIndex &index);
116     void removeValueFromModel(const QModelIndex &index);
117 };
118 
119 
120 #endif // KBIBTEX_GUI_VALUELISTMODEL_H
121