QElectroTech  0.70
inditextpropertieswidget.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 */
19 #include "ui_inditextpropertieswidget.h"
20 #include "independenttextitem.h"
22 #include "diagram.h"
23 #include "diagramcommands.h"
24 #include <QtGlobal>
25 #include <QLineEdit>
26 
33  PropertiesEditorWidget(parent),
35 {
36  ui->setupUi(this);
37  if (text) {
38  setText(text);
39  }
40 }
41 
47 IndiTextPropertiesWidget::IndiTextPropertiesWidget(QList<IndependentTextItem *> text_list, QWidget *parent) :
48  PropertiesEditorWidget (parent),
50 {
51  ui->setupUi(this);
52  setText(text_list);
53 }
54 
59  delete ui;
60 }
61 
67 {
68  if (m_text) {
69  for (QMetaObject::Connection c : m_connect_list) {
70  disconnect(c);
71  }
72  }
73 
74  m_text = text;
75  m_connect_list.clear();
76  m_connect_list << connect(m_text.data(), &IndependentTextItem::xChanged, this, &IndiTextPropertiesWidget::updateUi);
77  m_connect_list << connect(m_text.data(), &IndependentTextItem::yChanged, this, &IndiTextPropertiesWidget::updateUi);
78  m_connect_list << connect(m_text.data(), &IndependentTextItem::rotationChanged, this, &IndiTextPropertiesWidget::updateUi);
81 
82  updateUi();
83 }
84 
85 void IndiTextPropertiesWidget::setText(QList<IndependentTextItem *> text_list)
86 {
87  for (QMetaObject::Connection c : m_connect_list) {
88  disconnect(c);
89  }
90  m_connect_list.clear();
91  m_text_list.clear();
92  m_text = nullptr;
93 
94  if (text_list.size() == 0) {
95  updateUi();
96  }
97  else if (text_list.size() == 1)
98  {
99  setText(text_list.first());
100  m_text_list.clear();
101  }
102  else
103  {
104  for (IndependentTextItem *iti : text_list) {
105  m_text_list.append(QPointer<IndependentTextItem>(iti));
106  }
107  updateUi();
108  }
109 }
110 
117 {
118  Diagram *d = nullptr;
119 
120  if (m_text && m_text->diagram()) {
121  d = m_text->diagram();
122  } else if (!m_text_list.isEmpty()) {
123  for (QPointer<IndependentTextItem> piti : m_text_list) {
124  if (piti->diagram()) {
125  d = piti->diagram();
126  break;
127  }
128  }
129  }
130 
131  if (d)
132  {
133  QUndoCommand *undo = associatedUndo();
134  if (undo) {
135  d->undoStack().push(undo);
136  }
137  }
138 }
139 
146 {
147  if (m_live_edit == live_edit) {
148  return true;
149  }
150  m_live_edit = live_edit;
151 
152  if (m_live_edit) {
154  }
155  else {
156  for (QMetaObject::Connection c : m_edit_connection) {
157  disconnect(c);
158  }
159  m_edit_connection.clear();
160  }
161  return true;
162 }
163 
169 {
170  if (m_live_edit)
171  {
172  QPropertyUndoCommand *undo = nullptr;
173  //One text is edited
174  if (m_text_list.isEmpty())
175  {
176  if(ui->m_x_sb->value() != m_text->pos().x()) {
177  undo = new QPropertyUndoCommand(m_text.data(), "x", QVariant(m_text->pos().x()), QVariant(ui->m_x_sb->value()));
178  undo->setAnimated(true, false);
179  undo->setText(tr("Déplacer un champ texte"));
180  }
181  if(ui->m_y_sb->value() != m_text->pos().y()) {
182  undo = new QPropertyUndoCommand(m_text.data(), "y", QVariant(m_text->pos().y()), QVariant(ui->m_y_sb->value()));
183  undo->setAnimated(true, false);
184  undo->setText(tr("Déplacer un champ texte"));
185  }
186  if(ui->m_angle_sb->value() != m_text->rotation()) {
187  undo = new QPropertyUndoCommand(m_text.data(), "rotation", QVariant(m_text->rotation()), QVariant(ui->m_angle_sb->value()));
188  undo->setAnimated(true, false);
189  undo->setText(tr("Pivoter un champ texte"));
190  }
191  if (ui->m_line_edit->text() != m_text->toPlainText()) {
192  undo = new QPropertyUndoCommand(m_text.data(), "plainText", m_text->toPlainText(), ui->m_line_edit->text());
193  undo->setText(tr("Modifier un champ texte"));
194  }
195  if (ui->m_size_sb->value() != m_text->font().pointSize()) {
196  QFont font = m_text->font();
197  font.setPointSize(ui->m_size_sb->value());
198  undo = new QPropertyUndoCommand(m_text.data(), "font", m_text->font(), font);
199  undo->setText(tr("Modifier la taille d'un champ texte"));
200  }
201  if (m_font_is_selected &&
202  m_selected_font != m_text->font()) {
203  undo = new QPropertyUndoCommand(m_text.data(), "font", m_text->font(), m_selected_font);
204  undo->setText(tr("Modifier la police d'un champ texte"));
205  }
206 
207  return undo;
208  }
209  else //several text are edited, only size and rotation is available for edition
210  {
211  QUndoCommand *parent_undo = nullptr;
212  bool size_equal = true;
213  bool angle_equal = true;
214  bool font_equal = true;
215  qreal rotation_ = m_text_list.first()->rotation();
216  int size_ = m_text_list.first()->font().pointSize();
217  QFont font_ = m_text_list.first()->font();
218  for (QPointer<IndependentTextItem> piti : m_text_list)
219  {
220  if (piti->rotation() != rotation_) {
221  angle_equal = false;
222  }
223  if (piti->font().pointSize() != size_) {
224  size_equal = false;
225  }
226  if (piti->font() != font_) {
227  font_equal = false;
228  }
229  }
230 
231  if ((angle_equal && (ui->m_angle_sb->value() != rotation_)) ||
232  (!angle_equal && (ui->m_angle_sb->value() != ui->m_angle_sb->minimum())))
233  {
234  for (QPointer<IndependentTextItem> piti : m_text_list)
235  {
236  if (piti)
237  {
238  if (!parent_undo) {
239  parent_undo = new QUndoCommand(tr("Pivoter plusieurs champs texte"));
240  }
241  QPropertyUndoCommand *qpuc = new QPropertyUndoCommand(piti.data(), "rotation", QVariant(piti->rotation()), QVariant(ui->m_angle_sb->value()), parent_undo);
242  qpuc->setAnimated(true, false);
243  }
244  }
245  }
246  else if ((size_equal && (ui->m_size_sb->value() != size_)) ||
247  (!size_equal && (ui->m_size_sb->value() != ui->m_size_sb->minimum())))
248  {
249  for (QPointer<IndependentTextItem> piti : m_text_list)
250  {
251  if (piti)
252  {
253  if (!parent_undo) {
254  parent_undo = new QUndoCommand(tr("Modifier la taille de plusieurs champs texte"));
255  }
256  QFont font = piti->font();
257  font.setPointSize(ui->m_size_sb->value());
258  new QPropertyUndoCommand(piti.data(), "font", QVariant(piti->font()), QVariant(font), parent_undo);
259  }
260  }
261  }
262  else if ((m_font_is_selected && !font_equal) ||
263  (m_font_is_selected && (font_equal && (m_selected_font != font_))))
264  {
265  for (QPointer<IndependentTextItem> piti : m_text_list)
266  {
267  if (piti)
268  {
269  if (!parent_undo) {
270  parent_undo = new QUndoCommand(tr("Modifier la police de plusieurs champs texte"));
271  }
272  new QPropertyUndoCommand(piti.data(), "font", piti->font(), m_selected_font, parent_undo);
273  }
274  }
275  }
276  return parent_undo;
277  }
278  }
279  //In mode not live edit, only one text can be edited
280  else if (m_text_list.isEmpty())
281  {
282  QUndoCommand *undo = new QUndoCommand(tr("Modifier les propriétés d'un texte"));
283  if(ui->m_x_sb->value() != m_text->pos().x()) {
284  new QPropertyUndoCommand(m_text.data(), "x", QVariant(m_text->pos().x()), QVariant(ui->m_x_sb->value()), undo);
285  }
286  if(ui->m_y_sb->value() != m_text->pos().y()) {
287  new QPropertyUndoCommand(m_text.data(), "y", QVariant(m_text->pos().y()), QVariant(ui->m_y_sb->value()), undo);
288  }
289  if(ui->m_angle_sb->value() != m_text->rotation()) {
290  new QPropertyUndoCommand(m_text.data(), "rotation", QVariant(m_text->rotation()), QVariant(ui->m_angle_sb->value()), undo);
291  }
292  if (ui->m_line_edit->text() != m_text->toPlainText()) {
293  new ChangeDiagramTextCommand(m_text.data(), m_text->toHtml(), ui->m_line_edit->text(), undo);
294  }
295  if (ui->m_size_sb->value() != m_text->font().pointSize())
296  {
297  QFont font = m_text->font();
298  font.setPointSize(ui->m_size_sb->value());
299  new QPropertyUndoCommand(m_text.data(), "font", m_text->font(), font, undo);
300  }
301  if (m_font_is_selected && m_selected_font != m_text->font()) {
302  new QPropertyUndoCommand(m_text.data(), "font", m_text->font(), m_selected_font, undo);
303  }
304 
305  if (undo->childCount()) {
306  return undo;
307  } else {
308  return nullptr;
309  }
310  }
311  else {
312  return nullptr;
313  }
314 }
315 
321 {
322  for (QMetaObject::Connection c : m_edit_connection) {
323  disconnect(c);
324  }
325  m_edit_connection.clear();
326 
327  if (m_text_list.isEmpty())
328  {
329  m_edit_connection << connect(ui->m_x_sb, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &IndiTextPropertiesWidget::apply);
330  m_edit_connection << connect(ui->m_y_sb, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &IndiTextPropertiesWidget::apply);
331  m_edit_connection << connect(ui->m_line_edit, &QLineEdit::textEdited, this, &IndiTextPropertiesWidget::apply);
332  }
333  m_edit_connection << connect(ui->m_angle_sb, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &IndiTextPropertiesWidget::apply);
334  m_edit_connection << connect(ui->m_size_sb, QOverload<int>::of(&QSpinBox::valueChanged), [this]()
335  {
336  this->m_selected_font.setPointSize(ui->m_size_sb->value());
337  this->apply();
338  });
339 }
340 
345 {
346  if (!m_text && m_text_list.isEmpty()) {
347  return;
348  }
349 
350  //Disconnect every connections of editor widgets
351  //to avoid an unwanted edition (QSpinBox emit valueChanged no matter if changer by user or by program)
352  for (QMetaObject::Connection c : m_edit_connection) {
353  disconnect(c);
354  }
355  m_edit_connection.clear();
356 
357  ui->m_x_sb->setEnabled(m_text_list.isEmpty() ? true : false);
358  ui->m_y_sb->setEnabled(m_text_list.isEmpty() ? true : false);
359  ui->m_line_edit->setEnabled(m_text_list.isEmpty() ? true : false);
360  ui->m_advanced_editor_pb->setEnabled(m_text_list.isEmpty() ? true : false);
361 
362  if (m_text_list.isEmpty())
363  {
364  ui->m_x_sb->setValue(m_text->pos().x());
365  ui->m_y_sb->setValue(m_text->pos().y());
366  ui->m_line_edit->setText(m_text->toPlainText());
367  ui->m_angle_sb->setValue(m_text->rotation());
368  ui->m_size_sb->setValue(m_text->font().pointSize());
369 
370  ui->m_line_edit->setDisabled(m_text->isHtml() ? true : false);
371  ui->m_size_sb->setDisabled(m_text->isHtml() ? true : false);
372  ui->m_label->setVisible(m_text->isHtml() ? true : false);
373  ui->m_break_html_pb->setVisible(m_text->isHtml() ? true : false);
374  ui->m_font_pb->setDisabled(m_text->isHtml() ? true : false);
375  ui->m_font_pb->setText(m_text->isHtml() ? tr("Police") : m_text->font().family());
376  }
377  else
378  {
379  bool size_equal = true;
380  bool angle_equal = true;
381  bool font_equal = true;
382  qreal rotation_ = m_text_list.first()->rotation();
383  int size_ = m_text_list.first()->font().pointSize();
384  QFont font_ = m_text_list.first()->font();
385 
386  for (QPointer<IndependentTextItem> piti : m_text_list)
387  {
388  if (piti->rotation() != rotation_) {
389  angle_equal = false;
390  }
391  if (piti->font().pointSize() != size_) {
392  size_equal = false;
393  }
394  if (piti->font() != font_) {
395  font_equal = false;
396  }
397  }
398  ui->m_angle_sb->setValue(angle_equal ? rotation_ : 0);
399 
400  bool valid_ = true;
401  for (QPointer<IndependentTextItem> piti : m_text_list) {
402  if (piti->isHtml()) {
403  valid_ = false;
404  }
405  }
406  ui->m_font_pb->setEnabled(valid_);
407  ui->m_font_pb->setText(font_equal ? font_.family() : tr("Police"));
408  ui->m_size_sb->setEnabled(valid_);
409  ui->m_size_sb->setValue(size_equal ? size_ : 0);
410  ui->m_label->setVisible(false);
411  ui->m_break_html_pb->setVisible(true);
412  }
413 
414 
415  //Set the connection now
417 }
418 
423  if (m_text) {
424  m_text->edit();
425  }
426 }
427 
429 {
430  if (m_text) {
431  m_text->setPlainText(m_text->toPlainText());
432  }
433  for (QPointer<IndependentTextItem> piti : m_text_list) {
434  piti->setPlainText(piti->toPlainText());
435  }
436 
437  updateUi();
438 }
439 
441 {
442  if (!m_text && m_text_list.isEmpty()) {
443  return;
444  }
445  bool ok;
446  QFont font = m_text ? m_text->font() : m_text_list.first()->font();
447  m_selected_font = QFontDialog::getFont(&ok, font, this);
448  if (ok) {
449  m_font_is_selected = true;
450  ui->m_font_pb->setText(font.family());
451  ui->m_size_sb->setValue(font.pointSize());
452  apply();
453  } else {
454  ui->m_font_pb->setText(tr("Police"));
455  m_font_is_selected = false;
456  }
457 }
void fontChanged(QFont font)
The QPropertyUndoCommand class This undo command manage QProperty of a QObject. This undo command can...
void textEdited(const QString &old_str, const QString &new_str)
QPointer< IndependentTextItem > m_text
void updateUi() override
IndiTextPropertiesWidget::updateUi.
The PropertiesEditorWidget class This class extend QWidget method for have common way to edit propert...
bool setLiveEdit(bool live_edit) override
IndiTextPropertiesWidget::setLiveEdit.
Ui::IndiTextPropertiesWidget * ui
QIcon tr
Definition: qeticons.cpp:204
QList< QMetaObject::Connection > m_connect_list
IndiTextPropertiesWidget(IndependentTextItem *text=nullptr, QWidget *parent=nullptr)
IndiTextPropertiesWidget::IndiTextPropertiesWidget.
void setUpEditConnection()
IndiTextPropertiesWidget::setUpEditConnection Disconnect the previous connection, and reconnect the c...
void setText(IndependentTextItem *text)
IndiTextPropertiesWidget::setText.
QList< QPointer< IndependentTextItem > > m_text_list
QList< QMetaObject::Connection > m_edit_connection
QUndoStack & undoStack()
Definition: diagram.h:337
void apply() override
IndiTextPropertiesWidget::apply Apply the current edition through a QUndoCommand pushed to the undo s...
~IndiTextPropertiesWidget() override
IndiTextPropertiesWidget::~IndiTextPropertiesWidget.
void setAnimated(bool animate=true, bool first_time=true)
QPropertyUndoCommand::setAnimated.
The IndiTextPropertiesWidget class This widget is used to edit the properties of one or several indep...
QUndoCommand * associatedUndo() const override
IndiTextPropertiesWidget::associatedUndo.
void on_m_advanced_editor_pb_clicked()
IndiTextPropertiesWidget::on_m_advanced_editor_pb_clicked.