1 Star 0 Fork 0

吕冬/resource-manager

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
mainwindow.cpp 8.46 KB
一键复制 编辑 原始数据 按行查看 历史
#include "mainwindow.h"
#include <ui_mainwindow.h>
#include "resourcecenter.h"
#include "createworkdialog.h"
#include <iostream>
#include <fstream>
#include "createresourcedialog.h"
#include "profile.h"
#include <cereal/archives/json.hpp>
#include <QFileDialog>
#include <QMessageBox>
#include <QCloseEvent>
#define CONFIG_FILE_NAME "config.ldb"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, m_pMainDialogForm(new Ui::MainWindow)
{
m_pMainDialogForm->setupUi(this);
m_pScrollLayout = new QVBoxLayout;
m_pScrollLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);
m_pMainDialogForm->scrollAreaWidgetContents->setLayout(m_pScrollLayout);
connect(m_pMainDialogForm->actionload, &QAction::triggered, this, &MainWindow::on_action_load_clicked);
connect(m_pMainDialogForm->actionsave, &QAction::triggered, this, &MainWindow::on_action_save_clicked);
connect(m_pMainDialogForm->actionnew , &QAction::triggered, this, &MainWindow::on_action_new_clicked);
loadConfigFile();
}
MainWindow::~MainWindow()
{
saveConfigFile();
delete m_pMainDialogForm;
}
void MainWindow::addResourceWidget(const std::string &name)
{
ResourceCenter::instance()->addResource(name);
m_pMainDialogForm->groupBox->addResourceWidget(ResourceCenter::instance()->getResource(name));
}
int MainWindow::assignResourceToWork(const std::string &res, const std::string &work)
{
auto work_itor = m_pWorkWidgetsMapTable.find(work);
if (work_itor == m_pWorkWidgetsMapTable.cend())
{
return -1;
}
auto res_ptr = ResourceCenter::instance()->getResource(res);
if(res_ptr == nullptr)
{
return -2;
}
work_itor->second->addResourceWidget(res_ptr);
return 0;
}
void MainWindow::collectAssignedResources(std::set<std::string> &res_list, std::map<std::string, std::string> &res_to_work)
{
auto project_count = m_pScrollLayout->count();
for(auto i = 0; i < project_count; i++)
{
auto widget_ptr = m_pScrollLayout->itemAt(i)->widget();
if(widget_ptr == nullptr)
{
continue;
}
std::string class_name = widget_ptr->metaObject()->className();
std::set<std::string> work_res;
if (class_name == "DragWidget")
{
auto project_ptr = reinterpret_cast<DragWidget*>(widget_ptr);
project_ptr->collectResources(work_res);
for(auto& res : work_res)
{
res_to_work.emplace(res, project_ptr->title().toLocal8Bit().data());
}
res_list.insert(work_res.begin(), work_res.end());
}
}
}
void MainWindow::collectUnassignedResources(std::set<std::string> &res_list)
{
m_pMainDialogForm->groupBox->collectResources(res_list);
}
void MainWindow::collectWorks(std::set<std::string> &work_set)
{
auto project_count = m_pScrollLayout->count();
for(auto i = 0; i < project_count; i++)
{
auto widget_ptr = m_pScrollLayout->itemAt(i)->widget();
if(widget_ptr == nullptr)
{
continue;
}
std::string class_name = widget_ptr->metaObject()->className();
if (class_name == "DragWidget")
{
auto project_ptr = reinterpret_cast<DragWidget*>(widget_ptr);
work_set.emplace(project_ptr->title().toLocal8Bit().data());
}
}
}
void MainWindow::clear()
{
/*remove and delete work widgets*/
auto l = m_pScrollLayout;
QLayoutItem *child;
while ((child = l->takeAt(0)) != nullptr) {
DragWidget* dragwidget = reinterpret_cast<DragWidget*>(child->widget());
dragwidget->setParent(nullptr);
dragwidget->clear();
delete child;
}
m_pWorkWidgetsMapTable.clear();
m_pMainDialogForm->groupBox->clear();
ResourceCenter::instance()->clear();
}
void MainWindow::loadProfile(const QString& profile_name)
{
Profile load_profile;
std::ifstream is(profile_name.toLocal8Bit().data());
try{
cereal::JSONInputArchive ar(is);
ar(load_profile);
}
catch(cereal::Exception e)
{
QMessageBox msgbox(this);
msgbox.setText("无效的文件");
msgbox.exec();
return;
}
if (load_profile.m_mapAssignments.size()==0
&& load_profile.m_projects.size() == 0
&& load_profile.m_resources.size() == 0)
{
return;
}
m_strCurProfile = profile_name;
clear();
for (const auto& res : load_profile.m_resources)
{
addResourceWidget(res);
}
for (const auto& works : load_profile.m_projects)
{
addProjectWidget(works);
}
for (const auto& assign_itor : load_profile.m_mapAssignments)
{
assignResourceToWork(assign_itor.first, assign_itor.second);
}
}
void MainWindow::saveProfile(const QString &profile_name)
{
Profile new_profile;
std::set<std::string> &res_list = new_profile.m_resources;
std::set<std::string> &work_set = new_profile.m_projects;
std::map<std::string, std::string> &res_to_work = new_profile.m_mapAssignments;
/*collect unassigned resources*/
collectWorks(work_set);
collectUnassignedResources(res_list);
/*collect assigned resources*/
collectAssignedResources(res_list, res_to_work);
// if(res_list.size()==0 && work_set.size() == 0 && res_to_work.size()==0)
// {
// return;
// }
std::cout << "resources:" <<std::endl;
for(auto& res : res_list)
{
std::cout << res << std::endl;
}
std::cout << "works:" <<std::endl;
for(auto& work : work_set)
{
std::cout << work << std::endl;
}
std::cout << "work assignment:" << std::endl;
for(auto& assign_ref : res_to_work)
{
std::cout << assign_ref.first << "->" << assign_ref.second << std::endl;
}
std::ofstream os(profile_name.toLocal8Bit().data());
cereal::JSONOutputArchive ar(os);
ar(new_profile);
}
struct ConfigFile
{
std::string last_profile;
template<typename Archive>
void serialize(Archive &ar)
{
ar(last_profile);
}
};
void MainWindow::loadConfigFile()
{
ConfigFile cfg_file;
std::ifstream is(CONFIG_FILE_NAME);
try{
cereal::JSONInputArchive ar(is);
ar(cfg_file);
}
catch(cereal::Exception e)
{
return;
}
loadProfile(QString::fromLocal8Bit(cfg_file.last_profile.c_str()));
}
void MainWindow::saveConfigFile()
{
if (m_strCurProfile.isEmpty())
{
return;
}
ConfigFile cfg_file;
cfg_file.last_profile = m_strCurProfile.toLocal8Bit().data();
std::ofstream os(CONFIG_FILE_NAME);
cereal::JSONOutputArchive ar(os);
ar(cfg_file);
}
void MainWindow::closeEvent(QCloseEvent *event)
{
auto save_dlg = QMessageBox::question(this, "是否要保存", "是否要保存");
if (save_dlg == QMessageBox::Yes)
{
if (m_strCurProfile.isEmpty())
{
on_action_save_clicked();
}
else
{
saveProfile(m_strCurProfile);
}
}
event->accept();
}
void MainWindow::addProjectWidget(const std::string &name)
{
DragWidget* widget = new DragWidget;
widget->setTitle(QString::fromLocal8Bit(name.c_str()));
m_pScrollLayout->addWidget(widget);
m_pWorkWidgetsMapTable.emplace(name, widget);
}
void MainWindow::on_pushButton_clicked()
{
CreateWorkDialog dlg;
dlg.show();
dlg.exec();
if (!dlg.workName().isEmpty())
{
addProjectWidget(dlg.workName().toLocal8Bit().data());
}
}
void MainWindow::on_pushButton_2_clicked()
{
CreateResourceDialog dlg;
dlg.show();
dlg.exec();
auto res_name(dlg.resourceName());
if (!res_name.isEmpty())
{
ResourceCenter::instance()->addResource(res_name.toLocal8Bit().data());
m_pMainDialogForm->groupBox->addResourceWidget(ResourceCenter::instance()->getResource(res_name.toLocal8Bit().data()));
}
}
void MainWindow::on_action_new_clicked()
{
clear();
on_action_save_clicked();
}
void MainWindow::on_action_open_clicked()
{
}
void MainWindow::on_action_close_clicked()
{
}
void MainWindow::on_action_load_clicked()
{
QFileDialog fdlg;
QString file_name = std::move(fdlg.getOpenFileName(this, "选择一个配置文件", "./", "Json文件(*.json)" ));
loadProfile(file_name);
}
void MainWindow::on_action_save_clicked()
{
QFileDialog fdlg;
QString file_name = std::move(fdlg.getSaveFileName(this, "选择一个配置文件", "./", "Json文件(*.json)" ));
m_strCurProfile = file_name;
saveProfile(file_name);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/flvd/resource-manager.git
git@gitee.com:flvd/resource-manager.git
flvd
resource-manager
resource-manager
master

搜索帮助