summaryrefslogtreecommitdiff
path: root/src/modinfodialogimages.h
blob: 60ce9def784c55fc10c27d0ddef31114f23c99b7 (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
#ifndef MODINFODIALOGIMAGES_H
#define MODINFODIALOGIMAGES_H

#include "modinfodialogtab.h"
#include "filterwidget.h"
#include <QScrollBar>

class ImagesTab;

// vertical scrollbar, this is only to handle wheel events to scroll by one
// instead of the system's scroll setting
//
class ImagesScrollbar : public QScrollBar
{
public:
  using QScrollBar::QScrollBar;
  void setTab(ImagesTab* tab);

protected:
  // forwards to ImagesTab::thumbnailAreaWheelEvent()
  //
  void wheelEvent(QWheelEvent* event) override;

private:
  ImagesTab* m_tab = nullptr;
};


// widget inside the scroller, calls ImagesTab::paintThumbnailArea() when
// needed and also forwards mouse clicks and tooltip events
//
class ImagesThumbnails : public QWidget
{
  Q_OBJECT;

public:
  using QWidget::QWidget;
  void setTab(ImagesTab* tab);

protected:
  // forwards to ImagesTab::paintThumbnailArea()
  //
  void paintEvent(QPaintEvent* e) override;

  // forwards to ImagesTab::thumbnailAreaMouseEvent()
  //
  void mousePressEvent(QMouseEvent* e) override;

  // forwards to ImagesTab::thumbnailAreaWheelEvent()
  //
  void wheelEvent(QWheelEvent* e);

  // forwards to ImagesTab::scrollAreaResized()
  //
  void resizeEvent(QResizeEvent* e) override;

  // forwards to ImagesTab::showTooltip for tooltip events
  //
  bool event(QEvent* e) override;

private:
  ImagesTab* m_tab = nullptr;
};


// a widget that draws an image scaled to fit while keeping the aspect ratio
//
class ScalableImage : public QWidget
{
  Q_OBJECT;

public:
  ScalableImage(QString path={});

  // sets the image to draw
  void setImage(const QString& path);
  void setImage(QImage image);

  // removes the image, won't draw the border nor the image
  void clear();

  // tells the QWidget's layout manager this widget is always square
  bool hasHeightForWidth() const override;
  int heightForWidth(int w) const override;

protected:
  void paintEvent(QPaintEvent* e) override;

private:
  QString m_path;
  QImage m_original, m_scaled;
  int m_border;
};


// handles all the geometry calculations by ImagesTab for painting or handling
// mouse clicks
//
// a thumbnail looks like this:
//
//   +-----------------------+ <-- thumb rect
//   |   margins             |
//   |                       |
//   |   +-border--------+ <------ border rect
//   |   | padding       |   |
//   |   |               |   |
//   |   |   +-------+ <----------- image rect
//   |   |   |       |   |   |
//   |   |   | image |   |   |
//   |   |   |       |   |   |
//   |   |   +-------+   |   |
//   |   |               |   |
//   |   +---------------+   |
//   |                       |
//   +-----------------------+
//
//   spacing
//
//   +-----------------------+ <-- thumb rect
//   |   margins             |
//   |                       |
//     ....
//
//
class ImagesGeometry
{
public:
  // returned by indexAt() if the point is outside any possible thumbnail
  static constexpr std::size_t BadIndex =
    std::numeric_limits<std::size_t>::max();

  ImagesGeometry(
    const QSize& widgetSize, int margins, int border, int padding, int spacing);

  // returns the number of images fully visible in the widget
  //
  std::size_t fullyVisibleCount() const;

  // rectangle around the whole thumbnail
  //
  QRect thumbRect(std::size_t i) const;

  // rectangle of the border for the given thumbnail
  //
  QRect borderRect(std::size_t i) const;

  // rectangle of the image for the given thumbnail
  //
  QRect imageRect(std::size_t i) const;

  // returns the index of the image at the given point; this does not take into
  // account any scrolling, the image at the top of widget is always 0
  //
  std::size_t indexAt(const QPoint& p) const;

  // returns the size of the image that fits in imageRect() while keeping the
  // same aspect ratio as the given one
  //
  QSize scaledImageSize(const QSize& originalSize) const;

  // dumps stuff to qDebug()
  //
  void dump() const;

private:
  // size of the widget containing all the thumbnails
  const QSize m_widgetSize;

  // space outside the thumbnail border
  const int m_margins;

  // size of the border
  const int m_border;

  // space between the border and the image
  const int m_padding;

  // spacing between thumbnails
  const int m_spacing;

  // rectangle of the first thumbnail on top
  const QRect m_topRect;


  // calculates the top rectangle
  //
  QRect calcTopRect() const;
};


class ImagesTab : public ModInfoDialogTab
{
  Q_OBJECT;
  friend class ImagesScrollbar;
  friend class ImagesThumbnails;

public:
  ImagesTab(
    OrganizerCore& oc, PluginContainer& plugin,
    QWidget* parent, Ui::ModInfoDialog* ui, int id);

  void clear() override;
  bool feedFile(const QString& rootPath, const QString& fullPath) override;
  void update() override;
  void saveState(Settings& s) override;
  void restoreState(const Settings& s) override;

private:
  struct File
  {
    QString path;
    QImage original, thumbnail;
    bool failed = false;

    File(QString path)
      : path(std::move(path))
    {
    }
  };

  ScalableImage* m_image;
  std::vector<File> m_files;
  std::vector<File*> m_filteredFiles;
  std::vector<QString> m_supportedFormats;
  int m_margins, m_border, m_padding, m_spacing;
  const File* m_selection;
  FilterWidget m_filter;
  bool m_ddsAvailable, m_ddsEnabled;

  void getSupportedFormats();
  void enableDDS(bool b);
  void select(const File* file);
  bool needsFiltering() const;

  void scrollAreaResized(const QSize& s);
  void paintThumbnailsArea(QPaintEvent* e);
  void thumbnailAreaMouseEvent(QMouseEvent* e);
  void thumbnailAreaWheelEvent(QWheelEvent* e);
  void onScrolled();

  void showTooltip(QHelpEvent* e);
  void onExplore();
  void onShowDDS();
  void onFilterChanged();

  ImagesGeometry makeGeometry() const;

  void paintThumbnail(
    QPainter& painter, const ImagesGeometry& geo,
    File& file, std::size_t i);

  void paintThumbnailBorder(
    QPainter& painter, const ImagesGeometry& geo,
    std::size_t i);

  void paintThumbnailImage(
    QPainter& painter, const ImagesGeometry& geo,
    File& file, std::size_t i);

  const File* fileAtPos(const QPoint& p) const;

  std::size_t fileCount() const;
  const File* getFile(std::size_t i) const;
  File* getFile(std::size_t i);

  void filterImages();
  bool needsReload(const ImagesGeometry& geo, const File& file) const;
  void reload(const ImagesGeometry& geo, File& file);
  void resizeWidget();
};

#endif // MODINFODIALOGIMAGES_H