File indexing completed on 2024-10-06 03:31:32

0001 // SPDX-FileCopyrightText: 2007-2010 Peter Hedlund <peter.hedlund@kdemail.net>
0002 // SPDX-FileCopyrightText: 2023 Carl Schwan <carl@carlschwan.eu>
0003 // SPDX-License-Identifier: GPL-2.0-or-later
0004 
0005 #pragma once
0006 
0007 #include <QSortFilterProxyModel>
0008 #include "kwqcardmodel.h"
0009 
0010 /// Randomize the order of the items contained inside the KWQCardModel
0011 ///
0012 /// \author Peter Hedlund <peter.hedlund@kdemail.net>
0013 class KWQRandomSortModel : public QSortFilterProxyModel
0014 {
0015     Q_OBJECT
0016     Q_PROPERTY(KWQCardModel *cardModel READ cardModel WRITE setCardModel NOTIFY cardModelChanged)
0017     Q_PROPERTY(bool showErrorsOnly READ showErrorsOnly WRITE setShowErrorsOnly NOTIFY showErrorsOnlyChanged)
0018 
0019 public:
0020     enum ExtraRoles {
0021         MultipleChoiceRole = KWQCardModel::ExtraUserRoles + 1,
0022     };
0023 
0024     explicit KWQRandomSortModel(QObject *parent = nullptr);
0025 
0026     KWQCardModel *cardModel() const;
0027     void setCardModel(KWQCardModel *cardModel);
0028 
0029     bool showErrorsOnly() const;
0030     void setShowErrorsOnly(const bool showErrorsOnly);
0031 
0032     QVariant data(const QModelIndex &index, int role) const override;
0033     QHash<int, QByteArray> roleNames() const override;
0034 
0035 public Q_SLOTS:
0036     void reset();
0037     void restoreNativeOrder();
0038     void shuffle();
0039     void markAsError(const int row);
0040     void unMarkAsError(const int row);
0041 
0042 Q_SIGNALS:
0043     void cardModelChanged();
0044     void showErrorsOnlyChanged();
0045 
0046 protected:
0047     bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
0048     bool filterAcceptsRow(const int sourceRow, const QModelIndex &sourceParent) const override;
0049 private:
0050     KWQCardModel * m_sourceModel;
0051 
0052     enum Mode {
0053         QuestionInLeftColumn = 1,
0054         QuestionInRightColumn = 2,
0055     };
0056     void setRandomModes();
0057 
0058     bool m_showErrorsOnly = false;
0059     bool m_restoreNativeOrder = false;
0060     bool m_shuffle = false;
0061     QList<int> m_shuffleList;
0062     QList<int> m_errors;
0063     QList<Mode> m_modes;
0064 };