Mouse detection
This commit is contained in:
parent
e24837649c
commit
bcd01a95b3
5 changed files with 163 additions and 16 deletions
103
gridpreview.cpp
103
gridpreview.cpp
|
|
@ -12,19 +12,67 @@ void GridPreview::setSize(int newWidth, int newHeight)
|
|||
{
|
||||
width = newWidth;
|
||||
height = newHeight;
|
||||
|
||||
update();
|
||||
emit updatedContents();
|
||||
}
|
||||
|
||||
void GridPreview::addLine(Line &line)
|
||||
{
|
||||
lines.push_back(line);
|
||||
|
||||
update();
|
||||
emit updatedContents();
|
||||
}
|
||||
|
||||
void GridPreview::removeLine(int index)
|
||||
{
|
||||
lines.removeAt(index);
|
||||
|
||||
update();
|
||||
emit updatedContents();
|
||||
}
|
||||
|
||||
QString GridPreview::toLuaTable()
|
||||
void GridPreview::setLineColor(int index, QString color)
|
||||
{
|
||||
lines[index].setColor(color);
|
||||
|
||||
update();
|
||||
emit updatedContents();
|
||||
}
|
||||
|
||||
void GridPreview::setLineStartPoint(int index, int x, int y)
|
||||
{
|
||||
lines[index].setStartPoint(x, y);
|
||||
|
||||
update();
|
||||
emit updatedContents();
|
||||
}
|
||||
|
||||
void GridPreview::setLineEndPoint(int index, int x, int y)
|
||||
{
|
||||
lines[index].setEndPoint(x, y);
|
||||
|
||||
update();
|
||||
emit updatedContents();
|
||||
}
|
||||
|
||||
int GridPreview::getWidth()
|
||||
{
|
||||
return width;
|
||||
}
|
||||
|
||||
int GridPreview::getHeight()
|
||||
{
|
||||
return height;
|
||||
}
|
||||
|
||||
const Line &GridPreview::getLine(int index)
|
||||
{
|
||||
return lines.at(index);
|
||||
}
|
||||
|
||||
const QString GridPreview::toLuaTable()
|
||||
{
|
||||
QString output(tableTemplate.arg(width).arg(height));
|
||||
QString linesSubTable = "";
|
||||
|
|
@ -37,13 +85,15 @@ QString GridPreview::toLuaTable()
|
|||
return output.arg(linesSubTable);
|
||||
}
|
||||
|
||||
void GridPreview::paintEvent(QPaintEvent *event)
|
||||
void GridPreview::paintEvent(QPaintEvent *)
|
||||
{
|
||||
QPainter painter(this);
|
||||
QPen pen(Qt::black, 2);
|
||||
painter.setPen(pen);
|
||||
QBrush brush(Qt::black);
|
||||
bool widthBased = rect().width() > rect().height();
|
||||
double aspectRatio = (double) rect().width() / rect().height();
|
||||
double cellAspectRatio = (double) width / height;
|
||||
bool widthBased = aspectRatio > cellAspectRatio;
|
||||
int cellSize = widthBased ?
|
||||
rect().height() / height :
|
||||
rect().width() / width;
|
||||
|
|
@ -101,9 +151,54 @@ void GridPreview::paintEvent(QPaintEvent *event)
|
|||
}
|
||||
}
|
||||
|
||||
QColor GridPreview::colorFromString(QString color)
|
||||
const QColor GridPreview::colorFromString(const QString color)
|
||||
{
|
||||
if (!colorMap.contains(color))
|
||||
return colorMap[QString("red")];
|
||||
return colorMap[color];
|
||||
}
|
||||
|
||||
void GridPreview::mouseMoveEvent(QMouseEvent *event)
|
||||
{
|
||||
if (!mouseCaptured) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void GridPreview::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
mouseCaptured = true;
|
||||
|
||||
QPoint gridPos = localMousePosition(event);
|
||||
|
||||
std::cout << gridPos.x() << ',' << gridPos.y() << std::endl;
|
||||
}
|
||||
|
||||
void GridPreview::mouseReleaseEvent(QMouseEvent *event)
|
||||
{
|
||||
mouseCaptured = false;
|
||||
}
|
||||
|
||||
const QPoint GridPreview::localMousePosition(QMouseEvent *event)
|
||||
{
|
||||
QPoint position(event->position().toPoint());
|
||||
return snapToGrid(position.x(), position.y());
|
||||
}
|
||||
|
||||
const QPoint GridPreview::snapToGrid(int x, int y)
|
||||
{
|
||||
double aspectRatio = (double) rect().width() / rect().height();
|
||||
double cellAspectRatio = (double) width / height;
|
||||
bool widthBased = aspectRatio > cellAspectRatio;
|
||||
int cellSize = widthBased ?
|
||||
rect().height() / height :
|
||||
rect().width() / width;
|
||||
|
||||
int areaWidth = cellSize * width;
|
||||
int areaHeight = cellSize * height;
|
||||
|
||||
int offsetX = (rect().width() - areaWidth) / 2;
|
||||
int offsetY = (rect().height() - areaHeight) / 2;
|
||||
|
||||
return QPoint(1 + (x - offsetX) / cellSize, 1 + (y - offsetY) / cellSize);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,25 +3,46 @@
|
|||
|
||||
#include <QWidget>
|
||||
#include <QPainter>
|
||||
#include <QMouseEvent>
|
||||
#include <iostream>
|
||||
#include "line.h"
|
||||
|
||||
class GridPreview : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit GridPreview(QWidget *parent = nullptr);
|
||||
void setSize(int newWidth, int newHeight);
|
||||
void addLine(Line &line);
|
||||
void removeLine(int index);
|
||||
QString toLuaTable();
|
||||
void paintEvent(QPaintEvent* event);
|
||||
QColor colorFromString(QString color);
|
||||
private:
|
||||
int width = 5, height = 5;
|
||||
QVector<Line> lines {};
|
||||
QMap<QString, QColor> colorMap;
|
||||
const QString tableTemplate = "return {\n width = %1,\n height = %2,\n lines = {\n%3\n }\n}";
|
||||
|
||||
bool mouseCaptured = false;
|
||||
public:
|
||||
explicit GridPreview(QWidget *parent = nullptr);
|
||||
void setSize(int newWidth, int newHeight);
|
||||
|
||||
void addLine(Line &line);
|
||||
void removeLine(int index);
|
||||
|
||||
void setLineColor(int index, QString color);
|
||||
void setLineStartPoint(int index, int x, int y);
|
||||
void setLineEndPoint(int index, int x, int y);
|
||||
|
||||
int getWidth();
|
||||
int getHeight();
|
||||
const Line& getLine(int index);
|
||||
|
||||
const QString toLuaTable();
|
||||
void paintEvent(QPaintEvent* event);
|
||||
const QColor colorFromString(const QString color);
|
||||
|
||||
void mouseMoveEvent(QMouseEvent* event);
|
||||
void mousePressEvent(QMouseEvent* event);
|
||||
void mouseReleaseEvent(QMouseEvent* event);
|
||||
|
||||
const QPoint localMousePosition(QMouseEvent* event);
|
||||
const QPoint snapToGrid(int x, int y);
|
||||
signals:
|
||||
void updatedContents();
|
||||
};
|
||||
|
||||
#endif // GRIDPREVIEW_H
|
||||
|
|
|
|||
6
line.h
6
line.h
|
|
@ -5,6 +5,9 @@
|
|||
|
||||
class Line
|
||||
{
|
||||
private:
|
||||
int startX, startY, endX, endY;
|
||||
QString color;
|
||||
public:
|
||||
Line();
|
||||
void setStartPoint(int x, int y);
|
||||
|
|
@ -16,9 +19,6 @@ public:
|
|||
int getEndY();
|
||||
QString getColor();
|
||||
QString toLuaTable();
|
||||
private:
|
||||
int startX, startY, endX, endY;
|
||||
QString color;
|
||||
};
|
||||
|
||||
#endif // LINE_H
|
||||
|
|
|
|||
|
|
@ -6,9 +6,32 @@ MainWindow::MainWindow(QWidget *parent)
|
|||
, ui(new Ui::MainWindow)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
QObject::connect(ui->gridPreview, &GridPreview::updatedContents,
|
||||
this, &MainWindow::onGridUpdated);
|
||||
ui->gridPreview->updatedContents();
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void MainWindow::onGridUpdated()
|
||||
{
|
||||
ui->textPreview->setPlainText(ui->gridPreview->toLuaTable());
|
||||
|
||||
ui->spinGridWidth->setValue(ui->gridPreview->getWidth());
|
||||
ui->spinGridHeight->setValue(ui->gridPreview->getHeight());
|
||||
}
|
||||
|
||||
void MainWindow::on_spinGridWidth_valueChanged(int arg1)
|
||||
{
|
||||
ui->gridPreview->setSize(arg1, ui->gridPreview->getHeight());
|
||||
}
|
||||
|
||||
|
||||
void MainWindow::on_spinGridHeight_valueChanged(int arg1)
|
||||
{
|
||||
ui->gridPreview->setSize(ui->gridPreview->getWidth(), arg1);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,14 @@ public:
|
|||
MainWindow(QWidget *parent = nullptr);
|
||||
~MainWindow();
|
||||
|
||||
public slots:
|
||||
void onGridUpdated();
|
||||
|
||||
private slots:
|
||||
void on_spinGridWidth_valueChanged(int arg1);
|
||||
|
||||
void on_spinGridHeight_valueChanged(int arg1);
|
||||
|
||||
private:
|
||||
Ui::MainWindow *ui;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue