diff --git a/src/entities/PathComponent.cpp b/src/entities/PathComponent.cpp index c2432dce..052095c5 100644 --- a/src/entities/PathComponent.cpp +++ b/src/entities/PathComponent.cpp @@ -1,9 +1,14 @@ #include "entities/PathComponent.h" +#include +#include +#include + #include "common/SpdLogger.h" #include "common/RecourceHelper.h" #include "utils/TransformPath.h" #include "utils/OsgUtils.h" +#include "utils/FileUtils.h" #include "entities/Entity.h" #include "workspace/WorkSpace.h" #include "workspace/Timestep.h" @@ -107,7 +112,17 @@ void PathComponent::SetPath(const QString& path) { transformPath_->deleteLater(); } - const QString filePath = QString("%1/%2").arg(RecourceHelper::Get().GetBasePath()).arg(path); + const QString workPath = GetEntity()->GetWorkspace()->GetDir(); + QFileInfo fileInfo(path); + const QString filePath = QString("%1/%2").arg(workPath).arg(fileInfo.fileName()); + if (!FileUtils::CopyFileToPath(path, filePath, true)) { + LOG_ERROR("PathComponent::SetPath: Failed to copy file to workspace"); + QMessageBox::critical(nullptr, "Error", "Failed to copy file to workspace"); + return; + } + LOG_INFO("PathComponent::SetPath: {}", workPath.toStdString().c_str()); + + transformPath_ = TransformPath::LoadFromFile(filePath, this); path_ = path; } diff --git a/src/translations/Dyt_zh_CN.qm b/src/translations/Dyt_zh_CN.qm index 7c138064..7c0161f3 100644 Binary files a/src/translations/Dyt_zh_CN.qm and b/src/translations/Dyt_zh_CN.qm differ diff --git a/src/translations/Dyt_zh_CN.ts b/src/translations/Dyt_zh_CN.ts index 612d9e94..f4dbcaaf 100644 --- a/src/translations/Dyt_zh_CN.ts +++ b/src/translations/Dyt_zh_CN.ts @@ -728,7 +728,7 @@ Delete - + 删除 @@ -849,12 +849,12 @@ QFilePathEdit - + Open File - + All Files (*) @@ -1715,7 +1715,7 @@ new workspace - 新建 + 新建 diff --git a/src/ui/PropertyBrowser/qtpropertybrowserutils.cpp b/src/ui/PropertyBrowser/qtpropertybrowserutils.cpp index c78faf21..5138f16e 100644 --- a/src/ui/PropertyBrowser/qtpropertybrowserutils.cpp +++ b/src/ui/PropertyBrowser/qtpropertybrowserutils.cpp @@ -704,10 +704,11 @@ QFilePathEdit::QFilePathEdit(QWidget* parent) : lt->setStretch(1, 0); setLayout(lt); - /* m_initialvalue = m_timeedit->dateTime(); - connect(m_timeedit, &QTimeEdit::dateTimeChanged, this, &QtDateTimeEdit::dateTimeChanged); - connect(m_timeedit, &QTimeEdit::dateTimeChanged, this, [&] { m_button->setEnabled(m_initialvalue != m_timeedit->dateTime()); }); - connect(m_button, &QPushButton::clicked, this, [&] { m_timeedit->setDateTime(m_initialvalue); });*/ + + connect(m_stringEdit, &QLineEdit::textChanged, this, &QFilePathEdit::textChanged); + connect(m_stringEdit, &QLineEdit::textEdited, this, &QFilePathEdit::textEdited); + connect(m_stringEdit, &QLineEdit::textChanged, this, [&] { m_button->setEnabled(m_initialvalue != m_stringEdit->text()); }); + connect(m_stringEdit, &QLineEdit::textEdited, this, [&] { m_button->setEnabled(m_initialvalue != m_stringEdit->text()); }); connect(m_button, &QPushButton::clicked, this, &QFilePathEdit::onFileSelect); setFocusProxy(m_stringEdit); } @@ -731,9 +732,9 @@ void QFilePathEdit::setInitialText(const QString& val) { void QFilePathEdit::onFileSelect() { QString filePath = QFileDialog::getOpenFileName(this, tr("Open File"), m_initialvalue, tr("All Files (*)")); if (!filePath.isEmpty()) { - m_stringEdit->setText(filePath); m_fileName = QFileInfo(filePath).fileName(); m_initialvalue = filePath; + m_stringEdit->setText(filePath); } } diff --git a/src/ui/PropertyBrowser/qtpropertybrowserutils_p.h b/src/ui/PropertyBrowser/qtpropertybrowserutils_p.h index c4f31bc8..c12c433b 100644 --- a/src/ui/PropertyBrowser/qtpropertybrowserutils_p.h +++ b/src/ui/PropertyBrowser/qtpropertybrowserutils_p.h @@ -299,7 +299,8 @@ public: void setInitialText(const QString&); Q_SIGNALS: - void dateTimeChanged(const QDateTime&); + void textChanged(QString); + void textEdited(QString); protected: void onFileSelect(); diff --git a/src/utils/FileUtils.cpp b/src/utils/FileUtils.cpp new file mode 100644 index 00000000..78a6094f --- /dev/null +++ b/src/utils/FileUtils.cpp @@ -0,0 +1,30 @@ +#include "utils/FileUtils.h" + +#include +#include + +#include "common/SpdLogger.h" + + +bool FileUtils::CopyFileToPath(const QString& sourceDir, QString toDir, bool coverFileIfExist) { + toDir.replace("\\", "/"); + LOG_INFO("copy file from {} to {}", sourceDir.toStdString(), toDir.toStdString()); + if (sourceDir == toDir) { + return true; + } + if (!QFile::exists(sourceDir)) { + return false; + } + QDir* createfile = new QDir; + bool exist = createfile->exists(toDir); + if (exist) { + if (coverFileIfExist) { + createfile->remove(toDir); + } + } + + if (!QFile::copy(sourceDir, toDir)) { + return false; + } + return true; +} diff --git a/src/utils/FileUtils.h b/src/utils/FileUtils.h new file mode 100644 index 00000000..2b6d1a86 --- /dev/null +++ b/src/utils/FileUtils.h @@ -0,0 +1,9 @@ +#pragma once + +#include + + +class FileUtils { +public: + static bool CopyFileToPath(const QString& sourceDir, QString toDir, bool coverFileIfExist); +}; \ No newline at end of file diff --git a/src/utils/TransformPath.cpp b/src/utils/TransformPath.cpp index bb071b45..9092ed34 100644 --- a/src/utils/TransformPath.cpp +++ b/src/utils/TransformPath.cpp @@ -25,7 +25,8 @@ TransformPath* TransformPath::LoadFromFile(const QString& path, QObject* parent) QFile file(path); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { - LOG_WARN("Cannot open file for reading: {}", file.errorString().toStdString()); + LOG_WARN("Cannot open file for reading: path:{} error:{}", + path.toStdString(), file.errorString().toStdString()); return nullptr; } diff --git a/src/workspace/WorkSpace.cpp b/src/workspace/WorkSpace.cpp index 4934959f..384f2942 100644 --- a/src/workspace/WorkSpace.cpp +++ b/src/workspace/WorkSpace.cpp @@ -1,6 +1,8 @@ #include "workspace/WorkSpace.h" #include +#include +#include #include "workspace/WorkSpaceXMLParse.h" #include "workspace/WorkSpaceXMLWrite.h" @@ -29,6 +31,10 @@ WorkSpace::WorkSpace(const QString& path, QObject* parent) uuid_ = QUuid::createUuid().toString(); } +const QString WorkSpace::GetDir() const { + QFileInfo info(path_); + return info.absolutePath(); +} void WorkSpace::AddEntity(Entity* entity) { if (nullptr == entity) { diff --git a/src/workspace/WorkSpace.h b/src/workspace/WorkSpace.h index a8bf718d..044ab9ab 100644 --- a/src/workspace/WorkSpace.h +++ b/src/workspace/WorkSpace.h @@ -29,6 +29,8 @@ public: inline const QString& GetPath() const { return path_; } + const QString GetDir() const; + inline void SetUUid(const QString& uuid) { uuid_ = uuid; } diff --git a/src/workspace/WorkSpaceManager.cpp b/src/workspace/WorkSpaceManager.cpp index b75ba818..b8190f4f 100644 --- a/src/workspace/WorkSpaceManager.cpp +++ b/src/workspace/WorkSpaceManager.cpp @@ -59,6 +59,7 @@ void WorkSpaceManager::SaveDefaultWorkspace() { return; } + current->Save(); const QString path = current->GetPath(); QSettings settings(iniFile, QSettings::IniFormat); settings.setValue("workspace/path", path);