aboutsummaryrefslogtreecommitdiff
path: root/libs/installer_fomod/src/fomodscreenshotdialog.cpp
blob: 568faa4a207c23c0e2918721dd563a8e2766c1c6 (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
/*
Copyright (C) 2012 Sebastian Herbord. All rights reserved.

This file is part of Mod Organizer.

Mod Organizer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Mod Organizer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Mod Organizer.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "fomodscreenshotdialog.h"
#include "ui_fomodscreenshotdialog.h"

#include <QDirIterator>
#include <QFrame>
#include <QProxyStyle>
#include <QPushButton>
#include <QScreen>
#include <QTableWidget>
#include <QWindow>

#include "scalelabel.h"

constexpr int kScreenshotTileWidth   = 100;
constexpr int kScreenshotTileHeight  = 80;
constexpr int kScreenshotTileSpacing = 16;

// Disables the native dotted selection border around focused elements. Unfortunately,
// disabling it via stylesheets is broken in the latest version of Qt
// https://stackoverflow.com/questions/9795791/removing-dotted-border-without-setting-nofocus-in-windows-pyqt
class NoFocusProxyStyle : public QProxyStyle
{
public:
  NoFocusProxyStyle(QStyle* baseStyle = nullptr) : QProxyStyle(baseStyle) {}

  void drawPrimitive(PrimitiveElement element, const QStyleOption* option,
                     QPainter* painter, const QWidget* widget) const
  {
    if (element == QStyle::PE_FrameFocusRect) {
      return;
    }
    QProxyStyle::drawPrimitive(element, option, painter, widget);
  }
};

FomodScreenshotDialog::FomodScreenshotDialog(
    QWidget* parent, std::vector<std::pair<QString, QString>> carouselImages,
    int carouselIndex)
    : QDialog(parent, Qt::FramelessWindowHint), ui(new Ui::FomodScreenshotDialog),
      m_carouselImages(carouselImages)
{
  Q_INIT_RESOURCE(resources);

  ui->setupUi(this);
  setAttribute(Qt::WA_TranslucentBackground);

  // Manually maximize the dialog since showMaximized() clips over the taskbar
  QScreen* screen         = this->screen();
  QRect availableGeometry = screen->availableGeometry();
  setFixedSize(availableGeometry.width(), availableGeometry.height());
  move(availableGeometry.x(), availableGeometry.y());

  QTableWidget* carouselList = ui->carouselList;
  carouselList->setStyle(new NoFocusProxyStyle);

  carouselList->setRowCount(1);
  carouselList->setRowHeight(0, kScreenshotTileHeight);
  carouselList->setColumnCount(0);
  for (auto carouselImage : m_carouselImages) {
    QFrame* container   = new QFrame(carouselList);
    QVBoxLayout* layout = new QVBoxLayout(container);
    layout->setContentsMargins(0, 0, 0, 0);
    container->setLayout(layout);

    ScaleLabel* scaleLabel = new ScaleLabel(container);
    scaleLabel->setScalableResource(carouselImage.second);
    scaleLabel->setFixedSize(kScreenshotTileWidth, kScreenshotTileHeight);
    scaleLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    scaleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    scaleLabel->setCursor(Qt::PointingHandCursor);
    scaleLabel->setStatic(true);

    layout->addWidget(scaleLabel);
    layout->setAlignment(scaleLabel, Qt::AlignLeft | Qt::AlignVCenter);

    int column = carouselList->columnCount();
    carouselList->setColumnCount(column + 1);
    carouselList->setColumnWidth(
        column, kScreenshotTileWidth +
                    (column + 1 == carouselImages.size() ? 0 : kScreenshotTileSpacing));

    QTableWidgetItem* item = new QTableWidgetItem("");
    carouselList->setItem(0, column, item);
    carouselList->setCellWidget(0, column, container);
  }

  connect(carouselList, &QTableWidget::itemSelectionChanged, this,
          &FomodScreenshotDialog::selectedScreenshotChanged);
  ui->carouselList->selectColumn(carouselIndex);
}

FomodScreenshotDialog::~FomodScreenshotDialog()
{
  delete ui;
}

void FomodScreenshotDialog::on_closeButton_clicked()
{
  close();
}

void FomodScreenshotDialog::on_navigateLeft_clicked()
{
  int selectedColumn = getSelectedScreenshot();
  if (selectedColumn == 0) {
    return;
  }

  ui->carouselList->selectColumn(selectedColumn - 1);
}

void FomodScreenshotDialog::on_navigateRight_clicked()
{
  int selectedColumn = getSelectedScreenshot();
  if (selectedColumn == m_carouselImages.size() - 1) {
    return;
  }

  ui->carouselList->selectColumn(selectedColumn + 1);
}

void FomodScreenshotDialog::selectedScreenshotChanged()
{
  // In case the user ctrl+clicks current selection to result in an empty selection
  if (ui->carouselList->selectedItems().isEmpty()) {
    ui->carouselList->selectColumn(0);
    return;
  }

  int selectedColumn = getSelectedScreenshot();

  ui->imageTitleLabel->setText(m_carouselImages.at(selectedColumn).first);
  ui->image->setScalableResource(m_carouselImages.at(selectedColumn).second);
  ui->slideshowPosition->setText(QString("%1/%2").arg(
      QString::number(selectedColumn + 1), QString::number(m_carouselImages.size())));

  for (int column = 0; column < ui->carouselList->columnCount(); column++) {
    QWidget* widget        = ui->carouselList->cellWidget(0, column);
    ScaleLabel* scaleLabel = widget->findChild<ScaleLabel*>();
    if (column == selectedColumn) {
      scaleLabel->setStyleSheet(scaleLabel->styleSheet() +
                                "QLabel { border:2px solid white; }");
    } else {
      scaleLabel->setStyleSheet(scaleLabel->styleSheet() + "QLabel { border:none; }");
    }
  }
}

int FomodScreenshotDialog::getSelectedScreenshot()
{
  return ui->carouselList->selectedItems().front()->column();
}