diff --git a/DataStructureProject.pro b/DataStructureProject.pro index cf5256a..062fa56 100644 --- a/DataStructureProject.pro +++ b/DataStructureProject.pro @@ -4,7 +4,7 @@ # #------------------------------------------------- -QT += core gui +QT += core gui QT += xml greaterThan(QT_MAJOR_VERSION, 4): QT += widgets @@ -27,30 +27,21 @@ DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ main.cpp \ mainwindow.cpp \ - monitor.cpp \ - cars.cpp \ - parkinglot.cpp \ - cars.cpp \ - main.cpp \ - mainwindow.cpp \ - monitor.cpp \ - parkinglot.cpp \ - ParkingLotWidget.cpp \ - ParkingSpaceWidget.cpp \ - Road.cpp + monitor.cpp \ + cars.cpp \ + parkinglot.cpp \ + ParkingLotWidget.cpp \ + Road.cpp \ + ParkingSpaceWidget.cpp HEADERS += \ mainwindow.h \ - monitor.h \ - cars.h \ - parkinglot.h \ - cars.h \ - mainwindow.h \ - monitor.h \ - parkinglot.h \ - ParkingLotWidget.h \ - ParkingSpaceWidget.h \ - Road.h + monitor.h \ + cars.h \ + parkinglot.h \ + ParkingLotWidget.h \ + Road.h \ + ParkingSpaceWidget.h FORMS += \ mainwindow.ui diff --git a/ParkingLotWidget.cpp b/ParkingLotWidget.cpp index d1b45d9..67fa634 100644 --- a/ParkingLotWidget.cpp +++ b/ParkingLotWidget.cpp @@ -1,4 +1,4 @@ -#include "ParkingLotWidget.h" +#include "ParkingLotWidget.h" #include "ParkingSpaceWidget.h" #include #include @@ -55,7 +55,7 @@ void ParkingLotWidget::parse_xml(const QString & xml) layout = parseLayout(element); } else { - qDebug() << this -> objectName() << "²¼¾ÖÎļþ´íÎó"; + qDebug() << this -> objectName() << "布局文件错误"; } } @@ -88,10 +88,10 @@ QBoxLayout * ParkingLotWidget::parseLayout(const QDomElement & element) else if ((name.at(0) == 'v' || name.at(0) == 'h' )&& name.contains("Layout")) layout->addLayout(parseLayout(child)); else - qDebug() << this->objectName() << "´íÎóµÄxml±êÇ©"; + qDebug() << this->objectName() << "错误的xml标签"; child = child.nextSiblingElement(); } - layout->addStretch(); // Ìí¼ÓÉìËõ£¬ÊµÏÖ¾ÓÉÏ»ò¾Ó×ó¶ÔÆë + layout->addStretch(); // 添加伸缩,实现居上或居左对é½� layout->setMargin(0); layout->setSpacing(0); return layout; diff --git a/ParkingSpaceWidget.cpp b/ParkingSpaceWidget.cpp index 4f35089..7c1b930 100644 --- a/ParkingSpaceWidget.cpp +++ b/ParkingSpaceWidget.cpp @@ -1,4 +1,4 @@ -#include "ParkingSpaceWidget.h" +#include "ParkingSpaceWidget.h" #include #include #include "ParkingLotWidget.h" @@ -13,7 +13,7 @@ ParkingSpaceWidget::ParkingSpaceWidget(ParkingLotWidget* parent, const QString & case 'E':d = E; break; case 'W':d = W; break; case 'S':d = S; break; - default: qDebug() << this->objectName() << "´íÎóµÄ·½Ïò"; + default: qDebug() << this->objectName() << "错误的方å�‘"; } this->dir = d; switch (this->dir) @@ -46,7 +46,7 @@ QBoxLayout *ParkingSpaceWidget::makeParkingSapceGroup(ParkingLotWidget* parent, ParkingSpaceWidget *pk = new ParkingSpaceWidget(parent, dir); layout->addWidget(pk); } - layout->addStretch(); // ¾ÓÉÏ»ò¾Ó×ó¶ÔÆë + layout->addStretch(); // 居上或居左对é½� layout->setMargin(0); layout->setSpacing(0); return layout; diff --git a/cars.cpp b/cars.cpp index 65e66a3..e9e3c67 100644 --- a/cars.cpp +++ b/cars.cpp @@ -1,17 +1,50 @@ #include "cars.h" -#include #include -Cars::Cars(QWidget *parent) : QWidget(parent) +Cars::Cars(QWidget *parent, int dir, Cars::Color color) : QWidget(parent) { - m_color = Color(rand() % 3); - resize(200, 130); + if(color==RANDOM) + color = Cars::Color(rand()%3); //³µÉíÑÕɫһ¶¨ÊÇÈ·¶¨µÄ + m_color = color; + m_dir = dir; + m_pos = this->pos(); + resize(M_WID, M_LEN); + switch (m_color) { + case Pink: + m_pixmap = QPixmap(":/cars/pink"); + break; + case Red: + m_pixmap = QPixmap(":/cars/red"); + break; + case Yellow: + m_pixmap = QPixmap(":/cars/yellow"); + break; + default: + m_pixmap = QPixmap(":/cars/red"); + break; + } } void Cars::paintEvent(QPaintEvent *event) { QPainter painter(this); - QPixmap pix(":/cars/yellow"); - painter.drawPixmap(0, 0, 200, 130, pix); + m_pos = this->pos(); + painter.translate(M_WID/2, M_LEN/2); //×ªÒÆÔ¶µã£¬ÉèÖÃÐýתÖÐÐÄ + painter.rotate(m_dir); //Ðýת + painter.translate(-M_WID/2, -M_LEN/2); //ÒÆ»ØÔ¶µã + painter.drawPixmap(0, 0, M_WID, M_LEN, m_pixmap); //»æÖÆÐ¡³µ painter.setPen(QPen(Qt::red, 5)); - painter.drawRect(0, 0, 200, 130); + painter.drawRect(0, 0, M_WID, M_LEN); + event->ignore(); +} + +void Cars::Forward(int vel) +{ + float ang = m_dir*PI/180; + this->move(m_pos.x()+vel*sin(ang), m_pos.y()-vel*cos(ang)); +} + +void Cars::Backward(int vel) +{ + float ang = m_dir*PI/180; + this->move(m_pos.x()-vel*sin(ang), m_pos.y()+vel*cos(ang)); } diff --git a/cars.h b/cars.h index 9b1606c..5371938 100644 --- a/cars.h +++ b/cars.h @@ -4,19 +4,30 @@ #include #include #include +#include +#include + +//³µÉí³¤¿í +#define M_LEN (200) +#define M_WID (130) +#define PI (3.1415926f) class Cars : public QWidget { Q_OBJECT public: - enum Color { Pink, Red, Yellow }; - explicit Cars(QWidget *parent = nullptr); - explicit Cars(Color color, QWidget *parenet = nullptr) {} + enum Color { Pink, Red, Yellow, RANDOM }; + explicit Cars(QWidget *parent = nullptr, int dir = 0, Cars::Color color = RANDOM); void paintEvent(QPaintEvent *event); + void Forward(int vel); + void Backward(int vel); private: - Color m_color; + Color m_color; //³µÉíÑÕÉ« + int m_dir; //³µÍ·³¯Ïò ·½Ïò½Ç NΪ0£¬ ˳ʱÕëµÝÔö QPixmap m_pixmap; QString m_number; + QPoint m_pos; + signals: public slots: diff --git a/mainwindow.cpp b/mainwindow.cpp index 1d414fc..036d000 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -19,7 +19,7 @@ MainWindow::MainWindow(QWidget *parent) : // scrollArea->resize(1800, 800); // monitor = new Monitor(this); // Cars *car = new Cars(this); - ParkingLotWidget *pk = new ParkingLotWidget(this, "D:\\wming\\Documents\\DataStructureProject\\parkinglot_f1.xml"); + ParkingLotWidget *pk = new ParkingLotWidget(this, "parkinglot_f1.xml"); // pk->show(); // pk->creatMap(); /*QLabel *label = new QLabel(this); diff --git a/pinkcar.png b/pinkcar.png index e4febfb..3dff2d6 100644 Binary files a/pinkcar.png and b/pinkcar.png differ diff --git a/redcar.png b/redcar.png index e77d927..fa42307 100644 Binary files a/redcar.png and b/redcar.png differ diff --git a/yellowcar.png b/yellowcar.png index 6e0392a..c5098a3 100644 Binary files a/yellowcar.png and b/yellowcar.png differ