QtNovelUI/WordsIDE/srcedit_defaulttext.cpp

118 lines
3.2 KiB
C++
Raw Normal View History

#include "srcedit_defaulttext.h"
#include "keywordshighlighter.h"
2022-11-18 23:47:32 +00:00
#include <QComboBox>
#include <QFont>
#include <QGridLayout>
2022-11-18 05:43:27 +00:00
#include <QMenu>
#include <QSpinBox>
2023-02-26 14:44:00 +00:00
#include <QTextStream>
2022-11-17 08:26:05 +00:00
using namespace Components;
2022-11-18 05:43:27 +00:00
using namespace Enhancement;
2022-11-18 23:47:32 +00:00
using namespace Core;
2022-11-17 08:26:05 +00:00
class Impl_EditError : public MakeTools::EditException {
public:
Impl_EditError(const QString &title, const QString &reason) {
this->t = title;
this->r = reason;
buffer = r.toLocal8Bit();
}
2022-11-17 08:26:05 +00:00
// exception interface
public:
virtual const char *what() const override { return buffer; }
2022-11-17 08:26:05 +00:00
// ParseException interface
public:
virtual QString reason() const override { return r; }
virtual QString title() const override { return t; }
2022-11-17 08:26:05 +00:00
private:
QString r, t;
QByteArray buffer;
};
2022-11-17 08:26:05 +00:00
QString DefaultTextEdit::file_suffix = "txt";
2022-11-17 08:26:05 +00:00
DefaultTextEdit::DefaultTextEdit(QObject *parent) : MakeTools::FilePresent(parent), edit_square(new QTextEdit) {}
2022-11-17 08:26:05 +00:00
2023-03-17 13:58:38 +00:00
void DefaultTextEdit::jumpTo(const QList<QString> &path)
2022-11-17 08:26:05 +00:00
{
}
2023-03-17 13:58:38 +00:00
MakeTools::FilePresent::Features DefaultTextEdit::features()
{
return Features();
}
2023-03-17 13:58:38 +00:00
void DefaultTextEdit::applySetting(const QString &name, Core::AppCore *core)
2022-11-17 08:26:05 +00:00
{
2023-02-26 14:44:00 +00:00
name_store = name;
}
2023-03-17 13:58:38 +00:00
QString DefaultTextEdit::name() const
2023-01-01 05:10:31 +00:00
{
2023-02-26 14:44:00 +00:00
return name_store;
2023-01-01 05:10:31 +00:00
}
2023-03-17 13:58:38 +00:00
void DefaultTextEdit::load(const QFileInfo &target_file)
{
2023-02-26 14:44:00 +00:00
if(!target_file.exists())
throw new Impl_EditError("解析错误", QString("指定文件未找到:%1").arg(target_file.absoluteFilePath()));
2023-02-26 14:44:00 +00:00
target_file_path = target_file.canonicalFilePath();
QFile bin(target_file_path);
if(!bin.open(QIODevice::ReadOnly | QIODevice::Text))
throw new Impl_EditError("解析错误", QString("指定文件无法打开:%1").arg(target_file.absoluteFilePath()));
2023-02-26 14:44:00 +00:00
QTextStream tin(&bin);
tin.setCodec("UTF-8");
auto content = tin.readAll();
this->edit_square->setText(content);
2022-11-17 08:26:05 +00:00
}
2023-03-17 13:58:38 +00:00
void DefaultTextEdit::saveAs(const QString &path)
2022-11-25 01:18:54 +00:00
{
2023-02-26 14:44:00 +00:00
if(!path.isEmpty())
target_file_path = path;
2022-11-25 01:18:54 +00:00
2023-02-26 14:44:00 +00:00
QFile bout(target_file_path);
if(!bout.open(QIODevice::WriteOnly|QIODevice::Text))
throw new std::exception(QString("指定文件无法打开:%1").arg(target_file_path).toLocal8Bit());
2022-11-25 01:18:54 +00:00
2023-02-26 14:44:00 +00:00
QTextStream tout(&bout);
tout.setCodec("UTF-8");
tout << this->edit_square->toPlainText();
tout.flush();
2022-11-17 08:26:05 +00:00
}
2023-03-17 13:58:38 +00:00
QString DefaultTextEdit::relativeTargetPath(const QDir &base) const
2022-11-17 08:26:05 +00:00
{
2023-02-26 14:44:00 +00:00
if(target_file_path == "")
return "";
2022-11-17 08:26:05 +00:00
2023-02-26 14:44:00 +00:00
return base.relativeFilePath(target_file_path);
2022-11-17 08:26:05 +00:00
}
bool DefaultTextEdit::isModified() const { return true; }
2022-11-18 23:47:32 +00:00
QString DefaultTextEdit::absoluteTargetPath() const { return target_file_path; }
2022-11-18 23:47:32 +00:00
QWidget *DefaultTextEdit::widget() const { return edit_square; }
2022-11-18 23:47:32 +00:00
void DefaultTextEditFactory::create(const QFileInfo &target) {
QFile base_f(target.canonicalFilePath());
if (!base_f.exists()) {
throw new Impl_EditError("新建错误", "指定路径不合法,文件不存在:" + target.absoluteFilePath());
}
2022-11-18 23:47:32 +00:00
if (!base_f.open(QIODevice::WriteOnly | QIODevice::Text))
throw new Impl_EditError("新建错误", QString("指定文件无法建立:" + target.absoluteFilePath()));
base_f.write(" ");
base_f.flush();
base_f.close();
2022-11-18 23:47:32 +00:00
}