2014-02-12 17:36:35 +00:00
|
|
|
/*
|
2020-06-15 17:42:37 +02:00
|
|
|
Copyright 2006-2020 The QElectroTech Team
|
2014-02-12 17:36:35 +00:00
|
|
|
This file is part of QElectroTech.
|
|
|
|
|
|
|
|
QElectroTech is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
QElectroTech is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include "elementinfowidget.h"
|
|
|
|
#include "ui_elementinfowidget.h"
|
|
|
|
#include "qetapp.h"
|
2014-12-11 20:10:28 +00:00
|
|
|
#include "changeelementinformationcommand.h"
|
|
|
|
#include "diagram.h"
|
|
|
|
#include "elementinfopartwidget.h"
|
|
|
|
#include "element.h"
|
2020-11-08 19:16:02 +01:00
|
|
|
#include "qetinformation.h"
|
2014-02-12 17:36:35 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementInfoWidget::ElementInfoWidget
|
|
|
|
Constructor
|
|
|
|
@param elmt element to edit information
|
|
|
|
@param parent parent widget
|
|
|
|
*/
|
2014-02-12 17:36:35 +00:00
|
|
|
ElementInfoWidget::ElementInfoWidget(Element *elmt, QWidget *parent) :
|
2015-05-25 10:22:00 +00:00
|
|
|
AbstractElementPropertiesEditorWidget(parent),
|
2014-02-12 17:36:35 +00:00
|
|
|
ui(new Ui::ElementInfoWidget),
|
2015-07-22 13:36:44 +00:00
|
|
|
m_first_activation (false)
|
2014-02-12 17:36:35 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
buildInterface();
|
2015-05-25 10:22:00 +00:00
|
|
|
setElement(elmt);
|
2014-02-12 17:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementInfoWidget::~ElementInfoWidget
|
|
|
|
Destructor
|
|
|
|
*/
|
2014-02-12 17:36:35 +00:00
|
|
|
ElementInfoWidget::~ElementInfoWidget()
|
|
|
|
{
|
2015-05-31 15:02:41 +00:00
|
|
|
qDeleteAll(m_eipw_list);
|
2014-02-12 17:36:35 +00:00
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2015-05-25 10:22:00 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementInfoWidget::setElement
|
2020-08-18 21:28:52 +02:00
|
|
|
Set element to be the edited element
|
2020-08-16 11:19:36 +02:00
|
|
|
@param element
|
|
|
|
*/
|
2015-05-25 10:22:00 +00:00
|
|
|
void ElementInfoWidget::setElement(Element *element)
|
|
|
|
{
|
|
|
|
if (m_element == element) return;
|
2015-05-31 15:02:41 +00:00
|
|
|
|
|
|
|
if (m_element)
|
2017-08-22 17:24:51 +00:00
|
|
|
disconnect(m_element.data(), &Element::elementInfoChange, this, &ElementInfoWidget::elementInfoChange);
|
2015-05-31 15:02:41 +00:00
|
|
|
|
2015-05-25 10:22:00 +00:00
|
|
|
m_element = element;
|
2015-05-31 15:02:41 +00:00
|
|
|
updateUi();
|
|
|
|
|
2017-01-10 09:09:30 +00:00
|
|
|
ElementInfoPartWidget *f = infoPartWidgetForKey("formula");
|
|
|
|
ElementInfoPartWidget *l = infoPartWidgetForKey("label");
|
|
|
|
|
|
|
|
if (f && l)
|
|
|
|
{
|
|
|
|
if (f->text().isEmpty())
|
|
|
|
l->setEnabled(true);
|
|
|
|
else
|
|
|
|
l->setDisabled(true);
|
|
|
|
|
|
|
|
connect(f, &ElementInfoPartWidget::textChanged, [l](const QString text)
|
|
|
|
{
|
|
|
|
l->setEnabled(text.isEmpty()? true : false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-22 17:24:51 +00:00
|
|
|
connect(m_element.data(), &Element::elementInfoChange, this, &ElementInfoWidget::elementInfoChange);
|
2015-05-25 10:22:00 +00:00
|
|
|
}
|
|
|
|
|
2014-02-12 17:36:35 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementInfoWidget::apply
|
|
|
|
Apply the new information with a new undo command (got with method associatedUndo)
|
|
|
|
pushed to the stack of element project.
|
|
|
|
*/
|
2015-05-02 22:25:01 +00:00
|
|
|
void ElementInfoWidget::apply()
|
|
|
|
{
|
|
|
|
if (QUndoCommand *undo = associatedUndo())
|
2015-05-25 10:22:00 +00:00
|
|
|
m_element -> diagram() -> undoStack().push(undo);
|
2014-12-11 20:10:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementInfoWidget::associatedUndo
|
|
|
|
If the edited info is different of the actual element info,
|
|
|
|
return a QUndoCommand with the change.
|
|
|
|
If no change return nullptr;
|
|
|
|
@return
|
|
|
|
*/
|
2015-05-31 15:02:41 +00:00
|
|
|
QUndoCommand* ElementInfoWidget::associatedUndo() const
|
|
|
|
{
|
2015-06-09 16:28:54 +00:00
|
|
|
DiagramContext new_info = currentInfo();
|
2015-05-25 10:22:00 +00:00
|
|
|
DiagramContext old_info = m_element -> elementInformations();
|
2014-12-11 20:10:28 +00:00
|
|
|
|
2015-05-31 15:02:41 +00:00
|
|
|
if (old_info != new_info)
|
2015-05-25 10:22:00 +00:00
|
|
|
return (new ChangeElementInformationCommand(m_element, old_info, new_info));
|
2015-05-31 15:02:41 +00:00
|
|
|
|
2014-12-11 20:10:28 +00:00
|
|
|
return nullptr;
|
2014-02-12 17:36:35 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 15:02:41 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementInfoWidget::setLiveEdit
|
|
|
|
@param live_edit true : enable the live edit mode, false disable
|
|
|
|
@return always true;
|
|
|
|
*/
|
2015-05-31 15:02:41 +00:00
|
|
|
bool ElementInfoWidget::setLiveEdit(bool live_edit)
|
|
|
|
{
|
|
|
|
if (m_live_edit == live_edit) return true;
|
|
|
|
m_live_edit = live_edit;
|
|
|
|
|
|
|
|
if (m_live_edit)
|
|
|
|
enableLiveEdit();
|
|
|
|
else
|
|
|
|
disableLiveEdit();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-05 10:26:09 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementInfoWidget::event
|
|
|
|
Reimplemented from QWidget::event
|
|
|
|
Only give focus to the first line edit at first activation.
|
|
|
|
After send the event to QWidget.
|
|
|
|
@param event
|
|
|
|
@return
|
|
|
|
*/
|
2015-03-05 10:26:09 +00:00
|
|
|
bool ElementInfoWidget::event(QEvent *event)
|
|
|
|
{
|
|
|
|
if (m_first_activation)
|
|
|
|
{
|
|
|
|
if (event -> type() == QEvent::WindowActivate || event -> type() == QEvent::Show)
|
|
|
|
{
|
|
|
|
QTimer::singleShot(250, this, SLOT(firstActivated()));
|
|
|
|
m_first_activation = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(QWidget::event(event));
|
|
|
|
}
|
|
|
|
|
2015-05-31 15:02:41 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementInfoWidget::enableLiveEdit
|
|
|
|
Enable the live edit mode
|
|
|
|
*/
|
2015-05-31 15:02:41 +00:00
|
|
|
void ElementInfoWidget::enableLiveEdit()
|
|
|
|
{
|
2018-04-14 12:21:07 +00:00
|
|
|
for (ElementInfoPartWidget *eipw : m_eipw_list)
|
2015-05-31 15:02:41 +00:00
|
|
|
connect(eipw, &ElementInfoPartWidget::textChanged, this, &ElementInfoWidget::apply);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementInfoWidget::disableLiveEdit
|
|
|
|
disable the live edit mode
|
|
|
|
*/
|
2015-05-31 15:02:41 +00:00
|
|
|
void ElementInfoWidget::disableLiveEdit()
|
|
|
|
{
|
2018-04-14 12:21:07 +00:00
|
|
|
for (ElementInfoPartWidget *eipw : m_eipw_list)
|
2015-05-31 15:02:41 +00:00
|
|
|
disconnect(eipw, &ElementInfoPartWidget::textChanged, this, &ElementInfoWidget::apply);
|
|
|
|
}
|
|
|
|
|
2014-02-12 17:36:35 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementInfoWidget::buildInterface
|
|
|
|
Build the widget
|
|
|
|
*/
|
2015-05-31 15:02:41 +00:00
|
|
|
void ElementInfoWidget::buildInterface()
|
|
|
|
{
|
2020-11-08 19:16:02 +01:00
|
|
|
for (auto str : QETApp::elementInfoKeys())
|
2015-05-31 15:02:41 +00:00
|
|
|
{
|
2020-11-08 19:16:02 +01:00
|
|
|
ElementInfoPartWidget *eipw = new ElementInfoPartWidget(str, QETInformation::translatedInfoKey(str), this);
|
2014-02-12 17:36:35 +00:00
|
|
|
ui->scroll_vlayout->addWidget(eipw);
|
2015-05-31 15:02:41 +00:00
|
|
|
m_eipw_list << eipw;
|
2014-02-12 17:36:35 +00:00
|
|
|
}
|
2015-06-19 17:26:10 +00:00
|
|
|
ui->scroll_vlayout->addStretch();
|
2014-02-12 17:36:35 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 16:45:55 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementInfoWidget::infoPartWidgetForKey
|
|
|
|
@param key
|
2020-08-18 21:28:52 +02:00
|
|
|
@return the ElementInfoPartWidget with key key,
|
|
|
|
if not found return nullptr;
|
2020-08-16 11:19:36 +02:00
|
|
|
*/
|
2016-11-24 16:45:55 +00:00
|
|
|
ElementInfoPartWidget *ElementInfoWidget::infoPartWidgetForKey(const QString &key) const
|
|
|
|
{
|
2020-11-08 19:16:02 +01:00
|
|
|
for (auto eipw : m_eipw_list)
|
2016-11-24 16:45:55 +00:00
|
|
|
{
|
|
|
|
if (eipw->key() == key)
|
|
|
|
return eipw;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-02-12 17:36:35 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementInfoWidget::updateUi
|
|
|
|
fill information fetch in m_element_info to the
|
|
|
|
corresponding line edit
|
|
|
|
*/
|
2015-05-31 15:02:41 +00:00
|
|
|
void ElementInfoWidget::updateUi()
|
|
|
|
{
|
|
|
|
//We disable live edit to avoid wrong undo when we fill the line edit with new text
|
|
|
|
if (m_live_edit) disableLiveEdit();
|
2014-10-28 15:45:28 +00:00
|
|
|
|
2015-05-31 15:02:41 +00:00
|
|
|
DiagramContext element_info = m_element->elementInformations();
|
2018-04-14 12:21:07 +00:00
|
|
|
|
|
|
|
for (ElementInfoPartWidget *eipw : m_eipw_list) {
|
2015-05-31 15:02:41 +00:00
|
|
|
eipw -> setText (element_info[eipw->key()].toString());
|
2014-02-12 17:36:35 +00:00
|
|
|
}
|
2015-05-31 15:02:41 +00:00
|
|
|
|
2018-04-14 12:21:07 +00:00
|
|
|
if (m_live_edit) {
|
|
|
|
enableLiveEdit();
|
|
|
|
}
|
2014-02-12 17:36:35 +00:00
|
|
|
}
|
2015-03-05 10:26:09 +00:00
|
|
|
|
2015-06-09 16:28:54 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementInfoWidget::currentInfo
|
|
|
|
@return the info currently edited
|
|
|
|
*/
|
2015-06-09 16:28:54 +00:00
|
|
|
DiagramContext ElementInfoWidget::currentInfo() const
|
|
|
|
{
|
|
|
|
DiagramContext info_;
|
|
|
|
|
2019-03-31 13:33:48 +00:00
|
|
|
for (ElementInfoPartWidget *eipw : m_eipw_list)
|
|
|
|
{
|
2020-05-24 16:24:39 +02:00
|
|
|
|
2019-03-31 13:33:48 +00:00
|
|
|
//add value only if they're something to store
|
|
|
|
if (!eipw->text().isEmpty())
|
|
|
|
{
|
|
|
|
QString txt = eipw->text();
|
2019-03-31 18:43:45 +00:00
|
|
|
//remove line feed and carriage return
|
|
|
|
txt.remove("\r");
|
|
|
|
txt.remove("\n");
|
2019-03-31 13:33:48 +00:00
|
|
|
info_.addValue(eipw->key(), txt);
|
2018-04-14 12:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-09 16:28:54 +00:00
|
|
|
|
|
|
|
return info_;
|
|
|
|
}
|
|
|
|
|
2015-03-05 10:26:09 +00:00
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementInfoWidget::firstActivated
|
|
|
|
Slot activated when this widget is show.
|
|
|
|
Set the focus to the first line edit provided by this widget
|
|
|
|
*/
|
2020-09-07 22:03:40 +02:00
|
|
|
void ElementInfoWidget::firstActivated()
|
|
|
|
{
|
2015-05-31 15:02:41 +00:00
|
|
|
m_eipw_list.first() -> setFocusTolineEdit();
|
2015-03-05 10:26:09 +00:00
|
|
|
}
|
2015-06-09 16:28:54 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-16 11:19:36 +02:00
|
|
|
@brief ElementInfoWidget::elementInfoChange
|
|
|
|
This slot is called when m_element::elementInformation change.
|
|
|
|
*/
|
2015-06-09 16:28:54 +00:00
|
|
|
void ElementInfoWidget::elementInfoChange()
|
|
|
|
{
|
|
|
|
if(currentInfo() != m_element->elementInformations())
|
|
|
|
updateUi();
|
|
|
|
}
|