QElectroTech  0.70
lineeditor.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 "lineeditor.h"
19 #include "styleeditor.h"
20 #include "partline.h"
21 #include "qet.h"
22 #include "qeticons.h"
24 #include "elementscene.h"
25 
32 LineEditor::LineEditor(QETElementEditor *editor, PartLine *line, QWidget *parent) :
33  ElementItemEditor(editor, parent),
34  part(line),
35  m_locked (false)
36 {
37  style_ = new StyleEditor(editor);
38 
39  x1 = new QDoubleSpinBox();
40  y1 = new QDoubleSpinBox();
41  x2 = new QDoubleSpinBox();
42  y2 = new QDoubleSpinBox();
43 
44  x1 -> setRange(-5000, 5000);
45  y1 -> setRange(-5000, 5000);
46  x2 -> setRange(-5000, 5000);
47  y2 -> setRange(-5000, 5000);
48 
49  end1_type = new QComboBox();
50  end1_type -> addItem(QET::Icons::EndLineNone, tr("Normale", "type of the 1st end of a line"), Qet::None );
51  end1_type -> addItem(QET::Icons::EndLineSimple, tr("Flèche simple", "type of the 1st end of a line"), Qet::Simple );
52  end1_type -> addItem(QET::Icons::EndLineTriangle, tr("Flèche triangulaire", "type of the 1st end of a line"), Qet::Triangle);
53  end1_type -> addItem(QET::Icons::EndLineCircle, tr("Cercle", "type of the 1st end of a line"), Qet::Circle );
54  end1_type -> addItem(QET::Icons::EndLineDiamond, tr("Carré", "type of the 1st end of a line"), Qet::Diamond );
55  end2_type = new QComboBox();
56  end2_type -> addItem(QET::Icons::EndLineNone, tr("Normale", "type of the 2nd end of a line"), Qet::None );
57  end2_type -> addItem(QET::Icons::EndLineSimple, tr("Flèche simple", "type of the 2nd end of a line"), Qet::Simple );
58  end2_type -> addItem(QET::Icons::EndLineTriangle, tr("Flèche triangulaire", "type of the 2nd end of a line"), Qet::Triangle);
59  end2_type -> addItem(QET::Icons::EndLineCircle, tr("Cercle", "type of the 2nd end of a line"), Qet::Circle );
60  end2_type -> addItem(QET::Icons::EndLineDiamond, tr("Carré", "type of the 2nd end of a line"), Qet::Diamond );
61 
62  end1_length = new QDoubleSpinBox();
63  end2_length = new QDoubleSpinBox();
64 
65  QGridLayout *grid = new QGridLayout();
66  grid -> addWidget(new QLabel("x1"), 0, 0);
67  grid -> addWidget(x1, 0, 1);
68  grid -> addWidget(new QLabel("y1"), 0, 2);
69  grid -> addWidget(y1, 0, 3);
70  grid -> addWidget(new QLabel("x2"), 1, 0);
71  grid -> addWidget(x2, 1, 1);
72  grid -> addWidget(new QLabel("y2"), 1, 2);
73  grid -> addWidget(y2, 1, 3);
74 
75  QGridLayout *grid2 = new QGridLayout();
76  grid2 -> addWidget(new QLabel(tr("Fin 1")), 0, 0);
77  grid2 -> addWidget(end1_type, 0, 1);
78  grid2 -> addWidget(end1_length, 0, 2);
79  grid2 -> addWidget(new QLabel(tr("Fin 2")), 1, 0);
80  grid2 -> addWidget(end2_type, 1, 1);
81  grid2 -> addWidget(end2_length, 1, 2);
82 
83  QVBoxLayout *v_layout = new QVBoxLayout(this);
84  v_layout -> addWidget(style_);
85  v_layout -> addLayout(grid);
86  v_layout -> addLayout(grid2);
87  v_layout->addStretch();
88  updateForm();
89 }
90 
93 }
94 
103 {
104  if (!new_part)
105  {
106  if (part)
107  {
108  disconnect(part, &PartLine::lineChanged, this, &LineEditor::updateForm);
113  }
114  part = nullptr;
115  style_ -> setPart(nullptr);
116  return(true);
117  }
118  if (PartLine *part_line = dynamic_cast<PartLine *>(new_part))
119  {
120  if (part == part_line) return true;
121  if (part)
122  {
123  disconnect(part, &PartLine::lineChanged, this, &LineEditor::updateForm);
128  }
129  part = part_line;
130  style_ -> setPart(part);
131  updateForm();
137  return(true);
138  }
139  return(false);
140 }
141 
146  return(part);
147 }
148 
153 QPointF LineEditor::editedP1() const {
154  return part -> mapFromScene(x1->value(), y1->value());
155 }
156 
161 QPointF LineEditor::editedP2() const {
162  return part -> mapFromScene(x2->value(), y2->value());
163 }
164 
167 {
168  if (m_locked) return;
169  m_locked = true;
170  QVariant end = end1_type -> itemData(end1_type->currentIndex());
171 
172  if (end != part->property("end1"))
173  {
174  QPropertyUndoCommand *undo = new QPropertyUndoCommand(part, "end1", part->property("end1"), end);
175  undo->setText(tr("Modifier une ligne"));
176  elementScene()->undoStack().push(undo);
177  }
178  m_locked = false;
179 }
180 
183 {
184  if (m_locked) return;
185  m_locked = true;
186  double length = end1_length->value();
187 
188  if (length != part->property("length1"))
189  {
190  QPropertyUndoCommand *undo = new QPropertyUndoCommand(part, "length1", part->property("length1"), length);
191  undo->setText(tr("Modifier une ligne"));
192  undo->enableAnimation();
193  elementScene()->undoStack().push(undo);
194  }
195  m_locked = false;
196 }
197 
200 {
201  if (m_locked) return;
202  m_locked = true;
203  QVariant end = end2_type -> itemData(end2_type->currentIndex());
204 
205  if (end != part->property("end2"))
206  {
207  QPropertyUndoCommand *undo = new QPropertyUndoCommand(part, "end2", part->property("end2"), end);
208  undo->setText(tr("Modifier une ligne"));
209  elementScene()->undoStack().push(undo);
210  }
211  m_locked = false;
212 }
213 
216 {
217  if (m_locked) return;
218  m_locked = true;
219  double length = end2_length->value();
220 
221  if (length != part->property("length2"))
222  {
223  QPropertyUndoCommand *undo = new QPropertyUndoCommand(part, "length2", part->property("length2"), length);
224  undo->setText(tr("Modifier une ligne"));
225  undo->enableAnimation();
226  elementScene()->undoStack().push(undo);
227  }
228  m_locked = false;
229 }
230 
232 {
233  if (m_locked) return;
234  m_locked = true;
235  QLineF line (editedP1(), editedP2());
236 
237  if (line != part->property("line"))
238  {
239  QPropertyUndoCommand *undo = new QPropertyUndoCommand(part, "line", part->property("line"), line);
240  undo->setText(tr("Modifier une ligne"));
241  undo->enableAnimation();
242  elementScene()->undoStack().push(undo);
243  }
244  m_locked = false;
245 }
246 
252 {
253  if (!part) return;
254  activeConnections(false);
255  QPointF p1(part -> sceneP1());
256  QPointF p2(part -> sceneP2());
257  x1 -> setValue(p1.x());
258  y1 -> setValue(p1.y());
259  x2 -> setValue(p2.x());
260  y2 -> setValue(p2.y());
261  end1_type -> setCurrentIndex(end1_type->findData(part -> firstEndType()));
262  end1_length -> setValue(part -> firstEndLength());
263  end2_type -> setCurrentIndex(end2_type->findData(part -> secondEndType()));
264  end2_length -> setValue(part -> secondEndLength());
265  activeConnections(true);
266 }
267 
275 {
276  if (active)
277  {
278  connect(x1, SIGNAL(editingFinished()), this, SLOT(lineEditingFinished()));
279  connect(y1, SIGNAL(editingFinished()), this, SLOT(lineEditingFinished()));
280  connect(x2, SIGNAL(editingFinished()), this, SLOT(lineEditingFinished()));
281  connect(y2, SIGNAL(editingFinished()), this, SLOT(lineEditingFinished()));
282  connect(end1_type, SIGNAL(currentIndexChanged(int)), this, SLOT(updateLineEndType1()));
283  connect(end1_length, SIGNAL(editingFinished()), this, SLOT(updateLineEndLength1()));
284  connect(end2_type, SIGNAL(currentIndexChanged(int)), this, SLOT(updateLineEndType2()));
285  connect(end2_length, SIGNAL(editingFinished()), this, SLOT(updateLineEndLength2()));
286  }
287  else
288  {
289  disconnect(x1, SIGNAL(editingFinished()), this, SLOT(lineEditingFinished()));
290  disconnect(y1, SIGNAL(editingFinished()), this, SLOT(lineEditingFinished()));
291  disconnect(x2, SIGNAL(editingFinished()), this, SLOT(lineEditingFinished()));
292  disconnect(y2, SIGNAL(editingFinished()), this, SLOT(lineEditingFinished()));
293  disconnect(end1_type, SIGNAL(currentIndexChanged(int)), this, SLOT(updateLineEndType1()));
294  disconnect(end1_length, SIGNAL(editingFinished()), this, SLOT(updateLineEndLength1()));
295  disconnect(end2_type, SIGNAL(currentIndexChanged(int)), this, SLOT(updateLineEndType2()));
296  disconnect(end2_length, SIGNAL(editingFinished()), this, SLOT(updateLineEndLength2()));
297  }
298 }
~LineEditor() override
Destructeur.
Definition: lineeditor.cpp:92
The QPropertyUndoCommand class This undo command manage QProperty of a QObject. This undo command can...
void updateForm() override
LineEditor::updateForm Update the value of the widgets.
Definition: lineeditor.cpp:251
Circle.
Definition: qet.h:195
Regular line.
Definition: qet.h:192
void activeConnections(bool)
LineEditor::activeConnections Enable/disable connection between editor widget and slot editingFinishe...
Definition: lineeditor.cpp:274
Diamond.
Definition: qet.h:196
CustomElementPart * currentPart() const override
Definition: lineeditor.cpp:145
QIcon EndLineNone
Definition: qeticons.cpp:90
QIcon EndLineCircle
Definition: qeticons.cpp:88
void updateLineEndType2()
Met a jour le type de la seconde extremite.
Definition: lineeditor.cpp:199
void enableAnimation(bool animate=true)
QPropertyUndoCommand::enableAnimation True to enable animation.
QDoubleSpinBox * y2
Definition: lineeditor.h:45
QIcon EndLineSimple
Definition: qeticons.cpp:91
QIcon EndLineTriangle
Definition: qeticons.cpp:92
QDoubleSpinBox * x2
Definition: lineeditor.h:45
void firstEndLengthChanged()
QUndoStack & undoStack()
void lineEditingFinished()
Definition: lineeditor.cpp:231
void secondEndTypeChanged()
QDoubleSpinBox * x1
Definition: lineeditor.h:45
void lineChanged()
LineEditor(QETElementEditor *, PartLine *=nullptr, QWidget *=nullptr)
Definition: lineeditor.cpp:32
QDoubleSpinBox * end1_length
Definition: lineeditor.h:47
QComboBox * end2_type
Definition: lineeditor.h:46
QIcon tr
Definition: qeticons.cpp:204
QPointF editedP1() const
LineEditor::editedP1.
Definition: lineeditor.cpp:153
void firstEndTypeChanged()
QVariant property(const char *name) const override
QComboBox * end1_type
Definition: lineeditor.h:46
bool setPart(CustomElementPart *) override
LineEditor::setPart Specifie to this editor the part to edit. Note that an editor can accept or refus...
Definition: lineeditor.cpp:102
Triangle.
Definition: qet.h:194
QDoubleSpinBox * y1
Definition: lineeditor.h:45
QPointF editedP2() const
LineEditor::editedP2.
Definition: lineeditor.cpp:161
QDoubleSpinBox * end2_length
Definition: lineeditor.h:47
void secondEndLengthChanged()
Base-less triangle.
Definition: qet.h:193
void updateLineEndLength1()
Met a jour la longueur de la premiere extremite.
Definition: lineeditor.cpp:182
void updateLineEndLength2()
Met a jour la longueur de la seconde extremite.
Definition: lineeditor.cpp:215
PartLine * part
Definition: lineeditor.h:43
virtual ElementScene * elementScene() const
bool m_locked
Definition: lineeditor.h:48
void updateLineEndType1()
Met a jour le type de la premiere extremite.
Definition: lineeditor.cpp:166
StyleEditor * style_
Definition: lineeditor.h:44
QIcon EndLineDiamond
Definition: qeticons.cpp:89