Initial UI
This commit is contained in:
1 parent
4d52c8cc60
commit
3c0037cfc9
29 files changed
+1367
-2
No files matched your search
@@ -0,0 +1,21 @@
|
||||
#include "AppState.h"
|
||||
|
||||
AppState::AppState(QObject *parent)
|
||||
: QObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
QString AppState::lastFilePath() const
|
||||
{
|
||||
return m_lastFilePath;
|
||||
}
|
||||
|
||||
void AppState::save()
|
||||
{
|
||||
// Will use QSettings via TDD
|
||||
}
|
||||
|
||||
void AppState::restore()
|
||||
{
|
||||
// Will use QSettings via TDD
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef APPSTATE_H
|
||||
#define APPSTATE_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
|
||||
class AppState : public QObject {
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString lastFilePath READ lastFilePath NOTIFY lastFilePathChanged)
|
||||
|
||||
public:
|
||||
explicit AppState(QObject *parent = nullptr);
|
||||
|
||||
QString lastFilePath() const;
|
||||
|
||||
Q_INVOKABLE void save();
|
||||
Q_INVOKABLE void restore();
|
||||
|
||||
signals:
|
||||
void lastFilePathChanged();
|
||||
|
||||
private:
|
||||
QString m_lastFilePath;
|
||||
};
|
||||
|
||||
#endif // APPSTATE_H
|
||||
@@ -0,0 +1,108 @@
|
||||
#include "BookController.h"
|
||||
#include "ReaderFactory.h"
|
||||
#include "ReadingModel.h"
|
||||
#include "BookmarkModel.h"
|
||||
|
||||
BookController::BookController(QObject *parent)
|
||||
: QObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
QString BookController::bookTitle() const
|
||||
{
|
||||
return m_bookTitle;
|
||||
}
|
||||
|
||||
QString BookController::bookAuthor() const
|
||||
{
|
||||
return m_bookAuthor;
|
||||
}
|
||||
|
||||
bool BookController::loading() const
|
||||
{
|
||||
return m_loading;
|
||||
}
|
||||
|
||||
QString BookController::errorMessage() const
|
||||
{
|
||||
return m_errorMessage;
|
||||
}
|
||||
|
||||
bool BookController::hasBook() const
|
||||
{
|
||||
return m_reader != nullptr;
|
||||
}
|
||||
|
||||
QList<TocEntry> BookController::toc() const
|
||||
{
|
||||
if (m_reader)
|
||||
return m_reader->tableOfContents();
|
||||
return {};
|
||||
}
|
||||
|
||||
void BookController::setReadingModel(ReadingModel *model)
|
||||
{
|
||||
m_readingModel = model;
|
||||
}
|
||||
|
||||
void BookController::setBookmarkModel(BookmarkModel *model)
|
||||
{
|
||||
m_bookmarkModel = model;
|
||||
}
|
||||
|
||||
void BookController::openBook(const QUrl &fileUrl)
|
||||
{
|
||||
QString filePath = fileUrl.toLocalFile();
|
||||
|
||||
m_loading = true;
|
||||
emit loadingChanged();
|
||||
|
||||
m_errorMessage.clear();
|
||||
emit errorMessageChanged();
|
||||
|
||||
auto reader = ReaderFactory::create(filePath);
|
||||
if (!reader) {
|
||||
m_loading = false;
|
||||
emit loadingChanged();
|
||||
m_errorMessage = QStringLiteral("No format readers available");
|
||||
emit errorMessageChanged();
|
||||
emit bookError(m_errorMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!reader->open(filePath)) {
|
||||
m_loading = false;
|
||||
emit loadingChanged();
|
||||
m_errorMessage = reader->lastError();
|
||||
emit errorMessageChanged();
|
||||
emit bookError(m_errorMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
m_reader = std::move(reader);
|
||||
m_bookTitle = m_reader->title();
|
||||
m_bookAuthor = m_reader->author();
|
||||
|
||||
if (m_readingModel)
|
||||
m_readingModel->setReader(m_reader.get());
|
||||
|
||||
if (m_bookmarkModel)
|
||||
m_bookmarkModel->setBookId(filePath);
|
||||
|
||||
m_loading = false;
|
||||
emit loadingChanged();
|
||||
emit bookChanged();
|
||||
emit bookReady();
|
||||
}
|
||||
|
||||
void BookController::closeBook()
|
||||
{
|
||||
m_reader.reset();
|
||||
m_bookTitle.clear();
|
||||
m_bookAuthor.clear();
|
||||
|
||||
if (m_readingModel)
|
||||
m_readingModel->setReader(nullptr);
|
||||
|
||||
emit bookChanged();
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
#ifndef BOOKCONTROLLER_H
|
||||
#define BOOKCONTROLLER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QUrl>
|
||||
#include <QList>
|
||||
#include <memory>
|
||||
|
||||
#include "FormatReader.h"
|
||||
|
||||
class ReadingModel;
|
||||
class BookmarkModel;
|
||||
|
||||
class BookController : public QObject {
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString bookTitle READ bookTitle NOTIFY bookChanged)
|
||||
Q_PROPERTY(QString bookAuthor READ bookAuthor NOTIFY bookChanged)
|
||||
Q_PROPERTY(bool loading READ loading NOTIFY loadingChanged)
|
||||
Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged)
|
||||
Q_PROPERTY(bool hasBook READ hasBook NOTIFY bookChanged)
|
||||
Q_PROPERTY(QList<TocEntry> toc READ toc NOTIFY bookChanged)
|
||||
|
||||
public:
|
||||
explicit BookController(QObject *parent = nullptr);
|
||||
|
||||
QString bookTitle() const;
|
||||
QString bookAuthor() const;
|
||||
bool loading() const;
|
||||
QString errorMessage() const;
|
||||
bool hasBook() const;
|
||||
QList<TocEntry> toc() const;
|
||||
|
||||
void setReadingModel(ReadingModel *model);
|
||||
void setBookmarkModel(BookmarkModel *model);
|
||||
|
||||
Q_INVOKABLE void openBook(const QUrl &fileUrl);
|
||||
Q_INVOKABLE void closeBook();
|
||||
|
||||
signals:
|
||||
void bookReady();
|
||||
void bookError(const QString &error);
|
||||
void bookChanged();
|
||||
void loadingChanged();
|
||||
void errorMessageChanged();
|
||||
|
||||
private:
|
||||
std::unique_ptr<FormatReader> m_reader;
|
||||
ReadingModel *m_readingModel = nullptr;
|
||||
BookmarkModel *m_bookmarkModel = nullptr;
|
||||
QString m_bookTitle;
|
||||
QString m_bookAuthor;
|
||||
bool m_loading = false;
|
||||
QString m_errorMessage;
|
||||
};
|
||||
|
||||
#endif // BOOKCONTROLLER_H
|
||||
@@ -0,0 +1,45 @@
|
||||
#include "BookmarkModel.h"
|
||||
#include "BookmarkStore.h"
|
||||
|
||||
BookmarkModel::BookmarkModel(QObject *parent)
|
||||
: QObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
QStringList BookmarkModel::bookmarks() const
|
||||
{
|
||||
return m_bookmarks;
|
||||
}
|
||||
|
||||
void BookmarkModel::setBookId(const QString &bookId)
|
||||
{
|
||||
m_bookId = bookId;
|
||||
m_bookmarks.clear();
|
||||
if (m_store) {
|
||||
m_bookmarks = m_store->bookmarksForBook(m_bookId);
|
||||
}
|
||||
emit bookmarksChanged();
|
||||
}
|
||||
|
||||
void BookmarkModel::setStore(BookmarkStore *store)
|
||||
{
|
||||
m_store = store;
|
||||
}
|
||||
|
||||
void BookmarkModel::addBookmark(const QString &bookmark)
|
||||
{
|
||||
if (m_store) {
|
||||
m_store->addBookmark(m_bookId, bookmark);
|
||||
m_bookmarks = m_store->bookmarksForBook(m_bookId);
|
||||
emit bookmarksChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void BookmarkModel::removeBookmark(const QString &bookmark)
|
||||
{
|
||||
if (m_store) {
|
||||
m_store->removeBookmark(m_bookId, bookmark);
|
||||
m_bookmarks = m_store->bookmarksForBook(m_bookId);
|
||||
emit bookmarksChanged();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#ifndef BOOKMARKMODEL_H
|
||||
#define BOOKMARKMODEL_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QStringList>
|
||||
|
||||
class BookmarkStore;
|
||||
|
||||
class BookmarkModel : public QObject {
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QStringList bookmarks READ bookmarks NOTIFY bookmarksChanged)
|
||||
|
||||
public:
|
||||
explicit BookmarkModel(QObject *parent = nullptr);
|
||||
|
||||
QStringList bookmarks() const;
|
||||
|
||||
void setBookId(const QString &bookId);
|
||||
void setStore(BookmarkStore *store);
|
||||
|
||||
Q_INVOKABLE void addBookmark(const QString &bookmark);
|
||||
Q_INVOKABLE void removeBookmark(const QString &bookmark);
|
||||
|
||||
signals:
|
||||
void bookmarksChanged();
|
||||
|
||||
private:
|
||||
BookmarkStore *m_store = nullptr;
|
||||
QString m_bookId;
|
||||
QStringList m_bookmarks;
|
||||
};
|
||||
|
||||
#endif // BOOKMARKMODEL_H
|
||||
@@ -0,0 +1,24 @@
|
||||
#include "BookmarkStore.h"
|
||||
|
||||
BookmarkStore::BookmarkStore(QObject *parent)
|
||||
: QObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
QStringList BookmarkStore::bookmarksForBook(const QString &bookId) const
|
||||
{
|
||||
Q_UNUSED(bookId)
|
||||
return {};
|
||||
}
|
||||
|
||||
void BookmarkStore::addBookmark(const QString &bookId, const QString &bookmark)
|
||||
{
|
||||
Q_UNUSED(bookId)
|
||||
Q_UNUSED(bookmark)
|
||||
}
|
||||
|
||||
void BookmarkStore::removeBookmark(const QString &bookId, const QString &bookmark)
|
||||
{
|
||||
Q_UNUSED(bookId)
|
||||
Q_UNUSED(bookmark)
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef BOOKMARKSTORE_H
|
||||
#define BOOKMARKSTORE_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QStringList>
|
||||
|
||||
class BookmarkStore : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit BookmarkStore(QObject *parent = nullptr);
|
||||
|
||||
QStringList bookmarksForBook(const QString &bookId) const;
|
||||
void addBookmark(const QString &bookId, const QString &bookmark);
|
||||
void removeBookmark(const QString &bookId, const QString &bookmark);
|
||||
};
|
||||
|
||||
#endif // BOOKMARKSTORE_H
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef FORMATREADER_H
|
||||
#define FORMATREADER_H
|
||||
|
||||
#include <QString>
|
||||
#include <QList>
|
||||
|
||||
struct TocEntry {
|
||||
QString title;
|
||||
int chapterIndex;
|
||||
int depth; // 0 = top level
|
||||
};
|
||||
|
||||
struct Chapter {
|
||||
int index;
|
||||
QString title;
|
||||
QString contentHtml;
|
||||
};
|
||||
|
||||
class FormatReader {
|
||||
public:
|
||||
virtual ~FormatReader() = default;
|
||||
|
||||
virtual bool open(const QString &filePath) = 0;
|
||||
virtual QString lastError() = 0;
|
||||
virtual int chapterCount() = 0;
|
||||
virtual Chapter loadChapter(int index) = 0;
|
||||
virtual QList<TocEntry> tableOfContents() = 0;
|
||||
virtual QString title() = 0;
|
||||
virtual QString author() = 0;
|
||||
};
|
||||
|
||||
#endif // FORMATREADER_H
|
||||
@@ -0,0 +1,8 @@
|
||||
#include "ReaderFactory.h"
|
||||
#include "FormatReader.h"
|
||||
|
||||
std::unique_ptr<FormatReader> ReaderFactory::create(const QString &filePath)
|
||||
{
|
||||
Q_UNUSED(filePath)
|
||||
return nullptr;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef READERFACTORY_H
|
||||
#define READERFACTORY_H
|
||||
|
||||
#include <memory>
|
||||
#include <QString>
|
||||
|
||||
class FormatReader;
|
||||
|
||||
class ReaderFactory {
|
||||
public:
|
||||
static std::unique_ptr<FormatReader> create(const QString &filePath);
|
||||
};
|
||||
|
||||
#endif // READERFACTORY_H
|
||||
@@ -0,0 +1,88 @@
|
||||
#include "ReadingModel.h"
|
||||
#include "FormatReader.h"
|
||||
|
||||
ReadingModel::ReadingModel(QObject *parent)
|
||||
: QObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
QString ReadingModel::chapterTitle() const
|
||||
{
|
||||
return m_chapterTitle;
|
||||
}
|
||||
|
||||
QString ReadingModel::chapterContent() const
|
||||
{
|
||||
return m_chapterContent;
|
||||
}
|
||||
|
||||
int ReadingModel::currentChapter() const
|
||||
{
|
||||
return m_currentChapter;
|
||||
}
|
||||
|
||||
int ReadingModel::totalChapters() const
|
||||
{
|
||||
return m_totalChapters;
|
||||
}
|
||||
|
||||
double ReadingModel::progress() const
|
||||
{
|
||||
if (m_totalChapters == 0)
|
||||
return 0.0;
|
||||
return static_cast<double>(m_currentChapter + 1) / m_totalChapters;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
emit totalChaptersChanged();
|
||||
emit chapterChanged();
|
||||
}
|
||||
|
||||
void ReadingModel::nextChapter()
|
||||
{
|
||||
if (!m_reader || m_currentChapter >= m_totalChapters - 1)
|
||||
return;
|
||||
|
||||
m_currentChapter++;
|
||||
auto chapter = m_reader->loadChapter(m_currentChapter);
|
||||
m_chapterTitle = chapter.title;
|
||||
m_chapterContent = chapter.contentHtml;
|
||||
emit chapterChanged();
|
||||
}
|
||||
|
||||
void ReadingModel::previousChapter()
|
||||
{
|
||||
if (!m_reader || m_currentChapter <= 0)
|
||||
return;
|
||||
|
||||
m_currentChapter--;
|
||||
auto chapter = m_reader->loadChapter(m_currentChapter);
|
||||
m_chapterTitle = chapter.title;
|
||||
m_chapterContent = chapter.contentHtml;
|
||||
emit chapterChanged();
|
||||
}
|
||||
|
||||
void ReadingModel::goToChapter(int index)
|
||||
{
|
||||
if (!m_reader || index < 0 || index >= m_totalChapters)
|
||||
return;
|
||||
|
||||
m_currentChapter = index;
|
||||
auto chapter = m_reader->loadChapter(m_currentChapter);
|
||||
m_chapterTitle = chapter.title;
|
||||
m_chapterContent = chapter.contentHtml;
|
||||
emit chapterChanged();
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#ifndef READINGMODEL_H
|
||||
#define READINGMODEL_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
|
||||
class FormatReader;
|
||||
|
||||
class ReadingModel : public QObject {
|
||||
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:
|
||||
explicit ReadingModel(QObject *parent = nullptr);
|
||||
|
||||
QString chapterTitle() const;
|
||||
QString chapterContent() const;
|
||||
int currentChapter() const;
|
||||
int totalChapters() const;
|
||||
double progress() const;
|
||||
|
||||
void setReader(FormatReader *reader);
|
||||
|
||||
Q_INVOKABLE void nextChapter();
|
||||
Q_INVOKABLE void previousChapter();
|
||||
Q_INVOKABLE void goToChapter(int index);
|
||||
|
||||
signals:
|
||||
void chapterChanged();
|
||||
void totalChaptersChanged();
|
||||
|
||||
private:
|
||||
FormatReader *m_reader = nullptr;
|
||||
int m_currentChapter = 0;
|
||||
int m_totalChapters = 0;
|
||||
QString m_chapterTitle;
|
||||
QString m_chapterContent;
|
||||
};
|
||||
|
||||
#endif // READINGMODEL_H
|
||||
Reference in new issue
Block a user