QElectroTech  0.70
elementinfowidget.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 "elementinfowidget.h"
19 #include "ui_elementinfowidget.h"
20 #include "qetapp.h"
22 #include "diagram.h"
23 #include "elementinfopartwidget.h"
24 #include "element.h"
25 
34  ui(new Ui::ElementInfoWidget),
35  m_first_activation (false)
36 {
37  ui->setupUi(this);
39  setElement(elmt);
40 }
41 
47 {
48  qDeleteAll(m_eipw_list);
49  delete ui;
50 }
51 
58 {
59  if (m_element == element) return;
60 
61  if (m_element)
63 
64  m_element = element;
65  updateUi();
66 
69 
70  if (f && l)
71  {
72  if (f->text().isEmpty())
73  l->setEnabled(true);
74  else
75  l->setDisabled(true);
76 
77  connect(f, &ElementInfoPartWidget::textChanged, [l](const QString text)
78  {
79  l->setEnabled(text.isEmpty()? true : false);
80  });
81  }
82 
84 }
85 
92 {
93  if (QUndoCommand *undo = associatedUndo())
94  m_element -> diagram() -> undoStack().push(undo);
95 }
96 
105 {
106  DiagramContext new_info = currentInfo();
107  DiagramContext old_info = m_element -> elementInformations();
108 
109  if (old_info != new_info)
110  return (new ChangeElementInformationCommand(m_element, old_info, new_info));
111 
112  return nullptr;
113 }
114 
120 bool ElementInfoWidget::setLiveEdit(bool live_edit)
121 {
122  if (m_live_edit == live_edit) return true;
123  m_live_edit = live_edit;
124 
125  if (m_live_edit)
126  enableLiveEdit();
127  else
128  disableLiveEdit();
129 
130  return true;
131 }
132 
141 bool ElementInfoWidget::event(QEvent *event)
142 {
143  if (m_first_activation)
144  {
145  if (event -> type() == QEvent::WindowActivate || event -> type() == QEvent::Show)
146  {
147  QTimer::singleShot(250, this, SLOT(firstActivated()));
148  m_first_activation = false;
149  }
150  }
151  return(QWidget::event(event));
152 }
153 
159 {
160  for (ElementInfoPartWidget *eipw : m_eipw_list)
162 }
163 
169 {
170  for (ElementInfoPartWidget *eipw : m_eipw_list)
172 }
173 
179 {
180  foreach (QString str, QETApp::elementInfoKeys())
181  {
183  ui->scroll_vlayout->addWidget(eipw);
184  m_eipw_list << eipw;
185  }
186  ui->scroll_vlayout->addStretch();
187 }
188 
195 {
196  foreach (ElementInfoPartWidget *eipw, m_eipw_list)
197  {
198  if (eipw->key() == key)
199  return eipw;
200  }
201 
202  return nullptr;
203 }
204 
211 {
212  //We disable live edit to avoid wrong undo when we fill the line edit with new text
214 
215  DiagramContext element_info = m_element->elementInformations();
216 
217  for (ElementInfoPartWidget *eipw : m_eipw_list) {
218  eipw -> setText (element_info[eipw->key()].toString());
219  }
220 
221  if (m_live_edit) {
222  enableLiveEdit();
223  }
224 }
225 
231 {
232  DiagramContext info_;
233 
234  for (ElementInfoPartWidget *eipw : m_eipw_list)
235  {
236  //add value only if they're something to store
237  if (!eipw->text().isEmpty())
238  {
239  QString txt = eipw->text();
240  //remove line feed and carriage return
241  txt.remove("\r");
242  txt.remove("\n");
243  info_.addValue(eipw->key(), txt);
244  }
245  }
246 
247  return info_;
248 }
249 
256  m_eipw_list.first() -> setFocusTolineEdit();
257 }
258 
264 {
265  if(currentInfo() != m_element->elementInformations())
266  updateUi();
267 }
bool setLiveEdit(bool live_edit) override
ElementInfoWidget::setLiveEdit.
QUndoCommand * associatedUndo() const override
ElementInfoWidget::associatedUndo If the edited info is different of the actual element info...
DiagramContext currentInfo() const
ElementInfoWidget::currentInfo.
void firstActivated()
ElementInfoWidget::firstActivated Slot activated when this widget is show. Set the focus to the first...
Ui::ElementInfoWidget * ui
void updateUi() override
ElementInfoWidget::updateUi fill information fetch in m_element_info to the corresponding line edit...
bool addValue(const QString &, const QVariant &, bool show=true)
~ElementInfoWidget() override
ElementInfoWidget::~ElementInfoWidget Destructor.
The AbstractElementPropertiesEditorWidget class This class provide common method for all widget used ...
void setEnabled(bool e)
ElementInfoPartWidget::setEnabled enable the line edit.
The ChangeElementInformationCommand class This class manage undo/redo to change the element informati...
void elementInfoChange()
ElementInfoWidget::elementInfoChange This slot is called when m_element::elementInformation change...
void disableLiveEdit() override
ElementInfoWidget::disableLiveEdit disable the live edit mode.
QList< ElementInfoPartWidget * > m_eipw_list
void enableLiveEdit() override
ElementInfoWidget::enableLiveEdit Enable the live edit mode.
void textChanged(const QString &text)
static QStringList elementInfoKeys()
QETApp::elementInfoKeys.
Definition: qetapp.cpp:289
void setDisabled(bool d)
ElementInfoPartWidget::setDisabled disable the line edit.
void setElement(Element *element) override
ElementInfoWidget::setElement Set to be the edited element.
ElementInfoWidget(Element *elmt, QWidget *parent=nullptr)
ElementInfoWidget::ElementInfoWidget Constructor.
void apply() override
ElementInfoWidget::apply Apply the new information with a new undo command (got with method associate...
void elementInfoChange(DiagramContext old_info, DiagramContext new_info)
QString text() const
ElementInfoPartWidget::text.
bool event(QEvent *event) override
ElementInfoWidget::event Reimplemented from QWidget::event Only give focus to the first line edit at ...
void buildInterface()
ElementInfoWidget::buildInterface Build the widget.
static QString elementTranslatedInfoKey(const QString &)
ElementsProperties::translatedInfo Return the translated information key given by If don&#39;t match...
Definition: qetapp.cpp:321
ElementInfoPartWidget * infoPartWidgetForKey(const QString &key) const
ElementInfoWidget::infoPartWidgetForKey.
The ElementInfoWidget class this class is a widget to edit an element informations.