From 1489dc6a1cb3fc90d40fc30a43967b4ed1577f3f Mon Sep 17 00:00:00 2001 From: dismine Date: Fri, 16 May 2014 19:39:46 +0300 Subject: [PATCH] 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