From c828fb2343349f33e18f78d273a32d3907b85731 Mon Sep 17 00:00:00 2001 From: Saki Date: Mon, 1 Jun 2015 10:32:14 +0800 Subject: [PATCH] New session action --- Robot.pro | 13 +-- barrackseditor.cpp | 24 ------ barrackseditor.h | 36 --------- barrackseditorbase.cpp | 24 ------ barrackseditorbase.h | 36 --------- generateground.cpp | 109 ++++++++++++++++++++++++-- generateground.h | 35 ++++++++- generategroundbase.cpp | 24 ------ generategroundbase.h | 24 +++++- ground.cpp | 10 ++- groundpreviewer.h | 6 +- groundeditor.cpp => polygoneditor.cpp | 31 ++++++-- groundeditor.h => polygoneditor.h | 22 +++--- 13 files changed, 211 insertions(+), 183 deletions(-) delete mode 100644 barrackseditor.cpp delete mode 100644 barrackseditor.h delete mode 100644 barrackseditorbase.cpp delete mode 100644 barrackseditorbase.h delete mode 100644 generategroundbase.cpp rename groundeditor.cpp => polygoneditor.cpp (84%) rename groundeditor.h => polygoneditor.h (78%) diff --git a/Robot.pro b/Robot.pro index 2df9487..861c221 100644 --- a/Robot.pro +++ b/Robot.pro @@ -15,14 +15,11 @@ SOURCES += \ paneldock.cpp \ robotaddwidget.cpp \ generateground.cpp \ - groundeditor.cpp \ - generategroundbase.cpp \ - barrackseditor.cpp \ - barrackseditorbase.cpp \ pointeditor.cpp \ groundpreviewer.cpp \ groundrealtimepreviewer.cpp \ - groundglobal.cpp + groundglobal.cpp \ + polygoneditor.cpp HEADERS += \ mainwindow.h \ @@ -35,11 +32,9 @@ HEADERS += \ paneldock.h \ robotaddwidget.h \ generateground.h \ - groundeditor.h \ generategroundbase.h \ - barrackseditor.h \ - barrackseditorbase.h \ pointeditor.h \ groundpreviewer.h \ groundrealtimepreviewer.h \ - groundglobal.h + groundglobal.h \ + polygoneditor.h diff --git a/barrackseditor.cpp b/barrackseditor.cpp deleted file mode 100644 index 5d8a4d2..0000000 --- a/barrackseditor.cpp +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (C) Kreogist Dev Team - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -#include "barrackseditor.h" - -BarracksEditor::BarracksEditor(QWidget *parent) : - BarracksEditorBase(parent) -{ -} diff --git a/barrackseditor.h b/barrackseditor.h deleted file mode 100644 index 9a6f9c6..0000000 --- a/barrackseditor.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (C) Kreogist Dev Team - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -#ifndef BARRACKSEDITOR_H -#define BARRACKSEDITOR_H - -#include "barrackseditorbase.h" - -class BarracksEditor : public BarracksEditorBase -{ - Q_OBJECT -public: - explicit BarracksEditor(QWidget *parent = 0); - -signals: - -public slots: - -}; - -#endif // BARRACKSEDITOR_H diff --git a/barrackseditorbase.cpp b/barrackseditorbase.cpp deleted file mode 100644 index 09504e6..0000000 --- a/barrackseditorbase.cpp +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (C) Kreogist Dev Team - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -#include "barrackseditorbase.h" - -BarracksEditorBase::BarracksEditorBase(QWidget *parent) : - QWidget(parent) -{ -} diff --git a/barrackseditorbase.h b/barrackseditorbase.h deleted file mode 100644 index ee22c06..0000000 --- a/barrackseditorbase.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (C) Kreogist Dev Team - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -#ifndef BARRACKSEDITORBASE_H -#define BARRACKSEDITORBASE_H - -#include - -class BarracksEditorBase : public QWidget -{ - Q_OBJECT -public: - explicit BarracksEditorBase(QWidget *parent = 0); - -signals: - -public slots: - -}; - -#endif // BARRACKSEDITORBASE_H diff --git a/generateground.cpp b/generateground.cpp index b0171b8..0a6c6ba 100644 --- a/generateground.cpp +++ b/generateground.cpp @@ -18,17 +18,20 @@ #include #include #include +#include -#include "groundeditor.h" -#include "barrackseditor.h" +#include "groundbase.h" +#include "polygoneditor.h" +#include "groundpreviewer.h" #include "generateground.h" GenerateGround::GenerateGround(QWidget *parent) : GenerateGroundBase(parent), m_tabManager(new QTabWidget(this)), - m_groundEditor(new GroundEditor(this)), - m_barracksEditor(new BarracksEditor(this)), + m_borderEditor(new PolygonEditor(this)), + m_barracksEditor(new PolygonEditor(this)), + m_previewer(new GroundPreviewer(this)), m_okay(new QPushButton(this)), m_cancel(new QPushButton(this)) { @@ -37,13 +40,71 @@ GenerateGround::GenerateGround(QWidget *parent) : setWindowFlags(Qt::Sheet); #endif + //Link the previewer. + connect(m_borderEditor, &PolygonEditor::polygonChange, + m_previewer, &GroundPreviewer::setPreviewBorder); + connect(m_barracksEditor, &PolygonEditor::polygonChange, + m_previewer, &GroundPreviewer::setPreviewBarracks); + //Configure buttons. m_cancel->setShortcut(QKeySequence(Qt::Key_Escape)); + connect(m_okay, + static_cast(&QPushButton::clicked), + [=] + { + //Check is the ground and barracks polygon available. + QPolygonF ground=m_borderEditor->polygon(); + if(ground.size()<3) //Simplified than a triangle. + { + //Set the tab to ground editor. + m_tabManager->setCurrentWidget(m_borderEditor); + //Raise the information. + QMessageBox::information(this, + tr("Ground border invalid"), + tr("The ground border must be at least a triangle.")); + return; + } + QPolygonF barracks=m_barracksEditor->polygon(); + if(barracks.size()<3) //Simplified than a triangle. + { + //Set the tab to barracks editor. + m_tabManager->setCurrentWidget(m_barracksEditor); + //Raise the information. + QMessageBox::information(this, + tr("Barracks border invalid"), + tr("The barracks border must be at least a triangle.")); + return; + } + //Check the barracks is valid or not. + for(QPointF point : barracks) + { + //If there's one point which is not in the ground, + //Ask user to change the barracks. + if(!ground.containsPoint(point, Qt::OddEvenFill)) + { + //Set the tab to barracks editor. + m_tabManager->setCurrentWidget(m_barracksEditor); + //Raise the information. + QMessageBox::information(this, + tr("Barracks border invalid"), + tr("The barracks border must be inside the border.\n" + "You may:\n" + " 1. Enlarge the border." + " 2. Reduce the barracks.")); + return; + } + } + //Now all the polygon should be okay, save the data. + m_border=ground; + m_barracks=barracks; + //Finished. + done(QDialog::Accepted); + }); connect(m_cancel, static_cast(&QPushButton::clicked), [=] { - close(); + done(QDialog::Rejected); }); //Initial the layout. @@ -52,8 +113,9 @@ GenerateGround::GenerateGround(QWidget *parent) : setLayout(mainLayout); mainLayout->addWidget(m_tabManager, 1); + mainLayout->addWidget(m_previewer, 0, Qt::AlignCenter); - m_tabManager->addTab(m_groundEditor, ""); + m_tabManager->addTab(m_borderEditor, ""); m_tabManager->addTab(m_barracksEditor, ""); QBoxLayout *buttonLayout=new QBoxLayout(QBoxLayout::TopToBottom, @@ -74,3 +136,38 @@ void GenerateGround::retranslate() m_tabManager->setTabText(0, tr("Ground")); m_tabManager->setTabText(1, tr("Barracks")); } + +QPolygonF GenerateGround::barracks() const +{ + return m_barracks; +} + +void GenerateGround::setBarracks(const QPolygonF &barracks) +{ + //Save the barracks first. + m_barracks = barracks; + //Set the polygon to the editor. + m_barracksEditor->setPolygon(m_barracks); +} + +void GenerateGround::showEvent(QShowEvent *event) +{ + //Show the widget. + GenerateGroundBase::showEvent(event); + //Set default widget to border editor. + m_tabManager->setCurrentWidget(m_borderEditor); +} + +QPolygonF GenerateGround::border() const +{ + return m_border; +} + +void GenerateGround::setBorder(const QPolygonF &border) +{ + //Save the border first. + m_border = border; + //Set the polygon to the editor. + m_borderEditor->setPolygon(m_border); +} + diff --git a/generateground.h b/generateground.h index 12013b0..27fc388 100644 --- a/generateground.h +++ b/generateground.h @@ -23,8 +23,8 @@ class QPushButton; class QTabWidget; -class GroundEditor; -class BarracksEditor; +class PolygonEditor; +class GroundPreviewer; /*! * \brief The GenerateGround class is a default realization of * GenerateGroundBase. @@ -39,18 +39,45 @@ class GenerateGround : public GenerateGroundBase */ explicit GenerateGround(QWidget *parent = 0); + /*! + * \brief Reimplemented from GenerateGroundBase::border(). + */ + QPolygonF border() const; + + /*! + * \brief Reimplemented from GenerateGroundBase::barracks(). + */ + QPolygonF barracks() const; + signals: public slots: + /*! + * \brief Reimplemented from GenerateGroundBase::setBorder(). + */ + void setBorder(const QPolygonF &border); + + /*! + * \brief Reimplemented from GenerateGroundBase::setBarracks(). + */ + void setBarracks(const QPolygonF &barracks); + +protected: + /*! + * \brief Reimplemented from GenerateGroundBase::showEvent(). + */ + void showEvent(QShowEvent *event); private slots: void retranslate(); private: QTabWidget *m_tabManager; - GroundEditor *m_groundEditor; - BarracksEditor *m_barracksEditor; + PolygonEditor *m_borderEditor, *m_barracksEditor; + GroundPreviewer *m_previewer; QPushButton *m_okay, *m_cancel; + + QPolygonF m_border, m_barracks; }; #endif // GENERATEGROUND_H diff --git a/generategroundbase.cpp b/generategroundbase.cpp deleted file mode 100644 index a4c401b..0000000 --- a/generategroundbase.cpp +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (C) Kreogist Dev Team - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -#include "generategroundbase.h" - -GenerateGroundBase::GenerateGroundBase(QWidget *parent) : - QDialog(parent) -{ -} diff --git a/generategroundbase.h b/generategroundbase.h index 1949875..e492184 100644 --- a/generategroundbase.h +++ b/generategroundbase.h @@ -33,12 +33,34 @@ class GenerateGroundBase : public QDialog * \brief Construct a GenerateGroundBase dialog. * \param parent Set the parent widget. */ - explicit GenerateGroundBase(QWidget *parent = 0); + GenerateGroundBase(QWidget *parent = 0):QDialog(parent){} + + /*! + * \brief The current border information. + * \return The border polygon of the editor result. + */ + virtual QPolygonF border() const=0; + + /*! + * \brief The current barracks information. + * \return The barracks polygon of the editor result. + */ + virtual QPolygonF barracks() const=0; signals: public slots: + /*! + * \brief Set the border polygon, make the editor to display this polygon. + * \param border The border polygon. + */ + virtual void setBorder(const QPolygonF &border)=0; + /*! + * \brief Set the barracks polygon, make the editor to display this polygon. + * \param border The barracks polygon. + */ + virtual void setBarracks(const QPolygonF &barracks)=0; }; #endif // GENERATEGROUNDBASE_H diff --git a/ground.cpp b/ground.cpp index 533c667..f78e1d2 100644 --- a/ground.cpp +++ b/ground.cpp @@ -277,7 +277,15 @@ void Ground::onActionNew() //failed. If success, set the ground information, or else abandon. if(m_generator) { - m_generator->exec(); + //Clear the previous data. + m_generator->setBorder(QPolygonF()); + m_generator->setBarracks(QPolygonF()); + //Get the new data. + if(QDialog::Accepted==m_generator->exec()) + { + setBorder(m_generator->border()); + setBarracks(m_generator->barracks()); + } } } diff --git a/groundpreviewer.h b/groundpreviewer.h index 97ca416..495d365 100644 --- a/groundpreviewer.h +++ b/groundpreviewer.h @@ -45,6 +45,9 @@ class GroundPreviewer : public QWidget */ bool showPreviewPoint() const; +signals: + +public slots: /*! * \brief Set the preview border polygon. * \param groundBorder The border polygon. @@ -57,9 +60,6 @@ class GroundPreviewer : public QWidget */ void setPreviewBarracks(const QPolygonF &groundBarracks); -signals: - -public slots: /*! * \brief Make the preview widget to display the preview robot or not. * \param showPreviewPoint The preview enabled value. diff --git a/groundeditor.cpp b/polygoneditor.cpp similarity index 84% rename from groundeditor.cpp rename to polygoneditor.cpp index e6f65d9..3758d75 100644 --- a/groundeditor.cpp +++ b/polygoneditor.cpp @@ -22,11 +22,11 @@ #include "pointeditor.h" -#include "groundeditor.h" +#include "polygoneditor.h" #include -GroundEditor::GroundEditor(QWidget *parent) : +PolygonEditor::PolygonEditor(QWidget *parent) : QWidget(parent), m_groundPoints(new QTreeView(this)), m_groundData(new QStandardItemModel(this)) @@ -37,7 +37,11 @@ GroundEditor::GroundEditor(QWidget *parent) : m_groundPoints->setDragDropMode(QAbstractItemView::NoDragDrop); //Link the model. connect(m_groundData, &QStandardItemModel::itemChanged, - [=]{emit groundChange(ground());}); + [=]{emit polygonChange(polygon());}); + connect(m_groundData, &QStandardItemModel::rowsInserted, + [=]{emit polygonChange(polygon());}); + connect(m_groundData, &QStandardItemModel::rowsRemoved, + [=]{emit polygonChange(polygon());}); //Initial the layout. QBoxLayout *mainLayout=new QBoxLayout(QBoxLayout::TopToBottom, @@ -87,7 +91,7 @@ GroundEditor::GroundEditor(QWidget *parent) : retranslate(); } -QPolygonF GroundEditor::ground() +QPolygonF PolygonEditor::polygon() { //Check the point count in the editor widget. //If it cannot build a triangle, then return a empty QPolygonF. @@ -105,7 +109,22 @@ QPolygonF GroundEditor::ground() return border; } -void GroundEditor::retranslate() +void PolygonEditor::setPolygon(const QPolygonF &polygon) +{ + //Clear all the data in the ground data; + m_groundData->clear(); + //Set the header data. + retranslate(); + //Add all the information to ground data. + for(QPointF point:polygon) + { + addPoint(point); + } + //Emit the changed signal. + emit polygonChange(this->polygon()); +} + +void PolygonEditor::retranslate() { //Update ground points header. QStringList groundHeader; @@ -113,7 +132,7 @@ void GroundEditor::retranslate() m_groundData->setHorizontalHeaderLabels(groundHeader); } -void GroundEditor::addPoint(const QPointF &point) +void PolygonEditor::addPoint(const QPointF &point) { //Find the if point has been in the model, ignore the same point. for(int i=0; irowCount(); i++) diff --git a/groundeditor.h b/polygoneditor.h similarity index 78% rename from groundeditor.h rename to polygoneditor.h index 05e08ac..1536080 100644 --- a/groundeditor.h +++ b/polygoneditor.h @@ -16,8 +16,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#ifndef GROUNDEDITOR_H -#define GROUNDEDITOR_H +#ifndef POLYGONEDITOR_H +#define POLYGONEDITOR_H #include @@ -25,10 +25,9 @@ class QToolButton; class QTreeView; class QStandardItemModel; /*! - * \brief The GroundEditor class can edit a ground-guard-bordor, and set the new - * border to that ground. + * \brief The PolygonEditor class can edit or generate a QPolygon. */ -class GroundEditor : public QWidget +class PolygonEditor : public QWidget { Q_OBJECT public: @@ -36,13 +35,13 @@ class GroundEditor : public QWidget * \brief Construct Ground Editor. * \param parent The parent widget. */ - explicit GroundEditor(QWidget *parent = 0); + explicit PolygonEditor(QWidget *parent = 0); /*! * \brief Get the current ground. * \return The ground polygon. */ - QPolygonF ground(); + QPolygonF polygon(); signals: /*! @@ -50,9 +49,14 @@ class GroundEditor : public QWidget * emitted. * \param ground The new ground polygon. */ - void groundChange(QPolygonF ground); + void polygonChange(QPolygonF polygon); public slots: + /*! + * \brief Set the polygon to be edit. + * \param polygon The polygon. + */ + void setPolygon(const QPolygonF &polygon); private slots: void retranslate(); @@ -71,4 +75,4 @@ private slots: QStandardItemModel *m_groundData; }; -#endif // GROUNDEDITOR_H +#endif // POLYGONEDITOR_H