/
waterstone
/
file_processor
Обзор
Документация
Войти
/
waterstone
/
file_processor
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/table_model.cpp
233 строки
7 KB
Peter Sakhno
Initial commit
19 янв 2026, 11:08
19 янв 2026, 11:08
1ffa16c
Код
Авторство
О чём код?
#include "table_model.h" #include <QFileInfo> namespace { bool IsProgressValid(int progress) { return 0 <= progress and progress <= 100; } } void TableModel::AddTask(uint64_t task_id, const std::wstring& input_file, const std::wstring& output_file) { const int row = static_cast<decltype(row)>(m_data.size()); beginInsertRows({}, row, row); m_data.insert(m_data.end(), { task_id, { QDateTime::currentDateTime(), QString::fromStdWString(input_file), QString::fromStdWString(output_file), true }}); endInsertRows(); } void TableModel::UpdateProgress(uint64_t task_id, int progress) { auto it = m_data.find(task_id); if (it == m_data.end()) return; if (!it->second.running) return; if (!IsProgressValid(progress)) return; if (it->second.progress == progress) return; it->second.progress = progress; const int row = static_cast<decltype(row)>(std::distance(m_data.begin(), it)); const QModelIndex idx = index(row, InformationColumn); emit dataChanged(idx, idx, { Qt::DisplayRole }); } void TableModel::UpdateResult(uint64_t task_id, bool result, XorFileProcessor::ErrorCode error_code, const std::string& error_text) { auto it = m_data.find(task_id); if (it == m_data.end()) return; it->second.running = false; it->second.progress = 0; it->second.result = result; it->second.error_code = error_code; it->second.error_text = QString::fromStdString(error_text); const int row = static_cast<decltype(row)>(std::distance(m_data.begin(), it)); const QModelIndex idx = index(row, InformationColumn); emit dataChanged(idx, idx, { Qt::DisplayRole }); } void TableModel::AddTaskWithResult(uint64_t task_id, const std::wstring& input_file, const std::wstring& output_file, bool result, XorFileProcessor::ErrorCode error_code, const std::string& error_text) { const int row = static_cast<decltype(row)>(m_data.size()); beginInsertRows({}, row, row); m_data.insert(m_data.end(), { task_id, { QDateTime::currentDateTime(), QString::fromStdWString(input_file), QString::fromStdWString(output_file), false, 0, result, error_code, QString::fromStdString(error_text) } }); endInsertRows(); } QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const { if (Qt::Horizontal != orientation) return {}; if (section < TableModel::ColumnBegin or TableModel::ColumnEnd <= section) return {}; if (Qt::DisplayRole == role) { switch (section) { case TableModel::TimestampColumn: return tr("Date"); case TableModel::InputFileColumn: return tr("Input file"); case TableModel::OutputFileColumn: return tr("Output file"); case TableModel::InformationColumn: return tr("Information"); } } return {}; } int TableModel::rowCount(const QModelIndex& parent) const { if (parent.isValid ()) return 0; return static_cast<int>(m_data.size()); } int TableModel::columnCount(const QModelIndex& parent) const { if (parent.isValid ()) return 0; return static_cast<int>(TableModel::ColumnEnd); } QVariant TableModel::data(const QModelIndex& index, int role) const { if (!index.isValid()) return {}; if (Qt::DisplayRole != role) return {}; auto cit = std::next(m_data.cbegin(), index.row()); if (cit == m_data.cend()) return {}; const TaskInfo& task = cit->second; switch (index.column()) { case TimestampColumn: { if (task.timestamp.isNull() or !task.timestamp.isValid()) return tr("<Unknown>"); const QString out = task.timestamp.date().toString( QLocale::system().dateFormat(QLocale::ShortFormat)) % QStringLiteral(" ") % task.timestamp.time().toString( QLocale::system().timeFormat(QLocale::LongFormat)); return out; } case InputFileColumn: { if (task.input_file.isEmpty()) return tr("<Unknown>"); return QFileInfo(task.input_file).fileName(); } case OutputFileColumn: { if (task.output_file.isEmpty()) return tr("<Unknown>"); return QFileInfo(task.output_file).fileName(); } case InformationColumn: { if (task.running) { if (IsProgressValid(task.progress)) return tr("Processed %1% of input file").arg(task.progress); else return tr("Processing input file..."); } else { QString out; if (task.result) out = tr("Processing succeeded"); else out = tr("Processing failed"); switch (task.error_code) { case XorFileProcessor::ErrorCode::Cancelled: out = tr("Processing cancelled"); break; case XorFileProcessor::ErrorCode::MissingInputFile: out += QStringLiteral("\n") + tr("Input file not found"); break; case XorFileProcessor::ErrorCode::InputIsNotFile: out += QStringLiteral("\n") + tr("Input path is not file"); break; case XorFileProcessor::ErrorCode::CantOpenInputFile: out += QStringLiteral("\n") + tr("Failed to open input file"); break; case XorFileProcessor::ErrorCode::CantDeleteInputFile: out += QStringLiteral("\n") + tr("Failed to delet input file"); break; case XorFileProcessor::ErrorCode::CantOpenOutputFile: out += QStringLiteral("\n") + tr("Failed to open output file"); break; case XorFileProcessor::ErrorCode::NotFullyProcessed: out += QStringLiteral("\n") + tr("Input file not fully processed"); break; case XorFileProcessor::ErrorCode::FileSystemError: out += QStringLiteral("\n") + tr("File system error"); break; case XorFileProcessor::ErrorCode::SystemError: out += QStringLiteral("\n") + tr("System error"); break; case XorFileProcessor::ErrorCode::GeneralError: out += QStringLiteral("\n") + tr("General error"); break; case XorFileProcessor::ErrorCode::CantStartProcess: out += QStringLiteral("\n") + tr("Can't launch process task"); break; } if (!task.error_text.isEmpty()) out += QStringLiteral("\n") + task.error_text; return out; } } break; } return {}; }