QElectroTech  0.70
texteditor.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "texteditor.h"
19 #include "ui_texteditor.h"
20 #include "parttext.h"
22 
30 TextEditor::TextEditor(QETElementEditor *editor, PartText *text, QWidget *parent) :
31  ElementItemEditor(editor, parent),
32  ui(new Ui::TextEditor)
33 {
34  ui->setupUi(this);
36  if (text)
37  {
38  setPart(text);
39  updateForm();
40  }
41 }
42 
47  delete ui;
48 }
49 
55 {
56  if (m_text.isNull()) {
57  return;
58  }
59 
60  for (QMetaObject::Connection c : m_edit_connection) {
61  disconnect(c);
62  }
63  m_edit_connection.clear();
64 
65  ui->m_line_edit->setText(m_text->toPlainText());
66  ui->m_x_sb->setValue(m_text->pos().x());
67  ui->m_y_sb->setValue(m_text->pos().y());
68  ui->m_rotation_sb->setValue(m_text->rotation());
69  ui->m_size_sb->setValue(m_text->font().pointSize());
70  ui->m_font_pb->setText(m_text->font().family());
71  ui->m_color_pb->setColor(m_text->defaultTextColor());
72 
74 }
75 
84 {
85  if (!part)
86  {
87  m_text = nullptr;
88  for (QMetaObject::Connection c : m_change_connection) {
89  disconnect(c);
90  }
91  m_change_connection.clear();
92  return true;
93  }
94 
95  if (PartText *part_text = dynamic_cast<PartText *>(part))
96  {
97  if (part_text == m_text) {
98  return true;
99  }
100  m_text = part_text;
101 
102  m_change_connection.clear();
104  m_change_connection << connect(part_text, &PartText::xChanged, this, &TextEditor::updateForm);
105  m_change_connection << connect(part_text, &PartText::yChanged, this, &TextEditor::updateForm);
106  m_change_connection << connect(part_text, &PartText::rotationChanged, this, &TextEditor::updateForm);
107  m_change_connection << connect(part_text, &PartText::fontChanged, this, &TextEditor::updateForm);
108  m_change_connection << connect(part_text, &PartText::colorChanged, this, &TextEditor::updateForm);
109 
110  updateForm();
111  return true;
112  }
113  return false;
114 }
115 
121  return m_text;
122 }
123 
130 {
131  for (QMetaObject::Connection c : m_edit_connection) {
132  disconnect(c);
133  }
134  m_edit_connection.clear();
135 
136  m_edit_connection << connect(ui->m_line_edit, &QLineEdit::textEdited, [this]()
137  {
138  QString text_ = ui->m_line_edit->text();
139  if (text_ != m_text->toPlainText())
140  {
141  QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text, "text", m_text->toPlainText(), text_);
142  undo->setText(tr("Modifier le contenu d'un champ texte"));
143  undoStack().push(undo);
144  }
145  });
146  m_edit_connection << connect(ui->m_x_sb, QOverload<int>::of(&QSpinBox::valueChanged), [this]()
147  {
148  QPointF pos(ui->m_x_sb->value(), ui->m_y_sb->value());
149  if (pos != m_text->pos())
150  {
151  QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text, "pos", m_text->pos(), pos);
152  undo->setText(tr("Déplacer un champ texte"));
153  undo->setAnimated(true, false);
154  undoStack().push(undo);
155  }
156  });
157  m_edit_connection << connect(ui->m_y_sb, QOverload<int>::of(&QSpinBox::valueChanged), [this]()
158  {
159  QPointF pos(ui->m_x_sb->value(), ui->m_y_sb->value());
160  if (pos != m_text->pos())
161  {
162  QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text, "pos", m_text->pos(), pos);
163  undo->setText(tr("Déplacer un champ texte"));
164  undo->setAnimated(true, false);
165  undoStack().push(undo);
166  }
167  });
168  m_edit_connection << connect(ui->m_rotation_sb, QOverload<int>::of(&QSpinBox::valueChanged), [this]()
169  {
170  if (ui->m_rotation_sb->value() != m_text->rotation())
171  {
172  QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text, "rotation", m_text->rotation(), ui->m_rotation_sb->value());
173  undo->setText(tr("Pivoter un champ texte"));
174  undo->setAnimated(true, false);
175  undoStack().push(undo);
176  }
177  });
178  m_edit_connection << connect(ui->m_size_sb, QOverload<int>::of(&QSpinBox::valueChanged), [this]()
179  {
180  if (m_text->font().pointSize() != ui->m_size_sb->value())
181  {
182  QFont font_ = m_text->font();
183  font_.setPointSize(ui->m_size_sb->value());
184  QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text, "font", m_text->font(), font_);
185  undo->setText(tr("Modifier la police d'un texte"));
186  undoStack().push(undo);
187  }
188  });
189 }
190 
195 {
196  bool ok;
197  QFont font_ = QFontDialog::getFont(&ok, m_text->font(), this);
198 
199  if (ok && font_ != m_text->font())
200  {
201  ui->m_size_sb->blockSignals(true);
202  ui->m_size_sb->setValue(font_.pointSize());
203  ui->m_size_sb->blockSignals(false);
204 
205  ui->m_font_pb->setText(font_.family());
206  QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text, "font", m_text->font(), font_);
207  undo->setText(tr("Modifier la police d'un texte"));
208  undoStack().push(undo);
209  }
210 }
211 
216 void TextEditor::on_m_color_pb_changed(const QColor &newColor)
217 {
218  if (newColor != m_text->defaultTextColor())
219  {
220  QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text, "color", m_text->defaultTextColor(), newColor);
221  undo->setText(tr("Modifier la couleur d'un texte"));
222  undoStack().push(undo);
223  }
224 }
The QPropertyUndoCommand class This undo command manage QProperty of a QObject. This undo command can...
QList< QMetaObject::Connection > m_edit_connection
Definition: texteditor.h:54
void plainTextChanged(const QString &text)
void on_m_color_pb_changed(const QColor &newColor)
TextEditor::on_m_color_pb_changed.
Definition: texteditor.cpp:216
void colorChanged(const QColor &color)
QPointer< PartText > m_text
Definition: texteditor.h:53
bool setPart(CustomElementPart *part) override
TextEditor::setPart Set the current text to edit. Set to nullptr to clear the current text...
Definition: texteditor.cpp:83
QIcon tr
Definition: qeticons.cpp:204
~TextEditor() override
TextEditor::~TextEditor.
Definition: texteditor.cpp:46
TextEditor(QETElementEditor *editor, PartText *text=nullptr, QWidget *parent=nullptr)
TextEditor::TextEditor Default constructor.
Definition: texteditor.cpp:30
void on_m_font_pb_clicked()
TextEditor::on_m_font_pb_clicked.
Definition: texteditor.cpp:194
void updateForm() override
TextEditor::updateForm Update the gui.
Definition: texteditor.cpp:54
void fontChanged(const QFont &font)
CustomElementPart * currentPart() const override
TextEditor::currentPart.
Definition: texteditor.cpp:120
Ui::TextEditor * ui
Definition: texteditor.h:51
virtual QUndoStack & undoStack() const
void setUpEditConnection()
TextEditor::setUpEditConnection Setup the connection between the widgets of this editor and the undo ...
Definition: texteditor.cpp:129
QList< QMetaObject::Connection > m_change_connection
Definition: texteditor.h:55