QElectroTech  0.70
shapegraphicsitempropertieswidget.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_shapegraphicsitempropertieswidget.h"
20 #include "qetshapeitem.h"
21 #include "diagram.h"
23 
31  PropertiesEditorWidget(parent),
33  m_shape(nullptr)
34 {
35  ui->setupUi(this);
36  setItem(item);
37 }
38 
39 ShapeGraphicsItemPropertiesWidget::ShapeGraphicsItemPropertiesWidget(QList<QetShapeItem *> items_list, QWidget *parent) :
40  PropertiesEditorWidget (parent),
42 {
43  ui->setupUi(this);
44  setItems(items_list);
45 }
46 
52 {
53  delete ui;
54 }
55 
62 {
63  if (m_shape != shape)
64  {
65  for (QMetaObject::Connection c : m_connect_list) {
66  disconnect(c);
67  }
68 
69  m_connect_list.clear();
70  }
71  if (!shape) {
72  return;
73  }
74 
75  m_shape = shape;
76  ui->m_close_polygon->setVisible(m_shape->shapeType() == QetShapeItem::Polygon);
77  ui->m_filling_gb->setHidden(m_shape->shapeType() == QetShapeItem::Line);
78 
79  if (m_live_edit)
80  {
84  }
85 
87  updateUi();
88 }
89 
95 void ShapeGraphicsItemPropertiesWidget::setItems(QList<QetShapeItem *> shapes_list)
96 {
97  for (QMetaObject::Connection c : m_connect_list) {
98  disconnect(c);
99  }
100  m_connect_list.clear();
101  m_shapes_list.clear();
102  m_shape = nullptr;
103 
104  if (shapes_list.size() == 0) {
105  updateUi();
106  }
107  else if (shapes_list.size() == 1)
108  {
109  setItem(shapes_list.first());
110  }
111  else
112  {
113  for (QetShapeItem *shape : shapes_list) {
114  m_shapes_list.append(QPointer<QetShapeItem>(shape));
115  }
116  updateUi();
117  }
119 }
120 
127 {
128  Diagram *d = nullptr;
129 
130  if (m_shape && m_shape->diagram()) {
131  d = m_shape->diagram();
132  }
133  else if (!m_shapes_list.isEmpty())
134  {
135  for (QPointer<QetShapeItem> qsi : m_shapes_list)
136  {
137  if (qsi->diagram()) {
138  d = qsi->diagram();
139  break;
140  }
141  }
142  }
143 
144  if (d)
145  {
146  QUndoCommand *undo = associatedUndo();
147  if (undo) {
148  d->undoStack().push(undo);
149  }
150  }
151 }
152 
158  updateUi();
159 }
160 
168 {
169  if (m_live_edit)
170  {
171  //One shape is edited
172  if (m_shapes_list.isEmpty())
173  {
174  QPropertyUndoCommand *undo = nullptr;
175 
176  QPen old_pen = m_shape->pen();
177  QPen new_pen = old_pen;
178 
179  new_pen.setStyle(Qt::PenStyle(ui->m_style_cb->currentIndex() + 1));
180  new_pen.setWidthF(ui->m_size_dsb->value());
181 
182  if (ui->m_style_cb->currentIndex() ==5) {
183  new_pen.setDashPattern( QVector<qreal>() << 10 << 10 );
184  new_pen.setStyle( Qt::CustomDashLine );
185  }
186  //painter.setPen( new_pen );
187  new_pen.setColor(ui->m_color_kpb->color());
188 
189  if (new_pen != old_pen)
190  {
191  undo = new QPropertyUndoCommand(m_shape, "pen", old_pen, new_pen);
192  undo->setText(tr("Modifier le trait d'une forme"));
193  }
194 
195  QBrush old_brush = m_shape->brush();
196  QBrush new_brush = old_brush;
197  new_brush.setStyle(Qt::BrushStyle(ui->m_brush_style_cb->currentIndex()));
198  new_brush.setColor(ui->m_brush_color_kpb->color());
199 
200  if (new_brush != old_brush)
201  {
202  if (undo)
203  new QPropertyUndoCommand(m_shape, "brush", old_brush, new_brush, undo);
204  else
205  {
206  undo = new QPropertyUndoCommand(m_shape, "brush", old_brush, new_brush);
207  undo->setText(tr("Modifier le remplissage d'une forme"));
208  }
209  }
210 
211  if (ui->m_close_polygon->isChecked() != m_shape->isClosed())
212  {
213  if (undo)
214  new QPropertyUndoCommand(m_shape, "close", m_shape->isClosed(), ui->m_close_polygon->isChecked(), undo);
215  else
216  {
217  undo = new QPropertyUndoCommand(m_shape, "close", m_shape->isClosed(), ui->m_close_polygon->isChecked(), undo);
218  undo->setText(tr("Fermer le polygone"));
219  }
220  }
221 
222  return undo;
223  }
224  else if (!m_shapes_list.isEmpty()) //seberal shapes are edited
225  {
226  QUndoCommand *parent_undo = nullptr;
227  QetShapeItem *shape_ = m_shapes_list.first().data();
228 
229  //Pen
230  QHash <QetShapeItem *, QPen> pen_H;
231 
232  if (ui->m_style_cb->currentIndex() != -1 &&
233  Qt::PenStyle(ui->m_style_cb->currentIndex() + 1) != shape_->pen().style())
234  {
235  for (QPointer<QetShapeItem> qsi : m_shapes_list)
236  {
237  QPen pen = qsi->pen();
238 
239  if (ui->m_style_cb->currentIndex() ==5) {
240  pen.setDashPattern( QVector<qreal>() << 10 << 10 );
241  pen.setStyle( Qt::CustomDashLine );
242  } else {
243  pen.setStyle(Qt::PenStyle(ui->m_style_cb->currentIndex() + 1));
244  }
245  pen_H.insert(qsi, pen);
246  }
247  }
248 
249  if (ui->m_size_dsb->value() > 0 &&
250  ui->m_size_dsb->value() != shape_->pen().widthF())
251  {
252  for (QPointer<QetShapeItem> qsi : m_shapes_list)
253  {
254  QPen pen = pen_H.contains(qsi) ? pen_H.value(qsi) : qsi->pen();
255  pen.setWidthF(ui->m_size_dsb->value());
256  pen_H.insert(qsi, pen);
257  }
258  }
259 
260  QColor c =ui->m_color_kpb->color();
261  if (c != QPalette().color(QPalette::Button) && shape_->pen().color() != c)
262  {
263  for (QPointer<QetShapeItem> qsi : m_shapes_list)
264  {
265  QPen pen = pen_H.contains(qsi) ? pen_H.value(qsi) : qsi->pen();
266  pen.setColor(c);
267  pen_H.insert(qsi, pen);
268  }
269  }
270 
271  for (QPointer<QetShapeItem> qsi : pen_H.keys())
272  {
273  if (!parent_undo) {
274  parent_undo = new QUndoCommand(tr("Modifier une forme simple"));
275  }
276  new QPropertyUndoCommand(qsi, "pen", qsi->pen(), pen_H.value(qsi), parent_undo);
277  }
278 
279  //Brush
280  QHash <QetShapeItem *, QBrush> brush_H;
281  if (ui->m_brush_style_cb->currentIndex() != -1 &&
282  shape_->brush().style() != Qt::BrushStyle(ui->m_brush_style_cb->currentIndex()))
283  {
284  for (QPointer<QetShapeItem> qsi : m_shapes_list)
285  {
286  QBrush brush = qsi->brush();
287  brush.setStyle(Qt::BrushStyle(ui->m_brush_style_cb->currentIndex()));
288  brush_H.insert(qsi, brush);
289  }
290  }
291 
292  c = ui->m_brush_color_kpb->color();
293  if (c != QPalette().color(QPalette::Button) && shape_->brush().color() != c)
294  {
295  for (QPointer<QetShapeItem> qsi : m_shapes_list)
296  {
297  QBrush brush = brush_H.contains(qsi) ? brush_H.value(qsi) : qsi->brush();
298  brush.setColor(c);
299  brush_H.insert(qsi, brush);
300  }
301  }
302 
303  for (QPointer<QetShapeItem> qsi : brush_H.keys())
304  {
305  if (!parent_undo) {
306  parent_undo = new QUndoCommand(tr("Modifier une forme simple"));
307  }
308 
309  new QPropertyUndoCommand(qsi, "brush", qsi->brush(), brush_H.value(qsi), parent_undo);
310  }
311 
312  return parent_undo;
313  }
314  }
315  //In mode not live edit, only one shape can be edited
316  else if (m_shapes_list.isEmpty())
317  {
318  QUndoCommand *undo = new QUndoCommand(tr("Modifier les propriétés d'une forme simple"));
319  QPen old_pen = m_shape->pen();
320  QPen new_pen = old_pen;
321 
322  new_pen.setStyle(Qt::PenStyle(ui->m_style_cb->currentIndex() + 1));
323  new_pen.setWidthF(ui->m_size_dsb->value());
324 
325  if (ui->m_style_cb->currentIndex() ==5) {
326  new_pen.setDashPattern( QVector<qreal>() << 10 << 10 );
327  new_pen.setStyle( Qt::CustomDashLine );
328  }
329  //painter.setPen( new_pen );
330  new_pen.setColor(ui->m_color_kpb->color());
331 
332  if (new_pen != old_pen) {
333  new QPropertyUndoCommand(m_shape, "pen", old_pen, new_pen, undo);
334  }
335 
336  QBrush old_brush = m_shape->brush();
337  QBrush new_brush = old_brush;
338  new_brush.setStyle(Qt::BrushStyle(ui->m_brush_style_cb->currentIndex()));
339  new_brush.setColor(ui->m_brush_color_kpb->color());
340 
341  if (new_brush != old_brush) {
342  new QPropertyUndoCommand(m_shape, "brush", old_brush, new_brush, undo);
343  }
344 
345  if (ui->m_close_polygon->isChecked() != m_shape->isClosed()) {
346  QPropertyUndoCommand(m_shape, "close", m_shape->isClosed(), ui->m_close_polygon->isChecked(), undo);
347  }
348 
349  if (undo->childCount()) {
350  return undo;
351  } else {
352  delete undo;
353  return nullptr;
354  }
355  }
356  return nullptr;
357 }
358 
363 {
364  if (!m_shape && m_shapes_list.isEmpty()) {
365  return;
366  }
367 
368  //Disconnect every connections of editor widgets
369  //to avoid an unwanted edition (QSpinBox emit valueChanged no matter if changer by user or by program)
370  for (QMetaObject::Connection c : m_edit_connection) {
371  disconnect(c);
372  }
373  m_edit_connection.clear();
374 
375  if (m_shape)
376  {
377  //Pen
378  ui->m_style_cb->setCurrentIndex(static_cast<int>(m_shape->pen().style()) - 1);
379  ui->m_size_dsb ->setValue(m_shape->pen().widthF());
380  ui->m_color_kpb->setColor(m_shape->pen().color());
381  ui->m_color_kpb->setColor(m_shape->pen().color());
382 
383  //Brush
385  ui->m_filling_gb->setVisible(m_shape->isClosed());
386 
387  ui->m_brush_style_cb->setCurrentIndex(static_cast<int>(m_shape->brush().style()));
388  ui->m_brush_color_kpb->setColor(m_shape->brush().color());
389 
390  ui->m_lock_pos_cb->setChecked(!m_shape->isMovable());
391  ui->m_close_polygon->setChecked(m_shape->isClosed());
392  }
393  else if (m_shapes_list.size() >= 2)
394  {
395  ui->m_close_polygon->setHidden(true);
396  bool same = true;
397  //Pen
398  Qt::PenStyle ps = m_shapes_list.first()->pen().style();
399  for (QetShapeItem *qsi : m_shapes_list) {
400  if (qsi->pen().style() != ps) {
401  same = false;
402  break;
403  }
404  }
405  ui->m_style_cb->setCurrentIndex(same ? static_cast<int>(ps) - 1 : -1);
406 
407  same = true;
408  qreal pw = m_shapes_list.first()->pen().widthF();
409  for (QetShapeItem *qsi : m_shapes_list) {
410  if (qsi->pen().widthF() != pw) {
411  same = false;
412  break;
413  }
414  }
415  ui->m_size_dsb->setValue(same ? pw : 0);
416 
417  same = true;
418  QColor pc = m_shapes_list.first()->pen().color();
419  for (QetShapeItem *qsi : m_shapes_list) {
420  if (qsi->pen().color() != pc) {
421  same = false;
422  break;
423  }
424  }
425  ui->m_color_kpb->setColor(same ? pc : QColor());
426 
427  //Brush
428  ui->m_filling_gb->setVisible(true);
429 
430  same = true;
431  Qt::BrushStyle bs = m_shapes_list.first()->brush().style();
432  for (QetShapeItem *qsi : m_shapes_list) {
433  if (qsi->brush().style() != bs) {
434  same = false;
435  break;
436  }
437  }
438  ui->m_brush_style_cb->setCurrentIndex(same ? static_cast<int>(bs) : -1);
439 
440  same = true;
441  QColor bc = m_shapes_list.first()->brush().color();
442  for (QetShapeItem *qsi : m_shapes_list) {
443  if (qsi->brush().color() != bc) {
444  same = false;
445  break;
446  }
447  }
448  ui->m_brush_color_kpb->setColor(same ? bc : QColor());
449 
450  ui->m_lock_pos_cb->setChecked(false);
451  ui->m_close_polygon->setChecked(false);
452  }
453 
455 }
456 
463 {
464  if (live_edit == m_live_edit) {
465  return true;
466  }
467  m_live_edit = live_edit;
468 
469  if (m_live_edit) {
471  }
472  else
473  {
474  for (QMetaObject::Connection c : m_edit_connection) {
475  disconnect(c);
476  }
477  m_edit_connection.clear();
478  }
479  return true;
480 }
481 
487 {
488  for (QMetaObject::Connection c : m_edit_connection) {
489  disconnect(c);
490  }
491  m_edit_connection.clear();
492 
493  if (m_shape || !m_shapes_list.isEmpty())
494  {
495  m_edit_connection << connect (ui->m_style_cb, SIGNAL(activated(int)), this, SLOT(apply()));
496  m_edit_connection << connect (ui->m_size_dsb, SIGNAL(valueChanged(double)), this, SLOT(apply()));
497  m_edit_connection << connect (ui->m_brush_style_cb, SIGNAL(activated(int)), this, SLOT(apply()));
498  m_edit_connection << connect (ui->m_close_polygon, &QCheckBox::clicked, this, &ShapeGraphicsItemPropertiesWidget::apply);
501  }
502 }
503 
505 {
506  if (m_shape) {
507  m_shape->setMovable(!ui->m_lock_pos_cb->isChecked());
508  }
509  else if (!m_shapes_list.isEmpty()) {
510  for (QPointer<QetShapeItem> qsi : m_shapes_list) {
511  qsi->setMovable(!ui->m_lock_pos_cb->isChecked());
512  }
513  }
514 }
515 
517 {
518  if(newColor.isValid() && m_live_edit) {
519  apply();
520  }
521 }
522 
524 {
525  if(newColor.isValid() && m_live_edit) {
526  apply();
527  }
528 }
bool isClosed() const
Definition: qetshapeitem.h:92
The QPropertyUndoCommand class This undo command manage QProperty of a QObject. This undo command can...
QList< QMetaObject::Connection > m_connect_list
ShapeGraphicsItemPropertiesWidget(QetShapeItem *item, QWidget *parent=nullptr)
ShapeGraphicsItemPropertiesWidget::ShapeGraphicsItemPropertiesWidget Constructor. ...
void setItems(QList< QetShapeItem *> shapes_list)
ShapeGraphicsItemPropertiesWidget::setItems Set a list of shapes to be edited.
Diagram * diagram() const
QetGraphicsItem::diagram return the diagram of this item.
bool setLiveEdit(bool live_edit) override
ShapeGraphicsItemPropertiesWidget::setLiveEdit.
QUndoCommand * associatedUndo() const override
ShapeGraphicsItemPropertiesWidget::associatedUndo.
QList< QMetaObject::Connection > m_edit_connection
The PropertiesEditorWidget class This class extend QWidget method for have common way to edit propert...
void brushChanged()
The ShapeGraphicsItemPropertiesWidget class Provide a widget to edit the properties of a QetShapeItem...
~ShapeGraphicsItemPropertiesWidget() override
ShapeGraphicsItemPropertiesWidget::~ShapeGraphicsItemPropertiesWidget Destructor. ...
void closeChanged()
QBrush brush
Definition: qetshapeitem.h:40
Ui::ShapeGraphicsItemPropertiesWidget * ui
QIcon tr
Definition: qeticons.cpp:204
void updateUi() override
ShapeGraphicsItemPropertiesWidget::updateUi.
void reset() override
ShapeGraphicsItemPropertiesWidget::reset Reset the change.
void setUpEditConnection()
ShapeGraphicsItemPropertiesWidget::setUpEditConnection Disconnect the previous connection, and reconnect the connection between the editors widgets and void ShapeGraphicsItemPropertiesWidget::apply function.
QList< QPointer< QetShapeItem > > m_shapes_list
The QetShapeItem class this class is used to draw a basic shape (line, rectangle, ellipse) into a dia...
Definition: qetshapeitem.h:35
virtual bool isMovable() const
void setItem(QetShapeItem *shape)
ShapeGraphicsItemPropertiesWidget::setItem Set as the current edited item.
QUndoStack & undoStack()
Definition: diagram.h:337
void apply() override
ShapeGraphicsItemPropertiesWidget::apply Apply the current change, by pushing an undo command to the ...
ShapeType shapeType() const
Definition: qetshapeitem.h:76
void penChanged()
virtual void setMovable(bool movable)