mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-08-07 05:44:14 +02:00
Merge branch 'master' into qt6_cmake_joshua
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
#include "../../QPropertyUndoCommand/qpropertyundocommand.h"
|
||||
#include "../elementscene.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QRegularExpression>
|
||||
|
||||
/**
|
||||
@@ -39,7 +40,8 @@ CustomElementGraphicPart::CustomElementGraphicPart(QETElementEditor *editor,
|
||||
_lineweight(NormalWeight),
|
||||
_filling(NoneFilling),
|
||||
_color(BlackColor),
|
||||
_antialiased(false)
|
||||
_antialiased(false),
|
||||
m_first_move (false)
|
||||
{
|
||||
setFlags(QGraphicsItem::ItemIsSelectable
|
||||
| QGraphicsItem::ItemIsMovable
|
||||
@@ -1325,26 +1327,24 @@ void CustomElementGraphicPart::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
||||
|
||||
void CustomElementGraphicPart::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
//m_first_move is used to avoid an unwanted behavior
|
||||
//when the properties dock widget is displayed :
|
||||
//1 there is no selection
|
||||
//2 the dock widget width is set to minimum
|
||||
//3 select a part, the dock widget gain new widgets used to edit
|
||||
//the current selected part and the width of the dock grow
|
||||
//so the width of the QGraphicsView is reduced and cause a mouse move event.
|
||||
//When this case occur the part is moved but they should not. This bool fix it.
|
||||
if (Q_UNLIKELY(m_first_move)) {
|
||||
if (m_first_move) {
|
||||
// Suppress spurious move events fired when the properties dock
|
||||
// widget expands on first selection of a new item type, causing
|
||||
// the QGraphicsView to shrink and re-map coordinates. Screen
|
||||
// coordinates are stable across viewport changes; scene coords
|
||||
// are not — so use screenPos() for the threshold check.
|
||||
const QPointF d = event->screenPos() - event->buttonDownScreenPos(Qt::LeftButton);
|
||||
if (d.manhattanLength() < QApplication::startDragDistance())
|
||||
return;
|
||||
m_first_move = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if((event->buttons() & Qt::LeftButton) && (flags() & QGraphicsItem::ItemIsMovable))
|
||||
{
|
||||
if ((event->buttons() & Qt::LeftButton) && (flags() & QGraphicsItem::ItemIsMovable)) {
|
||||
QPointF pos = event->scenePos() + (m_origin_pos - event->buttonDownScenePos(Qt::LeftButton));
|
||||
event->modifiers() == Qt::ControlModifier ? setPos(pos) : setPos(elementScene()->snapToGrid(pos));
|
||||
}
|
||||
else
|
||||
} else {
|
||||
QGraphicsObject::mouseMoveEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
void CustomElementGraphicPart::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
#include "../../QPropertyUndoCommand/qpropertyundocommand.h"
|
||||
#include "../../qetapp.h"
|
||||
#include "../elementscene.h"
|
||||
#include "../../utils/qetutils.h"
|
||||
#include <QApplication>
|
||||
|
||||
#include <QColor>
|
||||
#include <QFont>
|
||||
@@ -141,7 +143,7 @@ const QDomElement PartDynamicTextField::toXml(QDomDocument &dom_doc) const
|
||||
root_element.setAttribute("y", QString::number(y));
|
||||
root_element.setAttribute("z", QString::number(zValue()));
|
||||
root_element.setAttribute("rotation", QString::number(QET::correctAngle(rot)));
|
||||
root_element.setAttribute("font", font().toString());
|
||||
root_element.setAttribute("font", QETUtils::fontToString(font()));
|
||||
root_element.setAttribute("uuid", m_uuid.toString());
|
||||
root_element.setAttribute("frame", m_frame? "true" : "false");
|
||||
root_element.setAttribute("text_width", QString::number(m_text_width));
|
||||
@@ -213,7 +215,7 @@ void PartDynamicTextField::fromXml(const QDomElement &dom_elmt) {
|
||||
|
||||
if (dom_elmt.hasAttribute("font")) {
|
||||
QFont font_;
|
||||
font_.fromString(dom_elmt.attribute("font"));
|
||||
QETUtils::fontFromString(font_, dom_elmt.attribute("font"));
|
||||
setFont(font_);
|
||||
}
|
||||
else if (dom_elmt.hasAttribute("font_size")) {
|
||||
@@ -495,12 +497,16 @@ bool PartDynamicTextField::keepVisualRotation() const {
|
||||
@param event
|
||||
*/
|
||||
void PartDynamicTextField::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
|
||||
if((event -> buttons() & Qt::LeftButton) && (flags() & QGraphicsItem::ItemIsMovable)) {
|
||||
QPointF pos = event -> scenePos() + (m_origin_pos - event -> buttonDownScenePos(Qt::LeftButton));
|
||||
event -> modifiers() == Qt::ControlModifier ? setPos(pos) : setPos(elementScene() -> snapToGrid(pos));
|
||||
}
|
||||
else
|
||||
if ((event->buttons() & Qt::LeftButton) && (flags() & QGraphicsItem::ItemIsMovable)) {
|
||||
// Suppress spurious moves from the properties dock resizing the viewport.
|
||||
const QPointF d = event->screenPos() - event->buttonDownScreenPos(Qt::LeftButton);
|
||||
if (d.manhattanLength() < QApplication::startDragDistance())
|
||||
return;
|
||||
QPointF pos = event->scenePos() + (m_origin_pos - event->buttonDownScenePos(Qt::LeftButton));
|
||||
event->modifiers() == Qt::ControlModifier ? setPos(pos) : setPos(elementScene()->snapToGrid(pos));
|
||||
} else {
|
||||
QGraphicsObject::mouseMoveEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,696 @@
|
||||
/*
|
||||
Copyright 2006-2026 The QElectroTech Team
|
||||
This file is part of QElectroTech.
|
||||
|
||||
QElectroTech is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
QElectroTech is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "partplctable.h"
|
||||
|
||||
#include "../../QPropertyUndoCommand/qpropertyundocommand.h"
|
||||
#include "../../QetGraphicsItemModeler/qetgraphicshandleritem.h"
|
||||
#include "../../QetGraphicsItemModeler/qetgraphicshandlerutility.h"
|
||||
#include "../../properties/elementdata.h"
|
||||
#include "../elementscene.h"
|
||||
#include "../editorcommands.h"
|
||||
#include "../ui/qetelementeditor.h"
|
||||
|
||||
#include <QPen>
|
||||
#include <algorithm>
|
||||
|
||||
/**
|
||||
@brief PartPlcTable::PartPlcTable
|
||||
Constructor
|
||||
@param editor the QETElementEditor of this item
|
||||
@param parent parent item
|
||||
*/
|
||||
PartPlcTable::PartPlcTable(QETElementEditor *editor, QGraphicsItem *parent) :
|
||||
CustomElementGraphicPart(editor, parent)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartPlcTable::~PartPlcTable
|
||||
*/
|
||||
PartPlcTable::~PartPlcTable()
|
||||
{
|
||||
removeHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartPlcTable::calculateTableSize
|
||||
Calculate the table size from PLC data in the element scene.
|
||||
@return the calculated size in item coordinates
|
||||
*/
|
||||
QSizeF PartPlcTable::calculateTableSize() const
|
||||
{
|
||||
if (!elementEditor() || !elementScene())
|
||||
return QSizeF(100, 50);
|
||||
|
||||
ElementData ed = elementScene()->elementData();
|
||||
if (ed.m_master_type != ElementData::PLC)
|
||||
return QSizeF(100, 50);
|
||||
|
||||
const auto &plc_data = ed.plcMasterData();
|
||||
if (plc_data.ios.isEmpty())
|
||||
return QSizeF(100, 50);
|
||||
|
||||
const int COL_COUNT = 5;
|
||||
|
||||
// Build list of visible columns
|
||||
QList<int> visible_cols;
|
||||
if (!plc_data.columnOrder.isEmpty()) {
|
||||
for (int logical : plc_data.columnOrder) {
|
||||
if (logical >= 0 && logical < COL_COUNT
|
||||
&& plc_data.colVisible.value(logical, true)
|
||||
&& !visible_cols.contains(logical))
|
||||
visible_cols.append(logical);
|
||||
}
|
||||
for (int i = 0; i < COL_COUNT; ++i) {
|
||||
if (plc_data.colVisible.value(i, true) && !visible_cols.contains(i))
|
||||
visible_cols.append(i);
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < COL_COUNT; ++i) {
|
||||
if (plc_data.colVisible.value(i, true))
|
||||
visible_cols.append(i);
|
||||
}
|
||||
}
|
||||
if (visible_cols.isEmpty())
|
||||
visible_cols << 0 << 1 << 2; // fallback: Type, Address, Function
|
||||
|
||||
// Default column widths
|
||||
QMap<int, qreal> col_widths;
|
||||
for (int col : visible_cols) {
|
||||
if (plc_data.colWidths.contains(col) && plc_data.colWidths[col] > 0)
|
||||
col_widths[col] = plc_data.colWidths[col];
|
||||
else {
|
||||
switch (col) {
|
||||
case 0: col_widths[col] = 35; break; // Type
|
||||
case 1: col_widths[col] = 25; break; // Address
|
||||
case 2: col_widths[col] = 50; break; // Function
|
||||
case 3: col_widths[col] = 40; break; // Comment
|
||||
case 5: col_widths[col] = 30; break; // CrossRef
|
||||
default: col_widths[col] = 30; break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
qreal row_h = plc_data.rowHeight > 0 ? plc_data.rowHeight : 8.0;
|
||||
qreal header_h = plc_data.showHeaders ? (row_h + 2.0) : 0;
|
||||
|
||||
qreal total_width = 0;
|
||||
for (int col : visible_cols)
|
||||
total_width += col_widths[col];
|
||||
|
||||
int total_ios = plc_data.ios.size();
|
||||
|
||||
// Collect active break positions (sorted)
|
||||
QList<int> breaks;
|
||||
for (int bp : plc_data.breakPositions) {
|
||||
if (bp > 0 && bp < total_ios && !breaks.contains(bp))
|
||||
breaks.append(bp);
|
||||
}
|
||||
std::sort(breaks.begin(), breaks.end());
|
||||
|
||||
// Build block boundaries
|
||||
QList<int> block_starts;
|
||||
block_starts.append(0);
|
||||
for (int bp : breaks)
|
||||
block_starts.append(bp);
|
||||
|
||||
qreal total_height;
|
||||
int block_count = block_starts.size();
|
||||
|
||||
if (block_count > 1) {
|
||||
// Find the tallest block (most rows)
|
||||
int max_rows = 0;
|
||||
for (int b = 0; b < block_count; ++b) {
|
||||
int start = block_starts.at(b);
|
||||
int end = (b + 1 < block_starts.size()) ? block_starts.at(b + 1) : total_ios;
|
||||
max_rows = qMax(max_rows, end - start);
|
||||
}
|
||||
total_height = header_h + max_rows * row_h;
|
||||
total_width = total_width * block_count + (block_count - 1) * 3;
|
||||
} else {
|
||||
total_height = header_h + total_ios * row_h;
|
||||
}
|
||||
|
||||
return QSizeF(total_width, total_height);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartPlcTable::paint
|
||||
Draw this PLC table
|
||||
@param painter
|
||||
@param options
|
||||
@param widget
|
||||
*/
|
||||
void PartPlcTable::paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget)
|
||||
{
|
||||
Q_UNUSED(widget);
|
||||
Q_UNUSED(options);
|
||||
|
||||
// Auto-size from PLC data
|
||||
QSizeF table_size = calculateTableSize();
|
||||
if (m_rect.size() != table_size) {
|
||||
prepareGeometryChange();
|
||||
QPointF top_left = m_rect.topLeft();
|
||||
m_rect = QRectF(top_left, table_size);
|
||||
}
|
||||
|
||||
applyStylesToQPainter(*painter);
|
||||
QPen t = painter->pen();
|
||||
t.setCosmetic(options && options->levelOfDetailFromTransform(painter->worldTransform()) < 1.0);
|
||||
if (isSelected())
|
||||
t.setColor(Qt::red);
|
||||
t.setJoinStyle(Qt::MiterJoin);
|
||||
if (!m_rect.width() || !m_rect.height())
|
||||
t.setWidth(0);
|
||||
painter->setPen(t);
|
||||
|
||||
// Get PLC data
|
||||
ElementData ed = (elementEditor() && elementScene()) ? elementScene()->elementData() : ElementData();
|
||||
if (ed.m_master_type != ElementData::PLC) {
|
||||
// Draw placeholder
|
||||
painter->setBrush(QColor(255, 255, 200));
|
||||
painter->drawRect(m_rect);
|
||||
painter->drawText(m_rect, Qt::AlignCenter, QObject::tr("Table PLC"));
|
||||
return;
|
||||
}
|
||||
|
||||
const auto &plc_data = ed.plcMasterData();
|
||||
if (plc_data.ios.isEmpty()) {
|
||||
painter->setBrush(QColor(255, 255, 200));
|
||||
painter->drawRect(m_rect);
|
||||
painter->drawText(m_rect, Qt::AlignCenter, QObject::tr("Table PLC (vide)"));
|
||||
return;
|
||||
}
|
||||
|
||||
const int COL_TYPE = 0;
|
||||
const int COL_ADDRESS = 1;
|
||||
const int COL_FUNCTION = 2;
|
||||
const int COL_COMMENT = 3;
|
||||
const int COL_CROSSREF = 4;
|
||||
const int COL_COUNT = 5;
|
||||
|
||||
QMap<int, QString> headers;
|
||||
headers[COL_TYPE] = QObject::tr("Type");
|
||||
headers[COL_ADDRESS] = QObject::tr("Adresse");
|
||||
headers[COL_FUNCTION] = QObject::tr("Fonction");
|
||||
headers[COL_COMMENT] = QObject::tr("Commentaire");
|
||||
headers[COL_CROSSREF] = QObject::tr("Réf. croisée");
|
||||
|
||||
// Override with custom column names if set
|
||||
if (!plc_data.columnNames.isEmpty()) {
|
||||
QList<int> all_cols;
|
||||
all_cols << COL_TYPE << COL_ADDRESS << COL_FUNCTION << COL_COMMENT << COL_CROSSREF;
|
||||
for (int i = 0; i < qMin(plc_data.columnNames.size(), all_cols.size()); ++i) {
|
||||
if (!plc_data.columnNames.at(i).isEmpty())
|
||||
headers[all_cols.at(i)] = plc_data.columnNames.at(i);
|
||||
}
|
||||
}
|
||||
|
||||
QList<int> visible_cols;
|
||||
if (!plc_data.columnOrder.isEmpty()) {
|
||||
for (int logical : plc_data.columnOrder) {
|
||||
if (logical >= 0 && logical < COL_COUNT
|
||||
&& plc_data.colVisible.value(logical, true)
|
||||
&& !visible_cols.contains(logical))
|
||||
visible_cols.append(logical);
|
||||
}
|
||||
for (int i = 0; i < COL_COUNT; ++i) {
|
||||
if (plc_data.colVisible.value(i, true) && !visible_cols.contains(i))
|
||||
visible_cols.append(i);
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < COL_COUNT; ++i) {
|
||||
if (plc_data.colVisible.value(i, true))
|
||||
visible_cols.append(i);
|
||||
}
|
||||
}
|
||||
if (visible_cols.isEmpty())
|
||||
return;
|
||||
|
||||
QMap<int, qreal> col_widths;
|
||||
for (int col : visible_cols) {
|
||||
if (plc_data.colWidths.contains(col) && plc_data.colWidths[col] > 0)
|
||||
col_widths[col] = plc_data.colWidths[col];
|
||||
else {
|
||||
switch (col) {
|
||||
case COL_TYPE: col_widths[col] = 35; break;
|
||||
case COL_ADDRESS: col_widths[col] = 25; break;
|
||||
case COL_FUNCTION: col_widths[col] = 50; break;
|
||||
case COL_COMMENT: col_widths[col] = 40; break;
|
||||
case COL_CROSSREF: col_widths[col] = 30; break;
|
||||
default: col_widths[col] = 30; break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
qreal row_h = plc_data.rowHeight > 0 ? plc_data.rowHeight : 8.0;
|
||||
qreal header_h = plc_data.showHeaders ? (row_h + 2.0) : 0;
|
||||
|
||||
int total_ios = plc_data.ios.size();
|
||||
|
||||
// Collect active break positions (sorted)
|
||||
QList<int> breaks;
|
||||
for (int bp : plc_data.breakPositions) {
|
||||
if (bp > 0 && bp < total_ios && !breaks.contains(bp))
|
||||
breaks.append(bp);
|
||||
}
|
||||
std::sort(breaks.begin(), breaks.end());
|
||||
|
||||
// Build block boundaries: start, break1, break2, ..., end
|
||||
QList<int> block_starts;
|
||||
block_starts.append(0);
|
||||
for (int bp : breaks)
|
||||
block_starts.append(bp);
|
||||
int block_count = block_starts.size();
|
||||
|
||||
// Draw background
|
||||
painter->save();
|
||||
painter->setPen(Qt::NoPen);
|
||||
painter->setBrush(Qt::white);
|
||||
painter->drawRect(m_rect);
|
||||
painter->restore();
|
||||
|
||||
// Draw outer border
|
||||
QPen border_pen(Qt::black, 0.5);
|
||||
painter->setPen(border_pen);
|
||||
painter->setBrush(Qt::NoBrush);
|
||||
painter->drawRect(m_rect);
|
||||
|
||||
// Draw each block
|
||||
for (int block = 0; block < block_count; ++block) {
|
||||
qreal block_w = 0;
|
||||
for (int col : visible_cols)
|
||||
block_w += col_widths[col];
|
||||
|
||||
qreal block_x = m_rect.x() + block * (block_w + 3);
|
||||
qreal cx = block_x;
|
||||
|
||||
// Draw column headers
|
||||
QFont header_font = plc_data.headerFont.family().isEmpty()
|
||||
? painter->font() : plc_data.headerFont;
|
||||
header_font.setBold(true);
|
||||
painter->setFont(header_font);
|
||||
|
||||
for (int col : visible_cols) {
|
||||
QRectF header_rect(cx, m_rect.y(), col_widths[col], header_h);
|
||||
painter->fillRect(header_rect, QColor(220, 220, 220));
|
||||
painter->setPen(border_pen);
|
||||
painter->drawRect(header_rect);
|
||||
QString header_text = headers.value(col, QString());
|
||||
painter->drawText(header_rect, Qt::AlignCenter, header_text);
|
||||
cx += col_widths[col];
|
||||
}
|
||||
|
||||
// Draw IO rows
|
||||
QFont cell_font = plc_data.cellFont.family().isEmpty()
|
||||
? painter->font() : plc_data.cellFont;
|
||||
painter->setFont(cell_font);
|
||||
int start_idx = block_starts.at(block);
|
||||
int end_idx = (block + 1 < block_starts.size())
|
||||
? block_starts.at(block + 1) : total_ios;
|
||||
|
||||
for (int row = 0; row < (end_idx - start_idx); ++row) {
|
||||
int io_idx = start_idx + row;
|
||||
const ElementData::PlcIO &io = plc_data.ios.at(io_idx);
|
||||
|
||||
qreal ry = m_rect.y() + header_h + row * row_h;
|
||||
cx = block_x;
|
||||
|
||||
for (int col : visible_cols) {
|
||||
QRectF cell_rect(cx, ry, col_widths[col], row_h);
|
||||
painter->setPen(border_pen);
|
||||
painter->drawRect(cell_rect);
|
||||
|
||||
QString cell_text;
|
||||
switch (col) {
|
||||
case COL_TYPE: cell_text = ElementData::translatedPlcIOType(io.type); break;
|
||||
case COL_ADDRESS: cell_text = io.address; break;
|
||||
case COL_FUNCTION: cell_text = io.functionText; break;
|
||||
case COL_COMMENT: cell_text = io.comment; break;
|
||||
case COL_CROSSREF: cell_text = io.crossRef; break;
|
||||
}
|
||||
|
||||
QRectF text_rect = cell_rect.adjusted(1, 0, -1, 0);
|
||||
painter->drawText(text_rect, Qt::AlignLeft | Qt::AlignVCenter, cell_text);
|
||||
|
||||
cx += col_widths[col];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (m_hovered)
|
||||
drawShadowShape(painter);
|
||||
|
||||
if (isSelected())
|
||||
drawCross(m_rect.center(), painter);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartPlcTable::toXml
|
||||
Export this PLC table part in xml
|
||||
@param xml_document : Xml document to use for create the xml element.
|
||||
@return an xml element that describe this part
|
||||
*/
|
||||
const QDomElement PartPlcTable::toXml(QDomDocument &xml_document) const
|
||||
{
|
||||
QDomElement xml_element = xml_document.createElement("plc_table");
|
||||
qreal x = qRound(m_rect.x() * 100.0) / 100.0;
|
||||
qreal y = qRound(m_rect.y() * 100.0) / 100.0;
|
||||
|
||||
xml_element.setAttribute("x", QString::number(x));
|
||||
xml_element.setAttribute("y", QString::number(y));
|
||||
|
||||
stylesToXml(xml_element);
|
||||
return xml_element;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartPlcTable::fromXml
|
||||
Import the properties of this PLC table part from a xml element.
|
||||
@param qde : Xml document to use.
|
||||
*/
|
||||
void PartPlcTable::fromXml(const QDomElement &qde)
|
||||
{
|
||||
stylesFromXml(qde);
|
||||
qreal x = qde.attribute("x", "0").toDouble();
|
||||
qreal y = qde.attribute("y", "0").toDouble();
|
||||
setPos(mapFromScene(x, y));
|
||||
|
||||
// Auto-size from PLC data
|
||||
QSizeF table_size = calculateTableSize();
|
||||
prepareGeometryChange();
|
||||
m_rect = QRectF(QPointF(0, 0), table_size);
|
||||
update();
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartPlcTable::rect
|
||||
@return : Returns the item's rectangle.
|
||||
*/
|
||||
QRectF PartPlcTable::rect() const
|
||||
{
|
||||
return m_rect;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartPlcTable::setRect
|
||||
Sets the item's rectangle to be the given rectangle.
|
||||
@param rect
|
||||
*/
|
||||
void PartPlcTable::setRect(const QRectF &rect)
|
||||
{
|
||||
if (rect == m_rect) return;
|
||||
prepareGeometryChange();
|
||||
m_rect = rect;
|
||||
adjustHandlerPos();
|
||||
update();
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartPlcTable::sceneGeometricRect
|
||||
@return the minimum, margin-less rectangle this part can fit into, in scene
|
||||
coordinates.
|
||||
*/
|
||||
QRectF PartPlcTable::sceneGeometricRect() const
|
||||
{
|
||||
return(mapToScene(m_rect).boundingRect());
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartPlcTable::shape
|
||||
@return the shape of this item
|
||||
*/
|
||||
QPainterPath PartPlcTable::shape() const
|
||||
{
|
||||
QPainterPath fill;
|
||||
fill.addRect(m_rect);
|
||||
|
||||
QPainterPath stroke;
|
||||
stroke.addRect(m_rect);
|
||||
|
||||
QPainterPathStroker pps;
|
||||
pps.setWidth(m_hovered? penWeight()+SHADOWS_HEIGHT : penWeight());
|
||||
stroke = pps.createStroke(stroke);
|
||||
|
||||
return fill.united(stroke);
|
||||
}
|
||||
|
||||
QPainterPath PartPlcTable::shadowShape() const
|
||||
{
|
||||
QPainterPath shape;
|
||||
shape.addRect(m_rect);
|
||||
|
||||
QPainterPathStroker pps;
|
||||
pps.setWidth(penWeight());
|
||||
|
||||
return (pps.createStroke(shape));
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartPlcTable::boundingRect
|
||||
@return Bounding rectangle this part can fit into
|
||||
*/
|
||||
QRectF PartPlcTable::boundingRect() const
|
||||
{
|
||||
qreal adjust = (SHADOWS_HEIGHT + penWeight()) / 2;
|
||||
if (penWeight() == 0) adjust += 0.5;
|
||||
|
||||
QRectF r = m_rect.normalized();
|
||||
r.adjust(-adjust, -adjust, adjust, adjust);
|
||||
|
||||
return(r);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartPlcTable::isUseless
|
||||
@return true if this part is irrelevant and does not deserve to be saved.
|
||||
A PLC table part is always relevant.
|
||||
*/
|
||||
bool PartPlcTable::isUseless() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartPlcTable::startUserTransformation
|
||||
@param initial_selection_rect
|
||||
*/
|
||||
void PartPlcTable::startUserTransformation(const QRectF &initial_selection_rect)
|
||||
{
|
||||
Q_UNUSED(initial_selection_rect)
|
||||
saved_points_.clear();
|
||||
saved_points_ << mapToScene(m_rect.topLeft()) << mapToScene(m_rect.bottomRight());
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartPlcTable::handleUserTransformation
|
||||
@param initial_selection_rect
|
||||
@param new_selection_rect
|
||||
*/
|
||||
void PartPlcTable::handleUserTransformation(const QRectF &initial_selection_rect, const QRectF &new_selection_rect)
|
||||
{
|
||||
QList<QPointF> mapped_points = mapPoints(initial_selection_rect, new_selection_rect, saved_points_);
|
||||
setRect(QRectF(mapFromScene(mapped_points.at(0)), mapFromScene(mapped_points.at(1))));
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartPlcTable::mouseReleaseEvent
|
||||
*/
|
||||
void PartPlcTable::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
CustomElementGraphicPart::mouseReleaseEvent(event);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartPlcTable::itemChange
|
||||
@param change
|
||||
@param value
|
||||
@return
|
||||
*/
|
||||
QVariant PartPlcTable::itemChange(GraphicsItemChange change, const QVariant &value)
|
||||
{
|
||||
if (change == ItemPositionHasChanged)
|
||||
{
|
||||
adjustHandlerPos();
|
||||
}
|
||||
else if (change == ItemSceneChange)
|
||||
{
|
||||
setSelected(false);
|
||||
}
|
||||
else if (change == ItemSceneHasChanged)
|
||||
{
|
||||
if (ElementScene *es = elementScene()) {
|
||||
connect(es, &ElementScene::elementInfoChanged,
|
||||
this, [this]() {
|
||||
QSizeF table_size = calculateTableSize();
|
||||
if (m_rect.size() != table_size) {
|
||||
QPointF top_left = m_rect.topLeft();
|
||||
setRect(QRectF(top_left, table_size));
|
||||
}
|
||||
update();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return QGraphicsItem::itemChange(change, value);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartPlcTable::sceneEventFilter
|
||||
@param watched
|
||||
@param event
|
||||
@return
|
||||
*/
|
||||
bool PartPlcTable::sceneEventFilter(QGraphicsItem *watched, QEvent *event)
|
||||
{
|
||||
if(watched->type() == QetGraphicsHandlerItem::Type)
|
||||
{
|
||||
QetGraphicsHandlerItem *qghi = qgraphicsitem_cast<QetGraphicsHandlerItem *>(watched);
|
||||
|
||||
if(m_handler_vector.contains(qghi))
|
||||
{
|
||||
m_vector_index = m_handler_vector.indexOf(qghi);
|
||||
if (m_vector_index != -1)
|
||||
{
|
||||
if(event->type() == QEvent::GraphicsSceneMousePress)
|
||||
{
|
||||
handlerMousePressEvent(qghi, static_cast<QGraphicsSceneMouseEvent *>(event));
|
||||
return true;
|
||||
}
|
||||
else if(event->type() == QEvent::GraphicsSceneMouseMove)
|
||||
{
|
||||
handlerMouseMoveEvent(qghi, static_cast<QGraphicsSceneMouseEvent *>(event));
|
||||
return true;
|
||||
}
|
||||
else if (event->type() == QEvent::GraphicsSceneMouseRelease)
|
||||
{
|
||||
handlerMouseReleaseEvent(qghi, static_cast<QGraphicsSceneMouseEvent *>(event));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartPlcTable::switchResizeMode
|
||||
*/
|
||||
void PartPlcTable::switchResizeMode()
|
||||
{
|
||||
if (m_resize_mode == 1)
|
||||
{
|
||||
m_resize_mode = 2;
|
||||
for (QetGraphicsHandlerItem *qghi : m_handler_vector)
|
||||
qghi->setColor(Qt::darkGreen);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_resize_mode = 1;
|
||||
qDeleteAll(m_handler_vector);
|
||||
m_handler_vector.clear();
|
||||
addHandler();
|
||||
for (QetGraphicsHandlerItem *qghi : m_handler_vector) {
|
||||
qghi->setColor(Qt::blue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartPlcTable::adjustHandlerPos
|
||||
*/
|
||||
void PartPlcTable::adjustHandlerPos()
|
||||
{
|
||||
if (m_handler_vector.isEmpty())
|
||||
return;
|
||||
|
||||
QVector<QPointF> points_vector = QetGraphicsHandlerUtility::pointsForRect(m_rect);
|
||||
|
||||
if (m_handler_vector.size() == points_vector.size())
|
||||
{
|
||||
points_vector = mapToScene(points_vector);
|
||||
for (int i = 0 ; i < points_vector.size() ; ++i)
|
||||
m_handler_vector.at(i)->setPos(points_vector.at(i));
|
||||
}
|
||||
else
|
||||
{
|
||||
qDeleteAll(m_handler_vector);
|
||||
m_handler_vector.clear();
|
||||
addHandler();
|
||||
}
|
||||
}
|
||||
|
||||
void PartPlcTable::handlerMousePressEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
Q_UNUSED(qghi)
|
||||
Q_UNUSED(event)
|
||||
|
||||
m_old_rect = m_rect;
|
||||
}
|
||||
|
||||
void PartPlcTable::handlerMouseMoveEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
Q_UNUSED(qghi)
|
||||
|
||||
QPointF new_pos = event->scenePos();
|
||||
if (event->modifiers() != Qt::ControlModifier)
|
||||
new_pos = elementScene()->snapToGrid(event->scenePos());
|
||||
new_pos = mapFromScene(new_pos);
|
||||
|
||||
setRect(QetGraphicsHandlerUtility::rectForPosAtIndex(m_rect, new_pos, m_vector_index));
|
||||
adjustHandlerPos();
|
||||
}
|
||||
|
||||
void PartPlcTable::handlerMouseReleaseEvent(QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
Q_UNUSED(qghi)
|
||||
Q_UNUSED(event)
|
||||
|
||||
QUndoCommand *undo = new QUndoCommand("Modifier une table PLC");
|
||||
if (m_old_rect != m_rect) {
|
||||
QPropertyUndoCommand *u = new QPropertyUndoCommand(this, "rect", QVariant(m_old_rect.normalized()), QVariant(m_rect.normalized()), undo);
|
||||
u->setAnimated(true, false);
|
||||
}
|
||||
|
||||
elementScene()->undoStack().push(undo);
|
||||
m_vector_index = -1;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartPlcTable::addHandler
|
||||
Do not add resize handlers - table size is data-driven.
|
||||
Move is handled by the base class QGraphicsItem drag behavior.
|
||||
*/
|
||||
void PartPlcTable::addHandler()
|
||||
{
|
||||
// No resize handlers - size comes from PLC data
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartPlcTable::removeHandler
|
||||
Remove the handlers of this item
|
||||
*/
|
||||
void PartPlcTable::removeHandler()
|
||||
{
|
||||
if (!m_handler_vector.isEmpty())
|
||||
{
|
||||
qDeleteAll(m_handler_vector);
|
||||
m_handler_vector.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
Copyright 2006-2026 The QElectroTech Team
|
||||
This file is part of QElectroTech.
|
||||
|
||||
QElectroTech is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
QElectroTech is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef PARTPLCTABLE_H
|
||||
#define PARTPLCTABLE_H
|
||||
|
||||
#include "customelementgraphicpart.h"
|
||||
#include "../../QetGraphicsItemModeler/qetgraphicshandleritem.h"
|
||||
|
||||
#include <QVector>
|
||||
|
||||
/**
|
||||
@brief The PartPlcTable class
|
||||
This class represents a PLC I/O table preview in the element editor.
|
||||
It shows the user where the PLC table will appear at runtime, so they
|
||||
can position other element parts (rectangles, terminals, etc.) around it.
|
||||
The actual PLC data is read from ElementScene::elementData().
|
||||
*/
|
||||
class PartPlcTable : public CustomElementGraphicPart
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(QRectF rect READ rect WRITE setRect)
|
||||
|
||||
public:
|
||||
PartPlcTable(QETElementEditor *editor, QGraphicsItem *parent = nullptr);
|
||||
~PartPlcTable() override;
|
||||
|
||||
enum { Type = UserType + 1120 };
|
||||
int type () const override { return Type; }
|
||||
void paint (QPainter *, const QStyleOptionGraphicsItem *, QWidget * = nullptr) override;
|
||||
QString name () const override { return QObject::tr("table PLC", "element part name"); }
|
||||
|
||||
QString xmlName () const override { return QString("plc_table"); }
|
||||
const QDomElement toXml (QDomDocument &) const override;
|
||||
void fromXml (const QDomElement &) override;
|
||||
|
||||
QRectF rect() const;
|
||||
void setRect(const QRectF &rect);
|
||||
|
||||
QRectF sceneGeometricRect() const override;
|
||||
QPainterPath shape () const override;
|
||||
QPainterPath shadowShape() const override;
|
||||
QRectF boundingRect() const override;
|
||||
bool isUseless() const override;
|
||||
|
||||
void startUserTransformation(const QRectF &) override;
|
||||
void handleUserTransformation(const QRectF &, const QRectF &) override;
|
||||
|
||||
void addHandler() override;
|
||||
void removeHandler() override;
|
||||
|
||||
protected:
|
||||
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
|
||||
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
|
||||
bool sceneEventFilter(QGraphicsItem *watched, QEvent *event) override;
|
||||
|
||||
private:
|
||||
void switchResizeMode();
|
||||
void adjustHandlerPos();
|
||||
void handlerMousePressEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
|
||||
void handlerMouseMoveEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
|
||||
void handlerMouseReleaseEvent (QetGraphicsHandlerItem *qghi, QGraphicsSceneMouseEvent *event);
|
||||
QSizeF calculateTableSize() const;
|
||||
|
||||
private:
|
||||
QRectF m_rect,
|
||||
m_old_rect;
|
||||
QList<QPointF> saved_points_;
|
||||
int m_resize_mode = 1,
|
||||
m_vector_index = -1;
|
||||
QVector<QetGraphicsHandlerItem *> m_handler_vector;
|
||||
};
|
||||
|
||||
#endif // PARTPLCTABLE_H
|
||||
@@ -17,6 +17,8 @@
|
||||
*/
|
||||
#include "partterminal.h"
|
||||
|
||||
#include "../elementscene.h"
|
||||
#include "../../QPropertyUndoCommand/qpropertyundocommand.h"
|
||||
#include "../../qetgraphicsitem/terminal.h"
|
||||
|
||||
/**
|
||||
@@ -100,6 +102,60 @@ void PartTerminal::paint(
|
||||
|
||||
if (m_hovered)
|
||||
drawShadowShape(painter);
|
||||
|
||||
if (d->m_show_name && !d->m_name.isEmpty()) {
|
||||
painter->save();
|
||||
painter->setFont(d->m_label_font);
|
||||
painter->setPen(d->m_label_color);
|
||||
painter->setRenderHint(QPainter::Antialiasing, true);
|
||||
painter->setRenderHint(QPainter::TextAntialiasing, true);
|
||||
|
||||
QPointF label_pos = d->m_label_pos;
|
||||
QRectF text_rect;
|
||||
QFontMetrics fm(d->m_label_font);
|
||||
QSizeF text_size = fm.size(Qt::TextSingleLine, d->m_name);
|
||||
|
||||
auto compute_rect = [&]() {
|
||||
qreal dx = 0, dy = 0;
|
||||
if (d->m_label_halignment & Qt::AlignLeft) dx = 0;
|
||||
else if (d->m_label_halignment & Qt::AlignHCenter) dx = -text_size.width() / 2.0;
|
||||
else if (d->m_label_halignment & Qt::AlignRight) dx = -text_size.width();
|
||||
if (d->m_label_valignment & Qt::AlignTop) dy = 0;
|
||||
else if (d->m_label_valignment & Qt::AlignVCenter) dy = -text_size.height() / 2.0;
|
||||
else if (d->m_label_valignment & Qt::AlignBottom) dy = -text_size.height();
|
||||
return QRectF(label_pos + QPointF(dx, dy), text_size);
|
||||
};
|
||||
|
||||
if (d->m_label_rotation != 0.0) {
|
||||
painter->translate(label_pos);
|
||||
painter->rotate(d->m_label_rotation);
|
||||
text_rect = QRectF(-text_size.width()/2.0, -text_size.height()/2.0,
|
||||
text_size.width(), text_size.height());
|
||||
if (d->m_label_frame) {
|
||||
painter->drawRect(text_rect.adjusted(-1, -1, 1, 1));
|
||||
}
|
||||
painter->drawText(text_rect, static_cast<int>(d->m_label_halignment | d->m_label_valignment), d->m_name);
|
||||
} else {
|
||||
text_rect = compute_rect();
|
||||
if (d->m_label_frame) {
|
||||
painter->drawRect(text_rect.adjusted(-1, -1, 1, 1));
|
||||
}
|
||||
painter->drawText(text_rect, static_cast<int>(Qt::AlignLeft | Qt::AlignTop), d->m_name);
|
||||
}
|
||||
|
||||
if (isSelected() || m_hovered) {
|
||||
QPen outline_pen(Qt::darkBlue, 0, Qt::DashLine);
|
||||
painter->setPen(outline_pen);
|
||||
painter->setBrush(Qt::NoBrush);
|
||||
if (d->m_label_rotation != 0.0) {
|
||||
painter->drawRect(text_rect.adjusted(-1, -1, 1, 1));
|
||||
} else {
|
||||
painter->drawRect(text_rect.adjusted(-1, -1, 1, 1));
|
||||
}
|
||||
}
|
||||
|
||||
painter->restore();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,7 +170,27 @@ QPainterPath PartTerminal::shape() const
|
||||
QPainterPathStroker pps;
|
||||
pps.setWidth(1);
|
||||
|
||||
return (pps.createStroke(shape));
|
||||
QPainterPath path = pps.createStroke(shape);
|
||||
|
||||
if (d->m_show_name && !d->m_name.isEmpty()) {
|
||||
path.addRect(labelRect());
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartTerminal::shadowShape
|
||||
@return the hover outline shape (terminal line only, no label rect)
|
||||
*/
|
||||
QPainterPath PartTerminal::shadowShape() const
|
||||
{
|
||||
QPainterPath shape;
|
||||
shape.lineTo(d -> m_second_point);
|
||||
|
||||
QPainterPathStroker pps;
|
||||
pps.setWidth(1);
|
||||
return pps.createStroke(shape);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,9 +204,40 @@ QRectF PartTerminal::boundingRect() const
|
||||
|
||||
qreal adjust = (SHADOWS_HEIGHT + 1) / 2;
|
||||
br.adjust(-adjust, -adjust, adjust, adjust);
|
||||
|
||||
if (d->m_show_name && !d->m_name.isEmpty()) {
|
||||
br = br.united(labelRect());
|
||||
}
|
||||
|
||||
return(br);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartTerminal::labelRect
|
||||
@return the rectangle of the label text (in item coordinates),
|
||||
or an empty rect if the label is not shown
|
||||
*/
|
||||
QRectF PartTerminal::labelRect() const
|
||||
{
|
||||
if (!d->m_show_name || d->m_name.isEmpty())
|
||||
return QRectF();
|
||||
|
||||
QFontMetrics fm(d->m_label_font);
|
||||
QSizeF text_size = fm.size(Qt::TextSingleLine, d->m_name);
|
||||
QPointF label_pos = d->m_label_pos;
|
||||
|
||||
qreal dx = 0, dy = 0;
|
||||
if (d->m_label_halignment & Qt::AlignLeft) dx = 0;
|
||||
else if (d->m_label_halignment & Qt::AlignHCenter) dx = -text_size.width() / 2.0;
|
||||
else if (d->m_label_halignment & Qt::AlignRight) dx = -text_size.width();
|
||||
|
||||
if (d->m_label_valignment & Qt::AlignTop) dy = 0;
|
||||
else if (d->m_label_valignment & Qt::AlignVCenter) dy = -text_size.height() / 2.0;
|
||||
else if (d->m_label_valignment & Qt::AlignBottom) dy = -text_size.height();
|
||||
|
||||
return QRectF(label_pos + QPointF(dx, dy), text_size).adjusted(-3, -3, 3, 3);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Definit l'orientation de la borne
|
||||
@@ -282,6 +389,93 @@ void PartTerminal::setNewUuid()
|
||||
d -> m_uuid = QUuid::createUuid();
|
||||
}
|
||||
|
||||
void PartTerminal::setShowName(bool show)
|
||||
{
|
||||
if (d->m_show_name == show) return;
|
||||
prepareGeometryChange();
|
||||
d->m_show_name = show;
|
||||
update();
|
||||
emit showNameChanged();
|
||||
}
|
||||
|
||||
void PartTerminal::setLabelPos(QPointF pos)
|
||||
{
|
||||
if (d->m_label_pos == pos) return;
|
||||
prepareGeometryChange();
|
||||
d->m_label_pos = pos;
|
||||
update();
|
||||
emit labelPosChanged();
|
||||
}
|
||||
|
||||
void PartTerminal::setLabelFont(QFont font)
|
||||
{
|
||||
if (d->m_label_font == font) return;
|
||||
prepareGeometryChange();
|
||||
d->m_label_font = font;
|
||||
update();
|
||||
emit labelFontChanged();
|
||||
}
|
||||
|
||||
void PartTerminal::setLabelRotation(qreal rotation)
|
||||
{
|
||||
if (qFuzzyCompare(d->m_label_rotation, rotation)) return;
|
||||
prepareGeometryChange();
|
||||
d->m_label_rotation = rotation;
|
||||
update();
|
||||
emit labelRotationChanged();
|
||||
}
|
||||
|
||||
void PartTerminal::setLabelHAlignment(Qt::Alignment align)
|
||||
{
|
||||
if (d->m_label_halignment == align) return;
|
||||
prepareGeometryChange();
|
||||
d->m_label_halignment = align;
|
||||
update();
|
||||
emit labelHAlignmentChanged();
|
||||
}
|
||||
|
||||
void PartTerminal::setLabelVAlignment(Qt::Alignment align)
|
||||
{
|
||||
if (d->m_label_valignment == align) return;
|
||||
prepareGeometryChange();
|
||||
d->m_label_valignment = align;
|
||||
update();
|
||||
emit labelVAlignmentChanged();
|
||||
}
|
||||
|
||||
void PartTerminal::setLabelFrame(bool frame)
|
||||
{
|
||||
if (d->m_label_frame == frame) return;
|
||||
prepareGeometryChange();
|
||||
d->m_label_frame = frame;
|
||||
update();
|
||||
emit labelFrameChanged();
|
||||
}
|
||||
|
||||
void PartTerminal::setLabelColor(QColor color)
|
||||
{
|
||||
if (d->m_label_color == color) return;
|
||||
d->m_label_color = color;
|
||||
update();
|
||||
emit labelColorChanged();
|
||||
}
|
||||
|
||||
void PartTerminal::setUseMasterLabel(bool use)
|
||||
{
|
||||
if (d->m_use_master_label == use) return;
|
||||
d->m_use_master_label = use;
|
||||
update();
|
||||
emit useMasterLabelChanged();
|
||||
}
|
||||
|
||||
void PartTerminal::setMasterLabelIndex(int index)
|
||||
{
|
||||
if (d->m_master_label_index == index) return;
|
||||
d->m_master_label_index = index;
|
||||
update();
|
||||
emit masterLabelIndexChanged();
|
||||
}
|
||||
|
||||
/**
|
||||
Updates the position of the second point according to the position
|
||||
and orientation of the terminal.
|
||||
@@ -338,3 +532,53 @@ void PartTerminal::handleUserTransformation(const QRectF &initial_selection_rect
|
||||
initial_selection_rect, new_selection_rect, QList<QPointF>() << saved_position_).first();
|
||||
setPos(mapped_point);
|
||||
}
|
||||
|
||||
void PartTerminal::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
if (event->button() == Qt::LeftButton && d->m_show_name && !d->m_name.isEmpty()) {
|
||||
QPointF local_click = event->pos();
|
||||
// Only start label drag when click is on label AND NOT on terminal line
|
||||
if (!shadowShape().contains(local_click) && labelRect().contains(local_click)) {
|
||||
m_dragging_label = true;
|
||||
m_original_label_pos = d->m_label_pos;
|
||||
}
|
||||
}
|
||||
CustomElementGraphicPart::mousePressEvent(event);
|
||||
}
|
||||
|
||||
void PartTerminal::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
if (m_dragging_label) {
|
||||
QPointF delta = event->pos() - event->buttonDownPos(Qt::LeftButton);
|
||||
QPointF new_pos = m_original_label_pos + delta;
|
||||
if (!(event->modifiers() & Qt::ControlModifier)) {
|
||||
ElementScene *scene = elementScene();
|
||||
if (scene) {
|
||||
QPointF scene_pos = mapToScene(new_pos);
|
||||
new_pos = mapFromScene(scene->snapToGrid(scene_pos));
|
||||
}
|
||||
}
|
||||
setLabelPos(new_pos);
|
||||
update();
|
||||
event->accept();
|
||||
return;
|
||||
}
|
||||
CustomElementGraphicPart::mouseMoveEvent(event);
|
||||
}
|
||||
|
||||
void PartTerminal::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
if (m_dragging_label) {
|
||||
m_dragging_label = false;
|
||||
if (m_original_label_pos != d->m_label_pos) {
|
||||
auto undo = new QPropertyUndoCommand(this, "label_pos",
|
||||
QVariant(m_original_label_pos), QVariant(d->m_label_pos));
|
||||
undo->setText(tr("Déplacer le label d'une borne"));
|
||||
undo->enableAnimation();
|
||||
elementScene()->undoStack().push(undo);
|
||||
}
|
||||
event->accept();
|
||||
return;
|
||||
}
|
||||
CustomElementGraphicPart::mouseReleaseEvent(event);
|
||||
}
|
||||
|
||||
@@ -34,6 +34,16 @@ class PartTerminal : public CustomElementGraphicPart
|
||||
Q_PROPERTY(Qet::Orientation orientation READ orientation WRITE setOrientation)
|
||||
Q_PROPERTY(QString terminal_name READ terminalName WRITE setTerminalName)
|
||||
Q_PROPERTY(TerminalData::Type terminal_type READ terminalType WRITE setTerminalType)
|
||||
Q_PROPERTY(bool show_name READ showName WRITE setShowName)
|
||||
Q_PROPERTY(QPointF label_pos READ labelPos WRITE setLabelPos)
|
||||
Q_PROPERTY(QFont label_font READ labelFont WRITE setLabelFont)
|
||||
Q_PROPERTY(qreal label_rotation READ labelRotation WRITE setLabelRotation)
|
||||
Q_PROPERTY(Qt::Alignment label_halignment READ labelHAlignment WRITE setLabelHAlignment)
|
||||
Q_PROPERTY(Qt::Alignment label_valignment READ labelVAlignment WRITE setLabelVAlignment)
|
||||
Q_PROPERTY(bool label_frame READ labelFrame WRITE setLabelFrame)
|
||||
Q_PROPERTY(QColor label_color READ labelColor WRITE setLabelColor)
|
||||
Q_PROPERTY(bool use_master_label READ useMasterLabel WRITE setUseMasterLabel)
|
||||
Q_PROPERTY(int master_label_index READ masterLabelIndex WRITE setMasterLabelIndex)
|
||||
|
||||
public:
|
||||
// constructors, destructor
|
||||
@@ -46,6 +56,16 @@ class PartTerminal : public CustomElementGraphicPart
|
||||
void orientationChanged();
|
||||
void nameChanged();
|
||||
void terminalTypeChanged();
|
||||
void showNameChanged();
|
||||
void labelPosChanged();
|
||||
void labelFontChanged();
|
||||
void labelRotationChanged();
|
||||
void labelHAlignmentChanged();
|
||||
void labelVAlignmentChanged();
|
||||
void labelFrameChanged();
|
||||
void labelColorChanged();
|
||||
void useMasterLabelChanged();
|
||||
void masterLabelIndexChanged();
|
||||
|
||||
// methods
|
||||
public:
|
||||
@@ -64,7 +84,7 @@ class PartTerminal : public CustomElementGraphicPart
|
||||
QWidget *) override;
|
||||
|
||||
QPainterPath shape() const override;
|
||||
QPainterPath shadowShape() const override {return shape();}
|
||||
QPainterPath shadowShape() const override;
|
||||
QRectF boundingRect() const override;
|
||||
bool isUseless() const override;
|
||||
QRectF sceneGeometricRect() const override;
|
||||
@@ -90,13 +110,52 @@ class PartTerminal : public CustomElementGraphicPart
|
||||
TerminalData::Type terminalType() const {return d->m_type;}
|
||||
void setTerminalType(TerminalData::Type type);
|
||||
|
||||
bool showName() const { return d->m_show_name; }
|
||||
void setShowName(bool show);
|
||||
|
||||
QPointF labelPos() const { return d->m_label_pos; }
|
||||
void setLabelPos(QPointF pos);
|
||||
|
||||
QFont labelFont() const { return d->m_label_font; }
|
||||
void setLabelFont(QFont font);
|
||||
|
||||
qreal labelRotation() const { return d->m_label_rotation; }
|
||||
void setLabelRotation(qreal rotation);
|
||||
|
||||
Qt::Alignment labelHAlignment() const { return d->m_label_halignment; }
|
||||
void setLabelHAlignment(Qt::Alignment align);
|
||||
|
||||
Qt::Alignment labelVAlignment() const { return d->m_label_valignment; }
|
||||
void setLabelVAlignment(Qt::Alignment align);
|
||||
|
||||
bool labelFrame() const { return d->m_label_frame; }
|
||||
void setLabelFrame(bool frame);
|
||||
|
||||
QColor labelColor() const { return d->m_label_color; }
|
||||
void setLabelColor(QColor color);
|
||||
|
||||
bool useMasterLabel() const { return d->m_use_master_label; }
|
||||
void setUseMasterLabel(bool use);
|
||||
|
||||
int masterLabelIndex() const { return d->m_master_label_index; }
|
||||
void setMasterLabelIndex(int index);
|
||||
|
||||
void setNewUuid();
|
||||
|
||||
QRectF labelRect() const;
|
||||
|
||||
protected:
|
||||
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
|
||||
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
|
||||
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
|
||||
|
||||
private:
|
||||
void updateSecondPoint();
|
||||
TerminalData* d; // pointer to the terminal data
|
||||
|
||||
private:
|
||||
QPointF saved_position_;
|
||||
bool m_dragging_label = false;
|
||||
QPointF m_original_label_pos;
|
||||
};
|
||||
#endif
|
||||
|
||||
@@ -18,10 +18,12 @@
|
||||
#include "parttext.h"
|
||||
|
||||
#include "../../QPropertyUndoCommand/qpropertyundocommand.h"
|
||||
#include <QApplication>
|
||||
#include "../../qetapp.h"
|
||||
#include "../elementprimitivedecorator.h"
|
||||
#include "../elementscene.h"
|
||||
#include "../ui/texteditor.h"
|
||||
#include "../../utils/qetutils.h"
|
||||
|
||||
/**
|
||||
Constructeur
|
||||
@@ -124,12 +126,26 @@ void PartText::fromXml(const QDomElement &xml_element) {
|
||||
}
|
||||
else if (xml_element.hasAttribute("font")) {
|
||||
QFont font_;
|
||||
font_.fromString(xml_element.attribute("font"));
|
||||
QETUtils::fontFromString(font_, xml_element.attribute("font"));
|
||||
setFont(font_);
|
||||
}
|
||||
|
||||
setDefaultTextColor(QColor(xml_element.attribute("color", "#000000")));
|
||||
setPlainText(xml_element.attribute("text"));
|
||||
|
||||
// Optional alignment (absent = historical behaviour: top-left anchor,
|
||||
// left-aligned lines), same attributes as the dynamic text fields.
|
||||
Qt::Alignment alignment_ = Qt::AlignTop | Qt::AlignLeft;
|
||||
QMetaEnum me = QMetaEnum::fromType<Qt::Alignment>();
|
||||
if (xml_element.hasAttribute("Halignment"))
|
||||
alignment_ = Qt::Alignment(
|
||||
me.keyToValue(xml_element.attribute("Halignment").toStdString().data()));
|
||||
if (xml_element.hasAttribute("Valignment"))
|
||||
alignment_ = Qt::Alignment(
|
||||
me.keyToValue(xml_element.attribute("Valignment").toStdString().data()))
|
||||
| (alignment_ & Qt::AlignHorizontal_Mask);
|
||||
setAlignment(alignment_);
|
||||
|
||||
setPos(xml_element.attribute("x").toDouble(),
|
||||
xml_element.attribute("y").toDouble());
|
||||
QGraphicsObject::setRotation(QET::correctAngle(xml_element.attribute("rotation", QString::number(0)).toDouble()));
|
||||
@@ -150,10 +166,22 @@ const QDomElement PartText::toXml(QDomDocument &xml_document) const
|
||||
xml_element.setAttribute("x", QString::number(x));
|
||||
xml_element.setAttribute("y", QString::number(y));
|
||||
xml_element.setAttribute("text", toPlainText());
|
||||
xml_element.setAttribute("font", font().toString());
|
||||
xml_element.setAttribute("font", QETUtils::fontToString(font()));
|
||||
xml_element.setAttribute("rotation", QString::number(rot));
|
||||
xml_element.setAttribute("color", defaultTextColor().name());
|
||||
|
||||
// Only written when different from the historical behaviour, so
|
||||
// existing .elmt files round-trip byte-identical.
|
||||
QMetaEnum me = QMetaEnum::fromType<Qt::Alignment>();
|
||||
if (m_alignment & Qt::AlignRight)
|
||||
xml_element.setAttribute("Halignment", me.valueToKey(Qt::AlignRight));
|
||||
else if (m_alignment & Qt::AlignHCenter)
|
||||
xml_element.setAttribute("Halignment", me.valueToKey(Qt::AlignHCenter));
|
||||
if (m_alignment & Qt::AlignBottom)
|
||||
xml_element.setAttribute("Valignment", me.valueToKey(Qt::AlignBottom));
|
||||
else if (m_alignment & Qt::AlignVCenter)
|
||||
xml_element.setAttribute("Valignment", me.valueToKey(Qt::AlignVCenter));
|
||||
|
||||
return(xml_element);
|
||||
}
|
||||
|
||||
@@ -305,24 +333,126 @@ void PartText::setDefaultTextColor(const QColor &color) {
|
||||
|
||||
void PartText::setPlainText(const QString &text) {
|
||||
if (text != this -> toPlainText()) {
|
||||
prepareAlignment();
|
||||
QGraphicsTextItem::setPlainText(text);
|
||||
applyLineAlignment();
|
||||
finishAlignment();
|
||||
emit plainTextChanged(text);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartText::setAlignment
|
||||
Set how this text is anchored to its position: when the content later
|
||||
changes, the given corner/center of the bounding rect keeps its place
|
||||
(the historical behaviour, and the default, is top-left). The
|
||||
horizontal part also aligns the lines of a multi-line text relative
|
||||
to each other. Changing the alignment never moves the text itself.
|
||||
@param alignment
|
||||
*/
|
||||
void PartText::setAlignment(const Qt::Alignment &alignment)
|
||||
{
|
||||
if (alignment == m_alignment)
|
||||
return;
|
||||
m_alignment = alignment;
|
||||
applyLineAlignment();
|
||||
emit alignmentChanged(m_alignment);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartText::applyLineAlignment
|
||||
Align the lines of a multi-line text relative to each other according
|
||||
to the horizontal part of the alignment property. QGraphicsTextItem
|
||||
only honors the document text option when a text width is set, hence
|
||||
the idealWidth() dance; -1 restores the free (historical) layout.
|
||||
*/
|
||||
void PartText::applyLineAlignment()
|
||||
{
|
||||
QTextOption option = document()->defaultTextOption();
|
||||
option.setAlignment(m_alignment & Qt::AlignHorizontal_Mask);
|
||||
document()->setDefaultTextOption(option);
|
||||
|
||||
setTextWidth(-1);
|
||||
if (m_alignment & (Qt::AlignHCenter | Qt::AlignRight))
|
||||
setTextWidth(document()->idealWidth());
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartText::prepareAlignment
|
||||
Call before a change of the bounding rect (see finishAlignment).
|
||||
*/
|
||||
void PartText::prepareAlignment()
|
||||
{
|
||||
m_alignment_rect = boundingRect();
|
||||
}
|
||||
|
||||
/**
|
||||
@brief PartText::finishAlignment
|
||||
Call after a change of the bounding rect: moves the text so that the
|
||||
point selected by the alignment property stays where it was (same
|
||||
logic as DiagramTextItem::finishAlignment).
|
||||
*/
|
||||
void PartText::finishAlignment()
|
||||
{
|
||||
QTransform transform;
|
||||
transform.rotate(rotation());
|
||||
qreal x, xa, y, ya;
|
||||
x = xa = 0;
|
||||
y = ya = 0;
|
||||
|
||||
if (m_alignment & Qt::AlignRight)
|
||||
{
|
||||
x = m_alignment_rect.right();
|
||||
xa = boundingRect().right();
|
||||
}
|
||||
else if (m_alignment & Qt::AlignHCenter)
|
||||
{
|
||||
x = m_alignment_rect.center().x();
|
||||
xa = boundingRect().center().x();
|
||||
}
|
||||
|
||||
if (m_alignment & Qt::AlignBottom)
|
||||
{
|
||||
y = m_alignment_rect.bottom();
|
||||
ya = boundingRect().bottom();
|
||||
}
|
||||
else if (m_alignment & Qt::AlignVCenter)
|
||||
{
|
||||
y = m_alignment_rect.center().y();
|
||||
ya = boundingRect().center().y();
|
||||
}
|
||||
|
||||
QPointF p = transform.map(QPointF(x, y));
|
||||
QPointF pa = transform.map(QPointF(xa, ya));
|
||||
|
||||
setPos(pos() - (pa - p));
|
||||
}
|
||||
|
||||
void PartText::setFont(const QFont &font) {
|
||||
if (font != this -> font()) {
|
||||
prepareAlignment();
|
||||
QGraphicsTextItem::setFont(font);
|
||||
applyLineAlignment();
|
||||
finishAlignment();
|
||||
// Re-anchor: the item's position transform is -margin(), and margin()
|
||||
// depends on the font ascent. Without re-running this on a font change,
|
||||
// the transform keeps the previous font's ascent — so the text renders
|
||||
// at a different spot after save/reopen (the position recomputes from
|
||||
// the saved font on load). See #158.
|
||||
adjustItemPosition();
|
||||
emit fontChanged(font);
|
||||
}
|
||||
}
|
||||
|
||||
void PartText::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
|
||||
if((event -> buttons() & Qt::LeftButton) && (flags() & QGraphicsItem::ItemIsMovable)) {
|
||||
QPointF pos = event -> scenePos() + (m_origin_pos - event -> buttonDownScenePos(Qt::LeftButton));
|
||||
event -> modifiers() == Qt::ControlModifier ? setPos(pos) : setPos(elementScene() -> snapToGrid(pos));
|
||||
}
|
||||
else {
|
||||
if ((event->buttons() & Qt::LeftButton) && (flags() & QGraphicsItem::ItemIsMovable)) {
|
||||
// Suppress spurious moves from the properties dock resizing the viewport.
|
||||
const QPointF d = event->screenPos() - event->buttonDownScreenPos(Qt::LeftButton);
|
||||
if (d.manhattanLength() < QApplication::startDragDistance())
|
||||
return;
|
||||
QPointF pos = event->scenePos() + (m_origin_pos - event->buttonDownScenePos(Qt::LeftButton));
|
||||
event->modifiers() == Qt::ControlModifier ? setPos(pos) : setPos(elementScene()->snapToGrid(pos));
|
||||
} else {
|
||||
QGraphicsObject::mouseMoveEvent(event);
|
||||
}
|
||||
}
|
||||
@@ -387,6 +517,11 @@ void PartText::startEdition()
|
||||
{
|
||||
// !previous_text.isNull() means the text is being edited
|
||||
previous_text = toPlainText();
|
||||
|
||||
// Anchor the aligned point across the whole inline edition; free the
|
||||
// text width so typing is not wrapped at the previous block width.
|
||||
prepareAlignment();
|
||||
setTextWidth(-1);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -395,7 +530,8 @@ void PartText::startEdition()
|
||||
*/
|
||||
void PartText::endEdition()
|
||||
{
|
||||
if (!previous_text.isNull()) {
|
||||
const bool was_editing = !previous_text.isNull();
|
||||
if (was_editing) {
|
||||
// the text was being edited
|
||||
QString new_text = toPlainText();
|
||||
if (previous_text != new_text) {
|
||||
@@ -412,4 +548,9 @@ void PartText::endEdition()
|
||||
setTextCursor(qtc);
|
||||
|
||||
setEditable(false);
|
||||
|
||||
if (was_editing) {
|
||||
applyLineAlignment();
|
||||
finishAlignment();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,11 +34,13 @@ class PartText : public QGraphicsTextItem, public CustomElementPart {
|
||||
Q_PROPERTY(QColor color READ defaultTextColor WRITE setDefaultTextColor NOTIFY colorChanged)
|
||||
Q_PROPERTY(QString text READ toPlainText WRITE setPlainText NOTIFY plainTextChanged)
|
||||
Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
|
||||
Q_PROPERTY(Qt::Alignment alignment READ alignment WRITE setAlignment NOTIFY alignmentChanged)
|
||||
|
||||
signals:
|
||||
void fontChanged(const QFont &font);
|
||||
void colorChanged(const QColor &color);
|
||||
void plainTextChanged(const QString &text);
|
||||
void alignmentChanged(Qt::Alignment alignment);
|
||||
|
||||
// constructors, destructor
|
||||
public:
|
||||
@@ -77,6 +79,8 @@ class PartText : public QGraphicsTextItem, public CustomElementPart {
|
||||
void setDefaultTextColor(const QColor &color);
|
||||
void setPlainText(const QString &text);
|
||||
void setFont(const QFont &font);
|
||||
void setAlignment(const Qt::Alignment &alignment);
|
||||
Qt::Alignment alignment() const {return m_alignment;}
|
||||
|
||||
public slots:
|
||||
void adjustItemPosition(int = 0);
|
||||
@@ -97,11 +101,16 @@ class PartText : public QGraphicsTextItem, public CustomElementPart {
|
||||
|
||||
private:
|
||||
QPointF margin() const;
|
||||
void applyLineAlignment();
|
||||
void prepareAlignment();
|
||||
void finishAlignment();
|
||||
QString previous_text;
|
||||
qreal real_font_size_;
|
||||
QPointF saved_point_;
|
||||
qreal saved_font_size_;
|
||||
QGraphicsItem *decorator_;
|
||||
QPointF m_origin_pos;
|
||||
Qt::Alignment m_alignment = (Qt::AlignTop | Qt::AlignLeft);
|
||||
QRectF m_alignment_rect;
|
||||
};
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user