2019-03-10 17:58:33 +00:00
|
|
|
/*
|
2020-06-15 17:42:37 +02:00
|
|
|
Copyright 2006-2020 The QElectroTech Team
|
2019-03-10 17:58:33 +00:00
|
|
|
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 "texteditor.h"
|
|
|
|
#include "ui_texteditor.h"
|
|
|
|
#include "parttext.h"
|
|
|
|
#include "QPropertyUndoCommand/qpropertyundocommand.h"
|
2020-06-14 18:14:14 +02:00
|
|
|
#include <cassert>
|
2019-03-10 17:58:33 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief TextEditor::TextEditor
|
|
|
|
Default constructor
|
|
|
|
@param editor : the element editor who use this editor
|
|
|
|
@param text : the text to edit
|
|
|
|
@param parent : the parent widget
|
|
|
|
*/
|
2019-03-10 17:58:33 +00:00
|
|
|
TextEditor::TextEditor(QETElementEditor *editor, PartText *text, QWidget *parent) :
|
2020-08-05 08:19:47 +01:00
|
|
|
ElementItemEditor(editor, parent),
|
|
|
|
ui(new Ui::TextEditor) {
|
2020-08-05 09:27:19 +01:00
|
|
|
ui -> setupUi(this);
|
2019-03-10 17:58:33 +00:00
|
|
|
setUpEditConnection();
|
2020-08-05 08:19:47 +01:00
|
|
|
if (text) {
|
2019-03-10 17:58:33 +00:00
|
|
|
setPart(text);
|
|
|
|
updateForm();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief TextEditor::~TextEditor
|
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
TextEditor::~TextEditor()
|
|
|
|
{
|
2019-03-10 17:58:33 +00:00
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief TextEditor::updateForm
|
|
|
|
Update the gui
|
|
|
|
*/
|
2019-03-10 17:58:33 +00:00
|
|
|
void TextEditor::updateForm()
|
|
|
|
{
|
|
|
|
if (m_text.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-05 08:19:47 +01:00
|
|
|
disconnectEditConnection();
|
2019-03-10 17:58:33 +00:00
|
|
|
|
2020-08-05 09:27:19 +01:00
|
|
|
ui -> m_line_edit -> setText(m_text -> toPlainText());
|
|
|
|
ui -> m_x_sb -> setValue(m_text -> pos().x());
|
|
|
|
ui -> m_y_sb -> setValue(m_text -> pos().y());
|
|
|
|
ui -> m_rotation_sb -> setValue(m_text -> rotation());
|
|
|
|
ui -> m_size_sb -> setValue(m_text -> font().pointSize());
|
|
|
|
ui -> m_font_pb -> setText(m_text -> font().family());
|
|
|
|
ui -> m_color_pb -> setColor(m_text -> defaultTextColor());
|
2019-03-10 17:58:33 +00:00
|
|
|
|
|
|
|
setUpEditConnection();
|
|
|
|
}
|
|
|
|
|
2020-05-28 18:29:56 +02:00
|
|
|
void TextEditor::setUpChangeConnection(QPointer<PartText> part) {
|
2020-08-05 08:19:47 +01:00
|
|
|
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);
|
2020-05-28 18:29:56 +02:00
|
|
|
}
|
|
|
|
|
2020-09-07 22:03:40 +02:00
|
|
|
void TextEditor::disconnectChangeConnection()
|
|
|
|
{
|
2020-08-05 08:19:47 +01:00
|
|
|
for (QMetaObject::Connection c : m_change_connection) {
|
|
|
|
disconnect(c);
|
|
|
|
}
|
|
|
|
m_change_connection.clear();
|
2020-05-28 18:29:56 +02:00
|
|
|
}
|
|
|
|
|
2020-09-07 22:03:40 +02:00
|
|
|
void TextEditor::disconnectEditConnection()
|
|
|
|
{
|
2020-08-05 08:19:47 +01:00
|
|
|
for (QMetaObject::Connection c : m_edit_connection) {
|
|
|
|
disconnect(c);
|
|
|
|
}
|
|
|
|
m_edit_connection.clear();
|
2020-05-28 18:29:56 +02:00
|
|
|
}
|
|
|
|
|
2019-03-10 17:58:33 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief TextEditor::setPart
|
|
|
|
Set the current text to edit.
|
2020-08-19 21:24:48 +02:00
|
|
|
Set part to nullptr to clear the current text.
|
2020-08-16 11:19:36 +02:00
|
|
|
@param part : part to edit
|
2020-08-19 21:24:48 +02:00
|
|
|
@return if part is a partext or nullptr, else return false
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2020-08-05 09:27:19 +01:00
|
|
|
bool TextEditor::setPart(CustomElementPart *part) {
|
|
|
|
if (!part) {
|
2019-03-10 17:58:33 +00:00
|
|
|
m_text = nullptr;
|
2020-07-15 20:20:07 +02:00
|
|
|
disconnectChangeConnection();
|
2019-03-10 17:58:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-07-15 20:20:07 +02:00
|
|
|
|
2020-08-05 09:27:19 +01:00
|
|
|
if (PartText *part_text = static_cast<PartText *>(part)) {
|
2019-03-10 17:58:33 +00:00
|
|
|
if (part_text == m_text) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
m_text = part_text;
|
2020-07-15 20:20:07 +02:00
|
|
|
setUpChangeConnection(m_text);
|
2019-03-10 17:58:33 +00:00
|
|
|
updateForm();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-28 18:29:56 +02:00
|
|
|
bool TextEditor::setParts(QList <CustomElementPart *> parts) {
|
2020-08-05 08:19:47 +01:00
|
|
|
if (parts.isEmpty()) {
|
2020-07-15 20:20:07 +02:00
|
|
|
m_parts.clear();
|
|
|
|
if (m_text) {
|
|
|
|
disconnectChangeConnection();
|
|
|
|
}
|
|
|
|
m_text = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
2020-08-05 08:19:47 +01:00
|
|
|
|
2020-08-05 09:27:19 +01:00
|
|
|
if (PartText *part = static_cast<PartText *>(parts.first())) {
|
2020-07-15 20:20:07 +02:00
|
|
|
if (m_text) {
|
|
|
|
disconnectChangeConnection();
|
|
|
|
}
|
|
|
|
m_text = part;
|
|
|
|
m_parts.clear();
|
|
|
|
m_parts.append(part);
|
2020-08-05 09:27:19 +01:00
|
|
|
for (int i=1; i < parts.length(); i++) {
|
2020-07-15 20:20:07 +02:00
|
|
|
m_parts.append(static_cast<PartText*>(parts[i]));
|
2020-08-05 09:27:19 +01:00
|
|
|
}
|
2020-07-15 20:20:07 +02:00
|
|
|
setUpChangeConnection(m_text);
|
|
|
|
updateForm();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return(false);
|
2020-05-28 18:29:56 +02:00
|
|
|
}
|
|
|
|
|
2019-03-10 17:58:33 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief TextEditor::currentPart
|
|
|
|
@return The current part
|
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
CustomElementPart *TextEditor::currentPart() const
|
|
|
|
{
|
2019-03-10 17:58:33 +00:00
|
|
|
return m_text;
|
|
|
|
}
|
|
|
|
|
2020-09-07 22:03:40 +02:00
|
|
|
QList<CustomElementPart*> TextEditor::currentParts() const
|
|
|
|
{
|
2020-07-15 20:20:07 +02:00
|
|
|
QList<CustomElementPart*> parts;
|
|
|
|
for (auto part: m_parts) {
|
|
|
|
parts.append(static_cast<CustomElementPart*>(part));
|
|
|
|
}
|
|
|
|
return parts;
|
2020-06-01 20:45:01 +02:00
|
|
|
}
|
|
|
|
|
2019-03-10 17:58:33 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief TextEditor::setUpEditConnection
|
|
|
|
Setup the connection between the widgets of this editor and the undo command
|
|
|
|
use to apply the change to the edited text.
|
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
void TextEditor::setUpEditConnection()
|
|
|
|
{
|
2020-07-15 20:20:07 +02:00
|
|
|
disconnectEditConnection();
|
2020-08-05 08:19:47 +01:00
|
|
|
|
2020-08-06 13:20:33 +02:00
|
|
|
m_edit_connection << connect(ui -> m_line_edit, &QLineEdit::editingFinished, [this]() {
|
2020-08-05 09:27:19 +01:00
|
|
|
QString text_ = ui -> m_line_edit -> text();
|
2020-07-15 20:20:07 +02:00
|
|
|
for (int i=0; i < m_parts.length(); i++) {
|
|
|
|
PartText* partText = m_parts[i];
|
2020-08-05 09:27:19 +01:00
|
|
|
if (text_ != partText -> toPlainText()) {
|
|
|
|
QPropertyUndoCommand *undo = new QPropertyUndoCommand(partText, "text", partText -> toPlainText(), text_);
|
|
|
|
undo -> setText(tr("Modifier le contenu d'un champ texte"));
|
2020-07-15 20:20:07 +02:00
|
|
|
undoStack().push(undo);
|
|
|
|
}
|
|
|
|
}
|
2019-03-10 17:58:33 +00:00
|
|
|
});
|
2020-08-05 08:19:47 +01:00
|
|
|
m_edit_connection << connect(ui->m_x_sb, QOverload<int>::of(&QSpinBox::valueChanged), [this]() {
|
2020-08-05 09:27:19 +01:00
|
|
|
QPointF pos(ui -> m_x_sb -> value(), 0);
|
2020-07-15 20:20:07 +02:00
|
|
|
for (int i=0; i < m_parts.length(); i++) {
|
|
|
|
PartText* partText = m_parts[i];
|
2020-08-05 09:27:19 +01:00
|
|
|
pos.setY(partText -> pos().y());
|
|
|
|
if (pos != partText -> pos()) {
|
|
|
|
QPropertyUndoCommand *undo = new QPropertyUndoCommand(partText, "pos", partText -> pos(), pos);
|
|
|
|
undo -> setText(tr("Déplacer un champ texte"));
|
|
|
|
undo -> setAnimated(true, false);
|
2020-07-15 20:20:07 +02:00
|
|
|
undoStack().push(undo);
|
|
|
|
}
|
|
|
|
}
|
2019-03-10 17:58:33 +00:00
|
|
|
});
|
2020-08-05 09:27:19 +01:00
|
|
|
m_edit_connection << connect(ui -> m_y_sb, QOverload<int>::of(&QSpinBox::valueChanged), [this]() {
|
|
|
|
QPointF pos(0, ui -> m_y_sb -> value());
|
2020-07-15 20:20:07 +02:00
|
|
|
for (int i=0; i < m_parts.length(); i++) {
|
|
|
|
PartText* partText = m_parts[i];
|
2020-08-05 09:27:19 +01:00
|
|
|
pos.setX(partText -> pos().x());
|
|
|
|
if (pos != partText -> pos()) {
|
|
|
|
QPropertyUndoCommand *undo = new QPropertyUndoCommand(partText, "pos", partText -> pos(), pos);
|
|
|
|
undo -> setText(tr("Déplacer un champ texte"));
|
|
|
|
undo -> setAnimated(true, false);
|
2020-07-15 20:20:07 +02:00
|
|
|
undoStack().push(undo);
|
|
|
|
}
|
|
|
|
}
|
2020-05-28 20:48:56 +02:00
|
|
|
});
|
2020-08-05 09:27:19 +01:00
|
|
|
m_edit_connection << connect(ui -> m_rotation_sb, QOverload<int>::of(&QSpinBox::valueChanged), [this]() {
|
2020-07-15 20:20:07 +02:00
|
|
|
for (int i=0; i < m_parts.length(); i++) {
|
|
|
|
PartText* partText = m_parts[i];
|
2020-08-05 09:27:19 +01:00
|
|
|
if (ui -> m_rotation_sb -> value() != partText -> rotation()) {
|
|
|
|
QPropertyUndoCommand *undo = new QPropertyUndoCommand(
|
|
|
|
partText, "rotation", partText -> rotation(), ui -> m_rotation_sb -> value());
|
|
|
|
undo -> setText(tr("Pivoter un champ texte"));
|
|
|
|
undo -> setAnimated(true, false);
|
2020-07-15 20:20:07 +02:00
|
|
|
undoStack().push(undo);
|
|
|
|
}
|
|
|
|
}
|
2020-05-28 20:48:56 +02:00
|
|
|
});
|
2020-08-05 08:19:47 +01:00
|
|
|
m_edit_connection << connect(ui->m_size_sb, QOverload<int>::of(&QSpinBox::valueChanged), [this]() {
|
2020-07-15 20:20:07 +02:00
|
|
|
for (int i=0; i < m_parts.length(); i++) {
|
|
|
|
PartText* partText = m_parts[i];
|
2020-08-05 09:27:19 +01:00
|
|
|
if (partText -> font().pointSize() != ui -> m_size_sb -> value()) {
|
|
|
|
QFont font_ = partText -> font();
|
|
|
|
font_.setPointSize(ui -> m_size_sb -> value());
|
|
|
|
QPropertyUndoCommand *undo = new QPropertyUndoCommand(partText, "font", partText -> font(), font_);
|
|
|
|
undo -> setText(tr("Modifier la police d'un texte"));
|
2020-07-15 20:20:07 +02:00
|
|
|
undoStack().push(undo);
|
|
|
|
}
|
|
|
|
}
|
2019-03-10 17:58:33 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief TextEditor::on_m_font_pb_clicked
|
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
void TextEditor::on_m_font_pb_clicked()
|
|
|
|
{
|
2019-03-10 17:58:33 +00:00
|
|
|
bool ok;
|
2020-08-05 09:27:19 +01:00
|
|
|
QFont font_ = QFontDialog::getFont(&ok, m_text -> font(), this);
|
2020-08-05 08:19:47 +01:00
|
|
|
|
2020-08-05 09:27:19 +01:00
|
|
|
if (ok && font_ != m_text -> font()) {
|
|
|
|
ui -> m_size_sb -> blockSignals(true);
|
|
|
|
ui -> m_size_sb -> setValue(font_.pointSize());
|
|
|
|
ui -> m_size_sb -> blockSignals(false);
|
2020-08-05 08:19:47 +01:00
|
|
|
|
2020-08-05 09:27:19 +01:00
|
|
|
ui -> m_font_pb -> setText(font_.family());
|
2020-07-15 20:20:07 +02:00
|
|
|
}
|
2020-08-05 08:19:47 +01:00
|
|
|
|
2020-07-15 20:20:07 +02:00
|
|
|
for (int i=0; i < m_parts.length(); i++) {
|
|
|
|
PartText* partText = m_parts[i];
|
2020-08-05 09:27:19 +01:00
|
|
|
if (ok && font_ != partText -> font()) {
|
|
|
|
QPropertyUndoCommand *undo = new QPropertyUndoCommand(partText, "font", partText -> font(), font_);
|
|
|
|
undo -> setText(tr("Modifier la police d'un texte"));
|
2020-07-15 20:20:07 +02:00
|
|
|
undoStack().push(undo);
|
|
|
|
}
|
|
|
|
}
|
2019-03-10 17:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief TextEditor::on_m_color_pb_changed
|
|
|
|
@param newColor
|
|
|
|
*/
|
2020-08-05 08:19:47 +01:00
|
|
|
void TextEditor::on_m_color_pb_changed(const QColor &newColor) {
|
2020-07-15 20:20:07 +02:00
|
|
|
for (int i=0; i < m_parts.length(); i++) {
|
|
|
|
PartText* partText = m_parts[i];
|
2020-08-05 09:27:19 +01:00
|
|
|
if (newColor != partText -> defaultTextColor()) {
|
|
|
|
QPropertyUndoCommand *undo = new QPropertyUndoCommand(
|
|
|
|
partText, "color", partText -> defaultTextColor(), newColor);
|
|
|
|
undo -> setText(tr("Modifier la couleur d'un texte"));
|
2020-07-15 20:20:07 +02:00
|
|
|
undoStack().push(undo);
|
|
|
|
}
|
|
|
|
}
|
2019-03-10 17:58:33 +00:00
|
|
|
}
|