/
pi_coder
/
ByteMachine
Обзор
Документация
Войти
/
pi_coder
/
ByteMachine
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/nodes/output_file_node.cpp
315 строк
10 KB
pimenov-and
Перенос функциональности из старого проекта, в основном это описания узлов
19 июл 2026, 20:13
19 июл 2026, 20:13
cb3c51b
Код
Авторство
О чём код?
//////////////////////////////////////////////////////////////// // ByteMachine // Узел для сохранения данных в файл //////////////////////////////////////////////////////////////// #include "output_file_node.h" #include "node_name_manager.h" #include "undo/undo_change_object_prop_value.h" #include "colors.h" #include "qt_helper.h" #include <QPainter> #include <QDomDocument> #include <QFile> #include <QUndoStack> //============================================================== using std::size_t; //============================================================== // Конструктор с параметрами //============================================================== OutputFileNode::OutputFileNode(QUndoStack *undoStack, QObject *parent) : BaseNode{undoStack, parent} { name_ = nodeNameManager()->addName("outFile"); Q_ASSERT(!name_.isEmpty()); createInputPin(); updateStateInfo(); } //============================================================== // Чтение из XML //============================================================== void OutputFileNode::readFromXml(const QDomElement &elem) { Q_ASSERT(!elem.isNull()); } //============================================================== // Запись в XML //============================================================== void OutputFileNode::writeToXml(QDomDocument &doc, QDomElement &elem) const { Q_ASSERT(!doc.isNull()); Q_ASSERT(!elem.isNull()); } //============================================================== // Функция перерисовки //============================================================== void OutputFileNode::draw(QPainter *painter, ColorThemes theme) const { Q_ASSERT(painter != nullptr); Q_ASSERT(!isUnknown(theme)); drawHighlight(painter, theme); drawSimpleBody(painter, theme); drawInputPins(painter, theme); drawStateArea(painter, theme); drawComments(painter, theme); } //============================================================== // Получение копии узла //============================================================== ShPtrBaseNode OutputFileNode::clone() const { const auto cloneNode = ShPtrOutputFileNode::create( undoStack(), parent()); cloneNode->setUndo(true); cloneNode->setName(name()); cloneNode->setTopLeft(topLeft()); cloneNode->setFilePath(filePath()); cloneNode->setBypass(isBypass()); cloneNode->setComment(comment()); cloneNode->setUndo(false); return cloneNode; } //============================================================== // Получение текста подсказки //============================================================== QString OutputFileNode::tooltipText() const { QString text{}; text += QString{"%1: \"%2\"\n"}.arg(tr("Name"), name()); text += QString{"%1: \"%2\"\n"}.arg(tr("File path"), filePath()); text += QString{"%1: %2\n"}.arg(tr("Bypass"), boolToStrTr(isBypass_)); text += QString{"%1: \"%2\""}.arg(tr("Comment"), comment()); return text; } //============================================================== // Получение размера данных //============================================================== size_t OutputFileNode::dataSize() const { if (stateInfo_.isError()) { return 0; } return 0; } //============================================================== // Получение байта данных //============================================================== quint8 OutputFileNode::dataByte(size_t) const { return 0; } //============================================================== // Получение блока данных //============================================================== ByteList OutputFileNode::dataBlock(size_t, size_t) const { return ByteList{}; } //============================================================== // Функция вызывается при изменении данных //============================================================== void OutputFileNode::dataChanged() { } //============================================================== // Получение входных пинов //============================================================== QVector<ShPtrInputPin> OutputFileNode::inputPins() { return QVector<ShPtrInputPin>{inputPin_}; } //============================================================== // Получение входных пинов (константный вариант) //============================================================== QVector<ShPtrConstInputPin> OutputFileNode::inputPins() const { return QVector<ShPtrConstInputPin>{inputPin_}; } //============================================================== // Задание пути к файлу //============================================================== void OutputFileNode::setFilePath(const QString &path) { if (filePath_ != path) { const QString oldFilePath = path; filePath_ = path; const PropValue value{"filePath", filePath_}; emit sigChangedProp(value); if (!isUndo_) { Q_ASSERT(undoStack_ != nullptr); const auto undoCmd = new UndoChangeObjectPropValue{this, "filePath", filePath_, oldFilePath}; undoStack_->push(undoCmd); } startHighlightTimer(); } } //============================================================== // Сброс пути к файлу //============================================================== void OutputFileNode::resetFilePath() { setFilePath(QString{}); } //============================================================== // Задание признака пропуска //============================================================== void OutputFileNode::setBypass(bool bypass) { if (isBypass_ != bypass) { const bool oldBypass = isBypass_; isBypass_ = bypass; const PropValue value{"bypass", isBypass_}; emit sigChangedProp(value); if (!isUndo_) { Q_ASSERT(undoStack_ != nullptr); const auto undoCmd = new UndoChangeObjectPropValue{this, "bypass", isBypass_, oldBypass}; undoStack_->push(undoCmd); } startHighlightTimer(); } } //============================================================== // Сброс признака пропуска //============================================================== void OutputFileNode::resetBypass() { setBypass(false); } //============================================================== // Виртуальная функция получения имени свойства из графического // интерфейса по его системному имени //============================================================== QString OutputFileNode::getUiPropertyName(const QString &systemName) { const QMap<QString, QString> map { {"name", tr("Name")}, {"left", tr("Left")}, {"top", tr("Top")}, {"width", tr("Width")}, {"height", tr("Height")}, {"topLeft", tr("Top and left")}, {"size", tr("Size")}, {"comment", tr("Comment")}, {"filePath", tr("File path")}, {"bypass", tr("Bypass")} }; return map.value(systemName, tr("Unknown")); } //============================================================== // Функция перевода //============================================================== void OutputFileNode::retranslate() { updateStateInfo(); } //============================================================== // Создание входного пина //============================================================== void OutputFileNode::createInputPin() { inputPin_ = ShPtrInputPin::create(this, 0); } //============================================================== // Получение признака подключения входного пина //============================================================== bool OutputFileNode::isConnectedInputPin() const { return inputPin_->isConnected(); } //============================================================== // Вывод комментариев //============================================================== void OutputFileNode::drawComments(QPainter *painter, ColorThemes theme) const { Q_ASSERT(painter != nullptr); Q_ASSERT(!isUnknown(theme)); if (isCommentsVisible()) { QString comments{}; comments += QString{" <<< %1: \"%2\"\n"}.arg(tr("Name"), name()); comments += QString{" %1: \"%2\"\n"}.arg(tr("File path"), filePath()); comments += QString{" %1: %2\n"}.arg(tr("Bypass"), boolToStrTr(isBypass_)); comments += QString{" %1: \"%2\"\n"}.arg(tr("Comment"), comment()); #ifdef QT_DEBUG comments += " -\n"; comments += QString{" %1: %2\n"}.arg("Id").arg(id()); #endif // QT_DEBUG const int commentsLeft = right(); const int commentsTop = top() + (headerHeight() - charHeight()) / 2; const int commentsFlags = Qt::AlignLeft | Qt::AlignTop | Qt::TextDontClip; const QRect commentsRect{commentsLeft, commentsTop, 0, 0}; painter->setPen(Colors::nodeText(theme)); painter->drawText(commentsRect, commentsFlags, comments); } } //============================================================== // Обновление состояния узла //============================================================== void OutputFileNode::updateStateInfo() { const NodeStateInfo oldStateInfo = stateInfo_; if (!isConnectedInputPin()) { const QString msg = tr("Input pin is not connected"); stateInfo_ = NodeStateInfo{NodeStates::Error, msg}; } else if (isErrorInParent(this)) { const QString msg = tr("Parent error"); stateInfo_ = NodeStateInfo{NodeStates::Error, msg}; } else { stateInfo_ = NodeStateInfo{NodeStates::Success, QString{}}; } if (stateInfo_ != oldStateInfo) { emit sigChangedState(stateInfo_); } }