118 lines
3.2 KiB
C++
118 lines
3.2 KiB
C++
#include "srcedit_defaulttext.h"
|
|
#include "keywordshighlighter.h"
|
|
#include <QComboBox>
|
|
#include <QFont>
|
|
#include <QGridLayout>
|
|
#include <QMenu>
|
|
#include <QSpinBox>
|
|
#include <QTextStream>
|
|
|
|
using namespace Components;
|
|
using namespace Enhancement;
|
|
using namespace Core;
|
|
|
|
class Impl_EditError : public MakeTools::EditException {
|
|
public:
|
|
Impl_EditError(const QString &title, const QString &reason) {
|
|
this->t = title;
|
|
this->r = reason;
|
|
buffer = r.toLocal8Bit();
|
|
}
|
|
|
|
// exception interface
|
|
public:
|
|
virtual const char *what() const override { return buffer; }
|
|
|
|
// ParseException interface
|
|
public:
|
|
virtual QString reason() const override { return r; }
|
|
virtual QString title() const override { return t; }
|
|
|
|
private:
|
|
QString r, t;
|
|
QByteArray buffer;
|
|
};
|
|
|
|
QString DefaultTextEdit::file_suffix = "txt";
|
|
|
|
DefaultTextEdit::DefaultTextEdit(QObject *parent) : MakeTools::FilePresent(parent), edit_square(new QTextEdit) {}
|
|
|
|
void DefaultTextEdit::jumpTo(const QList<QString> &path)
|
|
{
|
|
|
|
}
|
|
|
|
MakeTools::FilePresent::Features DefaultTextEdit::features()
|
|
{
|
|
return Features();
|
|
}
|
|
|
|
void DefaultTextEdit::applySetting(const QString &name, Core::AppCore *core)
|
|
{
|
|
name_store = name;
|
|
}
|
|
|
|
QString DefaultTextEdit::name() const
|
|
{
|
|
return name_store;
|
|
}
|
|
|
|
void DefaultTextEdit::load(const QFileInfo &target_file)
|
|
{
|
|
if(!target_file.exists())
|
|
throw new Impl_EditError("解析错误", 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 Impl_EditError("解析错误", 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 Impl_EditError("新建错误", "指定路径不合法,文件不存在:" + target.absoluteFilePath());
|
|
}
|
|
|
|
if (!base_f.open(QIODevice::WriteOnly | QIODevice::Text))
|
|
throw new Impl_EditError("新建错误", QString("指定文件无法建立:" + target.absoluteFilePath()));
|
|
|
|
base_f.write(" ");
|
|
base_f.flush();
|
|
base_f.close();
|
|
}
|