KTextAddons

textautogeneratehistorymodel.cpp
1/*
2 SPDX-FileCopyrightText: 2025 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6#include "textautogeneratehistorymodel.h"
7
8using namespace TextAutogenerateText;
9TextAutoGenerateHistoryModel::TextAutoGenerateHistoryModel(QObject *parent)
10 : QAbstractListModel{parent}
11{
12}
13
14TextAutoGenerateHistoryModel::~TextAutoGenerateHistoryModel() = default;
15
16int TextAutoGenerateHistoryModel::rowCount(const QModelIndex &parent) const
17{
18 if (parent.isValid()) {
19 return 0; // flat model
20 }
21 return mHistoryInfos.count();
22}
23
24QVariant TextAutoGenerateHistoryModel::data(const QModelIndex &index, int role) const
25{
26 if (index.row() < 0 || index.row() >= mHistoryInfos.count()) {
27 return {};
28 }
29 const auto &info = mHistoryInfos[index.row()];
30 switch (role) {
31 case Qt::DisplayRole:
32 case Subject:
33 return info.subject();
34 case DateTime:
35 return info.dateTime();
36 case Model:
37 return info.model();
38 case Engine:
39 return info.engine();
40 case ReferenceUuid:
41 return info.referenceUuid();
42 }
43 return {};
44}
45
46QList<TextAutoGenerateHistoryInfo> TextAutoGenerateHistoryModel::historyInfos() const
47{
48 return mHistoryInfos;
49}
50
51void TextAutoGenerateHistoryModel::setHistoryInfos(const QList<TextAutoGenerateHistoryInfo> &newHistoryInfos)
52{
54 mHistoryInfos = newHistoryInfos;
56}
57
58void TextAutoGenerateHistoryModel::clear()
59{
61 mHistoryInfos.clear();
63}
64
65void TextAutoGenerateHistoryModel::addInfo(const TextAutoGenerateHistoryInfo &msg)
66{
67 beginInsertRows(QModelIndex(), mHistoryInfos.count(), mHistoryInfos.count());
68 mHistoryInfos.append(msg);
70}
71
72bool TextAutoGenerateHistoryModel::removeInfo(const QByteArray &uuid)
73{
74 const auto find = [uuid](const TextAutoGenerateHistoryInfo &i) {
75 return i.referenceUuid() == uuid;
76 };
77 const auto it = std::find_if(mHistoryInfos.begin(), mHistoryInfos.end(), find);
78 if (it != mHistoryInfos.end()) {
79 const int i = std::distance(mHistoryInfos.begin(), it);
80 beginRemoveRows(QModelIndex(), i, i);
81 mHistoryInfos.removeAt(i);
83 return true;
84 }
85 return false;
86 // TODO remove it + remove in model.
87 // TODO
88}
89
90#include "moc_textautogeneratehistorymodel.cpp"
QAction * find(const QObject *recvr, const char *slot, QObject *parent)
void beginInsertRows(const QModelIndex &parent, int first, int last)
void beginRemoveRows(const QModelIndex &parent, int first, int last)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual QModelIndex parent(const QModelIndex &index) const const=0
DisplayRole
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 18 2025 12:00:52 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.