92 lines
3.1 KiB
C
92 lines
3.1 KiB
C
|
#ifndef COMNTOOLS_H
|
||
|
#define COMNTOOLS_H
|
||
|
|
||
|
#include "opstream.h"
|
||
|
#include <QStandardItemModel>
|
||
|
#include <functional>
|
||
|
|
||
|
namespace Tools {
|
||
|
|
||
|
template<class BaseType>
|
||
|
class ModelSyncs
|
||
|
{
|
||
|
public:
|
||
|
ModelSyncs(QStandardItemModel *model_base, std::function<bool(const BaseType &n, QStandardItem *p)> _equal,
|
||
|
std::function<void(const BaseType &n, QStandardItem *p)> sync_proc)
|
||
|
: model_present(model_base), sync_process(sync_proc), compare_process(_equal){}
|
||
|
|
||
|
void presentSync(std::function<QList<BaseType>(const BaseType &pnode)> items_peak)
|
||
|
{
|
||
|
auto default_pdata = BaseType();
|
||
|
auto datas = items_peak(default_pdata);
|
||
|
|
||
|
Operate::OpStream<QStandardItem*>([this](int &cnt, int idx)->QStandardItem*{
|
||
|
cnt = model_present->rowCount();
|
||
|
return model_present->item(idx);
|
||
|
}).filter([&datas, this](QStandardItem *const &it)->bool{
|
||
|
for(auto &d : datas)
|
||
|
if(compare_process(d, it))
|
||
|
return false;
|
||
|
return true;
|
||
|
}).forEach([this](QStandardItem *const &it){
|
||
|
model_present->removeRow(it->row());
|
||
|
});
|
||
|
|
||
|
for (auto idx = 0; idx < datas.size(); ++idx) {
|
||
|
if (idx >= model_present->rowCount()) {
|
||
|
auto ins = new QStandardItem();
|
||
|
ins->setEditable(false);
|
||
|
model_present->appendRow(ins);
|
||
|
}
|
||
|
|
||
|
auto &data = datas[idx];
|
||
|
auto item = model_present->item(idx);
|
||
|
sync_process(data, item);
|
||
|
|
||
|
layer_items_sync(data, item, items_peak);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
private:
|
||
|
QStandardItemModel *model_present;
|
||
|
std::function<void(const BaseType &n, QStandardItem *p)> sync_process;
|
||
|
std::function<bool(const BaseType &n, QStandardItem *p)> compare_process;
|
||
|
|
||
|
void layer_items_sync(const BaseType &data, QStandardItem *pnode,
|
||
|
std::function<QList<BaseType>(const BaseType &pnode)> items_peak){
|
||
|
auto datas = items_peak(data);
|
||
|
|
||
|
Operate::OpStream<QStandardItem*>([pnode](int &cnt, int idx)->QStandardItem*{
|
||
|
cnt = pnode->rowCount();
|
||
|
return pnode->child(idx);
|
||
|
}).filter([&datas, this](QStandardItem *const &it)->bool{
|
||
|
for(auto &d : datas)
|
||
|
if(compare_process(d, it))
|
||
|
return false;
|
||
|
return true;
|
||
|
}).forEach([pnode](QStandardItem *const &it){
|
||
|
pnode->removeRow(it->row());
|
||
|
});
|
||
|
|
||
|
for (auto idx = 0; idx < datas.size(); ++idx) {
|
||
|
if (idx >= pnode->rowCount()) {
|
||
|
auto ins = new QStandardItem();
|
||
|
ins->setEditable(false);
|
||
|
pnode->appendRow(ins);
|
||
|
}
|
||
|
|
||
|
auto &data = datas[idx];
|
||
|
auto item = pnode->child(idx);
|
||
|
sync_process(data, item);
|
||
|
|
||
|
layer_items_sync(data, item, items_peak);
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
|
||
|
#endif // COMNTOOLS_H
|