From 9bf2c4662da7f0d155c414b55af2cd8eaf02bee0 Mon Sep 17 00:00:00 2001 From: Patrick Proy Date: Fri, 16 May 2014 13:19:22 +0200 Subject: [PATCH 01/15] Branch create --HG-- branch : XML Editor --- src/app/mainwindow.ui | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/app/mainwindow.ui b/src/app/mainwindow.ui index ce4f28639..7c407b282 100644 --- a/src/app/mainwindow.ui +++ b/src/app/mainwindow.ui @@ -696,6 +696,7 @@ + @@ -999,6 +1000,11 @@ Pattern properties + + + Edit pattern XML code + + From f51f7030263ad6adcf41eda4d16c36d7c01e582b Mon Sep 17 00:00:00 2001 From: Patrick Proy Date: Fri, 16 May 2014 14:51:39 +0200 Subject: [PATCH 02/15] Main code push after branch creation --HG-- branch : XML Editor --- src/app/dialogs/app/dialogpatternxmledit.cpp | 692 +++++++++++++++++++ src/app/dialogs/app/dialogpatternxmledit.h | 245 +++++++ src/app/dialogs/app/dialogpatternxmledit.ui | 351 ++++++++++ src/app/dialogs/dialogs.h | 1 + src/app/dialogs/dialogs.pri | 9 +- src/app/mainwindow.cpp | 10 + src/app/mainwindow.h | 5 + 7 files changed, 1310 insertions(+), 3 deletions(-) create mode 100644 src/app/dialogs/app/dialogpatternxmledit.cpp create mode 100644 src/app/dialogs/app/dialogpatternxmledit.h create mode 100644 src/app/dialogs/app/dialogpatternxmledit.ui diff --git a/src/app/dialogs/app/dialogpatternxmledit.cpp b/src/app/dialogs/app/dialogpatternxmledit.cpp new file mode 100644 index 000000000..85c068c1c --- /dev/null +++ b/src/app/dialogs/app/dialogpatternxmledit.cpp @@ -0,0 +1,692 @@ +/************************************************************************ + ** + ** @file dialogpatternxmledit.cpp + ** @author Patrick Proy + ** @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 + ** 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 . + ** + *************************************************************************/ + + +#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; + +DialogPatternXmlEdit::DialogPatternXmlEdit(QWidget *parent,VPattern *xmldoc) : + QDialog(parent), + ui(new Ui::DialogPatternXmlEdit) +{ + ui->setupUi(this); + + xmlmodel = new vXMLTreeView(); + current_node_edited=0; + treeChange=false; + this->changeStackRoot = this->changeStackLast =0; + + 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(); + rootBases = (vXMLTreeElement**)malloc(sizeof(vXMLTreeElement*)*(drawnum+1)); + rootBasesNum=1; + rootBases[0]=new vXMLTreeElement("Valentina",vXMLTreeElement::TypeRoot,root,false); + // TODO : OOM exception. + + + 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); + + readNodes(root,standard_base,xmlmodel,false); + + ui->treeView_main->setModel(xmlmodel); + + //ui->treeView_main->expandAll(); + + // connectors + connect(ui->comboBox_Base_Selection, + static_cast(&QComboBox::currentIndexChanged), + this, + &DialogPatternXmlEdit::baseSelectionChanged); + connect(ui->treeView_main, + &QTreeView::clicked, + this, + &DialogPatternXmlEdit::Element_clicked); + 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); + connect(ui->pushButton_Remove_attribute, + &QPushButton::clicked, + this, + &DialogPatternXmlEdit::ButtonDeleteAttributeClicked); + 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); +} + +DialogPatternXmlEdit::ChangesStackElement* DialogPatternXmlEdit::CreateStackElement(short int typechange) { + ChangesStackElement* tmp; + + tmp= (ChangesStackElement* ) malloc(sizeof(ChangesStackElement)); + //if (tmp==0) TODO + tmp->type=typechange; + tmp->changedText=tmp->changedValue=false; + + if (changeStackLast==0) { + changeStackLast=changeStackRoot=tmp; + } else { + changeStackLast->next=tmp; + changeStackLast=changeStackLast->next; + } + changeStackLast->newValue=changeStackLast->newText=0; + changeStackLast->next=0; + return changeStackLast; +} + +void DialogPatternXmlEdit::ButtonApplyChangesClicked() { + QString Changes=""; + ChangesStackElement* currentChange; + if (this->changeStackRoot==0) Changes="No Changes"; + else { + currentChange=this->changeStackRoot; + 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("")); + Changes += "\n"; + } 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"; + } 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("")); + Changes += "\n"; + } + } + } + currentChange=currentChange->next; + } while (currentChange != 0); + } + QMessageBox::information(this, "Changes (not REALLY applied for now)", Changes); + // TODO : clear stack and apply +} + +void DialogPatternXmlEdit::ButtonCancelClicked(){ + this->clear_edit_data(); + if (this->current_node_edited_status=DialogPatternXmlEdit::ChangeTypeDelete) { + if (this->current_node_edited->getelementType() == vXMLTreeElement::TypeAttr) { + // Attribute previously deleted -> resore + QFont textfont=current_node_edited->font(); + textfont.setStrikeOut(false); + this->current_node_edited->setFont(textfont); + this->removeChangeStackElement(this->current_node_edited_stack); + } else { + QMessageBox::information(this, "Not Implemented", "Not Implemented"); + } + } +} + +void DialogPatternXmlEdit::removeChangeStackElement(ChangesStackElement* elmt) { + ChangesStackElement* index = this->changeStackRoot; + + if (changeStackRoot == elmt) { + if (elmt->newText != 0) free (elmt->newText); + if (elmt->newValue != 0) free (elmt->newValue); + this->changeStackRoot = elmt->next; + if (this->changeStackLast == elmt) { this->changeStackLast= elmt->next; } + free(elmt); + return; + } + while (index->next!=0) { + if (index->next == elmt) { + break; + } + index=index->next; + } + if (index->next == 0) { + // TODO : debug error here + return; + } + if (index->next->newText != 0) free (index->next->newText); + if (index->next->newValue != 0) free (index->next->newValue); + index->next=index->next->next; + if (this->changeStackLast == elmt) { this->changeStackLast= index;} + free(elmt); +} + +void DialogPatternXmlEdit::ButtonDeleteAttributeClicked(){ + ChangesStackElement* newstack; + + // TODO : maybe assert functions here + if (this->current_node_edited == 0) return; + if (this->current_node_edited->getelementType() != vXMLTreeElement::TypeAttr) return; + + newstack = this->CreateStackElement(DialogPatternXmlEdit::ChangeTypeDelete); + newstack->element= this->current_node_edited; + + QFont textfont=current_node_edited->font(); + textfont.setStrikeOut(true); + this->current_node_edited->setFont(textfont); + // Activate changes + this->treeChange=true; + ui->pushButton_Apply_Changes->setEnabled(true); + // clear dialog + this->clear_edit_data(); +} + +void DialogPatternXmlEdit::ButtonAddSonClicked(){ + ChangesStackElement* newstack; + //clear_edit_data(); + // TODO : maybe assert functions here + if (this->current_node_edited == 0) return; + if (this->current_node_edited->getelementType() != vXMLTreeElement::TypeNode) return; + + bool ok; + QString name = QInputDialog::getText(this, tr("Node Name"), + tr("Name:"), QLineEdit::Normal, + "", &ok); + if (!ok) return; + QString value = QInputDialog::getText(this, tr("Node Value (may be empty)"), + tr("Value:"), QLineEdit::Normal, + "", &ok); + if (!ok) return; + + // create element tree but do not add attribute in DOM (will be done in apply). + QDomNode empty; + vXMLTreeElement* tElement = new vXMLTreeElement( + name, + vXMLTreeElement::TypeNode, + empty,true); + + if (value !="") tElement->setTreeNodeValue(value); + current_node_edited->appendRow(tElement); + xmlmodel->appendchain(tElement); + QFont textfont=current_node_edited->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; + if (value !="") { newstack->changedValue=true;newstack->newValue=new QString(value);} + // Activate changes + treeChange=true; + ui->pushButton_Apply_Changes->setEnabled(true); +} + +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 + if (this->current_node_edited == 0) return; + if (this->current_node_edited->getelementType() != vXMLTreeElement::TypeNode) return; + + QString name = QInputDialog::getText(this, tr("Attribute Name"), + tr("Name:"), QLineEdit::Normal, + "", &ok); + if (!ok) return; + QString value = QInputDialog::getText(this, tr("Attribute Value"), + tr("Value:"), QLineEdit::Normal, + "", &ok); + if (!ok) return; + + // create element tree but do not add attribute in DOM (will be done in apply). + vXMLTreeElement* tElement = new vXMLTreeElement( + name, + vXMLTreeElement::TypeAttr, + empty,true); + + tElement->setTreeNodeValue(value); + current_node_edited->appendRow(tElement); + xmlmodel->appendchain(tElement); + QFont textfont=current_node_edited->font(); + textfont.setBold(true); + tElement->setFont(textfont); + // Add to change stack + newstack = this->CreateStackElement(DialogPatternXmlEdit::ChangeTypeAdd); + newstack->element= tElement; + newstack->type=vXMLTreeElement::TypeAttr; + newstack->newText=new QString(name); + newstack->newValue=new QString(value); + // Activate changes + treeChange=true; + ui->pushButton_Apply_Changes->setEnabled(true); +} + +void DialogPatternXmlEdit::ButtonSetClicked(){ + QString name,value; + ChangesStackElement* newstack; + + // TODO : check if a change has already been done + if (this->current_node_edited == 0) return; + name=ui->lineEdit_Name->text(); + value=ui->lineEdit_Value->text(); + bool nameedit,valueedit; + + nameedit = (name != current_node_edited->gettreeNodeName()) ? true : false; + valueedit= (value != current_node_edited->gettreeNodeValue()) ? true : false; + + if ( nameedit == true || valueedit == true ) { + newstack = this->CreateStackElement(DialogPatternXmlEdit::ChangeTypeModify); + newstack->element= this->current_node_edited; + + if (nameedit == true) { + newstack->changedText=true; + newstack->newText=new QString(name); + current_node_edited->setTreeNodeName(name); + } + if (valueedit == true) { + newstack->changedValue=true; + newstack->newValue= new QString(value); + current_node_edited->setTreeNodeValue(value); + } + + QFont textfont=current_node_edited->font(); + textfont.setBold(true); + current_node_edited->setFont(textfont); + treeChange=true; + ui->pushButton_Apply_Changes->setEnabled(true); + clear_edit_data(); + } +} + +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); +} + +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); +} + +void DialogPatternXmlEdit::clear_edit_data() { + 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); +} + +void DialogPatternXmlEdit::clearStack() { + ChangesStackElement * tmp; + while (changeStackRoot != 0) { + if (changeStackRoot->newText != 0) free (changeStackRoot->newText); + if (changeStackRoot->newValue != 0) free (changeStackRoot->newValue); + tmp=changeStackRoot; + changeStackRoot=changeStackRoot->next; + free(tmp); + } + changeStackLast=0; +} + +void DialogPatternXmlEdit::baseSelectionChanged(int value) +{ + QDomNode newbase; + + int index; + + if (this->treeChange == true) {//Changes have been made : warn ? + } + 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 + while (rootNode->rowCount() > 0) { + rootNode->removeRow(0); + } + xmlmodel->clearTree(); + + vXMLTreeElement* standard_base = new vXMLTreeElement( + rootBases[index]->gettreeNodeName(), + vXMLTreeElement::TypeRoot, + rootBases[index]->getDocNode(), + false); + rootNode->appendRow(standard_base); + readNodes(rootBases[index]->getDocNode(),standard_base,xmlmodel,true); + + ui->treeView_main->setModel(xmlmodel); + this->clear_edit_data(); + // TODO : warn if changes ? + this->clearStack(); + return; +} + +void DialogPatternXmlEdit::Element_clicked ( const QModelIndex & index ) { + + // Get item (function returns parent of clicked item) + vXMLTreeElement *item = (vXMLTreeElement *)index.internalPointer(); + + // Get child specified by index row/column + vXMLTreeElement * item2 = (vXMLTreeElement *) item->child(index.row(),index.column()); + + // Clear all data and disable buttons + this->clear_edit_data(); + // Check status of element in change stack + this->current_node_edited=item2; + this->current_node_edited_status=0; + DialogPatternXmlEdit::ChangesStackElement * local = this->changeStackRoot; + while (local != 0) { + if (local->element == item2) { + this->current_node_edited_status=local->type; + this->current_node_edited_stack=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 + if (item2->getelementType() != vXMLTreeElement::TypeRoot) { + ui->lineEdit_Name->setText(current_node_edited->gettreeNodeName()); + ui->lineEdit_Name->setEnabled(true); + // Fill value + ui->lineEdit_Value->setText(current_node_edited->gettreeNodeValue()); + ui->lineEdit_Value->setEnabled(true); + } + if (item2->getelementType() == vXMLTreeElement::TypeRoot) { + ui->label_type_value->setText(tr("Root node"));; + ui->pushButton_Add_son->setEnabled(true); + } 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); + } 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; + ChangesStackElement * tmp; while (changeStackRoot != 0) { + tmp=changeStackRoot->next; + free(changeStackRoot); + changeStackRoot=tmp; + } + //delete rootBases[0]; + //free(rootBases); + //delete xmlmodel; +} + +void DialogPatternXmlEdit::readNodes(QDomNode dNode, vXMLTreeElement* root,vXMLTreeView* xmlmodel, bool refresh) +{ + + QDomNode tNode,tNode2; + vXMLTreeElement* tElement,*tElement2; + int totalOfChilds = dNode.childNodes().size(); + if (totalOfChilds == 0) return; // empty xml + for (int i=0;iappendchain(tElement); + root->appendRow(tElement); + if (tNode.nodeName() == "draw") { + for (int i = 0; i < tNode.attributes().size(); i++) + { + if ((tNode.attributes().item(i).nodeName() == "name") + && (refresh == false)){ + ui->comboBox_Base_Selection->addItem(tNode.attributes().item(i).nodeValue(), + QVariant(rootBasesNum)); + rootBases[rootBasesNum]=new vXMLTreeElement( + QString("Drawing %1").arg(tNode.attributes().item(i).nodeValue()), + vXMLTreeElement::TypeRoot,tNode,false);; + rootBasesNum++; + //QStandardItem* rootNode = (QStandardItem*) rootBases[rootBasesNum]->invisibleRootItem(); + //rootNode->appendRow(tElement); + } + } + } + } + } + + do + { + tElement=xmlmodel->getCurrent()->elmt; + tNode=tElement->getDocNode(); + if (tElement->getelementType() == vXMLTreeElement::TypeNode) { + totalOfChilds = tNode.childNodes().size(); + for (int i=0;iappendchain(tElement2); + tElement->appendRow(tElement2); + } + } + + if (tNode.nodeType() == QDomNode::TextNode) { + tElement->setTreeNodeValue(tNode.nodeValue()); + } + + for (int i = 0; i < tNode.attributes().size(); i++) + { + tElement2 = new vXMLTreeElement( + tNode.attributes().item(i).nodeName(), + vXMLTreeElement::TypeAttr, + tNode,false); + tElement2->setTreeNodeValue(tNode.attributes().item(i).nodeValue()); + tElement->appendRow(tElement2); + xmlmodel->appendchain(tElement2); + } + } + xmlmodel->setCurrent(xmlmodel->getCurrent()->next); + } + while (xmlmodel->getCurrent() != 0); + return; +} + +// vXMLTreeView Code ----------------------------------------------------------------- +vXMLTreeView::vXMLTreeView(QObject *parent) : QStandardItemModel(parent) +{ + current=last=items=0; +} + +void vXMLTreeView::clearTree() { + + // TODO check if treeitems are deleted with the delete row command of QDom + + //clear the chain link + TreeElementchain * tmp; + while (items != 0) { + tmp=items; + items=items->next; + free (tmp); + } + // reset chain counters + current=last=items=0; + +} + +vXMLTreeView::~vXMLTreeView() +{ + +} + +void vXMLTreeView::appendchain(vXMLTreeElement* elmt) +{ + if (last == 0) { // first element + current=(TreeElementchain*)malloc(sizeof(TreeElementchain)); + if (current == 0) { + Q_ASSERT(current != 0); + // TODO : throw exception + } + current->elmt=elmt; + current->next=0; + last=items=current; + return; + } + TreeElementchain* temp=(TreeElementchain*)malloc(sizeof(TreeElementchain)); + if (temp == 0) { + Q_ASSERT(temp != 0); + // TODO : throw exception + } + temp->elmt=elmt; + temp->next=0; + last->next=temp; + last=temp; +} + +vXMLTreeView::TreeElementchain * vXMLTreeView::getCurrent() { return this->current;} +void vXMLTreeView::setCurrent(vXMLTreeView::TreeElementchain * value) { this->current=value;} +vXMLTreeView::TreeElementchain * vXMLTreeView::getLast() { return this->last;} +vXMLTreeView::TreeElementchain * vXMLTreeView::getItems() { return this->items;} + + +// vXMLTreeElement code ----------------------------------------------------------------- + +const short int vXMLTreeElement::TypeNode=1; +const short int vXMLTreeElement::TypeAttr=2; +const short int vXMLTreeElement::TypeRoot=3; + + +vXMLTreeElement::vXMLTreeElement(QString name, int nodetype, QDomNode source,bool editor) + : QStandardItem(name) +{ + this->elementType=nodetype; + this->DocNode=source; + this->addedNode=editor; + if (!editor) this->DocNode=source; + this->treeNodeName=name; + this->treeNodeValue=""; // TODO : translation ? + this->treeNodeValueSet=false, + this->setText(this->displayText()); + switch (this->elementType) { + case vXMLTreeElement::TypeAttr: + this->setBackground(BACKGROUND_COLOR_ATTRIBUTE); + break; + default: + break; + } +} + + +vXMLTreeElement::~vXMLTreeElement() +{ + +} +void vXMLTreeElement::setTreeNodeValue(QString value) +{ + this->treeNodeValue=value; + this->treeNodeValueSet=true; + this->setText(this->displayText()); +} + +void vXMLTreeElement::setTreeNodeName(QString value) +{ + this->treeNodeName=value; + this->setText(this->displayText()); +} + +QString vXMLTreeElement::displayText() +{ + if (this->elementType == vXMLTreeElement::TypeRoot) + return this->treeNodeName; + return QString("%1 : %2").arg((this->treeNodeName)).arg(this->treeNodeValue); +} + +short int vXMLTreeElement::getelementType() { return this->elementType;} + +QDomNode vXMLTreeElement::getDocNode() +{ + return this->DocNode; +} + +QString vXMLTreeElement::gettreeNodeName() { return this->treeNodeName;} +QString vXMLTreeElement::gettreeNodeValue() { + // Only return value if it's really set. + if (this->treeNodeValueSet) + return this->treeNodeValue; + else + return ""; +} +bool vXMLTreeElement::gettreeNodeValueSet() {return this->treeNodeValueSet;} diff --git a/src/app/dialogs/app/dialogpatternxmledit.h b/src/app/dialogs/app/dialogpatternxmledit.h new file mode 100644 index 000000000..6332c05d3 --- /dev/null +++ b/src/app/dialogs/app/dialogpatternxmledit.h @@ -0,0 +1,245 @@ +/************************************************************************ + ** + ** @file dialogpatternxmledit.h + ** @author Patrick Proy + ** @date 14 5, 2014 + ** + ** @brief Include 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 + ** 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 . + ** + *************************************************************************/ + +#ifndef DIALOGPATTERNXMLEDIT_H +#define DIALOGPATTERNXMLEDIT_H + +#include +#include +#include "../../xml/vpattern.h" + +//******************************************************************************************** +/** + * @brief The vXMLTreeElement class : node/attribute of xml pattern. Used by vXMLTreeView + * + ********************************************************************************************** + */ + +#define BACKGROUND_COLOR_ATTRIBUTE QBrush(Qt::GlobalColor::cyan) +class vXMLTreeElement : public QStandardItem +{ + +public: + /** + * @brief vXMLTreeElement + * @param name : display name + * @param nodetype : node type (node, attribute, root) + * @param source : the source dom node + */ + explicit vXMLTreeElement (QString name, int nodetype, QDomNode source,bool editor); + + explicit vXMLTreeElement (QString name, int nodetype); + + ~vXMLTreeElement (); + static const short int TypeNode; + static const short int TypeAttr; + static const short int TypeRoot; + /** + * @brief setNodeValue : set value of node (content or attribute) + * @param value : the value + */ + void setTreeNodeValue(QString value); + /** + * @brief displayText : text to display + * @return text to display + */ + QString displayText(); + /** + * @brief getDocNode + * @return current document node + */ + QDomNode getDocNode(); + QString gettreeNodeName(); + QString gettreeNodeValue(); + bool gettreeNodeValueSet(); + void setTreeNodeName(QString value); + /** + * @brief getelementType + * @return elementType value + */ + short int getelementType(); + +private: + /** + * @brief DocNode : link to current document node + */ + QDomNode DocNode; + + /** + * @brief addedNode : true if node is added by editor (implies empty DocNode) + */ + bool addedNode; + /** + * @brief type : element type (node, attribute, text value of node) + */ + short int elementType; + /** + * @brief nodeValue : Attribute or node value + */ + QString treeNodeValue; + /** + * @brief treeNodeValueSet : true if value has been set + */ + bool treeNodeValueSet; + /** + * @brief nodeValue : Attribute or node value + */ + QString treeNodeName; + Q_DISABLE_COPY(vXMLTreeElement) +}; + +//******************************************************************************************** +/** + * @brief The vXMLTreeView class : container to display/edit xml pattern + */ +class vXMLTreeView : public QStandardItemModel +{ +public: + explicit vXMLTreeView (QObject *parent = 0); + void appendchain(vXMLTreeElement* elmt); + + /** + * @brief The TreeElement struct : chained list of vXMLTreeElement + */ + typedef struct TreeElementchain { + vXMLTreeElement* elmt; + TreeElementchain* next; + } TreeElementchain; + + TreeElementchain * getCurrent(); + void setCurrent(TreeElementchain * value); + TreeElementchain * getLast(); + TreeElementchain * getItems(); + /** + * @brief clearTree : clear tree elements and listed items. + */ + void clearTree(); + ~vXMLTreeView (); +private: + Q_DISABLE_COPY(vXMLTreeView) + + /** + * @brief items : root of chained list of vXMLTreeElement + */ + TreeElementchain * items; + /** + * @brief current : used to parse in iteration. + */ + TreeElementchain * current; + /** + * @brief last : used to parse in iteration. + */ + TreeElementchain * last; + +}; + +//******************************************************************************************** + +namespace Ui { +class DialogPatternXmlEdit; +} +/** + * @brief The DialogPatternXmlEdit class : ui dialog for XML editing of pattern + */ +class DialogPatternXmlEdit : public QDialog +{ + Q_OBJECT + +public: + explicit DialogPatternXmlEdit(QWidget *parent = 0, VPattern *xmldoc = 0); + ~DialogPatternXmlEdit(); + + /** + * @brief clear_edit_data : clear input boxes and disables buttons. + */ + void clear_edit_data(); + + void nameTextEdited(QString newtext); + void valueTextEdited(QString newtext); + void ButtonSetClicked(); + void ButtonCancelClicked(); + void ButtonDeleteAttributeClicked(); + void ButtonAddSonClicked(); + void ButtonAddAttributeClicked(); + void ButtonApplyChangesClicked() ; + + + // Stack of changes definition + typedef struct ChangesStackElement { + short int type; + vXMLTreeElement *element; + QString *newText; + bool changedText; + QString *newValue; + bool changedValue; + ChangesStackElement* next; + } ChangesStackElement; + + // Change stack functions + ChangesStackElement* CreateStackElement(short int typechange); + void removeChangeStackElement(ChangesStackElement* elmt); + void clearStack(); + bool treeChange; + vXMLTreeElement* current_node_edited; + short int current_node_edited_status; + ChangesStackElement* current_node_edited_stack; + +private slots: + void baseSelectionChanged(int value); + void Element_clicked ( const QModelIndex & index ); + +private: + Q_DISABLE_COPY(DialogPatternXmlEdit) + Ui::DialogPatternXmlEdit *ui; + + VPattern *doc; + + QDomElement root; + QStandardItem *rootNode; + //typedef struct rootbases { vXMLTreeElement * root; QString name; } rootbases; + vXMLTreeElement ** rootBases; + qint16 rootBasesNum; + + void readNodes(QDomNode dNode, vXMLTreeElement* root, vXMLTreeView *xmlmodel, bool refresh); + vXMLTreeView* xmlmodel; + + + // Stack of changes + static const short int ChangeTypeDelete; + static const short int ChangeTypeAdd; + static const short int ChangeTypeModify; + + // Stack of changes + + ChangesStackElement* changeStackRoot; + ChangesStackElement* changeStackLast; +}; + + + +#endif // DIALOGPATTERNXMLEDIT_H diff --git a/src/app/dialogs/app/dialogpatternxmledit.ui b/src/app/dialogs/app/dialogpatternxmledit.ui new file mode 100644 index 000000000..8b4a7238e --- /dev/null +++ b/src/app/dialogs/app/dialogpatternxmledit.ui @@ -0,0 +1,351 @@ + + + DialogPatternXmlEdit + + + + 0 + 0 + 664 + 459 + + + + + 1 + 1 + + + + + 400 + 300 + + + + + 0 + 0 + + + + Qt::DefaultContextMenu + + + XML Editor + + + false + + + false + + + + + + QLayout::SetMaximumSize + + + + + QLayout::SetDefaultConstraint + + + 0 + + + + + false + + + QAbstractItemView::SingleSelection + + + + + + + QLayout::SetMinimumSize + + + 9 + + + + + + 0 + 0 + + + + Value : + + + + + + + false + + + + + + + false + + + + + + + + 0 + 0 + + + + Name : + + + + + + + Qt::Vertical + + + QSizePolicy::Preferred + + + + 20 + 10 + + + + + + + + QLayout::SetDefaultConstraint + + + + + + 0 + 0 + + + + <No selection> + + + + + + + + 0 + 0 + + + + Type : + + + + + + + false + + + Add attribute + + + + + + + false + + + Add son + + + + + + + + + QLayout::SetNoConstraint + + + QFormLayout::ExpandingFieldsGrow + + + + + false + + + Remove attibute + + + + + + + false + + + Remove node + + + + + + + false + + + Qt::LeftToRight + + + Set + + + + + + + false + + + Qt::LeftToRight + + + Cancel + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + false + + + Apply changes + + + + + + + false + + + Cancel changes + + + + + + + + + + 0 + 0 + + + + Base selection + + + + + + + + + + + + + + + 0 + 0 + + + + + 0 + 5 + + + + Qt::Horizontal + + + QDialogButtonBox::Ok + + + + + + + + + + + buttonBox + accepted() + DialogPatternXmlEdit + accept() + + + 248 + 254 + + + 157 + 274 + + + + + diff --git a/src/app/dialogs/dialogs.h b/src/app/dialogs/dialogs.h index a64ffcdbb..ec4e9c874 100644 --- a/src/app/dialogs/dialogs.h +++ b/src/app/dialogs/dialogs.h @@ -58,5 +58,6 @@ #include "app/dialogindividualmeasurements.h" #include "app/dialogstandardmeasurements.h" #include "app/dialogaboutapp.h" +#include "app/dialogpatternxmledit.h" #endif // DIALOGS_H diff --git a/src/app/dialogs/dialogs.pri b/src/app/dialogs/dialogs.pri index 62dd44ef5..03bff16a2 100644 --- a/src/app/dialogs/dialogs.pri +++ b/src/app/dialogs/dialogs.pri @@ -29,7 +29,8 @@ HEADERS += \ dialogs/app/dialogmeasurements.h \ dialogs/app/dialogstandardmeasurements.h \ dialogs/app/dialogindividualmeasurements.h \ - dialogs/app/dialogaboutapp.h + dialogs/app/dialogaboutapp.h \ + dialogs/app/dialogpatternxmledit.h SOURCES += \ dialogs/tools/dialogtriangle.cpp \ @@ -61,7 +62,8 @@ SOURCES += \ dialogs/app/dialogmeasurements.cpp \ dialogs/app/dialogstandardmeasurements.cpp \ dialogs/app/dialogindividualmeasurements.cpp \ - dialogs/app/dialogaboutapp.cpp + dialogs/app/dialogaboutapp.cpp \ + dialogs/app/dialogpatternxmledit.cpp FORMS += \ dialogs/tools/dialogtriangle.ui \ @@ -90,4 +92,5 @@ FORMS += \ dialogs/app/dialogmeasurements.ui \ dialogs/app/dialogstandardmeasurements.ui \ dialogs/app/dialogindividualmeasurements.ui \ - dialogs/app/dialogaboutapp.ui + dialogs/app/dialogaboutapp.ui \ + dialogs/app/dialogpatternxmledit.ui diff --git a/src/app/mainwindow.cpp b/src/app/mainwindow.cpp index 47155b812..288cfcd39 100644 --- a/src/app/mainwindow.cpp +++ b/src/app/mainwindow.cpp @@ -543,6 +543,14 @@ void MainWindow::PatternProperties() proper.exec(); } +//--------------------------------------------------------------------------------------------------------------------- +void MainWindow::EditPatternCode() +{ + //TODO : free dialog pointer at end of dialog + DialogPatternXmlEdit *Pattern = new DialogPatternXmlEdit (this,doc); + Pattern->show(); +} + //--------------------------------------------------------------------------------------------------------------------- void MainWindow::showEvent( QShowEvent *event ) { @@ -1359,6 +1367,8 @@ void MainWindow::CreateActions() connect(ui->actionOptions, &QAction::triggered, this, &MainWindow::Options); connect(ui->actionPattern_properties, &QAction::triggered, this, &MainWindow::PatternProperties); ui->actionPattern_properties->setEnabled(false); + connect(ui->actionEdit_pattern_code,&QAction::triggered,this,&MainWindow::EditPatternCode); + //ui->actionEdit_pattern_code->setEnabled(false); //Actions for recent files loaded by a main window application. for (int i = 0; i < MaxRecentFiles; ++i) diff --git a/src/app/mainwindow.h b/src/app/mainwindow.h index 9a8f8407f..ebb067940 100644 --- a/src/app/mainwindow.h +++ b/src/app/mainwindow.h @@ -363,6 +363,11 @@ public slots: void tableClosed(); void OpenRecentFile(); void PatternProperties(); + /** + * @brief Edit XML code of pattern + */ + void EditPatternCode(); + signals: /** * @brief ModelChosen emit after calculation all details. From c1bf6c8a73084c24cce27b1c0839785a089bf235 Mon Sep 17 00:00:00 2001 From: Patrick Proy Date: Fri, 16 May 2014 16:49:58 +0200 Subject: [PATCH 03/15] Code style --HG-- branch : XML Editor --- src/app/dialogs/app/dialogpatternxmledit.cpp | 525 +++++++++++-------- src/app/dialogs/app/dialogpatternxmledit.h | 95 +++- src/app/mainwindow.cpp | 2 +- 3 files changed, 369 insertions(+), 253 deletions(-) diff --git a/src/app/dialogs/app/dialogpatternxmledit.cpp b/src/app/dialogs/app/dialogpatternxmledit.cpp index 85c068c1c..815df0454 100644 --- a/src/app/dialogs/app/dialogpatternxmledit.cpp +++ b/src/app/dialogs/app/dialogpatternxmledit.cpp @@ -35,14 +35,12 @@ const short int DialogPatternXmlEdit::ChangeTypeDelete=1; const short int DialogPatternXmlEdit::ChangeTypeAdd=2; const short int DialogPatternXmlEdit::ChangeTypeModify=3; -DialogPatternXmlEdit::DialogPatternXmlEdit(QWidget *parent,VPattern *xmldoc) : - QDialog(parent), - ui(new Ui::DialogPatternXmlEdit) +DialogPatternXmlEdit::DialogPatternXmlEdit(QWidget *parent,VPattern *xmldoc):QDialog(parent),ui(new Ui::DialogPatternXmlEdit) { ui->setupUi(this); - xmlmodel = new vXMLTreeView(); - current_node_edited=0; + xmlmodel = new VXMLTreeView(); + currentNodeEdited=0; treeChange=false; this->changeStackRoot = this->changeStackLast =0; @@ -58,80 +56,52 @@ DialogPatternXmlEdit::DialogPatternXmlEdit(QWidget *parent,VPattern *xmldoc) : rootNode = (QStandardItem*) xmlmodel->invisibleRootItem(); qint16 drawnum=xmldoc->elementsByTagName("draw").size(); - rootBases = (vXMLTreeElement**)malloc(sizeof(vXMLTreeElement*)*(drawnum+1)); + rootBases = new VXMLTreeElement*[drawnum+1]; rootBasesNum=1; - rootBases[0]=new vXMLTreeElement("Valentina",vXMLTreeElement::TypeRoot,root,false); + rootBases[0]=new VXMLTreeElement("Valentina",VXMLTreeElement::TypeRoot,root,false); // TODO : OOM exception. - vXMLTreeElement* standard_base = new vXMLTreeElement("Valentina",vXMLTreeElement::TypeRoot,root,false); + 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); - readNodes(root,standard_base,xmlmodel,false); + ReadNodes(root,standard_base,xmlmodel,false); ui->treeView_main->setModel(xmlmodel); //ui->treeView_main->expandAll(); // connectors - connect(ui->comboBox_Base_Selection, - static_cast(&QComboBox::currentIndexChanged), - this, - &DialogPatternXmlEdit::baseSelectionChanged); - connect(ui->treeView_main, - &QTreeView::clicked, - this, - &DialogPatternXmlEdit::Element_clicked); - 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); - connect(ui->pushButton_Remove_attribute, - &QPushButton::clicked, - this, - &DialogPatternXmlEdit::ButtonDeleteAttributeClicked); - 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); + connect(ui->comboBox_Base_Selection,static_cast(&QComboBox::currentIndexChanged),this,&DialogPatternXmlEdit::BaseSelectionChanged); + 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); + connect(ui->pushButton_Remove_attribute,&QPushButton::clicked,this,&DialogPatternXmlEdit::ButtonDeleteAttributeClicked); + 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); } -DialogPatternXmlEdit::ChangesStackElement* DialogPatternXmlEdit::CreateStackElement(short int typechange) { +DialogPatternXmlEdit::ChangesStackElement* DialogPatternXmlEdit::CreateStackElement(short int typechange) +{ ChangesStackElement* tmp; - tmp= (ChangesStackElement* ) malloc(sizeof(ChangesStackElement)); + tmp= new ChangesStackElement; //if (tmp==0) TODO tmp->type=typechange; tmp->changedText=tmp->changedValue=false; - if (changeStackLast==0) { + if (changeStackLast==0) + { changeStackLast=changeStackRoot=tmp; - } else { + } + else + { changeStackLast->next=tmp; changeStackLast=changeStackLast->next; } @@ -140,29 +110,42 @@ DialogPatternXmlEdit::ChangesStackElement* DialogPatternXmlEdit::CreateStackElem return changeStackLast; } -void DialogPatternXmlEdit::ButtonApplyChangesClicked() { +void DialogPatternXmlEdit::ButtonApplyChangesClicked() +{ QString Changes=""; ChangesStackElement* currentChange; - if (this->changeStackRoot==0) Changes="No Changes"; - else { + if (this->changeStackRoot==0) + { + Changes="No Changes"; + } + else + { currentChange=this->changeStackRoot; - do { - if (currentChange->type == DialogPatternXmlEdit::ChangeTypeAdd) { + 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("")); Changes += "\n"; - } else { - if (currentChange->type == DialogPatternXmlEdit::ChangeTypeModify) { + } + 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"; - } else { - if (currentChange->type == DialogPatternXmlEdit::ChangeTypeDelete) { + } + else + { + if (currentChange->type == DialogPatternXmlEdit::ChangeTypeDelete) + { Changes += QString("Deleted type %1 : ").arg(currentChange->element->getelementType()); Changes += currentChange->element->gettreeNodeName(); Changes += "/"; @@ -179,97 +162,148 @@ void DialogPatternXmlEdit::ButtonApplyChangesClicked() { // TODO : clear stack and apply } -void DialogPatternXmlEdit::ButtonCancelClicked(){ - this->clear_edit_data(); - if (this->current_node_edited_status=DialogPatternXmlEdit::ChangeTypeDelete) { - if (this->current_node_edited->getelementType() == vXMLTreeElement::TypeAttr) { +void DialogPatternXmlEdit::ButtonCancelClicked() +{ + this->ClearEditData(); + if (this->currentNodeEditedStatus==DialogPatternXmlEdit::ChangeTypeDelete) + { + if (this->currentNodeEdited->getelementType() == VXMLTreeElement::TypeAttr) + { // Attribute previously deleted -> resore - QFont textfont=current_node_edited->font(); + QFont textfont=currentNodeEdited->font(); textfont.setStrikeOut(false); - this->current_node_edited->setFont(textfont); - this->removeChangeStackElement(this->current_node_edited_stack); - } else { + this->currentNodeEdited->setFont(textfont); + this->RemoveChangeStackElement(this->currentNodeEditedStack); + } + else + { QMessageBox::information(this, "Not Implemented", "Not Implemented"); } } } -void DialogPatternXmlEdit::removeChangeStackElement(ChangesStackElement* elmt) { +void DialogPatternXmlEdit::RemoveChangeStackElement(ChangesStackElement* elmt) +{ ChangesStackElement* index = this->changeStackRoot; - if (changeStackRoot == elmt) { - if (elmt->newText != 0) free (elmt->newText); - if (elmt->newValue != 0) free (elmt->newValue); + if (changeStackRoot == elmt) + { + if (elmt->newText != 0) + { + delete elmt->newText; + } + if (elmt->newValue != 0) + { + delete elmt->newValue; + } this->changeStackRoot = elmt->next; - if (this->changeStackLast == elmt) { this->changeStackLast= elmt->next; } - free(elmt); + if (this->changeStackLast == elmt) + { + this->changeStackLast= elmt->next; + } + delete elmt; return; } while (index->next!=0) { - if (index->next == elmt) { + if (index->next == elmt) + { break; } index=index->next; } - if (index->next == 0) { + if (index->next == 0) + { // TODO : debug error here return; } - if (index->next->newText != 0) free (index->next->newText); - if (index->next->newValue != 0) free (index->next->newValue); + if (index->next->newText != 0) + { + delete index->next->newText; + } + if (index->next->newValue != 0) + { + delete index->next->newValue; + } index->next=index->next->next; - if (this->changeStackLast == elmt) { this->changeStackLast= index;} - free(elmt); + if (this->changeStackLast == elmt) + { + this->changeStackLast= index; + } + delete elmt; } -void DialogPatternXmlEdit::ButtonDeleteAttributeClicked(){ +void DialogPatternXmlEdit::ButtonDeleteAttributeClicked() +{ ChangesStackElement* newstack; // TODO : maybe assert functions here - if (this->current_node_edited == 0) return; - if (this->current_node_edited->getelementType() != vXMLTreeElement::TypeAttr) return; + if (this->currentNodeEdited == 0) + { + return; + } + if (this->currentNodeEdited->getelementType() != VXMLTreeElement::TypeAttr) + { + return; + } newstack = this->CreateStackElement(DialogPatternXmlEdit::ChangeTypeDelete); - newstack->element= this->current_node_edited; + newstack->element= this->currentNodeEdited; - QFont textfont=current_node_edited->font(); + QFont textfont=currentNodeEdited->font(); textfont.setStrikeOut(true); - this->current_node_edited->setFont(textfont); + this->currentNodeEdited->setFont(textfont); // Activate changes this->treeChange=true; ui->pushButton_Apply_Changes->setEnabled(true); // clear dialog - this->clear_edit_data(); + this->ClearEditData(); } -void DialogPatternXmlEdit::ButtonAddSonClicked(){ +void DialogPatternXmlEdit::ButtonAddSonClicked() +{ ChangesStackElement* newstack; + bool ok; //clear_edit_data(); // TODO : maybe assert functions here - if (this->current_node_edited == 0) return; - if (this->current_node_edited->getelementType() != vXMLTreeElement::TypeNode) return; + if (this->currentNodeEdited == 0) + { + return; + } + if (this->currentNodeEdited->getelementType() != VXMLTreeElement::TypeNode) + { + return; + } + - bool ok; QString name = QInputDialog::getText(this, tr("Node Name"), tr("Name:"), QLineEdit::Normal, "", &ok); - if (!ok) return; + if (ok==false) + { + return; + } QString value = QInputDialog::getText(this, tr("Node Value (may be empty)"), tr("Value:"), QLineEdit::Normal, "", &ok); - if (!ok) return; + if (ok==false) + { + return; + } // create element tree but do not add attribute in DOM (will be done in apply). QDomNode empty; - vXMLTreeElement* tElement = new vXMLTreeElement( + VXMLTreeElement* tElement = new VXMLTreeElement( name, - vXMLTreeElement::TypeNode, + VXMLTreeElement::TypeNode, empty,true); - if (value !="") tElement->setTreeNodeValue(value); - current_node_edited->appendRow(tElement); + if (!value.isEmpty()) + { + tElement->setTreeNodeValue(value); + } + currentNodeEdited->appendRow(tElement); xmlmodel->appendchain(tElement); - QFont textfont=current_node_edited->font(); + QFont textfont=currentNodeEdited->font(); textfont.setBold(true); tElement->setFont(textfont); @@ -277,46 +311,59 @@ void DialogPatternXmlEdit::ButtonAddSonClicked(){ newstack = this->CreateStackElement(DialogPatternXmlEdit::ChangeTypeAdd); newstack->element=tElement; newstack->newText=new QString(name); newstack->changedText=true; - if (value !="") { newstack->changedValue=true;newstack->newValue=new QString(value);} + if (value.isEmpty()) + { + newstack->changedValue=true;newstack->newValue=new QString(value); + } // Activate changes treeChange=true; ui->pushButton_Apply_Changes->setEnabled(true); } -void DialogPatternXmlEdit::ButtonAddAttributeClicked(){ +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 - if (this->current_node_edited == 0) return; - if (this->current_node_edited->getelementType() != vXMLTreeElement::TypeNode) return; + if (this->currentNodeEdited == 0) + { + return; + } + if (this->currentNodeEdited->getelementType() != VXMLTreeElement::TypeNode) return; QString name = QInputDialog::getText(this, tr("Attribute Name"), tr("Name:"), QLineEdit::Normal, "", &ok); - if (!ok) return; + if (ok==false) + { + return; + } QString value = QInputDialog::getText(this, tr("Attribute Value"), tr("Value:"), QLineEdit::Normal, "", &ok); - if (!ok) return; + if (ok==false) + { + return; + } // create element tree but do not add attribute in DOM (will be done in apply). - vXMLTreeElement* tElement = new vXMLTreeElement( + VXMLTreeElement* tElement = new VXMLTreeElement( name, - vXMLTreeElement::TypeAttr, + VXMLTreeElement::TypeAttr, empty,true); tElement->setTreeNodeValue(value); - current_node_edited->appendRow(tElement); + currentNodeEdited->appendRow(tElement); xmlmodel->appendchain(tElement); - QFont textfont=current_node_edited->font(); + QFont textfont=currentNodeEdited->font(); textfont.setBold(true); tElement->setFont(textfont); // Add to change stack newstack = this->CreateStackElement(DialogPatternXmlEdit::ChangeTypeAdd); newstack->element= tElement; - newstack->type=vXMLTreeElement::TypeAttr; + newstack->type=VXMLTreeElement::TypeAttr; newstack->newText=new QString(name); newstack->newValue=new QString(value); // Activate changes @@ -324,56 +371,63 @@ void DialogPatternXmlEdit::ButtonAddAttributeClicked(){ ui->pushButton_Apply_Changes->setEnabled(true); } -void DialogPatternXmlEdit::ButtonSetClicked(){ +void DialogPatternXmlEdit::ButtonSetClicked() +{ QString name,value; ChangesStackElement* newstack; // TODO : check if a change has already been done - if (this->current_node_edited == 0) return; + if (this->currentNodeEdited == 0) return; name=ui->lineEdit_Name->text(); value=ui->lineEdit_Value->text(); bool nameedit,valueedit; - nameedit = (name != current_node_edited->gettreeNodeName()) ? true : false; - valueedit= (value != current_node_edited->gettreeNodeValue()) ? true : false; + nameedit = (name != currentNodeEdited->gettreeNodeName()) ? true : false; + valueedit= (value != currentNodeEdited->gettreeNodeValue()) ? true : false; - if ( nameedit == true || valueedit == true ) { + if ( nameedit == true || valueedit == true ) + { newstack = this->CreateStackElement(DialogPatternXmlEdit::ChangeTypeModify); - newstack->element= this->current_node_edited; + newstack->element= this->currentNodeEdited; - if (nameedit == true) { + if (nameedit == true) + { newstack->changedText=true; newstack->newText=new QString(name); - current_node_edited->setTreeNodeName(name); + currentNodeEdited->setTreeNodeName(name); } - if (valueedit == true) { + if (valueedit == true) + { newstack->changedValue=true; newstack->newValue= new QString(value); - current_node_edited->setTreeNodeValue(value); + currentNodeEdited->setTreeNodeValue(value); } - QFont textfont=current_node_edited->font(); + QFont textfont=currentNodeEdited->font(); textfont.setBold(true); - current_node_edited->setFont(textfont); + currentNodeEdited->setFont(textfont); treeChange=true; ui->pushButton_Apply_Changes->setEnabled(true); - clear_edit_data(); + ClearEditData(); } } -void DialogPatternXmlEdit::nameTextEdited(QString newtext) { +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); } -void DialogPatternXmlEdit::valueTextEdited(QString newtext) { +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); } -void DialogPatternXmlEdit::clear_edit_data() { +void DialogPatternXmlEdit::ClearEditData() +{ ui->lineEdit_Name->setText(""); ui->lineEdit_Value->setText(""); ui->lineEdit_Name->setEnabled(false); @@ -387,27 +441,36 @@ void DialogPatternXmlEdit::clear_edit_data() { ui->pushButton_Cancel_Values->setEnabled(false); } -void DialogPatternXmlEdit::clearStack() { +void DialogPatternXmlEdit::ClearStack() +{ ChangesStackElement * tmp; - while (changeStackRoot != 0) { - if (changeStackRoot->newText != 0) free (changeStackRoot->newText); - if (changeStackRoot->newValue != 0) free (changeStackRoot->newValue); + while (changeStackRoot != 0) + { + if (changeStackRoot->newText != 0) + { + delete changeStackRoot->newText; + } + if (changeStackRoot->newValue != 0) + { + delete changeStackRoot->newValue; + } tmp=changeStackRoot; changeStackRoot=changeStackRoot->next; - free(tmp); + delete tmp; } changeStackLast=0; } -void DialogPatternXmlEdit::baseSelectionChanged(int value) +void DialogPatternXmlEdit::BaseSelectionChanged(int value) { QDomNode newbase; int index; - if (this->treeChange == true) {//Changes have been made : warn ? + if (this->treeChange == true) + {//Changes have been made : warn ? } - clearStack(); + ClearStack(); ui->pushButton_Apply_Changes->setEnabled(false); index = ui->comboBox_Base_Selection->itemData(value).toInt(); //.convert(QVariant::Int); @@ -416,45 +479,50 @@ void DialogPatternXmlEdit::baseSelectionChanged(int value) // Clear all tree info and nodes // TODO : Check - while (rootNode->rowCount() > 0) { + while (rootNode->rowCount() > 0) + { rootNode->removeRow(0); } - xmlmodel->clearTree(); + xmlmodel->ClearTree(); - vXMLTreeElement* standard_base = new vXMLTreeElement( + VXMLTreeElement* standard_base = new VXMLTreeElement( rootBases[index]->gettreeNodeName(), - vXMLTreeElement::TypeRoot, + VXMLTreeElement::TypeRoot, rootBases[index]->getDocNode(), false); rootNode->appendRow(standard_base); - readNodes(rootBases[index]->getDocNode(),standard_base,xmlmodel,true); + ReadNodes(rootBases[index]->getDocNode(),standard_base,xmlmodel,true); ui->treeView_main->setModel(xmlmodel); - this->clear_edit_data(); + this->ClearEditData(); // TODO : warn if changes ? - this->clearStack(); + this->ClearStack(); return; } -void DialogPatternXmlEdit::Element_clicked ( const QModelIndex & index ) { +void DialogPatternXmlEdit::ElementClicked ( const QModelIndex & index ) +{ // Get item (function returns parent of clicked item) - vXMLTreeElement *item = (vXMLTreeElement *)index.internalPointer(); + VXMLTreeElement *item = (VXMLTreeElement *)index.internalPointer(); // Get child specified by index row/column - vXMLTreeElement * item2 = (vXMLTreeElement *) item->child(index.row(),index.column()); + VXMLTreeElement * item2 = (VXMLTreeElement *) item->child(index.row(),index.column()); // Clear all data and disable buttons - this->clear_edit_data(); + this->ClearEditData(); // Check status of element in change stack - this->current_node_edited=item2; - this->current_node_edited_status=0; + this->currentNodeEdited=item2; + this->currentNodeEditedStatus=0; DialogPatternXmlEdit::ChangesStackElement * local = this->changeStackRoot; - while (local != 0) { - if (local->element == item2) { - this->current_node_edited_status=local->type; - this->current_node_edited_stack=local; - if (local->type == DialogPatternXmlEdit::ChangeTypeDelete) { + while (local != 0) + { + 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; @@ -463,21 +531,27 @@ void DialogPatternXmlEdit::Element_clicked ( const QModelIndex & index ) { local=local->next; } // Fill the dialog and Activate the line edit - if (item2->getelementType() != vXMLTreeElement::TypeRoot) { - ui->lineEdit_Name->setText(current_node_edited->gettreeNodeName()); + if (item2->getelementType() != VXMLTreeElement::TypeRoot) + { + ui->lineEdit_Name->setText(currentNodeEdited->gettreeNodeName()); ui->lineEdit_Name->setEnabled(true); // Fill value - ui->lineEdit_Value->setText(current_node_edited->gettreeNodeValue()); + ui->lineEdit_Value->setText(currentNodeEdited->gettreeNodeValue()); ui->lineEdit_Value->setEnabled(true); } - if (item2->getelementType() == vXMLTreeElement::TypeRoot) { + if (item2->getelementType() == VXMLTreeElement::TypeRoot) + { ui->label_type_value->setText(tr("Root node"));; ui->pushButton_Add_son->setEnabled(true); - } else if (item2->getelementType() == vXMLTreeElement::TypeNode) { + } + 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); - } else if (item2->getelementType() == vXMLTreeElement::TypeAttr) { + } + else if (item2->getelementType() == VXMLTreeElement::TypeAttr) + { ui->label_type_value->setText(tr("Attribute")); ui->pushButton_Remove_attribute->setEnabled(true); } @@ -487,39 +561,45 @@ DialogPatternXmlEdit::~DialogPatternXmlEdit() { //xmlmodel->clearTree(); delete ui; - ChangesStackElement * tmp; while (changeStackRoot != 0) { + ChangesStackElement * tmp; + while (changeStackRoot != 0) + { tmp=changeStackRoot->next; - free(changeStackRoot); + delete changeStackRoot; changeStackRoot=tmp; } - //delete rootBases[0]; - //free(rootBases); - //delete xmlmodel; } -void DialogPatternXmlEdit::readNodes(QDomNode dNode, vXMLTreeElement* root,vXMLTreeView* xmlmodel, bool refresh) +void DialogPatternXmlEdit::ReadNodes(QDomNode dNode, VXMLTreeElement* root,VXMLTreeView* xmlmodel, bool refresh) { QDomNode tNode,tNode2; - vXMLTreeElement* tElement,*tElement2; + VXMLTreeElement* tElement,*tElement2; int totalOfChilds = dNode.childNodes().size(); - if (totalOfChilds == 0) return; // empty xml - for (int i=0;iappendchain(tElement); root->appendRow(tElement); - if (tNode.nodeName() == "draw") { + if (tNode.nodeName() == "draw") + { for (int i = 0; i < tNode.attributes().size(); i++) { if ((tNode.attributes().item(i).nodeName() == "name") - && (refresh == false)){ + && (refresh == false)) + { ui->comboBox_Base_Selection->addItem(tNode.attributes().item(i).nodeValue(), QVariant(rootBasesNum)); - rootBases[rootBasesNum]=new vXMLTreeElement( + rootBases[rootBasesNum]=new VXMLTreeElement( QString("Drawing %1").arg(tNode.attributes().item(i).nodeValue()), - vXMLTreeElement::TypeRoot,tNode,false);; + VXMLTreeElement::TypeRoot,tNode,false);; rootBasesNum++; //QStandardItem* rootNode = (QStandardItem*) rootBases[rootBasesNum]->invisibleRootItem(); //rootNode->appendRow(tElement); @@ -533,70 +613,77 @@ void DialogPatternXmlEdit::readNodes(QDomNode dNode, vXMLTreeElement* root,vXMLT { tElement=xmlmodel->getCurrent()->elmt; tNode=tElement->getDocNode(); - if (tElement->getelementType() == vXMLTreeElement::TypeNode) { + if (tElement->getelementType() == VXMLTreeElement::TypeNode) + { totalOfChilds = tNode.childNodes().size(); - for (int i=0;iappendchain(tElement2); tElement->appendRow(tElement2); } } - if (tNode.nodeType() == QDomNode::TextNode) { + if (tNode.nodeType() == QDomNode::TextNode) + { tElement->setTreeNodeValue(tNode.nodeValue()); } for (int i = 0; i < tNode.attributes().size(); i++) { - tElement2 = new vXMLTreeElement( + tElement2 = new VXMLTreeElement( tNode.attributes().item(i).nodeName(), - vXMLTreeElement::TypeAttr, + VXMLTreeElement::TypeAttr, tNode,false); tElement2->setTreeNodeValue(tNode.attributes().item(i).nodeValue()); tElement->appendRow(tElement2); xmlmodel->appendchain(tElement2); } } - xmlmodel->setCurrent(xmlmodel->getCurrent()->next); + xmlmodel->SetCurrent(xmlmodel->getCurrent()->next); } while (xmlmodel->getCurrent() != 0); return; } // vXMLTreeView Code ----------------------------------------------------------------- -vXMLTreeView::vXMLTreeView(QObject *parent) : QStandardItemModel(parent) +VXMLTreeView::VXMLTreeView(QObject *parent) : QStandardItemModel(parent) { current=last=items=0; } -void vXMLTreeView::clearTree() { +void VXMLTreeView::ClearTree() { // TODO check if treeitems are deleted with the delete row command of QDom //clear the chain link TreeElementchain * tmp; - while (items != 0) { + while (items != 0) + { tmp=items; items=items->next; - free (tmp); + delete tmp; } // reset chain counters current=last=items=0; } -vXMLTreeView::~vXMLTreeView() +VXMLTreeView::~VXMLTreeView() { } -void vXMLTreeView::appendchain(vXMLTreeElement* elmt) +void VXMLTreeView::appendchain(VXMLTreeElement* elmt) { - if (last == 0) { // first element - current=(TreeElementchain*)malloc(sizeof(TreeElementchain)); - if (current == 0) { + if (last == 0) + { // first element + current = new TreeElementchain; + if (current == 0) + { Q_ASSERT(current != 0); // TODO : throw exception } @@ -605,8 +692,9 @@ void vXMLTreeView::appendchain(vXMLTreeElement* elmt) last=items=current; return; } - TreeElementchain* temp=(TreeElementchain*)malloc(sizeof(TreeElementchain)); - if (temp == 0) { + TreeElementchain* temp= new TreeElementchain; + if (temp == 0) + { Q_ASSERT(temp != 0); // TODO : throw exception } @@ -616,32 +704,30 @@ void vXMLTreeView::appendchain(vXMLTreeElement* elmt) last=temp; } -vXMLTreeView::TreeElementchain * vXMLTreeView::getCurrent() { return this->current;} -void vXMLTreeView::setCurrent(vXMLTreeView::TreeElementchain * value) { this->current=value;} -vXMLTreeView::TreeElementchain * vXMLTreeView::getLast() { return this->last;} -vXMLTreeView::TreeElementchain * vXMLTreeView::getItems() { return this->items;} - // vXMLTreeElement code ----------------------------------------------------------------- -const short int vXMLTreeElement::TypeNode=1; -const short int vXMLTreeElement::TypeAttr=2; -const short int vXMLTreeElement::TypeRoot=3; +const short int VXMLTreeElement::TypeNode=1; +const short int VXMLTreeElement::TypeAttr=2; +const short int VXMLTreeElement::TypeRoot=3; -vXMLTreeElement::vXMLTreeElement(QString name, int nodetype, QDomNode source,bool editor) - : QStandardItem(name) +VXMLTreeElement::VXMLTreeElement(QString name, int nodetype, QDomNode source,bool editor): QStandardItem(name) { this->elementType=nodetype; this->DocNode=source; this->addedNode=editor; - if (!editor) this->DocNode=source; + if (!editor) + { + this->DocNode=source; + } this->treeNodeName=name; this->treeNodeValue=""; // TODO : translation ? this->treeNodeValueSet=false, this->setText(this->displayText()); - switch (this->elementType) { - case vXMLTreeElement::TypeAttr: + switch (this->elementType) + { + case VXMLTreeElement::TypeAttr: this->setBackground(BACKGROUND_COLOR_ATTRIBUTE); break; default: @@ -650,43 +736,36 @@ vXMLTreeElement::vXMLTreeElement(QString name, int nodetype, QDomNode source,boo } -vXMLTreeElement::~vXMLTreeElement() +VXMLTreeElement::~VXMLTreeElement() { } -void vXMLTreeElement::setTreeNodeValue(QString value) + +void VXMLTreeElement::setTreeNodeValue(QString value) { this->treeNodeValue=value; this->treeNodeValueSet=true; this->setText(this->displayText()); } -void vXMLTreeElement::setTreeNodeName(QString value) +void VXMLTreeElement::setTreeNodeName(QString value) { this->treeNodeName=value; this->setText(this->displayText()); } -QString vXMLTreeElement::displayText() +QString VXMLTreeElement::displayText() { - if (this->elementType == vXMLTreeElement::TypeRoot) + if (this->elementType == VXMLTreeElement::TypeRoot) return this->treeNodeName; return QString("%1 : %2").arg((this->treeNodeName)).arg(this->treeNodeValue); } -short int vXMLTreeElement::getelementType() { return this->elementType;} - -QDomNode vXMLTreeElement::getDocNode() -{ - return this->DocNode; -} - -QString vXMLTreeElement::gettreeNodeName() { return this->treeNodeName;} -QString vXMLTreeElement::gettreeNodeValue() { +QString VXMLTreeElement::gettreeNodeValue() { // Only return value if it's really set. if (this->treeNodeValueSet) return this->treeNodeValue; else return ""; } -bool vXMLTreeElement::gettreeNodeValueSet() {return this->treeNodeValueSet;} + diff --git a/src/app/dialogs/app/dialogpatternxmledit.h b/src/app/dialogs/app/dialogpatternxmledit.h index 6332c05d3..96eb01d8d 100644 --- a/src/app/dialogs/app/dialogpatternxmledit.h +++ b/src/app/dialogs/app/dialogpatternxmledit.h @@ -41,7 +41,7 @@ */ #define BACKGROUND_COLOR_ATTRIBUTE QBrush(Qt::GlobalColor::cyan) -class vXMLTreeElement : public QStandardItem +class VXMLTreeElement : public QStandardItem { public: @@ -51,11 +51,11 @@ public: * @param nodetype : node type (node, attribute, root) * @param source : the source dom node */ - explicit vXMLTreeElement (QString name, int nodetype, QDomNode source,bool editor); + explicit VXMLTreeElement (QString name, int nodetype, QDomNode source,bool editor); - explicit vXMLTreeElement (QString name, int nodetype); + explicit VXMLTreeElement (QString name, int nodetype); - ~vXMLTreeElement (); + ~VXMLTreeElement (); static const short int TypeNode; static const short int TypeAttr; static const short int TypeRoot; @@ -110,38 +110,58 @@ private: * @brief nodeValue : Attribute or node value */ QString treeNodeName; - Q_DISABLE_COPY(vXMLTreeElement) + Q_DISABLE_COPY(VXMLTreeElement) }; +inline short int VXMLTreeElement::getelementType() +{ + return this->elementType; +} + +inline QDomNode VXMLTreeElement::getDocNode() +{ + return this->DocNode; +} + +inline QString VXMLTreeElement::gettreeNodeName() +{ + return this->treeNodeName; +} + +inline bool VXMLTreeElement::gettreeNodeValueSet() +{ + return this->treeNodeValueSet; +} + //******************************************************************************************** /** * @brief The vXMLTreeView class : container to display/edit xml pattern */ -class vXMLTreeView : public QStandardItemModel +class VXMLTreeView : public QStandardItemModel { public: - explicit vXMLTreeView (QObject *parent = 0); - void appendchain(vXMLTreeElement* elmt); + explicit VXMLTreeView (QObject *parent = 0); + void appendchain(VXMLTreeElement* elmt); /** * @brief The TreeElement struct : chained list of vXMLTreeElement */ typedef struct TreeElementchain { - vXMLTreeElement* elmt; + VXMLTreeElement* elmt; TreeElementchain* next; } TreeElementchain; TreeElementchain * getCurrent(); - void setCurrent(TreeElementchain * value); - TreeElementchain * getLast(); - TreeElementchain * getItems(); + void SetCurrent(TreeElementchain * value); + TreeElementchain * GetLast(); + TreeElementchain * GetItems(); /** * @brief clearTree : clear tree elements and listed items. */ - void clearTree(); - ~vXMLTreeView (); + void ClearTree(); + ~VXMLTreeView (); private: - Q_DISABLE_COPY(vXMLTreeView) + Q_DISABLE_COPY(VXMLTreeView) /** * @brief items : root of chained list of vXMLTreeElement @@ -158,6 +178,23 @@ private: }; +inline VXMLTreeView::TreeElementchain * VXMLTreeView::getCurrent() +{ + return this->current; +} +inline void VXMLTreeView::SetCurrent(VXMLTreeView::TreeElementchain * value) +{ + this->current=value; +} +inline VXMLTreeView::TreeElementchain * VXMLTreeView::GetLast() +{ + return this->last; +} +inline VXMLTreeView::TreeElementchain * VXMLTreeView::GetItems() +{ + return this->items; +} + //******************************************************************************************** namespace Ui { @@ -177,10 +214,10 @@ public: /** * @brief clear_edit_data : clear input boxes and disables buttons. */ - void clear_edit_data(); + void ClearEditData(); - void nameTextEdited(QString newtext); - void valueTextEdited(QString newtext); + void NameTextEdited(QString newtext); + void ValueTextEdited(QString newtext); void ButtonSetClicked(); void ButtonCancelClicked(); void ButtonDeleteAttributeClicked(); @@ -192,7 +229,7 @@ public: // Stack of changes definition typedef struct ChangesStackElement { short int type; - vXMLTreeElement *element; + VXMLTreeElement *element; QString *newText; bool changedText; QString *newValue; @@ -202,16 +239,16 @@ public: // Change stack functions ChangesStackElement* CreateStackElement(short int typechange); - void removeChangeStackElement(ChangesStackElement* elmt); - void clearStack(); + void RemoveChangeStackElement(ChangesStackElement* elmt); + void ClearStack(); bool treeChange; - vXMLTreeElement* current_node_edited; - short int current_node_edited_status; - ChangesStackElement* current_node_edited_stack; + VXMLTreeElement* currentNodeEdited; + short int currentNodeEditedStatus; + ChangesStackElement* currentNodeEditedStack; private slots: - void baseSelectionChanged(int value); - void Element_clicked ( const QModelIndex & index ); + void BaseSelectionChanged(int value); + void ElementClicked ( const QModelIndex & index ); private: Q_DISABLE_COPY(DialogPatternXmlEdit) @@ -222,11 +259,11 @@ private: QDomElement root; QStandardItem *rootNode; //typedef struct rootbases { vXMLTreeElement * root; QString name; } rootbases; - vXMLTreeElement ** rootBases; + VXMLTreeElement ** rootBases; qint16 rootBasesNum; - void readNodes(QDomNode dNode, vXMLTreeElement* root, vXMLTreeView *xmlmodel, bool refresh); - vXMLTreeView* xmlmodel; + void ReadNodes(QDomNode dNode, VXMLTreeElement* root, VXMLTreeView *xmlmodel, bool refresh); + VXMLTreeView* xmlmodel; // Stack of changes diff --git a/src/app/mainwindow.cpp b/src/app/mainwindow.cpp index 288cfcd39..ef7d7b51d 100644 --- a/src/app/mainwindow.cpp +++ b/src/app/mainwindow.cpp @@ -546,8 +546,8 @@ void MainWindow::PatternProperties() //--------------------------------------------------------------------------------------------------------------------- void MainWindow::EditPatternCode() { - //TODO : free dialog pointer at end of dialog DialogPatternXmlEdit *Pattern = new DialogPatternXmlEdit (this,doc); + Pattern->setAttribute(Qt::WA_DeleteOnClose,true); Pattern->show(); } From cc572df448d7f8fd57647bb7e2c57590c1be1f73 Mon Sep 17 00:00:00 2001 From: Patrick Proy Date: Fri, 16 May 2014 17:13:45 +0200 Subject: [PATCH 04/15] Code style --HG-- branch : XML Editor --- src/app/dialogs/app/dialogpatternxmledit.cpp | 80 +++++++++++--------- 1 file changed, 43 insertions(+), 37 deletions(-) diff --git a/src/app/dialogs/app/dialogpatternxmledit.cpp b/src/app/dialogs/app/dialogpatternxmledit.cpp index 815df0454..df2244b63 100644 --- a/src/app/dialogs/app/dialogpatternxmledit.cpp +++ b/src/app/dialogs/app/dialogpatternxmledit.cpp @@ -35,14 +35,15 @@ const short int DialogPatternXmlEdit::ChangeTypeDelete=1; const short int DialogPatternXmlEdit::ChangeTypeAdd=2; const short int DialogPatternXmlEdit::ChangeTypeModify=3; -DialogPatternXmlEdit::DialogPatternXmlEdit(QWidget *parent,VPattern *xmldoc):QDialog(parent),ui(new Ui::DialogPatternXmlEdit) +DialogPatternXmlEdit::DialogPatternXmlEdit(QWidget *parent,VPattern *xmldoc):QDialog(parent), + ui(new Ui::DialogPatternXmlEdit) { ui->setupUi(this); xmlmodel = new VXMLTreeView(); - currentNodeEdited=0; + currentNodeEdited=nullptr; treeChange=false; - this->changeStackRoot = this->changeStackLast =0; + this->changeStackRoot = this->changeStackLast =nullptr ; doc=xmldoc; root = doc->documentElement(); @@ -75,13 +76,15 @@ DialogPatternXmlEdit::DialogPatternXmlEdit(QWidget *parent,VPattern *xmldoc):QDi //ui->treeView_main->expandAll(); // connectors - connect(ui->comboBox_Base_Selection,static_cast(&QComboBox::currentIndexChanged),this,&DialogPatternXmlEdit::BaseSelectionChanged); + connect(ui->comboBox_Base_Selection,static_cast(&QComboBox::currentIndexChanged), + this,&DialogPatternXmlEdit::BaseSelectionChanged); 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); - connect(ui->pushButton_Remove_attribute,&QPushButton::clicked,this,&DialogPatternXmlEdit::ButtonDeleteAttributeClicked); + connect(ui->pushButton_Remove_attribute,&QPushButton::clicked, + this,&DialogPatternXmlEdit::ButtonDeleteAttributeClicked); 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); @@ -96,7 +99,7 @@ DialogPatternXmlEdit::ChangesStackElement* DialogPatternXmlEdit::CreateStackElem tmp->type=typechange; tmp->changedText=tmp->changedValue=false; - if (changeStackLast==0) + if (changeStackLast==nullptr ) { changeStackLast=changeStackRoot=tmp; } @@ -105,8 +108,8 @@ DialogPatternXmlEdit::ChangesStackElement* DialogPatternXmlEdit::CreateStackElem changeStackLast->next=tmp; changeStackLast=changeStackLast->next; } - changeStackLast->newValue=changeStackLast->newText=0; - changeStackLast->next=0; + changeStackLast->newValue=changeStackLast->newText=nullptr; + changeStackLast->next=nullptr; return changeStackLast; } @@ -114,7 +117,7 @@ void DialogPatternXmlEdit::ButtonApplyChangesClicked() { QString Changes=""; ChangesStackElement* currentChange; - if (this->changeStackRoot==0) + if (this->changeStackRoot==nullptr) { Changes="No Changes"; } @@ -156,7 +159,7 @@ void DialogPatternXmlEdit::ButtonApplyChangesClicked() } } currentChange=currentChange->next; - } while (currentChange != 0); + } while (currentChange != nullptr); } QMessageBox::information(this, "Changes (not REALLY applied for now)", Changes); // TODO : clear stack and apply @@ -188,11 +191,11 @@ void DialogPatternXmlEdit::RemoveChangeStackElement(ChangesStackElement* elmt) if (changeStackRoot == elmt) { - if (elmt->newText != 0) + if (elmt->newText != nullptr) { delete elmt->newText; } - if (elmt->newValue != 0) + if (elmt->newValue != nullptr) { delete elmt->newValue; } @@ -204,23 +207,23 @@ void DialogPatternXmlEdit::RemoveChangeStackElement(ChangesStackElement* elmt) delete elmt; return; } - while (index->next!=0) { + while (index->next!=nullptr) { if (index->next == elmt) { break; } index=index->next; } - if (index->next == 0) + if (index->next == nullptr) { // TODO : debug error here return; } - if (index->next->newText != 0) + if (index->next->newText != nullptr) { delete index->next->newText; } - if (index->next->newValue != 0) + if (index->next->newValue != nullptr) { delete index->next->newValue; } @@ -237,7 +240,7 @@ void DialogPatternXmlEdit::ButtonDeleteAttributeClicked() ChangesStackElement* newstack; // TODO : maybe assert functions here - if (this->currentNodeEdited == 0) + if (this->currentNodeEdited == nullptr) { return; } @@ -265,7 +268,7 @@ void DialogPatternXmlEdit::ButtonAddSonClicked() bool ok; //clear_edit_data(); // TODO : maybe assert functions here - if (this->currentNodeEdited == 0) + if (this->currentNodeEdited == nullptr) { return; } @@ -327,11 +330,14 @@ void DialogPatternXmlEdit::ButtonAddAttributeClicked() QDomNode empty; // empty QNode to pas to create function : maybe a better way to do this ? // TODO : maybe assert functions here - if (this->currentNodeEdited == 0) + if (this->currentNodeEdited == nullptr) + { + return; + } + if (this->currentNodeEdited->getelementType() != VXMLTreeElement::TypeNode) { return; } - if (this->currentNodeEdited->getelementType() != VXMLTreeElement::TypeNode) return; QString name = QInputDialog::getText(this, tr("Attribute Name"), tr("Name:"), QLineEdit::Normal, @@ -377,7 +383,7 @@ void DialogPatternXmlEdit::ButtonSetClicked() ChangesStackElement* newstack; // TODO : check if a change has already been done - if (this->currentNodeEdited == 0) return; + if (this->currentNodeEdited == nullptr) return; name=ui->lineEdit_Name->text(); value=ui->lineEdit_Value->text(); bool nameedit,valueedit; @@ -444,13 +450,13 @@ void DialogPatternXmlEdit::ClearEditData() void DialogPatternXmlEdit::ClearStack() { ChangesStackElement * tmp; - while (changeStackRoot != 0) + while (changeStackRoot != nullptr) { - if (changeStackRoot->newText != 0) + if (changeStackRoot->newText != nullptr) { delete changeStackRoot->newText; } - if (changeStackRoot->newValue != 0) + if (changeStackRoot->newValue != nullptr) { delete changeStackRoot->newValue; } @@ -458,7 +464,7 @@ void DialogPatternXmlEdit::ClearStack() changeStackRoot=changeStackRoot->next; delete tmp; } - changeStackLast=0; + changeStackLast=nullptr; } void DialogPatternXmlEdit::BaseSelectionChanged(int value) @@ -515,7 +521,7 @@ void DialogPatternXmlEdit::ElementClicked ( const QModelIndex & index ) this->currentNodeEdited=item2; this->currentNodeEditedStatus=0; DialogPatternXmlEdit::ChangesStackElement * local = this->changeStackRoot; - while (local != 0) + while (local != nullptr) { if (local->element == item2) { @@ -562,7 +568,7 @@ DialogPatternXmlEdit::~DialogPatternXmlEdit() //xmlmodel->clearTree(); delete ui; ChangesStackElement * tmp; - while (changeStackRoot != 0) + while (changeStackRoot != nullptr) { tmp=changeStackRoot->next; delete changeStackRoot; @@ -652,7 +658,7 @@ void DialogPatternXmlEdit::ReadNodes(QDomNode dNode, VXMLTreeElement* root,VXMLT // vXMLTreeView Code ----------------------------------------------------------------- VXMLTreeView::VXMLTreeView(QObject *parent) : QStandardItemModel(parent) { - current=last=items=0; + current=last=items=nullptr; } void VXMLTreeView::ClearTree() { @@ -661,14 +667,14 @@ void VXMLTreeView::ClearTree() { //clear the chain link TreeElementchain * tmp; - while (items != 0) + while (items != nullptr) { tmp=items; items=items->next; delete tmp; } // reset chain counters - current=last=items=0; + current=last=items=nullptr; } @@ -679,27 +685,27 @@ VXMLTreeView::~VXMLTreeView() void VXMLTreeView::appendchain(VXMLTreeElement* elmt) { - if (last == 0) + if (last == nullptr) { // first element current = new TreeElementchain; - if (current == 0) + if (current == nullptr) { - Q_ASSERT(current != 0); + Q_ASSERT(current != nullptr); // TODO : throw exception } current->elmt=elmt; - current->next=0; + current->next=nullptr; last=items=current; return; } TreeElementchain* temp= new TreeElementchain; - if (temp == 0) + if (temp == nullptr) { - Q_ASSERT(temp != 0); + Q_ASSERT(temp != nullptr); // TODO : throw exception } temp->elmt=elmt; - temp->next=0; + temp->next=nullptr; last->next=temp; last=temp; } From 1489dc6a1cb3fc90d40fc30a43967b4ed1577f3f Mon Sep 17 00:00:00 2001 From: dismine Date: Fri, 16 May 2014 19:39:46 +0300 Subject: [PATCH 05/15] Unix Line Endings. Need for Vera++. --HG-- branch : XML Editor --- src/app/dialogs/app/dialogaboutapp.cpp | 130 +- src/app/dialogs/app/dialogaboutapp.h | 114 +- src/app/dialogs/app/dialogpatternxmledit.cpp | 1554 +++++++++--------- src/app/dialogs/app/dialogpatternxmledit.h | 564 +++---- 4 files changed, 1181 insertions(+), 1181 deletions(-) diff --git a/src/app/dialogs/app/dialogaboutapp.cpp b/src/app/dialogs/app/dialogaboutapp.cpp index ffad2670e..310d6f65b 100644 --- a/src/app/dialogs/app/dialogaboutapp.cpp +++ b/src/app/dialogs/app/dialogaboutapp.cpp @@ -1,65 +1,65 @@ -/************************************************************************ - ** - ** @file dialogaboutapp.cpp - ** @author Patrick Proy - ** @date 6 5, 2014 - ** - ** @brief - ** @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 - ** 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 . - ** - *************************************************************************/ - -#include "dialogaboutapp.h" -#include "ui_dialogaboutapp.h" -#include "../../version.h" - -DialogAboutApp::DialogAboutApp(QWidget *parent) : - QDialog(parent), - ui(new Ui::DialogAboutApp) -{ - ui->setupUi(this); - - ui->label_Valentina_Version->setText(QString("Valentina %1").arg(APP_VERSION)); - ui->label_QT_Version->setText(tr("Based on Qt %2 (32 bit)").arg(QT_VERSION_STR)); - - QDate date = QLocale(QLocale::C).toDate(QString(__DATE__).simplified(), QLatin1String("MMM d yyyy")); - ui->label_Valentina_Built->setText(tr("Built on %3 at %4").arg(date.toString()).arg(__TIME__)); - - ui->label_Legal_Stuff->setText(WARRANTY); - - ui->pushButton_Web_Site->setText(tr("Web site : %1").arg(VER_COMPANYDOMAIN_STR)); - connect(ui->pushButton_Web_Site, &QPushButton::clicked, - this, &DialogAboutApp::webButtonClicked ); - -} - -DialogAboutApp::~DialogAboutApp() -{ - delete ui; -} - -void DialogAboutApp::webButtonClicked() { - if ( ! QDesktopServices::openUrl(QUrl(VER_COMPANYDOMAIN_STR))) { - QMessageBox::warning(this, - tr("Warning"), - tr("Cannot open your default browser")); - } - -} +/************************************************************************ + ** + ** @file dialogaboutapp.cpp + ** @author Patrick Proy + ** @date 6 5, 2014 + ** + ** @brief + ** @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 + ** 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 . + ** + *************************************************************************/ + +#include "dialogaboutapp.h" +#include "ui_dialogaboutapp.h" +#include "../../version.h" + +DialogAboutApp::DialogAboutApp(QWidget *parent) : + QDialog(parent), + ui(new Ui::DialogAboutApp) +{ + ui->setupUi(this); + + ui->label_Valentina_Version->setText(QString("Valentina %1").arg(APP_VERSION)); + ui->label_QT_Version->setText(tr("Based on Qt %2 (32 bit)").arg(QT_VERSION_STR)); + + QDate date = QLocale(QLocale::C).toDate(QString(__DATE__).simplified(), QLatin1String("MMM d yyyy")); + ui->label_Valentina_Built->setText(tr("Built on %3 at %4").arg(date.toString()).arg(__TIME__)); + + ui->label_Legal_Stuff->setText(WARRANTY); + + ui->pushButton_Web_Site->setText(tr("Web site : %1").arg(VER_COMPANYDOMAIN_STR)); + connect(ui->pushButton_Web_Site, &QPushButton::clicked, + this, &DialogAboutApp::webButtonClicked ); + +} + +DialogAboutApp::~DialogAboutApp() +{ + delete ui; +} + +void DialogAboutApp::webButtonClicked() { + if ( ! QDesktopServices::openUrl(QUrl(VER_COMPANYDOMAIN_STR))) { + QMessageBox::warning(this, + tr("Warning"), + tr("Cannot open your default browser")); + } + +} diff --git a/src/app/dialogs/app/dialogaboutapp.h b/src/app/dialogs/app/dialogaboutapp.h index 0b771b9fe..e4df620e3 100644 --- a/src/app/dialogs/app/dialogaboutapp.h +++ b/src/app/dialogs/app/dialogaboutapp.h @@ -1,57 +1,57 @@ -/************************************************************************ - ** - ** @file dialogaboutapp.h - ** @author Patrick Proy - ** @date 6 5, 2014 - ** - ** @brief - ** @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 - ** 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 . - ** - *************************************************************************/ - -#ifndef DIALOGABOUTAPP_H -#define DIALOGABOUTAPP_H - -#include - -namespace Ui { -class DialogAboutApp; -} - -class DialogAboutApp : public QDialog -{ - Q_OBJECT - -public: - explicit DialogAboutApp(QWidget *parent = 0); - ~DialogAboutApp(); - -private: - Ui::DialogAboutApp *ui; - Q_DISABLE_COPY(DialogAboutApp) - -private slots: - /** - * @brief Fake button clicked - */ - void webButtonClicked(); -}; - -#endif // DIALOGABOUTAPP_H +/************************************************************************ + ** + ** @file dialogaboutapp.h + ** @author Patrick Proy + ** @date 6 5, 2014 + ** + ** @brief + ** @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 + ** 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 . + ** + *************************************************************************/ + +#ifndef DIALOGABOUTAPP_H +#define DIALOGABOUTAPP_H + +#include + +namespace Ui { +class DialogAboutApp; +} + +class DialogAboutApp : public QDialog +{ + Q_OBJECT + +public: + explicit DialogAboutApp(QWidget *parent = 0); + ~DialogAboutApp(); + +private: + Ui::DialogAboutApp *ui; + Q_DISABLE_COPY(DialogAboutApp) + +private slots: + /** + * @brief Fake button clicked + */ + void webButtonClicked(); +}; + +#endif // DIALOGABOUTAPP_H diff --git a/src/app/dialogs/app/dialogpatternxmledit.cpp b/src/app/dialogs/app/dialogpatternxmledit.cpp index df2244b63..3e558680e 100644 --- a/src/app/dialogs/app/dialogpatternxmledit.cpp +++ b/src/app/dialogs/app/dialogpatternxmledit.cpp @@ -1,777 +1,777 @@ -/************************************************************************ - ** - ** @file dialogpatternxmledit.cpp - ** @author Patrick Proy - ** @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 - ** 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 . - ** - *************************************************************************/ - - -#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; - -DialogPatternXmlEdit::DialogPatternXmlEdit(QWidget *parent,VPattern *xmldoc):QDialog(parent), - ui(new Ui::DialogPatternXmlEdit) -{ - ui->setupUi(this); - - xmlmodel = new VXMLTreeView(); - currentNodeEdited=nullptr; - treeChange=false; - 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(); - rootBases = new VXMLTreeElement*[drawnum+1]; - rootBasesNum=1; - rootBases[0]=new VXMLTreeElement("Valentina",VXMLTreeElement::TypeRoot,root,false); - // TODO : OOM exception. - - - 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); - - ReadNodes(root,standard_base,xmlmodel,false); - - ui->treeView_main->setModel(xmlmodel); - - //ui->treeView_main->expandAll(); - - // connectors - connect(ui->comboBox_Base_Selection,static_cast(&QComboBox::currentIndexChanged), - this,&DialogPatternXmlEdit::BaseSelectionChanged); - 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); - connect(ui->pushButton_Remove_attribute,&QPushButton::clicked, - this,&DialogPatternXmlEdit::ButtonDeleteAttributeClicked); - 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); -} - -DialogPatternXmlEdit::ChangesStackElement* DialogPatternXmlEdit::CreateStackElement(short int typechange) -{ - ChangesStackElement* tmp; - - tmp= new ChangesStackElement; - //if (tmp==0) TODO - tmp->type=typechange; - tmp->changedText=tmp->changedValue=false; - - if (changeStackLast==nullptr ) - { - changeStackLast=changeStackRoot=tmp; - } - else - { - changeStackLast->next=tmp; - changeStackLast=changeStackLast->next; - } - changeStackLast->newValue=changeStackLast->newText=nullptr; - changeStackLast->next=nullptr; - return changeStackLast; -} - -void DialogPatternXmlEdit::ButtonApplyChangesClicked() -{ - QString Changes=""; - ChangesStackElement* currentChange; - if (this->changeStackRoot==nullptr) - { - Changes="No Changes"; - } - else - { - currentChange=this->changeStackRoot; - 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("")); - Changes += "\n"; - } - 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"; - } - 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("")); - Changes += "\n"; - } - } - } - currentChange=currentChange->next; - } while (currentChange != nullptr); - } - QMessageBox::information(this, "Changes (not REALLY applied for now)", Changes); - // TODO : clear stack and apply -} - -void DialogPatternXmlEdit::ButtonCancelClicked() -{ - this->ClearEditData(); - if (this->currentNodeEditedStatus==DialogPatternXmlEdit::ChangeTypeDelete) - { - if (this->currentNodeEdited->getelementType() == VXMLTreeElement::TypeAttr) - { - // Attribute previously deleted -> resore - QFont textfont=currentNodeEdited->font(); - textfont.setStrikeOut(false); - this->currentNodeEdited->setFont(textfont); - this->RemoveChangeStackElement(this->currentNodeEditedStack); - } - else - { - QMessageBox::information(this, "Not Implemented", "Not Implemented"); - } - } -} - -void DialogPatternXmlEdit::RemoveChangeStackElement(ChangesStackElement* elmt) -{ - ChangesStackElement* index = this->changeStackRoot; - - if (changeStackRoot == elmt) - { - if (elmt->newText != nullptr) - { - delete elmt->newText; - } - if (elmt->newValue != nullptr) - { - delete elmt->newValue; - } - this->changeStackRoot = elmt->next; - if (this->changeStackLast == elmt) - { - this->changeStackLast= elmt->next; - } - delete elmt; - return; - } - while (index->next!=nullptr) { - if (index->next == elmt) - { - break; - } - index=index->next; - } - if (index->next == nullptr) - { - // TODO : debug error here - return; - } - if (index->next->newText != nullptr) - { - delete index->next->newText; - } - if (index->next->newValue != nullptr) - { - delete index->next->newValue; - } - index->next=index->next->next; - if (this->changeStackLast == elmt) - { - this->changeStackLast= index; - } - delete elmt; -} - -void DialogPatternXmlEdit::ButtonDeleteAttributeClicked() -{ - ChangesStackElement* newstack; - - // TODO : maybe assert functions here - if (this->currentNodeEdited == nullptr) - { - return; - } - if (this->currentNodeEdited->getelementType() != VXMLTreeElement::TypeAttr) - { - return; - } - - newstack = this->CreateStackElement(DialogPatternXmlEdit::ChangeTypeDelete); - newstack->element= this->currentNodeEdited; - - QFont textfont=currentNodeEdited->font(); - textfont.setStrikeOut(true); - this->currentNodeEdited->setFont(textfont); - // Activate changes - this->treeChange=true; - ui->pushButton_Apply_Changes->setEnabled(true); - // clear dialog - this->ClearEditData(); -} - -void DialogPatternXmlEdit::ButtonAddSonClicked() -{ - ChangesStackElement* newstack; - bool ok; - //clear_edit_data(); - // TODO : maybe assert functions here - if (this->currentNodeEdited == nullptr) - { - return; - } - if (this->currentNodeEdited->getelementType() != VXMLTreeElement::TypeNode) - { - return; - } - - - QString name = QInputDialog::getText(this, tr("Node Name"), - tr("Name:"), QLineEdit::Normal, - "", &ok); - if (ok==false) - { - return; - } - QString value = QInputDialog::getText(this, tr("Node Value (may be empty)"), - tr("Value:"), QLineEdit::Normal, - "", &ok); - if (ok==false) - { - return; - } - - // create element tree but do not add attribute in DOM (will be done in apply). - QDomNode empty; - VXMLTreeElement* tElement = new VXMLTreeElement( - name, - VXMLTreeElement::TypeNode, - empty,true); - - if (!value.isEmpty()) - { - tElement->setTreeNodeValue(value); - } - currentNodeEdited->appendRow(tElement); - xmlmodel->appendchain(tElement); - 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; - if (value.isEmpty()) - { - newstack->changedValue=true;newstack->newValue=new QString(value); - } - // Activate changes - treeChange=true; - ui->pushButton_Apply_Changes->setEnabled(true); -} - -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 - if (this->currentNodeEdited == nullptr) - { - return; - } - if (this->currentNodeEdited->getelementType() != VXMLTreeElement::TypeNode) - { - return; - } - - QString name = QInputDialog::getText(this, tr("Attribute Name"), - tr("Name:"), QLineEdit::Normal, - "", &ok); - if (ok==false) - { - return; - } - QString value = QInputDialog::getText(this, tr("Attribute Value"), - tr("Value:"), QLineEdit::Normal, - "", &ok); - if (ok==false) - { - return; - } - - // create element tree but do not add attribute in DOM (will be done in apply). - VXMLTreeElement* tElement = new VXMLTreeElement( - name, - VXMLTreeElement::TypeAttr, - empty,true); - - tElement->setTreeNodeValue(value); - currentNodeEdited->appendRow(tElement); - xmlmodel->appendchain(tElement); - QFont textfont=currentNodeEdited->font(); - textfont.setBold(true); - tElement->setFont(textfont); - // Add to change stack - newstack = this->CreateStackElement(DialogPatternXmlEdit::ChangeTypeAdd); - newstack->element= tElement; - newstack->type=VXMLTreeElement::TypeAttr; - newstack->newText=new QString(name); - newstack->newValue=new QString(value); - // Activate changes - treeChange=true; - ui->pushButton_Apply_Changes->setEnabled(true); -} - -void DialogPatternXmlEdit::ButtonSetClicked() -{ - QString name,value; - ChangesStackElement* newstack; - - // TODO : check if a change has already been done - if (this->currentNodeEdited == nullptr) return; - name=ui->lineEdit_Name->text(); - value=ui->lineEdit_Value->text(); - bool nameedit,valueedit; - - nameedit = (name != currentNodeEdited->gettreeNodeName()) ? true : false; - valueedit= (value != currentNodeEdited->gettreeNodeValue()) ? true : false; - - if ( nameedit == true || valueedit == true ) - { - newstack = this->CreateStackElement(DialogPatternXmlEdit::ChangeTypeModify); - newstack->element= this->currentNodeEdited; - - if (nameedit == true) - { - newstack->changedText=true; - newstack->newText=new QString(name); - currentNodeEdited->setTreeNodeName(name); - } - if (valueedit == true) - { - newstack->changedValue=true; - newstack->newValue= new QString(value); - currentNodeEdited->setTreeNodeValue(value); - } - - QFont textfont=currentNodeEdited->font(); - textfont.setBold(true); - currentNodeEdited->setFont(textfont); - treeChange=true; - ui->pushButton_Apply_Changes->setEnabled(true); - ClearEditData(); - } -} - -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); -} - -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); -} - -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); -} - -void DialogPatternXmlEdit::ClearStack() -{ - ChangesStackElement * tmp; - while (changeStackRoot != nullptr) - { - if (changeStackRoot->newText != nullptr) - { - delete changeStackRoot->newText; - } - if (changeStackRoot->newValue != nullptr) - { - delete changeStackRoot->newValue; - } - tmp=changeStackRoot; - changeStackRoot=changeStackRoot->next; - delete tmp; - } - changeStackLast=nullptr; -} - -void DialogPatternXmlEdit::BaseSelectionChanged(int value) -{ - QDomNode newbase; - - int index; - - if (this->treeChange == true) - {//Changes have been made : warn ? - } - 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 - while (rootNode->rowCount() > 0) - { - rootNode->removeRow(0); - } - xmlmodel->ClearTree(); - - VXMLTreeElement* standard_base = new VXMLTreeElement( - rootBases[index]->gettreeNodeName(), - VXMLTreeElement::TypeRoot, - rootBases[index]->getDocNode(), - false); - rootNode->appendRow(standard_base); - ReadNodes(rootBases[index]->getDocNode(),standard_base,xmlmodel,true); - - ui->treeView_main->setModel(xmlmodel); - this->ClearEditData(); - // TODO : warn if changes ? - this->ClearStack(); - return; -} - -void DialogPatternXmlEdit::ElementClicked ( const QModelIndex & index ) -{ - - // Get item (function returns parent of clicked item) - VXMLTreeElement *item = (VXMLTreeElement *)index.internalPointer(); - - // Get child specified by index row/column - VXMLTreeElement * item2 = (VXMLTreeElement *) item->child(index.row(),index.column()); - - // Clear all data and disable buttons - this->ClearEditData(); - // Check status of element in change stack - this->currentNodeEdited=item2; - this->currentNodeEditedStatus=0; - DialogPatternXmlEdit::ChangesStackElement * local = this->changeStackRoot; - while (local != nullptr) - { - 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 - if (item2->getelementType() != VXMLTreeElement::TypeRoot) - { - ui->lineEdit_Name->setText(currentNodeEdited->gettreeNodeName()); - ui->lineEdit_Name->setEnabled(true); - // Fill value - ui->lineEdit_Value->setText(currentNodeEdited->gettreeNodeValue()); - ui->lineEdit_Value->setEnabled(true); - } - if (item2->getelementType() == VXMLTreeElement::TypeRoot) - { - ui->label_type_value->setText(tr("Root node"));; - ui->pushButton_Add_son->setEnabled(true); - } - 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); - } - 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; - ChangesStackElement * tmp; - while (changeStackRoot != nullptr) - { - tmp=changeStackRoot->next; - delete changeStackRoot; - changeStackRoot=tmp; - } -} - -void DialogPatternXmlEdit::ReadNodes(QDomNode dNode, VXMLTreeElement* root,VXMLTreeView* xmlmodel, bool refresh) -{ - - QDomNode tNode,tNode2; - VXMLTreeElement* tElement,*tElement2; - int totalOfChilds = dNode.childNodes().size(); - if (totalOfChilds == 0) - { - return; // empty xml - } - for (int i=0;iappendchain(tElement); - root->appendRow(tElement); - if (tNode.nodeName() == "draw") - { - for (int i = 0; i < tNode.attributes().size(); i++) - { - if ((tNode.attributes().item(i).nodeName() == "name") - && (refresh == false)) - { - ui->comboBox_Base_Selection->addItem(tNode.attributes().item(i).nodeValue(), - QVariant(rootBasesNum)); - rootBases[rootBasesNum]=new VXMLTreeElement( - QString("Drawing %1").arg(tNode.attributes().item(i).nodeValue()), - VXMLTreeElement::TypeRoot,tNode,false);; - rootBasesNum++; - //QStandardItem* rootNode = (QStandardItem*) rootBases[rootBasesNum]->invisibleRootItem(); - //rootNode->appendRow(tElement); - } - } - } - } - } - - do - { - tElement=xmlmodel->getCurrent()->elmt; - tNode=tElement->getDocNode(); - if (tElement->getelementType() == VXMLTreeElement::TypeNode) - { - totalOfChilds = tNode.childNodes().size(); - for (int i=0;iappendchain(tElement2); - tElement->appendRow(tElement2); - } - } - - if (tNode.nodeType() == QDomNode::TextNode) - { - tElement->setTreeNodeValue(tNode.nodeValue()); - } - - for (int i = 0; i < tNode.attributes().size(); i++) - { - tElement2 = new VXMLTreeElement( - tNode.attributes().item(i).nodeName(), - VXMLTreeElement::TypeAttr, - tNode,false); - tElement2->setTreeNodeValue(tNode.attributes().item(i).nodeValue()); - tElement->appendRow(tElement2); - xmlmodel->appendchain(tElement2); - } - } - xmlmodel->SetCurrent(xmlmodel->getCurrent()->next); - } - while (xmlmodel->getCurrent() != 0); - return; -} - -// vXMLTreeView Code ----------------------------------------------------------------- -VXMLTreeView::VXMLTreeView(QObject *parent) : QStandardItemModel(parent) -{ - current=last=items=nullptr; -} - -void VXMLTreeView::ClearTree() { - - // TODO check if treeitems are deleted with the delete row command of QDom - - //clear the chain link - TreeElementchain * tmp; - while (items != nullptr) - { - tmp=items; - items=items->next; - delete tmp; - } - // reset chain counters - current=last=items=nullptr; - -} - -VXMLTreeView::~VXMLTreeView() -{ - -} - -void VXMLTreeView::appendchain(VXMLTreeElement* elmt) -{ - if (last == nullptr) - { // first element - current = new TreeElementchain; - if (current == nullptr) - { - Q_ASSERT(current != nullptr); - // TODO : throw exception - } - current->elmt=elmt; - current->next=nullptr; - last=items=current; - return; - } - TreeElementchain* temp= new TreeElementchain; - if (temp == nullptr) - { - Q_ASSERT(temp != nullptr); - // TODO : throw exception - } - temp->elmt=elmt; - temp->next=nullptr; - last->next=temp; - last=temp; -} - - -// vXMLTreeElement code ----------------------------------------------------------------- - -const short int VXMLTreeElement::TypeNode=1; -const short int VXMLTreeElement::TypeAttr=2; -const short int VXMLTreeElement::TypeRoot=3; - - -VXMLTreeElement::VXMLTreeElement(QString name, int nodetype, QDomNode source,bool editor): QStandardItem(name) -{ - this->elementType=nodetype; - this->DocNode=source; - this->addedNode=editor; - if (!editor) - { - this->DocNode=source; - } - this->treeNodeName=name; - this->treeNodeValue=""; // TODO : translation ? - this->treeNodeValueSet=false, - this->setText(this->displayText()); - switch (this->elementType) - { - case VXMLTreeElement::TypeAttr: - this->setBackground(BACKGROUND_COLOR_ATTRIBUTE); - break; - default: - break; - } -} - - -VXMLTreeElement::~VXMLTreeElement() -{ - -} - -void VXMLTreeElement::setTreeNodeValue(QString value) -{ - this->treeNodeValue=value; - this->treeNodeValueSet=true; - this->setText(this->displayText()); -} - -void VXMLTreeElement::setTreeNodeName(QString value) -{ - this->treeNodeName=value; - this->setText(this->displayText()); -} - -QString VXMLTreeElement::displayText() -{ - if (this->elementType == VXMLTreeElement::TypeRoot) - return this->treeNodeName; - return QString("%1 : %2").arg((this->treeNodeName)).arg(this->treeNodeValue); -} - -QString VXMLTreeElement::gettreeNodeValue() { - // Only return value if it's really set. - if (this->treeNodeValueSet) - return this->treeNodeValue; - else - return ""; -} - +/************************************************************************ + ** + ** @file dialogpatternxmledit.cpp + ** @author Patrick Proy + ** @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 + ** 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 . + ** + *************************************************************************/ + + +#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; + +DialogPatternXmlEdit::DialogPatternXmlEdit(QWidget *parent,VPattern *xmldoc):QDialog(parent), + ui(new Ui::DialogPatternXmlEdit) +{ + ui->setupUi(this); + + xmlmodel = new VXMLTreeView(); + currentNodeEdited=nullptr; + treeChange=false; + 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(); + rootBases = new VXMLTreeElement*[drawnum+1]; + rootBasesNum=1; + rootBases[0]=new VXMLTreeElement("Valentina",VXMLTreeElement::TypeRoot,root,false); + // TODO : OOM exception. + + + 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); + + ReadNodes(root,standard_base,xmlmodel,false); + + ui->treeView_main->setModel(xmlmodel); + + //ui->treeView_main->expandAll(); + + // connectors + connect(ui->comboBox_Base_Selection,static_cast(&QComboBox::currentIndexChanged), + this,&DialogPatternXmlEdit::BaseSelectionChanged); + 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); + connect(ui->pushButton_Remove_attribute,&QPushButton::clicked, + this,&DialogPatternXmlEdit::ButtonDeleteAttributeClicked); + 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); +} + +DialogPatternXmlEdit::ChangesStackElement* DialogPatternXmlEdit::CreateStackElement(short int typechange) +{ + ChangesStackElement* tmp; + + tmp= new ChangesStackElement; + //if (tmp==0) TODO + tmp->type=typechange; + tmp->changedText=tmp->changedValue=false; + + if (changeStackLast==nullptr ) + { + changeStackLast=changeStackRoot=tmp; + } + else + { + changeStackLast->next=tmp; + changeStackLast=changeStackLast->next; + } + changeStackLast->newValue=changeStackLast->newText=nullptr; + changeStackLast->next=nullptr; + return changeStackLast; +} + +void DialogPatternXmlEdit::ButtonApplyChangesClicked() +{ + QString Changes=""; + ChangesStackElement* currentChange; + if (this->changeStackRoot==nullptr) + { + Changes="No Changes"; + } + else + { + currentChange=this->changeStackRoot; + 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("")); + Changes += "\n"; + } + 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"; + } + 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("")); + Changes += "\n"; + } + } + } + currentChange=currentChange->next; + } while (currentChange != nullptr); + } + QMessageBox::information(this, "Changes (not REALLY applied for now)", Changes); + // TODO : clear stack and apply +} + +void DialogPatternXmlEdit::ButtonCancelClicked() +{ + this->ClearEditData(); + if (this->currentNodeEditedStatus==DialogPatternXmlEdit::ChangeTypeDelete) + { + if (this->currentNodeEdited->getelementType() == VXMLTreeElement::TypeAttr) + { + // Attribute previously deleted -> resore + QFont textfont=currentNodeEdited->font(); + textfont.setStrikeOut(false); + this->currentNodeEdited->setFont(textfont); + this->RemoveChangeStackElement(this->currentNodeEditedStack); + } + else + { + QMessageBox::information(this, "Not Implemented", "Not Implemented"); + } + } +} + +void DialogPatternXmlEdit::RemoveChangeStackElement(ChangesStackElement* elmt) +{ + ChangesStackElement* index = this->changeStackRoot; + + if (changeStackRoot == elmt) + { + if (elmt->newText != nullptr) + { + delete elmt->newText; + } + if (elmt->newValue != nullptr) + { + delete elmt->newValue; + } + this->changeStackRoot = elmt->next; + if (this->changeStackLast == elmt) + { + this->changeStackLast= elmt->next; + } + delete elmt; + return; + } + while (index->next!=nullptr) { + if (index->next == elmt) + { + break; + } + index=index->next; + } + if (index->next == nullptr) + { + // TODO : debug error here + return; + } + if (index->next->newText != nullptr) + { + delete index->next->newText; + } + if (index->next->newValue != nullptr) + { + delete index->next->newValue; + } + index->next=index->next->next; + if (this->changeStackLast == elmt) + { + this->changeStackLast= index; + } + delete elmt; +} + +void DialogPatternXmlEdit::ButtonDeleteAttributeClicked() +{ + ChangesStackElement* newstack; + + // TODO : maybe assert functions here + if (this->currentNodeEdited == nullptr) + { + return; + } + if (this->currentNodeEdited->getelementType() != VXMLTreeElement::TypeAttr) + { + return; + } + + newstack = this->CreateStackElement(DialogPatternXmlEdit::ChangeTypeDelete); + newstack->element= this->currentNodeEdited; + + QFont textfont=currentNodeEdited->font(); + textfont.setStrikeOut(true); + this->currentNodeEdited->setFont(textfont); + // Activate changes + this->treeChange=true; + ui->pushButton_Apply_Changes->setEnabled(true); + // clear dialog + this->ClearEditData(); +} + +void DialogPatternXmlEdit::ButtonAddSonClicked() +{ + ChangesStackElement* newstack; + bool ok; + //clear_edit_data(); + // TODO : maybe assert functions here + if (this->currentNodeEdited == nullptr) + { + return; + } + if (this->currentNodeEdited->getelementType() != VXMLTreeElement::TypeNode) + { + return; + } + + + QString name = QInputDialog::getText(this, tr("Node Name"), + tr("Name:"), QLineEdit::Normal, + "", &ok); + if (ok==false) + { + return; + } + QString value = QInputDialog::getText(this, tr("Node Value (may be empty)"), + tr("Value:"), QLineEdit::Normal, + "", &ok); + if (ok==false) + { + return; + } + + // create element tree but do not add attribute in DOM (will be done in apply). + QDomNode empty; + VXMLTreeElement* tElement = new VXMLTreeElement( + name, + VXMLTreeElement::TypeNode, + empty,true); + + if (!value.isEmpty()) + { + tElement->setTreeNodeValue(value); + } + currentNodeEdited->appendRow(tElement); + xmlmodel->appendchain(tElement); + 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; + if (value.isEmpty()) + { + newstack->changedValue=true;newstack->newValue=new QString(value); + } + // Activate changes + treeChange=true; + ui->pushButton_Apply_Changes->setEnabled(true); +} + +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 + if (this->currentNodeEdited == nullptr) + { + return; + } + if (this->currentNodeEdited->getelementType() != VXMLTreeElement::TypeNode) + { + return; + } + + QString name = QInputDialog::getText(this, tr("Attribute Name"), + tr("Name:"), QLineEdit::Normal, + "", &ok); + if (ok==false) + { + return; + } + QString value = QInputDialog::getText(this, tr("Attribute Value"), + tr("Value:"), QLineEdit::Normal, + "", &ok); + if (ok==false) + { + return; + } + + // create element tree but do not add attribute in DOM (will be done in apply). + VXMLTreeElement* tElement = new VXMLTreeElement( + name, + VXMLTreeElement::TypeAttr, + empty,true); + + tElement->setTreeNodeValue(value); + currentNodeEdited->appendRow(tElement); + xmlmodel->appendchain(tElement); + QFont textfont=currentNodeEdited->font(); + textfont.setBold(true); + tElement->setFont(textfont); + // Add to change stack + newstack = this->CreateStackElement(DialogPatternXmlEdit::ChangeTypeAdd); + newstack->element= tElement; + newstack->type=VXMLTreeElement::TypeAttr; + newstack->newText=new QString(name); + newstack->newValue=new QString(value); + // Activate changes + treeChange=true; + ui->pushButton_Apply_Changes->setEnabled(true); +} + +void DialogPatternXmlEdit::ButtonSetClicked() +{ + QString name,value; + ChangesStackElement* newstack; + + // TODO : check if a change has already been done + if (this->currentNodeEdited == nullptr) return; + name=ui->lineEdit_Name->text(); + value=ui->lineEdit_Value->text(); + bool nameedit,valueedit; + + nameedit = (name != currentNodeEdited->gettreeNodeName()) ? true : false; + valueedit= (value != currentNodeEdited->gettreeNodeValue()) ? true : false; + + if ( nameedit == true || valueedit == true ) + { + newstack = this->CreateStackElement(DialogPatternXmlEdit::ChangeTypeModify); + newstack->element= this->currentNodeEdited; + + if (nameedit == true) + { + newstack->changedText=true; + newstack->newText=new QString(name); + currentNodeEdited->setTreeNodeName(name); + } + if (valueedit == true) + { + newstack->changedValue=true; + newstack->newValue= new QString(value); + currentNodeEdited->setTreeNodeValue(value); + } + + QFont textfont=currentNodeEdited->font(); + textfont.setBold(true); + currentNodeEdited->setFont(textfont); + treeChange=true; + ui->pushButton_Apply_Changes->setEnabled(true); + ClearEditData(); + } +} + +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); +} + +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); +} + +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); +} + +void DialogPatternXmlEdit::ClearStack() +{ + ChangesStackElement * tmp; + while (changeStackRoot != nullptr) + { + if (changeStackRoot->newText != nullptr) + { + delete changeStackRoot->newText; + } + if (changeStackRoot->newValue != nullptr) + { + delete changeStackRoot->newValue; + } + tmp=changeStackRoot; + changeStackRoot=changeStackRoot->next; + delete tmp; + } + changeStackLast=nullptr; +} + +void DialogPatternXmlEdit::BaseSelectionChanged(int value) +{ + QDomNode newbase; + + int index; + + if (this->treeChange == true) + {//Changes have been made : warn ? + } + 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 + while (rootNode->rowCount() > 0) + { + rootNode->removeRow(0); + } + xmlmodel->ClearTree(); + + VXMLTreeElement* standard_base = new VXMLTreeElement( + rootBases[index]->gettreeNodeName(), + VXMLTreeElement::TypeRoot, + rootBases[index]->getDocNode(), + false); + rootNode->appendRow(standard_base); + ReadNodes(rootBases[index]->getDocNode(),standard_base,xmlmodel,true); + + ui->treeView_main->setModel(xmlmodel); + this->ClearEditData(); + // TODO : warn if changes ? + this->ClearStack(); + return; +} + +void DialogPatternXmlEdit::ElementClicked ( const QModelIndex & index ) +{ + + // Get item (function returns parent of clicked item) + VXMLTreeElement *item = (VXMLTreeElement *)index.internalPointer(); + + // Get child specified by index row/column + VXMLTreeElement * item2 = (VXMLTreeElement *) item->child(index.row(),index.column()); + + // Clear all data and disable buttons + this->ClearEditData(); + // Check status of element in change stack + this->currentNodeEdited=item2; + this->currentNodeEditedStatus=0; + DialogPatternXmlEdit::ChangesStackElement * local = this->changeStackRoot; + while (local != nullptr) + { + 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 + if (item2->getelementType() != VXMLTreeElement::TypeRoot) + { + ui->lineEdit_Name->setText(currentNodeEdited->gettreeNodeName()); + ui->lineEdit_Name->setEnabled(true); + // Fill value + ui->lineEdit_Value->setText(currentNodeEdited->gettreeNodeValue()); + ui->lineEdit_Value->setEnabled(true); + } + if (item2->getelementType() == VXMLTreeElement::TypeRoot) + { + ui->label_type_value->setText(tr("Root node"));; + ui->pushButton_Add_son->setEnabled(true); + } + 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); + } + 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; + ChangesStackElement * tmp; + while (changeStackRoot != nullptr) + { + tmp=changeStackRoot->next; + delete changeStackRoot; + changeStackRoot=tmp; + } +} + +void DialogPatternXmlEdit::ReadNodes(QDomNode dNode, VXMLTreeElement* root,VXMLTreeView* xmlmodel, bool refresh) +{ + + QDomNode tNode,tNode2; + VXMLTreeElement* tElement,*tElement2; + int totalOfChilds = dNode.childNodes().size(); + if (totalOfChilds == 0) + { + return; // empty xml + } + for (int i=0;iappendchain(tElement); + root->appendRow(tElement); + if (tNode.nodeName() == "draw") + { + for (int i = 0; i < tNode.attributes().size(); i++) + { + if ((tNode.attributes().item(i).nodeName() == "name") + && (refresh == false)) + { + ui->comboBox_Base_Selection->addItem(tNode.attributes().item(i).nodeValue(), + QVariant(rootBasesNum)); + rootBases[rootBasesNum]=new VXMLTreeElement( + QString("Drawing %1").arg(tNode.attributes().item(i).nodeValue()), + VXMLTreeElement::TypeRoot,tNode,false);; + rootBasesNum++; + //QStandardItem* rootNode = (QStandardItem*) rootBases[rootBasesNum]->invisibleRootItem(); + //rootNode->appendRow(tElement); + } + } + } + } + } + + do + { + tElement=xmlmodel->getCurrent()->elmt; + tNode=tElement->getDocNode(); + if (tElement->getelementType() == VXMLTreeElement::TypeNode) + { + totalOfChilds = tNode.childNodes().size(); + for (int i=0;iappendchain(tElement2); + tElement->appendRow(tElement2); + } + } + + if (tNode.nodeType() == QDomNode::TextNode) + { + tElement->setTreeNodeValue(tNode.nodeValue()); + } + + for (int i = 0; i < tNode.attributes().size(); i++) + { + tElement2 = new VXMLTreeElement( + tNode.attributes().item(i).nodeName(), + VXMLTreeElement::TypeAttr, + tNode,false); + tElement2->setTreeNodeValue(tNode.attributes().item(i).nodeValue()); + tElement->appendRow(tElement2); + xmlmodel->appendchain(tElement2); + } + } + xmlmodel->SetCurrent(xmlmodel->getCurrent()->next); + } + while (xmlmodel->getCurrent() != 0); + return; +} + +// vXMLTreeView Code ----------------------------------------------------------------- +VXMLTreeView::VXMLTreeView(QObject *parent) : QStandardItemModel(parent) +{ + current=last=items=nullptr; +} + +void VXMLTreeView::ClearTree() { + + // TODO check if treeitems are deleted with the delete row command of QDom + + //clear the chain link + TreeElementchain * tmp; + while (items != nullptr) + { + tmp=items; + items=items->next; + delete tmp; + } + // reset chain counters + current=last=items=nullptr; + +} + +VXMLTreeView::~VXMLTreeView() +{ + +} + +void VXMLTreeView::appendchain(VXMLTreeElement* elmt) +{ + if (last == nullptr) + { // first element + current = new TreeElementchain; + if (current == nullptr) + { + Q_ASSERT(current != nullptr); + // TODO : throw exception + } + current->elmt=elmt; + current->next=nullptr; + last=items=current; + return; + } + TreeElementchain* temp= new TreeElementchain; + if (temp == nullptr) + { + Q_ASSERT(temp != nullptr); + // TODO : throw exception + } + temp->elmt=elmt; + temp->next=nullptr; + last->next=temp; + last=temp; +} + + +// vXMLTreeElement code ----------------------------------------------------------------- + +const short int VXMLTreeElement::TypeNode=1; +const short int VXMLTreeElement::TypeAttr=2; +const short int VXMLTreeElement::TypeRoot=3; + + +VXMLTreeElement::VXMLTreeElement(QString name, int nodetype, QDomNode source,bool editor): QStandardItem(name) +{ + this->elementType=nodetype; + this->DocNode=source; + this->addedNode=editor; + if (!editor) + { + this->DocNode=source; + } + this->treeNodeName=name; + this->treeNodeValue=""; // TODO : translation ? + this->treeNodeValueSet=false, + this->setText(this->displayText()); + switch (this->elementType) + { + case VXMLTreeElement::TypeAttr: + this->setBackground(BACKGROUND_COLOR_ATTRIBUTE); + break; + default: + break; + } +} + + +VXMLTreeElement::~VXMLTreeElement() +{ + +} + +void VXMLTreeElement::setTreeNodeValue(QString value) +{ + this->treeNodeValue=value; + this->treeNodeValueSet=true; + this->setText(this->displayText()); +} + +void VXMLTreeElement::setTreeNodeName(QString value) +{ + this->treeNodeName=value; + this->setText(this->displayText()); +} + +QString VXMLTreeElement::displayText() +{ + if (this->elementType == VXMLTreeElement::TypeRoot) + return this->treeNodeName; + return QString("%1 : %2").arg((this->treeNodeName)).arg(this->treeNodeValue); +} + +QString VXMLTreeElement::gettreeNodeValue() { + // Only return value if it's really set. + if (this->treeNodeValueSet) + return this->treeNodeValue; + else + return ""; +} + diff --git a/src/app/dialogs/app/dialogpatternxmledit.h b/src/app/dialogs/app/dialogpatternxmledit.h index 96eb01d8d..6f16892cf 100644 --- a/src/app/dialogs/app/dialogpatternxmledit.h +++ b/src/app/dialogs/app/dialogpatternxmledit.h @@ -1,282 +1,282 @@ -/************************************************************************ - ** - ** @file dialogpatternxmledit.h - ** @author Patrick Proy - ** @date 14 5, 2014 - ** - ** @brief Include 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 - ** 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 . - ** - *************************************************************************/ - -#ifndef DIALOGPATTERNXMLEDIT_H -#define DIALOGPATTERNXMLEDIT_H - -#include -#include -#include "../../xml/vpattern.h" - -//******************************************************************************************** -/** - * @brief The vXMLTreeElement class : node/attribute of xml pattern. Used by vXMLTreeView - * - ********************************************************************************************** - */ - -#define BACKGROUND_COLOR_ATTRIBUTE QBrush(Qt::GlobalColor::cyan) -class VXMLTreeElement : public QStandardItem -{ - -public: - /** - * @brief vXMLTreeElement - * @param name : display name - * @param nodetype : node type (node, attribute, root) - * @param source : the source dom node - */ - explicit VXMLTreeElement (QString name, int nodetype, QDomNode source,bool editor); - - explicit VXMLTreeElement (QString name, int nodetype); - - ~VXMLTreeElement (); - static const short int TypeNode; - static const short int TypeAttr; - static const short int TypeRoot; - /** - * @brief setNodeValue : set value of node (content or attribute) - * @param value : the value - */ - void setTreeNodeValue(QString value); - /** - * @brief displayText : text to display - * @return text to display - */ - QString displayText(); - /** - * @brief getDocNode - * @return current document node - */ - QDomNode getDocNode(); - QString gettreeNodeName(); - QString gettreeNodeValue(); - bool gettreeNodeValueSet(); - void setTreeNodeName(QString value); - /** - * @brief getelementType - * @return elementType value - */ - short int getelementType(); - -private: - /** - * @brief DocNode : link to current document node - */ - QDomNode DocNode; - - /** - * @brief addedNode : true if node is added by editor (implies empty DocNode) - */ - bool addedNode; - /** - * @brief type : element type (node, attribute, text value of node) - */ - short int elementType; - /** - * @brief nodeValue : Attribute or node value - */ - QString treeNodeValue; - /** - * @brief treeNodeValueSet : true if value has been set - */ - bool treeNodeValueSet; - /** - * @brief nodeValue : Attribute or node value - */ - QString treeNodeName; - Q_DISABLE_COPY(VXMLTreeElement) -}; - -inline short int VXMLTreeElement::getelementType() -{ - return this->elementType; -} - -inline QDomNode VXMLTreeElement::getDocNode() -{ - return this->DocNode; -} - -inline QString VXMLTreeElement::gettreeNodeName() -{ - return this->treeNodeName; -} - -inline bool VXMLTreeElement::gettreeNodeValueSet() -{ - return this->treeNodeValueSet; -} - -//******************************************************************************************** -/** - * @brief The vXMLTreeView class : container to display/edit xml pattern - */ -class VXMLTreeView : public QStandardItemModel -{ -public: - explicit VXMLTreeView (QObject *parent = 0); - void appendchain(VXMLTreeElement* elmt); - - /** - * @brief The TreeElement struct : chained list of vXMLTreeElement - */ - typedef struct TreeElementchain { - VXMLTreeElement* elmt; - TreeElementchain* next; - } TreeElementchain; - - TreeElementchain * getCurrent(); - void SetCurrent(TreeElementchain * value); - TreeElementchain * GetLast(); - TreeElementchain * GetItems(); - /** - * @brief clearTree : clear tree elements and listed items. - */ - void ClearTree(); - ~VXMLTreeView (); -private: - Q_DISABLE_COPY(VXMLTreeView) - - /** - * @brief items : root of chained list of vXMLTreeElement - */ - TreeElementchain * items; - /** - * @brief current : used to parse in iteration. - */ - TreeElementchain * current; - /** - * @brief last : used to parse in iteration. - */ - TreeElementchain * last; - -}; - -inline VXMLTreeView::TreeElementchain * VXMLTreeView::getCurrent() -{ - return this->current; -} -inline void VXMLTreeView::SetCurrent(VXMLTreeView::TreeElementchain * value) -{ - this->current=value; -} -inline VXMLTreeView::TreeElementchain * VXMLTreeView::GetLast() -{ - return this->last; -} -inline VXMLTreeView::TreeElementchain * VXMLTreeView::GetItems() -{ - return this->items; -} - -//******************************************************************************************** - -namespace Ui { -class DialogPatternXmlEdit; -} -/** - * @brief The DialogPatternXmlEdit class : ui dialog for XML editing of pattern - */ -class DialogPatternXmlEdit : public QDialog -{ - Q_OBJECT - -public: - explicit DialogPatternXmlEdit(QWidget *parent = 0, VPattern *xmldoc = 0); - ~DialogPatternXmlEdit(); - - /** - * @brief clear_edit_data : clear input boxes and disables buttons. - */ - void ClearEditData(); - - void NameTextEdited(QString newtext); - void ValueTextEdited(QString newtext); - void ButtonSetClicked(); - void ButtonCancelClicked(); - void ButtonDeleteAttributeClicked(); - void ButtonAddSonClicked(); - void ButtonAddAttributeClicked(); - void ButtonApplyChangesClicked() ; - - - // Stack of changes definition - typedef struct ChangesStackElement { - short int type; - VXMLTreeElement *element; - QString *newText; - bool changedText; - QString *newValue; - bool changedValue; - ChangesStackElement* next; - } ChangesStackElement; - - // Change stack functions - ChangesStackElement* CreateStackElement(short int typechange); - void RemoveChangeStackElement(ChangesStackElement* elmt); - void ClearStack(); - bool treeChange; - VXMLTreeElement* currentNodeEdited; - short int currentNodeEditedStatus; - ChangesStackElement* currentNodeEditedStack; - -private slots: - void BaseSelectionChanged(int value); - void ElementClicked ( const QModelIndex & index ); - -private: - Q_DISABLE_COPY(DialogPatternXmlEdit) - Ui::DialogPatternXmlEdit *ui; - - VPattern *doc; - - QDomElement root; - QStandardItem *rootNode; - //typedef struct rootbases { vXMLTreeElement * root; QString name; } rootbases; - VXMLTreeElement ** rootBases; - qint16 rootBasesNum; - - void ReadNodes(QDomNode dNode, VXMLTreeElement* root, VXMLTreeView *xmlmodel, bool refresh); - VXMLTreeView* xmlmodel; - - - // Stack of changes - static const short int ChangeTypeDelete; - static const short int ChangeTypeAdd; - static const short int ChangeTypeModify; - - // Stack of changes - - ChangesStackElement* changeStackRoot; - ChangesStackElement* changeStackLast; -}; - - - -#endif // DIALOGPATTERNXMLEDIT_H +/************************************************************************ + ** + ** @file dialogpatternxmledit.h + ** @author Patrick Proy + ** @date 14 5, 2014 + ** + ** @brief Include 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 + ** 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 . + ** + *************************************************************************/ + +#ifndef DIALOGPATTERNXMLEDIT_H +#define DIALOGPATTERNXMLEDIT_H + +#include +#include +#include "../../xml/vpattern.h" + +//******************************************************************************************** +/** + * @brief The vXMLTreeElement class : node/attribute of xml pattern. Used by vXMLTreeView + * + ********************************************************************************************** + */ + +#define BACKGROUND_COLOR_ATTRIBUTE QBrush(Qt::GlobalColor::cyan) +class VXMLTreeElement : public QStandardItem +{ + +public: + /** + * @brief vXMLTreeElement + * @param name : display name + * @param nodetype : node type (node, attribute, root) + * @param source : the source dom node + */ + explicit VXMLTreeElement (QString name, int nodetype, QDomNode source,bool editor); + + explicit VXMLTreeElement (QString name, int nodetype); + + ~VXMLTreeElement (); + static const short int TypeNode; + static const short int TypeAttr; + static const short int TypeRoot; + /** + * @brief setNodeValue : set value of node (content or attribute) + * @param value : the value + */ + void setTreeNodeValue(QString value); + /** + * @brief displayText : text to display + * @return text to display + */ + QString displayText(); + /** + * @brief getDocNode + * @return current document node + */ + QDomNode getDocNode(); + QString gettreeNodeName(); + QString gettreeNodeValue(); + bool gettreeNodeValueSet(); + void setTreeNodeName(QString value); + /** + * @brief getelementType + * @return elementType value + */ + short int getelementType(); + +private: + /** + * @brief DocNode : link to current document node + */ + QDomNode DocNode; + + /** + * @brief addedNode : true if node is added by editor (implies empty DocNode) + */ + bool addedNode; + /** + * @brief type : element type (node, attribute, text value of node) + */ + short int elementType; + /** + * @brief nodeValue : Attribute or node value + */ + QString treeNodeValue; + /** + * @brief treeNodeValueSet : true if value has been set + */ + bool treeNodeValueSet; + /** + * @brief nodeValue : Attribute or node value + */ + QString treeNodeName; + Q_DISABLE_COPY(VXMLTreeElement) +}; + +inline short int VXMLTreeElement::getelementType() +{ + return this->elementType; +} + +inline QDomNode VXMLTreeElement::getDocNode() +{ + return this->DocNode; +} + +inline QString VXMLTreeElement::gettreeNodeName() +{ + return this->treeNodeName; +} + +inline bool VXMLTreeElement::gettreeNodeValueSet() +{ + return this->treeNodeValueSet; +} + +//******************************************************************************************** +/** + * @brief The vXMLTreeView class : container to display/edit xml pattern + */ +class VXMLTreeView : public QStandardItemModel +{ +public: + explicit VXMLTreeView (QObject *parent = 0); + void appendchain(VXMLTreeElement* elmt); + + /** + * @brief The TreeElement struct : chained list of vXMLTreeElement + */ + typedef struct TreeElementchain { + VXMLTreeElement* elmt; + TreeElementchain* next; + } TreeElementchain; + + TreeElementchain * getCurrent(); + void SetCurrent(TreeElementchain * value); + TreeElementchain * GetLast(); + TreeElementchain * GetItems(); + /** + * @brief clearTree : clear tree elements and listed items. + */ + void ClearTree(); + ~VXMLTreeView (); +private: + Q_DISABLE_COPY(VXMLTreeView) + + /** + * @brief items : root of chained list of vXMLTreeElement + */ + TreeElementchain * items; + /** + * @brief current : used to parse in iteration. + */ + TreeElementchain * current; + /** + * @brief last : used to parse in iteration. + */ + TreeElementchain * last; + +}; + +inline VXMLTreeView::TreeElementchain * VXMLTreeView::getCurrent() +{ + return this->current; +} +inline void VXMLTreeView::SetCurrent(VXMLTreeView::TreeElementchain * value) +{ + this->current=value; +} +inline VXMLTreeView::TreeElementchain * VXMLTreeView::GetLast() +{ + return this->last; +} +inline VXMLTreeView::TreeElementchain * VXMLTreeView::GetItems() +{ + return this->items; +} + +//******************************************************************************************** + +namespace Ui { +class DialogPatternXmlEdit; +} +/** + * @brief The DialogPatternXmlEdit class : ui dialog for XML editing of pattern + */ +class DialogPatternXmlEdit : public QDialog +{ + Q_OBJECT + +public: + explicit DialogPatternXmlEdit(QWidget *parent = 0, VPattern *xmldoc = 0); + ~DialogPatternXmlEdit(); + + /** + * @brief clear_edit_data : clear input boxes and disables buttons. + */ + void ClearEditData(); + + void NameTextEdited(QString newtext); + void ValueTextEdited(QString newtext); + void ButtonSetClicked(); + void ButtonCancelClicked(); + void ButtonDeleteAttributeClicked(); + void ButtonAddSonClicked(); + void ButtonAddAttributeClicked(); + void ButtonApplyChangesClicked() ; + + + // Stack of changes definition + typedef struct ChangesStackElement { + short int type; + VXMLTreeElement *element; + QString *newText; + bool changedText; + QString *newValue; + bool changedValue; + ChangesStackElement* next; + } ChangesStackElement; + + // Change stack functions + ChangesStackElement* CreateStackElement(short int typechange); + void RemoveChangeStackElement(ChangesStackElement* elmt); + void ClearStack(); + bool treeChange; + VXMLTreeElement* currentNodeEdited; + short int currentNodeEditedStatus; + ChangesStackElement* currentNodeEditedStack; + +private slots: + void BaseSelectionChanged(int value); + void ElementClicked ( const QModelIndex & index ); + +private: + Q_DISABLE_COPY(DialogPatternXmlEdit) + Ui::DialogPatternXmlEdit *ui; + + VPattern *doc; + + QDomElement root; + QStandardItem *rootNode; + //typedef struct rootbases { vXMLTreeElement * root; QString name; } rootbases; + VXMLTreeElement ** rootBases; + qint16 rootBasesNum; + + void ReadNodes(QDomNode dNode, VXMLTreeElement* root, VXMLTreeView *xmlmodel, bool refresh); + VXMLTreeView* xmlmodel; + + + // Stack of changes + static const short int ChangeTypeDelete; + static const short int ChangeTypeAdd; + static const short int ChangeTypeModify; + + // Stack of changes + + ChangesStackElement* changeStackRoot; + ChangesStackElement* changeStackLast; +}; + + + +#endif // DIALOGPATTERNXMLEDIT_H From b28bd29092d4c5a2cbc0f368d98007cb80f4df7c Mon Sep 17 00:00:00 2001 From: dismine Date: Fri, 16 May 2014 20:00:13 +0300 Subject: [PATCH 06/15] Vera++. --HG-- branch : XML Editor --- src/app/container/vincrement.cpp | 3 +- src/app/dialogs/app/dialogaboutapp.cpp | 16 +- src/app/dialogs/app/dialogaboutapp.h | 5 +- src/app/dialogs/app/dialogpatternxmledit.cpp | 167 +++++++++++-------- src/app/dialogs/app/dialogpatternxmledit.h | 20 +-- src/app/mainwindow.cpp | 8 +- src/app/widgets/vapplication.cpp | 6 +- src/libs/qmuparser/qmuparser.h | 2 +- 8 files changed, 128 insertions(+), 99 deletions(-) diff --git a/src/app/container/vincrement.cpp b/src/app/container/vincrement.cpp index 13f1f6da3..eff25c697 100644 --- a/src/app/container/vincrement.cpp +++ b/src/app/container/vincrement.cpp @@ -30,7 +30,8 @@ //--------------------------------------------------------------------------------------------------------------------- VIncrement::VIncrement() - :id(0), base(0), ksize(50.0), kheight(176.0), description(QString()){} + :id(0), base(0), ksize(50.0), kheight(176.0), description(QString()) +{} VIncrement::VIncrement(quint32 id, qreal base, qreal ksize, qreal kheight, QString description) :id(id), base(base), ksize(ksize), kheight(kheight), description(description) diff --git a/src/app/dialogs/app/dialogaboutapp.cpp b/src/app/dialogs/app/dialogaboutapp.cpp index 310d6f65b..04e78721e 100644 --- a/src/app/dialogs/app/dialogaboutapp.cpp +++ b/src/app/dialogs/app/dialogaboutapp.cpp @@ -30,6 +30,10 @@ #include "ui_dialogaboutapp.h" #include "../../version.h" +#include +#include + +//--------------------------------------------------------------------------------------------------------------------- DialogAboutApp::DialogAboutApp(QWidget *parent) : QDialog(parent), ui(new Ui::DialogAboutApp) @@ -50,16 +54,18 @@ DialogAboutApp::DialogAboutApp(QWidget *parent) : } +//--------------------------------------------------------------------------------------------------------------------- DialogAboutApp::~DialogAboutApp() { delete ui; } -void DialogAboutApp::webButtonClicked() { - if ( ! QDesktopServices::openUrl(QUrl(VER_COMPANYDOMAIN_STR))) { - QMessageBox::warning(this, - tr("Warning"), - tr("Cannot open your default browser")); +//--------------------------------------------------------------------------------------------------------------------- +void DialogAboutApp::webButtonClicked() +{ + if ( QDesktopServices::openUrl(QUrl(VER_COMPANYDOMAIN_STR)) == false) + { + QMessageBox::warning(this, tr("Warning"), tr("Cannot open your default browser")); } } diff --git a/src/app/dialogs/app/dialogaboutapp.h b/src/app/dialogs/app/dialogaboutapp.h index e4df620e3..830c869f8 100644 --- a/src/app/dialogs/app/dialogaboutapp.h +++ b/src/app/dialogs/app/dialogaboutapp.h @@ -31,8 +31,9 @@ #include -namespace Ui { -class DialogAboutApp; +namespace Ui +{ + class DialogAboutApp; } class DialogAboutApp : public QDialog diff --git a/src/app/dialogs/app/dialogpatternxmledit.cpp b/src/app/dialogs/app/dialogpatternxmledit.cpp index 3e558680e..f107f4c9a 100644 --- a/src/app/dialogs/app/dialogpatternxmledit.cpp +++ b/src/app/dialogs/app/dialogpatternxmledit.cpp @@ -26,24 +26,24 @@ ** *************************************************************************/ - #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; -DialogPatternXmlEdit::DialogPatternXmlEdit(QWidget *parent,VPattern *xmldoc):QDialog(parent), - ui(new Ui::DialogPatternXmlEdit) + +//--------------------------------------------------------------------------------------------------------------------- +DialogPatternXmlEdit::DialogPatternXmlEdit(QWidget *parent, VPattern *xmldoc) + :QDialog(parent), ui(new Ui::DialogPatternXmlEdit) { ui->setupUi(this); xmlmodel = new VXMLTreeView(); currentNodeEdited=nullptr; treeChange=false; - this->changeStackRoot = this->changeStackLast =nullptr ; + this->changeStackRoot = this->changeStackLast = nullptr; doc=xmldoc; root = doc->documentElement(); @@ -59,37 +59,40 @@ DialogPatternXmlEdit::DialogPatternXmlEdit(QWidget *parent,VPattern *xmldoc):QDi qint16 drawnum=xmldoc->elementsByTagName("draw").size(); rootBases = new VXMLTreeElement*[drawnum+1]; rootBasesNum=1; - rootBases[0]=new VXMLTreeElement("Valentina",VXMLTreeElement::TypeRoot,root,false); + rootBases[0]=new VXMLTreeElement("Valentina", VXMLTreeElement::TypeRoot, root, false); // TODO : OOM exception. - VXMLTreeElement* standard_base = new VXMLTreeElement("Valentina",VXMLTreeElement::TypeRoot,root,false); - ui->comboBox_Base_Selection->addItem(tr("All drawings"),QVariant(0)); + 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); - ReadNodes(root,standard_base,xmlmodel,false); + ReadNodes(root, standard_base, xmlmodel, false); ui->treeView_main->setModel(xmlmodel); //ui->treeView_main->expandAll(); // connectors - connect(ui->comboBox_Base_Selection,static_cast(&QComboBox::currentIndexChanged), - this,&DialogPatternXmlEdit::BaseSelectionChanged); - 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); - connect(ui->pushButton_Remove_attribute,&QPushButton::clicked, - this,&DialogPatternXmlEdit::ButtonDeleteAttributeClicked); - 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); + connect(ui->comboBox_Base_Selection, static_cast(&QComboBox::currentIndexChanged), + this, &DialogPatternXmlEdit::BaseSelectionChanged); + 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); + connect(ui->pushButton_Remove_attribute, &QPushButton::clicked, + this, &DialogPatternXmlEdit::ButtonDeleteAttributeClicked); + 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); } +//--------------------------------------------------------------------------------------------------------------------- DialogPatternXmlEdit::ChangesStackElement* DialogPatternXmlEdit::CreateStackElement(short int typechange) { ChangesStackElement* tmp; @@ -113,6 +116,7 @@ DialogPatternXmlEdit::ChangesStackElement* DialogPatternXmlEdit::CreateStackElem return changeStackLast; } +//--------------------------------------------------------------------------------------------------------------------- void DialogPatternXmlEdit::ButtonApplyChangesClicked() { QString Changes=""; @@ -155,7 +159,7 @@ void DialogPatternXmlEdit::ButtonApplyChangesClicked() Changes += (currentChange->element->gettreeNodeValueSet()) ? currentChange->element->gettreeNodeValue(): QString(tr("")); Changes += "\n"; - } + } } } currentChange=currentChange->next; @@ -165,6 +169,7 @@ void DialogPatternXmlEdit::ButtonApplyChangesClicked() // TODO : clear stack and apply } +//--------------------------------------------------------------------------------------------------------------------- void DialogPatternXmlEdit::ButtonCancelClicked() { this->ClearEditData(); @@ -185,6 +190,7 @@ void DialogPatternXmlEdit::ButtonCancelClicked() } } +//--------------------------------------------------------------------------------------------------------------------- void DialogPatternXmlEdit::RemoveChangeStackElement(ChangesStackElement* elmt) { ChangesStackElement* index = this->changeStackRoot; @@ -207,7 +213,8 @@ void DialogPatternXmlEdit::RemoveChangeStackElement(ChangesStackElement* elmt) delete elmt; return; } - while (index->next!=nullptr) { + while (index->next!=nullptr) + { if (index->next == elmt) { break; @@ -235,6 +242,7 @@ void DialogPatternXmlEdit::RemoveChangeStackElement(ChangesStackElement* elmt) delete elmt; } +//--------------------------------------------------------------------------------------------------------------------- void DialogPatternXmlEdit::ButtonDeleteAttributeClicked() { ChangesStackElement* newstack; @@ -262,6 +270,7 @@ void DialogPatternXmlEdit::ButtonDeleteAttributeClicked() this->ClearEditData(); } +//--------------------------------------------------------------------------------------------------------------------- void DialogPatternXmlEdit::ButtonAddSonClicked() { ChangesStackElement* newstack; @@ -295,12 +304,9 @@ void DialogPatternXmlEdit::ButtonAddSonClicked() // create element tree but do not add attribute in DOM (will be done in apply). QDomNode empty; - VXMLTreeElement* tElement = new VXMLTreeElement( - name, - VXMLTreeElement::TypeNode, - empty,true); + VXMLTreeElement* tElement = new VXMLTreeElement(name, VXMLTreeElement::TypeNode, empty, true); - if (!value.isEmpty()) + if (value.isEmpty() == false) { tElement->setTreeNodeValue(value); } @@ -323,6 +329,7 @@ void DialogPatternXmlEdit::ButtonAddSonClicked() ui->pushButton_Apply_Changes->setEnabled(true); } +//--------------------------------------------------------------------------------------------------------------------- void DialogPatternXmlEdit::ButtonAddAttributeClicked() { ChangesStackElement* newstack; @@ -355,10 +362,7 @@ void DialogPatternXmlEdit::ButtonAddAttributeClicked() } // create element tree but do not add attribute in DOM (will be done in apply). - VXMLTreeElement* tElement = new VXMLTreeElement( - name, - VXMLTreeElement::TypeAttr, - empty,true); + VXMLTreeElement* tElement = new VXMLTreeElement(name, VXMLTreeElement::TypeAttr, empty, true); tElement->setTreeNodeValue(value); currentNodeEdited->appendRow(tElement); @@ -377,16 +381,20 @@ void DialogPatternXmlEdit::ButtonAddAttributeClicked() ui->pushButton_Apply_Changes->setEnabled(true); } +//--------------------------------------------------------------------------------------------------------------------- void DialogPatternXmlEdit::ButtonSetClicked() { - QString name,value; + QString name, value; ChangesStackElement* newstack; // TODO : check if a change has already been done - if (this->currentNodeEdited == nullptr) return; + if (this->currentNodeEdited == nullptr) + { + return; + } name=ui->lineEdit_Name->text(); value=ui->lineEdit_Value->text(); - bool nameedit,valueedit; + bool nameedit, valueedit; nameedit = (name != currentNodeEdited->gettreeNodeName()) ? true : false; valueedit= (value != currentNodeEdited->gettreeNodeValue()) ? true : false; @@ -418,6 +426,7 @@ void DialogPatternXmlEdit::ButtonSetClicked() } } +//--------------------------------------------------------------------------------------------------------------------- void DialogPatternXmlEdit::NameTextEdited(QString newtext) { //QMessageBox::information(this, "nameTextEdited", QString("%1").arg(newtext)); @@ -425,6 +434,7 @@ void DialogPatternXmlEdit::NameTextEdited(QString newtext) ui->pushButton_Cancel_Values->setEnabled(true); } +//--------------------------------------------------------------------------------------------------------------------- void DialogPatternXmlEdit::ValueTextEdited(QString newtext) { //QMessageBox::information(this, "valueTextEdited", QString("%1").arg(newtext)); @@ -432,6 +442,7 @@ void DialogPatternXmlEdit::ValueTextEdited(QString newtext) ui->pushButton_Cancel_Values->setEnabled(true); } +//--------------------------------------------------------------------------------------------------------------------- void DialogPatternXmlEdit::ClearEditData() { ui->lineEdit_Name->setText(""); @@ -447,6 +458,7 @@ void DialogPatternXmlEdit::ClearEditData() ui->pushButton_Cancel_Values->setEnabled(false); } +//--------------------------------------------------------------------------------------------------------------------- void DialogPatternXmlEdit::ClearStack() { ChangesStackElement * tmp; @@ -467,6 +479,7 @@ void DialogPatternXmlEdit::ClearStack() changeStackLast=nullptr; } +//--------------------------------------------------------------------------------------------------------------------- void DialogPatternXmlEdit::BaseSelectionChanged(int value) { QDomNode newbase; @@ -497,7 +510,7 @@ void DialogPatternXmlEdit::BaseSelectionChanged(int value) rootBases[index]->getDocNode(), false); rootNode->appendRow(standard_base); - ReadNodes(rootBases[index]->getDocNode(),standard_base,xmlmodel,true); + ReadNodes(rootBases[index]->getDocNode(), standard_base, xmlmodel, true); ui->treeView_main->setModel(xmlmodel); this->ClearEditData(); @@ -506,6 +519,7 @@ void DialogPatternXmlEdit::BaseSelectionChanged(int value) return; } +//--------------------------------------------------------------------------------------------------------------------- void DialogPatternXmlEdit::ElementClicked ( const QModelIndex & index ) { @@ -513,7 +527,7 @@ void DialogPatternXmlEdit::ElementClicked ( const QModelIndex & index ) VXMLTreeElement *item = (VXMLTreeElement *)index.internalPointer(); // Get child specified by index row/column - VXMLTreeElement * item2 = (VXMLTreeElement *) item->child(index.row(),index.column()); + VXMLTreeElement * item2 = (VXMLTreeElement *) item->child(index.row(), index.column()); // Clear all data and disable buttons this->ClearEditData(); @@ -550,7 +564,7 @@ void DialogPatternXmlEdit::ElementClicked ( const QModelIndex & index ) ui->label_type_value->setText(tr("Root node"));; ui->pushButton_Add_son->setEnabled(true); } - else if (item2->getelementType() == VXMLTreeElement::TypeNode) + else if (item2->getelementType() == VXMLTreeElement::TypeNode) { ui->label_type_value->setText(tr("Node")); ui->pushButton_Add_attribute->setEnabled(true); @@ -563,6 +577,7 @@ void DialogPatternXmlEdit::ElementClicked ( const QModelIndex & index ) } } +//--------------------------------------------------------------------------------------------------------------------- DialogPatternXmlEdit::~DialogPatternXmlEdit() { //xmlmodel->clearTree(); @@ -576,11 +591,12 @@ DialogPatternXmlEdit::~DialogPatternXmlEdit() } } -void DialogPatternXmlEdit::ReadNodes(QDomNode dNode, VXMLTreeElement* root,VXMLTreeView* xmlmodel, bool refresh) +//--------------------------------------------------------------------------------------------------------------------- +void DialogPatternXmlEdit::ReadNodes(QDomNode dNode, VXMLTreeElement* root, VXMLTreeView* xmlmodel, bool refresh) { - QDomNode tNode,tNode2; - VXMLTreeElement* tElement,*tElement2; + QDomNode tNode, tNode2; + VXMLTreeElement* tElement, *tElement2; int totalOfChilds = dNode.childNodes().size(); if (totalOfChilds == 0) { @@ -591,7 +607,7 @@ void DialogPatternXmlEdit::ReadNodes(QDomNode dNode, VXMLTreeElement* root,VXMLT tNode = dNode.childNodes().at(i); if (tNode.nodeType() != QDomNode::CommentNode) { - tElement = new VXMLTreeElement(tNode.nodeName(),VXMLTreeElement::TypeNode,tNode,false); + tElement = new VXMLTreeElement(tNode.nodeName(), VXMLTreeElement::TypeNode, tNode, false); xmlmodel->appendchain(tElement); root->appendRow(tElement); if (tNode.nodeName() == "draw") @@ -605,7 +621,7 @@ void DialogPatternXmlEdit::ReadNodes(QDomNode dNode, VXMLTreeElement* root,VXMLT QVariant(rootBasesNum)); rootBases[rootBasesNum]=new VXMLTreeElement( QString("Drawing %1").arg(tNode.attributes().item(i).nodeValue()), - VXMLTreeElement::TypeRoot,tNode,false);; + VXMLTreeElement::TypeRoot, tNode, false); rootBasesNum++; //QStandardItem* rootNode = (QStandardItem*) rootBases[rootBasesNum]->invisibleRootItem(); //rootNode->appendRow(tElement); @@ -627,7 +643,7 @@ void DialogPatternXmlEdit::ReadNodes(QDomNode dNode, VXMLTreeElement* root,VXMLT tNode2 = tNode.childNodes().at(i); if (tNode2.nodeType() != QDomNode::CommentNode) { - tElement2 = new VXMLTreeElement(tNode2.nodeName(),VXMLTreeElement::TypeNode,tNode2,false); + tElement2 = new VXMLTreeElement(tNode2.nodeName(), VXMLTreeElement::TypeNode, tNode2, false); xmlmodel->appendchain(tElement2); tElement->appendRow(tElement2); } @@ -640,10 +656,8 @@ void DialogPatternXmlEdit::ReadNodes(QDomNode dNode, VXMLTreeElement* root,VXMLT for (int i = 0; i < tNode.attributes().size(); i++) { - tElement2 = new VXMLTreeElement( - tNode.attributes().item(i).nodeName(), - VXMLTreeElement::TypeAttr, - tNode,false); + tElement2 = new VXMLTreeElement(tNode.attributes().item(i).nodeName(), VXMLTreeElement::TypeAttr, + tNode, false); tElement2->setTreeNodeValue(tNode.attributes().item(i).nodeValue()); tElement->appendRow(tElement2); xmlmodel->appendchain(tElement2); @@ -655,13 +669,15 @@ void DialogPatternXmlEdit::ReadNodes(QDomNode dNode, VXMLTreeElement* root,VXMLT return; } -// vXMLTreeView Code ----------------------------------------------------------------- +//--------------------------------------------------------------------------------------------------------------------- VXMLTreeView::VXMLTreeView(QObject *parent) : QStandardItemModel(parent) { current=last=items=nullptr; } -void VXMLTreeView::ClearTree() { +//--------------------------------------------------------------------------------------------------------------------- +void VXMLTreeView::ClearTree() +{ // TODO check if treeitems are deleted with the delete row command of QDom @@ -675,14 +691,13 @@ void VXMLTreeView::ClearTree() { } // reset chain counters current=last=items=nullptr; - } +//--------------------------------------------------------------------------------------------------------------------- VXMLTreeView::~VXMLTreeView() -{ - -} +{} +//--------------------------------------------------------------------------------------------------------------------- void VXMLTreeView::appendchain(VXMLTreeElement* elmt) { if (last == nullptr) @@ -710,43 +725,40 @@ void VXMLTreeView::appendchain(VXMLTreeElement* elmt) last=temp; } - -// vXMLTreeElement code ----------------------------------------------------------------- - const short int VXMLTreeElement::TypeNode=1; const short int VXMLTreeElement::TypeAttr=2; const short int VXMLTreeElement::TypeRoot=3; - -VXMLTreeElement::VXMLTreeElement(QString name, int nodetype, QDomNode source,bool editor): QStandardItem(name) +//--------------------------------------------------------------------------------------------------------------------- +VXMLTreeElement::VXMLTreeElement(QString name, int nodetype, QDomNode source, bool editor) + : QStandardItem(name) { this->elementType=nodetype; this->DocNode=source; this->addedNode=editor; - if (!editor) + if (editor == false) { this->DocNode=source; } this->treeNodeName=name; this->treeNodeValue=""; // TODO : translation ? this->treeNodeValueSet=false, - this->setText(this->displayText()); + this->setText(this->displayText()); switch (this->elementType) { - case VXMLTreeElement::TypeAttr: - this->setBackground(BACKGROUND_COLOR_ATTRIBUTE); - break; - default: - break; + case VXMLTreeElement::TypeAttr: + this->setBackground(BACKGROUND_COLOR_ATTRIBUTE); + break; + default: + break; } } - +//--------------------------------------------------------------------------------------------------------------------- VXMLTreeElement::~VXMLTreeElement() -{ - -} +{} +//--------------------------------------------------------------------------------------------------------------------- void VXMLTreeElement::setTreeNodeValue(QString value) { this->treeNodeValue=value; @@ -754,24 +766,33 @@ void VXMLTreeElement::setTreeNodeValue(QString value) this->setText(this->displayText()); } +//--------------------------------------------------------------------------------------------------------------------- void VXMLTreeElement::setTreeNodeName(QString value) { this->treeNodeName=value; this->setText(this->displayText()); } +//--------------------------------------------------------------------------------------------------------------------- QString VXMLTreeElement::displayText() { if (this->elementType == VXMLTreeElement::TypeRoot) + { return this->treeNodeName; + } return QString("%1 : %2").arg((this->treeNodeName)).arg(this->treeNodeValue); } -QString VXMLTreeElement::gettreeNodeValue() { +//--------------------------------------------------------------------------------------------------------------------- +QString VXMLTreeElement::gettreeNodeValue() +{ // Only return value if it's really set. if (this->treeNodeValueSet) + { return this->treeNodeValue; + } else - return ""; + { + return QString(); + } } - diff --git a/src/app/dialogs/app/dialogpatternxmledit.h b/src/app/dialogs/app/dialogpatternxmledit.h index 6f16892cf..8c5cac0a9 100644 --- a/src/app/dialogs/app/dialogpatternxmledit.h +++ b/src/app/dialogs/app/dialogpatternxmledit.h @@ -37,21 +37,20 @@ /** * @brief The vXMLTreeElement class : node/attribute of xml pattern. Used by vXMLTreeView * - ********************************************************************************************** */ #define BACKGROUND_COLOR_ATTRIBUTE QBrush(Qt::GlobalColor::cyan) class VXMLTreeElement : public QStandardItem { -public: +public: /** * @brief vXMLTreeElement * @param name : display name * @param nodetype : node type (node, attribute, root) * @param source : the source dom node */ - explicit VXMLTreeElement (QString name, int nodetype, QDomNode source,bool editor); + explicit VXMLTreeElement (QString name, int nodetype, QDomNode source, bool editor); explicit VXMLTreeElement (QString name, int nodetype); @@ -146,7 +145,8 @@ public: /** * @brief The TreeElement struct : chained list of vXMLTreeElement */ - typedef struct TreeElementchain { + typedef struct TreeElementchain + { VXMLTreeElement* elmt; TreeElementchain* next; } TreeElementchain; @@ -197,8 +197,9 @@ inline VXMLTreeView::TreeElementchain * VXMLTreeView::GetItems() //******************************************************************************************** -namespace Ui { -class DialogPatternXmlEdit; +namespace Ui +{ + class DialogPatternXmlEdit; } /** * @brief The DialogPatternXmlEdit class : ui dialog for XML editing of pattern @@ -223,11 +224,12 @@ public: void ButtonDeleteAttributeClicked(); void ButtonAddSonClicked(); void ButtonAddAttributeClicked(); - void ButtonApplyChangesClicked() ; + void ButtonApplyChangesClicked(); // Stack of changes definition - typedef struct ChangesStackElement { + typedef struct ChangesStackElement + { short int type; VXMLTreeElement *element; QString *newText; @@ -277,6 +279,4 @@ private: ChangesStackElement* changeStackLast; }; - - #endif // DIALOGPATTERNXMLEDIT_H diff --git a/src/app/mainwindow.cpp b/src/app/mainwindow.cpp index ef7d7b51d..93d18d6bc 100644 --- a/src/app/mainwindow.cpp +++ b/src/app/mainwindow.cpp @@ -503,7 +503,7 @@ void MainWindow::ClosedDialogCutArc(int result) void MainWindow::About() { DialogAboutApp * about_dialog = new DialogAboutApp(this); - about_dialog->setAttribute(Qt::WA_DeleteOnClose,true); + about_dialog->setAttribute(Qt::WA_DeleteOnClose, true); about_dialog->show(); } @@ -546,8 +546,8 @@ void MainWindow::PatternProperties() //--------------------------------------------------------------------------------------------------------------------- void MainWindow::EditPatternCode() { - DialogPatternXmlEdit *Pattern = new DialogPatternXmlEdit (this,doc); - Pattern->setAttribute(Qt::WA_DeleteOnClose,true); + DialogPatternXmlEdit *Pattern = new DialogPatternXmlEdit (this, doc); + Pattern->setAttribute(Qt::WA_DeleteOnClose, true); Pattern->show(); } @@ -1367,7 +1367,7 @@ void MainWindow::CreateActions() connect(ui->actionOptions, &QAction::triggered, this, &MainWindow::Options); connect(ui->actionPattern_properties, &QAction::triggered, this, &MainWindow::PatternProperties); ui->actionPattern_properties->setEnabled(false); - connect(ui->actionEdit_pattern_code,&QAction::triggered,this,&MainWindow::EditPatternCode); + connect(ui->actionEdit_pattern_code, &QAction::triggered, this, &MainWindow::EditPatternCode); //ui->actionEdit_pattern_code->setEnabled(false); //Actions for recent files loaded by a main window application. diff --git a/src/app/widgets/vapplication.cpp b/src/app/widgets/vapplication.cpp index 43de0a2d7..51160358a 100644 --- a/src/app/widgets/vapplication.cpp +++ b/src/app/widgets/vapplication.cpp @@ -148,7 +148,7 @@ QString VApplication::pathToTables() const return QApplication::applicationDirPath() + QStringLiteral("/tables/individual"); #else QDir dir(QApplication::applicationDirPath() + QStringLiteral("/tables/individual")); - if(dir.exist()) + if (dir.exist()) { return dir.absolutePath(); } @@ -168,7 +168,7 @@ QString VApplication::pathToTables() const return QApplication::applicationDirPath() + QStringLiteral("/tables/standard"); #else QDir dir(QApplication::applicationDirPath() + QStringLiteral("/tables/standard")); - if(dir.exist()) + if (dir.exist()) { return dir.absolutePath(); } @@ -191,7 +191,7 @@ QString VApplication::translationsPath() const return QApplication::applicationDirPath() + QStringLiteral("/translations"); #else QDir dir(QApplication::applicationDirPath() + QStringLiteral("/translations")); - if(dir.exist()) + if (dir.exist()) { return dir.absolutePath(); } diff --git a/src/libs/qmuparser/qmuparser.h b/src/libs/qmuparser/qmuparser.h index 50892dd99..db2a4c8a7 100644 --- a/src/libs/qmuparser/qmuparser.h +++ b/src/libs/qmuparser/qmuparser.h @@ -80,7 +80,7 @@ namespace qmu static qreal Avg(const qreal*, int); // mean value static qreal Min(const qreal*, int); // minimum static qreal Max(const qreal*, int); // maximum -}; + }; //--------------------------------------------------------------------------------------------------------------------- /** From e1c3f418e5d4af7a9e89192b7476df2f2d7d2d8e Mon Sep 17 00:00:00 2001 From: dismine Date: Fri, 16 May 2014 20:24:51 +0300 Subject: [PATCH 07/15] GCC warnings. --HG-- branch : XML Editor --- src/app/dialogs/app/dialogpatternxmledit.cpp | 32 ++++++++++---------- src/app/dialogs/app/dialogpatternxmledit.h | 1 - 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/app/dialogs/app/dialogpatternxmledit.cpp b/src/app/dialogs/app/dialogpatternxmledit.cpp index f107f4c9a..0ed508211 100644 --- a/src/app/dialogs/app/dialogpatternxmledit.cpp +++ b/src/app/dialogs/app/dialogpatternxmledit.cpp @@ -36,14 +36,14 @@ const short int DialogPatternXmlEdit::ChangeTypeModify=3; //--------------------------------------------------------------------------------------------------------------------- DialogPatternXmlEdit::DialogPatternXmlEdit(QWidget *parent, VPattern *xmldoc) - :QDialog(parent), ui(new Ui::DialogPatternXmlEdit) + :QDialog(parent), treeChange(false), currentNodeEdited(nullptr), currentNodeEditedStatus(0), + currentNodeEditedStack(nullptr), ui(new Ui::DialogPatternXmlEdit), doc(nullptr), root(QDomElement()), + rootNode(nullptr), rootBases(nullptr), rootBasesNum(0), xmlmodel(nullptr), changeStackRoot(nullptr), + changeStackLast(nullptr) { ui->setupUi(this); xmlmodel = new VXMLTreeView(); - currentNodeEdited=nullptr; - treeChange=false; - this->changeStackRoot = this->changeStackLast = nullptr; doc=xmldoc; root = doc->documentElement(); @@ -53,9 +53,10 @@ DialogPatternXmlEdit::DialogPatternXmlEdit(QWidget *parent, VPattern *xmldoc) ui->treeView_main->setWindowTitle("XML"); - + //TODO warning: use of old-style cast [-Wold-style-cast] rootNode = (QStandardItem*) xmlmodel->invisibleRootItem(); + //TODO warning: conversion to 'qint16 {aka short int}' from 'int' may alter its value [-Wconversion] qint16 drawnum=xmldoc->elementsByTagName("draw").size(); rootBases = new VXMLTreeElement*[drawnum+1]; rootBasesNum=1; @@ -524,9 +525,11 @@ void DialogPatternXmlEdit::ElementClicked ( const QModelIndex & index ) { // Get item (function returns parent of clicked item) + //TODO warning: use of old-style cast [-Wold-style-cast] VXMLTreeElement *item = (VXMLTreeElement *)index.internalPointer(); // Get child specified by index row/column + //TODO warning: use of old-style cast [-Wold-style-cast] VXMLTreeElement * item2 = (VXMLTreeElement *) item->child(index.row(), index.column()); // Clear all data and disable buttons @@ -670,10 +673,9 @@ void DialogPatternXmlEdit::ReadNodes(QDomNode dNode, VXMLTreeElement* root, VXML } //--------------------------------------------------------------------------------------------------------------------- -VXMLTreeView::VXMLTreeView(QObject *parent) : QStandardItemModel(parent) -{ - current=last=items=nullptr; -} +VXMLTreeView::VXMLTreeView(QObject *parent) + : QStandardItemModel(parent), items(nullptr), current(nullptr), last(nullptr) +{} //--------------------------------------------------------------------------------------------------------------------- void VXMLTreeView::ClearTree() @@ -730,19 +732,17 @@ const short int VXMLTreeElement::TypeAttr=2; const short int VXMLTreeElement::TypeRoot=3; //--------------------------------------------------------------------------------------------------------------------- +//TODO warning: conversion to 'short int' from 'int' may alter its value [-Wconversion] +//treeNodeValueSet(false), treeNodeName(name) +// ^ VXMLTreeElement::VXMLTreeElement(QString name, int nodetype, QDomNode source, bool editor) - : QStandardItem(name) + : QStandardItem(name), DocNode(source), addedNode(editor), elementType(nodetype), treeNodeValue(""), + treeNodeValueSet(false), treeNodeName(name) { - this->elementType=nodetype; - this->DocNode=source; - this->addedNode=editor; if (editor == false) { this->DocNode=source; } - this->treeNodeName=name; - this->treeNodeValue=""; // TODO : translation ? - this->treeNodeValueSet=false, this->setText(this->displayText()); switch (this->elementType) { diff --git a/src/app/dialogs/app/dialogpatternxmledit.h b/src/app/dialogs/app/dialogpatternxmledit.h index 8c5cac0a9..b5ad38d42 100644 --- a/src/app/dialogs/app/dialogpatternxmledit.h +++ b/src/app/dialogs/app/dialogpatternxmledit.h @@ -175,7 +175,6 @@ private: * @brief last : used to parse in iteration. */ TreeElementchain * last; - }; inline VXMLTreeView::TreeElementchain * VXMLTreeView::getCurrent() From 99818c3d285fbd309e99ec7368be642f75cc01c2 Mon Sep 17 00:00:00 2001 From: Patrick Proy Date: Sun, 18 May 2014 21:42:36 +0200 Subject: [PATCH 08/15] Added node delete Added undo change button --HG-- branch : XML Editor --- src/app/dialogs/app/dialogpatternxmledit.cpp | 473 +++++++++++++++---- src/app/dialogs/app/dialogpatternxmledit.h | 59 ++- src/app/dialogs/app/dialogpatternxmledit.ui | 6 +- 3 files changed, 441 insertions(+), 97 deletions(-) diff --git a/src/app/dialogs/app/dialogpatternxmledit.cpp b/src/app/dialogs/app/dialogpatternxmledit.cpp index 0ed508211..764441b9e 100644 --- a/src/app/dialogs/app/dialogpatternxmledit.cpp +++ b/src/app/dialogs/app/dialogpatternxmledit.cpp @@ -53,11 +53,9 @@ DialogPatternXmlEdit::DialogPatternXmlEdit(QWidget *parent, VPattern *xmldoc) ui->treeView_main->setWindowTitle("XML"); - //TODO warning: use of old-style cast [-Wold-style-cast] - rootNode = (QStandardItem*) xmlmodel->invisibleRootItem(); + rootNode = xmlmodel->invisibleRootItem(); - //TODO warning: conversion to 'qint16 {aka short int}' from 'int' may alter its value [-Wconversion] - qint16 drawnum=xmldoc->elementsByTagName("draw").size(); + int drawnum=xmldoc->elementsByTagName("draw").size(); rootBases = new VXMLTreeElement*[drawnum+1]; rootBasesNum=1; rootBases[0]=new VXMLTreeElement("Valentina", VXMLTreeElement::TypeRoot, root, false); @@ -87,10 +85,133 @@ DialogPatternXmlEdit::DialogPatternXmlEdit(QWidget *parent, VPattern *xmldoc) connect(ui->pushButton_Remove_attribute, &QPushButton::clicked, this, &DialogPatternXmlEdit::ButtonDeleteAttributeClicked); 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); + connect(ui->pushButton_Add_attribute, &QPushButton::clicked, + this, &DialogPatternXmlEdit::ButtonAddAttributeClicked); + connect(ui->pushButton_Apply_Changes, &QPushButton::clicked, + this, &DialogPatternXmlEdit::ButtonApplyChangesClicked); + connect(ui->pushButton_Undo_Last_Change, &QPushButton::clicked, this, &DialogPatternXmlEdit::ButtonUndoLastChange); + connect(ui->pushButton_Remove_Node, &QPushButton::clicked, this, &DialogPatternXmlEdit::ButtonDeleteNode); +} + +//--------------------------------------------------------------------------------------------------------------------- +void DialogPatternXmlEdit::ButtonDeleteNode() +{ + ChangesStackElement* newstack; + + // TODO : maybe assert functions here + if (this->currentNodeEdited == nullptr) + { + return; + } + if (this->currentNodeEdited->GetelementType() != VXMLTreeElement::TypeNode) + { + return; + } + + if (this->currentNodeEditedStatus != 0) + { + if (this->currentNodeEditedStatus == DialogPatternXmlEdit::ChangeTypeDelete) + { // Change already done ??? ignore... + return; + } + if (this->currentNodeEditedStatus == DialogPatternXmlEdit::ChangeTypeAdd) + { // Attribute was previously added, just undo the change + if ( this->UndoChange(this->currentNodeEditedStack) == false ) + { // Error in undo... warn user + QMessageBox::warning(this, tr("No changes"), tr("Cannot delete previously created node")); + return; + } + this->ClearEditData(); + return; + } + if (this->currentNodeEditedStatus == DialogPatternXmlEdit::ChangeTypeModify) + { // Node was edited : remove change then continue to delete node + this->UndoChange(this->currentNodeEditedStack); + } + } + + // Modify display + QFont textfont=currentNodeEdited->font(); + textfont.setStrikeOut(true); + this->currentNodeEdited->setFont(textfont); + // + this->DeleteNodeAndSons(this->currentNodeEdited, true); + // Create change + newstack = this->CreateStackElement(DialogPatternXmlEdit::ChangeTypeDelete); + newstack->element= this->currentNodeEdited; + // Activate changes + this->treeChange=true; + ui->pushButton_Apply_Changes->setEnabled(true); + ui->pushButton_Undo_Last_Change->setEnabled(true); + // clear dialog + this->ClearEditData(); + return; +} + +//--------------------------------------------------------------------------------------------------------------------- +bool DialogPatternXmlEdit::DeleteNodeAndSons(VXMLTreeElement * currentNode, bool onlydeactivate) +{ + + QList stack; + VXMLTreeElement * currentNodeStack; + stack << currentNode; + while (stack.isEmpty() == false) + { + //pop first element + currentNodeStack=stack.takeFirst(); + if (onlydeactivate==true) + { + currentNodeStack->SetSetlectable(false); + QFont textfont=currentNodeStack->font(); + textfont.setStrikeOut(true); + currentNodeStack->setFont(textfont); + } + // clear all changes made in son node and not on current or it will loop + if (currentNodeStack != currentNode) + { + ChangesStackElement * local = this->changeStackRoot; + while (local != nullptr) + { + if (local->element == currentNodeStack) + { + this->UndoChange(local); + break; + } + local=local->next; + } + } + // add sons to stack + int index=currentNodeStack->rowCount(); + while (index > 0) + { + stack << static_cast (currentNodeStack->child(index-1)); + index--; + } + //stack.removeFirst(); + } + currentNode->SetSetlectable(true); + + if (onlydeactivate==false) + { // Remove node from parent + // Get parent node + QStandardItem * parent= currentNode->parent(); + int index=parent->rowCount(); + while (index >= 0) + { + if (static_cast (parent->child(index)) == currentNode) + { + break; + } + index--; + } + if (index < 0) + { + Q_ASSERT(index==0); + return false; + } + parent->removeRow(index); + } + return true; } //--------------------------------------------------------------------------------------------------------------------- @@ -113,14 +234,136 @@ DialogPatternXmlEdit::ChangesStackElement* DialogPatternXmlEdit::CreateStackElem changeStackLast=changeStackLast->next; } changeStackLast->newValue=changeStackLast->newText=nullptr; + changeStackLast->oldValue=changeStackLast->oldText=nullptr; changeStackLast->next=nullptr; return changeStackLast; } +//--------------------------------------------------------------------------------------------------------------------- +bool DialogPatternXmlEdit::UndoChange(DialogPatternXmlEdit::ChangesStackElement* current) +{ + Q_ASSERT(current != nullptr); + + VXMLTreeElement * currentNode = current->element; + + if (current->type == DialogPatternXmlEdit::ChangeTypeDelete) + { + if (currentNode->GetelementType()==VXMLTreeElement::TypeAttr) + { + // Attribute previously deleted -> restore + QFont textfont=currentNode->font(); + textfont.setStrikeOut(false); + currentNode->setFont(textfont); + this->RemoveChangeStackElement(current); + } + else if (currentNode->GetelementType()==VXMLTreeElement::TypeNode) + { + QList stack; + stack << currentNode; + while (stack.isEmpty() == false) + { + currentNode=stack.takeFirst(); + currentNode->SetSetlectable(true); + QFont textfont=currentNode->font(); + textfont.setStrikeOut(false); + currentNode->setFont(textfont); + + int index=currentNode->rowCount(); + while (index > 0) + { + stack << static_cast (currentNode->child(index-1)); + index--; + } + } + this->RemoveChangeStackElement(current); + } + } + else if (current->type == DialogPatternXmlEdit::ChangeTypeAdd) + { + if (currentNode->GetelementType()==VXMLTreeElement::TypeAttr) + { + // Get parent node + VXMLTreeElement * parent= static_cast (currentNode->parent()); + int index=parent->rowCount(); + while (index >= 0) + { + if (static_cast (parent->child(index)) == currentNode) + { + break; + } + index--; + } + if (index < 0) + { + Q_ASSERT(index==0); + return false; + } + parent->removeRow(index); + this->RemoveChangeStackElement(current); + } + else if (currentNode->GetelementType()==VXMLTreeElement::TypeNode) + { + if (this->DeleteNodeAndSons(currentNode, false) == false) + { + // TODO : error message. + return false; + } + this->RemoveChangeStackElement(current); + } + } + else if (current->type == DialogPatternXmlEdit::ChangeTypeModify) + { + if (current->changedText == true) + { + currentNode->SetTreeNodeName(*current->oldText); + } + if (current->changedValue == true) + { + currentNode->SetTreeNodeValue(*current->oldValue); + } + + QFont textfont=currentNode->font(); + textfont.setBold(false); + currentNode->setFont(textfont); + this->RemoveChangeStackElement(current); + } + // Check if last change was undone + if (this->changeStackLast == nullptr) + { + this->treeChange=false; + ui->pushButton_Apply_Changes->setEnabled(false); + ui->pushButton_Undo_Last_Change->setEnabled(false); + } + return true; +} + + +//--------------------------------------------------------------------------------------------------------------------- +void DialogPatternXmlEdit::ButtonUndoLastChange() +{ + ChangesStackElement* current=this->changeStackLast; + + if (current == nullptr) + { + QMessageBox::warning(this, tr("No changes"), tr("No changes left")); + this->ui->pushButton_Undo_Last_Change->setEnabled(false); + return; + } + if (this->UndoChange(current) == false) + { + QMessageBox::warning(this, tr("No changes"), tr("Cannot undo change")); + } + this->ClearEditData(); + +} + //--------------------------------------------------------------------------------------------------------------------- void DialogPatternXmlEdit::ButtonApplyChangesClicked() { QString Changes=""; + QStringList stringtype; + stringtype << "node" << "attribute" << "root"; + QString nodetype; ChangesStackElement* currentChange; if (this->changeStackRoot==nullptr) { @@ -131,20 +374,21 @@ void DialogPatternXmlEdit::ButtonApplyChangesClicked() currentChange=this->changeStackRoot; do { + nodetype=stringtype.at(currentChange->element->GetelementType()-1); if (currentChange->type == DialogPatternXmlEdit::ChangeTypeAdd) { - Changes += QString("Added type %1 : ").arg(currentChange->element->getelementType()); - Changes += currentChange->element->gettreeNodeName(); + Changes += QString("Added type %1 : ").arg(nodetype); + Changes += currentChange->element->GettreeNodeName(); Changes += "/"; - Changes += (currentChange->element->gettreeNodeValueSet()) ? - currentChange->element->gettreeNodeValue(): QString(tr("")); + Changes += (currentChange->element->GettreeNodeValueSet()) ? + currentChange->element->GettreeNodeValue(): QString(tr("")); Changes += "\n"; } else { if (currentChange->type == DialogPatternXmlEdit::ChangeTypeModify) { - Changes += QString("Modified type %1 : ").arg(currentChange->element->getelementType()); + Changes += QString("Modified type %1 : ").arg(nodetype); Changes += (currentChange->changedText) ? *currentChange->newText : QString(tr("Unchanged")); Changes += "/"; Changes += (currentChange->changedValue) ? *currentChange->newValue: QString(tr("Unchanged")); @@ -154,11 +398,11 @@ void DialogPatternXmlEdit::ButtonApplyChangesClicked() { if (currentChange->type == DialogPatternXmlEdit::ChangeTypeDelete) { - Changes += QString("Deleted type %1 : ").arg(currentChange->element->getelementType()); - Changes += currentChange->element->gettreeNodeName(); + Changes += QString("Deleted type %1 : ").arg(nodetype); + Changes += currentChange->element->GettreeNodeName(); Changes += "/"; - Changes += (currentChange->element->gettreeNodeValueSet()) ? - currentChange->element->gettreeNodeValue(): QString(tr("")); + Changes += (currentChange->element->GettreeNodeValueSet()) ? + currentChange->element->GettreeNodeValue(): QString(tr("")); Changes += "\n"; } } @@ -176,17 +420,9 @@ void DialogPatternXmlEdit::ButtonCancelClicked() this->ClearEditData(); if (this->currentNodeEditedStatus==DialogPatternXmlEdit::ChangeTypeDelete) { - if (this->currentNodeEdited->getelementType() == VXMLTreeElement::TypeAttr) + if (this->UndoChange(this->currentNodeEditedStack) == false) { - // Attribute previously deleted -> resore - QFont textfont=currentNodeEdited->font(); - textfont.setStrikeOut(false); - this->currentNodeEdited->setFont(textfont); - this->RemoveChangeStackElement(this->currentNodeEditedStack); - } - else - { - QMessageBox::information(this, "Not Implemented", "Not Implemented"); + QMessageBox::warning(this, "Error", "Cannot undo"); } } } @@ -206,6 +442,14 @@ void DialogPatternXmlEdit::RemoveChangeStackElement(ChangesStackElement* elmt) { delete elmt->newValue; } + if (elmt->oldText != nullptr) + { + delete elmt->oldText; + } + if (elmt->oldValue != nullptr) + { + delete elmt->oldValue; + } this->changeStackRoot = elmt->next; if (this->changeStackLast == elmt) { @@ -224,7 +468,7 @@ void DialogPatternXmlEdit::RemoveChangeStackElement(ChangesStackElement* elmt) } if (index->next == nullptr) { - // TODO : debug error here + Q_ASSERT(index->next != nullptr); return; } if (index->next->newText != nullptr) @@ -235,6 +479,14 @@ void DialogPatternXmlEdit::RemoveChangeStackElement(ChangesStackElement* elmt) { delete index->next->newValue; } + if (index->next->oldText != nullptr) + { + delete index->next->oldText; + } + if (index->next->oldValue != nullptr) + { + delete index->next->oldValue; + } index->next=index->next->next; if (this->changeStackLast == elmt) { @@ -253,11 +505,32 @@ void DialogPatternXmlEdit::ButtonDeleteAttributeClicked() { return; } - if (this->currentNodeEdited->getelementType() != VXMLTreeElement::TypeAttr) + if (this->currentNodeEdited->GetelementType() != VXMLTreeElement::TypeAttr) { return; } + if (this->currentNodeEditedStatus != 0) + { + if (this->currentNodeEditedStatus == DialogPatternXmlEdit::ChangeTypeDelete) + { // Change already done ??? ignore... + return; + } + if (this->currentNodeEditedStatus == DialogPatternXmlEdit::ChangeTypeAdd) + { // Attribute was previously added, just undo the change + if ( this->UndoChange(this->currentNodeEditedStack) == false ) + { // Error in undo... warn user + QMessageBox::warning(this, tr("No changes"), tr("Cannot delete previously created attribute")); + return; + } + this->ClearEditData(); + return; + } + if (this->currentNodeEditedStatus == DialogPatternXmlEdit::ChangeTypeModify) + { // Node was edited : remove change then continue to delete node + this->UndoChange(this->currentNodeEditedStack); + } + } newstack = this->CreateStackElement(DialogPatternXmlEdit::ChangeTypeDelete); newstack->element= this->currentNodeEdited; @@ -267,6 +540,7 @@ void DialogPatternXmlEdit::ButtonDeleteAttributeClicked() // Activate changes this->treeChange=true; ui->pushButton_Apply_Changes->setEnabled(true); + ui->pushButton_Undo_Last_Change->setEnabled(true); // clear dialog this->ClearEditData(); } @@ -282,7 +556,7 @@ void DialogPatternXmlEdit::ButtonAddSonClicked() { return; } - if (this->currentNodeEdited->getelementType() != VXMLTreeElement::TypeNode) + if (this->currentNodeEdited->GetelementType() != VXMLTreeElement::TypeNode) { return; } @@ -309,7 +583,7 @@ void DialogPatternXmlEdit::ButtonAddSonClicked() if (value.isEmpty() == false) { - tElement->setTreeNodeValue(value); + tElement->SetTreeNodeValue(value); } currentNodeEdited->appendRow(tElement); xmlmodel->appendchain(tElement); @@ -328,6 +602,7 @@ void DialogPatternXmlEdit::ButtonAddSonClicked() // Activate changes treeChange=true; ui->pushButton_Apply_Changes->setEnabled(true); + ui->pushButton_Undo_Last_Change->setEnabled(true); } //--------------------------------------------------------------------------------------------------------------------- @@ -342,7 +617,7 @@ void DialogPatternXmlEdit::ButtonAddAttributeClicked() { return; } - if (this->currentNodeEdited->getelementType() != VXMLTreeElement::TypeNode) + if (this->currentNodeEdited->GetelementType() != VXMLTreeElement::TypeNode) { return; } @@ -365,7 +640,7 @@ void DialogPatternXmlEdit::ButtonAddAttributeClicked() // create element tree but do not add attribute in DOM (will be done in apply). VXMLTreeElement* tElement = new VXMLTreeElement(name, VXMLTreeElement::TypeAttr, empty, true); - tElement->setTreeNodeValue(value); + tElement->SetTreeNodeValue(value); currentNodeEdited->appendRow(tElement); xmlmodel->appendchain(tElement); QFont textfont=currentNodeEdited->font(); @@ -380,6 +655,7 @@ void DialogPatternXmlEdit::ButtonAddAttributeClicked() // Activate changes treeChange=true; ui->pushButton_Apply_Changes->setEnabled(true); + ui->pushButton_Undo_Last_Change->setEnabled(true); } //--------------------------------------------------------------------------------------------------------------------- @@ -388,7 +664,6 @@ void DialogPatternXmlEdit::ButtonSetClicked() QString name, value; ChangesStackElement* newstack; - // TODO : check if a change has already been done if (this->currentNodeEdited == nullptr) { return; @@ -397,32 +672,67 @@ void DialogPatternXmlEdit::ButtonSetClicked() value=ui->lineEdit_Value->text(); bool nameedit, valueedit; - nameedit = (name != currentNodeEdited->gettreeNodeName()) ? true : false; - valueedit= (value != currentNodeEdited->gettreeNodeValue()) ? true : false; + nameedit = (name != currentNodeEdited->GettreeNodeName()) ? true : false; + valueedit= (value != currentNodeEdited->GettreeNodeValue()) ? true : false; if ( nameedit == true || valueedit == true ) { - newstack = this->CreateStackElement(DialogPatternXmlEdit::ChangeTypeModify); - newstack->element= this->currentNodeEdited; - - if (nameedit == true) - { - newstack->changedText=true; - newstack->newText=new QString(name); - currentNodeEdited->setTreeNodeName(name); + if (this->currentNodeEditedStatus != 0) + { // If node was created or edited previously, rewrite + if (this->currentNodeEditedStatus == DialogPatternXmlEdit::ChangeTypeDelete) + { // You shouldn't be able to edit a deleted node... + Q_ASSERT(this->currentNodeEditedStatus != DialogPatternXmlEdit::ChangeTypeDelete); + ClearEditData(); + return; + } + newstack=this->currentNodeEditedStack; + if (nameedit == true) + { + newstack->changedText=true; + *newstack->newText=name; + currentNodeEdited->SetTreeNodeName(name); + } + if (valueedit == true) + { + if (newstack->changedValue==true) + { + *newstack->newValue = value; + } + else + { + newstack->changedValue=true; + newstack->newValue= new QString(value); + newstack->oldValue= new QString(this->currentNodeEdited->GettreeNodeValue()); + } + currentNodeEdited->SetTreeNodeValue(value); + } } - if (valueedit == true) + else { - newstack->changedValue=true; - newstack->newValue= new QString(value); - currentNodeEdited->setTreeNodeValue(value); - } + newstack = this->CreateStackElement(DialogPatternXmlEdit::ChangeTypeModify); + newstack->element= this->currentNodeEdited; + if (nameedit == true) + { + newstack->changedText=true; + newstack->newText=new QString(name); + newstack->oldText=new QString(this->currentNodeEdited->GettreeNodeName()); + currentNodeEdited->SetTreeNodeName(name); + } + if (valueedit == true) + { + newstack->changedValue=true; + newstack->newValue= new QString(value); + newstack->oldValue= new QString(this->currentNodeEdited->GettreeNodeValue()); + currentNodeEdited->SetTreeNodeValue(value); + } + } QFont textfont=currentNodeEdited->font(); textfont.setBold(true); currentNodeEdited->setFont(textfont); treeChange=true; ui->pushButton_Apply_Changes->setEnabled(true); + ui->pushButton_Undo_Last_Change->setEnabled(true); ClearEditData(); } } @@ -457,6 +767,7 @@ void DialogPatternXmlEdit::ClearEditData() ui->pushButton_Add_son->setEnabled(false); ui->pushButton_Remove_attribute->setEnabled(false); ui->pushButton_Cancel_Values->setEnabled(false); + ui->pushButton_Remove_Node->setEnabled(false); } //--------------------------------------------------------------------------------------------------------------------- @@ -490,7 +801,7 @@ void DialogPatternXmlEdit::BaseSelectionChanged(int value) if (this->treeChange == true) {//Changes have been made : warn ? } - ClearStack(); + this->ClearStack(); ui->pushButton_Apply_Changes->setEnabled(false); index = ui->comboBox_Base_Selection->itemData(value).toInt(); //.convert(QVariant::Int); @@ -503,15 +814,15 @@ void DialogPatternXmlEdit::BaseSelectionChanged(int value) { rootNode->removeRow(0); } - xmlmodel->ClearTree(); + this->xmlmodel->ClearTree(); VXMLTreeElement* standard_base = new VXMLTreeElement( - rootBases[index]->gettreeNodeName(), + rootBases[index]->GettreeNodeName(), VXMLTreeElement::TypeRoot, - rootBases[index]->getDocNode(), + rootBases[index]->GetDocNode(), false); rootNode->appendRow(standard_base); - ReadNodes(rootBases[index]->getDocNode(), standard_base, xmlmodel, true); + ReadNodes(rootBases[index]->GetDocNode(), standard_base, xmlmodel, true); ui->treeView_main->setModel(xmlmodel); this->ClearEditData(); @@ -534,10 +845,15 @@ void DialogPatternXmlEdit::ElementClicked ( const QModelIndex & index ) // Clear all data and disable buttons this->ClearEditData(); + // Check if element is selectable at this time + if (item2->IsSelectable() == false) + { + return; + } // Check status of element in change stack this->currentNodeEdited=item2; this->currentNodeEditedStatus=0; - DialogPatternXmlEdit::ChangesStackElement * local = this->changeStackRoot; + ChangesStackElement * local = this->changeStackRoot; while (local != nullptr) { if (local->element == item2) @@ -554,26 +870,27 @@ void DialogPatternXmlEdit::ElementClicked ( const QModelIndex & index ) local=local->next; } // Fill the dialog and Activate the line edit - if (item2->getelementType() != VXMLTreeElement::TypeRoot) + if (item2->GetelementType() != VXMLTreeElement::TypeRoot) { - ui->lineEdit_Name->setText(currentNodeEdited->gettreeNodeName()); + ui->lineEdit_Name->setText(currentNodeEdited->GettreeNodeName()); ui->lineEdit_Name->setEnabled(true); // Fill value - ui->lineEdit_Value->setText(currentNodeEdited->gettreeNodeValue()); + ui->lineEdit_Value->setText(currentNodeEdited->GettreeNodeValue()); ui->lineEdit_Value->setEnabled(true); } - if (item2->getelementType() == VXMLTreeElement::TypeRoot) + if (item2->GetelementType() == VXMLTreeElement::TypeRoot) { ui->label_type_value->setText(tr("Root node"));; ui->pushButton_Add_son->setEnabled(true); } - else if (item2->getelementType() == VXMLTreeElement::TypeNode) + 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); + ui->pushButton_Remove_Node->setEnabled(true); } - else if (item2->getelementType() == VXMLTreeElement::TypeAttr) + else if (item2->GetelementType() == VXMLTreeElement::TypeAttr) { ui->label_type_value->setText(tr("Attribute")); ui->pushButton_Remove_attribute->setEnabled(true); @@ -583,15 +900,9 @@ void DialogPatternXmlEdit::ElementClicked ( const QModelIndex & index ) //--------------------------------------------------------------------------------------------------------------------- DialogPatternXmlEdit::~DialogPatternXmlEdit() { - //xmlmodel->clearTree(); + this->ClearStack(); + this->xmlmodel->ClearTree(); delete ui; - ChangesStackElement * tmp; - while (changeStackRoot != nullptr) - { - tmp=changeStackRoot->next; - delete changeStackRoot; - changeStackRoot=tmp; - } } //--------------------------------------------------------------------------------------------------------------------- @@ -637,8 +948,8 @@ void DialogPatternXmlEdit::ReadNodes(QDomNode dNode, VXMLTreeElement* root, VXML do { tElement=xmlmodel->getCurrent()->elmt; - tNode=tElement->getDocNode(); - if (tElement->getelementType() == VXMLTreeElement::TypeNode) + tNode=tElement->GetDocNode(); + if (tElement->GetelementType() == VXMLTreeElement::TypeNode) { totalOfChilds = tNode.childNodes().size(); for (int i=0;isetTreeNodeValue(tNode.nodeValue()); + tElement->SetTreeNodeValue(tNode.nodeValue()); } for (int i = 0; i < tNode.attributes().size(); i++) { tElement2 = new VXMLTreeElement(tNode.attributes().item(i).nodeName(), VXMLTreeElement::TypeAttr, tNode, false); - tElement2->setTreeNodeValue(tNode.attributes().item(i).nodeValue()); + tElement2->SetTreeNodeValue(tNode.attributes().item(i).nodeValue()); tElement->appendRow(tElement2); xmlmodel->appendchain(tElement2); } @@ -737,13 +1048,13 @@ const short int VXMLTreeElement::TypeRoot=3; // ^ VXMLTreeElement::VXMLTreeElement(QString name, int nodetype, QDomNode source, bool editor) : QStandardItem(name), DocNode(source), addedNode(editor), elementType(nodetype), treeNodeValue(""), - treeNodeValueSet(false), treeNodeName(name) + treeNodeValueSet(false), treeNodeName(name), selectable(true) { if (editor == false) { this->DocNode=source; } - this->setText(this->displayText()); + this->setText(this->DisplayText()); switch (this->elementType) { case VXMLTreeElement::TypeAttr: @@ -759,22 +1070,22 @@ VXMLTreeElement::~VXMLTreeElement() {} //--------------------------------------------------------------------------------------------------------------------- -void VXMLTreeElement::setTreeNodeValue(QString value) +void VXMLTreeElement::SetTreeNodeValue(QString value) { this->treeNodeValue=value; this->treeNodeValueSet=true; - this->setText(this->displayText()); + this->setText(this->DisplayText()); } //--------------------------------------------------------------------------------------------------------------------- -void VXMLTreeElement::setTreeNodeName(QString value) +void VXMLTreeElement::SetTreeNodeName(QString value) { this->treeNodeName=value; - this->setText(this->displayText()); + this->setText(this->DisplayText()); } //--------------------------------------------------------------------------------------------------------------------- -QString VXMLTreeElement::displayText() +QString VXMLTreeElement::DisplayText() { if (this->elementType == VXMLTreeElement::TypeRoot) { @@ -784,7 +1095,7 @@ QString VXMLTreeElement::displayText() } //--------------------------------------------------------------------------------------------------------------------- -QString VXMLTreeElement::gettreeNodeValue() +QString VXMLTreeElement::GettreeNodeValue() { // Only return value if it's really set. if (this->treeNodeValueSet) diff --git a/src/app/dialogs/app/dialogpatternxmledit.h b/src/app/dialogs/app/dialogpatternxmledit.h index b5ad38d42..4a8fccdc7 100644 --- a/src/app/dialogs/app/dialogpatternxmledit.h +++ b/src/app/dialogs/app/dialogpatternxmledit.h @@ -40,6 +40,7 @@ */ #define BACKGROUND_COLOR_ATTRIBUTE QBrush(Qt::GlobalColor::cyan) +#define BACKGROUND_COLOR_INACTIVE_NODE QBrush(Qt::GlobalColor::gray) class VXMLTreeElement : public QStandardItem { @@ -62,26 +63,28 @@ public: * @brief setNodeValue : set value of node (content or attribute) * @param value : the value */ - void setTreeNodeValue(QString value); + void SetTreeNodeValue(QString value); /** * @brief displayText : text to display * @return text to display */ - QString displayText(); + QString DisplayText(); /** * @brief getDocNode * @return current document node */ - QDomNode getDocNode(); - QString gettreeNodeName(); - QString gettreeNodeValue(); - bool gettreeNodeValueSet(); - void setTreeNodeName(QString value); + QDomNode GetDocNode(); + QString GettreeNodeName(); + QString GettreeNodeValue(); + bool GettreeNodeValueSet(); + void SetTreeNodeName(QString value); /** * @brief getelementType * @return elementType value */ - short int getelementType(); + short int GetelementType(); + bool IsSelectable(); + void SetSetlectable(bool value); private: /** @@ -109,25 +112,39 @@ private: * @brief nodeValue : Attribute or node value */ QString treeNodeName; + /** + * @brief selectable : can item be selected to be changed (ex : false if father deleted). + */ + bool selectable; Q_DISABLE_COPY(VXMLTreeElement) }; -inline short int VXMLTreeElement::getelementType() +inline bool VXMLTreeElement::IsSelectable() +{ + return this->selectable; +} + +inline void VXMLTreeElement::SetSetlectable(bool value) +{ + this->selectable=value; +} + +inline short int VXMLTreeElement::GetelementType() { return this->elementType; } -inline QDomNode VXMLTreeElement::getDocNode() +inline QDomNode VXMLTreeElement::GetDocNode() { return this->DocNode; } -inline QString VXMLTreeElement::gettreeNodeName() +inline QString VXMLTreeElement::GettreeNodeName() { return this->treeNodeName; } -inline bool VXMLTreeElement::gettreeNodeValueSet() +inline bool VXMLTreeElement::GettreeNodeValueSet() { return this->treeNodeValueSet; } @@ -224,7 +241,8 @@ public: void ButtonAddSonClicked(); void ButtonAddAttributeClicked(); void ButtonApplyChangesClicked(); - + void ButtonUndoLastChange(); + void ButtonDeleteNode(); // Stack of changes definition typedef struct ChangesStackElement @@ -232,8 +250,10 @@ public: short int type; VXMLTreeElement *element; QString *newText; + QString *oldText; bool changedText; QString *newValue; + QString *oldValue; bool changedValue; ChangesStackElement* next; } ChangesStackElement; @@ -246,6 +266,19 @@ public: VXMLTreeElement* currentNodeEdited; short int currentNodeEditedStatus; ChangesStackElement* currentNodeEditedStack; + /** + * @brief UndoChange : undo change in change stack element + * @param current : change to undo + * @return true if undo has been done, false on error + */ + bool UndoChange(ChangesStackElement* current); + /** + * @brief DeleteNodeAndSons : delete node and all it's attribute and sons below + * @param currentNode : node to delete + * @param onlydeactivate : if true, dont delete just deactivate sons and attributes + * @return false if changes couldn't be done + */ + bool DeleteNodeAndSons(VXMLTreeElement *currentNode, bool onlydeactivate); private slots: void BaseSelectionChanged(int value); diff --git a/src/app/dialogs/app/dialogpatternxmledit.ui b/src/app/dialogs/app/dialogpatternxmledit.ui index 8b4a7238e..5336d3c21 100644 --- a/src/app/dialogs/app/dialogpatternxmledit.ui +++ b/src/app/dialogs/app/dialogpatternxmledit.ui @@ -200,7 +200,7 @@ - + false @@ -272,12 +272,12 @@ - + false - Cancel changes + Undo last From bb3e8e58bbce7ba6d627ced0a1ea7320f174be3c Mon Sep 17 00:00:00 2001 From: Patrick Proy Date: Mon, 19 May 2014 08:04:33 +0200 Subject: [PATCH 09/15] Corrected display problems when restoring nodes Changes casting styles --HG-- branch : XML Editor --- src/app/dialogs/app/dialogpatternxmledit.cpp | 37 ++++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/src/app/dialogs/app/dialogpatternxmledit.cpp b/src/app/dialogs/app/dialogpatternxmledit.cpp index 764441b9e..f9379e910 100644 --- a/src/app/dialogs/app/dialogpatternxmledit.cpp +++ b/src/app/dialogs/app/dialogpatternxmledit.cpp @@ -154,18 +154,12 @@ bool DialogPatternXmlEdit::DeleteNodeAndSons(VXMLTreeElement * currentNode, bool QList stack; VXMLTreeElement * currentNodeStack; + // First undo all changes on sons stack << currentNode; while (stack.isEmpty() == false) { //pop first element currentNodeStack=stack.takeFirst(); - if (onlydeactivate==true) - { - currentNodeStack->SetSetlectable(false); - QFont textfont=currentNodeStack->font(); - textfont.setStrikeOut(true); - currentNodeStack->setFont(textfont); - } // clear all changes made in son node and not on current or it will loop if (currentNodeStack != currentNode) { @@ -189,6 +183,29 @@ bool DialogPatternXmlEdit::DeleteNodeAndSons(VXMLTreeElement * currentNode, bool } //stack.removeFirst(); } + // Next set strike font on all + if (onlydeactivate==true) + { + stack << currentNode; + while (stack.isEmpty() == false) + { + //pop first element + currentNodeStack=stack.takeFirst(); + + currentNodeStack->SetSetlectable(false); + QFont textfont=currentNodeStack->font(); + textfont.setStrikeOut(true); + currentNodeStack->setFont(textfont); + // add sons to stack + int index=currentNodeStack->rowCount(); + while (index > 0) + { + stack << static_cast (currentNodeStack->child(index-1)); + index--; + } + } + } + currentNode->SetSetlectable(true); if (onlydeactivate==false) @@ -836,12 +853,10 @@ void DialogPatternXmlEdit::ElementClicked ( const QModelIndex & index ) { // Get item (function returns parent of clicked item) - //TODO warning: use of old-style cast [-Wold-style-cast] - VXMLTreeElement *item = (VXMLTreeElement *)index.internalPointer(); + VXMLTreeElement *item = static_cast (index.internalPointer()); // Get child specified by index row/column - //TODO warning: use of old-style cast [-Wold-style-cast] - VXMLTreeElement * item2 = (VXMLTreeElement *) item->child(index.row(), index.column()); + VXMLTreeElement * item2 = static_cast (item->child(index.row(), index.column())); // Clear all data and disable buttons this->ClearEditData(); From dc1d7c5fe2880240b0fc6fc0a079156540f72638 Mon Sep 17 00:00:00 2001 From: dismine Date: Mon, 19 May 2014 21:03:12 +0300 Subject: [PATCH 10/15] Make xml editor disable on the start. --HG-- branch : XML Editor --- src/app/mainwindow.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/app/mainwindow.cpp b/src/app/mainwindow.cpp index 93d18d6bc..356ab269e 100644 --- a/src/app/mainwindow.cpp +++ b/src/app/mainwindow.cpp @@ -1089,6 +1089,8 @@ void MainWindow::SetEnableWidgets(bool enable) } ui->actionTable->setEnabled(enable); ui->actionHistory->setEnabled(enable); + ui->actionPattern_properties->setEnabled(enable); + ui->actionEdit_pattern_code->setEnabled(enable); } //--------------------------------------------------------------------------------------------------------------------- @@ -1368,7 +1370,7 @@ void MainWindow::CreateActions() connect(ui->actionPattern_properties, &QAction::triggered, this, &MainWindow::PatternProperties); ui->actionPattern_properties->setEnabled(false); connect(ui->actionEdit_pattern_code, &QAction::triggered, this, &MainWindow::EditPatternCode); - //ui->actionEdit_pattern_code->setEnabled(false); + ui->actionEdit_pattern_code->setEnabled(false); //Actions for recent files loaded by a main window application. for (int i = 0; i < MaxRecentFiles; ++i) From b83633541bce0c8356ca828a7661620413812557 Mon Sep 17 00:00:00 2001 From: dismine Date: Mon, 19 May 2014 21:07:51 +0300 Subject: [PATCH 11/15] Hide header. --HG-- branch : XML Editor --- src/app/dialogs/app/dialogpatternxmledit.ui | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/app/dialogs/app/dialogpatternxmledit.ui b/src/app/dialogs/app/dialogpatternxmledit.ui index 5336d3c21..f6ce6432d 100644 --- a/src/app/dialogs/app/dialogpatternxmledit.ui +++ b/src/app/dialogs/app/dialogpatternxmledit.ui @@ -62,6 +62,12 @@ QAbstractItemView::SingleSelection + + true + + + false + From ffda1e5f8db95a7146ea3b256ef185ba94419f47 Mon Sep 17 00:00:00 2001 From: dismine Date: Mon, 19 May 2014 21:16:26 +0300 Subject: [PATCH 12/15] All pattern pieces instead All drawings. --HG-- branch : XML Editor --- src/app/dialogs/app/dialogpatternxmledit.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/app/dialogs/app/dialogpatternxmledit.cpp b/src/app/dialogs/app/dialogpatternxmledit.cpp index f9379e910..dc1211f7a 100644 --- a/src/app/dialogs/app/dialogpatternxmledit.cpp +++ b/src/app/dialogs/app/dialogpatternxmledit.cpp @@ -29,6 +29,8 @@ #include "dialogpatternxmledit.h" #include "ui_dialogpatternxmledit.h" +#include + const short int DialogPatternXmlEdit::ChangeTypeDelete=1; const short int DialogPatternXmlEdit::ChangeTypeAdd=2; const short int DialogPatternXmlEdit::ChangeTypeModify=3; @@ -63,7 +65,7 @@ DialogPatternXmlEdit::DialogPatternXmlEdit(QWidget *parent, VPattern *xmldoc) VXMLTreeElement* standard_base = new VXMLTreeElement("Valentina", VXMLTreeElement::TypeRoot, root, false); - ui->comboBox_Base_Selection->addItem(tr("All drawings"), QVariant(0)); + ui->comboBox_Base_Selection->addItem(tr("All pattern pieces"), QVariant(0)); //rootBases[0]=(QDomNode) doc->DocumentNode; rootNode->appendRow(standard_base); @@ -579,9 +581,7 @@ void DialogPatternXmlEdit::ButtonAddSonClicked() } - QString name = QInputDialog::getText(this, tr("Node Name"), - tr("Name:"), QLineEdit::Normal, - "", &ok); + QString name = QInputDialog::getText(this, tr("Node Name"), tr("Name:"), QLineEdit::Normal, "", &ok); if (ok==false) { return; From 81e8638a6d294bc04ef35b726186d3d68e6a71b9 Mon Sep 17 00:00:00 2001 From: Patrick Proy Date: Sat, 24 May 2014 16:23:47 +0200 Subject: [PATCH 13/15] Change formula input to multiline (in dialogendline only ) Other dialogs don't work for now on this version. --HG-- branch : XML Editor --- src/app/dialogs/tools/dialogendline.cpp | 36 +++++- src/app/dialogs/tools/dialogendline.h | 12 ++ src/app/dialogs/tools/dialogendline.ui | 73 +++++++++-- src/app/dialogs/tools/dialogtool.cpp | 117 ++++++++++++++++-- src/app/dialogs/tools/dialogtool.h | 6 + src/app/share/resources/icon.qrc | 2 + .../share/resources/icon/16x16/go-down.png | Bin 0 -> 756 bytes .../share/resources/icon/16x16/go-next.png | Bin 0 -> 829 bytes 8 files changed, 218 insertions(+), 28 deletions(-) create mode 100644 src/app/share/resources/icon/16x16/go-down.png create mode 100644 src/app/share/resources/icon/16x16/go-next.png diff --git a/src/app/dialogs/tools/dialogendline.cpp b/src/app/dialogs/tools/dialogendline.cpp index b9b1b4279..cdc6aad14 100644 --- a/src/app/dialogs/tools/dialogendline.cpp +++ b/src/app/dialogs/tools/dialogendline.cpp @@ -39,10 +39,13 @@ DialogEndLine::DialogEndLine(const VContainer *data, QWidget *parent) ui->setupUi(this); InitVariables(ui); labelResultCalculation = ui->labelResultCalculation; - lineEditFormula = ui->lineEditFormula; + plainTextEditFormula = ui->plainTextEditFormula; labelEditFormula = ui->labelEditFormula; labelEditNamePoint = ui->labelEditNamePoint; + // TODO : auto extend height on first value length. + this->formulaBaseHeight=ui->plainTextEditFormula->height(); + InitOkCansel(ui); flagFormula = false; flagName = false; @@ -57,7 +60,29 @@ DialogEndLine::DialogEndLine(const VContainer *data, QWidget *parent) connect(ui->listWidget, &QListWidget::itemDoubleClicked, this, &DialogEndLine::PutVal); connect(ui->toolButtonEqual, &QPushButton::clicked, this, &DialogEndLine::EvalFormula); connect(ui->lineEditNamePoint, &QLineEdit::textChanged, this, &DialogEndLine::NamePointChanged); - connect(ui->lineEditFormula, &QLineEdit::textChanged, this, &DialogEndLine::FormulaChanged); + connect(ui->plainTextEditFormula, &QPlainTextEdit::textChanged, this, &DialogEndLine::FormulaTextChanged); + connect(ui->pushButtonGrowLength, &QPushButton::clicked, this, &DialogEndLine::DeployFormulaTextEdit); +} + +//--------------------------------------------------------------------------------------------------------------------- +void DialogEndLine::FormulaTextChanged() +{ + // TODO issue #79 : back to FormulaChanged when full update + // Also remove this function if only one function called here + this->FormulaChanged2(); +} + +//--------------------------------------------------------------------------------------------------------------------- +void DialogEndLine::DeployFormulaTextEdit() +{ + if (ui->plainTextEditFormula->height() < 64) + { + ui->plainTextEditFormula->setFixedHeight(64); + } + else + { + ui->plainTextEditFormula->setFixedHeight(this->formulaBaseHeight); + } } //--------------------------------------------------------------------------------------------------------------------- @@ -90,7 +115,10 @@ void DialogEndLine::setTypeLine(const QString &value) void DialogEndLine::setFormula(const QString &value) { formula = value; - ui->lineEditFormula->setText(formula); + ui->plainTextEditFormula->setPlainText(value); + //QTextCursor cursor = ui->plainTextEditFormula->textCursor(); + //cursor.insertText(value); + //ui->plainTextEditFormula->setCursor(cursor); } //--------------------------------------------------------------------------------------------------------------------- @@ -111,7 +139,7 @@ void DialogEndLine::DialogAccepted() { pointName = ui->lineEditNamePoint->text(); typeLine = GetTypeLine(ui->comboBoxLineType); - formula = ui->lineEditFormula->text(); + formula = ui->plainTextEditFormula->toPlainText(); angle = ui->doubleSpinBoxAngle->value(); basePointId = getCurrentObjectId(ui->comboBoxBasePoint); emit DialogClosed(QDialog::Accepted); diff --git a/src/app/dialogs/tools/dialogendline.h b/src/app/dialogs/tools/dialogendline.h index 7dfc82b03..9765d6cbb 100644 --- a/src/app/dialogs/tools/dialogendline.h +++ b/src/app/dialogs/tools/dialogendline.h @@ -112,6 +112,14 @@ public slots: * @brief DialogAccepted save data and emit signal about closed dialog. */ virtual void DialogAccepted(); + /** + * @brief DeployFormulaTextEdit grow or shrink formula input + */ + void DeployFormulaTextEdit(); + /** + * @brief FormulaTextChanged when formula text changes for validation and calc + */ + void FormulaTextChanged(); private: Q_DISABLE_COPY(DialogEndLine) /** @@ -138,6 +146,10 @@ private: * @brief basePointId id base point of line */ quint32 basePointId; + /** + * @brief formulaBaseHeight base height defined by dialogui + */ + int formulaBaseHeight; }; inline QString DialogEndLine::getPointName() const diff --git a/src/app/dialogs/tools/dialogendline.ui b/src/app/dialogs/tools/dialogendline.ui index 1d6c59866..797078a5a 100644 --- a/src/app/dialogs/tools/dialogendline.ui +++ b/src/app/dialogs/tools/dialogendline.ui @@ -6,8 +6,8 @@ 0 0 - 539 - 559 + 486 + 522 @@ -74,17 +74,17 @@ - - - - 0 - 0 - + + + Qt::Horizontal - - Formula for calculation of length of line + + + 40 + 20 + - + @@ -147,6 +147,56 @@ + + + + + + + 16777215 + 24 + + + + + 4 + 0 + + + + + + + + + 16 + 16 + + + + <html><head/><body><p>Show full calculation in message box</p></body></html> + + + + + + + :/icon/16x16/go-down.png + :/icon/16x16/go-next.png:/icon/16x16/go-down.png + + + + 16 + 16 + + + + true + + + + + @@ -550,7 +600,6 @@ - lineEditFormula comboBoxBasePoint lineEditNamePoint doubleSpinBoxAngle diff --git a/src/app/dialogs/tools/dialogtool.cpp b/src/app/dialogs/tools/dialogtool.cpp index c10df30c0..ada3d1e16 100644 --- a/src/app/dialogs/tools/dialogtool.cpp +++ b/src/app/dialogs/tools/dialogtool.cpp @@ -33,10 +33,15 @@ #include +// TODO : for issue #79 +// replace lineEditFormula -> plainTextEditFormula +// delete lineEditFormala everywhrer +// delete PutValHere and eval overloaded functions + //--------------------------------------------------------------------------------------------------------------------- DialogTool::DialogTool(const VContainer *data, QWidget *parent) :QDialog(parent), data(data), isInitialized(false), flagName(true), flagFormula(true), timerFormula(nullptr), - bOk(nullptr), spinBoxAngle(nullptr), lineEditFormula(nullptr), listWidget(nullptr), + bOk(nullptr), spinBoxAngle(nullptr), lineEditFormula(nullptr), plainTextEditFormula(nullptr), listWidget(nullptr), labelResultCalculation(nullptr), labelDescription(nullptr), labelEditNamePoint(nullptr), labelEditFormula(nullptr), radioButtonSizeGrowth(nullptr), radioButtonStandardTable(nullptr), radioButtonIncrements(nullptr), radioButtonLengthLine(nullptr), radioButtonLengthArc(nullptr), @@ -283,22 +288,42 @@ void DialogTool::ChangeCurrentData(QComboBox *box, const quint32 &value) const } } -//--------------------------------------------------------------------------------------------------------------------- void DialogTool::PutValHere(QLineEdit *lineEdit, QListWidget *listWidget) -{ +{ // TODO issue #79 : erase this function after all tools updated to plainTextEdit Q_CHECK_PTR(lineEdit); Q_CHECK_PTR(listWidget); QListWidgetItem *item = listWidget->currentItem(); Q_CHECK_PTR(item); + int pos = lineEdit->cursorPosition(); lineEdit->setText(lineEdit->text().insert(lineEdit->cursorPosition(), item->text())); lineEdit->setFocus(); lineEdit->setCursorPosition(pos + item->text().size()); + +} + +//--------------------------------------------------------------------------------------------------------------------- +void DialogTool::PutValHere(QPlainTextEdit *plainTextEdit, QListWidget *listWidget) +{ + Q_CHECK_PTR(plainTextEdit); + Q_CHECK_PTR(listWidget); + QListWidgetItem *item = listWidget->currentItem(); + Q_CHECK_PTR(item); + + QTextCursor cursor = plainTextEdit->textCursor(); + cursor.insertText(item->text()); + plainTextEdit->setTextCursor(cursor); + /* + int pos = lineEdit->cursorPosition(); + lineEdit->setText(lineEdit->text().insert(lineEdit->cursorPosition(), item->text())); + lineEdit->setFocus(); + lineEdit->setCursorPosition(pos + item->text().size()); + */ } //--------------------------------------------------------------------------------------------------------------------- void DialogTool::ValFormulaChanged(bool &flag, QLineEdit *edit, QTimer *timer) -{ +{// TODO issue #79 : erase this function after all tools updated to plainTextEdit Q_CHECK_PTR(edit); Q_CHECK_PTR(timer); Q_CHECK_PTR(labelEditFormula); @@ -313,10 +338,26 @@ void DialogTool::ValFormulaChanged(bool &flag, QLineEdit *edit, QTimer *timer) } timer->start(1000); } - //--------------------------------------------------------------------------------------------------------------------- -void DialogTool::Eval(QLineEdit *edit, bool &flag, QTimer *timer, QLabel *label) +void DialogTool::ValFormulaChanged(bool &flag, QPlainTextEdit *edit, QTimer *timer) { + Q_CHECK_PTR(edit); + Q_CHECK_PTR(timer); + Q_CHECK_PTR(labelEditFormula); + if (edit->toPlainText().isEmpty()) + { + flag = false; + CheckState(); + QPalette palette = labelEditFormula->palette(); + palette.setColor(labelEditFormula->foregroundRole(), Qt::red); + labelEditFormula->setPalette(palette); + return; + } + timer->start(1000); +} + +void DialogTool::Eval(QLineEdit *edit, bool &flag, QTimer *timer, QLabel *label) +{ // TODO issue #79 : erase this function after all tools updated to plainTextEdit Q_CHECK_PTR(edit); Q_CHECK_PTR(timer); Q_CHECK_PTR(label); @@ -350,6 +391,45 @@ void DialogTool::Eval(QLineEdit *edit, bool &flag, QTimer *timer, QLabel *label) labelEditFormula->setPalette(palette); } +//--------------------------------------------------------------------------------------------------------------------- +void DialogTool::Eval(QPlainTextEdit *edit, bool &flag, QTimer *timer, QLabel *label) +{ + Q_CHECK_PTR(edit); + Q_CHECK_PTR(timer); + Q_CHECK_PTR(label); + Q_CHECK_PTR(labelEditFormula); + QPalette palette = labelEditFormula->palette(); + if (edit->toPlainText().isEmpty()) + { + flag = false; + palette.setColor(labelEditFormula->foregroundRole(), Qt::red); + } + else + { + Calculator cal(data); + QString errorMsg; + // Replace line return with spaces for calc + QString formula = edit->toPlainText(); + formula.replace("\n"," "); + qreal result = cal.eval(formula, &errorMsg); + if (errorMsg.isEmpty() == false) + { + label->setText(tr("Error")); + flag = false; + palette.setColor(labelEditFormula->foregroundRole(), Qt::red); + } + else + { + label->setText(QString().setNum(result)); + flag = true; + palette.setColor(labelEditFormula->foregroundRole(), QColor(76, 76, 76)); + } + } + CheckState(); + timer->stop(); + labelEditFormula->setPalette(palette); +} + //--------------------------------------------------------------------------------------------------------------------- void DialogTool::setCurrentPointId(QComboBox *box, quint32 &pointId, const quint32 &value, const quint32 &id) const { @@ -489,13 +569,22 @@ void DialogTool::DialogRejected() //--------------------------------------------------------------------------------------------------------------------- void DialogTool::FormulaChanged() -{ +{ // TODO issue #79 : erase after full update of tools. QLineEdit* edit = qobject_cast(sender()); if (edit) { ValFormulaChanged(flagFormula, edit, timerFormula); } } +//--------------------------------------------------------------------------------------------------------------------- +void DialogTool::FormulaChanged2() +{ + QPlainTextEdit* edit = qobject_cast(sender()); + if (edit) + { + ValFormulaChanged(flagFormula, edit, timerFormula); + } +} //--------------------------------------------------------------------------------------------------------------------- void DialogTool::ArrowUp() @@ -556,9 +645,9 @@ void DialogTool::ArrowRightDown() //--------------------------------------------------------------------------------------------------------------------- void DialogTool::EvalFormula() { - Q_CHECK_PTR(lineEditFormula); + Q_CHECK_PTR(plainTextEditFormula); Q_CHECK_PTR(labelResultCalculation); - Eval(lineEditFormula, flagFormula, timerFormula, labelResultCalculation); + Eval(plainTextEditFormula, flagFormula, timerFormula, labelResultCalculation); } //--------------------------------------------------------------------------------------------------------------------- @@ -615,19 +704,23 @@ void DialogTool::Increments() //--------------------------------------------------------------------------------------------------------------------- void DialogTool::PutHere() { - PutValHere(lineEditFormula, listWidget); + PutValHere(plainTextEditFormula, listWidget); } //--------------------------------------------------------------------------------------------------------------------- void DialogTool::PutVal(QListWidgetItem *item) { - Q_CHECK_PTR(lineEditFormula); + Q_CHECK_PTR(plainTextEditFormula); Q_CHECK_PTR(item); - int pos = lineEditFormula->cursorPosition(); + QTextCursor cursor = plainTextEditFormula->textCursor(); + cursor.insertText(item->text()); + plainTextEditFormula->setTextCursor(cursor); + /*int pos = plainTextEditFormula->cursorPosition(); lineEditFormula->setText(lineEditFormula->text().insert(lineEditFormula->cursorPosition(), item->text())); lineEditFormula->setFocus(); lineEditFormula->setCursorPosition(pos + item->text().size()); + */ } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/dialogs/tools/dialogtool.h b/src/app/dialogs/tools/dialogtool.h index 6def423d3..dc8eed65b 100644 --- a/src/app/dialogs/tools/dialogtool.h +++ b/src/app/dialogs/tools/dialogtool.h @@ -37,6 +37,7 @@ #include #include #include +#include #include "../../container/vcontainer.h" #include "../../widgets/vapplication.h" @@ -102,6 +103,7 @@ public slots: * @brief formula check formula */ void FormulaChanged(); + void FormulaChanged2(); /** * @brief ArrowUp set angle value 90 degree */ @@ -214,6 +216,7 @@ protected: * @brief lineEditFormula linEdit for formula */ QLineEdit *lineEditFormula; + QPlainTextEdit *plainTextEditFormula; /** * @brief listWidget listWidget with variables */ @@ -341,6 +344,7 @@ protected: * @param listWidget listWidget */ void PutValHere(QLineEdit *lineEdit, QListWidget *listWidget); + void PutValHere(QPlainTextEdit *plainTextEdit, QListWidget *listWidget); /** * @brief ValFormulaChanged handle change formula * @param flag flag state of formula @@ -348,6 +352,7 @@ protected: * @param timer timer of formula */ void ValFormulaChanged(bool &flag, QLineEdit *edit, QTimer * timer); + void ValFormulaChanged(bool &flag, QPlainTextEdit *edit, QTimer * timer); /** * @brief Eval evaluate formula and show result * @param edit lineEdit of formula @@ -355,6 +360,7 @@ protected: * @param timer timer of formula * @param label label for signal error */ + void Eval(QPlainTextEdit *edit, bool &flag, QTimer *timer, QLabel *label); void Eval(QLineEdit *edit, bool &flag, QTimer *timer, QLabel *label); /** * @brief setCurrentPointId set current point id in combobox diff --git a/src/app/share/resources/icon.qrc b/src/app/share/resources/icon.qrc index 64d48ee8e..75a714bc7 100644 --- a/src/app/share/resources/icon.qrc +++ b/src/app/share/resources/icon.qrc @@ -52,5 +52,7 @@ icon/flags/uk.png icon/Graduation.png icon/individual.png + icon/16x16/go-down.png + icon/16x16/go-next.png diff --git a/src/app/share/resources/icon/16x16/go-down.png b/src/app/share/resources/icon/16x16/go-down.png new file mode 100644 index 0000000000000000000000000000000000000000..a74287892e3a8b6135f8c7239530e962021fdb49 GIT binary patch literal 756 zcmVL}00NXrL_t(I%gvH&NYham#-Foklnb4= za@o|3Y!y0`O(L?SME#IxQR^aQMHY#sU?r(hlTnGLWr>%p6cbXzP$@5r{Lsz3q?1Y` z`(Wgibl#_%{@*@~t(N$`=hMqM&w0-8ea`{@kxx*HxlS)A3yMz2iXpONrZuOKe3UnR z!4jRleAiq{c3i2o_q#(_qav?EwRA4`BPA5HYGqMRB21p*7cBq)_IF4}UA{n>f{jcCNl{kPcsbde3wz4*)8PR0s_`5W7>AxOr@Rn&9TiU@b6{5eP$C z9s4mdgJ1m(lG`vvY61fx^m^K%F=cWd4bN~)GYIk$ zt!4p!uB^Fx0GaWDZUFGQq0&f@aV-|XCo=F=lV7}0k`pDGoMP5G5irkZ?ITVI-0sd~ zGIyvu2ioc?s~rI#DZ6^`gEJFZ~8kU=k=Mtl+)?0000Px#AY({UO#lFTCIA3{ga82g0001h=l}q9FaQARU;qF* zm;eA5aGbhPJOBUy33hNnX8-^I5C8xG5CB}pt-}BS00v@9M??Ss00000`9r&Z00009 za7bBm000tn000tn0p4aGcmMzbQAtEWR5%f}!D&d-aU2KWZ&0s-mJ*Us1VO1U%rsXd zSSG_PgtW{uk143hQd?wRo9X6}_wr(DR?0S-W-}dgF{6cftY*h%g)bx`qx4Nez0jh5 zpY{)h>C|^U57DCP+r6|FiK3g<)nRx+&f+fDBm_h&r6*IGitZ@=HqxS8MIX87GQX!K z=d!hp(pxWCTG*6p#_7e(%Qkfp`Adu#LO!6A803~$iBZWEsQe=? zlsyE8GM|8QZ6IMk^NiBKVKX_~Cy)?lPm~h=kAYo1KiK}k1$gOZuv Date: Mon, 26 May 2014 14:20:14 +0200 Subject: [PATCH 14/15] Backout changeset 38e907833133b065c72ba362fed040f608d4b66d --HG-- branch : XML Editor --- src/app/dialogs/tools/dialogendline.cpp | 36 +----- src/app/dialogs/tools/dialogendline.h | 12 -- src/app/dialogs/tools/dialogendline.ui | 73 ++--------- src/app/dialogs/tools/dialogtool.cpp | 117 ++---------------- src/app/dialogs/tools/dialogtool.h | 6 - src/app/share/resources/icon.qrc | 2 - .../share/resources/icon/16x16/go-down.png | Bin 756 -> 0 bytes .../share/resources/icon/16x16/go-next.png | Bin 829 -> 0 bytes 8 files changed, 28 insertions(+), 218 deletions(-) delete mode 100644 src/app/share/resources/icon/16x16/go-down.png delete mode 100644 src/app/share/resources/icon/16x16/go-next.png diff --git a/src/app/dialogs/tools/dialogendline.cpp b/src/app/dialogs/tools/dialogendline.cpp index cdc6aad14..b9b1b4279 100644 --- a/src/app/dialogs/tools/dialogendline.cpp +++ b/src/app/dialogs/tools/dialogendline.cpp @@ -39,13 +39,10 @@ DialogEndLine::DialogEndLine(const VContainer *data, QWidget *parent) ui->setupUi(this); InitVariables(ui); labelResultCalculation = ui->labelResultCalculation; - plainTextEditFormula = ui->plainTextEditFormula; + lineEditFormula = ui->lineEditFormula; labelEditFormula = ui->labelEditFormula; labelEditNamePoint = ui->labelEditNamePoint; - // TODO : auto extend height on first value length. - this->formulaBaseHeight=ui->plainTextEditFormula->height(); - InitOkCansel(ui); flagFormula = false; flagName = false; @@ -60,29 +57,7 @@ DialogEndLine::DialogEndLine(const VContainer *data, QWidget *parent) connect(ui->listWidget, &QListWidget::itemDoubleClicked, this, &DialogEndLine::PutVal); connect(ui->toolButtonEqual, &QPushButton::clicked, this, &DialogEndLine::EvalFormula); connect(ui->lineEditNamePoint, &QLineEdit::textChanged, this, &DialogEndLine::NamePointChanged); - connect(ui->plainTextEditFormula, &QPlainTextEdit::textChanged, this, &DialogEndLine::FormulaTextChanged); - connect(ui->pushButtonGrowLength, &QPushButton::clicked, this, &DialogEndLine::DeployFormulaTextEdit); -} - -//--------------------------------------------------------------------------------------------------------------------- -void DialogEndLine::FormulaTextChanged() -{ - // TODO issue #79 : back to FormulaChanged when full update - // Also remove this function if only one function called here - this->FormulaChanged2(); -} - -//--------------------------------------------------------------------------------------------------------------------- -void DialogEndLine::DeployFormulaTextEdit() -{ - if (ui->plainTextEditFormula->height() < 64) - { - ui->plainTextEditFormula->setFixedHeight(64); - } - else - { - ui->plainTextEditFormula->setFixedHeight(this->formulaBaseHeight); - } + connect(ui->lineEditFormula, &QLineEdit::textChanged, this, &DialogEndLine::FormulaChanged); } //--------------------------------------------------------------------------------------------------------------------- @@ -115,10 +90,7 @@ void DialogEndLine::setTypeLine(const QString &value) void DialogEndLine::setFormula(const QString &value) { formula = value; - ui->plainTextEditFormula->setPlainText(value); - //QTextCursor cursor = ui->plainTextEditFormula->textCursor(); - //cursor.insertText(value); - //ui->plainTextEditFormula->setCursor(cursor); + ui->lineEditFormula->setText(formula); } //--------------------------------------------------------------------------------------------------------------------- @@ -139,7 +111,7 @@ void DialogEndLine::DialogAccepted() { pointName = ui->lineEditNamePoint->text(); typeLine = GetTypeLine(ui->comboBoxLineType); - formula = ui->plainTextEditFormula->toPlainText(); + formula = ui->lineEditFormula->text(); angle = ui->doubleSpinBoxAngle->value(); basePointId = getCurrentObjectId(ui->comboBoxBasePoint); emit DialogClosed(QDialog::Accepted); diff --git a/src/app/dialogs/tools/dialogendline.h b/src/app/dialogs/tools/dialogendline.h index 9765d6cbb..7dfc82b03 100644 --- a/src/app/dialogs/tools/dialogendline.h +++ b/src/app/dialogs/tools/dialogendline.h @@ -112,14 +112,6 @@ public slots: * @brief DialogAccepted save data and emit signal about closed dialog. */ virtual void DialogAccepted(); - /** - * @brief DeployFormulaTextEdit grow or shrink formula input - */ - void DeployFormulaTextEdit(); - /** - * @brief FormulaTextChanged when formula text changes for validation and calc - */ - void FormulaTextChanged(); private: Q_DISABLE_COPY(DialogEndLine) /** @@ -146,10 +138,6 @@ private: * @brief basePointId id base point of line */ quint32 basePointId; - /** - * @brief formulaBaseHeight base height defined by dialogui - */ - int formulaBaseHeight; }; inline QString DialogEndLine::getPointName() const diff --git a/src/app/dialogs/tools/dialogendline.ui b/src/app/dialogs/tools/dialogendline.ui index 797078a5a..1d6c59866 100644 --- a/src/app/dialogs/tools/dialogendline.ui +++ b/src/app/dialogs/tools/dialogendline.ui @@ -6,8 +6,8 @@ 0 0 - 486 - 522 + 539 + 559 @@ -74,17 +74,17 @@ - - - Qt::Horizontal + + + + 0 + 0 + - - - 40 - 20 - + + Formula for calculation of length of line - + @@ -147,56 +147,6 @@ - - - - - - - 16777215 - 24 - - - - - 4 - 0 - - - - - - - - - 16 - 16 - - - - <html><head/><body><p>Show full calculation in message box</p></body></html> - - - - - - - :/icon/16x16/go-down.png - :/icon/16x16/go-next.png:/icon/16x16/go-down.png - - - - 16 - 16 - - - - true - - - - - @@ -600,6 +550,7 @@ + lineEditFormula comboBoxBasePoint lineEditNamePoint doubleSpinBoxAngle diff --git a/src/app/dialogs/tools/dialogtool.cpp b/src/app/dialogs/tools/dialogtool.cpp index ada3d1e16..c10df30c0 100644 --- a/src/app/dialogs/tools/dialogtool.cpp +++ b/src/app/dialogs/tools/dialogtool.cpp @@ -33,15 +33,10 @@ #include -// TODO : for issue #79 -// replace lineEditFormula -> plainTextEditFormula -// delete lineEditFormala everywhrer -// delete PutValHere and eval overloaded functions - //--------------------------------------------------------------------------------------------------------------------- DialogTool::DialogTool(const VContainer *data, QWidget *parent) :QDialog(parent), data(data), isInitialized(false), flagName(true), flagFormula(true), timerFormula(nullptr), - bOk(nullptr), spinBoxAngle(nullptr), lineEditFormula(nullptr), plainTextEditFormula(nullptr), listWidget(nullptr), + bOk(nullptr), spinBoxAngle(nullptr), lineEditFormula(nullptr), listWidget(nullptr), labelResultCalculation(nullptr), labelDescription(nullptr), labelEditNamePoint(nullptr), labelEditFormula(nullptr), radioButtonSizeGrowth(nullptr), radioButtonStandardTable(nullptr), radioButtonIncrements(nullptr), radioButtonLengthLine(nullptr), radioButtonLengthArc(nullptr), @@ -288,42 +283,22 @@ void DialogTool::ChangeCurrentData(QComboBox *box, const quint32 &value) const } } +//--------------------------------------------------------------------------------------------------------------------- void DialogTool::PutValHere(QLineEdit *lineEdit, QListWidget *listWidget) -{ // TODO issue #79 : erase this function after all tools updated to plainTextEdit +{ Q_CHECK_PTR(lineEdit); Q_CHECK_PTR(listWidget); QListWidgetItem *item = listWidget->currentItem(); Q_CHECK_PTR(item); - int pos = lineEdit->cursorPosition(); lineEdit->setText(lineEdit->text().insert(lineEdit->cursorPosition(), item->text())); lineEdit->setFocus(); lineEdit->setCursorPosition(pos + item->text().size()); - -} - -//--------------------------------------------------------------------------------------------------------------------- -void DialogTool::PutValHere(QPlainTextEdit *plainTextEdit, QListWidget *listWidget) -{ - Q_CHECK_PTR(plainTextEdit); - Q_CHECK_PTR(listWidget); - QListWidgetItem *item = listWidget->currentItem(); - Q_CHECK_PTR(item); - - QTextCursor cursor = plainTextEdit->textCursor(); - cursor.insertText(item->text()); - plainTextEdit->setTextCursor(cursor); - /* - int pos = lineEdit->cursorPosition(); - lineEdit->setText(lineEdit->text().insert(lineEdit->cursorPosition(), item->text())); - lineEdit->setFocus(); - lineEdit->setCursorPosition(pos + item->text().size()); - */ } //--------------------------------------------------------------------------------------------------------------------- void DialogTool::ValFormulaChanged(bool &flag, QLineEdit *edit, QTimer *timer) -{// TODO issue #79 : erase this function after all tools updated to plainTextEdit +{ Q_CHECK_PTR(edit); Q_CHECK_PTR(timer); Q_CHECK_PTR(labelEditFormula); @@ -338,26 +313,10 @@ void DialogTool::ValFormulaChanged(bool &flag, QLineEdit *edit, QTimer *timer) } timer->start(1000); } -//--------------------------------------------------------------------------------------------------------------------- -void DialogTool::ValFormulaChanged(bool &flag, QPlainTextEdit *edit, QTimer *timer) -{ - Q_CHECK_PTR(edit); - Q_CHECK_PTR(timer); - Q_CHECK_PTR(labelEditFormula); - if (edit->toPlainText().isEmpty()) - { - flag = false; - CheckState(); - QPalette palette = labelEditFormula->palette(); - palette.setColor(labelEditFormula->foregroundRole(), Qt::red); - labelEditFormula->setPalette(palette); - return; - } - timer->start(1000); -} +//--------------------------------------------------------------------------------------------------------------------- void DialogTool::Eval(QLineEdit *edit, bool &flag, QTimer *timer, QLabel *label) -{ // TODO issue #79 : erase this function after all tools updated to plainTextEdit +{ Q_CHECK_PTR(edit); Q_CHECK_PTR(timer); Q_CHECK_PTR(label); @@ -391,45 +350,6 @@ void DialogTool::Eval(QLineEdit *edit, bool &flag, QTimer *timer, QLabel *label) labelEditFormula->setPalette(palette); } -//--------------------------------------------------------------------------------------------------------------------- -void DialogTool::Eval(QPlainTextEdit *edit, bool &flag, QTimer *timer, QLabel *label) -{ - Q_CHECK_PTR(edit); - Q_CHECK_PTR(timer); - Q_CHECK_PTR(label); - Q_CHECK_PTR(labelEditFormula); - QPalette palette = labelEditFormula->palette(); - if (edit->toPlainText().isEmpty()) - { - flag = false; - palette.setColor(labelEditFormula->foregroundRole(), Qt::red); - } - else - { - Calculator cal(data); - QString errorMsg; - // Replace line return with spaces for calc - QString formula = edit->toPlainText(); - formula.replace("\n"," "); - qreal result = cal.eval(formula, &errorMsg); - if (errorMsg.isEmpty() == false) - { - label->setText(tr("Error")); - flag = false; - palette.setColor(labelEditFormula->foregroundRole(), Qt::red); - } - else - { - label->setText(QString().setNum(result)); - flag = true; - palette.setColor(labelEditFormula->foregroundRole(), QColor(76, 76, 76)); - } - } - CheckState(); - timer->stop(); - labelEditFormula->setPalette(palette); -} - //--------------------------------------------------------------------------------------------------------------------- void DialogTool::setCurrentPointId(QComboBox *box, quint32 &pointId, const quint32 &value, const quint32 &id) const { @@ -569,17 +489,8 @@ void DialogTool::DialogRejected() //--------------------------------------------------------------------------------------------------------------------- void DialogTool::FormulaChanged() -{ // TODO issue #79 : erase after full update of tools. - QLineEdit* edit = qobject_cast(sender()); - if (edit) - { - ValFormulaChanged(flagFormula, edit, timerFormula); - } -} -//--------------------------------------------------------------------------------------------------------------------- -void DialogTool::FormulaChanged2() { - QPlainTextEdit* edit = qobject_cast(sender()); + QLineEdit* edit = qobject_cast(sender()); if (edit) { ValFormulaChanged(flagFormula, edit, timerFormula); @@ -645,9 +556,9 @@ void DialogTool::ArrowRightDown() //--------------------------------------------------------------------------------------------------------------------- void DialogTool::EvalFormula() { - Q_CHECK_PTR(plainTextEditFormula); + Q_CHECK_PTR(lineEditFormula); Q_CHECK_PTR(labelResultCalculation); - Eval(plainTextEditFormula, flagFormula, timerFormula, labelResultCalculation); + Eval(lineEditFormula, flagFormula, timerFormula, labelResultCalculation); } //--------------------------------------------------------------------------------------------------------------------- @@ -704,23 +615,19 @@ void DialogTool::Increments() //--------------------------------------------------------------------------------------------------------------------- void DialogTool::PutHere() { - PutValHere(plainTextEditFormula, listWidget); + PutValHere(lineEditFormula, listWidget); } //--------------------------------------------------------------------------------------------------------------------- void DialogTool::PutVal(QListWidgetItem *item) { - Q_CHECK_PTR(plainTextEditFormula); + Q_CHECK_PTR(lineEditFormula); Q_CHECK_PTR(item); - QTextCursor cursor = plainTextEditFormula->textCursor(); - cursor.insertText(item->text()); - plainTextEditFormula->setTextCursor(cursor); - /*int pos = plainTextEditFormula->cursorPosition(); + int pos = lineEditFormula->cursorPosition(); lineEditFormula->setText(lineEditFormula->text().insert(lineEditFormula->cursorPosition(), item->text())); lineEditFormula->setFocus(); lineEditFormula->setCursorPosition(pos + item->text().size()); - */ } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/dialogs/tools/dialogtool.h b/src/app/dialogs/tools/dialogtool.h index dc8eed65b..6def423d3 100644 --- a/src/app/dialogs/tools/dialogtool.h +++ b/src/app/dialogs/tools/dialogtool.h @@ -37,7 +37,6 @@ #include #include #include -#include #include "../../container/vcontainer.h" #include "../../widgets/vapplication.h" @@ -103,7 +102,6 @@ public slots: * @brief formula check formula */ void FormulaChanged(); - void FormulaChanged2(); /** * @brief ArrowUp set angle value 90 degree */ @@ -216,7 +214,6 @@ protected: * @brief lineEditFormula linEdit for formula */ QLineEdit *lineEditFormula; - QPlainTextEdit *plainTextEditFormula; /** * @brief listWidget listWidget with variables */ @@ -344,7 +341,6 @@ protected: * @param listWidget listWidget */ void PutValHere(QLineEdit *lineEdit, QListWidget *listWidget); - void PutValHere(QPlainTextEdit *plainTextEdit, QListWidget *listWidget); /** * @brief ValFormulaChanged handle change formula * @param flag flag state of formula @@ -352,7 +348,6 @@ protected: * @param timer timer of formula */ void ValFormulaChanged(bool &flag, QLineEdit *edit, QTimer * timer); - void ValFormulaChanged(bool &flag, QPlainTextEdit *edit, QTimer * timer); /** * @brief Eval evaluate formula and show result * @param edit lineEdit of formula @@ -360,7 +355,6 @@ protected: * @param timer timer of formula * @param label label for signal error */ - void Eval(QPlainTextEdit *edit, bool &flag, QTimer *timer, QLabel *label); void Eval(QLineEdit *edit, bool &flag, QTimer *timer, QLabel *label); /** * @brief setCurrentPointId set current point id in combobox diff --git a/src/app/share/resources/icon.qrc b/src/app/share/resources/icon.qrc index 75a714bc7..64d48ee8e 100644 --- a/src/app/share/resources/icon.qrc +++ b/src/app/share/resources/icon.qrc @@ -52,7 +52,5 @@ icon/flags/uk.png icon/Graduation.png icon/individual.png - icon/16x16/go-down.png - icon/16x16/go-next.png diff --git a/src/app/share/resources/icon/16x16/go-down.png b/src/app/share/resources/icon/16x16/go-down.png deleted file mode 100644 index a74287892e3a8b6135f8c7239530e962021fdb49..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 756 zcmVL}00NXrL_t(I%gvH&NYham#-Foklnb4= za@o|3Y!y0`O(L?SME#IxQR^aQMHY#sU?r(hlTnGLWr>%p6cbXzP$@5r{Lsz3q?1Y` z`(Wgibl#_%{@*@~t(N$`=hMqM&w0-8ea`{@kxx*HxlS)A3yMz2iXpONrZuOKe3UnR z!4jRleAiq{c3i2o_q#(_qav?EwRA4`BPA5HYGqMRB21p*7cBq)_IF4}UA{n>f{jcCNl{kPcsbde3wz4*)8PR0s_`5W7>AxOr@Rn&9TiU@b6{5eP$C z9s4mdgJ1m(lG`vvY61fx^m^K%F=cWd4bN~)GYIk$ zt!4p!uB^Fx0GaWDZUFGQq0&f@aV-|XCo=F=lV7}0k`pDGoMP5G5irkZ?ITVI-0sd~ zGIyvu2ioc?s~rI#DZ6^`gEJFZ~8kU=k=Mtl+)?0000Px#AY({UO#lFTCIA3{ga82g0001h=l}q9FaQARU;qF* zm;eA5aGbhPJOBUy33hNnX8-^I5C8xG5CB}pt-}BS00v@9M??Ss00000`9r&Z00009 za7bBm000tn000tn0p4aGcmMzbQAtEWR5%f}!D&d-aU2KWZ&0s-mJ*Us1VO1U%rsXd zSSG_PgtW{uk143hQd?wRo9X6}_wr(DR?0S-W-}dgF{6cftY*h%g)bx`qx4Nez0jh5 zpY{)h>C|^U57DCP+r6|FiK3g<)nRx+&f+fDBm_h&r6*IGitZ@=HqxS8MIX87GQX!K z=d!hp(pxWCTG*6p#_7e(%Qkfp`Adu#LO!6A803~$iBZWEsQe=? zlsyE8GM|8QZ6IMk^NiBKVKX_~Cy)?lPm~h=kAYo1KiK}k1$gOZuv Date: Wed, 28 May 2014 21:16:41 +0200 Subject: [PATCH 15/15] Apply changes to DOM tree. No validation yet. --HG-- branch : XML Editor --- src/app/dialogs/app/dialogpatternxmledit.cpp | 169 +++++++++++++++++-- src/app/dialogs/app/dialogpatternxmledit.h | 74 ++++++++ src/app/dialogs/app/dialogpatternxmledit.ui | 10 ++ 3 files changed, 236 insertions(+), 17 deletions(-) diff --git a/src/app/dialogs/app/dialogpatternxmledit.cpp b/src/app/dialogs/app/dialogpatternxmledit.cpp index dc1211f7a..21044fd26 100644 --- a/src/app/dialogs/app/dialogpatternxmledit.cpp +++ b/src/app/dialogs/app/dialogpatternxmledit.cpp @@ -45,22 +45,20 @@ DialogPatternXmlEdit::DialogPatternXmlEdit(QWidget *parent, VPattern *xmldoc) { ui->setupUi(this); - xmlmodel = new VXMLTreeView(); - - doc=xmldoc; - root = doc->documentElement(); - // disable edit trigers : - //ui->treeView_main->editTriggers(QTreeView::NoEditTriggers); + this->xmlmodel = new VXMLTreeView(); + this->doc=xmldoc; + this->root = this->doc->documentElement(); + // disable edit trigers (no edition directly on tree) ui->treeView_main->setEditTriggers(QTreeView::NoEditTriggers); ui->treeView_main->setWindowTitle("XML"); - rootNode = xmlmodel->invisibleRootItem(); + this->rootNode = this->xmlmodel->invisibleRootItem(); int drawnum=xmldoc->elementsByTagName("draw").size(); - rootBases = new VXMLTreeElement*[drawnum+1]; - rootBasesNum=1; - rootBases[0]=new VXMLTreeElement("Valentina", VXMLTreeElement::TypeRoot, root, false); + this->rootBases = new VXMLTreeElement*[drawnum+1]; + this->rootBasesNum=1; + this->rootBases[0]=new VXMLTreeElement("Valentina", VXMLTreeElement::TypeRoot, root, false); // TODO : OOM exception. @@ -68,9 +66,9 @@ DialogPatternXmlEdit::DialogPatternXmlEdit(QWidget *parent, VPattern *xmldoc) ui->comboBox_Base_Selection->addItem(tr("All pattern pieces"), QVariant(0)); //rootBases[0]=(QDomNode) doc->DocumentNode; - rootNode->appendRow(standard_base); + this->rootNode->appendRow(standard_base); - ReadNodes(root, standard_base, xmlmodel, false); + this->ReadNodes(this->root, standard_base, this->xmlmodel, false); ui->treeView_main->setModel(xmlmodel); @@ -95,6 +93,64 @@ DialogPatternXmlEdit::DialogPatternXmlEdit(QWidget *parent, VPattern *xmldoc) connect(ui->pushButton_Remove_Node, &QPushButton::clicked, this, &DialogPatternXmlEdit::ButtonDeleteNode); } +bool DialogPatternXmlEdit::CheckChanges(QString &message, QDomNode testRoot) +{ + message="OK"; + return true; +} + +//--------------------------------------------------------------------------------------------------------------------- +bool DialogPatternXmlEdit::ApplyAttributeChange(QDomNode domElement, QString name, QString value) +{ + if (domElement.isElement() == false) + { + return false; + } + domElement.toElement().setAttribute(name,value); + return true; +} + +//--------------------------------------------------------------------------------------------------------------------- +bool DialogPatternXmlEdit::ApplyNodeChange(QDomNode domElement, QString name, QString value) +{ + if (domElement.isElement() == false) + { + return false; + } + domElement.toElement().setNodeValue(value); + domElement.toElement().setTagName(name); + return true; +} + +//--------------------------------------------------------------------------------------------------------------------- +bool DialogPatternXmlEdit::ApplyNodeAdd(QDomNode domElement, VXMLTreeElement* treeElement, QString name, QString value) +{ + QDomElement newnode = domElement.ownerDocument().createElement(name); + //Q_ASSERT(newnode != nullptr); + newnode.setNodeValue(value); + domElement.appendChild(newnode); + treeElement->SetDocNode(newnode); + return true; +} + +bool DialogPatternXmlEdit::ApplyAttributeDelete(QDomNode domElement, QString name) +{ + if (domElement.isElement() == false) + { + return false; + } + domElement.toElement().removeAttribute(name); + return true; +} + +//--------------------------------------------------------------------------------------------------------------------- +bool DialogPatternXmlEdit::ApplyNodeDelete(QDomNode domElement) +{ + QDomNode mother=domElement.parentNode(); + mother.removeChild(domElement); + return true; +} + //--------------------------------------------------------------------------------------------------------------------- void DialogPatternXmlEdit::ButtonDeleteNode() { @@ -431,6 +487,83 @@ void DialogPatternXmlEdit::ButtonApplyChangesClicked() } QMessageBox::information(this, "Changes (not REALLY applied for now)", Changes); // TODO : clear stack and apply + + currentChange=this->changeStackRoot; + // copy DOM + QDomNode newroot = root.cloneNode(true); + + QString name, value; + while (currentChange != nullptr) + { + if (currentChange->type == DialogPatternXmlEdit::ChangeTypeModify) + { + name=(currentChange->changedText == false) ? currentChange->element->GettreeNodeName() + : *currentChange->newText; + value=(currentChange->changedValue == false) ? currentChange->element->GettreeNodeValue() + : *currentChange->newValue; + if (currentChange->element->GetelementType() == VXMLTreeElement::TypeAttr) + { + + this->ApplyAttributeChange(currentChange->element->GetDocNode(), name, value); + } + else if (currentChange->element->GetelementType() == VXMLTreeElement::TypeNode) + { + this->ApplyNodeChange(currentChange->element->GetDocNode(), name, value); + } + else + { + QMessageBox::warning(this, "Error in changes", "Invalid node type"); + return; + } + } + else if (currentChange->type == DialogPatternXmlEdit::ChangeTypeAdd) + { + name=(currentChange->changedText == false) ? currentChange->element->GettreeNodeName() + : *currentChange->newText; + value=(currentChange->changedValue == false) ? currentChange->element->GettreeNodeValue() + : *currentChange->newValue; + if (currentChange->element->GetelementType() == VXMLTreeElement::TypeAttr) + { + this->ApplyAttributeChange(currentChange->element->GetFatherElement()->GetDocNode(), name, value); + } + else if (currentChange->element->GetelementType() == VXMLTreeElement::TypeNode) + { + this->ApplyNodeAdd(currentChange->element->GetFatherElement()->GetDocNode(), currentChange->element, + name, value); + } + else + { + QMessageBox::warning(this, "Error in changes", "Invalid node type"); + return; + } + } + else if (currentChange->type == DialogPatternXmlEdit::ChangeTypeDelete) + { + if (currentChange->element->GetelementType() == VXMLTreeElement::TypeAttr) + { + name=(currentChange->changedText == false) ? currentChange->element->GettreeNodeName() + : *currentChange->newText; + this->ApplyAttributeDelete(currentChange->element->GetDocNode(), name); + } + else if (currentChange->element->GetelementType() == VXMLTreeElement::TypeNode) + { + this->ApplyNodeDelete(currentChange->element->GetDocNode().parentNode()); + } + else + { + QMessageBox::warning(this, "Error in changes", "Invalid node type"); + return; + } + } + currentChange=currentChange->next; + } + QString message; + if (this->CheckChanges(message,newroot) == false) + { + QMessageBox::warning(this, "Error in changes", message); + return; + } + this->doc->FullUpdateTree(); } //--------------------------------------------------------------------------------------------------------------------- @@ -586,17 +719,17 @@ void DialogPatternXmlEdit::ButtonAddSonClicked() { return; } - QString value = QInputDialog::getText(this, tr("Node Value (may be empty)"), - tr("Value:"), QLineEdit::Normal, - "", &ok); + QString value = QInputDialog::getText(this, tr("Node Value (may be empty)"),tr("Value:"), + QLineEdit::Normal,"", &ok); if (ok==false) { return; } - // create element tree but do not add attribute in DOM (will be done in apply). + // create element tree and put father tree element to catch up with DOM tree. QDomNode empty; VXMLTreeElement* tElement = new VXMLTreeElement(name, VXMLTreeElement::TypeNode, empty, true); + tElement->SetFatherElement(this->currentNodeEdited); if (value.isEmpty() == false) { @@ -656,6 +789,8 @@ void DialogPatternXmlEdit::ButtonAddAttributeClicked() // create element tree but do not add attribute in DOM (will be done in apply). VXMLTreeElement* tElement = new VXMLTreeElement(name, VXMLTreeElement::TypeAttr, empty, true); + // set parent node. + tElement->SetFatherElement(this->currentNodeEdited); tElement->SetTreeNodeValue(value); currentNodeEdited->appendRow(tElement); @@ -1063,7 +1198,7 @@ const short int VXMLTreeElement::TypeRoot=3; // ^ VXMLTreeElement::VXMLTreeElement(QString name, int nodetype, QDomNode source, bool editor) : QStandardItem(name), DocNode(source), addedNode(editor), elementType(nodetype), treeNodeValue(""), - treeNodeValueSet(false), treeNodeName(name), selectable(true) + treeNodeValueSet(false), treeNodeName(name), selectable(true), parentnode(nullptr) { if (editor == false) { diff --git a/src/app/dialogs/app/dialogpatternxmledit.h b/src/app/dialogs/app/dialogpatternxmledit.h index 4a8fccdc7..51e3e316f 100644 --- a/src/app/dialogs/app/dialogpatternxmledit.h +++ b/src/app/dialogs/app/dialogpatternxmledit.h @@ -74,8 +74,23 @@ public: * @return current document node */ QDomNode GetDocNode(); + /** + * @brief SetDocNode set tree element DomNode + * @param node + */ + inline void SetDocNode(QDomNode node) + { + this->DocNode=node; + } + inline void SetAddedNode(bool state) {this->addedNode=state;} + inline bool GetAddedNode() {return this->addedNode;} + QString GettreeNodeName(); QString GettreeNodeValue(); + /** + * @brief GettreeNodeValueSet check if value has been set + * @return true if value is set + */ bool GettreeNodeValueSet(); void SetTreeNodeName(QString value); /** @@ -86,12 +101,26 @@ public: bool IsSelectable(); void SetSetlectable(bool value); + inline void SetFatherElement(VXMLTreeElement* parent) + { + this->parentnode=parent; + } + inline VXMLTreeElement* GetFatherElement() + { + return this->parentnode; + } + private: /** * @brief DocNode : link to current document node */ QDomNode DocNode; + /** + * @brief parentnode parent node of this element. If null then the DocNode must be valid + */ + VXMLTreeElement* parentnode; + /** * @brief addedNode : true if node is added by editor (implies empty DocNode) */ @@ -280,6 +309,51 @@ public: */ bool DeleteNodeAndSons(VXMLTreeElement *currentNode, bool onlydeactivate); + /** + * @brief ApplyAttributeChange change or add attribute for node + * @param domElement : node containing attribute + * @param name + * @param value + * @return true on success + */ + bool ApplyAttributeChange(QDomNode domElement, QString name, QString value); + /** + * @brief ApplyNodeChange Change name or text content of node + * @param domElement node + * @param name + * @param value text content of node + * @return true on success + */ + bool ApplyNodeChange(QDomNode domElement, QString name, QString value); + /** + * @brief ApplyNodeAdd add node as child of domElement and set DocNode of treeElement + * @param domElement + * @param treeElement the XMLTreeElement of added node + * @param name + * @param value + * @return true on success + */ + bool ApplyNodeAdd(QDomNode domElement, VXMLTreeElement* treeElement, QString name, QString value); + /** + * @brief ApplyAttributeDelete delete attribute for node + * @param domElement + * @param name attribute name + * @return true on success + */ + bool ApplyAttributeDelete(QDomNode domElement, QString name); + /** + * @brief ApplyNodeDelete delete node domElement + * @param domElement + * @return true on success + */ + bool ApplyNodeDelete(QDomNode domElement); + /** + * @brief CheckChanges Check if changes made are OK + * @param message error message returned if false + * @param testRoot root of DOM to test + * @return true if validated, false otherwise + */ + bool CheckChanges(QString &message, QDomNode testRoot); private slots: void BaseSelectionChanged(int value); void ElementClicked ( const QModelIndex & index ); diff --git a/src/app/dialogs/app/dialogpatternxmledit.ui b/src/app/dialogs/app/dialogpatternxmledit.ui index f6ce6432d..2b4c4a83a 100644 --- a/src/app/dialogs/app/dialogpatternxmledit.ui +++ b/src/app/dialogs/app/dialogpatternxmledit.ui @@ -287,6 +287,16 @@ + + + + Immediate apply + + + false + + +