added txt support

This commit is contained in:
Agel committed 2026-03-15 22:18:26 +01:00
1 parent 64a70bc4cf
commit 21656a3e9c
10 files changed
+441 -120

No files matched your search

+2 -1
View File
@@ -2,6 +2,7 @@
#define FORMATREADER_H
#include <QString>
#include <QStringList>
#include <QList>
struct TocEntry {
@@ -13,7 +14,7 @@ struct TocEntry {
struct Chapter {
int index;
QString title;
QString contentHtml;
QStringList contentChunks;
};
class FormatReader {
+43 -22
View File
@@ -2,20 +2,38 @@
#include "FormatReader.h"
ReadingModel::ReadingModel(QObject *parent)
: QObject(parent)
: QAbstractListModel(parent)
{
}
int ReadingModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return m_chunks.size();
}
QVariant ReadingModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() < 0 || index.row() >= m_chunks.size())
return {};
if (role == ContentRole)
return m_chunks.at(index.row());
return {};
}
QHash<int, QByteArray> ReadingModel::roleNames() const
{
return {{ContentRole, "content"}};
}
QString ReadingModel::chapterTitle() const
{
return m_chapterTitle;
}
QString ReadingModel::chapterContent() const
{
return m_chapterContent;
}
int ReadingModel::currentChapter() const
{
return m_currentChapter;
@@ -33,19 +51,28 @@ double ReadingModel::progress() const
return static_cast<double>(m_currentChapter + 1) / m_totalChapters;
}
void ReadingModel::loadChapterData(int index)
{
beginResetModel();
m_chunks.clear();
m_chapterTitle.clear();
if (m_reader && index >= 0 && index < m_totalChapters) {
auto chapter = m_reader->loadChapter(index);
m_chapterTitle = chapter.title;
m_chunks = chapter.contentChunks;
}
endResetModel();
}
void ReadingModel::setReader(FormatReader *reader)
{
m_reader = reader;
m_currentChapter = 0;
m_totalChapters = reader ? reader->chapterCount() : 0;
m_chapterTitle.clear();
m_chapterContent.clear();
if (m_reader && m_totalChapters > 0) {
auto chapter = m_reader->loadChapter(0);
m_chapterTitle = chapter.title;
m_chapterContent = chapter.contentHtml;
}
loadChapterData(0);
emit totalChaptersChanged();
emit chapterChanged();
@@ -57,9 +84,7 @@ void ReadingModel::nextChapter()
return;
m_currentChapter++;
auto chapter = m_reader->loadChapter(m_currentChapter);
m_chapterTitle = chapter.title;
m_chapterContent = chapter.contentHtml;
loadChapterData(m_currentChapter);
emit chapterChanged();
}
@@ -69,9 +94,7 @@ void ReadingModel::previousChapter()
return;
m_currentChapter--;
auto chapter = m_reader->loadChapter(m_currentChapter);
m_chapterTitle = chapter.title;
m_chapterContent = chapter.contentHtml;
loadChapterData(m_currentChapter);
emit chapterChanged();
}
@@ -81,8 +104,6 @@ void ReadingModel::goToChapter(int index)
return;
m_currentChapter = index;
auto chapter = m_reader->loadChapter(m_currentChapter);
m_chapterTitle = chapter.title;
m_chapterContent = chapter.contentHtml;
loadChapterData(m_currentChapter);
emit chapterChanged();
}
+15 -5
View File
@@ -1,24 +1,32 @@
#ifndef READINGMODEL_H
#define READINGMODEL_H
#include <QObject>
#include <QAbstractListModel>
#include <QString>
#include <QStringList>
class FormatReader;
class ReadingModel : public QObject {
class ReadingModel : public QAbstractListModel {
Q_OBJECT
Q_PROPERTY(QString chapterTitle READ chapterTitle NOTIFY chapterChanged)
Q_PROPERTY(QString chapterContent READ chapterContent NOTIFY chapterChanged)
Q_PROPERTY(int currentChapter READ currentChapter NOTIFY chapterChanged)
Q_PROPERTY(int totalChapters READ totalChapters NOTIFY totalChaptersChanged)
Q_PROPERTY(double progress READ progress NOTIFY chapterChanged)
public:
enum Roles {
ContentRole = Qt::UserRole + 1
};
explicit ReadingModel(QObject *parent = nullptr);
// QAbstractListModel overrides
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QHash<int, QByteArray> roleNames() const override;
QString chapterTitle() const;
QString chapterContent() const;
int currentChapter() const;
int totalChapters() const;
double progress() const;
@@ -34,11 +42,13 @@ signals:
void totalChaptersChanged();
private:
void loadChapterData(int index);
FormatReader *m_reader = nullptr;
int m_currentChapter = 0;
int m_totalChapters = 0;
QString m_chapterTitle;
QString m_chapterContent;
QStringList m_chunks;
};
#endif // READINGMODEL_H
+74 -10
View File
@@ -8,6 +8,8 @@
#include <uchardet/uchardet.h>
#include <iconv.h>
static constexpr int kChunkSize = 4096;
static QString decodeWithIconv(const QByteArray &raw, const char *charset)
{
iconv_t cd = iconv_open("UTF-8", charset);
@@ -31,6 +33,69 @@ static QString decodeWithIconv(const QByteArray &raw, const char *charset)
return QString::fromUtf8(out);
}
// Convert a raw text fragment into HTML paragraphs.
// Splits on blank lines (\n\n+) to create <p> blocks;
// single newlines within a block become <br>.
static QString textToHtml(const QString &text)
{
QString html;
const QStringList blocks = text.split(QRegularExpression("\\n\\n+"));
for (const QString &block : blocks) {
QString escaped = block.toHtmlEscaped().trimmed();
if (escaped.isEmpty())
continue;
escaped.replace('\n', QStringLiteral("<br>\n"));
html += QStringLiteral("<p>") + escaped + QStringLiteral("</p>\n");
}
return html;
}
// Split raw text into chunks of approximately kChunkSize characters,
// preferring to split at \n boundaries, falling back to whitespace.
static QStringList splitRawText(const QString &content)
{
QStringList rawChunks;
int pos = 0;
const int len = content.size();
while (pos < len) {
if (len - pos <= kChunkSize) {
rawChunks.append(content.mid(pos));
break;
}
int end = pos + kChunkSize;
// Look backward for a newline to split at
int splitAt = -1;
for (int i = end; i > pos; --i) {
if (content.at(i) == '\n') {
splitAt = i + 1; // include the newline in the current chunk
break;
}
}
// No newline found — fall back to whitespace
if (splitAt < 0) {
for (int i = end; i > pos; --i) {
if (content.at(i).isSpace()) {
splitAt = i + 1;
break;
}
}
}
// No whitespace either — hard split
if (splitAt < 0)
splitAt = end;
rawChunks.append(content.mid(pos, splitAt - pos));
pos = splitAt;
}
return rawChunks;
}
bool TxtReader::open(const QString &filePath)
{
QFile file(filePath);
@@ -89,18 +154,17 @@ Chapter TxtReader::loadChapter(int index)
if (index != 0 || m_content.isEmpty())
return Chapter{index, {}, {}};
// Split on blank lines to create paragraphs; single newlines become <br>
QString html;
const QStringList blocks = m_content.split(QRegularExpression("\\n\\n+"));
for (const QString &block : blocks) {
QString escaped = block.toHtmlEscaped().trimmed();
if (escaped.isEmpty())
continue;
escaped.replace('\n', QStringLiteral("<br>\n"));
html += QStringLiteral("<p>") + escaped + QStringLiteral("</p>\n");
QStringList rawChunks = splitRawText(m_content);
QStringList htmlChunks;
htmlChunks.reserve(rawChunks.size());
for (const QString &raw : rawChunks) {
QString html = textToHtml(raw);
if (!html.isEmpty())
htmlChunks.append(html);
}
return Chapter{0, m_title, html};
return Chapter{0, m_title, htmlChunks};
}
QList<TocEntry> TxtReader::tableOfContents()