func:1.添加删除添加功能。
2.添加编辑属性界面 3.删除添加同步到界面未完成。
This commit is contained in:
93
src/attribute_edit.cpp
Normal file
93
src/attribute_edit.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
#include "attribute_edit.h"
|
||||
#include "ui_attribute_edit.h"
|
||||
|
||||
CAttributeEdit::CAttributeEdit(QWidget* parent) : QDialog(parent), ui(new Ui::CAttributeEdit)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
setWindowTitle(u8"属性编辑");
|
||||
setMinimumSize(400, 600);
|
||||
|
||||
connect(ui->btnOk, &QPushButton::clicked, this, &CAttributeEdit::handle_ok);
|
||||
connect(ui->btnCancel, &QPushButton::clicked, this, [&]() {
|
||||
is_ok_ = false;
|
||||
close();
|
||||
});
|
||||
}
|
||||
|
||||
void CAttributeEdit::handle_ok()
|
||||
{
|
||||
property_.clear();
|
||||
int row = table_->rowCount();
|
||||
for (int i = 0; i < row; ++i) {
|
||||
QString key = table_->item(i, 0)->text();
|
||||
QString value = table_->item(i, 1)->text();
|
||||
property_.emplace_back(key.toLocal8Bit().constData(), value.toLocal8Bit().constData());
|
||||
}
|
||||
is_ok_ = true;
|
||||
close();
|
||||
}
|
||||
|
||||
|
||||
CAttributeEdit::~CAttributeEdit()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void CAttributeEdit::showEvent(QShowEvent* event)
|
||||
{
|
||||
show_before();
|
||||
QDialog::showEvent(event);
|
||||
}
|
||||
|
||||
void CAttributeEdit::set_attribute(const Property_t& property)
|
||||
{
|
||||
property_.clear();
|
||||
property_ = property;
|
||||
}
|
||||
|
||||
void CAttributeEdit::get_attribute(Property_t& property)
|
||||
{
|
||||
property.clear();
|
||||
property = property_;
|
||||
}
|
||||
|
||||
void CAttributeEdit::show_before()
|
||||
{
|
||||
init_table();
|
||||
|
||||
for (const auto& item : property_) {
|
||||
int row = table_->rowCount();
|
||||
table_->insertRow(row);
|
||||
|
||||
QTableWidgetItem* pkey = new QTableWidgetItem();
|
||||
pkey->setText(item.key.c_str());
|
||||
pkey->setFlags(pkey->flags() & ~Qt::ItemIsEditable);
|
||||
table_->setItem(row, 0, pkey);
|
||||
|
||||
QTableWidgetItem* pvalue = new QTableWidgetItem();
|
||||
pvalue->setText(item.value.c_str());
|
||||
table_->setItem(row, 1, pvalue);
|
||||
}
|
||||
}
|
||||
|
||||
void CAttributeEdit::init_table()
|
||||
{
|
||||
if (table_ == nullptr) {
|
||||
QStringList list;
|
||||
list << u8"属性" << u8"值";
|
||||
table_ = new QTableWidget();
|
||||
table_->setColumnCount(list.size());
|
||||
table_->setHorizontalHeaderLabels(list);
|
||||
table_->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||||
table_->setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection);
|
||||
table_->setColumnWidth(0, 80);
|
||||
table_->setColumnWidth(1, 200);
|
||||
QHBoxLayout* ly = new QHBoxLayout();
|
||||
ly->addWidget(table_);
|
||||
ui->widget->setLayout(ly);
|
||||
} else {
|
||||
table_->clearContents();
|
||||
table_->setRowCount(0);
|
||||
}
|
||||
}
|
||||
40
src/attribute_edit.h
Normal file
40
src/attribute_edit.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef ATTRIBUTE_EDIT_H
|
||||
#define ATTRIBUTE_EDIT_H
|
||||
|
||||
#include <QDialog>
|
||||
#include <QTableWidget>
|
||||
#include "xml_opr.h"
|
||||
|
||||
namespace Ui {
|
||||
class CAttributeEdit;
|
||||
}
|
||||
|
||||
class CAttributeEdit : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit CAttributeEdit(QWidget* parent = nullptr);
|
||||
~CAttributeEdit();
|
||||
|
||||
protected:
|
||||
void showEvent(QShowEvent* event) override;
|
||||
|
||||
public:
|
||||
void set_attribute(const Property_t& property);
|
||||
void get_attribute(Property_t& property);
|
||||
private:
|
||||
void show_before();
|
||||
void init_table();
|
||||
void handle_ok();
|
||||
|
||||
public:
|
||||
bool is_ok_{};
|
||||
|
||||
private:
|
||||
Ui::CAttributeEdit* ui;
|
||||
Property_t property_{};
|
||||
QTableWidget* table_{};
|
||||
};
|
||||
|
||||
#endif // ATTRIBUTE_EDIT_H
|
||||
62
src/attribute_edit.ui
Normal file
62
src/attribute_edit.ui
Normal file
@@ -0,0 +1,62 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>CAttributeEdit</class>
|
||||
<widget class="QDialog" name="CAttributeEdit">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>318</width>
|
||||
<height>618</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Dialog</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QWidget" name="widget" native="true">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<spacer name="horizontalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="btnOk">
|
||||
<property name="text">
|
||||
<string>确认</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="btnCancel">
|
||||
<property name="text">
|
||||
<string>取消</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
@@ -3,7 +3,7 @@
|
||||
CXmlOpr::CXmlOpr() = default;
|
||||
CXmlOpr::~CXmlOpr() = default;
|
||||
|
||||
bool CXmlOpr::open(const std::string &xml_path)
|
||||
bool CXmlOpr::open(const std::string& xml_path)
|
||||
{
|
||||
if (doc_.LoadFile(xml_path.c_str()) != tinyxml2::XML_SUCCESS) {
|
||||
return false;
|
||||
@@ -31,22 +31,20 @@ bool CXmlOpr::parse_xml(std::vector<tinyxml2::XMLElement*>& vec)
|
||||
|
||||
if (parent_node_ == nullptr) {
|
||||
parent_node_ = doc_.FirstChildElement(item.c_str());
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
parent_node_ = parent_node_->FirstChildElement(item.c_str());
|
||||
}
|
||||
}
|
||||
vec.clear();
|
||||
element* purpose_node = parent_node_->FirstChildElement(opr_base_.the_node.c_str());
|
||||
while (purpose_node)
|
||||
{
|
||||
Element_t* purpose_node = parent_node_->FirstChildElement(opr_base_.the_node.c_str());
|
||||
while (purpose_node) {
|
||||
vec.push_back(purpose_node);
|
||||
purpose_node = purpose_node->NextSiblingElement();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void CXmlOpr::insert_brother_node(element* brother, element* newer)
|
||||
void CXmlOpr::insert_brother_node(Element_t* brother, Element_t* newer)
|
||||
{
|
||||
if (!brother || !newer) {
|
||||
return;
|
||||
@@ -54,12 +52,12 @@ void CXmlOpr::insert_brother_node(element* brother, element* newer)
|
||||
parent_node_->InsertAfterChild(brother, newer);
|
||||
}
|
||||
|
||||
element* CXmlOpr::copy_element(element* ele)
|
||||
Element_t* CXmlOpr::copy_element(Element_t* ele)
|
||||
{
|
||||
if (!ele) {
|
||||
return nullptr;
|
||||
}
|
||||
element* ret = doc_.NewElement(ele->Name());
|
||||
Element_t* ret = doc_.NewElement(ele->Name());
|
||||
const auto* attribute = ele->FirstAttribute();
|
||||
while (attribute) {
|
||||
ret->SetAttribute(attribute->Name(), attribute->Value());
|
||||
@@ -68,6 +66,11 @@ element* CXmlOpr::copy_element(element* ele)
|
||||
return ret;
|
||||
}
|
||||
|
||||
void CXmlOpr::del_element(Element_t* ele)
|
||||
{
|
||||
parent_node_->DeleteChild(ele);
|
||||
}
|
||||
|
||||
bool CXmlOpr::save()
|
||||
{
|
||||
auto ret = doc_.SaveFile(xml_path_.c_str());
|
||||
@@ -76,3 +79,32 @@ bool CXmlOpr::save()
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void CXmlOpr::get_key_value(Element_t* ele, Property_t& vec)
|
||||
{
|
||||
if (ele == nullptr) {
|
||||
return;
|
||||
}
|
||||
vec.clear();
|
||||
const auto* attribute = ele->FirstAttribute();
|
||||
while (attribute) {
|
||||
vec.emplace_back(attribute->Name(), attribute->Value());
|
||||
attribute = attribute->Next();
|
||||
}
|
||||
}
|
||||
|
||||
void CXmlOpr::key_value_to_element(Element_t* ele, const Property_t& vec)
|
||||
{
|
||||
if (ele == nullptr) {
|
||||
return;
|
||||
}
|
||||
for (const auto& data : vec) {
|
||||
ele->SetAttribute(data.key.c_str(), data.value.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
SKeyValue::SKeyValue(const char* pkey, const char* pvalue)
|
||||
{
|
||||
key = std::string(pkey);
|
||||
value = std::string(pvalue);
|
||||
}
|
||||
|
||||
@@ -4,9 +4,17 @@
|
||||
#include <string>
|
||||
#include <tinyxml2.h>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include "../public_def.h"
|
||||
|
||||
typedef tinyxml2::XMLElement element;
|
||||
struct SKeyValue {
|
||||
SKeyValue(const char* pkey, const char* pvalue);
|
||||
std::string key;
|
||||
std::string value;
|
||||
};
|
||||
|
||||
typedef tinyxml2::XMLElement Element_t;
|
||||
typedef std::vector<SKeyValue> Property_t;
|
||||
class CXmlOpr
|
||||
{
|
||||
public:
|
||||
@@ -14,18 +22,23 @@ public:
|
||||
~CXmlOpr();
|
||||
|
||||
public:
|
||||
bool open(const std::string& xml_path);
|
||||
void set_baseinfo(const OprBase& base);
|
||||
bool parse_xml(std::vector<element*>& vec);
|
||||
void insert_brother_node(element* brother, element* newer);
|
||||
element* copy_element(element* ele);
|
||||
bool save();
|
||||
bool open(const std::string& xml_path);
|
||||
void set_baseinfo(const OprBase& base);
|
||||
bool parse_xml(std::vector<Element_t*>& vec);
|
||||
void insert_brother_node(Element_t* brother, Element_t* newer);
|
||||
Element_t* copy_element(Element_t* ele);
|
||||
void del_element(Element_t* ele);
|
||||
bool save();
|
||||
|
||||
public:
|
||||
void get_key_value(Element_t* ele, Property_t& vec);
|
||||
void key_value_to_element(Element_t* ele, const Property_t& vec);
|
||||
|
||||
private:
|
||||
tinyxml2::XMLDocument doc_{};
|
||||
OprBase opr_base_{};
|
||||
std::string xml_path_{};
|
||||
element* parent_node_{};
|
||||
Element_t* parent_node_{};
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user