QtNovelUI/WordsIDE/default_textpresent.cpp

133 lines
3.0 KiB
C++
Raw Normal View History

2023-02-26 16:22:44 +00:00
#include "default_textpresent.h"
2022-11-18 05:43:27 +00:00
#include "keywordshightlighter.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;
using namespace Parse::Result;
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
2023-02-26 16:22:44 +00:00
DefaultTextPresent:: DefaultTextPresent()
2023-02-26 14:44:00 +00:00
:edit_square(new QTextEdit){}
2022-11-17 08:26:05 +00:00
2023-02-26 14:44:00 +00:00
void DefaultTextPresent::modeReset(const QString &) const
2022-11-17 08:26:05 +00:00
{
}
2023-02-26 14:44:00 +00:00
QList<QString> DefaultTextPresent::modes() const
2022-11-17 08:26:05 +00:00
{
return QList<QString>() << "文本";
}
2023-02-26 14:44:00 +00:00
QString DefaultTextPresent::currentMode() const
2022-11-17 08:26:05 +00:00
{
return modes()[0];
}
2023-02-26 14:44:00 +00:00
void DefaultTextPresent::jumpTo(const QList<QString> &path)
2022-11-17 08:26:05 +00:00
{
}
2023-02-26 14:44:00 +00:00
std::tuple<bool, QString> DefaultTextPresent::create(const QDir &target, const QString &name, const QString &suffix)
2022-11-17 08:26:05 +00:00
{
2023-02-26 14:44:00 +00:00
auto x = target.filePath(QString("%1.%2").arg(name, suffix));
if(QFileInfo::exists(x))
return std::make_tuple(false, x);
2022-11-17 08:26:05 +00:00
2023-02-26 14:44:00 +00:00
QFile xf(x);
if(!xf.open(QIODevice::WriteOnly|QIODevice::Text))
throw new std::exception(QString("指定文件无法建立:" + x).toLocal8Bit());
2022-11-17 08:26:05 +00:00
2023-02-26 14:44:00 +00:00
return std::make_tuple(true, target.relativeFilePath(x));
2022-11-17 08:26:05 +00:00
}
2023-02-26 14:44:00 +00:00
MakeTools::ContentPresent * DefaultTextPresent::newInst() const
{
2023-02-26 14:44:00 +00:00
return new DefaultTextPresent();
}
2023-02-26 14:44:00 +00:00
void DefaultTextPresent::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-02-26 14:44:00 +00:00
QString DefaultTextPresent::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-02-26 14:44:00 +00:00
void DefaultTextPresent::load(const QFileInfo &target_file)
{
2023-02-26 14:44:00 +00:00
if(!target_file.exists())
throw new std::exception(QString("指定文件未找到:%1").arg(target_file.absoluteFilePath()).toLocal8Bit());
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 std::exception(QString("指定文件无法打开:%1").arg(target_file.absoluteFilePath()).toLocal8Bit());
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-02-26 14:44:00 +00:00
void DefaultTextPresent::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-02-26 14:44:00 +00:00
QString DefaultTextPresent::targetPath(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
}
2023-02-26 14:44:00 +00:00
bool DefaultTextPresent::isModified() const
2022-11-18 23:47:32 +00:00
{
2023-02-26 14:44:00 +00:00
return true;
2022-11-18 23:47:32 +00:00
}
2023-02-26 14:44:00 +00:00
QList<QString> DefaultTextPresent::suffixes() const
2022-11-18 23:47:32 +00:00
{
2023-02-26 14:44:00 +00:00
return QStringList() << "txt" << "*";
2022-11-18 23:47:32 +00:00
}
2023-02-26 14:44:00 +00:00
QString DefaultTextPresent::absulateTargetPath() const
2022-11-18 23:47:32 +00:00
{
2023-02-26 14:44:00 +00:00
return target_file_path;
2022-11-18 23:47:32 +00:00
}
2023-02-26 14:44:00 +00:00
QWidget * DefaultTextPresent::widget() const
2022-11-18 23:47:32 +00:00
{
2023-02-26 14:44:00 +00:00
return edit_square;
2022-11-18 23:47:32 +00:00
}
2023-02-26 14:44:00 +00:00
QString DefaultTextPresent::getText() const
2022-11-18 23:47:32 +00:00
{
2023-02-26 14:44:00 +00:00
return edit_square->toPlainText();
2022-11-18 23:47:32 +00:00
}