summaryrefslogtreecommitdiff
path: root/src/getsuyomi.cpp
blob: 0472496ecc3852a254b3fcccbf1cccc241112b73 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "getsuyomi.hpp"

#include "main_window.hpp"

#include <algorithm>

namespace getsuyomi {

Getsuyomi::Getsuyomi()
{
    // m_pageLeft.setContentsMargins(0, 1, 0, 1);
    // m_pageRight.setContentsMargins(0, 1, 0, 1);

    m_layout = new QHBoxLayout();

    m_layout->addStretch();

    m_layout->addWidget(&m_pageLeft);
    m_layout->addWidget(&m_pageRight);

    m_layout->addStretch();

    // m_layout->setContentsMargins(0, 1, 0, 1);

    setLayout(m_layout);
}

Getsuyomi::~Getsuyomi()
{
    if (m_archive != nullptr) {
        delete m_archive;
    }
}

void Getsuyomi::setArchive(Archive* archive)
{
    if (archive == m_archive) {
        return;
    }
    delete (m_archive);
    m_archive = archive;
    m_pageNumber = 0;
    setPages();
}

void Getsuyomi::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 Getsuyomi::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 Getsuyomi::setPageLayout(PageLayout newPageLayout)
{
    m_pageLayout = newPageLayout;
    setPages();
}

void Getsuyomi::setPages()
{
    m_pageLeft.setPixmap(QPixmap());
    m_pageRight.setPixmap(QPixmap());

    if (m_archive == nullptr) {
        return;
    }

    switch (m_pageLayout) {
    case PageLayout::Dual:
        return setPagesDual();
    case PageLayout::Manga:
        return setPagesManga();
    case PageLayout::Single:
    default:
        return setPagesNormal();
    }
}

void Getsuyomi::setPagesNormal()
{
    auto page1 = m_archive->getPage(m_pageNumber);
    auto& label1 = m_pageLeft;

    if (page1.isOkay()) {
        label1.setPixmap(QPixmap::fromImage(page1.getOkay()));
        label1.resize(label1.pixmap().size());
    }
}

void Getsuyomi::setPagesDual()
{
    auto& label1 = m_pageLeft;
    auto& label2 = m_pageRight;
    setPages(label1, label2);
}

void Getsuyomi::setPagesManga()
{
    auto& label1 = m_pageRight;
    auto& label2 = m_pageLeft;
    setPages(label1, label2);
}

void Getsuyomi::setPages(QLabel& label1, QLabel& label2)
{
    auto page1 = m_archive->getPage(m_pageNumber);
    auto page2 = m_archive->getPage(m_pageNumber + 1);

    const bool okay1 = page1.isOkay();
    const bool okay2 = page2.isOkay();

    if (okay1 && okay2) {
        label1.setPixmap(QPixmap::fromImage(page1.getOkay()));
        label2.setPixmap(QPixmap::fromImage(page2.getOkay()));
        auto label1Size = label1.pixmap().size();
        auto label2Size = label2.pixmap().size();
        if (label1Size.height() <= label2Size.height()) {
            label1.resize(label1Size);
            label1.resize(label1Size);
        } else {
            label1.resize(label2Size);
            label1.resize(label2Size);
        }
    } else if (okay1) {
        label1.setPixmap(QPixmap::fromImage(page1.getOkay()));
        label1.resize(label1.pixmap().size());
    } else if (okay2) {
        label2.setPixmap(QPixmap::fromImage(page2.getOkay()));
        label2.resize(label2.pixmap().size());
    }
}

} // namespace getsuyomi