File indexing completed on 2024-05-19 15:40:35

0001 // SPDX-FileCopyrightText: 2024 Carl Schwan <carlschwan@kde.org>
0002 // SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0003 
0004 #include "consolelog.h"
0005 
0006 #include <QFileInfo>
0007 
0008 ConsoleLog::ConsoleLog(QObject *parent)
0009     : QAbstractListModel(parent)
0010 {
0011 }
0012 
0013 QVariant ConsoleLog::data(const QModelIndex &index, int role) const
0014 {
0015     Q_ASSERT(checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid));
0016 
0017     const auto &logEntry = m_logEntries[index.row()];
0018     switch (role) {
0019     case Qt::DisplayRole:
0020     case OutputRole:
0021         return logEntry.output;
0022     case ImagePathRole:
0023         return logEntry.imagePath;
0024     case ImageNameRole:
0025         return QFileInfo(logEntry.imagePath).fileName();
0026     default:
0027         return {};
0028     }
0029 }
0030 
0031 int ConsoleLog::rowCount(const QModelIndex &parent) const
0032 {
0033     return parent.isValid() ? 0 : m_logEntries.size();
0034 }
0035 
0036 QHash<int, QByteArray> ConsoleLog::roleNames() const
0037 {
0038     return {
0039         {OutputRole, "output"},
0040         {ImagePathRole, "imagePath"},
0041         {ImageNameRole, "imageName"},
0042     };
0043 }
0044 
0045 void ConsoleLog::addConsoleEntry(const QString &output, const QString &imagePath)
0046 {
0047     m_logEntries.emplace_back(output, imagePath);
0048 }