diff --git a/data_tools/tables_connection/tables_connection.cpp b/data_tools/tables_connection/tables_connection.cpp index 0804e50..85e23e0 100644 --- a/data_tools/tables_connection/tables_connection.cpp +++ b/data_tools/tables_connection/tables_connection.cpp @@ -10,6 +10,8 @@ void TablesConnection::Setup(DataManager* manager, PlotArea* area) { // MARK: U.T. by Targets void TablesConnection::UpdateTable(const std::vector& targets) { + DisableTablesConnections(); + if (targets.empty()) { targets_table_->setColumnCount(0); return; @@ -27,7 +29,7 @@ void TablesConnection::UpdateTable(const std::vector& targets) { item); // номер к.т. = индекс + 1 // в строки добавляем индекс на полотне и координаты - item = new QTableWidgetItem(QString::number(targets[i].GetIndexOnPlot())); + item = new QTableWidgetItem(QString::number(targets[i].GetData().GetId())); item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); targets_table_->setItem(0, static_cast(i), item); @@ -43,11 +45,14 @@ void TablesConnection::UpdateTable(const std::vector& targets) { } targets_table_->update(); + UpdateTablesConnections(); } // MARK: U.T. by Hills void TablesConnection::UpdateTable(const std::vector& hills) { + DisableTablesConnections(); + if (hills.empty()) { hills_table_->setColumnCount(0); return; @@ -75,7 +80,7 @@ void TablesConnection::UpdateTable(const std::vector& hills) { item); // номер рельефа = индекс + 1 // в строки добавляем индекс на полотне и координаты всех точек - item = new QTableWidgetItem(QString::number(hills[i].GetIndexOnPlot())); + item = new QTableWidgetItem(QString::number(hills[i].GetData().GetId())); item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); hills_table_->setItem(0, static_cast(i), item); @@ -104,12 +109,15 @@ void TablesConnection::UpdateTable(const std::vector& hills) { } hills_table_->update(); + UpdateTablesConnections(); } // MARK: U.T. by Tr. Lines void TablesConnection::UpdateTable( const std::vector& trappy_lines) { + DisableTablesConnections(); + if (trappy_lines.empty()) { tr_lines_table_->setColumnCount(0); return; @@ -127,8 +135,8 @@ void TablesConnection::UpdateTable( item); // номер линии = индекс + 1 // в строки добавляем индекс на полотне - item = - new QTableWidgetItem(QString::number(trappy_lines[i].GetIndexOnPlot())); + item = new QTableWidgetItem( + QString::number(trappy_lines[i].GetData().GetId())); item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); tr_lines_table_->setItem(0, static_cast(i), item); @@ -154,15 +162,27 @@ void TablesConnection::UpdateTable( item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled); tr_lines_table_->setItem(2, static_cast(i), item); + + // если эти номера остались огромными, то валидные к.т. не нашлись + if (t_1_n == ULLONG_MAX || t_2_n == ULLONG_MAX) { + QMessageBox::warning(targets_table_.get(), "Error!", + "Wrong targets numbers in TrappyLines!"); + + manager_->Remove(gui::ObjectType::TrappyLines, i); + area_->Redraw(); + } } tr_lines_table_->update(); + UpdateTablesConnections(); } // MARK: U.T. by Tr. Circles void TablesConnection::UpdateTable( const std::vector& trappy_circles) { + DisableTablesConnections(); + if (trappy_circles.empty()) { tr_circles_table_->setColumnCount(0); return; @@ -181,7 +201,7 @@ void TablesConnection::UpdateTable( // в строки добавляем индекс на полотне, координаты точки и радиус item = new QTableWidgetItem( - QString::number(trappy_circles[i].GetIndexOnPlot())); + QString::number(trappy_circles[i].GetData().GetId())); item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); tr_circles_table_->setItem(0, static_cast(i), item); @@ -204,6 +224,27 @@ void TablesConnection::UpdateTable( } tr_circles_table_->update(); + UpdateTablesConnections(); +} + +void TablesConnection::UpdateTable(gui::ObjectType obj_type) { + switch (obj_type) { + case gui::ObjectType::Targets: + UpdateTable(manager_->GetTargets()); + break; + + case gui::ObjectType::Hills: + UpdateTable(manager_->GetHills()); + break; + + case gui::ObjectType::TrappyCircles: + UpdateTable(manager_->GetTrappyCircles()); + break; + + case gui::ObjectType::TrappyLines: + UpdateTable(manager_->GetTrappyLines()); + break; + } } void TablesConnection::UpdateTables() { @@ -234,7 +275,10 @@ void TablesConnection::TargetsItemChanged(int row, int column) { } catch (const std::exception& e) { manager_->Remove(gui::ObjectType::Targets, static_cast(column)); - UpdateTables(); + UpdateTable(gui::ObjectType::Targets); + + // (в случае удаления к.т., которая была привязана, надо обновить) + UpdateTable(gui::ObjectType::TrappyLines); area_->Redraw(); QMessageBox::critical(targets_table_.get(), "Error!", e.what()); @@ -277,7 +321,7 @@ void TablesConnection::HillsItemChanged(int row, int column) { } catch (const std::exception& e) { manager_->Remove(gui::ObjectType::Hills, static_cast(column)); - UpdateTables(); + UpdateTable(gui::ObjectType::Hills); area_->Redraw(); QMessageBox::critical(targets_table_.get(), "Error!", e.what()); @@ -307,7 +351,7 @@ void TablesConnection::TrappyCirclesItemChanged(int row, int column) { } catch (const std::exception& e) { manager_->Remove(gui::ObjectType::TrappyCircles, static_cast(column)); - UpdateTables(); + UpdateTable(gui::ObjectType::TrappyCircles); area_->Redraw(); QMessageBox::critical(targets_table_.get(), "Error!", e.what()); @@ -343,7 +387,7 @@ void TablesConnection::TrappyLinesItemChanged(int row, int column) { } catch (const std::exception& e) { manager_->Remove(gui::ObjectType::TrappyLines, static_cast(column)); - UpdateTables(); + UpdateTable(gui::ObjectType::TrappyLines); area_->Redraw(); QMessageBox::warning(targets_table_.get(), "Error!", e.what()); @@ -353,46 +397,74 @@ void TablesConnection::TrappyLinesItemChanged(int row, int column) { // MARK: Remove Items void TablesConnection::RemoveTargetItem() { + DisableTablesConnections(); + manager_->Remove(gui::ObjectType::Targets, selected_column_); area_->Redraw(); UpdateTable(manager_->GetTargets()); UpdateTable(manager_->GetTrappyLines()); + + UpdateTablesConnections(); } void TablesConnection::RemoveHillItem() { + DisableTablesConnections(); + manager_->Remove(gui::ObjectType::Hills, selected_column_); area_->Redraw(); UpdateTable(manager_->GetHills()); + + UpdateTablesConnections(); } void TablesConnection::RemoveTrappyCircleItem() { + DisableTablesConnections(); + manager_->Remove(gui::ObjectType::TrappyCircles, selected_column_); area_->Redraw(); UpdateTable(manager_->GetTrappyCircles()); + + UpdateTablesConnections(); } void TablesConnection::RemoveTrappyLineItem() { + DisableTablesConnections(); + manager_->Remove(gui::ObjectType::TrappyLines, selected_column_); area_->Redraw(); UpdateTable(manager_->GetTrappyLines()); + + UpdateTablesConnections(); } // MARK: Update Connections void TablesConnection::UpdateTablesConnections() { - { - QObject::connect(targets_table_.get(), &QTableWidget::cellChanged, this, - &TablesConnection::TargetsItemChanged); + QObject::connect(targets_table_.get(), &QTableWidget::cellChanged, this, + &TablesConnection::TargetsItemChanged); - QObject::connect(hills_table_.get(), &QTableWidget::cellChanged, this, - &TablesConnection::HillsItemChanged); + QObject::connect(hills_table_.get(), &QTableWidget::cellChanged, this, + &TablesConnection::HillsItemChanged); - QObject::connect(tr_circles_table_.get(), &QTableWidget::cellChanged, this, - &TablesConnection::TrappyCirclesItemChanged); + QObject::connect(tr_circles_table_.get(), &QTableWidget::cellChanged, this, + &TablesConnection::TrappyCirclesItemChanged); - QObject::connect(tr_lines_table_.get(), &QTableWidget::cellChanged, this, - &TablesConnection::TrappyLinesItemChanged); - } + QObject::connect(tr_lines_table_.get(), &QTableWidget::cellChanged, this, + &TablesConnection::TrappyLinesItemChanged); +} + +void TablesConnection::DisableTablesConnections() { + QObject::disconnect(targets_table_.get(), &QTableWidget::cellChanged, this, + &TablesConnection::TargetsItemChanged); + + QObject::disconnect(hills_table_.get(), &QTableWidget::cellChanged, this, + &TablesConnection::HillsItemChanged); + + QObject::disconnect(tr_circles_table_.get(), &QTableWidget::cellChanged, this, + &TablesConnection::TrappyCirclesItemChanged); + + QObject::disconnect(tr_lines_table_.get(), &QTableWidget::cellChanged, this, + &TablesConnection::TrappyLinesItemChanged); } void TablesConnection::UpdateRemoveButtonConnections() { diff --git a/data_tools/tables_connection/tables_connection.h b/data_tools/tables_connection/tables_connection.h index d23914a..d1fece4 100644 --- a/data_tools/tables_connection/tables_connection.h +++ b/data_tools/tables_connection/tables_connection.h @@ -52,28 +52,10 @@ class TablesConnection : public QObject { void UpdateTables(); /** - * @brief Обновляет значения таблицы с Targets - * @param targets: вектор новых значений - */ - void UpdateTable(const std::vector& targets); - - /** - * @brief Обновляет значения таблицы с Hills - * @param hills: вектор новых значений + * @brief Обновляет значения таблицы с определенным типом объекта + * @param obj_type: тип объекта */ - void UpdateTable(const std::vector& hills); - - /** - * @brief Обновляет значения таблицы с TrappyLines - * @param trappy_lines: вектор новых значений - */ - void UpdateTable(const std::vector& trappy_lines); - - /** - * @brief Обновляет значения таблицы с TrappyCircles - * @param trappy_circles: вектор новых значений - */ - void UpdateTable(const std::vector& trappy_circles); + void UpdateTable(gui::ObjectType obj_type); private slots: void TargetsItemChanged(int row, int column); @@ -125,8 +107,33 @@ class TablesConnection : public QObject { } private: + /** + * @brief Обновляет значения таблицы с Targets + * @param targets: вектор новых значений + */ + void UpdateTable(const std::vector& targets); + + /** + * @brief Обновляет значения таблицы с Hills + * @param hills: вектор новых значений + */ + void UpdateTable(const std::vector& hills); + + /** + * @brief Обновляет значения таблицы с TrappyLines + * @param trappy_lines: вектор новых значений + */ + void UpdateTable(const std::vector& trappy_lines); + + /** + * @brief Обновляет значения таблицы с TrappyCircles + * @param trappy_circles: вектор новых значений + */ + void UpdateTable(const std::vector& trappy_circles); + int selected_column_{INT_MAX}; void UpdateTablesConnections(); + void DisableTablesConnections(); void UpdateRemoveButtonConnections(); std::unique_ptr targets_table_{nullptr}; diff --git a/gui/airport.cpp b/gui/airport.cpp index ec360f5..7e1718d 100644 --- a/gui/airport.cpp +++ b/gui/airport.cpp @@ -2,7 +2,7 @@ void gui::Airport::Draw(QCustomPlot* plot) { Target::Draw(plot); - auto graph = plot->graph(static_cast(Target::GetIndexOnPlot())); + auto graph = Target::GetGraphPtr(); graph->setPen(QColor(0, 0, 0, 255)); graph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCrossCircle, 10)); diff --git a/gui/base.h b/gui/base.h index b00ee9f..36adb27 100644 --- a/gui/base.h +++ b/gui/base.h @@ -15,17 +15,6 @@ class Drawable { */ virtual void Draw(QCustomPlot* plot) = 0; - /** - * @brief Возвращает индекс на полотне [plottable] - * @return size_t: индекс - */ - virtual size_t GetIndexOnPlot() const { return index_on_plot_; } - - protected: - virtual void SetIndexOnPlot(QCustomPlot* plot) { - index_on_plot_ = plot->plottableCount() - 1; - } - private: size_t index_on_plot_{ULLONG_MAX}; }; diff --git a/gui/hill.cpp b/gui/hill.cpp index 0646104..f110230 100644 --- a/gui/hill.cpp +++ b/gui/hill.cpp @@ -24,7 +24,4 @@ void gui::Hill::Draw(QCustomPlot* plot) { // замыкаем, соединяя с первой точкой curve_->addData(points[0].x, points[0].y); - - // индекс последнего созданного = кол-во всех - 1 - SetIndexOnPlot(plot); } diff --git a/gui/target.cpp b/gui/target.cpp index d207660..174a7c1 100644 --- a/gui/target.cpp +++ b/gui/target.cpp @@ -10,7 +10,4 @@ void gui::Target::Draw(QCustomPlot* plot) { graph_->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 4)); graph_->setData({GetPoint().x}, {GetPoint().y}); - - // индекс последнего созданного = кол-во всех - 1 - SetIndexOnPlot(plot); } diff --git a/gui/trappy_circle.cpp b/gui/trappy_circle.cpp index 719e76a..5ebdd7d 100644 --- a/gui/trappy_circle.cpp +++ b/gui/trappy_circle.cpp @@ -14,7 +14,4 @@ void gui::TrappyCircle::Draw(QCustomPlot* plot) { GetCenter().y + GetRadius()); ellipse_->bottomRight->setCoords(GetCenter().x + GetRadius(), GetCenter().y - GetRadius()); - - // индекс последнего созданного = кол-во всех - 1 - SetIndexOnPlot(plot); } diff --git a/gui/trappy_circle.h b/gui/trappy_circle.h index 6cc54a1..7c39578 100644 --- a/gui/trappy_circle.h +++ b/gui/trappy_circle.h @@ -39,15 +39,6 @@ class TrappyCircle : public Drawable { void Draw(QCustomPlot* plot) override; - /** - * @brief Возвращает индекс на полотне [item] - * @details Этот метод перегружен у TrappyCircle, так как его фигуры на - * полотне являются QCPItemEllipse, которые в свою очередь относятся к Items, - * а не Plottables по отношению к QCustomPlot plot - * @return size_t: индекс - */ - size_t GetIndexOnPlot() const override { return item_index_; } - /** * @brief Возвращает значение указателя на полотне * @return QCPItemEllipse*: указатель @@ -59,10 +50,6 @@ class TrappyCircle : public Drawable { } private: - void SetIndexOnPlot(QCustomPlot* plot) override { - item_index_ = plot->itemCount() - 1; - } - lib::TrappyCircle data_; QColor color_; size_t item_index_{ULLONG_MAX}; diff --git a/gui/trappy_line.cpp b/gui/trappy_line.cpp index 18b4190..001940a 100644 --- a/gui/trappy_line.cpp +++ b/gui/trappy_line.cpp @@ -20,9 +20,6 @@ void TrappyLine::Draw(QCustomPlot* plot) { graph_->addData(targets.first.GetPoint().x, targets.first.GetPoint().y); graph_->addData(targets.second.GetPoint().x, targets.second.GetPoint().y); - - // индекс последнего созданного = кол-во всех - 1 - SetIndexOnPlot(plot); } void TrappyLine::UpdateData(gui::Target* first_target, diff --git a/lib/hill.cpp b/lib/hill.cpp index e661dfa..0d9bd4f 100644 --- a/lib/hill.cpp +++ b/lib/hill.cpp @@ -47,7 +47,10 @@ void Hill::SetJsonInfo(const QJsonObject& hill_obj) { vertices_.push_back(vertice); } - SetId(static_cast(hill_obj.value("Id").toInt())); + unsigned short id = static_cast(hill_obj.value("Id").toInt()); + if (id < 40000 || id > 49999) + throw std::invalid_argument("Invalid file format: incorrect id in 'Hill'!"); + SetId(id); CheckErrorValues(); } diff --git a/lib/target.cpp b/lib/target.cpp index 19b2f32..dd4601f 100644 --- a/lib/target.cpp +++ b/lib/target.cpp @@ -16,14 +16,18 @@ void Target::SetJsonInfo(const QJsonObject& target_obj) { if (!(target_obj.contains("X") && target_obj.contains("Y") && target_obj.contains("Id"))) throw std::invalid_argument( - "Invalid file format: missing X,Y or Id field in Targets!"); + "Invalid file format: missing X, Y or Id field in Targets!"); double x = target_obj.value("X").toDouble(); double y = target_obj.value("Y").toDouble(); - SetPoint(x, y); - SetId(static_cast(target_obj.value("Id").toInt())); + unsigned short id = + static_cast(target_obj.value("Id").toInt()); + if (id < 10000 || id > 19999) + throw std::invalid_argument( + "Invalid file format: incorrect id in 'Target'!"); + SetId(id); CheckErrorValues(); } diff --git a/lib/trappy_circle.cpp b/lib/trappy_circle.cpp index 87e2979..b51d888 100644 --- a/lib/trappy_circle.cpp +++ b/lib/trappy_circle.cpp @@ -39,7 +39,12 @@ void TrappyCircle::SetJsonInfo(const QJsonObject& trappy_circle_obj) { SetCenter({x, y}); SetRadius(r); - SetId(static_cast(trappy_circle_obj.value("Id").toInt())); + unsigned short id = + static_cast(trappy_circle_obj.value("Id").toInt()); + if (id < 20000 || id > 29999) + throw std::invalid_argument( + "Invalid file format: incorrect id in 'Trappy_Circle'!"); + SetId(id); CheckErrorValues(); } diff --git a/main/gui_json_file/gui_json_file.h b/main/gui_json_file/gui_json_file.h index 2f57194..fb8de03 100644 --- a/main/gui_json_file/gui_json_file.h +++ b/main/gui_json_file/gui_json_file.h @@ -11,20 +11,20 @@ class GuiJsonFile { void Save(data_tools::DataManager* plot_area); void Open(data_tools::DataManager* plot_area); + void Close() { file_->close(); } QString GetFileName() const { return QString::fromStdString( file_->filesystemFileName().filename().string()); } - QString GetParentPath() const { - return QString::fromStdString( - file_->filesystemFileName().parent_path().string()); + QString GetAbsolutePath() const { + return QString::fromStdString(file_->filesystemFileName().string()); } - QString GetRelativePath() const { + QString GetParentPath() const { return QString::fromStdString( - file_->filesystemFileName().relative_path().string()); + file_->filesystemFileName().parent_path().string()); } void SetFile(const QString& file_name) { file_->setFileName(file_name); } diff --git a/main/gui_json_file/open.cpp b/main/gui_json_file/open.cpp index b867bd2..2a59b4e 100644 --- a/main/gui_json_file/open.cpp +++ b/main/gui_json_file/open.cpp @@ -30,6 +30,13 @@ std::vector GetHillsFromFile(QJsonArray arr) { return hills; } +bool IsExistId(std::vector ids, unsigned short curr_id) { + for (const auto& id : ids) { + if (curr_id == id) return true; + } + return false; +} + void GuiJsonFile::Open(data_tools::DataManager* manager) { if (file_->open(QIODevice::ReadOnly | QFile::Text)) { QJsonObject root = LoadJson(); @@ -46,38 +53,83 @@ void GuiJsonFile::Open(data_tools::DataManager* manager) { std::vector targets; + std::vector targets_ids; for (size_t i = 0; i < static_cast(json_targets.size()); i++) { lib::Target t; t.SetJsonInfo(json_targets.at(i).toObject()); targets.push_back(t); + + if (IsExistId(targets_ids, t.GetId())) + throw std::invalid_argument( + "Invalid file format: there are identical id's in 'Targets'!"); + + targets_ids.push_back(t.GetId()); } manager->Set(GetTargetsFromFile(json_targets)); + std::vector trappy_circles_ids; for (size_t i = 0; i < static_cast(json_trappy_circles.size()); i++) { lib::TrappyCircle trc; trc.SetJsonInfo(json_trappy_circles.at(i).toObject()); + + if (IsExistId(trappy_circles_ids, trc.GetId())) + throw std::invalid_argument( + "Invalid file format: there are identical id's in " + "'Trappy_Circles'!"); + + trappy_circles_ids.push_back(trc.GetId()); } manager->Set(GetTrappyCirclesFromFile(json_trappy_circles)); + std::vector trappy_lines_ids; for (size_t i = 0; i < static_cast(json_trappy_lines.size()); i++) { QJsonObject json_tr_line = json_trappy_lines.at(i).toObject(); + unsigned short id1 = static_cast(json_tr_line.value("Id_P1").toInt()); unsigned short id2 = static_cast(json_tr_line.value("Id_P2").toInt()); + std::pair targets_ptrs; for (const auto& target : manager->GetTargetsPtrs()) { if (target->GetData().GetId() == id1) targets_ptrs.first = target; if (target->GetData().GetId() == id2) targets_ptrs.second = target; } - manager->Add( - new gui::TrappyLine(targets_ptrs.first, targets_ptrs.second)); + + if (targets_ptrs.first == nullptr || targets_ptrs.second == nullptr) + throw std::invalid_argument( + "Invalid file format: non-existent id for 'Target' in " + "'Trappy_Line'!"); + + gui::TrappyLine* trl{ + new gui::TrappyLine(targets_ptrs.first, targets_ptrs.second)}; + + unsigned short id = + static_cast(json_tr_line.value("Id").toInt()); + if (IsExistId(trappy_lines_ids, id)) + throw std::invalid_argument( + "Invalid file format: there are identical id's in " + "'Trappy_Lines'!"); + if (id < 30000 || id > 39999) + throw std::invalid_argument( + "Invalid file format: incorrect id in 'Trappy_Line'!"); + + trappy_lines_ids.push_back(id); + trl->GetData().SetId(id); + manager->Add(trl); } + std::vector hills_ids; for (size_t i = 0; i < static_cast(json_hills.size()); i++) { lib::Hill h; h.SetJsonInfo(json_hills.at(i).toObject()); + + if (IsExistId(hills_ids, h.GetId())) + throw std::invalid_argument( + "Invalid file format: there are identical id's in 'Hills'!"); + + hills_ids.push_back(h.GetId()); } manager->Set(GetHillsFromFile(json_hills)); diff --git a/main/mainwindow.cpp b/main/mainwindow.cpp index 6a7f1b5..25b6cd4 100644 --- a/main/mainwindow.cpp +++ b/main/mainwindow.cpp @@ -12,9 +12,6 @@ MainWindow::MainWindow(QWidget* parent) ui->plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables | QCP::iSelectItems); - connect(ui->plot, &QCustomPlot::mouseDoubleClick, this, - &MainWindow::mousePressObjectsButton); - connect(ui->plot, &QCustomPlot::mousePress, this, &MainWindow::mousePressContextMenu); diff --git a/main/mainwindow_connections/interaction_buttons.cpp b/main/mainwindow_connections/interaction_buttons.cpp index 6f131e2..8a9e845 100644 --- a/main/mainwindow_connections/interaction_buttons.cpp +++ b/main/mainwindow_connections/interaction_buttons.cpp @@ -32,8 +32,8 @@ void MainWindow::DisconnectObject(gui::ObjectType obj_type) { break; } - connect(ui->plot, &QCustomPlot::mouseDoubleClick, this, - &MainWindow::mousePressObjectsButton); + disconnect(ui->plot, &QCustomPlot::mouseDoubleClick, this, + &MainWindow::mousePressObjectsButton); connect(ui->plot, &QCustomPlot::mousePress, this, &MainWindow::mousePressContextMenu); @@ -70,7 +70,6 @@ void MainWindow::DeleteLastAddedObject() { } area_->Redraw(); - t_connection_->UpdateTables(); what_obj_addition_ = WhatObjectAddition::Nothing; } @@ -96,6 +95,9 @@ void MainWindow::mousePressObjectsButton(QMouseEvent* mouse_event) { ui->plot->setCursor(Qt::CrossCursor); cursor_ = CursorType::DefaultCursor; + + // после финального добавления обновляем таблицу + t_connection_->UpdateTable(gui::ObjectType::Targets); break; } @@ -161,7 +163,6 @@ void MainWindow::mousePressObjectsButton(QMouseEvent* mouse_event) { } area_->Redraw(); - t_connection_->UpdateTables(); } catch (const std::exception& e) { QMessageBox::critical(this, "Error!", e.what()); @@ -172,8 +173,11 @@ void MainWindow::mousePressSetRadiusFromPlot(QMouseEvent* mouse_event) { if (mouse_event->button() == Qt::LeftButton) { DisconnectObject(gui::ObjectType::TrappyCircles); what_obj_addition_ = WhatObjectAddition::Nothing; + area_->Redraw(); - t_connection_->UpdateTables(); + + // после финального добавления обновляем таблицу + t_connection_->UpdateTable(gui::ObjectType::TrappyCircles); } } @@ -214,7 +218,9 @@ void MainWindow::mousePressSelectSecondTarget(QMouseEvent* mouse_event) { what_obj_addition_ = WhatObjectAddition::Nothing; area_->Redraw(); - t_connection_->UpdateTables(); + + // после финального добавления обновляем таблицу + t_connection_->UpdateTable(gui::ObjectType::TrappyLines); return; } @@ -251,6 +257,9 @@ void MainWindow::mousePressAddVertice(QMouseEvent* mouse_event) { DisconnectObject(gui::ObjectType::Hills); what_obj_addition_ = WhatObjectAddition::Nothing; + // после финального добавления обновляем таблицу + t_connection_->UpdateTable(gui::ObjectType::Hills); + } else if (manager_->GetHills()[last].GetPoints()[0] == manager_->GetHills()[last].GetPoints()[1]) { size_t last_p = manager_->GetHills()[last].GetPoints().size() - 1; @@ -261,7 +270,6 @@ void MainWindow::mousePressAddVertice(QMouseEvent* mouse_event) { manager_->GetHillsPtrs()[last]->AddVertice({x, y}); area_->Redraw(); - t_connection_->UpdateTables(); } } @@ -283,7 +291,6 @@ void MainWindow::mousePressDeleteLastVertice(QMouseEvent* mouse_event) { } area_->Redraw(); - t_connection_->UpdateTables(); } } diff --git a/main/mainwindow_connections/menu_add_actions.cpp b/main/mainwindow_connections/menu_add_actions.cpp index afbd94b..1d83689 100644 --- a/main/mainwindow_connections/menu_add_actions.cpp +++ b/main/mainwindow_connections/menu_add_actions.cpp @@ -42,7 +42,7 @@ void MainWindow::AddTarget(std::string x, std::string y) { manager_->Add(new gui::Target(std::stod(x), std::stod(y))); area_->Redraw(); - t_connection_->UpdateTables(); + t_connection_->UpdateTable(gui::ObjectType::Targets); } catch (const std::exception& e) { QMessageBox::critical(this, "Error!", e.what()); @@ -57,7 +57,7 @@ void MainWindow::AddTrappyCircle(std::string x, std::string y, manager_->Add( new gui::TrappyCircle(std::stod(x), std::stod(y), std::stod(radius))); area_->Redraw(); - t_connection_->UpdateTables(); + t_connection_->UpdateTable(gui::ObjectType::TrappyCircles); } catch (const std::exception& e) { QMessageBox::critical(this, "Error!", e.what()); @@ -77,7 +77,7 @@ void MainWindow::AddTrappyLine(std::string x1, std::string y1, std::string x2, manager_->GetTargetsPtrs()[manager_->GetTargets().size() - 1])); area_->Redraw(); - t_connection_->UpdateTables(); + t_connection_->UpdateTable(gui::ObjectType::TrappyLines); } catch (const std::exception& e) { QMessageBox::critical(this, "Error!", e.what()); @@ -96,7 +96,7 @@ void MainWindow::AddHill( manager_->Add(new gui::Hill(lib_points)); area_->Redraw(); - t_connection_->UpdateTables(); + t_connection_->UpdateTable(gui::ObjectType::Hills); } catch (const std::exception& e) { QMessageBox::critical(this, "Error!", e.what()); diff --git a/main/mainwindow_connections/menu_file_actions.cpp b/main/mainwindow_connections/menu_file_actions.cpp index a68b1ab..65faf8b 100644 --- a/main/mainwindow_connections/menu_file_actions.cpp +++ b/main/mainwindow_connections/menu_file_actions.cpp @@ -96,20 +96,23 @@ void MainWindow::on_actionOpen_triggered() { if (file_name.isEmpty()) return; - QString old_filename = json_file_.GetFileName(); + QString old_filename = json_file_.GetAbsolutePath(); try { json_file_.SetFile(file_name); manager_->Clear(); json_file_.Open(manager_.get()); - area_->Redraw(); - t_connection_->UpdateTables(); } catch (const std::exception& e) { QMessageBox::critical(this, "Error!", e.what()); - + json_file_.Close(); + manager_->Clear(); json_file_.SetFile(old_filename); + json_file_.Open(manager_.get()); } + + area_->Redraw(); + t_connection_->UpdateTables(); } // Кнопка "Save" @@ -131,7 +134,7 @@ bool MainWindow::on_actionSave_as_triggered() { DeleteLastAddedObject(); QString file_name = QFileDialog::getSaveFileName( - this, tr("Save as"), json_file_.GetRelativePath(), tr("File (*.json)")); + this, tr("Save as"), json_file_.GetParentPath(), tr("File (*.json)")); if (!file_name.isEmpty()) { json_file_.SetFile(file_name); @@ -139,4 +142,4 @@ bool MainWindow::on_actionSave_as_triggered() { return false; } return true; -} \ No newline at end of file +} diff --git a/main/mainwindow_connections/other_connections.cpp b/main/mainwindow_connections/other_connections.cpp index 841c87c..e39a80d 100644 --- a/main/mainwindow_connections/other_connections.cpp +++ b/main/mainwindow_connections/other_connections.cpp @@ -15,6 +15,8 @@ gui::ObjectType MainWindow::GetObjType() const { void MainWindow::on_pushButtonAddTarget_clicked() { DeleteLastAddedObject(); + connect(ui->plot, &QCustomPlot::mouseDoubleClick, this, + &MainWindow::mousePressObjectsButton); ui->plot->setCursor(QCursor(QPixmap("../images/better_cross_cursor.png") .scaled(QSize(24, 24), Qt::KeepAspectRatio))); @@ -23,6 +25,8 @@ void MainWindow::on_pushButtonAddTarget_clicked() { void MainWindow::on_pushButtonAddTrappyCircle_clicked() { DeleteLastAddedObject(); + connect(ui->plot, &QCustomPlot::mouseDoubleClick, this, + &MainWindow::mousePressObjectsButton); ui->plot->setCursor(QCursor(QPixmap("../images/move_cursor.png") .scaled(QSize(24, 24), Qt::KeepAspectRatio))); @@ -31,6 +35,8 @@ void MainWindow::on_pushButtonAddTrappyCircle_clicked() { void MainWindow::on_pushButtonAddTrappyLine_clicked() { DeleteLastAddedObject(); + connect(ui->plot, &QCustomPlot::mouseDoubleClick, this, + &MainWindow::mousePressObjectsButton); ui->plot->setCursor(QCursor(QPixmap("../images/choose_cursor.png") .scaled(QSize(24, 24), Qt::KeepAspectRatio))); @@ -39,6 +45,8 @@ void MainWindow::on_pushButtonAddTrappyLine_clicked() { void MainWindow::on_pushButtonAddHill_clicked() { DeleteLastAddedObject(); + connect(ui->plot, &QCustomPlot::mouseDoubleClick, this, + &MainWindow::mousePressObjectsButton); ui->plot->setCursor(QCursor(QPixmap("../images/move_cursor.png") .scaled(QSize(24, 24), Qt::KeepAspectRatio))); @@ -98,44 +106,50 @@ void MainWindow::mousePressRemoveObject() { if (ui->plot->selectedGraphs()[0] == manager_->GetTargetsPtrs()[i]->GetGraphPtr()) { manager_->Remove(gui::ObjectType::Targets, i); + t_connection_->UpdateTable(gui::ObjectType::Targets); is_found = true; break; } } + if (!is_found) for (size_t i = 0; i < manager_->GetTrappyLines().size(); i++) { if (ui->plot->selectedGraphs()[0] == manager_->GetTrappyLinesPtrs()[i]->GetGraphPtr()) { manager_->Remove(gui::ObjectType::TrappyLines, i); + t_connection_->UpdateTable(gui::ObjectType::TrappyLines); break; } } + } else if (ui->plot->selectedPlottables().size() > 0) { for (size_t i = 0; i < manager_->GetHills().size(); i++) { if (ui->plot->selectedPlottables()[0] == manager_->GetHillsPtrs()[i]->GetCurvePtr()) { manager_->Remove(gui::ObjectType::Hills, i); + t_connection_->UpdateTable(gui::ObjectType::Hills); break; } } + } else if (ui->plot->selectedItems().size() > 0) for (size_t i = 0; i < manager_->GetTrappyCircles().size(); i++) { if (ui->plot->selectedItems()[0] == manager_->GetTrappyCirclesPtrs()[i]->GetItemEllipsePtr()) { manager_->Remove(gui::ObjectType::TrappyCircles, i); + t_connection_->UpdateTable(gui::ObjectType::TrappyCircles); break; } } area_->Redraw(); - t_connection_->UpdateTables(); } void MainWindow::mousePressContextMenu(QMouseEvent* mouse_event) { if (mouse_event->button() == Qt::RightButton && - (ui->plot->selectedGraphs().size() > 0 || - ui->plot->selectedItems().size() > 0 || - ui->plot->selectedPlottables().size() > 0)) { + (!ui->plot->selectedGraphs().empty() || + !ui->plot->selectedItems().empty() || + !ui->plot->selectedPlottables().empty())) { ui->plot->setContextMenuPolicy(Qt::ActionsContextMenu); QMenu* menu{new QMenu(this)};