summaryrefslogtreecommitdiff
path: root/src/main_window.cpp
blob: 9972ecbd2fa790338302100bb4a9e5925afc1f72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#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