2025-06-23 10:41:33 +08:00
|
|
|
|
|
|
|
|
|
#pragma execution_character_set("utf-8")
|
|
|
|
|
#include "CodeDisplayView.h"
|
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
#include "Base.h"
|
|
|
|
|
#include "SysManager.h"
|
|
|
|
|
#include <QFileinfo>
|
|
|
|
|
#include <QProcess>
|
|
|
|
|
#include <QDesktopServices>
|
|
|
|
|
|
|
|
|
|
CodeDisplayView::CodeDisplayView(QWidget* parent)
|
|
|
|
|
: QDialog(parent)
|
|
|
|
|
{
|
|
|
|
|
_webView = new WebEngineView(QUrl(BaseUrl + "#/codedisplay"), this);
|
|
|
|
|
|
|
|
|
|
QVBoxLayout* layout = new QVBoxLayout();
|
|
|
|
|
layout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
|
setLayout(layout);
|
|
|
|
|
layout->addWidget(_webView);
|
|
|
|
|
|
|
|
|
|
_eventModule = new SignalEventModule("CodeDisplayView");
|
|
|
|
|
QObject::connect(_eventModule, &SignalEventModule::onmessage, this, &CodeDisplayView::onMessage);
|
|
|
|
|
_webView->addEventModule(_eventModule);
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
|
_frameless = std::move(std::unique_ptr<QFramelessHelper>(new QFramelessHelper(this, _webView, true, (QDialog*)this)));
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CodeDisplayView::~CodeDisplayView()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
void CodeDisplayView::Close()
|
|
|
|
|
{
|
|
|
|
|
this->close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CodeDisplayView::onMessage(const std::string& eventName, json& parameter)
|
|
|
|
|
{
|
|
|
|
|
LOG(INFO) << CommonHelper::utf8ToStdString("CodeDisplayView receive message.......");
|
|
|
|
|
LOG(INFO) << eventName;
|
|
|
|
|
LOG(INFO) << CommonHelper::jsonToString(parameter);
|
|
|
|
|
TRY{
|
|
|
|
|
if (eventName == "close") {
|
|
|
|
|
Close();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (eventName == "opened") {
|
|
|
|
|
updateFileList();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (eventName == "open-file")
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//源码浏览打开
|
2025-06-23 10:41:33 +08:00
|
|
|
|
openCurFile(parameter);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (eventName == "save-code-file")
|
|
|
|
|
{
|
|
|
|
|
saveCodeFile(parameter);
|
|
|
|
|
}
|
|
|
|
|
else if (eventName == "open-directory")
|
|
|
|
|
{
|
|
|
|
|
openDir(parameter);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
CATCH(parameter);
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
|
|
|
|
|
bool CodeDisplayView::nativeEvent(const QByteArray& eventType, void* message, qintptr* result)
|
|
|
|
|
#else
|
|
|
|
|
bool CodeDisplayView::nativeEvent(const QByteArray& eventType, void* message, long* result)
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
if (_frameless && _frameless->nativeEvent(eventType, message, result))
|
|
|
|
|
return true;
|
|
|
|
|
return QDialog::nativeEvent(eventType, message, result);
|
|
|
|
|
}
|
|
|
|
|
void CodeDisplayView::updateFileList()
|
|
|
|
|
{
|
|
|
|
|
//std::vector<std::string> clist;
|
|
|
|
|
//for (auto j : cfileList)
|
|
|
|
|
//{
|
|
|
|
|
// QFileInfo fileInfo(j);
|
|
|
|
|
// clist.push_back(fileInfo.fileName().toStdString());
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
json data;
|
|
|
|
|
data["CFileList"] = json::array();
|
|
|
|
|
data["NetFileList"] = json::array();
|
|
|
|
|
for (auto j : cfileList)
|
|
|
|
|
{
|
|
|
|
|
QFileInfo fileInfo(j);
|
|
|
|
|
data["CFileList"].push_back(fileInfo.fileName().toStdString());
|
|
|
|
|
}
|
|
|
|
|
for (auto i : netfileList)
|
|
|
|
|
{
|
|
|
|
|
QFileInfo fileInfo(i);
|
|
|
|
|
data["NetFileList"].push_back(fileInfo.fileName().toStdString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_eventModule->send("update-filelist", data);
|
2025-06-23 18:01:09 +08:00
|
|
|
|
LOG(INFO) << CommonHelper::utf8ToStdString("向前端发送 update-filelist message.......");
|
2025-06-23 10:41:33 +08:00
|
|
|
|
LOG(INFO) << CommonHelper::jsonToString(data);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//打开选定文件
|
2025-06-23 10:41:33 +08:00
|
|
|
|
void CodeDisplayView::openCurFile(json& parameter)
|
|
|
|
|
{
|
|
|
|
|
auto data = parameter["data"];
|
|
|
|
|
QString filePath = CommonHelper::utf8ToQString(data["filePath"]);
|
|
|
|
|
|
|
|
|
|
SysManager& sysMgr = GetSysManager();
|
|
|
|
|
QString fileFullPath = sysMgr._codeGeneratePath + "/" + filePath;
|
|
|
|
|
// QTextCodec* codec = QTextCodec::codecForName("utf-8");
|
|
|
|
|
QFile file(fileFullPath);
|
|
|
|
|
if (file.open(QIODevice::ReadOnly))
|
|
|
|
|
{
|
|
|
|
|
QByteArray array = file.readAll();
|
|
|
|
|
QString str = QString::fromLocal8Bit(array);
|
|
|
|
|
parameter["response"] = CommonHelper::qstringToUtf8(str);
|
|
|
|
|
file.close();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-06-23 18:01:09 +08:00
|
|
|
|
LOG(ERROR) << CommonHelper::utf8ToStdString("CodeDisplayView::openCurFile:openFile failed ");
|
2025-06-23 10:41:33 +08:00
|
|
|
|
parameter["response"] = "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeDisplayView::openDir(json& parameter)
|
|
|
|
|
{
|
|
|
|
|
auto data = parameter["data"];
|
|
|
|
|
QString filename = CommonHelper::utf8ToQString(data["path"]);
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//先用系统配置中的生成代码路径来确认位置
|
2025-06-23 10:41:33 +08:00
|
|
|
|
SysManager& sysMgr = GetSysManager();
|
|
|
|
|
QString fileFullPath = sysMgr._codeGeneratePath + "/" + filename;
|
|
|
|
|
|
2025-06-23 18:01:09 +08:00
|
|
|
|
// QString folderPath = "D:/测试db/DBConfig.dbp";
|
2025-06-23 10:41:33 +08:00
|
|
|
|
QString cmd = QString("explorer.exe /select,%1").arg(fileFullPath.replace('/', '\\'));
|
|
|
|
|
QProcess::startDetached(cmd);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CodeDisplayView::saveCodeFile(json& parameter)
|
|
|
|
|
{
|
|
|
|
|
auto data = parameter["data"];
|
|
|
|
|
QString content = CommonHelper::utf8ToQString(data["content"]);
|
|
|
|
|
QString filename = CommonHelper::utf8ToQString(data["filePath"]);
|
|
|
|
|
|
|
|
|
|
SysManager& sysMgr = GetSysManager();
|
|
|
|
|
QString fileFullPath = sysMgr._codeGeneratePath + "/" + filename;
|
|
|
|
|
QFile sFile(fileFullPath);
|
|
|
|
|
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//以重写、只写方式打开文件(运行到此时,文件已经存在目录中)
|
2025-06-23 10:41:33 +08:00
|
|
|
|
if (!sFile.open(QIODevice::Truncate | QIODevice::WriteOnly | QIODevice::Text))
|
|
|
|
|
printf("Open jsonfile failed!\n");
|
|
|
|
|
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//写入文件内容
|
2025-06-23 10:41:33 +08:00
|
|
|
|
sFile.write(content.toStdString().c_str());
|
2025-06-23 18:01:09 +08:00
|
|
|
|
//关闭文件
|
2025-06-23 10:41:33 +08:00
|
|
|
|
sFile.close();
|
|
|
|
|
}
|