stuff-from-scratch/apps/notes_tk/text_editor/TextEditorController.cpp
2023-12-21 09:18:44 +00:00

58 lines
1.2 KiB
C++

#include "TextEditorController.h"
#include "File.h"
TextEditorController::TextEditorController()
: mModel(TextEditorModel::Create()),
mSavePath(),
mLoadPath()
{
}
Ptr<TextEditorController> TextEditorController::Create()
{
return std::make_unique<TextEditorController>();
}
void TextEditorController::SetContent(const String& content)
{
mModel->GetDocument()->SetContent(content);
}
String TextEditorController::GetContent() const
{
return mModel->GetDocument()->GetContent();
}
void TextEditorController::OnSave()
{
if(mSavePath.empty()) return;
File outfile(mSavePath);
outfile.open(File::AccessMode::Write);
outfile.writeText(mModel->GetDocument()->GetContent());
}
void TextEditorController::OnLoad()
{
if(mLoadPath.empty()) return;
File infile(mLoadPath);
infile.open(File::AccessMode::Read);
mModel->GetDocument()->SetContent(infile.readText());
}
void TextEditorController::SetSavePath(const std::filesystem::path& path)
{
mSavePath = path;
}
void TextEditorController::SetLoadPath(const std::filesystem::path& path)
{
mLoadPath = path;
}
void TextEditorController::OnClear()
{
mModel->GetDocument()->Clear();
}