valentina_old/src/app/dialogs/app/dialogpatternxmledit.cpp

778 lines
25 KiB
C++
Raw Normal View History

/************************************************************************
**
** @file dialogpatternxmledit.cpp
** @author Patrick Proy <patrick(at)proy.org>
** @date 14 5, 2014
**
** @brief Code file for the XML editor dialog
** @copyright
** This source code is part of the Valentine project, a pattern making
** program, whose allow create and modeling patterns of clothing.
** Copyright (C) 2013 Valentina project
** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
**
** Valentina 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 3 of the License, or
** (at your option) any later version.
**
** Valentina 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 Valentina. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#include "dialogpatternxmledit.h"
#include "ui_dialogpatternxmledit.h"
const short int DialogPatternXmlEdit::ChangeTypeDelete=1;
const short int DialogPatternXmlEdit::ChangeTypeAdd=2;
const short int DialogPatternXmlEdit::ChangeTypeModify=3;
2014-05-16 17:13:45 +02:00
DialogPatternXmlEdit::DialogPatternXmlEdit(QWidget *parent,VPattern *xmldoc):QDialog(parent),
ui(new Ui::DialogPatternXmlEdit)
{
ui->setupUi(this);
2014-05-16 16:49:58 +02:00
xmlmodel = new VXMLTreeView();
2014-05-16 17:13:45 +02:00
currentNodeEdited=nullptr;
treeChange=false;
2014-05-16 17:13:45 +02:00
this->changeStackRoot = this->changeStackLast =nullptr ;
doc=xmldoc;
root = doc->documentElement();
// disable edit trigers :
//ui->treeView_main->editTriggers(QTreeView::NoEditTriggers);
ui->treeView_main->setEditTriggers(QTreeView::NoEditTriggers);
ui->treeView_main->setWindowTitle("XML");
rootNode = (QStandardItem*) xmlmodel->invisibleRootItem();
qint16 drawnum=xmldoc->elementsByTagName("draw").size();
2014-05-16 16:49:58 +02:00
rootBases = new VXMLTreeElement*[drawnum+1];
rootBasesNum=1;
2014-05-16 16:49:58 +02:00
rootBases[0]=new VXMLTreeElement("Valentina",VXMLTreeElement::TypeRoot,root,false);
// TODO : OOM exception.
2014-05-16 16:49:58 +02:00
VXMLTreeElement* standard_base = new VXMLTreeElement("Valentina",VXMLTreeElement::TypeRoot,root,false);
ui->comboBox_Base_Selection->addItem(tr("All drawings"),QVariant(0));
//rootBases[0]=(QDomNode) doc->DocumentNode;
rootNode->appendRow(standard_base);
2014-05-16 16:49:58 +02:00
ReadNodes(root,standard_base,xmlmodel,false);
ui->treeView_main->setModel(xmlmodel);
//ui->treeView_main->expandAll();
// connectors
2014-05-16 17:13:45 +02:00
connect(ui->comboBox_Base_Selection,static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
this,&DialogPatternXmlEdit::BaseSelectionChanged);
2014-05-16 16:49:58 +02:00
connect(ui->treeView_main,&QTreeView::clicked,this,&DialogPatternXmlEdit::ElementClicked);
connect(ui->lineEdit_Name,&QLineEdit::textEdited,this,&DialogPatternXmlEdit::NameTextEdited);
connect(ui->lineEdit_Value,&QLineEdit::textEdited,this,&DialogPatternXmlEdit::ValueTextEdited);
connect(ui->pushButton_Set_Values,&QPushButton::clicked,this,&DialogPatternXmlEdit::ButtonSetClicked);
connect(ui->pushButton_Cancel_Values,&QPushButton::clicked,this,&DialogPatternXmlEdit::ButtonCancelClicked);
2014-05-16 17:13:45 +02:00
connect(ui->pushButton_Remove_attribute,&QPushButton::clicked,
this,&DialogPatternXmlEdit::ButtonDeleteAttributeClicked);
2014-05-16 16:49:58 +02:00
connect(ui->pushButton_Add_son,&QPushButton::clicked,this,&DialogPatternXmlEdit::ButtonAddSonClicked);
connect(ui->pushButton_Add_attribute,&QPushButton::clicked,this,&DialogPatternXmlEdit::ButtonAddAttributeClicked);
connect(ui->pushButton_Apply_Changes,&QPushButton::clicked,this,&DialogPatternXmlEdit::ButtonApplyChangesClicked);
}
2014-05-16 16:49:58 +02:00
DialogPatternXmlEdit::ChangesStackElement* DialogPatternXmlEdit::CreateStackElement(short int typechange)
{
ChangesStackElement* tmp;
2014-05-16 16:49:58 +02:00
tmp= new ChangesStackElement;
//if (tmp==0) TODO
tmp->type=typechange;
tmp->changedText=tmp->changedValue=false;
2014-05-16 17:13:45 +02:00
if (changeStackLast==nullptr )
2014-05-16 16:49:58 +02:00
{
changeStackLast=changeStackRoot=tmp;
2014-05-16 16:49:58 +02:00
}
else
{
changeStackLast->next=tmp;
changeStackLast=changeStackLast->next;
}
2014-05-16 17:13:45 +02:00
changeStackLast->newValue=changeStackLast->newText=nullptr;
changeStackLast->next=nullptr;
return changeStackLast;
}
2014-05-16 16:49:58 +02:00
void DialogPatternXmlEdit::ButtonApplyChangesClicked()
{
QString Changes="";
ChangesStackElement* currentChange;
2014-05-16 17:13:45 +02:00
if (this->changeStackRoot==nullptr)
2014-05-16 16:49:58 +02:00
{
Changes="No Changes";
}
else
{
currentChange=this->changeStackRoot;
2014-05-16 16:49:58 +02:00
do
{
if (currentChange->type == DialogPatternXmlEdit::ChangeTypeAdd)
{
Changes += QString("Added type %1 : ").arg(currentChange->element->getelementType());
Changes += currentChange->element->gettreeNodeName();
Changes += "/";
Changes += (currentChange->element->gettreeNodeValueSet()) ?
currentChange->element->gettreeNodeValue(): QString(tr("<no value>"));
Changes += "\n";
2014-05-16 16:49:58 +02:00
}
else
{
if (currentChange->type == DialogPatternXmlEdit::ChangeTypeModify)
{
Changes += QString("Modified type %1 : ").arg(currentChange->element->getelementType());
Changes += (currentChange->changedText) ? *currentChange->newText : QString(tr("Unchanged"));
Changes += "/";
Changes += (currentChange->changedValue) ? *currentChange->newValue: QString(tr("Unchanged"));
Changes += "\n";
2014-05-16 16:49:58 +02:00
}
else
{
if (currentChange->type == DialogPatternXmlEdit::ChangeTypeDelete)
{
Changes += QString("Deleted type %1 : ").arg(currentChange->element->getelementType());
Changes += currentChange->element->gettreeNodeName();
Changes += "/";
Changes += (currentChange->element->gettreeNodeValueSet()) ?
currentChange->element->gettreeNodeValue(): QString(tr("<no value>"));
Changes += "\n";
}
}
}
currentChange=currentChange->next;
2014-05-16 17:13:45 +02:00
} while (currentChange != nullptr);
}
QMessageBox::information(this, "Changes (not REALLY applied for now)", Changes);
// TODO : clear stack and apply
}
2014-05-16 16:49:58 +02:00
void DialogPatternXmlEdit::ButtonCancelClicked()
{
this->ClearEditData();
if (this->currentNodeEditedStatus==DialogPatternXmlEdit::ChangeTypeDelete)
{
if (this->currentNodeEdited->getelementType() == VXMLTreeElement::TypeAttr)
{
// Attribute previously deleted -> resore
2014-05-16 16:49:58 +02:00
QFont textfont=currentNodeEdited->font();
textfont.setStrikeOut(false);
2014-05-16 16:49:58 +02:00
this->currentNodeEdited->setFont(textfont);
this->RemoveChangeStackElement(this->currentNodeEditedStack);
}
else
{
QMessageBox::information(this, "Not Implemented", "Not Implemented");
}
}
}
2014-05-16 16:49:58 +02:00
void DialogPatternXmlEdit::RemoveChangeStackElement(ChangesStackElement* elmt)
{
ChangesStackElement* index = this->changeStackRoot;
2014-05-16 16:49:58 +02:00
if (changeStackRoot == elmt)
{
2014-05-16 17:13:45 +02:00
if (elmt->newText != nullptr)
2014-05-16 16:49:58 +02:00
{
delete elmt->newText;
}
2014-05-16 17:13:45 +02:00
if (elmt->newValue != nullptr)
2014-05-16 16:49:58 +02:00
{
delete elmt->newValue;
}
this->changeStackRoot = elmt->next;
2014-05-16 16:49:58 +02:00
if (this->changeStackLast == elmt)
{
this->changeStackLast= elmt->next;
}
delete elmt;
return;
}
2014-05-16 17:13:45 +02:00
while (index->next!=nullptr) {
2014-05-16 16:49:58 +02:00
if (index->next == elmt)
{
break;
}
index=index->next;
}
2014-05-16 17:13:45 +02:00
if (index->next == nullptr)
2014-05-16 16:49:58 +02:00
{
// TODO : debug error here
return;
}
2014-05-16 17:13:45 +02:00
if (index->next->newText != nullptr)
2014-05-16 16:49:58 +02:00
{
delete index->next->newText;
}
2014-05-16 17:13:45 +02:00
if (index->next->newValue != nullptr)
2014-05-16 16:49:58 +02:00
{
delete index->next->newValue;
}
index->next=index->next->next;
2014-05-16 16:49:58 +02:00
if (this->changeStackLast == elmt)
{
this->changeStackLast= index;
}
delete elmt;
}
2014-05-16 16:49:58 +02:00
void DialogPatternXmlEdit::ButtonDeleteAttributeClicked()
{
ChangesStackElement* newstack;
// TODO : maybe assert functions here
2014-05-16 17:13:45 +02:00
if (this->currentNodeEdited == nullptr)
2014-05-16 16:49:58 +02:00
{
return;
}
if (this->currentNodeEdited->getelementType() != VXMLTreeElement::TypeAttr)
{
return;
}
newstack = this->CreateStackElement(DialogPatternXmlEdit::ChangeTypeDelete);
2014-05-16 16:49:58 +02:00
newstack->element= this->currentNodeEdited;
2014-05-16 16:49:58 +02:00
QFont textfont=currentNodeEdited->font();
textfont.setStrikeOut(true);
2014-05-16 16:49:58 +02:00
this->currentNodeEdited->setFont(textfont);
// Activate changes
this->treeChange=true;
ui->pushButton_Apply_Changes->setEnabled(true);
// clear dialog
2014-05-16 16:49:58 +02:00
this->ClearEditData();
}
2014-05-16 16:49:58 +02:00
void DialogPatternXmlEdit::ButtonAddSonClicked()
{
ChangesStackElement* newstack;
2014-05-16 16:49:58 +02:00
bool ok;
//clear_edit_data();
// TODO : maybe assert functions here
2014-05-16 17:13:45 +02:00
if (this->currentNodeEdited == nullptr)
2014-05-16 16:49:58 +02:00
{
return;
}
if (this->currentNodeEdited->getelementType() != VXMLTreeElement::TypeNode)
{
return;
}
QString name = QInputDialog::getText(this, tr("Node Name"),
tr("Name:"), QLineEdit::Normal,
"", &ok);
2014-05-16 16:49:58 +02:00
if (ok==false)
{
return;
}
QString value = QInputDialog::getText(this, tr("Node Value (may be empty)"),
tr("Value:"), QLineEdit::Normal,
"", &ok);
2014-05-16 16:49:58 +02:00
if (ok==false)
{
return;
}
// create element tree but do not add attribute in DOM (will be done in apply).
QDomNode empty;
2014-05-16 16:49:58 +02:00
VXMLTreeElement* tElement = new VXMLTreeElement(
name,
2014-05-16 16:49:58 +02:00
VXMLTreeElement::TypeNode,
empty,true);
2014-05-16 16:49:58 +02:00
if (!value.isEmpty())
{
tElement->setTreeNodeValue(value);
}
currentNodeEdited->appendRow(tElement);
xmlmodel->appendchain(tElement);
2014-05-16 16:49:58 +02:00
QFont textfont=currentNodeEdited->font();
textfont.setBold(true);
tElement->setFont(textfont);
// Add to change stack
newstack = this->CreateStackElement(DialogPatternXmlEdit::ChangeTypeAdd);
newstack->element=tElement;
newstack->newText=new QString(name); newstack->changedText=true;
2014-05-16 16:49:58 +02:00
if (value.isEmpty())
{
newstack->changedValue=true;newstack->newValue=new QString(value);
}
// Activate changes
treeChange=true;
ui->pushButton_Apply_Changes->setEnabled(true);
}
2014-05-16 16:49:58 +02:00
void DialogPatternXmlEdit::ButtonAddAttributeClicked()
{
ChangesStackElement* newstack;
bool ok;
QDomNode empty; // empty QNode to pas to create function : maybe a better way to do this ?
// TODO : maybe assert functions here
2014-05-16 17:13:45 +02:00
if (this->currentNodeEdited == nullptr)
{
return;
}
if (this->currentNodeEdited->getelementType() != VXMLTreeElement::TypeNode)
2014-05-16 16:49:58 +02:00
{
return;
}
QString name = QInputDialog::getText(this, tr("Attribute Name"),
tr("Name:"), QLineEdit::Normal,
"", &ok);
2014-05-16 16:49:58 +02:00
if (ok==false)
{
return;
}
QString value = QInputDialog::getText(this, tr("Attribute Value"),
tr("Value:"), QLineEdit::Normal,
"", &ok);
2014-05-16 16:49:58 +02:00
if (ok==false)
{
return;
}
// create element tree but do not add attribute in DOM (will be done in apply).
2014-05-16 16:49:58 +02:00
VXMLTreeElement* tElement = new VXMLTreeElement(
name,
2014-05-16 16:49:58 +02:00
VXMLTreeElement::TypeAttr,
empty,true);
tElement->setTreeNodeValue(value);
2014-05-16 16:49:58 +02:00
currentNodeEdited->appendRow(tElement);
xmlmodel->appendchain(tElement);
2014-05-16 16:49:58 +02:00
QFont textfont=currentNodeEdited->font();
textfont.setBold(true);
tElement->setFont(textfont);
// Add to change stack
newstack = this->CreateStackElement(DialogPatternXmlEdit::ChangeTypeAdd);
newstack->element= tElement;
2014-05-16 16:49:58 +02:00
newstack->type=VXMLTreeElement::TypeAttr;
newstack->newText=new QString(name);
newstack->newValue=new QString(value);
// Activate changes
treeChange=true;
ui->pushButton_Apply_Changes->setEnabled(true);
}
2014-05-16 16:49:58 +02:00
void DialogPatternXmlEdit::ButtonSetClicked()
{
QString name,value;
ChangesStackElement* newstack;
// TODO : check if a change has already been done
2014-05-16 17:13:45 +02:00
if (this->currentNodeEdited == nullptr) return;
name=ui->lineEdit_Name->text();
value=ui->lineEdit_Value->text();
bool nameedit,valueedit;
2014-05-16 16:49:58 +02:00
nameedit = (name != currentNodeEdited->gettreeNodeName()) ? true : false;
valueedit= (value != currentNodeEdited->gettreeNodeValue()) ? true : false;
2014-05-16 16:49:58 +02:00
if ( nameedit == true || valueedit == true )
{
newstack = this->CreateStackElement(DialogPatternXmlEdit::ChangeTypeModify);
2014-05-16 16:49:58 +02:00
newstack->element= this->currentNodeEdited;
2014-05-16 16:49:58 +02:00
if (nameedit == true)
{
newstack->changedText=true;
newstack->newText=new QString(name);
2014-05-16 16:49:58 +02:00
currentNodeEdited->setTreeNodeName(name);
}
2014-05-16 16:49:58 +02:00
if (valueedit == true)
{
newstack->changedValue=true;
newstack->newValue= new QString(value);
2014-05-16 16:49:58 +02:00
currentNodeEdited->setTreeNodeValue(value);
}
2014-05-16 16:49:58 +02:00
QFont textfont=currentNodeEdited->font();
textfont.setBold(true);
2014-05-16 16:49:58 +02:00
currentNodeEdited->setFont(textfont);
treeChange=true;
ui->pushButton_Apply_Changes->setEnabled(true);
2014-05-16 16:49:58 +02:00
ClearEditData();
}
}
2014-05-16 16:49:58 +02:00
void DialogPatternXmlEdit::NameTextEdited(QString newtext)
{
//QMessageBox::information(this, "nameTextEdited", QString("%1").arg(newtext));
ui->pushButton_Set_Values->setEnabled(true);
ui->pushButton_Cancel_Values->setEnabled(true);
}
2014-05-16 16:49:58 +02:00
void DialogPatternXmlEdit::ValueTextEdited(QString newtext)
{
//QMessageBox::information(this, "valueTextEdited", QString("%1").arg(newtext));
ui->pushButton_Set_Values->setEnabled(true);
ui->pushButton_Cancel_Values->setEnabled(true);
}
2014-05-16 16:49:58 +02:00
void DialogPatternXmlEdit::ClearEditData()
{
ui->lineEdit_Name->setText("");
ui->lineEdit_Value->setText("");
ui->lineEdit_Name->setEnabled(false);
ui->lineEdit_Value->setEnabled(false);
ui->label_type_value->setText(tr("No selection"));
ui->pushButton_Set_Values->setEnabled(false);
ui->pushButton_Add_attribute->setEnabled(false);
ui->pushButton_Add_son->setEnabled(false);
ui->pushButton_Remove_attribute->setEnabled(false);
ui->pushButton_Cancel_Values->setEnabled(false);
}
2014-05-16 16:49:58 +02:00
void DialogPatternXmlEdit::ClearStack()
{
ChangesStackElement * tmp;
2014-05-16 17:13:45 +02:00
while (changeStackRoot != nullptr)
2014-05-16 16:49:58 +02:00
{
2014-05-16 17:13:45 +02:00
if (changeStackRoot->newText != nullptr)
2014-05-16 16:49:58 +02:00
{
delete changeStackRoot->newText;
}
2014-05-16 17:13:45 +02:00
if (changeStackRoot->newValue != nullptr)
2014-05-16 16:49:58 +02:00
{
delete changeStackRoot->newValue;
}
tmp=changeStackRoot;
changeStackRoot=changeStackRoot->next;
2014-05-16 16:49:58 +02:00
delete tmp;
}
2014-05-16 17:13:45 +02:00
changeStackLast=nullptr;
}
2014-05-16 16:49:58 +02:00
void DialogPatternXmlEdit::BaseSelectionChanged(int value)
{
QDomNode newbase;
int index;
2014-05-16 16:49:58 +02:00
if (this->treeChange == true)
{//Changes have been made : warn ?
}
2014-05-16 16:49:58 +02:00
ClearStack();
ui->pushButton_Apply_Changes->setEnabled(false);
index = ui->comboBox_Base_Selection->itemData(value).toInt(); //.convert(QVariant::Int);
Q_ASSERT(value < rootBasesNum);
// QMessageBox::information(this, "test", QString("%1:%2").arg(value).arg(index));
// Clear all tree info and nodes
// TODO : Check
2014-05-16 16:49:58 +02:00
while (rootNode->rowCount() > 0)
{
rootNode->removeRow(0);
}
2014-05-16 16:49:58 +02:00
xmlmodel->ClearTree();
2014-05-16 16:49:58 +02:00
VXMLTreeElement* standard_base = new VXMLTreeElement(
rootBases[index]->gettreeNodeName(),
2014-05-16 16:49:58 +02:00
VXMLTreeElement::TypeRoot,
rootBases[index]->getDocNode(),
false);
rootNode->appendRow(standard_base);
2014-05-16 16:49:58 +02:00
ReadNodes(rootBases[index]->getDocNode(),standard_base,xmlmodel,true);
ui->treeView_main->setModel(xmlmodel);
2014-05-16 16:49:58 +02:00
this->ClearEditData();
// TODO : warn if changes ?
2014-05-16 16:49:58 +02:00
this->ClearStack();
return;
}
2014-05-16 16:49:58 +02:00
void DialogPatternXmlEdit::ElementClicked ( const QModelIndex & index )
{
// Get item (function returns parent of clicked item)
2014-05-16 16:49:58 +02:00
VXMLTreeElement *item = (VXMLTreeElement *)index.internalPointer();
// Get child specified by index row/column
2014-05-16 16:49:58 +02:00
VXMLTreeElement * item2 = (VXMLTreeElement *) item->child(index.row(),index.column());
// Clear all data and disable buttons
2014-05-16 16:49:58 +02:00
this->ClearEditData();
// Check status of element in change stack
2014-05-16 16:49:58 +02:00
this->currentNodeEdited=item2;
this->currentNodeEditedStatus=0;
DialogPatternXmlEdit::ChangesStackElement * local = this->changeStackRoot;
2014-05-16 17:13:45 +02:00
while (local != nullptr)
2014-05-16 16:49:58 +02:00
{
if (local->element == item2)
{
this->currentNodeEditedStatus=local->type;
this->currentNodeEditedStack=local;
if (local->type == DialogPatternXmlEdit::ChangeTypeDelete)
{
// Node is deleted, only enable cancel button
ui->pushButton_Cancel_Values->setEnabled(true);
return;
}
}
local=local->next;
}
// Fill the dialog and Activate the line edit
2014-05-16 16:49:58 +02:00
if (item2->getelementType() != VXMLTreeElement::TypeRoot)
{
ui->lineEdit_Name->setText(currentNodeEdited->gettreeNodeName());
ui->lineEdit_Name->setEnabled(true);
// Fill value
2014-05-16 16:49:58 +02:00
ui->lineEdit_Value->setText(currentNodeEdited->gettreeNodeValue());
ui->lineEdit_Value->setEnabled(true);
}
2014-05-16 16:49:58 +02:00
if (item2->getelementType() == VXMLTreeElement::TypeRoot)
{
ui->label_type_value->setText(tr("Root node"));;
ui->pushButton_Add_son->setEnabled(true);
2014-05-16 16:49:58 +02:00
}
else if (item2->getelementType() == VXMLTreeElement::TypeNode)
{
ui->label_type_value->setText(tr("Node"));
ui->pushButton_Add_attribute->setEnabled(true);
ui->pushButton_Add_son->setEnabled(true);
2014-05-16 16:49:58 +02:00
}
else if (item2->getelementType() == VXMLTreeElement::TypeAttr)
{
ui->label_type_value->setText(tr("Attribute"));
ui->pushButton_Remove_attribute->setEnabled(true);
}
}
DialogPatternXmlEdit::~DialogPatternXmlEdit()
{
//xmlmodel->clearTree();
delete ui;
2014-05-16 16:49:58 +02:00
ChangesStackElement * tmp;
2014-05-16 17:13:45 +02:00
while (changeStackRoot != nullptr)
2014-05-16 16:49:58 +02:00
{
tmp=changeStackRoot->next;
2014-05-16 16:49:58 +02:00
delete changeStackRoot;
changeStackRoot=tmp;
}
}
2014-05-16 16:49:58 +02:00
void DialogPatternXmlEdit::ReadNodes(QDomNode dNode, VXMLTreeElement* root,VXMLTreeView* xmlmodel, bool refresh)
{
QDomNode tNode,tNode2;
2014-05-16 16:49:58 +02:00
VXMLTreeElement* tElement,*tElement2;
int totalOfChilds = dNode.childNodes().size();
2014-05-16 16:49:58 +02:00
if (totalOfChilds == 0)
{
return; // empty xml
}
for (int i=0;i<totalOfChilds;i++)
{
tNode = dNode.childNodes().at(i);
2014-05-16 16:49:58 +02:00
if (tNode.nodeType() != QDomNode::CommentNode)
{
tElement = new VXMLTreeElement(tNode.nodeName(),VXMLTreeElement::TypeNode,tNode,false);
xmlmodel->appendchain(tElement);
root->appendRow(tElement);
2014-05-16 16:49:58 +02:00
if (tNode.nodeName() == "draw")
{
for (int i = 0; i < tNode.attributes().size(); i++)
{
if ((tNode.attributes().item(i).nodeName() == "name")
2014-05-16 16:49:58 +02:00
&& (refresh == false))
{
ui->comboBox_Base_Selection->addItem(tNode.attributes().item(i).nodeValue(),
QVariant(rootBasesNum));
2014-05-16 16:49:58 +02:00
rootBases[rootBasesNum]=new VXMLTreeElement(
QString("Drawing %1").arg(tNode.attributes().item(i).nodeValue()),
2014-05-16 16:49:58 +02:00
VXMLTreeElement::TypeRoot,tNode,false);;
rootBasesNum++;
//QStandardItem* rootNode = (QStandardItem*) rootBases[rootBasesNum]->invisibleRootItem();
//rootNode->appendRow(tElement);
}
}
}
}
}
do
{
tElement=xmlmodel->getCurrent()->elmt;
tNode=tElement->getDocNode();
2014-05-16 16:49:58 +02:00
if (tElement->getelementType() == VXMLTreeElement::TypeNode)
{
totalOfChilds = tNode.childNodes().size();
2014-05-16 16:49:58 +02:00
for (int i=0;i<totalOfChilds;i++)
{
tNode2 = tNode.childNodes().at(i);
2014-05-16 16:49:58 +02:00
if (tNode2.nodeType() != QDomNode::CommentNode)
{
tElement2 = new VXMLTreeElement(tNode2.nodeName(),VXMLTreeElement::TypeNode,tNode2,false);
xmlmodel->appendchain(tElement2);
tElement->appendRow(tElement2);
}
}
2014-05-16 16:49:58 +02:00
if (tNode.nodeType() == QDomNode::TextNode)
{
tElement->setTreeNodeValue(tNode.nodeValue());
}
for (int i = 0; i < tNode.attributes().size(); i++)
{
2014-05-16 16:49:58 +02:00
tElement2 = new VXMLTreeElement(
tNode.attributes().item(i).nodeName(),
2014-05-16 16:49:58 +02:00
VXMLTreeElement::TypeAttr,
tNode,false);
tElement2->setTreeNodeValue(tNode.attributes().item(i).nodeValue());
tElement->appendRow(tElement2);
xmlmodel->appendchain(tElement2);
}
}
2014-05-16 16:49:58 +02:00
xmlmodel->SetCurrent(xmlmodel->getCurrent()->next);
}
while (xmlmodel->getCurrent() != 0);
return;
}
// vXMLTreeView Code -----------------------------------------------------------------
2014-05-16 16:49:58 +02:00
VXMLTreeView::VXMLTreeView(QObject *parent) : QStandardItemModel(parent)
{
2014-05-16 17:13:45 +02:00
current=last=items=nullptr;
}
2014-05-16 16:49:58 +02:00
void VXMLTreeView::ClearTree() {
// TODO check if treeitems are deleted with the delete row command of QDom
//clear the chain link
TreeElementchain * tmp;
2014-05-16 17:13:45 +02:00
while (items != nullptr)
2014-05-16 16:49:58 +02:00
{
tmp=items;
items=items->next;
2014-05-16 16:49:58 +02:00
delete tmp;
}
// reset chain counters
2014-05-16 17:13:45 +02:00
current=last=items=nullptr;
}
2014-05-16 16:49:58 +02:00
VXMLTreeView::~VXMLTreeView()
{
}
2014-05-16 16:49:58 +02:00
void VXMLTreeView::appendchain(VXMLTreeElement* elmt)
{
2014-05-16 17:13:45 +02:00
if (last == nullptr)
2014-05-16 16:49:58 +02:00
{ // first element
current = new TreeElementchain;
2014-05-16 17:13:45 +02:00
if (current == nullptr)
2014-05-16 16:49:58 +02:00
{
2014-05-16 17:13:45 +02:00
Q_ASSERT(current != nullptr);
// TODO : throw exception
}
current->elmt=elmt;
2014-05-16 17:13:45 +02:00
current->next=nullptr;
last=items=current;
return;
}
2014-05-16 16:49:58 +02:00
TreeElementchain* temp= new TreeElementchain;
2014-05-16 17:13:45 +02:00
if (temp == nullptr)
2014-05-16 16:49:58 +02:00
{
2014-05-16 17:13:45 +02:00
Q_ASSERT(temp != nullptr);
// TODO : throw exception
}
temp->elmt=elmt;
2014-05-16 17:13:45 +02:00
temp->next=nullptr;
last->next=temp;
last=temp;
}
// vXMLTreeElement code -----------------------------------------------------------------
2014-05-16 16:49:58 +02:00
const short int VXMLTreeElement::TypeNode=1;
const short int VXMLTreeElement::TypeAttr=2;
const short int VXMLTreeElement::TypeRoot=3;
2014-05-16 16:49:58 +02:00
VXMLTreeElement::VXMLTreeElement(QString name, int nodetype, QDomNode source,bool editor): QStandardItem(name)
{
this->elementType=nodetype;
this->DocNode=source;
this->addedNode=editor;
2014-05-16 16:49:58 +02:00
if (!editor)
{
this->DocNode=source;
}
this->treeNodeName=name;
this->treeNodeValue="<empty>"; // TODO : translation ?
this->treeNodeValueSet=false,
this->setText(this->displayText());
2014-05-16 16:49:58 +02:00
switch (this->elementType)
{
case VXMLTreeElement::TypeAttr:
this->setBackground(BACKGROUND_COLOR_ATTRIBUTE);
break;
default:
break;
}
}
2014-05-16 16:49:58 +02:00
VXMLTreeElement::~VXMLTreeElement()
{
}
2014-05-16 16:49:58 +02:00
void VXMLTreeElement::setTreeNodeValue(QString value)
{
this->treeNodeValue=value;
this->treeNodeValueSet=true;
this->setText(this->displayText());
}
2014-05-16 16:49:58 +02:00
void VXMLTreeElement::setTreeNodeName(QString value)
{
this->treeNodeName=value;
this->setText(this->displayText());
}
2014-05-16 16:49:58 +02:00
QString VXMLTreeElement::displayText()
{
2014-05-16 16:49:58 +02:00
if (this->elementType == VXMLTreeElement::TypeRoot)
return this->treeNodeName;
return QString("%1 : %2").arg((this->treeNodeName)).arg(this->treeNodeValue);
}
2014-05-16 16:49:58 +02:00
QString VXMLTreeElement::gettreeNodeValue() {
// Only return value if it's really set.
if (this->treeNodeValueSet)
return this->treeNodeValue;
else
return "";
}
2014-05-16 16:49:58 +02:00