Skip to content

Commit

Permalink
Merge pull request #53 from UmbrellaLeaf5/gui_interaction_buttons
Browse files Browse the repository at this point in the history
Gui interaction buttons
  • Loading branch information
UmbrellaLeaf5 authored Apr 20, 2024
2 parents 9bdd27d + 3cfdc89 commit 8bab892
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 11 deletions.
2 changes: 2 additions & 0 deletions gui/hill.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class Hill : public Drawable {
const std::vector<lib::Point>& GetPoints() const { return data_.GetPoints(); }
std::vector<lib::Point>& GetPoints() { return data_.GetPoints(); }

void AddVertice(lib::Point vertice) { data_.AddVertice(vertice); }

void Draw(QCustomPlot* plot) override;

/**
Expand Down
8 changes: 8 additions & 0 deletions gui/trappy_line.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ class TrappyLine : public Drawable {
TrappyLine& operator=(const TrappyLine&) = default;
TrappyLine& operator=(TrappyLine&&) = default;

void SetFirstTarget(gui::Target* target) {
SetTargets(target, targets_.second);
}

void SetSecondTarget(gui::Target* target) {
SetTargets(targets_.first, target);
}

void SetTargets(gui::Target* first_target, gui::Target* second_target) {
UpdateData(first_target, second_target);
}
Expand Down
2 changes: 2 additions & 0 deletions lib/hill.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class Hill : public JSONable {
const std::vector<Point>& GetPoints() const { return vertices_; }
std::vector<Point>& GetPoints() { return vertices_; }

void AddVertice(Point vertice) { vertices_.push_back(vertice); }

private:
std::vector<Point> vertices_;
};
Expand Down
6 changes: 3 additions & 3 deletions main/mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "mainwindow.h"

#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent),
ui(new Ui::MainWindow),
Expand All @@ -14,8 +12,10 @@ MainWindow::MainWindow(QWidget* parent)
ui->plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom |
QCP::iSelectPlottables | QCP::iSelectItems);

connect(ui->plot, SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
SLOT(mousePressObjectsButton(QMouseEvent*)));
connect(ui->plot, SIGNAL(mousePress(QMouseEvent*)), this,
SLOT(on_plot_MousePressed()));
SLOT(mousePressDiscard(QMouseEvent*)));

area_->Setup(manager_.get());
t_connection_->Setup(manager_.get(), area_.get());
Expand Down
36 changes: 32 additions & 4 deletions main/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <QMainWindow>

#include "./ui_mainwindow.h"
#include "add_objects_forms/add_hill_form.h"
#include "add_objects_forms/add_target_form.h"
#include "add_objects_forms/add_trappy_circle_form.h"
Expand All @@ -11,6 +12,14 @@
#include "data_tools/tables_connection/tables_connection.h"
#include "gui_json_file/gui_json_file.h"

enum class CursorType {
TargetCursor,
TrCircleCursor,
TrLineCursor,
HillCursor,
DefaultCursor
};

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
Expand All @@ -25,8 +34,6 @@ class MainWindow : public QMainWindow {
MainWindow(QWidget* parent = nullptr);
~MainWindow();

void closeEvent(QCloseEvent* event) override;

private slots:
void on_pushButtonAddTarget_clicked();
void on_pushButtonAddTrappyCircle_clicked();
Expand All @@ -40,8 +47,6 @@ class MainWindow : public QMainWindow {
void on_actionTrappy_Line_triggered();
void on_actionHill_triggered();

void on_plot_MousePressed();

void on_actionBeautify_triggered();

bool on_actionSave_as_triggered();
Expand All @@ -50,6 +55,27 @@ class MainWindow : public QMainWindow {
void on_actionOpen_triggered();
void on_actionNew_triggered();


void mousePressObjectsButton(QMouseEvent* mouse_event);
void DisconnectObject(gui::ObjectType obj_type);
void mousePressDiscard(QMouseEvent* mouse_event);
void closeEvent(QCloseEvent* event) override;

// Слоты для TrappyCircle
void mousePressSetRadiusFromPlot(QMouseEvent* mouse_event) {
DisconnectObject(gui::ObjectType::TrappyCircles);
}
void mousePressDiscardTrappyCircle(QMouseEvent* mouse_event);
void mouseMoveSetRadiusFromPlot(QMouseEvent* mouse_event);

// Слоты для TrappyLine
void mousePressSelectSecondTarget(QMouseEvent* mouse_event);
void mousePressDiscardTrappyLine(QMouseEvent* mouse_event);

// Слоты для Hill
void mousePressAddVertice(QMouseEvent* mouse_event);
void mousePressDiscardHill(QMouseEvent* mouse_event);

void on_targetAddFromTablePushButton_clicked();
void on_hillAddFromTablePushButton_clicked();
void on_trappyCircleAddFromTablePushButton_clicked();
Expand All @@ -68,6 +94,8 @@ class MainWindow : public QMainWindow {

Ui::MainWindow* ui;
GuiJsonFile json_file_;
CursorType cursor_ = CursorType::DefaultCursor;

bool OpenMessageWindow();
gui::ObjectType GetObjType(CursorType cursor_type);
};
223 changes: 219 additions & 4 deletions main/mainwindow_connections.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
#include "mainwindow.h"
#include <cmath>

#include "mainwindow.h"
// здесь описаны все соединения кнопок со слотами

#include "./ui_mainwindow.h"
gui::ObjectType MainWindow::GetObjType(CursorType cursor_type) {
switch (cursor_type) {
case CursorType::TargetCursor:
case CursorType::DefaultCursor:
return gui::ObjectType::Targets;
case CursorType::TrCircleCursor:
return gui::ObjectType::TrappyCircles;
case CursorType::TrLineCursor:
return gui::ObjectType::TrappyLines;
case CursorType::HillCursor:
return gui::ObjectType::Hills;
}
}

// MARK: Add functions

Expand Down Expand Up @@ -42,23 +55,39 @@ void MainWindow::AddHill(std::vector<lib::Point> points) {
// MARK: Cursors buttons

void MainWindow::on_pushButtonAddTarget_clicked() {
if (cursor_ != CursorType::DefaultCursor)
DisconnectObject(GetObjType(cursor_));

ui->plot->setCursor(QCursor(QPixmap("../images/target.png")
.scaled(QSize(24, 24), Qt::KeepAspectRatio)));
cursor_ = CursorType::TargetCursor;
}

void MainWindow::on_pushButtonAddTrappyCircle_clicked() {
if (cursor_ != CursorType::DefaultCursor)
DisconnectObject(GetObjType(cursor_));

ui->plot->setCursor(QCursor(
QPixmap("../images/AA.png").scaled(QSize(24, 24), Qt::KeepAspectRatio)));
cursor_ = CursorType::TrCircleCursor;
}

void MainWindow::on_pushButtonAddTrappyLine_clicked() {
if (cursor_ != CursorType::DefaultCursor)
DisconnectObject(GetObjType(cursor_));

ui->plot->setCursor(QCursor(QPixmap("../images/enemy.png")
.scaled(QSize(24, 24), Qt::KeepAspectRatio)));
cursor_ = CursorType::TrLineCursor;
}

void MainWindow::on_pushButtonAddHill_clicked() {
if (cursor_ != CursorType::DefaultCursor)
DisconnectObject(GetObjType(cursor_));

ui->plot->setCursor(QCursor(QPixmap("../images/high_hills.png")
.scaled(QSize(24, 24), Qt::KeepAspectRatio)));
cursor_ = CursorType::HillCursor;
}

// MARK: Actions trig.
Expand Down Expand Up @@ -101,9 +130,195 @@ void MainWindow::on_actionBeautify_triggered() {
ui->plot->replot();
}

void MainWindow::on_plot_MousePressed() {
void MainWindow::DisconnectObject(gui::ObjectType obj_type) {
switch (obj_type) {
case gui::ObjectType::TrappyCircles: {
disconnect(ui->plot, SIGNAL(mouseMove(QMouseEvent*)), this,
SLOT(mouseMoveSetRadiusFromPlot(QMouseEvent*)));
disconnect(ui->plot, SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
SLOT(mousePressSetRadiusFromPlot(QMouseEvent*)));
disconnect(ui->plot, SIGNAL(mousePress(QMouseEvent*)), this,
SLOT(mousePressDiscardTrappyCircle(QMouseEvent*)));
break;
}
case gui::ObjectType::TrappyLines: {
disconnect(ui->plot, SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
SLOT(mousePressSelectSecondTarget(QMouseEvent*)));
disconnect(ui->plot, SIGNAL(mousePress(QMouseEvent*)), this,
SLOT(mousePressDiscardTrappyLine(QMouseEvent*)));
break;
}
case gui::ObjectType::Hills: {
disconnect(ui->plot, SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
SLOT(mousePressAddVertice(QMouseEvent*)));
disconnect(ui->plot, SIGNAL(mousePress(QMouseEvent*)), this,
SLOT(mousePressDiscardHill(QMouseEvent*)));
}
default:
break;
}
connect(ui->plot, SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
SLOT(mousePressObjectsButton(QMouseEvent*)));

ui->plot->setCursor(Qt::CrossCursor);
ui->plot->replot();
cursor_ = CursorType::DefaultCursor;
}

void MainWindow::mousePressDiscard(QMouseEvent* mouse_event) {
if (mouse_event->button() == Qt::RightButton) {
cursor_ = CursorType::DefaultCursor;
ui->plot->setCursor(Qt::CrossCursor);
}
}

void MainWindow::mousePressObjectsButton(QMouseEvent* mouse_event) {
double x = ui->plot->xAxis->pixelToCoord(mouse_event->pos().x());
double y = ui->plot->yAxis->pixelToCoord(mouse_event->pos().y());

switch (cursor_) {
case CursorType::TargetCursor: {
manager_->Add(new gui::Target(x, y));

ui->plot->setCursor(Qt::CrossCursor);
cursor_ = CursorType::DefaultCursor;
break;
}
case CursorType::TrCircleCursor: {
manager_->Add(new gui::TrappyCircle({x, y}, 0));
disconnect(ui->plot, SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
SLOT(mousePressObjectsButton(QMouseEvent*)));

connect(ui->plot, SIGNAL(mouseMove(QMouseEvent*)), this,
SLOT(mouseMoveSetRadiusFromPlot(QMouseEvent*)));
connect(ui->plot, SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
SLOT(mousePressSetRadiusFromPlot(QMouseEvent*)));
connect(ui->plot, SIGNAL(mousePress(QMouseEvent*)), this,
SLOT(mousePressDiscardTrappyCircle(QMouseEvent*)));

ui->plot->setCursor(Qt::ClosedHandCursor);
break;
}
case CursorType::TrLineCursor: {
if (!ui->plot->selectedGraphs().empty()) {
for (const auto& t_ptr : manager_->GetTargetsPtrs()) {
if (t_ptr->GetGraphPtr() == ui->plot->selectedGraphs()[0]) {
manager_->Add(new gui::TrappyLine(t_ptr, t_ptr));
break;
}
}
disconnect(ui->plot, SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
SLOT(mousePressObjectsButton(QMouseEvent*)));

connect(ui->plot, SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
SLOT(mousePressSelectSecondTarget(QMouseEvent*)));
connect(ui->plot, SIGNAL(mousePress(QMouseEvent*)), this,
SLOT(mousePressDiscardTrappyLine(QMouseEvent*)));
}
break;
}
case CursorType::HillCursor: {
manager_->Add(new gui::Hill{{x, y}, {x, y}});

disconnect(ui->plot, SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
SLOT(mousePressObjectsButton(QMouseEvent*)));

connect(ui->plot, SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
SLOT(mousePressAddVertice(QMouseEvent*)));
connect(ui->plot, SIGNAL(mousePress(QMouseEvent*)), this,
SLOT(mousePressDiscardHill(QMouseEvent*)));
break;
}
default:
break;
}
area_->Redraw();
t_connection_->UpdateTables();
}

void MainWindow::mouseMoveSetRadiusFromPlot(QMouseEvent* mouse_event) {
size_t last = manager_->GetTrappyCircles().size() - 1;

// Расстояние от центра до курсора (по оси x)
double x = abs(ui->plot->xAxis->pixelToCoord(mouse_event->pos().x()) -
manager_->GetTrappyCircles()[last].GetCenter().x);

// Расстояние от центра до курсора (по оси y)
double y = abs(ui->plot->yAxis->pixelToCoord(mouse_event->pos().y()) -
manager_->GetTrappyCircles()[last].GetCenter().y);

// Значение радиуса
double r = pow(pow(x, 2) + pow(y, 2), 0.5);

manager_->GetTrappyCirclesPtrs()[last]->SetRadius(r);
area_->Redraw();
t_connection_->UpdateTables();
}

void MainWindow::mousePressDiscardTrappyCircle(QMouseEvent* mouse_event) {
if (mouse_event->button() == Qt::RightButton) {
size_t last = manager_->GetTrappyCircles().size() - 1;
manager_->Remove(gui::ObjectType::TrappyCircles, last);

DisconnectObject(gui::ObjectType::TrappyCircles);
area_->Redraw();
t_connection_->UpdateTables();
}
}

void MainWindow::mousePressSelectSecondTarget(QMouseEvent* mouse_event) {
if (!ui->plot->selectedGraphs().empty()) {
for (const auto& t_ptr : manager_->GetTargetsPtrs()) {
if (t_ptr->GetGraphPtr() == ui->plot->selectedGraphs()[0]) {
size_t last = manager_->GetTrappyLines().size() - 1;
if (manager_->GetTrappyLinesPtrs()[last]->GetTargetsPtrs().second !=
t_ptr) {
manager_->GetTrappyLinesPtrs()[last]->SetSecondTarget(t_ptr);

DisconnectObject(gui::ObjectType::TrappyLines);
area_->Redraw();
t_connection_->UpdateTables();
break;
}
}
}
}
}

void MainWindow::mousePressDiscardTrappyLine(QMouseEvent* mouse_event) {
if (mouse_event->button() == Qt::RightButton) {
size_t last = manager_->GetTrappyLines().size() - 1;
manager_->Remove(gui::ObjectType::TrappyLines, last);

DisconnectObject(gui::ObjectType::TrappyLines);
area_->Redraw();
t_connection_->UpdateTables();
}
}

void MainWindow::mousePressAddVertice(QMouseEvent* mouse_event) {
double x = ui->plot->xAxis->pixelToCoord(mouse_event->pos().x());
double y = ui->plot->yAxis->pixelToCoord(mouse_event->pos().y());
size_t last = manager_->GetHills().size() - 1;

if (abs(manager_->GetHills()[last].GetPoints()[0].x - x) < 0.1 &&
abs(manager_->GetHills()[last].GetPoints()[0].y - y) < 0.1) {
DisconnectObject(gui::ObjectType::Hills);
} else
manager_->GetHillsPtrs()[last]->AddVertice({x, y});

area_->Redraw();
t_connection_->UpdateTables();
}

void MainWindow::mousePressDiscardHill(QMouseEvent* mouse_event) {
if (mouse_event->button() == Qt::RightButton) {
size_t last = manager_->GetHills().size() - 1;
manager_->Remove(gui::ObjectType::Hills, last);

DisconnectObject(gui::ObjectType::Hills);
area_->Redraw();
t_connection_->UpdateTables();
}
}

// MARK: Json connections
Expand Down

0 comments on commit 8bab892

Please sign in to comment.