make partTextEditor capable to modify multiple parts at the same time

This commit is contained in:
Martin Marmsoler 2020-05-28 18:29:56 +02:00 committed by Laurent Trinques
parent a2d466240b
commit a07e973482

View File

@ -57,10 +57,7 @@ void TextEditor::updateForm()
return; return;
} }
for (QMetaObject::Connection c : m_edit_connection) { disconnectEditConnection();
disconnect(c);
}
m_edit_connection.clear();
ui->m_line_edit->setText(m_text->toPlainText()); ui->m_line_edit->setText(m_text->toPlainText());
ui->m_x_sb->setValue(m_text->pos().x()); ui->m_x_sb->setValue(m_text->pos().x());
@ -73,6 +70,30 @@ void TextEditor::updateForm()
setUpEditConnection(); setUpEditConnection();
} }
void TextEditor::setUpChangeConnection(QPointer<PartText> part) {
assert(m_change_connection.isEmpty());
m_change_connection << connect(part, &PartText::plainTextChanged, this, &TextEditor::updateForm);
m_change_connection << connect(part, &PartText::xChanged, this, &TextEditor::updateForm);
m_change_connection << connect(part, &PartText::yChanged, this, &TextEditor::updateForm);
m_change_connection << connect(part, &PartText::rotationChanged, this, &TextEditor::updateForm);
m_change_connection << connect(part, &PartText::fontChanged, this, &TextEditor::updateForm);
m_change_connection << connect(part, &PartText::colorChanged, this, &TextEditor::updateForm);
}
void TextEditor::disconnectChangeConnection() {
for (QMetaObject::Connection c : m_change_connection) {
disconnect(c);
}
m_change_connection.clear();
}
void TextEditor::disconnectEditConnection() {
for (QMetaObject::Connection c : m_edit_connection) {
disconnect(c);
}
m_edit_connection.clear();
}
/** /**
* @brief TextEditor::setPart * @brief TextEditor::setPart
* Set the current text to edit. * Set the current text to edit.
@ -85,10 +106,7 @@ bool TextEditor::setPart(CustomElementPart *part)
if (!part) if (!part)
{ {
m_text = nullptr; m_text = nullptr;
for (QMetaObject::Connection c : m_change_connection) { disconnectChangeConnection();
disconnect(c);
}
m_change_connection.clear();
return true; return true;
} }
@ -99,13 +117,7 @@ bool TextEditor::setPart(CustomElementPart *part)
} }
m_text = part_text; m_text = part_text;
m_change_connection.clear(); setUpChangeConnection(m_text);
m_change_connection << connect(part_text, &PartText::plainTextChanged, this, &TextEditor::updateForm);
m_change_connection << connect(part_text, &PartText::xChanged, this, &TextEditor::updateForm);
m_change_connection << connect(part_text, &PartText::yChanged, this, &TextEditor::updateForm);
m_change_connection << connect(part_text, &PartText::rotationChanged, this, &TextEditor::updateForm);
m_change_connection << connect(part_text, &PartText::fontChanged, this, &TextEditor::updateForm);
m_change_connection << connect(part_text, &PartText::colorChanged, this, &TextEditor::updateForm);
updateForm(); updateForm();
return true; return true;
@ -113,6 +125,37 @@ bool TextEditor::setPart(CustomElementPart *part)
return false; return false;
} }
bool TextEditor::setParts(QList <CustomElementPart *> parts) {
if (parts.isEmpty())
{
m_parts.clear();
if (m_text) {
disconnectChangeConnection();
}
m_text = nullptr;
return true;
}
if (PartText *part= static_cast<PartText *>(parts.first()))
{
if (m_text) {
disconnectChangeConnection();
}
m_text = part;
m_parts.clear();
m_parts.append(part);
for (int i=1; i < parts.length(); i++)
m_parts.append(dynamic_cast<PartText*>(parts[i]));
setUpChangeConnection(m_text);
updateForm();
return true;
}
return(false);
}
/** /**
* @brief TextEditor::currentPart * @brief TextEditor::currentPart
* @return The current part * @return The current part
@ -128,63 +171,72 @@ CustomElementPart *TextEditor::currentPart() const {
*/ */
void TextEditor::setUpEditConnection() void TextEditor::setUpEditConnection()
{ {
for (QMetaObject::Connection c : m_edit_connection) { disconnectEditConnection();
disconnect(c);
}
m_edit_connection.clear();
m_edit_connection << connect(ui->m_line_edit, &QLineEdit::textEdited, [this]() m_edit_connection << connect(ui->m_line_edit, &QLineEdit::textEdited, [this]()
{ {
QString text_ = ui->m_line_edit->text(); QString text_ = ui->m_line_edit->text();
if (text_ != m_text->toPlainText()) for (auto partText: m_parts) {
{ if (text_ != partText->toPlainText())
QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text, "text", m_text->toPlainText(), text_); {
undo->setText(tr("Modifier le contenu d'un champ texte")); QPropertyUndoCommand *undo = new QPropertyUndoCommand(partText, "text", partText->toPlainText(), text_);
undoStack().push(undo); undo->setText(tr("Modifier le contenu d'un champ texte"));
} undoStack().push(undo);
}
}
}); });
m_edit_connection << connect(ui->m_x_sb, QOverload<int>::of(&QSpinBox::valueChanged), [this]() m_edit_connection << connect(ui->m_x_sb, QOverload<int>::of(&QSpinBox::valueChanged), [this]()
{ {
QPointF pos(ui->m_x_sb->value(), ui->m_y_sb->value()); QPointF pos(ui->m_x_sb->value(), 0);
if (pos != m_text->pos()) for (auto partText: m_parts) {
{ pos.setY(partText->pos().y());
QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text, "pos", m_text->pos(), pos); if (pos != partText->pos())
undo->setText(tr("Déplacer un champ texte")); {
undo->setAnimated(true, false); QPropertyUndoCommand *undo = new QPropertyUndoCommand(partText, "pos", partText->pos(), pos);
undoStack().push(undo); undo->setText(tr("Déplacer un champ texte"));
} undo->setAnimated(true, false);
undoStack().push(undo);
}
}
}); });
m_edit_connection << connect(ui->m_y_sb, QOverload<int>::of(&QSpinBox::valueChanged), [this]() m_edit_connection << connect(ui->m_y_sb, QOverload<int>::of(&QSpinBox::valueChanged), [this]()
{ {
QPointF pos(ui->m_x_sb->value(), ui->m_y_sb->value()); QPointF pos(0, ui->m_y_sb->value());
if (pos != m_text->pos()) for (auto partText: m_parts) {
{ pos.setX(partText->pos().x());
QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text, "pos", m_text->pos(), pos); if (pos != partText->pos())
undo->setText(tr("Déplacer un champ texte")); {
undo->setAnimated(true, false); QPropertyUndoCommand *undo = new QPropertyUndoCommand(partText, "pos", partText->pos(), pos);
undoStack().push(undo); undo->setText(tr("Déplacer un champ texte"));
} undo->setAnimated(true, false);
undoStack().push(undo);
}
}
}); });
m_edit_connection << connect(ui->m_rotation_sb, QOverload<int>::of(&QSpinBox::valueChanged), [this]() m_edit_connection << connect(ui->m_rotation_sb, QOverload<int>::of(&QSpinBox::valueChanged), [this]()
{ {
if (ui->m_rotation_sb->value() != m_text->rotation()) for (auto partText: m_parts) {
{ if (ui->m_rotation_sb->value() != partText->rotation())
QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text, "rotation", m_text->rotation(), ui->m_rotation_sb->value()); {
undo->setText(tr("Pivoter un champ texte")); QPropertyUndoCommand *undo = new QPropertyUndoCommand(partText, "rotation", partText->rotation(), ui->m_rotation_sb->value());
undo->setAnimated(true, false); undo->setText(tr("Pivoter un champ texte"));
undoStack().push(undo); undo->setAnimated(true, false);
} undoStack().push(undo);
}
}
}); });
m_edit_connection << connect(ui->m_size_sb, QOverload<int>::of(&QSpinBox::valueChanged), [this]() m_edit_connection << connect(ui->m_size_sb, QOverload<int>::of(&QSpinBox::valueChanged), [this]()
{ {
if (m_text->font().pointSize() != ui->m_size_sb->value()) for (auto partText: m_parts) {
{ if (partText->font().pointSize() != ui->m_size_sb->value())
QFont font_ = m_text->font(); {
font_.setPointSize(ui->m_size_sb->value()); QFont font_ = partText->font();
QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text, "font", m_text->font(), font_); font_.setPointSize(ui->m_size_sb->value());
undo->setText(tr("Modifier la police d'un texte")); QPropertyUndoCommand *undo = new QPropertyUndoCommand(partText, "font", partText->font(), font_);
undoStack().push(undo); undo->setText(tr("Modifier la police d'un texte"));
} undoStack().push(undo);
}
}
}); });
} }
@ -196,17 +248,22 @@ void TextEditor::on_m_font_pb_clicked()
bool ok; bool ok;
QFont font_ = QFontDialog::getFont(&ok, m_text->font(), this); QFont font_ = QFontDialog::getFont(&ok, m_text->font(), this);
if (ok && font_ != m_text->font()) if (ok && font_ != m_text->font()) {
{ ui->m_size_sb->blockSignals(true);
ui->m_size_sb->blockSignals(true); ui->m_size_sb->setValue(font_.pointSize());
ui->m_size_sb->setValue(font_.pointSize()); ui->m_size_sb->blockSignals(false);
ui->m_size_sb->blockSignals(false);
ui->m_font_pb->setText(font_.family()); ui->m_font_pb->setText(font_.family());
QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text, "font", m_text->font(), font_); }
undo->setText(tr("Modifier la police d'un texte"));
undoStack().push(undo); for (auto partText: m_parts) {
} if (ok && font_ != partText->font())
{
QPropertyUndoCommand *undo = new QPropertyUndoCommand(partText, "font", partText->font(), font_);
undo->setText(tr("Modifier la police d'un texte"));
undoStack().push(undo);
}
}
} }
/** /**
@ -215,10 +272,12 @@ void TextEditor::on_m_font_pb_clicked()
*/ */
void TextEditor::on_m_color_pb_changed(const QColor &newColor) void TextEditor::on_m_color_pb_changed(const QColor &newColor)
{ {
if (newColor != m_text->defaultTextColor()) for (auto partText: m_parts) {
{ if (newColor != partText->defaultTextColor())
QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text, "color", m_text->defaultTextColor(), newColor); {
undo->setText(tr("Modifier la couleur d'un texte")); QPropertyUndoCommand *undo = new QPropertyUndoCommand(partText, "color", partText->defaultTextColor(), newColor);
undoStack().push(undo); undo->setText(tr("Modifier la couleur d'un texte"));
} undoStack().push(undo);
}
}
} }