QtNovelUI/WordsIDE/srcedit_defaulttext.cpp

124 lines
3.6 KiB
C++
Raw Permalink Normal View History

#include "srcedit_defaulttext.h"
#include "keywordshighlighter.h"
2022-11-18 23:47:32 +00:00
#include <QComboBox>
#include <QFont>
#include <QGridLayout>
2023-09-10 05:28:52 +00:00
#include <QLabel>
2022-11-18 05:43:27 +00:00
#include <QMenu>
2023-09-10 05:28:52 +00:00
#include <QPushButton>
#include <QSpinBox>
2023-02-26 14:44:00 +00:00
#include <QTextStream>
2023-08-27 14:09:46 +00:00
#include <libConfig.h>
2022-11-17 08:26:05 +00:00
2023-12-24 04:49:58 +00:00
class GenException : public std::exception{
private:
QString exception_reason;
public:
GenException(const QString &msg):exception_reason(msg){}
// exception interface
public:
virtual const char *what() const noexcept override{
return exception_reason.toLocal8Bit();
}
};
using namespace Presents;
2022-11-18 05:43:27 +00:00
using namespace Enhancement;
2022-11-18 23:47:32 +00:00
using namespace Core;
2023-08-27 14:09:46 +00:00
using namespace Config;
2022-11-17 08:26:05 +00:00
QString DefaultTextEdit::file_suffix = "txt";
2022-11-17 08:26:05 +00:00
2023-08-27 14:09:46 +00:00
DefaultTextEdit::DefaultTextEdit(DocumentsManager *mgr, const Route &path, QObject *parent)
: FilePresent(parent), mgr_inst(mgr), path_store(path), 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-08-27 14:09:46 +00:00
Route DefaultTextEdit::accessPath() const { return path_store; }
#include "manager_docs.h"
void DefaultTextEdit::beforeClose() { mgr_inst->save(); }
DocumentsManager *DefaultTextEdit::docsManager() const { return mgr_inst; }
2023-09-10 05:28:52 +00:00
void DefaultTextEdit::applySetting(const QString &name, const Configration *cfg) { 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())
2023-08-27 14:09:46 +00:00
throw new SimpleException<bool>("解析错误", 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))
2023-08-27 14:09:46 +00:00
throw new SimpleException<bool>("解析错误", 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))
2023-12-24 04:49:58 +00:00
throw new GenException(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()) {
2023-08-27 14:09:46 +00:00
throw new SimpleException<bool>("新建错误", "指定路径不合法,文件不存在:" + target.absoluteFilePath());
}
2022-11-18 23:47:32 +00:00
if (!base_f.open(QIODevice::WriteOnly | QIODevice::Text))
2023-08-27 14:09:46 +00:00
throw new SimpleException<bool>("新建错误", QString("指定文件无法建立:" + target.absoluteFilePath()));
base_f.write(" ");
base_f.flush();
base_f.close();
2022-11-18 23:47:32 +00:00
}
2023-09-10 05:28:52 +00:00
QDialog *DefaultTextEditFactory::createTempConfigPanel(Config::Configration *cfg_port) {
auto panel = new QDialog();
panel->setWindowTitle("文本编辑器配置");
auto layout = new QGridLayout(panel);
layout->addWidget(new QLabel("选择字体", panel));
layout->addWidget(new QPushButton("选择字体", panel));
return panel;
}