commit 532ce2d0ae4f0c005ff29250313985730d47e596 Author: codeboss <2422523675@qq.com> Date: Wed Mar 6 23:09:20 2024 +0800 初始化仓库 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fab7372 --- /dev/null +++ b/.gitignore @@ -0,0 +1,73 @@ +# This file is used to ignore files which are generated +# ---------------------------------------------------------------------------- + +*~ +*.autosave +*.a +*.core +*.moc +*.o +*.obj +*.orig +*.rej +*.so +*.so.* +*_pch.h.cpp +*_resource.rc +*.qm +.#* +*.*# +core +!core/ +tags +.DS_Store +.directory +*.debug +Makefile* +*.prl +*.app +moc_*.cpp +ui_*.h +qrc_*.cpp +Thumbs.db +*.res +*.rc +/.qmake.cache +/.qmake.stash + +# qtcreator generated files +*.pro.user* + +# xemacs temporary files +*.flc + +# Vim temporary files +.*.swp + +# Visual Studio generated files +*.ib_pdb_index +*.idb +*.ilk +*.pdb +*.sln +*.suo +*.vcproj +*vcproj.*.*.user +*.ncb +*.sdf +*.opensdf +*.vcxproj +*vcxproj.* + +# MinGW generated files +*.Debug +*.Release + +# Python byte code +*.pyc + +# Binaries +# -------- +*.dll +*.exe + diff --git a/DataStream.pro b/DataStream.pro new file mode 100644 index 0000000..974fc9a --- /dev/null +++ b/DataStream.pro @@ -0,0 +1,38 @@ +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++11 + +# You can make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +msvc { + QMAKE_CXXFLAGS += /utf-8 + QMAKE_CFLAGS += /utf-8 +} + +SOURCES += \ + dataprototype.cpp \ + datatypeconfigration.cpp \ + main.cpp \ + mapcore.cpp \ + moduleinterface.cpp \ + moduleprototype.cpp \ + moduletreeview.cpp \ + widget.cpp + +HEADERS += \ + dataprototype.h \ + datatypeconfigration.h \ + mapcore.h \ + moduleinterface.h \ + moduleprototype.h \ + moduletreeview.h \ + widget.h + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/dataprototype.cpp b/dataprototype.cpp new file mode 100644 index 0000000..c0bd620 --- /dev/null +++ b/dataprototype.cpp @@ -0,0 +1,150 @@ +#include "dataprototype.h" + +RuntimeItem::RuntimeItem(AssembleType t) : type_store(t) {} + +RuntimeItem::RuntimeItem(const RuntimeItem &other) : RuntimeItem(other.baseType()) { + this->listener_collection.append(other.listener_collection); +} + +std::shared_ptr RuntimeItem::add(std::shared_ptr inst) const +{ + if(this->listener_collection.contains(inst)) + return this->shared_from_this(); + + auto ninst = std::const_pointer_cast(this->clone()); + ninst->listener_collection.append(inst); + itemNotify([ninst, this](std::shared_ptr p) { p->itemHasbeenUpdated(ninst, this->shared_from_this()); }); + return ninst; +} + +std::shared_ptr RuntimeItem::del(std::shared_ptr inst) const +{ + if(!this->listener_collection.contains(inst)) + return this->shared_from_this(); + + auto ninst = std::const_pointer_cast(this->clone()); + ninst->listener_collection.removeAll(inst); + itemNotify([ninst, this](std::shared_ptr p) { p->itemHasbeenUpdated(ninst, this->shared_from_this()); }); + return ninst; +} + +void RuntimeItem::itemNotify(std::function)> proc) const +{ + for(auto < : listener_collection) + proc(lt); +} + +AssembleType RuntimeItem::baseType() const { return type_store; } + +qulonglong RuntimeItem::hashCode() const { return (qulonglong)this; } + +std::shared_ptr RuntimeItem::clone() const{ + return std::make_shared(*this); +} + +TypeTemplate::TypeTemplate() : RuntimeItem(AssembleType::DATAS_ASSEMBLE) {} + +TypeTemplate::TypeTemplate(const TypeTemplate &other) +: RuntimeItem(other) +{ + this->name_store = other.name_store; + this->type_store = other.type_store; + this->io_type = other.io_type; + this->comps_store = other.comps_store; +} + +QString TypeTemplate::name() const { return this->name_store; } + +std::shared_ptr TypeTemplate::setName(const QString &val) const{ + auto ninst = std::static_pointer_cast(std::const_pointer_cast(this->clone())); + ninst->name_store = val; + ninst->itemNotify([ninst, this](std::shared_ptr p) { p->itemHasbeenUpdated(ninst, this->shared_from_this()); }); + return ninst; +} + +QString TypeTemplate::type() const { return this->type_store; } + +std::shared_ptr TypeTemplate::setType(const QString &val) const +{ + auto ninst = std::static_pointer_cast(std::const_pointer_cast(this->clone())); + ninst->name_store = val; + ninst->itemNotify([ninst, this](std::shared_ptr p) { p->itemHasbeenUpdated(ninst, this->shared_from_this()); }); + return ninst; +} + +DataOritation TypeTemplate::oritation() const { return io_type; } + +std::shared_ptr TypeTemplate::setOritation(DataOritation t) const +{ + auto ninst = std::static_pointer_cast(std::const_pointer_cast(this->clone())); + ninst->io_type = t; + ninst->itemNotify([ninst, this](std::shared_ptr p) { p->itemHasbeenUpdated(ninst, this->shared_from_this()); }); + return ninst; +} + +std::shared_ptr TypeTemplate::clone() const +{ + return std::make_shared(*this); +} + +DownAccess::DownAccess(std::shared_ptr peer) : runtime_item(peer) {} + +DownAccess::DownAccess(const DownAccess &other) +{ + this->runtime_item = other.runtime_item; + this->children_store = other.children_store; +} + +std::shared_ptr DownAccess::itemPeer() const { return this->runtime_item; } + +QList > DownAccess::members() const { return this->children_store; } + +std::shared_ptr DownAccess::append(std::shared_ptr child_inst) const{ + if(this->children_store.contains(child_inst)) + return this->shared_from_this(); + + auto ninst = std::const_pointer_cast(this->clone()); + ninst->children_store.append(child_inst); + itemPeer()->itemNotify( + [this, child_inst](std::shared_ptr inst) { + inst->memberHasbeenAppended(*this->itemPeer(), child_inst->itemPeer()); + }); + + return ninst; +} + +std::shared_ptr DownAccess::remove(std::shared_ptr child_inst) const { + if(!this->children_store.contains(child_inst)) + return this->shared_from_this(); + + auto ninst = std::const_pointer_cast(this->clone()); + itemPeer()->itemNotify( + [this, child_inst](std::shared_ptr inst) { + inst->memberAboutToBeRemove(*this->itemPeer(), child_inst->itemPeer()); + }); + + ninst->children_store.removeAll(child_inst); + return ninst; +} + +std::shared_ptr DownAccess::replace(std::shared_ptr current, std::shared_ptr previous) const +{ + return this->remove(previous)->append(current); +} + +std::shared_ptr DownAccess::clone() const { + return std::make_shared(*this); +} + +UpAccess::UpAccess(std::shared_ptr parent, std::shared_ptr down):runtime_downward(down), parent_ptr(parent){} + +UpAccess::UpAccess(const UpAccess &other) { runtime_downward = other.runtime_downward; } + +std::shared_ptr UpAccess::downwardPeers() const +{ + return this->runtime_downward; +} + +std::shared_ptr UpAccess::parent() const { return parent_ptr; } + +std::shared_ptr UpAccess::clone() const { return std::make_shared(*this); } diff --git a/dataprototype.h b/dataprototype.h new file mode 100644 index 0000000..b84a46c --- /dev/null +++ b/dataprototype.h @@ -0,0 +1,137 @@ +#ifndef DATAPROTOTYPE_H +#define DATAPROTOTYPE_H + +#include +#include + +class RuntimeItem; + +/** + * @brief 实体观察接口 + */ +class ItemListener{ +public: + virtual ~ItemListener() = default; + + virtual void itemHasbeenUpdated(std::shared_ptr current, std::shared_ptr previous) = 0; + + virtual void memberHasbeenAppended(const RuntimeItem &parent, std::shared_ptr inst) = 0; + virtual void memberAboutToBeRemove(const RuntimeItem &parent, std::shared_ptr inst) = 0; + virtual void memberHasbeenReplaced(const RuntimeItem &parent, std::shared_ptr current, std::shared_ptr previous) = 0; +}; + + +/** + * @brief 聚合体类型 + */ +enum class AssembleType { + DATAS_ASSEMBLE, // 数据聚合体 + MODULES_UNIT, // 模块单元 +}; + +/** + * @brief 内存实例接口 + */ +class RuntimeItem : public std::enable_shared_from_this { +public: + explicit RuntimeItem(AssembleType t); + explicit RuntimeItem(const RuntimeItem & other); + + std::shared_ptr add(std::shared_ptr inst) const; + std::shared_ptr del(std::shared_ptr inst) const; + void itemNotify(std::function)> proc) const; + + AssembleType baseType() const; + virtual qulonglong hashCode() const; + virtual std::shared_ptr clone() const; + +private: + AssembleType type_store; + QList> listener_collection; +}; + +/** + * @brief 下行实体访问接口 + */ +class DownAccess : public std::enable_shared_from_this{ +public: + explicit DownAccess(std::shared_ptr peer); + explicit DownAccess(const DownAccess &other); + + template + std::shared_ptr typePeer() const { + return std::dynamic_pointer_cast(itemPeer()); + } + std::shared_ptr itemPeer() const; + QList> members() const; + + std::shared_ptr append(std::shared_ptr child_inst) const; + std::shared_ptr remove(std::shared_ptr child_inst) const; + std::shared_ptr replace(std::shared_ptr current, std::shared_ptr previous) const; + + virtual std::shared_ptr clone() const; + +private: + std::shared_ptr runtime_item; + QList> children_store; +}; + +/** + * @brief 上行访问接口 + */ +class UpAccess : public std::enable_shared_from_this { +public: + explicit UpAccess(std::shared_ptr parent, std::shared_ptr down); + explicit UpAccess(const UpAccess &other); + + template + std::shared_ptr typePeer() const { + return std::dynamic_pointer_cast(itemPeer()); + } + std::shared_ptr downwardPeers() const; + std::shared_ptr parent() const; + virtual std::shared_ptr clone() const; + +private: + std::shared_ptr runtime_downward; + std::shared_ptr parent_ptr; +}; + +/** + * @brief 定义数据传输方向 + */ +enum class DataOritation { + INPUT, // 引入 + OUTPUT, // 输出 + CONSUME, // 引入消耗 + GENERATE, // 引出生成 + UNDEFINED, // 未定义 +}; +/** + * @brief 数据接口 + */ +class TypeTemplate : public RuntimeItem +{ +private: + QString name_store = "foo"; + QString type_store = "整形"; + DataOritation io_type = DataOritation::UNDEFINED; + QHash, QString>> comps_store; + +public: + explicit TypeTemplate(); + explicit TypeTemplate(const TypeTemplate & other); + + QString name() const; + std::shared_ptr setName(const QString &val) const; + + QString type() const; + std::shared_ptr setType(const QString &val) const; + + DataOritation oritation() const; + std::shared_ptr setOritation(DataOritation t) const; + + virtual std::shared_ptr clone() const; +}; + +#endif // DATAPROTOTYPE_H diff --git a/datatypeconfigration.cpp b/datatypeconfigration.cpp new file mode 100644 index 0000000..0710dd0 --- /dev/null +++ b/datatypeconfigration.cpp @@ -0,0 +1,85 @@ +#include "datatypeconfigration.h" + +#include +#include + +DataConfigration::DataConfigration(MapCore &core, std::shared_ptr target, QWidget *parent) + : QWidget(parent), + core_bind(core), + target_bind(target), + member_present(new QTableView(this)), + member_model(new QStandardItemModel(this)), + preview_present(new QTreeView(this)), + preview_model(new QStandardItemModel(this)), + append(new QPushButton("添加", this)), + remove(new QPushButton("删除", this)) { + auto layout = new QVBoxLayout(this); + auto splitter = new QSplitter(this); + layout->addWidget(splitter); + + auto widget = new QWidget(this); + splitter->addWidget(widget); + splitter->addWidget(preview_present); + + auto xlayout = new QVBoxLayout(widget); + xlayout->addWidget(member_present); + xlayout->addWidget(append); + xlayout->addWidget(remove); + + member_present->setModel(member_model); + preview_present->setModel(preview_model); + + xlayout->setMargin(2); + layout->setMargin(2); + xlayout->setSpacing(2); + layout->setSpacing(2); + + this->member_model->setHorizontalHeaderLabels(QStringList() << "成员名称" << "成员类型"); + this->preview_model->setHorizontalHeaderLabels(QStringList() << "成员名称" << "成员类型"); + + present_member(target); + present_struct(target); + + connect(this->append, &QPushButton::clicked, [=](){ + auto ninst = std::make_shared(std::make_shared()); + auto target_x = target_bind->append(ninst); + this->core_bind.refreshRoot(target_bind, target_x); + this->target_bind = target_x; + + this->member_model->removeRows(0, member_model->rowCount()); + this->preview_model->removeRows(0, preview_model->rowCount()); + + present_member(target_bind); + present_struct(target_bind); + + }); +} + +void DataConfigration::present_member(std::shared_ptr base) +{ + auto list = base->members(); + for(auto &d : list){ + QList row; + row << new QStandardItem(d->typePeer()->name()); + row << new QStandardItem(d->typePeer()->type()); + this->member_model->appendRow(row); + } +} + +void DataConfigration::present_struct(std::shared_ptr base, QStandardItem *target) +{ + auto list = base->members(); + for(auto &d : list){ + QList row; + row << new QStandardItem(d->typePeer()->name()); + row << new QStandardItem(d->typePeer()->type()); + if(!target) + this->preview_model->appendRow(row); + else + target->appendRow(row); + + row.first()->setEditable(false); + row.last()->setEditable(false); + present_struct(d, row.first()); + } +} diff --git a/datatypeconfigration.h b/datatypeconfigration.h new file mode 100644 index 0000000..d0d55c1 --- /dev/null +++ b/datatypeconfigration.h @@ -0,0 +1,32 @@ +#ifndef DATATYPECONFIGRATION_H +#define DATATYPECONFIGRATION_H + +#include +#include +#include +#include +#include +#include "mapcore.h" +#include "dataprototype.h" + +class DataConfigration : public QWidget +{ + Q_OBJECT +public: + explicit DataConfigration(MapCore &core, std::shared_ptr target, QWidget *parent = nullptr); + +private: + MapCore &core_bind; + std::shared_ptr target_bind; + QTableView *const member_present; + QStandardItemModel *const member_model; + QTreeView *const preview_present; + QStandardItemModel *const preview_model; + + QPushButton *const append, *const remove; + + void present_member(std::shared_ptr base); + void present_struct(std::shared_ptr base, QStandardItem *target = nullptr); +}; + +#endif // DATATYPECONFIGRATION_H diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..a817efc --- /dev/null +++ b/main.cpp @@ -0,0 +1,26 @@ +#include "widget.h" + +#include +#include "mapcore.h" +#include "datatypeconfigration.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); +// Widget w; +// w.show(); + MapCore core; + auto target = std::make_shared(std::make_shared()); + core.setRoot(target); + auto tnode0 = std::make_shared(std::make_shared()); + auto tnode1 = std::make_shared(std::make_shared()); + auto tnode2 = std::make_shared(std::make_shared()); + tnode0 = tnode0->append(tnode1); + target = target->append(tnode0); + target = target->append(tnode2); + + DataConfigration dialog(core, target); + dialog.show(); + + return a.exec(); +} diff --git a/mapcore.cpp b/mapcore.cpp new file mode 100644 index 0000000..fb8e71a --- /dev/null +++ b/mapcore.cpp @@ -0,0 +1,84 @@ +#include "mapcore.h" + +MapCore::MapCore() +{ + +} + +std::shared_ptr MapCore::root() const{ return root_bind; } + +void MapCore::setRoot(std::shared_ptr root) { this->root_bind = root; } + +std::shared_ptr MapCore::refreshRoot(std::shared_ptr old, std::shared_ptr _new){ + MapVisitor visitor(*this); + + auto links = visitor.findLink(old->itemPeer()); + std::function(std::shared_ptr, std::shared_ptr)> replace_cascade = + [&replace_cascade, this](std::shared_ptr current_up, std::shared_ptr current_new) -> std::shared_ptr { + auto pinst_up = current_up->parent(); + if(!pinst_up) + return current_new; + + auto pinst = pinst_up->downwardPeers(); + auto new_pinst = pinst->replace(current_up->downwardPeers(), current_new); + return replace_cascade(pinst_up, new_pinst); + }; + + this->root_bind = replace_cascade(links, _new); + return this->root_bind; +} + +MapVisitor::MapVisitor(MapCore &base) : core_bind(base) {} + +std::shared_ptr MapVisitor::findTarget(std::shared_ptr node) const{ + return find_target(core_bind.root(), node); +} + +std::shared_ptr MapVisitor::findLink(std::shared_ptr node) const +{ + return this->find_link(nullptr, core_bind.root(), node); +} + +QList > MapVisitor::collect(std::function)> proc) const{ + return collect(core_bind.root(), proc); +} + +std::shared_ptr MapVisitor::find_link(std::shared_ptr pinst, std::shared_ptr handle, std::shared_ptr node) const +{ + if(handle->itemPeer() == node) + return std::make_shared(pinst, handle); + + auto target_ptr = std::make_shared(pinst, handle); + for(auto &hc : handle->members()){ + auto result = find_link(target_ptr, hc, node); + if(result) + return result; + } + + return nullptr; +} + +std::shared_ptr MapVisitor::find_target(std::shared_ptr handle, std::shared_ptr node) const { + if (handle->itemPeer() == node) { + return handle; + } + + for (auto &it : handle->members()) { + auto result = find_target(it, node); + if (result) + return result; + } + + return nullptr; +} + +QList > MapVisitor::collect(std::shared_ptr handle, std::function)> proc) const { + QList> retlist; + if(proc(handle)) + retlist.append(handle); + + for(auto &c : handle->members()) + retlist.append(collect(c, proc)); + + return retlist; +} diff --git a/mapcore.h b/mapcore.h new file mode 100644 index 0000000..1afb506 --- /dev/null +++ b/mapcore.h @@ -0,0 +1,53 @@ +#ifndef MAPCORE_H +#define MAPCORE_H + +#include +#include "moduleprototype.h" + + +class MapCore +{ +public: + MapCore(); + + std::shared_ptr root() const; + void setRoot(std::shared_ptr root); + std::shared_ptr refreshRoot(std::shared_ptr old, std::shared_ptr _new); + + QList> dataTypes() const; + void typeAppend(std::shared_ptr type); + void typeRemove(std::shared_ptr type); + void typeUpdate(std::shared_ptr type); + +private: + std::shared_ptr root_bind = nullptr; +}; + +class MapVisitor{ +public: + explicit MapVisitor(MapCore &base); + /** + * @brief 获取指定节点的访问句柄 + * @param node + * @return + */ + std::shared_ptr findTarget(std::shared_ptr node) const; + + std::shared_ptr findLink(std::shared_ptr node) const; + + /** + * @brief 获取满足指定条件的节点 + * @param proc + * @return + */ + QList> collect(std::function)> proc) const; + +private: + MapCore &core_bind; + + std::shared_ptr find_link(std::shared_ptr pinst, std::shared_ptr handle, std::shared_ptr node) const; + std::shared_ptr find_target(std::shared_ptr handle, std::shared_ptr node) const; + QList> collect(std::shared_ptr handle, std::function)> proc) const; +}; + +#endif // MAPCORE_H diff --git a/moduleinterface.cpp b/moduleinterface.cpp new file mode 100644 index 0000000..0f77991 --- /dev/null +++ b/moduleinterface.cpp @@ -0,0 +1,7 @@ +#include "moduleinterface.h" + +ModuleInterface::ModuleInterface(MapCore &core, std::shared_ptr target, QWidget *parent) + : QWidget(parent), core_bind(core), target_bind(target) +{ + +} diff --git a/moduleinterface.h b/moduleinterface.h new file mode 100644 index 0000000..fa9d9cd --- /dev/null +++ b/moduleinterface.h @@ -0,0 +1,21 @@ +#ifndef MODULEINTERFACE_H +#define MODULEINTERFACE_H + +#include +#include "moduleprototype.h" +#include "mapcore.h" + +class ModuleInterface : public QWidget +{ + Q_OBJECT +public: + explicit ModuleInterface(MapCore &core, std::shared_ptr target, QWidget *parent = nullptr); + +signals: + +private: + MapCore &core_bind; + std::shared_ptr target_bind; +}; + +#endif // MODULEINTERFACE_H diff --git a/moduleprototype.cpp b/moduleprototype.cpp new file mode 100644 index 0000000..f33f4e4 --- /dev/null +++ b/moduleprototype.cpp @@ -0,0 +1,27 @@ +#include "moduleprototype.h" + +ModulePrototype::ModulePrototype() + : RuntimeItem(AssembleType::MODULES_UNIT) +{ + +} + +ModulePrototype::ModulePrototype(const ModulePrototype &other) + : RuntimeItem(other.baseType()) +{ + this->module_name = other.module_name; +} + +QString ModulePrototype::name() const { return this->module_name; } + +std::shared_ptr ModulePrototype::setName(const QString &val) const{ + auto ninst = std::static_pointer_cast(std::const_pointer_cast(this->clone())); + ninst->module_name = val; + ninst->itemNotify([ninst, this](std::shared_ptr p) { p->itemHasbeenUpdated(ninst, this->shared_from_this()); }); + return ninst; +} + +std::shared_ptr ModulePrototype::clone() const +{ + return std::make_shared(*this); +} diff --git a/moduleprototype.h b/moduleprototype.h new file mode 100644 index 0000000..c14d65f --- /dev/null +++ b/moduleprototype.h @@ -0,0 +1,28 @@ +#ifndef MODULEPROTOTYPE_H +#define MODULEPROTOTYPE_H + +#include +#include +#include +#include "dataprototype.h" + +/** + * @brief 模块原型 + */ +class ModulePrototype : public RuntimeItem +{ +private: + QString module_name; + +public: + explicit ModulePrototype(); + explicit ModulePrototype(const ModulePrototype &other); + + QString name() const; + std::shared_ptr setName(const QString &val) const; + + virtual std::shared_ptr clone() const override; +}; + + +#endif // MODULEPROTOTYPE_H diff --git a/moduletreeview.cpp b/moduletreeview.cpp new file mode 100644 index 0000000..8df10a6 --- /dev/null +++ b/moduletreeview.cpp @@ -0,0 +1,6 @@ +#include "moduletreeview.h" + +ModuleTreeView::ModuleTreeView() +{ + +} diff --git a/moduletreeview.h b/moduletreeview.h new file mode 100644 index 0000000..6f80a9a --- /dev/null +++ b/moduletreeview.h @@ -0,0 +1,11 @@ +#ifndef MODULETREEVIEW_H +#define MODULETREEVIEW_H + + +class ModuleTreeView +{ +public: + ModuleTreeView(); +}; + +#endif // MODULETREEVIEW_H diff --git a/widget.cpp b/widget.cpp new file mode 100644 index 0000000..7350ff4 --- /dev/null +++ b/widget.cpp @@ -0,0 +1,11 @@ +#include "widget.h" + +Widget::Widget(QWidget *parent) + : QWidget(parent) +{ +} + +Widget::~Widget() +{ +} + diff --git a/widget.h b/widget.h new file mode 100644 index 0000000..0f77e91 --- /dev/null +++ b/widget.h @@ -0,0 +1,14 @@ +#ifndef WIDGET_H +#define WIDGET_H + +#include + +class Widget : public QWidget +{ + Q_OBJECT + +public: + Widget(QWidget *parent = nullptr); + ~Widget(); +}; +#endif // WIDGET_H