界面部分基本完成。程序的架构实在是恶心到不行,明天从头再来。
main.cpp:
C++语言: Codee#2609401#include
02#include
03#include "pathfinding.h"
04
05intmain(intargc,charargv[])
06{
07QApplicationa(argc,argv);
08Widgetwindow=newWidget;
09
10QRectfrect=window->frameGeometry();
11frect.moveCenter(QDesktopWidget().availableGeometry().center());
12window->move(frect.topLeft());
13window->show();
14
15returna.exec();
16}
path-finding.h:
C++语言: Codee#2609501#ifndef PATHFINDING_H
02#define PATHFINDING_H
03
04#include
05#include
06
07#include "control.h"
08
09classWidget:publicQWidget
10{
11Q_OBJECT
12
13public:
14Widget(QWidgetparent=0);
15voidpaintEvent(QPaintEventevent);
16voidmousePressEvent(QMouseEventevent);
17voidmouseMoveEvent(QMouseEventevent);
18voidkeyPressEvent(QKeyEventevent);
19voidsetWindowPixel(constQPoint&pos,boolopaque);
20
21private:
22enum{LENGTH=910};
23enum{WIDTH=700};
24enum{BLOCKSIZE=35};
25enum{MES_Y_OFFSET=16};
26
27boolblocks[LENGTH/BLOCKSIZE][WIDTH/BLOCKSIZE];
28boolonDrag;
29QPointhelpImagePos;
30QPointhelpMessagePos;
31QPointcontrolImagePos;
32QPointcontrolMessagePos;
33QPointcursorpos;
34QPointstartBlock;
35QPointendBlock;
36ControlcontrolMessage[5];
37boolcheckPosition(constQPoint&pos);
38intcontrolSelection;
39intspeed;
40};
41
42#endif// PATHFINDING_H
path-finding.cpp:
C++语言: Codee#26096001#include "pathfinding.h"
002
003Widget::Widget(QWidgetparent)
004:QWidget(parent)
005{
006for(inti=0;i<LENGTH/BLOCKSIZE;++i)
007for(intj=0;j<WIDTH/BLOCKSIZE;++j)
008blocks[i][j]=true;
009startBlock=QPoint(0,0);
010blocks[0][0]=false;
011
012endBlock=QPoint(10,10);
013blocks[10][10]=false;
014onDrag=false;
015
016controlMessage[0]=Control(QString("Dijkstra"),
017QColor(Qt::darkGreen));
018controlMessage[1]=Control(QString("Bi-Directional BFS"),
019QColor(Qt::darkGreen));
020
021
022helpImagePos=QPoint(5,5);
023helpMessagePos=QPoint(10,10);
024controlImagePos=QPoint(790,5);
025controlMessagePos=QPoint(795,10);
026controlSelection=0;
027speed=1;
028
029setAttribute(Qt::WA_StaticContents);
030setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum);
031this->setMouseTracking(true);
032this->setFixedSize(LENGTH,WIDTH);
033
034/
035QImage image=new QImage;
036image->load(":/image/layer2.png");
037
038QImage fixedImage(image->size(), QImage::Format_ARGB32_Premultiplied);
039QPainter imagePainter(&fixedImage);
040imagePainter.setCompositionMode(QPainter::CompositionMode_Source);
041imagePainter.fillRect(fixedImage.rect(), Qt::transparent);
042imagePainter.setCompositionMode(QPainter::CompositionMode_SourceOver);
043imagePainter.drawImage(QPoint(10,10), image);
044imagePainter.end();
045/
046
047//QImage pic(":/image/test.jpg");
048/
049I try to use QLabel to show the help message
050but failed,picture and text can't show at the
051same time
052label->setPixmap(QPixmap::convertFromImage(pic));
053/
054/
055button->setText(controlMessage[0].GetString() + "n" +
056controlMessage[1].GetString());
057label->setPixmap(QPixmap::fromImage(pic));
058label->show();
059/
060/
061QHBoxLayout layout = new QHBoxLayout;
062layout->addWidget(label);
063this->setLayout(layout);
064/
065}
066
067voidWidget::paintEvent(QPaintEventevent)
068{
069QPainterpainter(this);
070painter.setOpacity(0.8);
071QColorcolor=Qt::red;
072
073/ draw the start block /
074/
075painter.fillRect(QRect(BLOCKSIZEstartBlock.x(), BLOCKSIZEstartBlock.y(),
076BLOCKSIZE, BLOCKSIZE), color);
077/
078
079//qDebug("The startblock pos:%d,%d", startBlock.x(), startBlock.y());
080/ draw the simple blocks /
081for(inti=0;i<LENGTH/BLOCKSIZE;++i)
082for(intj=0;j<WIDTH/BLOCKSIZE;++j)
083{
084QPointcur(i,j);
085if(cur==startBlock)
086color=Qt::red;
087elseif(cur==endBlock)
088color=Qt::green;
089
090elseif(!blocks[i][j])
091color=Qt::gray;
092else
093color=Qt::white;
094painter.fillRect(QRect(BLOCKSIZEi,BLOCKSIZEj,
095BLOCKSIZE,BLOCKSIZE),color);
096}
097
098painter.setPen(QPen(Qt::gray));
099/ draw the grids /
100for(inti=0;i<LENGTH;i+=BLOCKSIZE)
101painter.drawLine(i,0,
102i,WIDTH);
103for(intj=0;j<=WIDTH;j+=BLOCKSIZE)
104painter.drawLine(0,j,
105LENGTH,j);
106
107painter.setFont(QFont("Arial",50));
108painter.drawText(rect(),Qt::AlignCenter,"path-finding demo");
109
110QImageimage=newQImage;
111image->load(":/image/layer1.png");
112/
113qDebug("imagesize:t%d,%d", image->size().width(),
114image->size().height());
115/
116image=image->scaled(330,100);
117
118
119//QImage fixedImage(image->size(), QImage::Format_ARGB32_Premultiplied);
120
121QPainterimagePainter(this);
122//imagePainter.setCompositionMode(QPainter::CompositionMode_Source);
123imagePainter.setCompositionMode(QPainter::CompositionMode_SourceOver);
124imagePainter.drawImage(helpImagePos,image);
125
126
127QPaintertextPainter(this);
128QFontfont("Arial",8);
129font.setWeight(QFont::Bold);
130//font.setStretch(1);
131QStringstring[]=
132{
133"Drag Green and Red block to set source and target",
134"Hold down mouse button to draw maze",
135"Press to select algorithm"
136"Press and
137"Press
138"Press
139};
140
141textPainter.setFont(font);
142textPainter.setPen(QPen(Qt::white));
143for(inti=0;i<6;++i)
144textPainter.drawText(10,20+MES_Y_OFFSETi,string[i]);
145
146QStringcontrolstr[]=
147{
148"A (Manhattan)",
149"A (Euclidean)",
150"A (Chebyshev)",
151"Dijkstra",
152"Bi-Directional BFS",
153};
154image->load(":/image/layer1.png");
155imagePainter.drawImage(controlImagePos,image);
156for(inti=0;i<5;++i)
157{
158if(i==controlSelection)
159textPainter.setPen(QPen(Qt::red));
160else
161textPainter.setPen(QPen(Qt::white));
162textPainter.drawText(795,20+MES_Y_OFFSETi,controlstr[i]);
163}
164textPainter.drawText(795,20+MES_Y_OFFSET5,
165QString("Speed:")+QString::number(8speed)+QString("X"));
166
167
168/
169qDebug("helpimagepos:%d,%dnmes:%d,%d",helpImagePos.x(),
170helpImagePos.y(),
171helpMessagePos.x(),
172helpMessagePos.y());
173/
174/
175painter.drawText(rect(),Qt::AlignRight &Qt::AlignTop,
176controlMessage[1].GetString());
177/
178
179
180}
181
182/ 1. check the cursor's position,if on the start blocks,go to 2,
183 else go to 3;
184 2. drag the start block,set onDrag=true
185 3. just draw the current block
186/
187voidWidget::mousePressEvent(QMouseEventevent)
188{
189cursorpos=QPoint(event->pos().x()/BLOCKSIZE,
190event->pos().y()/BLOCKSIZE);
191if(cursorpos==startBlock)
192onDrag=true;
193else
194{
195setWindowPixel(event->pos(),false);
196onDrag=false;
197}
198}
199
200voidWidget::mouseMoveEvent(QMouseEventevent)
201{
202/
203 check the cursor's position
204 if out of the main window
205 the application will faile
206/
207if(checkPosition(event->pos()))
208{
209QPointtmppos=QPoint(event->pos().x()/BLOCKSIZE,
210event->pos().y()/BLOCKSIZE);
211if(cursorpos!=tmppos&&
212(event->buttons()&Qt::LeftButton) ||
213(event->buttons()&Qt::RightButton))
214// event->buttons() & Qt::RightButton)
215{
216//qDebug("Time elapsed: %d ms", time.msecsTo(curTime));
217//setWindowPixel(event->pos(), true);
218/ is on drag ,use this to change the start
219 block's position,clear the old pos
220 then take the new pos
221/
222if(onDrag)
223{
224//qDebug("tmppos:%d,%d", tmppos.x(), tmppos.y());
225blocks[startBlock.x()][startBlock.y()]=true;
226startBlock=tmppos;
227blocks[tmppos.x()][tmppos.y()]=false;
228}
229setWindowPixel(event->pos(),onDrag);
230}
231}
232//cursorpos=tmppos;
233}
234
235voidWidget::setWindowPixel(constQPoint&pos,boolopaque)
236{
237intx=pos.x()/BLOCKSIZE;
238inty=pos.y()/BLOCKSIZE;
239cursorpos=QPoint(x,y);
240
241if(!opaque)
242blocks[x][y]=!blocks[x][y];
243update();
244}
245
246boolWidget::checkPosition(constQPoint&pos)
247{
248QRectrect(0,0,LENGTH,WIDTH);
249returnrect.contains(pos);
250}
251
252voidWidget::keyPressEvent(QKeyEventevent)
253{
254intkeyPressed=toupper(event->key());
255switch(keyPressed)
256{
257case'W':
258if(--controlSelection<0)
259controlSelection=4;
260break;
261case'S':
262if(++controlSelection>4)
263controlSelection=0;
264break;
265case'A':
266if(--speed<0)
267speed=0;
268break;
269case'D':
270if(++speed>5)
271speed=5;
272break;
273case'R':
274for(inti=0;i<LENGTH/BLOCKSIZE;++i)
275for(intj=0;j<WIDTH/BLOCKSIZE;++j)
276blocks[i][j]=true;
277blocks[startBlock.x()][startBlock.y()]=false;
278blocks[endBlock.x()][endBlock.y()]=false;
279break;
280case' ':
281break;
282default:
283return;
284
285}
286
287if(keyPressed=='W'||keyPressed=='S'||
288keyPressed=='A'||keyPressed=='D'||
289keyPressed=='R'||keyPressed==' ')
290{
291qDebug("keypressed: %c;sel: %d",keyPressed,controlSelection);
292repaint();
293}
294}原文链接: https://www.cnblogs.com/invisible/archive/2012/04/16/2452103.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/47636
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!