/
pi_coder
/
ByteMachine
Обзор
Документация
Войти
/
pi_coder
/
ByteMachine
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/nodes/area_node.cpp
354 строки
11 KB
pimenov-and
Перенос функциональности из старого проекта, в основном это описания узлов
19 июл 2026, 20:13
19 июл 2026, 20:13
cb3c51b
Код
Авторство
О чём код?
//////////////////////////////////////////////////////////////// // ByteMachine // Декоративный узел для выделения группы узлов //////////////////////////////////////////////////////////////// #include "area_node.h" #include "node_name_manager.h" #include "colors.h" #include "undo/undo_change_object_prop_value.h" #include <QPainter> #include <QDomDocument> #include <QUndoStack> //============================================================== using std::size_t; //============================================================== // Конструктор с параметрами //============================================================== AreaNode::AreaNode(QUndoStack *undoStack, QObject *parent) : BaseNode{undoStack, parent} { name_ = nodeNameManager()->addName("area"); Q_ASSERT(!name_.isEmpty()); // Задание начальных размером const int width = AreaNode::minWidth(); const int height = AreaNode::minHeight(); setUndo(true); BaseNode::setWidth(width); BaseNode::setHeight(height); setUndo(false); } //============================================================== // Чтение из XML //============================================================== void AreaNode::readFromXml(const QDomElement &elem) { Q_ASSERT(!elem.isNull()); const QString name = readNameFromXml(elem); const qint32 left = readLeftFromXml(elem); const qint32 top = readTopFromXml(elem); const QString comment = readCommentFromXml(elem); Q_UNUSED(name) Q_UNUSED(left) Q_UNUSED(top) Q_UNUSED(comment) } //============================================================== // Запись в XML //============================================================== void AreaNode::writeToXml(QDomDocument &doc, QDomElement &elem) const { Q_ASSERT(!doc.isNull()); Q_ASSERT(!elem.isNull()); writeNameToXml(doc, elem); writeLeftToXml(doc, elem); writeTopToXml(doc, elem); writeCommentToXml(doc, elem); } //============================================================== // Рисование узла //============================================================== void AreaNode::draw(QPainter *painter, ColorThemes theme) const { Q_ASSERT(painter != nullptr); Q_ASSERT(!isUnknown(theme)); drawBody(painter, theme); drawResizeMarker(painter, theme); } //============================================================== // Получение копии узла //============================================================== ShPtrBaseNode AreaNode::clone() const { const ShPtrAreaNode cloneNode = ShPtrAreaNode::create( undoStack(), parent()); cloneNode->setUndo(true); cloneNode->setLeft(left()); cloneNode->setTop(top()); cloneNode->setWidth(width()); cloneNode->setHeight(height()); cloneNode->setColor(color()); cloneNode->setText(text()); cloneNode->setTextAlign(textAlign()); cloneNode->setUndo(false); return cloneNode; } //============================================================== // Получение размера данных //============================================================== size_t AreaNode::dataSize() const { return 0; } //============================================================== // Получение байта данных //============================================================== quint8 AreaNode::dataByte(size_t) const { return 0; } //============================================================== // Получение блока данных //============================================================== ByteList AreaNode::dataBlock(size_t, size_t) const { return ByteList{}; } //============================================================== // Функция вызывается при изменении данных //============================================================== void AreaNode::dataChanged() { } //============================================================== // Задание цвета //============================================================== void AreaNode::setColor(AreaNodeColors color) { if (color_ != color) { const AreaNodeColors oldColor = color_; color_ = color; const PropValue value{"color", QVariant::fromValue(color_)}; emit sigChangedProp(value); if (!isUndo_) { Q_ASSERT(undoStack_ != nullptr); const auto undoCmd = new UndoChangeObjectPropValue{this, "color", QVariant::fromValue(color_), QVariant::fromValue(oldColor)}; undoStack_->push(undoCmd); } } } //============================================================== // Сброс цвета //============================================================== void AreaNode::resetColor() { setColor(AreaNodeColors::Mint); } //============================================================== // Задание текста //============================================================== void AreaNode::setText(const QString &text) { if (text_ != text) { const QString oldText = text_; text_ = text; const PropValue value{"text", text_}; emit sigChangedProp(value); if (!isUndo_) { Q_ASSERT(undoStack_ != nullptr); const auto undoCmd = new UndoChangeObjectPropValue{this, "text", QVariant::fromValue(text_), QVariant::fromValue(oldText)}; undoStack_->push(undoCmd); } } } //============================================================== // Сброс текста //============================================================== void AreaNode::resetText() { setText("Area"); } //============================================================== // Задание выравнивания текста //============================================================== void AreaNode::setTextAlign(AreaNodeTextAligns align) { if (textAlign_ != align) { const AreaNodeTextAligns oldTextAlign = textAlign_; textAlign_ = align; const PropValue value{"textAlign", QVariant::fromValue(textAlign_)}; emit sigChangedProp(value); if (!isUndo_) { Q_ASSERT(undoStack_ != nullptr); const auto undoCmd = new UndoChangeObjectPropValue{this, "textAlign", QVariant::fromValue(textAlign_), QVariant::fromValue(oldTextAlign)}; undoStack_->push(undoCmd); } } } //============================================================== // Сброс выравнивания текста //============================================================== void AreaNode::resetTextAlign() { setTextAlign(AreaNodeTextAligns::Top); } //============================================================== // Функция получения имени свойства для графического // интерфейса по его системному имени //============================================================== QString AreaNode::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")}, {"color", tr("Color")}, {"text", tr("Text")}, {"textAlign", tr("Text align")} }; return map.value(systemName, tr("Unknown")); } //============================================================== // Функция перевода //============================================================== void AreaNode::retranslate() { } //============================================================== // Вывод тела //============================================================== void AreaNode::drawBody(QPainter *painter, ColorThemes theme) const { Q_ASSERT(painter != nullptr); Q_ASSERT(!isUnknown(theme)); // Вывод тела const QColor borderColor = isSelected() ? Colors::nodeSelectBorder(theme) : areaNodeColorToQColor(color_); QPen pen{borderColor, 2}; pen.setJoinStyle(Qt::MiterJoin); painter->setPen(pen); QColor backColor = areaNodeColorToQColor(color_); backColor.setAlpha(128); painter->setBrush(backColor); painter->drawRect(rect()); // Вывод текста painter->save(); QFont font = painter->font(); font.setBold(true); painter->setFont(font); painter->setPen(Colors::nodeText(theme)); int flags = Qt::AlignHCenter | Qt::TextWrapAnywhere; flags |= (isTop(textAlign_) ? Qt::AlignTop : Qt::AlignBottom); painter->drawText(rect() - QMargins{gridSize(), (headerHeight() - charHeight()) / 2, gridSize(), (headerHeight() - charHeight()) / 2}, flags, text_); painter->restore(); } //============================================================== // Вывод маркера изменения размера //============================================================== void AreaNode::drawResizeMarker(QPainter *painter, ColorThemes theme) const { Q_ASSERT(painter != nullptr); const QColor color = isSelected() ? Colors::nodeSelectBorder(theme) : areaNodeColorToQColor(color_); constexpr int halfSizeMarker = resizebleMarkerSize_ / 2; painter->fillRect(right() - halfSizeMarker, bottom() - halfSizeMarker, resizebleMarkerSize_, resizebleMarkerSize_, color); } //============================================================== // Чтение цвета из XML //============================================================== AreaNodeColors AreaNode::readColorFromXml(const QDomElement &elem) const { Q_ASSERT(!elem.isNull()); return AreaNodeColors::Mint; } //============================================================== // Запись цвета в XML //============================================================== void AreaNode::writeColorToXml(QDomDocument &doc, QDomElement &elem) const { Q_ASSERT(!doc.isNull()); Q_ASSERT(!elem.isNull()); } //============================================================== // Чтение текста из XML //============================================================== QString AreaNode::readTextFromXml(const QDomElement &elem) const { Q_ASSERT(!elem.isNull()); return QString{}; } //============================================================== // Запись текста в XML //============================================================== void AreaNode::writeTextToXml(QDomDocument &doc, QDomElement &elem) const { Q_ASSERT(!doc.isNull()); Q_ASSERT(!elem.isNull()); } //============================================================== // Чтение выравнивания текста из XML //============================================================== AreaNodeTextAligns AreaNode::readTextAlignFromXml(const QDomElement &elem) const { Q_ASSERT(!elem.isNull()); return AreaNodeTextAligns::Top; } //============================================================== // Запись выравнивания текста в XML //============================================================== void AreaNode::writeTextAlignToXml(QDomDocument &doc, QDomElement &elem) const { Q_ASSERT(!doc.isNull()); Q_ASSERT(!elem.isNull()); }