ff7tk  1.2.0.13
Work with Final Fantasy 7 game data
ImageGridWidget.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2009 - 2022 Arzel Jérôme <myst6re@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-or-later
3 
4 #pragma once
5 
6 #include <QWidget>
7 
8 #include <ff7tkqtwidgets_export.h>
9 
10 typedef QPoint Cell;
11 
12 class FF7TKQTWIDGETS_EXPORT ImageGridWidget : public QWidget
13 {
14  Q_OBJECT
15 public:
19  MultiSelection
20  };
21  explicit ImageGridWidget(QWidget *parent = nullptr);
22  inline const QPixmap &pixmap() const {
23  return _pixmap;
24  }
25  void setPixmap(const QPixmap &pixmap);
26  void setPixmap(const QPoint &point, const QPixmap &pixmap);
27  inline QPoint pixmapPoint() const {
28  return _pixmapPoint;
29  }
30  void setPixmapPoint(const QPoint &point);
31  inline int cellSize() const {
32  return _cellSize;
33  }
34  void setCellSize(int size);
35  inline int groupedCellSize() const {
36  return _groupedCellSize;
37  }
38  void setGroupedCellSize(int size);
39  void setCustomLines(const QList<QLine> &lines);
40  inline QSize gridSize() const {
41  return _gridSize.isValid() ? _gridSize : (_cellSize == 0 ? QSize() : _pixmap.size() / _cellSize);
42  }
43  inline QSize gridSizePixel() const {
44  return _gridSize.isValid() ? _gridSize * _cellSize : _pixmap.size();
45  }
46  void setGridSize(const QSize &gridSize);
47  QPixmap cellPixmap(const Cell &cell) const;
48  inline const QList<Cell> &selectedCells() const {
49  return _selectedCells;
50  }
51  inline SelectionMode selectionMode() const {
52  return _selectionMode;
53  }
54  void setSelectionMode(SelectionMode mode);
55 signals:
56  void currentSelectionChanged(const QList<Cell> &cells);
57  void highlighted(const Cell &cell);
58  void clicked(const Cell &cell);
59 public slots:
60  inline void setSelectedCell(const Cell &cell) {
61  setSelectedCells(QList<Cell>() << cell);
62  }
63  void setSelectedCells(const QList<Cell> &cells);
64 protected:
65  virtual void paintEvent(QPaintEvent *event) override;
66  virtual void mouseMoveEvent(QMouseEvent *event) override;
67  virtual void leaveEvent(QEvent *event) override;
68  virtual void mousePressEvent(QMouseEvent *event) override;
69  virtual void mouseReleaseEvent(QMouseEvent *event) override;
70  virtual void keyPressEvent(QKeyEvent *event) override;
71  virtual void resizeEvent(QResizeEvent *event) override;
72  virtual QSize minimumSizeHint() const override;
73  virtual QSize sizeHint() const override;
74 private:
75  QList<QLine> createGrid(const QSize &gridS, int cellSize);
76  void drawSelection(QPainter &painter, QPoint selection);
77  inline QPoint scaledPoint(const Cell &cell) const {
78  return _scaledRatio * cell;
79  }
80  Cell getCell(const QPoint &pos) const;
81  bool cellIsInRange(const Cell &cell) const;
82  void updateGrid();
83  void clearHover();
84 
85  QPixmap _pixmap;
86  QList<QLine> _gridLines, _groupedGridLines, _customLines;
87  Cell _hoverCell, _startMousePress;
88  QList<Cell> _selectedCells;
89  QPoint _pixmapPoint, _scaledPixmapPoint, _scaledGridPoint;
90  QSize _scaledPixmapSize;
91  QSize _gridSize;
92  double _scaledRatio;
93  SelectionMode _selectionMode;
94  int _cellSize, _groupedCellSize;
95 };
ImageGridWidget::setSelectedCell
void setSelectedCell(const Cell &cell)
Definition: ImageGridWidget.h:60
ImageGridWidget::groupedCellSize
int groupedCellSize() const
Definition: ImageGridWidget.h:35
ImageGridWidget::SelectionMode
SelectionMode
Definition: ImageGridWidget.h:16
ImageGridWidget::gridSizePixel
QSize gridSizePixel() const
Definition: ImageGridWidget.h:43
ImageGridWidget::selectedCells
const QList< Cell > & selectedCells() const
Definition: ImageGridWidget.h:48
ImageGridWidget::cellSize
int cellSize() const
Definition: ImageGridWidget.h:31
ImageGridWidget::SingleSelection
@ SingleSelection
Definition: ImageGridWidget.h:18
ImageGridWidget::pixmapPoint
QPoint pixmapPoint() const
Definition: ImageGridWidget.h:27
ImageGridWidget::selectionMode
SelectionMode selectionMode() const
Definition: ImageGridWidget.h:51
ImageGridWidget::gridSize
QSize gridSize() const
Definition: ImageGridWidget.h:40
Cell
QPoint Cell
Definition: ImageGridWidget.h:10
ImageGridWidget::NoSelection
@ NoSelection
Definition: ImageGridWidget.h:17
ImageGridWidget::pixmap
const QPixmap & pixmap() const
Definition: ImageGridWidget.h:22
ImageGridWidget
Definition: ImageGridWidget.h:12