QElectroTech  0.70
dynamictextfieldeditor.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 "dynamictextfieldeditor.h"
19 #include "ui_dynamictextfieldeditor.h"
20 #include "customelementpart.h"
21 #include "partdynamictextfield.h"
23 #include "qetelementeditor.h"
24 #include "qetapp.h"
26 #include "alignmenttextdialog.h"
27 
28 #include <QPointer>
29 #include <QGraphicsItem>
30 #include <QColorDialog>
31 
33  ElementItemEditor(editor, parent),
34  ui(new Ui::DynamicTextFieldEditor)
35 {
36  ui->setupUi(this);
37  ui->m_composite_text_pb->setDisabled(true);
38  ui->m_elmt_info_cb->setDisabled(true);
39  if(text_field)
40  setPart(text_field);
42 }
43 
45 {
46  delete ui;
47  if(!m_connection_list.isEmpty())
48  for(const QMetaObject::Connection& con : m_connection_list)
49  disconnect(con);
50 }
51 
59 {
60  //Remove previous connection
61  if(!m_connection_list.isEmpty())
62  for(const QMetaObject::Connection& con : m_connection_list)
63  disconnect(con);
64 
65  QGraphicsItem *qgi = part->toItem();
66  if(!qgi)
67  return false;
68  else if (qgi->type() != PartDynamicTextField::Type)
69  return false;
70 
71  m_text_field = static_cast<PartDynamicTextField *>(qgi);
72  updateForm();
73 
74  //Setup the connection
75  m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::colorChanged, [this](){this->updateForm();});
76  m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::fontChanged, [this](){this->updateForm();});
77  m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::taggChanged, [this](){this->updateForm();});
78  m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::textFromChanged, [this](){this->updateForm();});
79  m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::textChanged, [this](){this->updateForm();});
80  m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::infoNameChanged, [this](){this->updateForm();});
81  m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::rotationChanged, [this](){this->updateForm();});
82  m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::frameChanged, [this](){this->updateForm();});
83  m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::textWidthChanged,[this]() {this->updateForm();});
84  m_connection_list << connect(m_text_field.data(), &PartDynamicTextField::compositeTextChanged, [this]() {this->updateForm();});
85 
86  return true;
87 }
88 
95  return m_text_field.data();
96 }
97 
99 {
100  if(m_text_field)
101  {
102  ui->m_x_sb->setValue(m_text_field.data()->x());
103  ui->m_y_sb->setValue(m_text_field.data()->y());
104  ui->m_rotation_sb->setValue(QET::correctAngle(m_text_field.data()->rotation()));
105  ui->m_frame_cb->setChecked(m_text_field.data()->frame());
106  ui->m_user_text_le->setText(m_text_field.data()->text());
107  ui->m_size_sb->setValue(m_text_field.data()->font().pointSize());
108  ui->m_color_kpb->setColor(m_text_field.data()->color());
109  ui->m_width_sb->setValue(m_text_field.data()->textWidth());
110  ui->m_font_pb->setText(m_text_field->font().family());
111 
112  switch (m_text_field.data()->textFrom())
113  {
115  ui->m_text_from_cb->setCurrentIndex(0);
116  break;
118  {
119  ui->m_text_from_cb->setCurrentIndex(1);
120  ui->m_elmt_info_cb->setCurrentIndex(ui->m_elmt_info_cb->findData(m_text_field.data()->infoName()));
121  break;
122  }
124  ui->m_text_from_cb->setCurrentIndex(2);
125  }
126 
127  on_m_text_from_cb_activated(ui->m_text_from_cb->currentIndex()); //For enable the good widget
128  }
129 }
130 
136 {
137  ui->m_elmt_info_cb->clear();
138 
139  QStringList strl;
140  QString type = elementEditor()->elementScene()->elementType();
141 
142  if(type.contains("report"))
143  strl << "function" << "tension-protocol";
144  else
145  strl = QETApp::elementInfoKeys();
146 
147  //We use a QMap because the keys of the map are sorted, then no matter the curent local,
148  //the value of the combo box are always alphabetically sorted
149  QMap <QString, QString> info_map;
150  for(const QString& str : strl)
151  info_map.insert(QETApp::elementTranslatedInfoKey(str), str);
152 
153  for (const QString& key : info_map.keys())
154  ui->m_elmt_info_cb->addItem(key, info_map.value(key));
155 }
156 
158 {
159  QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text_field, "x", m_text_field.data()->x(), ui->m_x_sb->value());
160  undo->setText(tr("Déplacer un champ texte"));
161  undo->enableAnimation(true);
162  undoStack().push(undo);
163 }
164 
166 {
167  QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text_field, "y", m_text_field.data()->y(), ui->m_y_sb->value());
168  undo->setText(tr("Déplacer un champ texte"));
169  undo->enableAnimation(true);
170  undoStack().push(undo);
171 }
172 
174 {
175  QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text_field, "rotation", m_text_field.data()->rotation(), ui->m_rotation_sb->value());
176  undo->setText(tr("Pivoter un champ texte"));
177  undo->enableAnimation(true);
178  undoStack().push(undo);
179 }
180 
182 {
183  QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text_field, "text", m_text_field.data()->text(), ui->m_user_text_le->text());
184  undo->setText(tr("Modifier le texte d'un champ texte"));
185  undoStack().push(undo);
186 }
187 
189 {
190  QFont font_ = m_text_field->font();
191  font_.setPointSize(ui->m_size_sb->value());
192  QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text_field, "font", m_text_field.data()->font(), font_);
193  undo->setText(tr("Modifier la police d'un champ texte"));
194  undoStack().push(undo);
195 }
196 
198 {
199  bool frame = ui->m_frame_cb->isChecked();
200 
201  if(frame != m_text_field.data()->frame())
202  {
203  QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text_field, "frame", m_text_field.data()->frame(), frame);
204  undo->setText(tr("Modifier le cadre d'un champ texte"));
205  undoStack().push(undo);
206  }
207 }
208 
210 {
211  qreal width = (qreal)ui->m_width_sb->value();
212 
213  if(width != m_text_field.data()->textWidth())
214  {
215  QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text_field, "textWidth", m_text_field.data()->textWidth(), width);
216  undo->setText(tr("Modifier la largeur d'un texte"));
217  undoStack().push(undo);
218  }
219 }
220 
222 {
223  Q_UNUSED(arg1)
224 
225  QString info = ui->m_elmt_info_cb->currentData().toString();
226 
227  if(info != m_text_field.data()->infoName())
228  {
229  QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text_field, "infoName", m_text_field.data()->infoName(), info);
230  undo->setText(tr("Modifier l'information d'un texte"));
231  undoStack().push(undo);
232 
233  m_text_field.data()->setPlainText(elementEditor()->elementScene()->elementInformation().value(m_text_field.data()->infoName()).toString());
234  }
235 }
236 
238 {
239  ui->m_user_text_le->setDisabled(true);
240  ui->m_elmt_info_cb->setDisabled(true);
241  ui->m_composite_text_pb->setDisabled(true);
242 
243  if(index == 0)
244  ui->m_user_text_le->setEnabled(true);
245  else if (index == 1)
246  ui->m_elmt_info_cb->setEnabled(true);
247  else
248  ui->m_composite_text_pb->setEnabled(true);
249 
251  if(index == 0) tf = DynamicElementTextItem::UserText;
252  else if(index == 1) tf = DynamicElementTextItem::ElementInfo;
254 
255  if(tf != m_text_field.data()->textFrom())
256  {
257  QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text_field, "textFrom", m_text_field.data()->textFrom(), tf);
258  undo->setText(tr("Modifier la source de texte, d'un texte"));
259  undoStack().push(undo);
260  }
261 }
262 
264 {
265  CompositeTextEditDialog ctd(m_text_field.data()->compositeText(), this);
266  if(ctd.exec())
267  {
268  QString ct = ctd.plainText();
269  if(ct != m_text_field.data()->compositeText())
270  {
271  QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text_field.data(), "compositeText", m_text_field.data()->compositeText(), ctd.plainText());
272  undoStack().push(undo);
273  }
274  }
275 }
276 
278 {
279  AlignmentTextDialog atd(m_text_field.data()->alignment(), this);
280  atd.exec();
281 
282  if(atd.alignment() != m_text_field.data()->alignment())
283  {
284  QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text_field.data(), "alignment", QVariant(m_text_field.data()->alignment()), QVariant(atd.alignment()));
285  undo->setText(tr("Modifier l'alignement d'un champ texte"));
286  undoStack().push(undo);
287  }
288 }
289 
291 {
292  bool ok;
293  QFont font_ = QFontDialog::getFont(&ok, m_text_field->font(), this);
294  if (ok && font_ != this->font())
295  {
296  ui->m_font_pb->setText(font_.family());
297  ui->m_size_sb->setValue(font_.pointSize());
298 
299  QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text_field.data(), "font", m_text_field->font(), font_);
300  undo->setText(tr("Modifier la police d'un champ texte"));
301  undoStack().push(undo);
302  }
303 }
304 
306 {
307  if(newColor.isValid() && newColor != m_text_field.data()->color())
308  {
309  QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_text_field, "color", m_text_field.data()->color(), newColor);
310  undo->setText(tr("Modifier la couleur d'un champ texte"));
311  undoStack().push(undo);
312  }
313 }
The QPropertyUndoCommand class This undo command manage QProperty of a QObject. This undo command can...
The CompositeTextEditDialog class CompositeTextEditDialog display a dialog use to write the complex t...
void on_m_color_kpb_changed(const QColor &newColor)
void on_m_elmt_info_cb_activated(const QString &arg1)
void compositeTextChanged(QString text)
DynamicTextFieldEditor(QETElementEditor *editor, PartDynamicTextField *text_field=nullptr, QWidget *parent=nullptr)
void colorChanged(QColor color)
The DynamicTextFieldEditor class This class provide a widget used to edit the property of a dynamic t...
The PartDynamicTextField class This class represents an editable dynamic text field which may be used...
void textChanged(QString text)
void fillInfoComboBox()
DynamicTextFieldEditor::fillInfoComboBox Fill the combo box "element information".
void enableAnimation(bool animate=true)
QPropertyUndoCommand::enableAnimation True to enable animation.
ElementScene * elementScene() const
virtual QGraphicsItem * toItem()
void fontChanged(QFont font)
Ui::DynamicTextFieldEditor * ui
qreal correctAngle(const qreal &)
Definition: qet.cpp:505
QList< QMetaObject::Connection > m_connection_list
void textWidthChanged(qreal width)
QIcon tr
Definition: qeticons.cpp:204
bool setPart(CustomElementPart *part) override
DynamicTextFieldEditor::setPart Set as current edited part of this widget.
static QStringList elementInfoKeys()
QETApp::elementInfoKeys.
Definition: qetapp.cpp:289
void taggChanged(QString tagg)
void textFromChanged(DynamicElementTextItem::TextFrom text_from)
virtual QETElementEditor * elementEditor() const
void infoNameChanged(QString info)
void on_m_text_from_cb_activated(int index)
virtual ElementScene * elementScene() const
QString elementType() const
Definition: elementscene.h:100
QPointer< PartDynamicTextField > m_text_field
static QString elementTranslatedInfoKey(const QString &)
ElementsProperties::translatedInfo Return the translated information key given by If don&#39;t match...
Definition: qetapp.cpp:321
void frameChanged(bool frame)
CustomElementPart * currentPart() const override
DynamicTextFieldEditor::currentPart.
QString plainText() const
CompositeTextEditDialog::plainText.
virtual QUndoStack & undoStack() const