/
ka_pie
/
PeakOff
Обзор
Документация
Войти
/
ka_pie
/
PeakOff
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/historymodel.cpp
163 строки
4 KB
Viktoria
final version of PeakOff
14 июл 2026, 11:13
14 июл 2026, 11:13
a838933
Код
Авторство
О чём код?
#include "historymodel.h" #include <QDebug> HistoryModel::HistoryModel(QObject *parent) : QAbstractListModel(parent) { } int HistoryModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); return m_history.size(); } QVariant HistoryModel::data(const QModelIndex &index, int role) const { if (!index.isValid() || index.row() < 0 || index.row() >= m_history.size()) return QVariant(); const HistoryEntry &entry = m_history[index.row()]; switch (role) { case ImageRole: { QImage scaled = entry.image.scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation); return QVariant::fromValue(scaled); } case OperationNameRole: return entry.operationName; case TimestampRole: return QDateTime::fromMSecsSinceEpoch(entry.timestamp).toString("hh:mm:ss"); default: return QVariant(); } } QHash<int, QByteArray> HistoryModel::roleNames() const { QHash<int, QByteArray> roles; roles[ImageRole] = "image"; roles[OperationNameRole] = "operationName"; roles[TimestampRole] = "timestamp"; return roles; } void HistoryModel::push(const QImage &image, const QString &operationName) { if (m_currentIndex >= 0 && m_currentIndex < m_history.size() - 1) { beginRemoveRows(QModelIndex(), m_currentIndex + 1, m_history.size() - 1); m_history.resize(m_currentIndex + 1); endRemoveRows(); } beginInsertRows(QModelIndex(), m_history.size(), m_history.size()); m_history.append(HistoryEntry(image, operationName)); m_currentIndex = m_history.size() - 1; endInsertRows(); emit countChanged(); emit historyChanged(); } bool HistoryModel::undo() { if (!canUndo()) return false; int rowToRemove = m_history.size() - 1; beginRemoveRows(QModelIndex(), rowToRemove, rowToRemove); m_history.removeLast(); m_currentIndex--; endRemoveRows(); qDebug() << "[HistoryModel] undo() - новый currentIndex =" << m_currentIndex; emit historyChanged(); emit countChanged(); return true; } bool HistoryModel::redo() { if (!canRedo()) return false; m_currentIndex++; emit historyChanged(); emit dataChanged(index(0), index(m_history.size() - 1)); return true; } bool HistoryModel::canUndo() const { return m_currentIndex > 0; } bool HistoryModel::canRedo() const { return m_currentIndex >= 0 && m_currentIndex < m_history.size() - 1; } void HistoryModel::clear() { beginResetModel(); m_history.clear(); m_currentIndex = -1; endResetModel(); emit countChanged(); emit historyChanged(); } void HistoryModel::jumpTo(int index) { if (index < 0 || index >= m_history.size()) return; if (index < m_history.size() - 1) { beginRemoveRows(QModelIndex(), index + 1, m_history.size() - 1); while (m_history.size() > index + 1) { m_history.removeLast(); } endRemoveRows(); } m_currentIndex = index; emit countChanged(); emit historyChanged(); } QImage HistoryModel::getCurrentImage() const { if (m_currentIndex >= 0 && m_currentIndex < m_history.size()) return m_history[m_currentIndex].image; return QImage(); } QImage HistoryModel::getFullImageAt(int index) const { if (index >= 0 && index < m_history.size()) return m_history[index].image; return QImage(); } void HistoryModel::addStep(const QVariantMap &step) { beginInsertRows(QModelIndex(), rowCount(), rowCount()); m_steps.append(step); endInsertRows(); } QVariantMap HistoryModel::get(int row) const { QVariantMap map; if (row < 0 || row >= m_history.size()) { return map; } const HistoryEntry &entry = m_history[row]; map["operationName"] = entry.operationName; map["parameters"] = QVariantMap(); return map; }