#include "srcedit_defaulttext.h" #include "keywordshighlighter.h" #include #include #include #include #include #include #include using namespace Components; using namespace Enhancement; using namespace Core; using namespace Config; QString DefaultTextEdit::file_suffix = "txt"; DefaultTextEdit::DefaultTextEdit(DocumentsManager *mgr, const Route &path, QObject *parent) : FilePresent(parent), mgr_inst(mgr), path_store(path), edit_square(new QTextEdit) {} void DefaultTextEdit::jumpTo(const QList &path) { } Route DefaultTextEdit::accessPath() const { return path_store; } #include "manager_docs.h" void DefaultTextEdit::beforeClose() { mgr_inst->save(); } DocumentsManager *DefaultTextEdit::docsManager() const { return mgr_inst; } void DefaultTextEdit::applySetting(const QString &name) { name_store = name; } QString DefaultTextEdit::name() const { return name_store; } void DefaultTextEdit::load(const QFileInfo &target_file) { if(!target_file.exists()) throw new SimpleException("解析错误", QString("指定文件未找到:%1").arg(target_file.absoluteFilePath())); target_file_path = target_file.canonicalFilePath(); QFile bin(target_file_path); if(!bin.open(QIODevice::ReadOnly | QIODevice::Text)) throw new SimpleException("解析错误", QString("指定文件无法打开:%1").arg(target_file.absoluteFilePath())); QTextStream tin(&bin); tin.setCodec("UTF-8"); auto content = tin.readAll(); this->edit_square->setText(content); } void DefaultTextEdit::saveAs(const QString &path) { if(!path.isEmpty()) target_file_path = path; QFile bout(target_file_path); if(!bout.open(QIODevice::WriteOnly|QIODevice::Text)) throw new std::exception(QString("指定文件无法打开:%1").arg(target_file_path).toLocal8Bit()); QTextStream tout(&bout); tout.setCodec("UTF-8"); tout << this->edit_square->toPlainText(); tout.flush(); } QString DefaultTextEdit::relativeTargetPath(const QDir &base) const { if(target_file_path == "") return ""; return base.relativeFilePath(target_file_path); } bool DefaultTextEdit::isModified() const { return true; } QString DefaultTextEdit::absoluteTargetPath() const { return target_file_path; } QWidget *DefaultTextEdit::widget() const { return edit_square; } void DefaultTextEditFactory::create(const QFileInfo &target) { QFile base_f(target.canonicalFilePath()); if (!base_f.exists()) { throw new SimpleException("新建错误", "指定路径不合法,文件不存在:" + target.absoluteFilePath()); } if (!base_f.open(QIODevice::WriteOnly | QIODevice::Text)) throw new SimpleException("新建错误", QString("指定文件无法建立:" + target.absoluteFilePath())); base_f.write(" "); base_f.flush(); base_f.close(); }