File indexing completed on 2024-05-05 05:04:33

0001 /***************************************************************************
0002  *   SPDX-License-Identifier: GPL-2.0-or-later
0003  *                                                                         *
0004  *   SPDX-FileCopyrightText: 2016-2019 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 BIBLIOGRAPHY_MODEL_H
0021 #define BIBLIOGRAPHY_MODEL_H
0022 
0023 #include <QAbstractListModel>
0024 #include <QSortFilterProxyModel>
0025 #include <QSet>
0026 
0027 #include <File>
0028 #include <Value>
0029 #include "searchenginelist.h"
0030 
0031 class OnlineSearchAbstract;
0032 class BibliographyModel;
0033 
0034 class BibliographyRoles
0035 {
0036 public:
0037     enum Roles {BibTeXIdRole = Qt::UserRole + 100, FoundViaRole = Qt::UserRole + 102, EntryRole = Qt::UserRole + 999, TitleRole = Qt::UserRole + 1000, AuthorRole = Qt::UserRole + 1010, AuthorShortRole = Qt::UserRole + 1011, YearRole = Qt::UserRole + 1020, WherePublishedRole = Qt::UserRole + 1100, UrlRole = Qt::UserRole + 1110, DoiRole = Qt::UserRole + 1111};
0038 };
0039 
0040 class SortedBibliographyModel : public QSortFilterProxyModel, public BibliographyRoles {
0041     Q_OBJECT
0042 public:
0043     static const int SortAuthorNewestTitle, SortAuthorOldestTitle, SortNewestAuthorTitle, SortOldestAuthorTitle;
0044 
0045     SortedBibliographyModel();
0046     ~SortedBibliographyModel();
0047 
0048     virtual QHash<int, QByteArray> roleNames() const;
0049 
0050     bool isBusy() const;
0051     int progress() const;
0052     int sortOrder() const;
0053     void setSortOrder(int sortOrder);
0054     Q_PROPERTY(int sortOrder READ sortOrder WRITE setSortOrder NOTIFY sortOrderChanged)
0055     Q_INVOKABLE QStringList humanReadableSortOrder() const;
0056 
0057     Q_INVOKABLE void startSearch(const QString &freeText, const QString &title, const QString &author);
0058     Q_INVOKABLE void clear();
0059     Q_PROPERTY(bool busy READ isBusy NOTIFY busyChanged)
0060     Q_PROPERTY(int progress READ progress NOTIFY progressChanged)
0061 
0062 signals:
0063     void busyChanged();
0064     void progressChanged();
0065     void sortOrderChanged(int sortOrder);
0066 
0067 protected:
0068     virtual bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const;
0069 
0070 private:
0071     enum SortingTriState {True = -1, Undecided = 0, False = 1};
0072     enum AgeSorting {MostRecentFirst = 0, LeastRecentFirst = 1};
0073 
0074     SortingTriState compareAuthors(const QSharedPointer<const Entry> entryLeft, const QSharedPointer<const Entry> entryRight, SortingTriState previousDecision = Undecided) const;
0075     SortingTriState compareYears(const QSharedPointer<const Entry> entryLeft, const QSharedPointer<const Entry> entryRight, AgeSorting ageSorting, SortingTriState previousDecision = Undecided) const;
0076     SortingTriState compareTitles(const QSharedPointer<const Entry> entryLeft, const QSharedPointer<const Entry> entryRight, SortingTriState previousDecision = Undecided) const;
0077 
0078     int m_sortOrder;
0079     BibliographyModel *model;
0080 
0081     QString removeNobiliaryParticle(const QString &lastname) const;
0082 };
0083 
0084 class BibliographyModel : public QAbstractListModel, public BibliographyRoles {
0085     Q_OBJECT
0086 public:
0087     explicit BibliographyModel();
0088     ~BibliographyModel();
0089 
0090     int rowCount(const QModelIndex &parent = QModelIndex()) const;
0091     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
0092     const QSharedPointer<const Entry> entry(int row) const;
0093     static QString valueToText(const Value &value);
0094 
0095     bool isBusy() const;
0096     int progress() const;
0097 
0098     void startSearch(const QString &freeText, const QString &title, const QString &author);
0099     void clear();
0100 
0101 signals:
0102     void busyChanged();
0103     void progressChanged();
0104 
0105 private slots:
0106     void newEntry(QSharedPointer<Entry>);
0107     void searchFinished();
0108 
0109 private:
0110     File *m_file;
0111     int m_runningSearches;
0112     SearchEngineList *m_searchEngineList;
0113 
0114     static QStringList valueToList(const Value &value);
0115     static QString personToText(const QSharedPointer<const Person> &person);
0116     static QString valueItemToText(const QSharedPointer<ValueItem> &valueItem);
0117     static QString beautifyLaTeX(const QString &input);
0118 };
0119 
0120 #endif // BIBLIOGRAPHY_MODEL_H