2014-02-12 17:36:35 +00:00
|
|
|
/*
|
2015-02-20 14:56:22 +00:00
|
|
|
Copyright 2006-2015 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"
|
2014-02-12 17:36:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief ElementInfoWidget::ElementInfoWidget
|
|
|
|
* Constructor
|
|
|
|
* @param elmt element to edit information
|
|
|
|
* @param parent parent widget
|
|
|
|
*/
|
|
|
|
ElementInfoWidget::ElementInfoWidget(Element *elmt, QWidget *parent) :
|
|
|
|
QWidget(parent),
|
|
|
|
ui(new Ui::ElementInfoWidget),
|
|
|
|
element_(elmt),
|
2015-03-05 10:26:09 +00:00
|
|
|
elmt_info(elmt->elementInformations()),
|
|
|
|
m_first_activation (true)
|
2014-02-12 17:36:35 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
buildInterface();
|
|
|
|
fillInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief ElementInfoWidget::~ElementInfoWidget
|
|
|
|
* Destructor
|
|
|
|
*/
|
|
|
|
ElementInfoWidget::~ElementInfoWidget()
|
|
|
|
{
|
|
|
|
qDeleteAll(eipw_list);
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief ElementInfoWidget::apply
|
2014-12-11 20:10:28 +00:00
|
|
|
* Apply the new information with a new undo command (got with method associatedUndo)
|
|
|
|
* pushed to the stack of element project.
|
|
|
|
* Return true if new info change, else false.
|
2014-02-12 17:36:35 +00:00
|
|
|
*/
|
2014-12-11 20:10:28 +00:00
|
|
|
bool ElementInfoWidget::apply() {
|
|
|
|
if (QUndoCommand *undo = associatedUndo()) {
|
|
|
|
element_ -> diagram() -> undoStack().push(undo);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
QUndoCommand* ElementInfoWidget::associatedUndo() const {
|
|
|
|
DiagramContext new_info;
|
|
|
|
DiagramContext old_info = element_ -> elementInformations();
|
|
|
|
|
2014-02-12 17:36:35 +00:00
|
|
|
foreach (ElementInfoPartWidget *eipw, eipw_list) {
|
2014-12-11 20:10:28 +00:00
|
|
|
//add value only if they're something to store
|
2014-02-12 17:36:35 +00:00
|
|
|
if (!eipw->text().isEmpty())
|
2014-12-11 20:10:28 +00:00
|
|
|
new_info.addValue(eipw->key(),
|
2014-02-12 17:36:35 +00:00
|
|
|
eipw->text(),
|
|
|
|
eipw->mustShow());
|
|
|
|
}
|
2014-12-11 20:10:28 +00:00
|
|
|
|
|
|
|
if (old_info != new_info) {
|
|
|
|
return (new ChangeElementInformationCommand(element_, old_info, new_info));
|
|
|
|
}
|
|
|
|
return nullptr;
|
2014-02-12 17:36:35 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 10:26:09 +00: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
|
|
|
|
*/
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2014-02-12 17:36:35 +00:00
|
|
|
/**
|
|
|
|
* @brief ElementInfoWidget::buildInterface
|
|
|
|
* Build the widget
|
|
|
|
*/
|
|
|
|
void ElementInfoWidget::buildInterface() {
|
|
|
|
foreach (QString str, QETApp::elementInfoKeys()) {
|
|
|
|
ElementInfoPartWidget *eipw = new ElementInfoPartWidget(str, QETApp::elementTranslatedInfoKey(str), this);
|
|
|
|
ui->scroll_vlayout->addWidget(eipw);
|
|
|
|
eipw_list << eipw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief ElementInfoWidget::fillInfo
|
|
|
|
* fill information fetch in elmt_info to the
|
|
|
|
* corresponding line edit
|
|
|
|
*/
|
|
|
|
void ElementInfoWidget::fillInfo() {
|
|
|
|
foreach (ElementInfoPartWidget *eipw, eipw_list) {
|
2014-10-28 15:45:28 +00:00
|
|
|
|
|
|
|
eipw -> setText (elmt_info[eipw->key()].toString());
|
|
|
|
eipw -> setShow (elmt_info.keyMustShow(eipw->key()));
|
|
|
|
|
|
|
|
//If the current eipw is for label or comment and the text is empty
|
|
|
|
//we force the checkbox to ckecked
|
|
|
|
if (eipw -> key() == "label" || eipw -> key() == "comment") {
|
|
|
|
if (elmt_info[eipw->key()].toString().isEmpty())
|
|
|
|
eipw->setShow(true);
|
|
|
|
}
|
|
|
|
else //< for other eipw we hide the checkbox
|
|
|
|
eipw->setHideShow(true);
|
2014-02-12 17:36:35 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-05 10:26:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief ElementInfoWidget::firstActivated
|
|
|
|
* Slot activated when this widget is show.
|
|
|
|
* Set the focus to the first line edit provided by this widget
|
|
|
|
*/
|
|
|
|
void ElementInfoWidget::firstActivated() {
|
|
|
|
eipw_list.first() -> setFocusTolineEdit();
|
|
|
|
}
|