aboutsummaryrefslogtreecommitdiff
path: root/libs/installer_fomod_plus/installer/ui/FomodImageViewer.cpp
blob: cb99d272779d786dd664272bbdf716b8ccc479d7 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "FomodImageViewer.h"

#include "ScaleLabel.h"
#include "UIHelper.h"

#include <QLabel>
#include <QScrollArea>
#include <QtConcurrent/QtConcurrent>

constexpr int PREVIEW_IMAGE_WIDTH  = 160;
constexpr int PREVIEW_IMAGE_HEIGHT = 90;

/*
+----------------------------------------------------------+
|n/N                                                    X  |
+---+--------------------------------------------------+---+
|   |                                                  |   |
|   |                                                  |   |
|   |                                                  |   |
|   |                                                  |   |
|   |                                                  |   |
|   |                                                  |   |
| < |               Image                              | > |
|   |                                                  |   |
|   |                                                  |   |
|   |                                                  |   |
|   |                                                  |   |
|   |                 label                            |   |
+------+------+------+---------------------------------+---+
|      |      |      |  ...previews                        |
|      |      |      |                                     |
+------+------+------+-------------------------------------+
*/
constexpr auto BUTTON_STYLE =
    "font-size: 16px; font-weight: bold; color: white; background-color: black; padding: 5px; border-radius: 1px solid black;";

FomodImageViewer::FomodImageViewer(QWidget* parent,
    const QString& fomodPath,
    const std::shared_ptr<StepViewModel>& activeStep,
    const std::shared_ptr<PluginViewModel>& activePlugin) : QDialog(parent), mFomodPath(fomodPath),
                                                            mActiveStep(activeStep),
                                                            mActivePlugin(activePlugin)
{

    setWindowFlags(Qt::FramelessWindowHint | Qt::Window);
    setAttribute(Qt::WA_TranslucentBackground);
    setStyleSheet("background-color: rgba(0, 0, 0, 150);");

    const QScreen* screen         = this->screen();
    const QRect availableGeometry = screen->availableGeometry();
    setFixedSize(availableGeometry.width(), availableGeometry.height());
    move(availableGeometry.x(), availableGeometry.y());

    collectImages();
    mMainImageWrapper = createSinglePhotoPane(this);
    mTopBar           = createTopBar(this);
    mPreviewImages    = createPreviewImages(this);
    mCenterRow        = createCenterRow(this);

    const auto layout = new QVBoxLayout(this);
    layout->setContentsMargins(0, 0, 0, 0); // Remove margins
    layout->setSpacing(0); // Remove spacing between widgets
    layout->addWidget(mTopBar);
    layout->addWidget(mCenterRow, 1);
    layout->addWidget(mPreviewImages);
    setLayout(layout);

    select(mCurrentIndex);

    setFocusPolicy(Qt::StrongFocus); // so we can receive key events
}

void FomodImageViewer::collectImages()
{
    mLabelsAndImages.clear();
    for (const auto& groupViewModel : mActiveStep->getGroups()) {
        for (const auto& pluginViewModel : groupViewModel->getPlugins()) {
            if (pluginViewModel->getImagePath().empty()) {
                continue;
            }
            QString imagePath = UIHelper::getFullImagePath(mFomodPath,
                QString::fromStdString(pluginViewModel->getImagePath()));
            mLabelsAndImages.emplace_back(QString::fromStdString(pluginViewModel->getName()), imagePath);

            if (pluginViewModel == mActivePlugin) {
                mCurrentIndex = static_cast<int>(mLabelsAndImages.size()) - 1;
            }
        }
    }
}

QWidget* FomodImageViewer::createCenterRow(QWidget* parent)
{
    const auto centerRow = new QWidget(parent);
    const auto layout    = new QHBoxLayout(centerRow);

    mBackButton    = createBackButton(centerRow);
    mForwardButton = createForwardButton(centerRow);

    mBackButton->setFocusPolicy(Qt::NoFocus);
    mForwardButton->setFocusPolicy(Qt::NoFocus);

    layout->addWidget(mBackButton);
    layout->addWidget(mMainImageWrapper, 1);
    layout->addWidget(mForwardButton);

    return centerRow;
}

// ReSharper disable once CppMemberFunctionMayBeStatic
QWidget* FomodImageViewer::createSinglePhotoPane(QWidget* parent)
{
    const auto singlePhotoPane = new QWidget(parent);
    const auto layout          = new QVBoxLayout(singlePhotoPane);

    // const auto [labelText, imagePath] = pair;

    mMainImage = new ScaleLabel(singlePhotoPane);
    mMainImage->setAlignment(Qt::AlignCenter);
    layout->addWidget(mMainImage, 1);

    mLabel = new QLabel(singlePhotoPane);
    // mLabel->setText(labelText);
    mLabel->setAlignment(Qt::AlignCenter);
    mLabel->setStyleSheet("color: white; font-size: 20px;");
    layout->addWidget(mLabel);

    return singlePhotoPane;
}

QScrollArea* FomodImageViewer::createPreviewImages(QWidget* parent)
{
    mImagePanes.clear();
    const auto previewImages = new QScrollArea(parent);
    const auto widget        = new QWidget(previewImages);
    const auto layout        = new QHBoxLayout(previewImages);

    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);

    for (int i = 0; i < mLabelsAndImages.size(); i++) {
        const auto imageLabel = new ScaleLabel(previewImages);
        imageLabel->setAlignment(Qt::AlignCenter);
        imageLabel->setFixedSize(PREVIEW_IMAGE_WIDTH, PREVIEW_IMAGE_HEIGHT);
        imageLabel->setFocusPolicy(Qt::NoFocus);
        connect(imageLabel, &ScaleLabel::clicked, this, [this, i] {
            select(i);
        });
        layout->addWidget(imageLabel);
        mImagePanes.emplace_back(imageLabel);

        // imageLabel->setScalableResource(mLabelsAndImages[i].second);
        const auto imagePath = mLabelsAndImages[i].second;

        QThreadPool::globalInstance()->start([imageLabel, imagePath]() {
            QMetaObject::invokeMethod(imageLabel, [imageLabel, imagePath]() {
                imageLabel->setScalableResource(imagePath);
            }, Qt::QueuedConnection);
        });
    }

    widget->setLayout(layout);
    previewImages->setFixedHeight(PREVIEW_IMAGE_HEIGHT + 10);
    previewImages->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    previewImages->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    previewImages->setFocusPolicy(Qt::NoFocus);

    previewImages->setWidget(widget);
    previewImages->setStyleSheet("QScrollArea { border: none; }");

    return previewImages;
}

QPushButton* FomodImageViewer::createBackButton(QWidget* parent) const
{
    const auto backButton = new QPushButton(parent);
    backButton->setText("<");
    backButton->setStyleSheet(BUTTON_STYLE);
    connect(backButton, &QPushButton::clicked, this, &FomodImageViewer::goBack);
    return backButton;
}

QPushButton* FomodImageViewer::createForwardButton(QWidget* parent) const
{
    const auto forwardButton = new QPushButton(parent);
    forwardButton->setText(">");
    forwardButton->
        setStyleSheet(BUTTON_STYLE);
    connect(forwardButton, &QPushButton::clicked, this, &FomodImageViewer::goForward);
    return forwardButton;
}

QWidget* FomodImageViewer::createTopBar(QWidget* parent)
{
    const auto topBar = new QWidget(parent);
    const auto layout = new QHBoxLayout(topBar);

    // counter, spacer, close button
    mCounter = new QLabel(topBar);
    mCounter->setStyleSheet(BUTTON_STYLE);
    layout->addWidget(mCounter);

    layout->addStretch();

    mCloseButton = createCloseButton(topBar);
    layout->addWidget(mCloseButton);
    return topBar;
}

QPushButton* FomodImageViewer::createCloseButton(QWidget* parent)
{
    const auto closeButton = new QPushButton(parent);
    // const QIcon icon(":/fomod/close");
    // closeButton->setIcon(icon);
    closeButton->setText("X");
    closeButton->setStyleSheet("color: white; background-color: black; padding: 5px; border-radius: 1px solid black;");
    connect(closeButton, &QPushButton::clicked, this, &FomodImageViewer::close);
    return closeButton;
}

void FomodImageViewer::updateCounterText() const
{
    mCounter->setText(QString::number(mCurrentIndex + 1) + "/" + QString::number(mLabelsAndImages.size()));
}

void FomodImageViewer::goBack()
{
    if (mCurrentIndex == 0) {
        return;
    }
    select(--mCurrentIndex);
}

void FomodImageViewer::goForward()
{
    if (mCurrentIndex == mLabelsAndImages.size() - 1) {
        return;
    }
    select(++mCurrentIndex);
}

void FomodImageViewer::select(const int index)
{
    if (index < 0 || index >= mLabelsAndImages.size()) {
        return;
    }

    // Remove border from previously selected image
    mCurrentIndex = index; // check for bounds?
    updateCounterText();

    for (int i = 0; i < mImagePanes.size(); i++) {
        if (i == mCurrentIndex) {
            mImagePanes[i]->setStyleSheet("border: 2px solid white;");
        } else {
            mImagePanes[i]->setStyleSheet("");
        }
    }

    const auto& imagePath = mLabelsAndImages[index].second;
    const auto& labelText = mLabelsAndImages[index].first;
    mLabel->setText(labelText);
    mMainImage->setScalableResource(imagePath);
}

void FomodImageViewer::keyPressEvent(QKeyEvent* event)
{
    switch (event->key()) {
    case Qt::Key_Left:
        goBack();
        break;
    case Qt::Key_Right:
        goForward();
        break;
    default:
        QDialog::keyPressEvent(event);
    }
}

void FomodImageViewer::showEvent(QShowEvent* event)
{
    QDialog::showEvent(event);
    setFocus();
}