Select Git revision
ToolboxItemModel.cpp
-
Oliver Bock authored
git-svn-id: https://svn.origo.ethz.ch/fidelity@3 53d1999f-d1a8-4366-aa61-588fded17473
Oliver Bock authoredgit-svn-id: https://svn.origo.ethz.ch/fidelity@3 53d1999f-d1a8-4366-aa61-588fded17473
ComponentItem.cpp 10.33 KiB
/*************************************************************************
* Copyright (C) 2007 by Oliver Bock *
* bock@tfh-berlin.de *
* *
* This file is part of Fidelity. *
* *
* Fidelity 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, version 3 of the License. *
* *
* Fidelity 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 Fidelity. If not, see <http://www.gnu.org/licenses/>. *
* *
*************************************************************************/
#include "ComponentItem.h"
// Constructors/Destructors
Fidelity::GUI::ComponentItem::ComponentItem(QPointer<IComponent> component)
{
// Initialisations
m_NodeItemList = new QList<QPointer<ANodeItem> >();
m_LinkItemList = new QList<QPointer<ComponentLinkItem> >();
m_ParameterModel = new QStandardItemModel();
m_ParameterModel->setHorizontalHeaderLabels(QStringList()
<< "Identifier" << "Parameter" << "Value" << "Unit" << "Type" << "Description");
// Configure basics
m_RotationAngle = 0;
unsetCursor();
setFlag(QGraphicsItem::ItemIsMovable);
setFlag(QGraphicsItem::ItemIsSelectable);
setAcceptsHoverEvents(true);
// Components are drawn on the default z-plane
setZValue(0);
if(component!=NULL) {
m_Component = component;
// Set SVG image (max. 48px per side!)
setSharedRenderer(component->Icon());
// Configure nodes
NodeTable* beamNodes = component->BeamNodeTable();
NodeTable* controlNodes = component->ControlNodeTable();
NodeTable::const_iterator itNodes = beamNodes->constBegin();
while (itNodes != beamNodes->constEnd()) {
// DON'T delete this item. Its parent will care for it...
BeamNodeItem* nodeItem = new BeamNodeItem(itNodes.value(), this);
nodeItem->setToolTip("<b>Beam Node</b><hr>" \
"Name: "+itNodes.value()->Label()+"<br>"
"Description: "+itNodes.value()->Description());
m_NodeItemList->append(nodeItem);
++itNodes;
}
itNodes = controlNodes->constBegin();
while (itNodes != controlNodes->constEnd()) {
// DON'T delete this item. Its parent will care for it...
ControlNodeItem* nodeItem = new ControlNodeItem(itNodes.value(), this);
nodeItem->setToolTip("<b>Control Node</b><hr>" \
"Name: "+itNodes.value()->Label()+"<br>"
"Description: "+itNodes.value()->Description());
m_NodeItemList->append(nodeItem);
++itNodes;
}
// Configure label
QString label = component->Label();
m_Label = new ComponentLabelItem(label, this);
// Track label changes
connect(m_Label, SIGNAL(textModified(QString)),
this, SLOT(labelChanged(QString)));
// Configure parameter (load into model)
ParameterTable* params = component->ParamTable();
ParameterTable::const_iterator itParams = params->constBegin();
while (itParams != params->constEnd()) {
QList<QStandardItem*> row;
QString paramType = ParameterHandler::identifyType(itParams.value());
// Add all parameter attributes (only value is fully editable!)
QStandardItem* item = new QStandardItem(itParams.value()->Identifier());
item->setFlags(Qt::ItemIsEnabled);
row.append(item);
item = new QStandardItem(itParams.value()->Label());
item->setFlags(Qt::ItemIsEnabled);
row.append(item);
// Boolean values are handled using checkable items instead of text
if(paramType != "bool") {
item = new QStandardItem(ParameterHandler::ValueString(itParams.value(),paramType));
}
else {
item = new QStandardItem("Yes");
item->setCheckable(true);
item->setEditable(false);
if(ParameterHandler::ValueString(itParams.value(),paramType)=="true") {
item->setCheckState(Qt::Checked);
}
}
row.append(item);
item = new QStandardItem(itParams.value()->Unit());
item->setFlags(Qt::ItemIsEnabled);
row.append(item);
item = new QStandardItem(paramType);
item->setFlags(Qt::ItemIsEnabled);
row.append(item);
item = new QStandardItem(itParams.value()->Description());
item->setFlags(Qt::ItemIsEnabled);
row.append(item);
m_ParameterModel->appendRow(row);
++itParams;
}
// Track parameter changes
connect(m_ParameterModel, SIGNAL(itemChanged(QStandardItem*)),
this, SLOT(parameterModelItemChanged(QStandardItem*)));
}
else {
QMessageBox::warning(0, "Warning", "The drag 'n' drop operation failed!");
}
}
Fidelity::GUI::ComponentItem::~ComponentItem()
{
// Delete children and attached links
if(m_Label) delete m_Label;
for(int i = 0; i<m_LinkItemList->size(); ++i) {
// Schedules link for deletion (wait for event loop!)
m_LinkItemList->at(i)->deleteLater();
}
for(int i = 0; i<m_NodeItemList->size(); ++i) {
delete m_NodeItemList->at(i);
}
// Clean up members
if(m_NodeItemList) delete m_NodeItemList;
if(m_LinkItemList) delete m_LinkItemList;
if(m_ParameterModel) delete m_ParameterModel;
if(m_Component) delete m_Component;
}
// Public methods
ANodeItem* Fidelity::GUI::ComponentItem::findNodeItem(NodeItemType type, QString name) const
{
QList<QGraphicsItem*> childList = QGraphicsItem::children();
// Iterate over all children
for(int i = 0; i < childList.size(); i++) {
BeamNodeItem* beamNode = dynamic_cast<BeamNodeItem*>(childList.at(i));
ControlNodeItem* controlNode = dynamic_cast<ControlNodeItem*>(childList.at(i));
// Look for ANodeItems only
if(beamNode != NULL && type == BeamNode && beamNode->Node()->Name() == name) {
return(beamNode);
}
else if(controlNode != NULL && type == ControlNode && controlNode->Node()->Name() == name) {
return(controlNode);
}
}
return(NULL);
}
void Fidelity::GUI::ComponentItem::compileXmlFragment(QDomDocumentFragment& fragment) const
{
// Create component and set its attributes
QDomElement component = fragment.ownerDocument().createElement("component");
component.setAttribute("identifier",m_Component->Identifier());
component.setAttribute("label",m_Label->toPlainText());
component.setAttribute("uuid",m_Component->InstanceID().toString());
component.setAttribute("workbenchX",scenePos().x());
component.setAttribute("workbenchY",scenePos().y());
component.setAttribute("rotation",m_RotationAngle);
for(int i = 0; i < m_ParameterModel->rowCount(); i++) {
// Create parameter and set its attributes
QDomElement parameter = fragment.ownerDocument().createElement("parameter");
parameter.setAttribute("identifier",m_ParameterModel->item(i,0)->text());
parameter.setAttribute("unit",m_ParameterModel->item(i,3)->text());
parameter.setAttribute("datatype",m_ParameterModel->item(i,4)->text());
// Set parameter value
QDomText value;
if(m_ParameterModel->item(i,4)->text() != "bool") {
// Set normal text value
value = fragment.ownerDocument().createTextNode(m_ParameterModel->item(i,2)->text());
}
else {
// Boolean values are handled using checkable items instead of text
Qt::CheckState state = m_ParameterModel->item(i,2)->checkState();
if(state == Qt::Checked) {
value = fragment.ownerDocument().createTextNode("true");
}
else {
value = fragment.ownerDocument().createTextNode("false");
}
}
parameter.appendChild(value);
// Finally add complete parameter to component
component.appendChild(parameter);
}
fragment.firstChild().appendChild(component);
}
void Fidelity::GUI::ComponentItem::addComponentLinkItem(ComponentLinkItem* link)
{
// Be sure we're notified if the link is removed
connect(link, SIGNAL(linkDeleted(QPointer<ComponentLinkItem>)),
this, SLOT(removeComponentLinkItem(QPointer<ComponentLinkItem>)));
m_LinkItemList->append(link);
}
void Fidelity::GUI::ComponentItem::rotate(const int degrees)
{
// Rotate counter-clockwise
QGraphicsSvgItem::rotate(360 - degrees);
// Save and adjust current angle
m_RotationAngle += degrees;
if(m_RotationAngle >= 360) {
m_RotationAngle = 0;
}
}
// Event handlers
void Fidelity::GUI::ComponentItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
QGraphicsSvgItem::mousePressEvent(event);
setCursor(Qt::ClosedHandCursor);
setSelected(true);
// Update parameter table view
emit(componentClicked(m_ParameterModel));
}
void Fidelity::GUI::ComponentItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
QGraphicsSvgItem::mouseReleaseEvent(event);
setCursor(Qt::OpenHandCursor);
}
void Fidelity::GUI::ComponentItem::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
{
QGraphicsSvgItem::hoverEnterEvent(event);
setCursor(Qt::OpenHandCursor);
}
void Fidelity::GUI::ComponentItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
{
QGraphicsSvgItem::hoverLeaveEvent(event);
unsetCursor();
}
QVariant Fidelity::GUI::ComponentItem::itemChange(GraphicsItemChange change, const QVariant& value)
{
if(change == ItemPositionHasChanged) {
// Notify others that we got modified
emit componentModified();
}
return QGraphicsSvgItem::itemChange(change, value);
}
// Slots
void Fidelity::GUI::ComponentItem::parameterModelItemChanged(QStandardItem* item)
{
Q_UNUSED(item);
// Notify others that we got modified
emit componentModified();
}
void Fidelity::GUI::ComponentItem::labelChanged(QString label)
{
// Notify others that we got modified
m_Component->setLabel(label);
emit componentModified();
}
void Fidelity::GUI::ComponentItem::removeComponentLinkItem(QPointer<ComponentLinkItem> item)
{
m_LinkItemList->removeAll(item);
}
// Accessor methods
IComponent* Fidelity::GUI::ComponentItem::Component() const
{
return(m_Component);
}
void Fidelity::GUI::ComponentItem::setLabel(QString label)
{
m_Label->setPlainText(label);
m_Component->setLabel(label);
}
QStandardItemModel* Fidelity::GUI::ComponentItem::ParameterModel() const
{
return(m_ParameterModel);
}