diff --git a/Robot.pro b/Robot.pro index d2822a9..07a17f2 100644 --- a/Robot.pro +++ b/Robot.pro @@ -34,7 +34,9 @@ SOURCES += \ groundglobal.cpp \ polygoneditor.cpp \ robotmanagewidget.cpp \ - about.cpp + about.cpp \ + gridwidget.cpp \ + robotbase.cpp HEADERS += \ mainwindow.h \ @@ -54,7 +56,9 @@ HEADERS += \ groundglobal.h \ polygoneditor.h \ robotmanagewidget.h \ - about.h + about.h \ + gridwidget.h \ + robotbase.h RESOURCES += \ res.qrc diff --git a/about.cpp b/about.cpp index 103c3b5..3eeafe4 100644 --- a/about.cpp +++ b/about.cpp @@ -31,6 +31,7 @@ void About::showAbout(QWidget *parent) About::About(QWidget *parent) : QDialog(parent) { + setWindowTitle(tr("About Robot Emulator")); //Set the main layout. QBoxLayout *mainLayout=new QBoxLayout(QBoxLayout::TopToBottom, this); @@ -40,7 +41,7 @@ About::About(QWidget *parent) : icon->setPixmap(QPixmap("://res/icon.png")); icon->setFixedSize(256, 256); icon->setScaledContents(true); - mainLayout->addWidget(icon, 1, Qt::AlignHCenter); + mainLayout->addWidget(icon, 1, Qt::AlignCenter); mainLayout->addWidget(new QLabel(tr("Robot Emulator"), this), 0, Qt::AlignHCenter); diff --git a/generateground.cpp b/generateground.cpp index 7ad1bb4..ce16839 100644 --- a/generateground.cpp +++ b/generateground.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include "groundbase.h" @@ -32,6 +33,7 @@ GenerateGround::GenerateGround(QWidget *parent) : m_borderEditor(new PolygonEditor(this)), m_barracksEditor(new PolygonEditor(this)), m_previewer(new GroundPreviewer(this)), + m_previewerGroup(new QGroupBox(this)), m_okay(new QPushButton(this)), m_cancel(new QPushButton(this)) { @@ -113,7 +115,12 @@ GenerateGround::GenerateGround(QWidget *parent) : setLayout(mainLayout); mainLayout->addWidget(m_tabManager, 1); - mainLayout->addWidget(m_previewer, 0, Qt::AlignCenter); + mainLayout->addWidget(m_previewerGroup, 0, Qt::AlignCenter); + + QBoxLayout *groupLayout=new QBoxLayout(QBoxLayout::LeftToRight, + m_previewerGroup); + groupLayout->addWidget(m_previewer); + m_previewerGroup->setLayout(groupLayout); m_tabManager->addTab(m_borderEditor, ""); m_tabManager->addTab(m_barracksEditor, ""); @@ -133,6 +140,8 @@ void GenerateGround::retranslate() m_okay->setText(tr("Ok")); m_cancel->setText(tr("Cancel")); + m_previewerGroup->setTitle(tr("Previewer")); + m_tabManager->setTabText(0, tr("Ground")); m_tabManager->setTabText(1, tr("Barracks")); } diff --git a/generateground.h b/generateground.h index 27fc388..d900fea 100644 --- a/generateground.h +++ b/generateground.h @@ -21,6 +21,7 @@ #include "generategroundbase.h" +class QGroupBox; class QPushButton; class QTabWidget; class PolygonEditor; @@ -75,6 +76,7 @@ private slots: QTabWidget *m_tabManager; PolygonEditor *m_borderEditor, *m_barracksEditor; GroundPreviewer *m_previewer; + QGroupBox *m_previewerGroup; QPushButton *m_okay, *m_cancel; QPolygonF m_border, m_barracks; diff --git a/gridwidget.cpp b/gridwidget.cpp new file mode 100644 index 0000000..059062a --- /dev/null +++ b/gridwidget.cpp @@ -0,0 +1,68 @@ +/* + * 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 + +#include "groundglobal.h" + +#include "gridwidget.h" + +GridWidget::GridWidget(QWidget *parent) : + QWidget(parent), + m_widget(nullptr) +{ + setAutoFillBackground(true); +} + +void GridWidget::paintEvent(QPaintEvent *event) +{ + //Paint the widget. + QWidget::paintEvent(event); + //Paint the reference line. + QPainter painter(this); + painter.setPen(GroundGlobal::instance()->referenceLineColor()); + painter.setBrush(QColor(0,0,0,0)); + for(int i=30; i<=height(); i+=30) + { + painter.drawLine(0, i, width(), i); + } + for(int i=30; i<=width(); i+=30) + { + painter.drawLine(i, 0, i, height()); + } +} +QWidget *GridWidget::widget() const +{ + return m_widget; +} + +void GridWidget::setWidget(QWidget *widget) +{ + m_widget = widget; + m_widget->setParent(this); +} + +void GridWidget::resizeEvent(QResizeEvent *event) +{ + QWidget::resizeEvent(event); + //If the widget is not null, resize it. + if(m_widget) + { + m_widget->resize(size()); + } +} + diff --git a/gridwidget.h b/gridwidget.h new file mode 100644 index 0000000..1812ca2 --- /dev/null +++ b/gridwidget.h @@ -0,0 +1,45 @@ +/* + * 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 GRIDWIDGET_H +#define GRIDWIDGET_H + +#include + +class GridWidget : public QWidget +{ + Q_OBJECT +public: + explicit GridWidget(QWidget *parent = 0); + + QWidget *widget() const; + void setWidget(QWidget *widget); + +signals: + +public slots: + +protected: + void resizeEvent(QResizeEvent *event); + void paintEvent(QPaintEvent *event); + +private: + QWidget *m_widget; +}; + +#endif // GRIDWIDGET_H diff --git a/ground.cpp b/ground.cpp index 969ab0f..ba22a28 100644 --- a/ground.cpp +++ b/ground.cpp @@ -50,9 +50,12 @@ Ground::Ground(QWidget *parent) : m_lastOpenFolder(QApplication::applicationDirPath()), m_lastSaveFolder(QApplication::applicationDirPath()) { + //Set parameters. setContentsMargins(0,0,0,0); + //Initial robots. + Robot::initialRobotPatameters(); //Configure the timer. - m_timeline->setInterval(16); //This will update the image for 60fps. + m_timeline->setInterval(6); //This will update the image for 60fps. connect(m_timeline, &QTimer::timeout, this, &Ground::onActionUpdateRobot); @@ -68,6 +71,11 @@ Ground::Ground(QWidget *parent) : m_actions[SaveAs]->setShortcut(QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_S)); m_actions[Close]->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_W)); + //Disable the save, save as and close as default. + m_actions[Save]->setEnabled(false); + m_actions[SaveAs]->setEnabled(false); + m_actions[Close]->setEnabled(false); + connect(m_actions[New], static_cast(&QAction::triggered), [=]{onActionNew();}); @@ -181,30 +189,36 @@ void Ground::paintEvent(QPaintEvent *event) painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform, true); + //Set translation. painter.translate(Robot::detectRadius(), Robot::detectRadius()); - //Draw the reference line. - painter.setPen(m_groundGlobal->referenceLineColor()); - painter.setBrush(QColor(0,0,0,0)); - for(int i=0; i<=height(); i+=30) - { - painter.drawLine(0, i, width(), i); - } - for(int i=0; i<=width(); i+=30) - { - painter.drawLine(i, 0, i, height()); - } //Draw the border. painter.setPen(m_groundGlobal->borderColor()); + QBrush borderBrush(Qt::DiagCrossPattern); + borderBrush.setColor(m_groundGlobal->borderColor()); + painter.setBrush(borderBrush); painter.drawPolygon(m_border); //Draw the barracks. painter.setPen(m_groundGlobal->barracksColor()); + painter.setBrush(QColor(0,0,0,0)); painter.drawPolygon(m_barracks); //Draw all the robot. + //First, draw the detect area. + painter.setPen(Qt::NoPen); + for(Robot *robot : m_robotList) + { + robot->paintRobotDetectArea(&painter); + } + //Then, draw the parameter. + painter.setPen(Robot::directionLineColor()); for(Robot *robot : m_robotList) { robot->paintRobotParameter(&painter); + } + //Finally, draw the robot. + for(Robot *robot : m_robotList) + { robot->paintRobot(&painter); } } @@ -294,7 +308,7 @@ void Ground::onActionUpdateRobot() void Ground::onActionNew() { //Stop the time line. - m_timeline->stop(); + pause(); //Close the current file. if(!onActionClose()) { @@ -319,7 +333,7 @@ void Ground::onActionNew() bool Ground::onActionOpen() { //Stop the time line. - m_timeline->stop(); + pause(); //Close the current file. if(!onActionClose()) @@ -378,8 +392,8 @@ bool Ground::onActionSaveAs() bool Ground::onActionClose() { - //Stop the time line. - m_timeline->stop(); + //Stop the timeline. + pause(); //Check if the current state is already close. if(m_filePath.isEmpty() && !m_changed) @@ -427,6 +441,10 @@ bool Ground::onActionClose() m_filePath=QString(); m_fileName=QString(); m_changed=false; + //Disable save and save as. + m_actions[Save]->setEnabled(false); + m_actions[SaveAs]->setEnabled(false); + m_actions[Close]->setEnabled(false); //Update the panel. update(); return true; @@ -646,6 +664,10 @@ bool Ground::readGroundData(const QString &filePath) m_changed=false; //Update the image. update(); + //Enabled save and save as actions. + m_actions[Save]->setEnabled(true); + m_actions[SaveAs]->setEnabled(true); + m_actions[Close]->setEnabled(true); return true; } return false; diff --git a/groundglobal.cpp b/groundglobal.cpp index 5f8dd50..2c9ea46 100644 --- a/groundglobal.cpp +++ b/groundglobal.cpp @@ -27,12 +27,26 @@ GroundGlobal *GroundGlobal::instance() GroundGlobal::GroundGlobal(QObject *parent) : QObject(parent), - m_borderColor(QColor(0,0,255)), + m_borderColor(QColor(5, 73, 88)), m_barracksColor(QColor(255,127,0)), - m_referenceLineColor(QColor(200,200,200)) + m_referenceLineColor(QColor(27, 68, 76)), + m_baseColor(QColor(1,33,44)) { } +QColor GroundGlobal::baseColor() const +{ + return m_baseColor; +} + +void GroundGlobal::setBaseColor(const QColor &baseColor) +{ + //Save base color. + m_baseColor = baseColor; + //Ask to change base color. + emit baseColorChanged(m_baseColor); +} + QColor GroundGlobal::borderColor() { return m_borderColor; diff --git a/groundglobal.h b/groundglobal.h index b8f0908..68d98a1 100644 --- a/groundglobal.h +++ b/groundglobal.h @@ -54,7 +54,18 @@ class GroundGlobal : public QObject */ QColor referenceLineColor(); + /*! + * \brief Get the color of the ground base. + * \return The QColor of the ground base. + */ + QColor baseColor() const; + signals: + /*! + * \brief When this signal is emitted, it will ask the container to change + * the base color. + */ + void baseColorChanged(const QColor &color); public slots: /*! @@ -69,10 +80,16 @@ public slots: */ void setBarracksColor(const QColor &barracksColor); + /*! + * \brief Sets the color of the ground base. + * \param baseColor The prefer color of the ground. + */ + void setBaseColor(const QColor &baseColor); + private: static GroundGlobal *m_instance; explicit GroundGlobal(QObject *parent = 0); - QColor m_borderColor, m_barracksColor, m_referenceLineColor; + QColor m_borderColor, m_barracksColor, m_referenceLineColor, m_baseColor; }; #endif // GROUNDGLOBAL_H diff --git a/main.cpp b/main.cpp index fc808ed..65d0a68 100644 --- a/main.cpp +++ b/main.cpp @@ -15,6 +15,8 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#include + #include "mainwindow.h" #include @@ -32,6 +34,8 @@ int main(int argc, char *argv[]) QApplication::setApplicationName("Robot Emulator"); QApplication::setApplicationDisplayName("Robot Emulator"); QApplication::setApplicationVersion("1.0"); + //Set style. + QApplication::setStyle(QStyleFactory::create("fusion")); //Conrtruct main window. MainWindow mainWindow; mainWindow.show(); diff --git a/mainwindow.cpp b/mainwindow.cpp index df7e4fc..2fa46e8 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -16,11 +16,14 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include -#include #include +#include +#include #include "about.h" +#include "gridwidget.h" #include "ground.h" +#include "groundglobal.h" #include "paneldock.h" #include "menubar.h" #include "robotmanagement.h" @@ -40,11 +43,80 @@ MainWindow::MainWindow(QWidget *parent) : //Set properties. setWindowIcon(QIcon("://res/icon.png")); setMinimumSize(500, 309); + //Initial the grid widget. + GridWidget *gridWidget=new GridWidget(this); + setCentralWidget(gridWidget); //Initial the scroll area. QScrollArea *groundArea=new QScrollArea(this); - setCentralWidget(groundArea); + gridWidget->setWidget(groundArea); + groundArea->setAutoFillBackground(true); groundArea->setAlignment(Qt::AlignCenter); groundArea->setWidget(m_ground); + groundArea->setFrameStyle(QFrame::NoFrame); + groundArea->verticalScrollBar()->setStyleSheet("QScrollBar:vertical {" + " border: 0px solid grey;" + " background: rgba(0, 0, 0, 0);" + " width: 8px;" + "}" + "QScrollBar::handle:vertical {" + " background: rgba(100, 100, 100);" + " min-height: 10px;" + " border-radius: 4px;" + "}" + "QScrollBar::add-line:vertical {" + " border: 0px solid grey;" + " background: rgba(0, 0, 0, 100);" + " height: 0px;" + " subcontrol-position: down;" + " subcontrol-origin: margin;" + "}" + "QScrollBar::sub-line:vertical {" + " border: 0px solid grey;" + " background: rgba(0, 0, 0, 100);" + " height: 0px;" + " subcontrol-position: up;" + " subcontrol-origin: margin;" + "}"); + groundArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal {" + " border: 0px solid grey;" + " background: rgba(0, 0, 0, 0);" + " height: 8px;" + "}" + "QScrollBar::handle:horizontal {" + " background: rgba(100, 100, 100);" + " min-height: 10px;" + " border-radius: 4px;" + "}" + "QScrollBar::add-line:horizontal {" + " border: 0px solid grey;" + " background: rgba(0, 0, 0, 100);" + " width: 0px;" + " subcontrol-position: down;" + " subcontrol-origin: margin;" + "}" + "QScrollBar::sub-line:horizontal {" + " border: 0px solid grey;" + " background: rgba(0, 0, 0, 100);" + " width: 0px;" + " subcontrol-position: up;" + " subcontrol-origin: margin;" + "}"); + + //Update the palette of ground area. + QPalette pal=groundArea->palette(); + pal.setColor(QPalette::Window, QColor(0,0,0,0)); + groundArea->setPalette(pal); + pal=gridWidget->palette(); + pal.setColor(QPalette::Window, GroundGlobal::instance()->baseColor()); + gridWidget->setPalette(pal); + //Link the color changed signal. + connect(GroundGlobal::instance(), &GroundGlobal::baseColorChanged, + [=](const QColor &color) + { + QPalette pal=gridWidget->palette(); + pal.setColor(QPalette::Window, color); + gridWidget->setPalette(pal); + }); //Set the ground generator. m_ground->setGenerator(m_groundGenerator); diff --git a/mainwindow.h b/mainwindow.h index 4f3949e..343d184 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -21,7 +21,6 @@ #include -class QScrollArea; class Ground; class PanelDock; class MenuBar; diff --git a/pointeditor.cpp b/pointeditor.cpp index f351912..943e919 100644 --- a/pointeditor.cpp +++ b/pointeditor.cpp @@ -87,6 +87,8 @@ PointEditor::PointEditor(QWidget *parent) : //Initial the edit area layout. QGridLayout *editAreaLayout=new QGridLayout(mainLayout->widget()); + editAreaLayout->setContentsMargins(0,0,0,0); + editAreaLayout->setVerticalSpacing(0); mainLayout->addLayout(editAreaLayout, 1); editAreaLayout->addWidget(new QLabel("X:"), 0, 0, 1, 1, Qt::AlignRight); diff --git a/polygoneditor.cpp b/polygoneditor.cpp index 94acaa2..377519d 100644 --- a/polygoneditor.cpp +++ b/polygoneditor.cpp @@ -156,9 +156,11 @@ void PolygonEditor::addPoint(const QPointF &point) { m_groundData->insertRow(m_groundPoints->currentIndex().row()+1, pointRow); + m_groundPoints->setCurrentIndex(pointRow.at(0)->index()); } else { m_groundData->appendRow(pointRow); + m_groundPoints->setCurrentIndex(pointRow.at(0)->index()); } } diff --git a/robot.cpp b/robot.cpp index 5dd527c..c5429be 100644 --- a/robot.cpp +++ b/robot.cpp @@ -15,14 +15,12 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include - #include "robot.h" #include Robot::Robot() : - m_angle(0.0), + RobotBase(), m_hasGuardianLine(false), m_guardianLine(QPointF(0,0), QPointF(0,0)), m_toP1Distance(0.0), @@ -43,115 +41,6 @@ Robot::Robot(qreal x, qreal y) : setPos(x, y); } -QPointF Robot::pos() const -{ - return m_pos; -} - -void Robot::setPos(const QPointF &pos) -{ - m_pos = pos; -} - -void Robot::paintRobot(QPainter *painter) -{ - //Draw the robot. - painter->setPen(m_robotColor); - painter->drawEllipse(m_pos, m_robotSize, m_robotSize); -} - -void Robot::paintRobotParameter(QPainter *painter) -{ - //Draw the direction of the robot. - QLineF directionLine(m_pos, m_pos+QPointF(m_detectRadius, 0)); - directionLine.setAngle(m_angle); - painter->setPen(m_directionLineColor); - painter->drawLine(directionLine); - - //Draw the detect radius. - painter->setPen(m_detectRadiusColor); - painter->drawEllipse(m_pos, m_detectRadius, m_detectRadius); -} - -int Robot::m_robotSize=2; - -int Robot::robotSize() -{ - return m_robotSize; -} - -void Robot::setRobotSize(int robotSize) -{ - m_robotSize = robotSize; -} - -int Robot::m_detectRadius=20; - -int Robot::detectRadius() -{ - return m_detectRadius; -} - -void Robot::setDetectRadius(int detectRadius) -{ - m_detectRadius = detectRadius; -} - -QColor Robot::m_robotColor=QColor(255,0,255); - -void Robot::setRobotColor(const QColor &robotColor) -{ - m_robotColor=robotColor; -} - -QColor Robot::robotColor() -{ - return m_robotColor; -} - -QColor Robot::m_detectRadiusColor=QColor(127,0,255); - -QColor Robot::detectRadiusColor() -{ - return m_detectRadiusColor; -} - -void Robot::setDetectRadiusColor(const QColor &detectRadiusColor) -{ - m_detectRadiusColor = detectRadiusColor; -} - -qreal Robot::angle() const -{ - return m_angle; -} - -void Robot::setAngle(const qreal &angle) -{ - m_angle = angle; - //Change the angle. - while(m_angle>360.0) - { - m_angle-=360.0; - } - while(m_angle<0.0) - { - m_angle+=360.0; - } -} - -QColor Robot::m_directionLineColor=QColor(127, 127, 0); - -QColor Robot::directionLineColor() -{ - return m_directionLineColor; -} - -void Robot::setDirectionLineColor(const QColor &detectLineColor) -{ - m_directionLineColor = detectLineColor; -} - void Robot::addToDetectList(Robot *robot) { //Check the robot whether has already be in the list. @@ -453,4 +342,3 @@ void Robot::moveToOppositeDirection() m_angle=m_guardianLine.angle(); } } - diff --git a/robot.h b/robot.h index e605166..f591c1b 100644 --- a/robot.h +++ b/robot.h @@ -19,10 +19,10 @@ #ifndef ROBOT_H #define ROBOT_H -#include #include #include -#include + +#include "robotbase.h" class QPainter; /*! @@ -32,7 +32,7 @@ class QPainter; * You have to recover the memory yourself. It don't have a parent. \n * This is not a QObject for memory reduce, so there's no signal and slots. */ -class Robot +class Robot : public RobotBase { public: /*! @@ -54,118 +54,6 @@ class Robot */ Robot(qreal x, qreal y); - /*! - * \brief The position of the robot. - * \return The QPointF format robot position. - */ - QPointF pos() const; - - /*! - * \brief Set the position of a robot. - * \param pos The position of the robot. - */ - void setPos(const QPointF &pos); - - /*! - * \brief This is an overloaded function.\n - * Set the position of a robot. - * \param x The x position of the robot. - * \param y The y position of the robot. - */ - inline void setPos(qreal x, qreal y) - { - setPos(QPointF(x, y)); - } - - /*! - * \brief Paint the robot with the specific painter. - * \param painter The painting painter. - */ - void paintRobot(QPainter *painter); - - /*! - * \brief Paint the parameter of the robot with the specific painter.\n - * The parameters of a robots include the range and angle. - * \param painter The painting painter. - */ - void paintRobotParameter(QPainter *painter); - - /*! - * \brief Get the size of all the robots. - * \return The size of all robots. - */ - static int robotSize(); - - /*! - * \brief Change the size of all the robots. - * \param robotSize The new size of all robots. - */ - static void setRobotSize(int robotSize); - - /*! - * \brief Get the detect radius of all the robots. - * \return The detect radius of all robots. - */ - static int detectRadius(); - - /*! - * \brief Change all the detect radius of all the robots. - * \param detectRadius The new detect radius of all the robots. - */ - static void setDetectRadius(int detectRadius); - - /*! - * \brief Sets the color of the robot. - * \param robotColor The prefer color of all robots. - */ - static void setRobotColor(const QColor &robotColor); - - /*! - * \brief Get the color of all robots. - * \return The QColor of the robots. - */ - static QColor robotColor(); - - /*! - * \brief Get the color of the detection radius border of the robots. - * \return The QColor of the detection radius border of the robots. - */ - static QColor detectRadiusColor(); - - /*! - * \brief Change the color of the robot detection radius border. - * \param robotColor The prefer color of all the detection radius border of - * robots. - */ - static void setDetectRadiusColor(const QColor &detectRadiusColor); - - /*! - * \brief Get the moving angle of the robot. \n - * The default angle of the robot is 0 (3'o clock position). - * \return The angle of the robot. - */ - qreal angle() const; - - /*! - * \brief Change the moving angle of the robot, the value should be ranged - * from 0 degrees to 360 degrees. The program will automatically change the - * value. - * \param angle The prefer angle of the robot. - */ - void setAngle(const qreal &angle); - - /*! - * \brief Get the color the all the direction line color of the robots. - * \return The color of the direction line. - */ - static QColor directionLineColor(); - - /*! - * \brief Change the color of the direction line. - * \param directionLineColor The direction line color. - */ - static void setDirectionLineColor(const QColor &directionLineColor); - /*! * \brief Add a detected robot to the detection list. \n * If the robot has been in the detect list, it won't add it twice. @@ -268,6 +156,11 @@ class Robot return QLineF(p1, p2).length(); } + /*! + * \brief This will return the detected list is empty or not. It's used to + * reset the robot. + * \return If the list is empty, return true. + */ bool isDetectedListEmpty() { return m_detectedRobotList.isEmpty(); @@ -290,14 +183,9 @@ class Robot return status1.distance < status2.distance; } }; - - static int m_robotSize, m_detectRadius; - QPointF m_pos; + //Robot list information. QList m_detectedRobotList; - static QColor m_robotColor, m_detectRadiusColor, m_directionLineColor; - qreal m_angle; - //Gardian line information bool m_hasGuardianLine; QLineF m_guardianLine, m_oppositeGuardianLine; diff --git a/robotaddwidget.cpp b/robotaddwidget.cpp index 4dc3425..8926826 100644 --- a/robotaddwidget.cpp +++ b/robotaddwidget.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -26,6 +27,7 @@ RobotAddWidget::RobotAddWidget(QWidget *parent) : QWidget(parent), + m_editArea(new QGroupBox(this)), m_okay(new QPushButton(this)), m_cancel(new QPushButton(this)), m_xData(new QSpinBox(this)), @@ -60,7 +62,8 @@ RobotAddWidget::RobotAddWidget(QWidget *parent) : QBoxLayout *controlLayout=new QBoxLayout(QBoxLayout::TopToBottom, mainLayout->widget()); controlLayout->setSpacing(2); - mainLayout->addLayout(controlLayout); + m_editArea->setLayout(controlLayout); + mainLayout->addWidget(m_editArea, 1); QBoxLayout *positionLayout=new QBoxLayout(QBoxLayout::LeftToRight, mainLayout->widget()); @@ -140,6 +143,8 @@ void RobotAddWidget::retranslate() m_labels[0]->setText(tr("X:")); m_labels[1]->setText(tr("Y:")); m_labels[2]->setText(tr("Angle:")); + + m_editArea->setTitle(tr("Robot Initial Status")); } void RobotAddWidget::onActionParameterChange() diff --git a/robotaddwidget.h b/robotaddwidget.h index 312cab8..787fbd5 100644 --- a/robotaddwidget.h +++ b/robotaddwidget.h @@ -25,6 +25,7 @@ class QPushButton; class QSpinBox; class QSlider; class QLabel; +class QGroupBox; class GroundBase; /*! * \brief The RobotAddWidget class is a widget for user to generate a robot in a @@ -100,6 +101,7 @@ private slots: void onActionParameterChange(); private: + QGroupBox *m_editArea; QPushButton *m_okay, *m_cancel; QLabel *m_labels[3]; QSpinBox *m_xData, *m_yData; diff --git a/robotbase.cpp b/robotbase.cpp new file mode 100644 index 0000000..ddd462f --- /dev/null +++ b/robotbase.cpp @@ -0,0 +1,152 @@ +/* + * 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 + +#include "robotbase.h" + +RobotBase::RobotBase() : + m_pos(QPointF(0, 0)), + m_angle(0.0) +{ +} + +QPointF RobotBase::pos() const +{ + return m_pos; +} + +void RobotBase::setPos(const QPointF &pos) +{ + m_pos = pos; +} + +void RobotBase::paintRobot(QPainter *painter) +{ + QPen robotPen(m_robotColor); + robotPen.setWidth(2); + + //Draw the robot. + painter->setPen(robotPen); + painter->drawEllipse(m_pos, m_robotSize, m_robotSize); +} + +void RobotBase::paintRobotParameter(QPainter *painter) +{ + //Draw the direction of the robot. + QLineF directionLine(m_pos, m_pos+QPointF(m_detectRadius, 0)); + directionLine.setAngle(m_angle); + painter->drawLine(directionLine); +} + +void RobotBase::paintRobotDetectArea(QPainter *painter) +{ + //Draw the detect radius. + m_detectRadiusGradient.setFocalPoint(m_pos); + m_detectRadiusGradient.setCenter(m_pos); + painter->setBrush(m_detectRadiusGradient); + painter->drawEllipse(m_pos, m_detectRadius, m_detectRadius); +} + +int RobotBase::m_robotSize=2; + +int RobotBase::robotSize() +{ + return m_robotSize; +} + +void RobotBase::setRobotSize(int robotSize) +{ + m_robotSize = robotSize; +} + +int RobotBase::m_detectRadius=20; + +int RobotBase::detectRadius() +{ + return m_detectRadius; +} + +void RobotBase::setDetectRadius(int detectRadius) +{ + m_detectRadius = detectRadius; +} + +QColor RobotBase::m_robotColor=QColor(255,255,255); + +void RobotBase::setRobotColor(const QColor &robotColor) +{ + m_robotColor=robotColor; +} + +QColor RobotBase::robotColor() +{ + return m_robotColor; +} + +QColor RobotBase::m_detectRadiusColor=QColor(100,48,67); + +QColor RobotBase::detectRadiusColor() +{ + return m_detectRadiusColor; +} + +void RobotBase::setDetectRadiusColor(const QColor &detectRadiusColor) +{ + m_detectRadiusColor = detectRadiusColor; +} + +qreal RobotBase::angle() const +{ + return m_angle; +} + +void RobotBase::setAngle(const qreal &angle) +{ + m_angle = angle; + //Change the angle. + while(m_angle>360.0) + { + m_angle-=360.0; + } + while(m_angle<0.0) + { + m_angle+=360.0; + } +} + +QColor RobotBase::m_directionLineColor=QColor(98, 228, 238); + +QColor RobotBase::directionLineColor() +{ + return m_directionLineColor; +} + +void RobotBase::setDirectionLineColor(const QColor &detectLineColor) +{ + m_directionLineColor = detectLineColor; +} + +QRadialGradient RobotBase::m_detectRadiusGradient=QRadialGradient(); + +void RobotBase::initialRobotPatameters() +{ + m_detectRadiusGradient.setRadius(m_detectRadius); + m_detectRadiusGradient.setColorAt(0, QColor(0,0,0,0)); + m_detectRadiusGradient.setColorAt(0.6, QColor(0,0,0,0)); + m_detectRadiusGradient.setColorAt(1, m_detectRadiusColor); +} diff --git a/robotbase.h b/robotbase.h new file mode 100644 index 0000000..ea34833 --- /dev/null +++ b/robotbase.h @@ -0,0 +1,171 @@ +/* + * 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 ROBOTBASE_H +#define ROBOTBASE_H + +#include +#include +#include + +/*! + * \brief The RobotBase class is a base class of robot, it contains all the + * basic functions of a robot. + */ +class RobotBase +{ +public: + /*! + * \brief Construct a RobotBase class. + */ + RobotBase(); + + /*! + * \brief The position of the robot. + * \return The QPointF format robot position. + */ + QPointF pos() const; + + /*! + * \brief Set the position of a robot. + * \param pos The position of the robot. + */ + void setPos(const QPointF &pos); + + /*! + * \brief This is an overloaded function.\n + * Set the position of a robot. + * \param x The x position of the robot. + * \param y The y position of the robot. + */ + inline void setPos(qreal x, qreal y) + { + setPos(QPointF(x, y)); + } + + /*! + * \brief Paint the robot with the specific painter. + * \param painter The painting painter. + */ + void paintRobot(QPainter *painter); + + /*! + * \brief Paint the parameter of the robot with the specific painter.\n + * The parameters of a robots include the range and angle. + * \param painter The painting painter. + */ + void paintRobotParameter(QPainter *painter); + + /*! + * \brief Paint the detect area of the current robot with the specific + * painter. + * \param painter The painting painter. + */ + void paintRobotDetectArea(QPainter *painter); + + /*! + * \brief Get the size of all the robots. + * \return The size of all robots. + */ + static int robotSize(); + + /*! + * \brief Change the size of all the robots. + * \param robotSize The new size of all robots. + */ + static void setRobotSize(int robotSize); + + /*! + * \brief Get the detect radius of all the robots. + * \return The detect radius of all robots. + */ + static int detectRadius(); + + /*! + * \brief Change all the detect radius of all the robots. + * \param detectRadius The new detect radius of all the robots. + */ + static void setDetectRadius(int detectRadius); + + /*! + * \brief Sets the color of the robot. + * \param robotColor The prefer color of all robots. + */ + static void setRobotColor(const QColor &robotColor); + + /*! + * \brief Get the color of all robots. + * \return The QColor of the robots. + */ + static QColor robotColor(); + + /*! + * \brief Get the color of the detection radius border of the robots. + * \return The QColor of the detection radius border of the robots. + */ + static QColor detectRadiusColor(); + + /*! + * \brief Change the color of the robot detection radius border. + * \param robotColor The prefer color of all the detection radius border of + * robots. + */ + static void setDetectRadiusColor(const QColor &detectRadiusColor); + + /*! + * \brief Get the moving angle of the robot. \n + * The default angle of the robot is 0 (3'o clock position). + * \return The angle of the robot. + */ + qreal angle() const; + + /*! + * \brief Change the moving angle of the robot, the value should be ranged + * from 0 degrees to 360 degrees. The program will automatically change the + * value. + * \param angle The prefer angle of the robot. + */ + void setAngle(const qreal &angle); + + /*! + * \brief Get the color the all the direction line color of the robots. + * \return The color of the direction line. + */ + static QColor directionLineColor(); + + /*! + * \brief Change the color of the direction line. + * \param directionLineColor The direction line color. + */ + static void setDirectionLineColor(const QColor &directionLineColor); + + /*! + * \brief Initial the grapihcs parameters of all the Robot. + */ + static void initialRobotPatameters(); + +protected: + QPointF m_pos; + qreal m_angle; + + static int m_robotSize, m_detectRadius; + static QColor m_robotColor, m_detectRadiusColor, m_directionLineColor; + static QRadialGradient m_detectRadiusGradient; +}; + +#endif // ROBOTBASE_H diff --git a/robotmanagement.cpp b/robotmanagement.cpp index 3719a69..5c018f3 100644 --- a/robotmanagement.cpp +++ b/robotmanagement.cpp @@ -17,6 +17,7 @@ */ #include #include +#include #include "ground.h" #include "robot.h" @@ -31,6 +32,7 @@ RobotManagement::RobotManagement(QWidget *parent) : QDialog(parent), + m_previewGroup(new QGroupBox(this)), m_ground(nullptr), m_groundPreview(new GroundRealtimePreviewer(this)), m_stackLayout(new QStackedLayout), @@ -40,7 +42,11 @@ RobotManagement::RobotManagement(QWidget *parent) : QBoxLayout *mainLayout=new QBoxLayout(QBoxLayout::LeftToRight, this); setLayout(mainLayout); - mainLayout->addWidget(m_groundPreview); + QBoxLayout *previewLayout=new QBoxLayout(QBoxLayout::LeftToRight, + m_previewGroup); + previewLayout->addWidget(m_groundPreview); + m_previewGroup->setLayout(previewLayout); + mainLayout->addWidget(m_previewGroup); mainLayout->addLayout(m_stackLayout, 1); //If we are using Mac OS X, using the sheet window flag. #ifdef Q_OS_MACX @@ -176,6 +182,8 @@ void RobotManagement::retranslate() { m_actions[AddRobot]->setText(tr("Add robot")); m_actions[ManageRobot]->setText(tr("Manage robots")); + + m_previewGroup->setTitle(tr("Previewer")); } void RobotManagement::onActionAddRobot(const QPointF &position, diff --git a/robotmanagement.h b/robotmanagement.h index 10bb449..21a31b5 100644 --- a/robotmanagement.h +++ b/robotmanagement.h @@ -22,6 +22,7 @@ #include class QStackedLayout; +class QGroupBox; class GroundBase; class MenuBar; class RobotAddWidget; @@ -86,6 +87,7 @@ private slots: }; QAction *m_actions[RobotManagementActionsCount]; + QGroupBox *m_previewGroup; GroundBase *m_ground; GroundRealtimePreviewer *m_groundPreview; QStackedLayout *m_stackLayout; diff --git a/robotmanagewidget.cpp b/robotmanagewidget.cpp index 8c9d496..cfbadd5 100644 --- a/robotmanagewidget.cpp +++ b/robotmanagewidget.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include "robot.h" #include "groundbase.h" @@ -36,11 +37,13 @@ RobotManageWidget::RobotManageWidget(QWidget *parent) : m_deletePoint(new QPushButton(this)), m_robotInitialDataView(new QTreeView(this)), m_robotInitialDataModel(new QStandardItemModel(this)), - m_ground(nullptr) + m_ground(nullptr), + m_manageBox(new QGroupBox(this)) { //Set the palette. QPalette pal=m_deletePoint->palette(); pal.setColor(QPalette::Button, QColor(255,0,0)); + pal.setColor(QPalette::ButtonText, QColor(255,255,255)); m_deletePoint->setPalette(pal); //Configure robot list. @@ -77,12 +80,14 @@ RobotManageWidget::RobotManageWidget(QWidget *parent) : mainLayout->setContentsMargins(0,0,0,0); setLayout(mainLayout); + //Generate the editor layout. QBoxLayout *robotViewLayout=new QBoxLayout(QBoxLayout::TopToBottom, mainLayout->widget()); + m_manageBox->setLayout(robotViewLayout); robotViewLayout->addWidget(m_robotInitialDataView, 1); robotViewLayout->addWidget(m_deletePoint); - mainLayout->addLayout(robotViewLayout, 1); + mainLayout->addWidget(m_manageBox, 1); //Generate the button layout. QBoxLayout *buttonLayout=new QBoxLayout(QBoxLayout::TopToBottom, @@ -161,6 +166,8 @@ void RobotManageWidget::retranslate() m_cancel->setText(tr("Cancel")); m_deletePoint->setText(tr("Delete")); + m_manageBox->setTitle(tr("Robots")); + updateHeader(); } diff --git a/robotmanagewidget.h b/robotmanagewidget.h index b427a39..1676959 100644 --- a/robotmanagewidget.h +++ b/robotmanagewidget.h @@ -25,6 +25,7 @@ class QTreeView; class QPushButton; class QStandardItemModel; class QGridLayout; +class QGroupBox; class Robot; class GroundBase; /*! @@ -133,6 +134,7 @@ private slots: QStandardItemModel *m_robotInitialDataModel; QList m_robotList; GroundBase *m_ground; + QGroupBox *m_manageBox; }; #endif // ROBOTMANAGEWIDGET_H