一、系统界面
二、主要代码
运用多线程实现多辆小车的正常运行
运用 QTimer定时器控制交通灯的变换
头文件
1.car.h
#ifndef CAR_H
#define CAR_H
#include "mainwindow.h"
#include <QThread>
#include <QTime>
#include <QCoreApplication>
#include <QEventLoop>
#include <iostream>
#include "position.h"
using namespace std;
class Car : public QThread
{
Q_OBJECT
public :
Car();
Car(MainWindow *gui,int i,int x,int y, int width, int height);
~Car();
MainWindow *pMain;
int eastbar;
int westbar;
int carnumber;
int dir;
int position_x;
int position_y;
int position_width;
int position_height;
int bar;
QString carlicense;
QString carmoney;
QString carmoney2;
QString carmodel;
int car_x;
int car_y;
int car_width;
int car_height;
int mx;
int my;
int park_x;
int park_y;
int init_car_x;
int init_car_y;
void run();
void run_in();
void sleep(int t);
void FencesPower(int i); //栅栏控制
void redEnterCheck(int i);//红外检测入口栅栏何时关闭
void setCarInfo(QString carName,QString money,QString brand);//设置小车信息
void stopCarTime(unsigned int msec); //停车时间
void scanCar(int i); //扫描仪扫描小车信息
void cheatPark(); //诱导停车
void wheel(); //车辆转弯
void stopPark(); //车位停车
void walkExit();
void walkFangxiangToExit();
void exitPark();
QString getCarlicense();
QString getCarmoney();
QString getCarmodel();
void stop();
signals:
void update(int i,int x,int y, int width, int height);
void Fence_Update(int i);
void trans_update(int i,unsigned int ang);
void show_update(int i,QString str);
public slots:
void slotUpdate(int i,int x,int y, int width, int height);
void FenceUpdate(int i);
void transUpdate(int i, unsigned int angle);
void showUpdate(int i, QString carInfo);
};
#endif // CAR_H
2.mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTime>
#include <QApplication>
#include <QDebug>
#include <string>
#include <qfile.h>
#include <qtextstream.h>
#include <iostream>
#include <QDateTime>
#include<fstream>
#include<map>
#include <QTimer>
#include <QLabel>
#define INSTANCE 3
#define STOP 1
#define RUN 2
using namespace std;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void lightOn(QLabel *label);
void lightOff(QLabel *label);
void wheel();
void TurnPower(int x,int y);
void turnLeft();
void throughLight();
void stop();
void run();
void mysleep(unsigned int msec);
void parking();
void FencesEnterOn(); //入口栅栏开
void FencesEnterOff(); //入口栅栏关
void FencesExitOn(); //出口栅栏开
void FencesExitOff(); //出口栅栏关
void writeTofile(QString carInfo); //将小车信息写入文件函数
void splitCarInfor(QString carInfo,vector<string>& v); //切割小车信息
QString readFromFile(QString path); //返回从数据库中获取到的车位信息
string showToEnter(QString carInfo); //入口显示屏显示小车信息
void showToExit(QString carInfo); //入口显示屏显示小车信息
void getSysTime();
string readSqlCar(); //读取小车的车位信息
int judgeIsNullOfCar(QString linestr); //判断从数据库中获取到的车位是否被占用
void splitCarPort(string& str);
void setParkFlag(); //更改小车标志位
void writeDataFile(int count);
void initCarPort(); //map中初始化车位和标志位
string judgeCarPortFlag(); //判断车位标志能否停车
string readPositionFromFile(QString path);//从文件中读指定车位的位置信息
bool isPort(string lineStr); //判断从文件中读到的信息与对应车位是否一致
void setCarInfo(int num);
void trans(int ,unsigned int );
void updateposition(int ,int ,int ,int , int );
void updatelight();
private slots:
void on_pushButton_clicked();
void onTimerOut();
private:
string getXY(string lineStr); //提取lineStr的xy设置mx,my
string setWork();
private:
Ui::MainWindow *ui;
//car position
int mx;//car position x
int car_state;
int my;//car position y
int count;
string mLocalTime;
QTimer *timer;
int settimer1;
int settimer2;
map<string,int> maplive; //车位,标志
string mCarPort; //车位
int position_x;
int position_y;
int position_weight;
int position_height;
int position_x1;
int position_y1;
int position_weight1;
int position_height1;
int position_x2;
int position_y2;
int position_weight2;
int position_height2;
QPixmap pixmap;
QPixmap pixmap1;
QMatrix matrix;
QMatrix matrix1;
QPixmap pixmap3;
QMatrix matrix3;
QPixmap pixmap4;
QPixmap pixmap6;
QPixmap pixmap5;
QTimer *timer2;
QTimer *timer1;
QTimer *timer3;
int carx;
int carx2;
int light;
//Car *thread1,*thread2,*thread3,;
};
#endif // MAINWINDOW_H
3.position.h
#ifndef POSITION_H
#define POSITION_H
#define fencesEnter_x 540
#define fencesEnter_y 240
#define up 1
#define down 2
#define left 3
#define right 4
#define westbar_x 670
#define fencesExit_x 544
#define fencesExit_y 500
#define ExitRed_x 580
#define ExitRed_y 520
#define enterRed_x 580
#define enterRed_y 310
#define midLine_x 130
#define midLine_y 390
#define downStreet_x 130
#define downStreet_y 440
#define scanBtn_x 550
#define scanBtn_y 260
#define flagUp_x 130
#define flagUp_y 270
#define upStreet_x 130
#define upStreet_y 440
#define scanBtnExit_x 544
#define scanBtnExit_y 470
#define flagLineFive_x 610
#define CHECK_LENGTH 5
#define INSTANCE 2
#define leftline_x 128
#define leftline_y 230
#define downline_x 130
#define downline_y 670
#endif // POSITION_H
2.源文件
1.car.cpp
#include "car.h"
extern int isRun;
extern int flag;
extern struct PARK_POSITION park;
extern int isRun;
extern int westflag;
extern int eastflag;
Car::Car(MainWindow *gui, int i,int x,int y,int width,int height)
{ pMain=gui;
carnumber=i;
position_x=x;
position_y=y;
position_height=height;
position_width=width;
init_car_x=x;
init_car_y=y;
dir=0;
pMain=gui;
carnumber=i;
car_x=x;
car_y=y;
car_height=height;
car_width=width;
connect(this,SIGNAL(update(int ,int ,int , int, int)),this,SLOT(slotUpdate(int ,int ,int , int , int )));
connect(this,SIGNAL(Fence_Update(int)),this,SLOT(FenceUpdate(int)));
connect(this,SIGNAL(trans_update(int,unsigned int)),this,SLOT(transUpdate(int,unsigned int)));
connect(this,SIGNAL(show_update(int,QString)),this,SLOT(showUpdate(int,QString)));
cout<<"carnumber="<<carnumber<<endl;
cout<<"x="<<car_x<<endl;
cout<<"y="<<car_y<<endl;
cout<<"width="<<car_width<<endl;
cout<<"height="<<car_height<<endl;
}
Car::Car()
{
}
Car::~Car()
{
}
void Car::sleep(int t)
{
QTime newTime = QTime::currentTime().addMSecs(t);
while(QTime::currentTime() < newTime)
QCoreApplication::processEvents(QEventLoop::AllEvents,100);
}
void Car::setCarInfo(QString license, QString money, QString model)
{
carlicense = license;
carmoney= money;
carmodel= model;
}
QString Car:: getCarlicense()
{
return carlicense;
}
QString Car:: getCarmodel()
{
return carmodel;
}
QString Car:: getCarmoney()
{
return carmoney;
}
//检测入口栅栏何时打开
void Car::FencesPower(int flag)
{
int fences_x;
if(flag==3) fences_x=fencesExit_x;
if(flag==1) fences_x = fencesEnter_x;
if(carnumber==1){
for( int i=car_x; i-car_width<fences_x-CHECK_LENGTH; ++i)
{
emit update(carnumber,i,car_y,car_width,car_height);
sleep(12);
car_x=i;
}}
if(carnumber==2){
for( int i=car_x; i-car_width>fences_x-CHECK_LENGTH; --i)
{
emit update(carnumber,i,car_y,car_width,car_height);
sleep(12);
car_x=i;
}
}
if(carnumber==3){
for( int i=car_x; i<400; ++i)
{
emit update(carnumber,i,car_y,car_width,car_height);
sleep(12);
car_x=i;
}
int i;
while(isRun){
if(carnumber==3){
for( int i=car_x; i<400; ++i)
{
emit update(carnumber,i,car_y,car_width,car_height);
sleep(12);
car_x=i;
}
if(eastflag==1)
{
for(i=car_x;i<1500;++i)
{
emit update(carnumber ,i,car_y,car_width,car_height);
sleep(12);
}
car_x=i;
}
if(eastflag==0 && car_x >=670)
{
for(i=car_x;i<1000;++i)
{
emit update(carnumber,i,car_y,car_width,car_height);
sleep(12);
}
car_x=i;
}
if(car_x>=1000)
{
car_x=0;
emit update(carnumber,car_x,car_y,car_width,car_height);
}}
}
}
if(flag==1){
emit trans_update(carnumber,90);
// emit update(carnumber,car_x,car_y,car_height,car_width);
int tem=car_height;
car_height = car_width;
car_width = tem;
}
int fences_y ;
if(flag==1)fences_y = fencesEnter_y;
if(flag==3)fences_y = fencesExit_y;
int car_h = car_height;
for( int i=car_y; i+car_h<fences_y-CHECK_LENGTH; ++i)
{
emit update(carnumber,car_x,i,car_width,car_height);
sleep(10);
car_y=i;
}
emit Fence_Update(flag);
}
//转弯
void Car::wheel()
{
if(car_x > park_x)
{
emit trans_update(carnumber,90);
int tem=car_height;
car_height = car_width;
car_width = tem;
for(int i=car_x; i>park_x-INSTANCE; --i)
{
emit update(carnumber,i,car_y,car_width,car_height);
car_x=i;
sleep(10);
}
}else
{
emit trans_update(carnumber,270);
// emit update(carnumber,car_x,car_y,car_height,car_width);
int tem=car_height;
car_height = car_width;
car_width = tem;
for(int i=car_x; i<park_x-INSTANCE; ++i)
{ car_x=i;
emit update(carnumber,i,car_y,car_width,car_height);
sleep(10);
}
}
}
void Car::stop(){
emit update(carnumber,car_x,car_y,car_width,car_height);
sleep(10);
}
//停车
void Car::stopPark()
{
if(car_y > park_y)
{
emit trans_update(carnumber,270);
// emit update(carnumber,car_x,car_y,car_height,car_width);
int tem=car_height;
car_height = car_width;
car_width = tem;
for(int i=car_y; i>park_y+CHECK_LENGTH; --i)
{car_y=i;
emit update(carnumber,car_x,i,car_width,car_height);
sleep(10);
}
}else
{
emit trans_update(carnumber,90);
// emit update(carnumber,car_x,car_y,car_height,car_width);
int tem=car_height;
car_height = car_width;
car_width = tem;
for(int i=car_y; i<park_y-CHECK_LENGTH; ++i)
{ car_y=i;
emit update(carnumber,car_x,i,car_width,car_height);
sleep(10);
}
}
}
//红外扫描车的信息
void Car::scanCar(int flag)
{ int scan_y;
if(flag==1)scan_y= scanBtn_y;
if(flag==3)scan_y = scanBtnExit_y;
if(car_y == scan_y)
{
QString carInfo =getCarlicense()+" "+getCarmoney()+" "+getCarmodel();
emit show_update(flag,carInfo);
}
}
//红外检测入口栅栏何时关闭
void Car::redEnterCheck(int flag)
{
if(flag==2 && car_y == enterRed_y)emit Fence_Update(flag);
if(flag==4 && car_y == ExitRed_y) emit Fence_Update(flag);
}
void Car::cheatPark()
{
wheel();
stopPark();
}
void Car::stopCarTime(unsigned int msec)
{
sleep(msec);
}
void Car::run()
{
FencesPower(1);
if(carnumber==1){
for(int i=car_y; i+car_height<downStreet_y; ++i)
{
emit update(carnumber,car_x,i,car_width,car_height);
car_y=i;
scanCar(1);
redEnterCheck(2);
sleep(10);
}
cheatPark(); //诱导停车
cout<<"cheatPark()"<<endl;
stopCarTime(1000); //停车时间 sleep(1000);
exitPark();
walkExit();
int i;
for( i=car_x; i<400; ++i)
{
emit update(carnumber,i,car_y,car_width,car_height);
sleep(12);
car_x=i;
}
if(eastflag==1)
{
for(i=position_x;i<1500;++i)
{
emit update(carnumber ,i,car_y,car_width,car_height);
sleep(12);
}
car_x=i;
}
if(eastflag==0 && car_x >=670)
{
for(i=car_x;i<1500;++i)
{
emit update(carnumber,i,car_y,car_width,car_height);
sleep(12);
}
car_x=i;
}
if(car_x>=1500)
{
car_x=0;
emit update(carnumber,car_x,car_y,car_width,car_height);
}}
if(carnumber==2){
for(int i=car_y; i+car_height<downStreet_y; ++i)
{
emit update(carnumber,car_x,i,car_width,car_height);
car_y=i;
scanCar(1);
redEnterCheck(2);
sleep(12);
}
cheatPark(); //诱导停车
cout<<"cheatPark()"<<endl;
stopCarTime(200); //停车时间 sleep(1000);
exitPark();
walkExit();
}
int i;
while(isRun){
if(carnumber==3){
for( int i=car_x; i<400; ++i)
{
emit update(carnumber,i,car_y,car_width,car_height);
sleep(12);
car_x=i;
}
if(eastflag==1)
{
for(i=position_x;i<1500;++i)
{
emit update(carnumber ,i,car_y,car_width,car_height);
sleep(12);
}
car_x=i;
}
if(eastflag==0 && car_x >=670)
{
for(i=car_x;i<1500;++i)
{
emit update(carnumber,i,car_y,car_width,car_height);
sleep(12);
}
car_x=i;
}
if(car_x>=1500)
{
car_x=0;
emit update(carnumber,car_x,car_y,car_width,car_height);
}}
}
}
/**********************************************************************/
//驶出停车场
void Car::exitPark()
{
if(car_y < midLine_y)//说明在上部分停车
{
for(int i=car_y; i<midLine_y; ++i)
{
emit update(carnumber,car_x,i,car_width,car_height);
car_y=i;
sleep(10);
}
}
else //下半部停车
{
for(int i=car_y; i-INSTANCE>midLine_y; --i)
{ car_y=i;
emit update(carnumber,car_x,i,car_width,car_height);
sleep(10);
}
}
walkFangxiangToExit();
}
void Car::walkExit()
{
if(dir==left)emit trans_update(carnumber,90);
if(dir == right)emit trans_update(carnumber,270);
// emit update(carnumber,car_x,car_y,car_height,car_width);
int tem=car_height;
car_height = car_width;
car_width = tem;
for(int i= car_y;i< scanBtnExit_y+CHECK_LENGTH;++i)
{
car_y=i;
scanCar(3);
cout<<"car_y="<<car_y<<endl;
emit update(carnumber,car_x,i,car_width,car_height);
sleep(10);
}
FencesPower(3);
for( int i=car_y; i+car_height<ExitRed_y+CHECK_LENGTH; ++i)
{
car_y=i;
redEnterCheck(4);
emit update(carnumber,car_x,i,car_width,car_height);
sleep(10);
}
//go ahead to the downline
for(int i = car_y;i+INSTANCE+car_height<downline_y;++i)
{
car_y=i;
emit update(carnumber,car_x,i,car_width,car_height);
sleep(10);
}
//trans 90
emit trans_update(carnumber,90);
//emit update(carnumber,car_x,car_y,car_height,car_width);
tem=car_height;
car_height = car_width;
car_width = tem;
//go ahead to the leftline
for(int i = car_x;i-INSTANCE>leftline_x;--i)
{
car_x=i;
emit update(carnumber,i,car_y,car_width,car_height);
sleep(10);
}
emit trans_update(carnumber,90);
//emit update(carnumber,car_x,car_y,car_height,car_width);
tem=car_height;
car_height = car_width;
car_width = tem;
//go ahead to the init_car_y
for(int i = car_y;i-INSTANCE>init_car_y;--i)
{
car_y=i;
emit update(carnumber,car_x,i,car_width,car_height);
sleep(10);
}
emit trans_update(carnumber,90);
//emit update(carnumber,car_x,car_y,car_height,car_width);
tem=car_height;
car_height = car_width;
car_width = tem;
//go ahead to the init_car_x
for(int i = car_x;i+car_height<init_car_x;++i)
{
car_x=i;
emit update(carnumber,i,car_y,car_width,car_height);
sleep(10);
}
cout<<"init_car_x="<<init_car_x<<" init_car_y="<<init_car_y<<endl;
cout<<"car_x="<<car_x<<" car_y="<<car_y<<endl;
stop();
}
void Car::walkFangxiangToExit()
{
//从右边走向出口
if(car_x > flagLineFive_x)
{
emit trans_update(carnumber,270);
dir=right;
//emit update(carnumber,car_x,car_y,car_height,car_width);
int tem=car_height;
car_height = car_width;
car_width = tem;
for(int i=car_x; i-INSTANCE > flagLineFive_x; --i)
{ car_x=i;
emit update(carnumber,i,car_y,car_width,car_height);
sleep(10);
}
}else //从左边走向出口
{
dir = left;
if(car_y>midLine_y) emit trans_update(carnumber,90);//下半部分停车
else emit trans_update(carnumber,270);
// emit update(carnumber,car_x,car_y,car_height,car_width);
int tem=car_height;
car_height = car_width;
car_width = tem;
for(int i=car_x; i+INSTANCE < flagLineFive_x; ++i)
{ car_x=i;
emit update(carnumber,i,car_y,car_width,car_height);
sleep(10);
}
}
}
//********************************************************************//
//*****************************************************************//
void Car::slotUpdate(int i,int x,int y, int width, int height)
{
pMain->updateposition(i,x,y,width,height);
}
void Car::FenceUpdate(int i)
{
if(i==1) pMain->FencesEnterOn();
else if(i==2)pMain->FencesEnterOff();
else if(i==3)pMain->FencesExitOn();
else if(i==4)pMain->FencesExitOff();
else i=0;
}
void Car::transUpdate(int i, unsigned int angle)
{
pMain->trans(i,angle);
}
void Car::showUpdate(int i, QString carInfo)
{
if(i==1){
string str =pMain->showToEnter(carInfo);
int idx = str.find(" ");
if(idx != -1)
{
park_x = atoi( str.substr(0,idx).c_str());
park_y = atoi( str.substr(idx+1,str.length()).c_str());
}
}
if(i==3) pMain->showToExit(carInfo);
}
2.mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "car.h"
int isRun;
int westflag;
int eastflag;
Car * thread1,*thread2,*thread3;
//车位y坐标
void MainWindow::initCarPort()
{
maplive.insert(pair<string,int>("A1",0));
maplive.insert(pair<string,int>("A2",1));
maplive.insert(pair<string,int>("A3",1));
maplive.insert(pair<string,int>("A4",0));
maplive.insert(pair<string,int>("A5",0));
maplive.insert(pair<string,int>("A6",0));
maplive.insert(pair<string,int>("A7",0));
maplive.insert(pair<string,int>("A8",0));
maplive.insert(pair<string,int>("A9",0));
maplive.insert(pair<string,int>("A10",0));
maplive.insert(pair<string,int>("A11",0));
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
initCarPort();
ui->setupUi(this);
timer2 =new QTimer(this);
carx=ui->car->x();
carx2=ui->car3->x();
timer1=new QTimer(this);
QString str= "car: autoxi\r\nmoney: 300$\r\nNumber: ERO678";
position_x = ui->car->x();
position_y = ui->car->y();
position_weight = ui->car->width();
position_height = ui->car->height();
position_x1 = ui->car2->x();
position_y1 = ui->car2->y();
position_weight1 = ui->car2->width();
position_height1 = ui->car2->height();
position_x2 = ui->car3->x();
position_y2 = ui->car3->y();
position_weight2 = ui->car3->width();
position_height2 = ui->car3->height();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::mysleep(unsigned int msec)
{
QTime newTime = QTime::currentTime().addMSecs(msec);
while(QTime::currentTime() < newTime)
QCoreApplication::processEvents(QEventLoop::AllEvents,100);
}
//将小车信息写入文件
void MainWindow::writeTofile(QString carInfo)
{
QFile f("F:\\text.txt");
if(!f.open(QIODevice::WriteOnly|QIODevice::Append))
{
qDebug()<<"Open failed.";
}else
{
QTextStream txtOutput(&f);
txtOutput<<carInfo<<endl;
}
f.close();
}
/*
读文件操作
void MainWindow::readFromFile()
{
QFile f("F:\\text.txt");
if(!f.open(QIODevice::ReadOnly|QIODevice::Text))
{
qDebug()<<"Open failed.";
}else
{
QTextStream txtInput(&f);
QString lineStr;
while(!txtInput.atEnd())
{
lineStr = txtInput.readLine();
qDebug()<<lineStr;
}
}
f.close();
}
*/
//判断从数据库中获取到的车位判断是否被占用
/*int MainWindow::judgeIsNullOfCar(QString linestr)
{
string s = linestr.toStdString();
int flagCar = s[s.length()-1]-'0';
if(0 == flagCar) return 1; //说明此车位未被占用
else return -1; //说明此车位已被占用
}
//返回从数据库中获取到的车位信息
QString MainWindow::readFromFile(QString path)
{
QFile f(path);
QString str = "fill";
if(!f.open(QIODevice::ReadOnly|QIODevice::Text))
qDebug()<<"Open failed.";
else
{
QTextStream txtInput(&f);
QString lineStr;
while(!txtInput.atEnd())
{
lineStr = txtInput.readLine();
if( judgeIsNullOfCar(lineStr) == 1 )
{
f.close();
str = lineStr;
break;
}
}
}
f.close();
return str;
}*/
//切割小车信息
void MainWindow::splitCarInfor(QString carInfo,vector<string>& v)
{
string str = carInfo.toStdString();
while(1)
{
int idx = str.find(" ");
if(idx==-1)
{
break;
}
string s = str.substr(0,idx);
v.push_back(s);
str = str.substr(idx+1,str.length());
}
}
//切割车位信息
/*void MainWindow::splitCarPort(string& str)
{
vector<string> v;
while(1)
{
int idx = str.find(" ");
if(idx==-1)
{
break;
}
string s = str.substr(0,idx);
v.push_back(s);
str = str.substr(idx+1,str.length());
}
mCarPort = v[0];
mx = atoi(v[1].c_str());
my = atoi(v[2].c_str());
}
//读小车车位信息
string MainWindow::readSqlCar()
{
QString result = readFromFile("F:\\data.txt");
return result.toStdString();
}*/
//返回true说明匹配成功
bool MainWindow::isPort(string lineStr)
{
int len = mCarPort.length();
int res = -1;
cout<<"macr:"<<mCarPort<<endl;
cout<<"str:"<<lineStr<<endl;
res = lineStr.compare(0,len,mCarPort,0,len);//返回0车位匹配,否则不匹配
if(res == 0)return true;
return false;
}
void MainWindow::getSysTime()
{
QDateTime local(QDateTime::currentDateTime());
mLocalTime = local.toString().toStdString();
/* QDate d = local.date();
cout<<d.year()<<endl;
cout<<d.month()<<endl;
cout<<d.day()<<endl;
QTime t = local.time();
cout<<t.hour()<<endl;
cout<<t.minute()<<endl;
cout<<t.second()<<endl;*/
}
//设置xy坐标
string MainWindow::getXY(string lineStr)
{
string str = "";
string s;
//cout<<"lineStr:"<<lineStr<<endl;
int index = lineStr.find(" ");
if(index != -1)
{
str = lineStr.substr(index+1,lineStr.length());
}
return str;
// int idx = str.find(" ");
// if(idx != -1)
// {
// park.mx = atoi( str.substr(0,idx).c_str() );
// park.my = atoi( str.substr(idx+1,str.length()).c_str() );
// }
}
//从文件中读取对应车位的位置
string MainWindow::readPositionFromFile(QString path)
{
QFile f(path);
string pos_str="";
bool res = false;
if(!f.open(QIODevice::ReadOnly|QIODevice::Text))
qDebug()<<"Open failed.";
else
{
QTextStream txtInput(&f);
QString lineStr;
while(!txtInput.atEnd())
{
lineStr = txtInput.readLine();
//cout<<"lineStr:"<<lineStr.toStdString()<<endl;
res = isPort(lineStr.toStdString());//对比车位是否一致
//cout<<"res"<<res<<endl;
if(res)
{
pos_str = getXY(lineStr.toStdString());//提取并设置xy
break;
}
}
}
f.close();
return pos_str;
}
//判断map中小车的标志位是否被占用,并设置mCarPort满返回fill
string MainWindow::judgeCarPortFlag()
{
map<string,int>::iterator ite;
string res = "fill";
for(ite = maplive.begin();ite != maplive.end();++ite)
{
if(ite->second == 0)
{
ite->second = 1;
mCarPort = ite->first;
return ite->first;
}
}
return res;
}
string MainWindow::setWork()
{
string res = judgeCarPortFlag();
if(res.compare("fill") == 0)return res;
string pos = readPositionFromFile("data.txt");
return pos;
}
//入口显示,分配停车位,未满则显示车位,满了则无法停车。。。。
string MainWindow::showToEnter(QString carInfo)
{
vector<string> carSpliteInfo; //小车的信息划分存储
splitCarInfor(carInfo,carSpliteInfo);
getSysTime();
vector<string> v;
v.push_back("card:");
v.push_back("money:");
v.push_back("carPort:");
v.push_back("time:");
string s; //
string sub;
string str = setWork(); //失败返回fill,成功返回车位
if(str.compare("fill") == 0) carSpliteInfo.push_back("fill");
else
{
//splitCarPort(str);
carSpliteInfo.push_back(mCarPort); //将划分得到的车位加入
carSpliteInfo.push_back(mLocalTime);
}
for(int i=0;i<carSpliteInfo.size();++i)
{
cout<<carSpliteInfo[i]<<" ";
sub = v[i]+carSpliteInfo[i]+'\n';
s += sub;
}
ui->enterShowLabel->setText(s.c_str());
return str;
}
//入口显示,分配停车位,未满则显示车位,满了则无法停车。。。。
void MainWindow::showToExit(QString carInfo)
{
vector<string> carSpliteInfo; //小车的信息划分存储
splitCarInfor(carInfo,carSpliteInfo);
getSysTime();
vector<string> v;
v.push_back("车牌:");
v.push_back("money:");
v.push_back("brand:");
v.push_back("carPort:");
v.push_back("time:");
string s; //
string sub;
{
//splitCarPort(str);
carSpliteInfo.push_back(mCarPort); //将划分得到的车位加入
carSpliteInfo.push_back(mLocalTime);
}
for(int i=0;i<carSpliteInfo.size();++i)
{
cout<<carSpliteInfo[i]<<" ";
sub = v[i]+carSpliteInfo[i]+'\n';
s += sub;
cout<<"s==="<<s<<endl;
}
ui->exitShowLable->setText(s.c_str());
}
//红外检测入口栅栏打开
void MainWindow::FencesEnterOn()
{
int distanceInEnterDoor = ui->enterDoorTwo->x()-ui->enterDoorOne->x();
ui->fencesEnter->setGeometry(ui->fencesEnter->x(),
ui->fencesEnter->y(),
ui->fencesEnter->width()-distanceInEnterDoor,
ui->fencesEnter->height());
}
//红外检测入口栅栏关闭
void MainWindow::FencesEnterOff()
{
int distanceInEnterDoor = ui->enterDoorTwo->x()-ui->enterDoorOne->x();
ui->fencesEnter->setGeometry(ui->fencesEnter->x(),
ui->fencesEnter->y(),
ui->fencesEnter->width()+distanceInEnterDoor,
ui->fencesEnter->height());
}
//红外检测出口栅栏打开
void MainWindow::FencesExitOn()
{
int distanceInExitDoor = ui->exitDoorTwo->x()-ui->exitDoorOne->x();
ui->fencesExit->setGeometry(ui->fencesExit->x(),
ui->fencesExit->y(),
ui->fencesExit->width()-distanceInExitDoor,
ui->fencesExit->height());
}
//红外检测出口栅栏关闭
void MainWindow::FencesExitOff()
{
int distanceInExitDoor = ui->exitDoorTwo->x()-ui->exitDoorOne->x();
ui->fencesExit->setGeometry(ui->fencesExit->x(),
ui->fencesExit->y(),
ui->fencesExit->width()+distanceInExitDoor,
ui->fencesExit->height());
}
/*void MainWindow::setParkFlag()
{
maplive.find(mCarPort);
//fstream file("test.txt",ios::in|ios::out);
string buff;
ifstream infile;//=new ifstream(file);
infile.open("F:\\data.txt",ios::in|ios::out);
int offset=0;
int index=-1;
int count=0;
while(getline(infile,buff))
{
cout<<"count:"<<count<<endl;
index=buff.find_first_of(" ",offset);
string str=buff.substr(offset,index-offset);
count+=buff.length()+1;
cout<<"length :"<<buff.length();
cout<<"str:"<<str<<endl;
if(str==mCarPort)
{
//count-=1;
cout<<"count:"<<count<<endl;
writeDataFile(count);//she zhi sign;
break;
}
}
}*/
//*********************************************************
//********************************************************
void MainWindow::trans(int i,unsigned int angle)
{
if(i==2){
QPixmap pixmap1(":/new/prefix1/picture/picture/bus.jpg");
matrix1.rotate(angle);
ui->car2->setPixmap(pixmap1.transformed(matrix1,Qt::SmoothTransformation));
ui->car2->setGeometry(ui->car2->x(),ui->car2->y(),ui->car2->height(),ui->car2->width());}
if(i==1){
QPixmap pixmap(":/new/prefix1/picture/picture/you.png");
matrix.rotate(angle);
ui->car->setPixmap(pixmap.transformed(matrix,Qt::SmoothTransformation));
ui->car->setGeometry(ui->car->x(),ui->car->y(),ui->car->height(),ui->car->width());}
if(i==3){
QPixmap pixmap3(":/new/prefix1/picture/picture/you.png");
matrix3.rotate(angle);
ui->car3->setPixmap(pixmap3.transformed(matrix3,Qt::SmoothTransformation));
ui->car3->setGeometry(ui->car3->x(),ui->car3->y(),ui->car3->height(),ui->car3->width());}
}
void MainWindow::run()
{
// getSysTime();
thread1=new Car(this,1,position_x,position_y,position_weight,position_height);
setCarInfo(1);
thread1->start();
thread2=new Car(this,2,position_x1,position_y1,position_weight1,position_height1);
setCarInfo(2);
thread2->start();
thread3=new Car(this,3,position_x2,position_y2,position_weight2,position_height2);
thread3->start();
}
void MainWindow::setCarInfo(int num)
{
if(num==1)thread1->setCarInfo("陕A14785","50","奥迪");
if(num==2)thread2->setCarInfo("陕A25896","100","宾利");
}
void MainWindow::updateposition(int i,int x,int y,int width, int height)
{
if(i==1) ui->car->setGeometry(x,y,width,height);
if(i==2) ui->car2->setGeometry(x,y,width,height);
if(i==3) ui->car3->setGeometry(x,y,width,height);
}
void MainWindow::on_pushButton_clicked()
{
QPixmap pixmap(":/new/prefix1/picture/picture/you.png");
ui->car->setPixmap(pixmap);
QPixmap pixmap1(":/new/prefix1/picture/picture/bus.jpg");
ui->car2->setPixmap(pixmap1);
QPixmap pixmap3(":/new/prefix1/picture/picture/you.png");
ui->car3->setPixmap(pixmap3);
run();
wheel();
settimer1 = 14;
settimer2 = -1;
ui->lcdNumber->setDigitCount(2);
ui->lcdNumber->setMode(QLCDNumber::Dec);
ui->lcdNumber->setSegmentStyle(QLCDNumber::Filled);
ui->lcdNumber->display(settimer1);
ui->lcdNumber_2->setDigitCount(2);
ui->lcdNumber_2->setMode(QLCDNumber::Dec);
ui->lcdNumber_2->setSegmentStyle(QLCDNumber::Filled);
ui->lcdNumber_2->display(settimer1);
westflag=0;
eastflag=0;
timer = new QTimer();
timer->setInterval(1000);
timer->start();
isRun=1;
connect(timer,SIGNAL(timeout()),this, SLOT(onTimerOut()));
timer2->start(5);
timer1->start(5);
}
void MainWindow::updatelight()
{
settimer2=14;
settimer1=-1;
ui->lcdNumber->display(settimer2);
ui->lcdNumber_2->display(settimer2);
westflag=0;
eastflag=0;
}
void MainWindow::lightOn(QLabel *label)
{
QImage image(":/new/prefix1/picture/picture/liang.jpg");
label->setPixmap(QPixmap::fromImage(image));
label->setGeometry(label->x(),label->y(),label->width(),label->height());
label->show();
}
void MainWindow::lightOff(QLabel *label)
{
QImage image(":/new/prefix1/picture/picture/an.jpg");
label->setPixmap(QPixmap::fromImage(image));
label->setGeometry(label->x(),label->y(),label->width(),label->height());
label->show();
}
void MainWindow::onTimerOut()
{
if(settimer2>0)
{
eastflag=0;
westflag=0;
settimer2--;
ui->red->show();
ui->yellow->hide();
ui->green->hide();
ui->lcdNumber->display(settimer2);
ui->red_2->show();
ui->yellow_2->hide();
ui->green_2->hide();
ui->lcdNumber_2->display(settimer2);
}
if(settimer2==0)
{
eastflag=0;
westflag=0;
ui->yellow->show();
ui->red->hide();
ui->yellow_2->show();
ui->red_2->hide();
mysleep(1000);
ui->green->show();
ui->yellow->hide();
ui->green_2->show();
ui->yellow_2->hide();
settimer1 = 14;
settimer2=-1;
ui->lcdNumber->display(settimer1);
ui->lcdNumber_2->display(settimer1);
}
if(settimer1>0)
{
eastflag=1;
westflag=1;
settimer1--;
//east
ui->red->hide();
ui->yellow->hide();
ui->green->show();
ui->lcdNumber->display(settimer1);
ui->red_2->hide();
ui->yellow_2->hide();
ui->green_2->show();
ui->lcdNumber_2->display(settimer1);
}
if(settimer1==0)
{
eastflag=0;
westflag=0;
ui->yellow->show();
ui->green->hide();
ui->yellow_2->show();
ui->green_2->hide();
mysleep(1000);
ui->red->show();
ui->yellow->hide();
ui->red_2->show();
ui->yellow_2->hide();
settimer2 = 14;
settimer1=-1;
ui->lcdNumber->display(settimer2);
ui->lcdNumber_2->display(settimer2);
}
}
三、运行结果