File indexing completed on 2024-05-12 05:12:45

0001 /*
0002     SPDX-FileCopyrightText: 2018 Sandro Knauß <sknauss@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QAbstractItemModel>
0010 #include <QMap>
0011 
0012 class QStandardItemModel;
0013 
0014 class DebugModel : public QAbstractItemModel
0015 {
0016     Q_OBJECT
0017 public:
0018     enum Direction { ClientToServer, ServerToClient };
0019 
0020     struct Message {
0021         QString sender;
0022         Direction direction;
0023         QString message;
0024     };
0025 
0026     enum Roles { MessageRole = Qt::UserRole, IdentifierRole };
0027 
0028     enum Columns {
0029         DirectionColumn,
0030         SenderColumn,
0031         MessageColumn,
0032 
0033         _ColumnCount
0034     };
0035 
0036     explicit DebugModel(QObject *parent = nullptr);
0037     ~DebugModel() override;
0038 
0039     void addMessage(const QString &sender, Direction direction, const QString &message);
0040 
0041     void setSenderFilterModel(QStandardItemModel *senderFilterModel);
0042 
0043     int rowCount(const QModelIndex &parent = {}) const override;
0044     int columnCount(const QModelIndex &parent = {}) const override;
0045 
0046     QModelIndex index(int row, int column, const QModelIndex &parent = {}) const override;
0047     QModelIndex parent(const QModelIndex &child = {}) const override;
0048 
0049     QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
0050     QVariant data(const QModelIndex &index, int role) const override;
0051 
0052     bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
0053 
0054 private:
0055     QString cacheString(const QString &str, QMap<QString, QString> &cache, QStandardItemModel *model = nullptr);
0056     QString displaySender(const QString &identifer) const;
0057 
0058     QList<Message> mMessages;
0059     QStandardItemModel *mSenderFilterModel = nullptr;
0060     QMap<QString, QString> mSenderCache;
0061 };