summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDominick Allen <djallen@librehumanitas.org>2024-09-25 12:16:30 -0500
committerDominick Allen <djallen@librehumanitas.org>2024-09-25 12:16:30 -0500
commit8477ffbfba2f15edb9f87e9336e2c4599000c32a (patch)
tree059a36f9d526b0807a7bd0b20979577f9a7e7d0e /src
parentb6e3cc840e255b78ee53e55b420aeee130e51ce1 (diff)
Moving forward.
Diffstat (limited to 'src')
-rw-r--r--src/archive.cpp31
-rw-r--r--src/archive.hpp7
-rw-r--r--src/bookmouse.cpp33
-rw-r--r--src/bookmouse.hpp32
-rw-r--r--src/bookmouse_old.cpp106
-rw-r--r--src/bookmouse_old.hpp51
-rw-r--r--src/demo.cpp8
-rw-r--r--src/imgui_context.hpp7
-rw-r--r--src/main_window.cpp129
-rw-r--r--src/main_window.hpp48
10 files changed, 78 insertions, 374 deletions
diff --git a/src/archive.cpp b/src/archive.cpp
index 93b3773..6c0d3ed 100644
--- a/src/archive.cpp
+++ b/src/archive.cpp
@@ -5,11 +5,11 @@
namespace bookmouse {
-ZipArchive::ZipArchive(const std::string& filename)
+ZipArchive::ZipArchive(const fud::String& filename)
{
// qDebug("Open file %s", filename.data());
int err;
- m_archive = zip_open(filename.data(), 0, &err);
+ m_archive = zip_open(filename.c_str(), 0, &err);
if (m_archive == nullptr) {
zip_error_t error;
zip_error_init_with_code(&error, err);
@@ -31,9 +31,18 @@ void ZipArchive::populate()
// qDebug("%zu pages", numEntries);
struct NameIndex {
- std::string name;
+ fud::String name;
size_t index;
size_t filesize;
+
+ static bool compare(const NameIndex& lhs, const NameIndex& rhs)
+ {
+ return std::lexicographical_compare( // force break
+ lhs.name.begin(),
+ lhs.name.end(),
+ rhs.name.begin(),
+ rhs.name.end());
+ }
};
std::vector<NameIndex> nameIndices{};
@@ -58,7 +67,7 @@ void ZipArchive::populate()
// qWarning("cannot get name %s", zip_error_strerror(error));
continue;
}
- std::string name{nameCString};
+ fud::String name{nameCString};
if (name.empty() || name.back() == '/') {
// qDebug("Directory %s", name.empty() ? "N/A" : nameCString);
continue;
@@ -69,7 +78,7 @@ void ZipArchive::populate()
}
std::sort(nameIndices.begin(), nameIndices.end(), [](const auto& lhs, const auto& rhs) {
- return std::lexicographical_compare(lhs.name.begin(), lhs.name.end(), rhs.name.begin(), rhs.name.end());
+ return NameIndex::compare(lhs, rhs);
});
m_sortedIndices.reserve(nameIndices.size());
@@ -91,12 +100,9 @@ ZipArchive::~ZipArchive()
}
}
-ZipArchive::ZipArchive(ZipArchive&& rhs)
- : m_archive{rhs.m_archive},
- m_sortedIndices{std::move(rhs.m_sortedIndices)},
- m_filenames{std::move(rhs.m_filenames)},
- m_fileSizes{std::move(rhs.m_fileSizes)},
- m_pages{std::move(rhs.m_pages)}
+ZipArchive::ZipArchive(ZipArchive&& rhs) :
+ m_archive{rhs.m_archive}, m_sortedIndices{std::move(rhs.m_sortedIndices)}, m_filenames{std::move(rhs.m_filenames)},
+ m_fileSizes{std::move(rhs.m_fileSizes)}, m_pages{std::move(rhs.m_pages)}
{
rhs.m_archive = nullptr;
}
@@ -161,7 +167,8 @@ ArchiveResult ZipArchive::getPage(size_t page)
return ArchiveResult::okay(std::cref(*m_pages[page]));
}
-size_t ZipArchive::numPages() const {
+size_t ZipArchive::numPages() const
+{
return m_pages.size();
}
diff --git a/src/archive.hpp b/src/archive.hpp
index 257f204..4a3df16 100644
--- a/src/archive.hpp
+++ b/src/archive.hpp
@@ -6,7 +6,8 @@
#include <cstdlib>
#include <memory>
#include <optional>
-#include <string>
+
+#include <fud_string.hpp>
#include <vector>
#include <zip.h>
@@ -31,7 +32,7 @@ class Archive {
class ZipArchive : public Archive {
public:
- explicit ZipArchive(const std::string& filename);
+ explicit ZipArchive(const fud::String& filename);
virtual ~ZipArchive() override;
ZipArchive(const ZipArchive& rhs) = delete;
ZipArchive(ZipArchive&& rhs);
@@ -47,7 +48,7 @@ class ZipArchive : public Archive {
zip_t* m_archive{nullptr};
std::vector<size_t> m_sortedIndices{};
- std::vector<std::string> m_filenames{};
+ std::vector<fud::String> m_filenames{};
std::vector<size_t> m_fileSizes{};
std::vector<std::optional<std::vector<char>>> m_pages{};
diff --git a/src/bookmouse.cpp b/src/bookmouse.cpp
index 77c2304..4899453 100644
--- a/src/bookmouse.cpp
+++ b/src/bookmouse.cpp
@@ -8,6 +8,8 @@
namespace bookmouse {
+constexpr const char* OpenDialogHandle = "Open File";
+
Bookmouse::Bookmouse() :
m_sdlContext{}, m_mainWindow{m_sdlContext}, m_glContext{m_mainWindow},
m_imgui{m_glContext, m_sdlContext, m_mainWindow}
@@ -106,10 +108,16 @@ void Bookmouse::updateState()
&m_running, // Is this right?
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_MenuBar);
IM_ASSERT(stateResult);
- if (ImGui::BeginMenuBar()) {
- menuBar();
+ if (ImguiMenuBar menuBar{}) {
+ menuing();
+ }
+ if (m_openDialog) {
+ m_openDialog = false;
+ ImGui::OpenPopup(OpenDialogHandle);
}
+ openDialog();
+
// Edit a color stored as 4 floats
fud::Array<float, 4> my_color{};
ImGui::ColorEdit4("Color", my_color.data());
@@ -129,21 +137,16 @@ void Bookmouse::updateState()
ImGui::End();
}
-constexpr const char* OpenDialogHandle = "Open File";
-void Bookmouse::menuBar()
+void Bookmouse::menuing()
{
- bool clickOpen = false;
if (ImguiMenu menu{"File"}) {
IM_ASSERT(menu);
- spdlog::debug("HERE {}", menu);
- if (ImGui::MenuItem("Open..", "Ctrl+O")) { /* Do stuff */
- spdlog::debug("Open - m_openDialog was {}", m_openDialog);
- clickOpen = true;
+ if (ImGui::MenuItem("Open Archive", "Ctrl+O")) { /* Do stuff */
+ m_openDialog = true;
}
if (ImGui::MenuItem("Close", "Ctrl+Q")) {
m_running = false;
}
- spdlog::debug("THERE");
}
if (ImguiMenu menu{"Help"}) {
@@ -153,18 +156,9 @@ void Bookmouse::menuBar()
}
ImGui::EndMenu();
}
- ImGui::EndMenuBar();
-
- if (clickOpen) {
- clickOpen = false;
- ImGui::OpenPopup(OpenDialogHandle);
- }
-
- openDialog();
}
void Bookmouse::openDialog() {
- // if (ImGui::Begin("Another Window", &m_openDialog, 0)) {
if (ImGui::BeginPopupModal(OpenDialogHandle)) {
ImGui::Text("Hello from another window!");
if (ImGui::Button("Close Me")) {
@@ -172,7 +166,6 @@ void Bookmouse::openDialog() {
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
- // ImGui::End();
}
}
diff --git a/src/bookmouse.hpp b/src/bookmouse.hpp
index a983025..95cbd61 100644
--- a/src/bookmouse.hpp
+++ b/src/bookmouse.hpp
@@ -1,13 +1,26 @@
#ifndef BOOKMOUSE_HPP
#define BOOKMOUSE_HPP
+#include "archive.hpp"
#include "gl_context.hpp"
#include "imgui_context.hpp"
#include "sdl_context.hpp"
#include "sdl_main_window.hpp"
+#include <memory>
+
namespace bookmouse {
+constexpr const char* AppName = "Bookmouse";
+constexpr const char* AppVersionString = "1.0.0";
+
+enum class PageLayout : uint8_t
+{
+ Single,
+ Dual,
+ Manga
+};
+
class Bookmouse {
public:
Bookmouse() noexcept(false);
@@ -19,9 +32,11 @@ class Bookmouse {
void updateState();
void renderFrame();
- void menuBar();
+ void menuing();
void openDialog();
+ void setArchive(std::unique_ptr<Archive>&& archive);
+
SdlContext m_sdlContext;
SdlMainWindow m_mainWindow;
GlContext m_glContext;
@@ -29,6 +44,21 @@ class Bookmouse {
bool m_running{true};
bool m_openDialog{false};
+
+ std::unique_ptr<Archive> m_archive{nullptr};
+ PageLayout m_pageLayout{PageLayout::Single};
+
+ size_t m_pageNumber{0};
+
+ // pageLeft
+ // pageRight
+ /*
+ void setPages();
+ void setPages(QLabel& label1, QLabel& label2);
+ void setPagesNormal();
+ void setPagesDual();
+ void setPagesManga();
+ */
};
} // namespace bookmouse
diff --git a/src/bookmouse_old.cpp b/src/bookmouse_old.cpp
deleted file mode 100644
index 9ba09c4..0000000
--- a/src/bookmouse_old.cpp
+++ /dev/null
@@ -1,106 +0,0 @@
-#include "bookmouse.hpp"
-#include "main_window.hpp"
-
-namespace bookmouse {
-
-Bookmouse::Bookmouse()
-{
- m_layout = new QVBoxLayout();
-
- m_layout->addWidget(&m_pageLeft);
- m_layout->addWidget(&m_pageRight);
-
- setLayout(m_layout);
-}
-
-void Bookmouse::setArchive(Archive* archive) {
- if (archive == m_archive) {
- return;
- }
- delete(m_archive);
- m_archive = archive;
- m_pageNumber = 0;
- setPages();
-}
-
-void Bookmouse::next() {
- auto numPages = m_archive->numPages();
- size_t increment = 1;
- if (m_pageLayout != PageLayout::Single) {
- increment = 2;
- }
- if ((m_pageNumber + increment) >= numPages)
- {
- return;
- }
- m_pageNumber += increment;
- setPages();
-}
-
-void Bookmouse::back() {
- auto numPages = m_archive->numPages();
- size_t decrement = 1;
- if (m_pageLayout != PageLayout::Single) {
- decrement = 2;
- }
- if (m_pageNumber < decrement)
- {
- m_pageNumber = 0;
- decrement = 0;
- }
- m_pageNumber -= decrement;
- setPages();
-}
-
-void Bookmouse::setPages() {
- m_pageLeft.setPixmap(QPixmap());
- m_pageRight.setPixmap(QPixmap());
-
- switch (m_pageLayout)
- {
- case PageLayout::Dual:
- return setPagesDual();
- case PageLayout::Manga:
- return setPagesManga();
- case PageLayout::Single:
- default:
- return setPagesNormal();
- }
-}
-
-void Bookmouse::setPagesNormal() {
- auto page1 = m_archive->getPage(m_pageNumber);
- auto& label1 = m_pageLeft;
-
- if (page1.isOkay()) {
- label1.setPixmap(QPixmap::fromImage(page1.getOkay()));
- }
-}
-
-void Bookmouse::setPagesDual() {
- auto& label1 = m_pageLeft;
- auto& label2 = m_pageRight;
- setPages(label1, label2);
-}
-
-void Bookmouse::setPagesManga() {
- auto& label1 = m_pageRight;
- auto& label2 = m_pageLeft;
- setPages(label1, label2);
-}
-
-void Bookmouse::setPages(QLabel& label1, QLabel& label2) {
- auto page1 = m_archive->getPage(m_pageNumber);
- auto page2 = m_archive->getPage(m_pageNumber + 1);
-
- if (page1.isOkay()) {
- label1.setPixmap(QPixmap::fromImage(page1.getOkay()));
- }
-
-
- if (page2.isOkay()) {
- label2.setPixmap(QPixmap::fromImage(page1.getOkay()));
- }
-}
-
-} // namespace bookmouse
diff --git a/src/bookmouse_old.hpp b/src/bookmouse_old.hpp
deleted file mode 100644
index b2a3a5a..0000000
--- a/src/bookmouse_old.hpp
+++ /dev/null
@@ -1,51 +0,0 @@
-#ifndef BOOKMOUSE_HPP
-#define BOOKMOUSE_HPP
-
-#include "archive.hpp"
-
-#include <QtWidgets>
-#include <cstdint>
-#include <vector>
-
-namespace bookmouse {
-
-enum class PageLayout : uint8_t {
- Single,
- Dual,
- Manga
-};
-
-class Bookmouse : public QWidget {
- Q_OBJECT
-
- public:
- friend class BookmouseApp;
- Bookmouse();
-
- public slots:
- void setArchive(Archive* archive);
- void next();
- void back();
-
- private:
- void setPages();
- void setPagesNormal();
- void setPagesDual();
- void setPagesManga();
- void setPages(QLabel& label1, QLabel& label2);
-
- QLayout* m_layout{nullptr};
-
- Archive* m_archive{nullptr};
-
- PageLayout m_pageLayout{PageLayout::Single};
-
- QLabel m_pageLeft{};
- QLabel m_pageRight{};
-
- size_t m_pageNumber{0};
-};
-
-} // namespace bookmouse
-
-#endif
diff --git a/src/demo.cpp b/src/demo.cpp
index 40abca5..433dd5d 100644
--- a/src/demo.cpp
+++ b/src/demo.cpp
@@ -36,9 +36,9 @@ bool LoadTextureFromMemory(const void* data, size_t data_size, GLuint* out_textu
spdlog::info("Got result: {} pixels per row of {} scanlines with {} channels", width, height, channelsInFile);
// Create a OpenGL texture identifier
- GLuint image_texture;
- glGenTextures(1, &image_texture);
- glBindTexture(GL_TEXTURE_2D, image_texture);
+ GLuint imageTexture;
+ glGenTextures(1, &imageTexture);
+ glBindTexture(GL_TEXTURE_2D, imageTexture);
// Setup filtering parameters for display
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
@@ -49,7 +49,7 @@ bool LoadTextureFromMemory(const void* data, size_t data_size, GLuint* out_textu
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data);
stbi_image_free(image_data);
- *out_texture = image_texture;
+ *out_texture = imageTexture;
*out_width = width;
*out_height = height;
diff --git a/src/imgui_context.hpp b/src/imgui_context.hpp
index 435db9a..c6059d6 100644
--- a/src/imgui_context.hpp
+++ b/src/imgui_context.hpp
@@ -72,11 +72,18 @@ class ConditionalRaii {
} \
};
+STRUCT_FUNCTOR_ARGS(ImGuiBeginMenuBar, ImGui::BeginMenuBar)
+STRUCT_FUNCTOR(ImGuiEndMenuBar, ImGui::EndMenuBar)
+
+using ImguiMenuBar = ConditionalRaii<ImGuiBeginMenuBar, ImGuiEndMenuBar>;
+
STRUCT_FUNCTOR_ARGS(ImGuiBeginMenu, ImGui::BeginMenu)
STRUCT_FUNCTOR(ImGuiEndMenu, ImGui::EndMenu)
using ImguiMenu = ConditionalRaii<ImGuiBeginMenu, ImGuiEndMenu>;
+// STRUCT_FUNCTOR_ARGS(ImGui
+
#undef STRUCT_FUNCTOR
#undef STRUCT_FUNCTOR_ARGS
diff --git a/src/main_window.cpp b/src/main_window.cpp
deleted file mode 100644
index 9972ecb..0000000
--- a/src/main_window.cpp
+++ /dev/null
@@ -1,129 +0,0 @@
-#include "main_window.hpp"
-
-#include <QString>
-#include <zip.h>
-
-namespace bookmouse {
-
-BookmouseApp::BookmouseApp() : m_bookmouse{new Bookmouse()}
-{
- setCentralWidget(m_bookmouse);
- setup();
-}
-
-void BookmouseApp::setup()
-{
- QCoreApplication::setApplicationName(AppName);
- QCoreApplication::setApplicationVersion(AppVersionString);
- setWindowTitle(AppName);
-
- createActions();
- createMenus();
- createToolBar();
-
- constexpr int minimumWidth = 640;
- constexpr int minimumHeight = 480;
- setMinimumSize(minimumWidth, minimumHeight);
-
- show();
-}
-
-void BookmouseApp::createActions()
-{
- m_openFile = new QAction(QIcon::fromTheme(QIcon::ThemeIcon::DocumentOpen), tr("&Open File"), this);
- m_openFile->setShortcuts(QKeySequence::Open);
- m_openFile->setStatusTip(tr("Open a file"));
- connect(m_openFile, &QAction::triggered, this, &BookmouseApp::openFile);
-
- m_openDirectory = new QAction(QIcon::fromTheme(QIcon::ThemeIcon::FolderOpen), tr("Open Directory"), this);
- m_openDirectory->setShortcut(Qt::CTRL | Qt::ALT | Qt::Key_O);
- m_openDirectory->setStatusTip(tr("Open a directory"));
- connect(m_openDirectory, &QAction::triggered, this, &BookmouseApp::openDirectory);
-
- m_quitAction = new QAction(QIcon::fromTheme(QIcon::ThemeIcon::ApplicationExit), tr("&Quit"), this);
- m_quitAction->setShortcuts(QKeySequence::Quit);
- m_quitAction->setStatusTip(tr("Quit"));
- connect(m_quitAction, &QAction::triggered, this, &BookmouseApp::quit);
-
- auto nextShortcuts = QList<QKeySequence>{};
- nextShortcuts.append(QKeySequence{Qt::Key_L});
- nextShortcuts.append(QKeySequence{Qt::Key_Right});
- nextShortcuts.append(QKeySequence{Qt::Key_Down});
- m_nextAction = new QAction(QIcon::fromTheme(QIcon::ThemeIcon::GoNext), tr("Next"), this);
- m_nextAction->setShortcuts(nextShortcuts);
- m_nextAction->setStatusTip(tr("Next"));
- connect(m_nextAction, &QAction::triggered, this, &BookmouseApp::next);
-
- auto backShortcuts = QList<QKeySequence>{};
- backShortcuts.append(QKeySequence{Qt::Key_H});
- backShortcuts.append(QKeySequence{Qt::Key_Left});
- backShortcuts.append(QKeySequence{Qt::Key_Up});
- m_backAction = new QAction(QIcon::fromTheme(QIcon::ThemeIcon::GoPrevious), tr("Back"), this);
- m_backAction->setShortcuts(backShortcuts);
- m_backAction->setStatusTip(tr("Back"));
- connect(m_backAction, &QAction::triggered, this, &BookmouseApp::back);
-}
-
-void BookmouseApp::createMenus()
-{
- m_menuBar = menuBar()->addMenu(tr("&File"));
- m_menuBar->addAction(m_openFile);
- m_menuBar->addAction(m_openDirectory);
- m_menuBar->addAction(m_quitAction);
-}
-
-void BookmouseApp::createToolBar()
-{
- m_toolBar = addToolBar(tr("&Navigation"));
- m_toolBar->addAction(m_backAction);
- m_toolBar->addAction(m_nextAction);
-}
-
-void BookmouseApp::openFile()
-{
- auto filename = QFileDialog::getOpenFileName(
- this,
- tr("Open Archive"),
- QDir::homePath(),
- tr("Archive types (*.zip *.cbz *.cbr *.gz)"));
-
- if (filename.endsWith(".zip")) {
- try {
- auto* archive = new ZipArchive(filename);
- m_bookmouse->setArchive(archive);
- } catch (std::runtime_error& exc) {
- qCritical("Failed to change archive");
- }
- } else {
- qCritical("Unsupported file extension");
- }
-}
-
-void BookmouseApp::openDirectory()
-{
- QString directory = QFileDialog::getExistingDirectory(
- this,
- tr("Open Directory"),
- QDir::homePath(),
- QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
- qDebug("Open directory %s", qPrintable(directory));
-}
-
-void BookmouseApp::quit()
-{
- QCoreApplication::quit();
-}
-
-void BookmouseApp::next()
-{
- qDebug("Next");
- m_bookmouse->next();
-}
-
-void BookmouseApp::back()
-{
- qDebug("Back");
- m_bookmouse->back();
-}
-
-} // namespace bookmouse
diff --git a/src/main_window.hpp b/src/main_window.hpp
deleted file mode 100644
index d114e31..0000000
--- a/src/main_window.hpp
+++ /dev/null
@@ -1,48 +0,0 @@
-#ifndef MAIN_WINDOW_HPP
-#define MAIN_WINDOW_HPP
-
-#include "archive.hpp"
-#include "bookmouse.hpp"
-
-#include <QtWidgets>
-
-namespace bookmouse {
-
-constexpr const char* AppName = "Bookmouse";
-constexpr const char* AppVersionString = "1.0.0";
-
-class BookmouseApp : public QMainWindow {
- Q_OBJECT
-
- public:
- BookmouseApp();
-
- private:
- void setup();
- void createActions();
- void createMenus();
- void createToolBar();
-
- Bookmouse* m_bookmouse;
-
- QAction* m_openFile;
- QAction* m_openDirectory;
- QAction* m_quitAction;
- QAction* m_nextAction;
- QAction* m_backAction;
-
- QMenu* m_menuBar;
- QToolBar* m_toolBar;
-
- private slots:
- void openFile();
- void openDirectory();
- void quit();
-
- void next();
- void back();
-};
-
-} // namespace bookmouse
-
-#endif