search-demo 三.拖拽起始点
完成目标:让其实顶点可以随着鼠标拖动而改变位置
途中思路不清,忘记乘以BLOCKSIZE却无法察觉。
鼠标键按住不放,之后拖动,如果超出主界面会导致程序崩溃。检查之后发现,没有进行坐标范围限定,加入判定函数即ok。
main:
C++语言: Codee#26079
01 #include <QtGui>
02 #include <QtCore>
03 #include "pathfinding.h"
04
05 int main(int argc, char *argv[])
06 {
07 QApplication a(argc, argv);
08 Widget* window=new Widget;
09
10 QRect frect = window->frameGeometry();
11 frect.moveCenter(QDesktopWidget().availableGeometry().center());
12 window->move(frect.topLeft());
13 window->show();
14
15 return a.exec();
16 }
02 #include <QtCore>
03 #include "pathfinding.h"
04
05 int main(int argc, char *argv[])
06 {
07 QApplication a(argc, argv);
08 Widget* window=new Widget;
09
10 QRect frect = window->frameGeometry();
11 frect.moveCenter(QDesktopWidget().availableGeometry().center());
12 window->move(frect.topLeft());
13 window->show();
14
15 return a.exec();
16 }
pathfinding.h
C++语言: Codee#26080
01 #ifndef PATHFINDING_H
02 #define PATHFINDING_H
03
04 #include <QtGui>
05 #include <QtCore>
06
07 class Widget : public QWidget
08 {
09 Q_OBJECT
10
11 public:
12 Widget(QWidget* parent = 0);
13 void paintEvent(QPaintEvent* event);
14 void mousePressEvent(QMouseEvent* event);
15 void mouseMoveEvent(QMouseEvent* event);
16 void setWindowPixel(const QPoint& pos, bool opaque);
17
18 private:
19 enum {LENGTH = 910};
20 enum {WIDTH = 700};
21 enum {BLOCKSIZE = 35};
22 bool blocks[LENGTH/BLOCKSIZE][WIDTH/BLOCKSIZE];
23 bool onDrag;
24 QPoint cursorpos;
25 QPoint startBlock;
26 QPoint endBlock;
27 bool checkPosition(const QPoint& pos);
28 };
29
30 #endif // PATHFINDING_H
02 #define PATHFINDING_H
03
04 #include <QtGui>
05 #include <QtCore>
06
07 class Widget : public QWidget
08 {
09 Q_OBJECT
10
11 public:
12 Widget(QWidget* parent = 0);
13 void paintEvent(QPaintEvent* event);
14 void mousePressEvent(QMouseEvent* event);
15 void mouseMoveEvent(QMouseEvent* event);
16 void setWindowPixel(const QPoint& pos, bool opaque);
17
18 private:
19 enum {LENGTH = 910};
20 enum {WIDTH = 700};
21 enum {BLOCKSIZE = 35};
22 bool blocks[LENGTH/BLOCKSIZE][WIDTH/BLOCKSIZE];
23 bool onDrag;
24 QPoint cursorpos;
25 QPoint startBlock;
26 QPoint endBlock;
27 bool checkPosition(const QPoint& pos);
28 };
29
30 #endif // PATHFINDING_H
pathfinding.cpp
C++语言: Codee#26081
001 #include "pathfinding.h"
002
003 Widget::Widget(QWidget* parent)
004 : QWidget(parent)
005 {
006 for (int i = 0; i < LENGTH / BLOCKSIZE; ++i)
007 for (int j = 0; j < WIDTH / BLOCKSIZE; ++j)
008 blocks[i][j] = true;
009 startBlock = QPoint(0, 0);
010 blocks[0][0] = false;
011
012 endBlock = QPoint(10, 10);
013 blocks[10][10] = false;
014 onDrag = false;
015
016 setAttribute(Qt::WA_StaticContents);
017 setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
018 this->setMouseTracking(true);
019 this->setFixedSize(LENGTH, WIDTH);
020 }
021
022 void Widget::paintEvent(QPaintEvent* event)
023 {
024 QPainter painter(this);
025 painter.setOpacity(0.5);
026 QColor color = Qt::red;
027
028 /* draw the start block */
029 /*
030 painter.fillRect(QRect(BLOCKSIZE*startBlock.x(), BLOCKSIZE*startBlock.y(),
031 BLOCKSIZE, BLOCKSIZE), color);
032 */
033
034 //qDebug("The startblock pos:%d,%d", startBlock.x(), startBlock.y());
035 /* draw the simple blocks */
036 for (int i = 0; i < LENGTH / BLOCKSIZE; ++i)
037 for (int j = 0; j < WIDTH / BLOCKSIZE; ++j)
038 {
039 QPoint cur(i, j);
040 if (cur == startBlock)
041 color = Qt::red;
042 else if (cur == endBlock)
043 color = Qt::green;
044
045 else if (!blocks[i][j])
046 color = Qt::gray;
047 else
048 color = Qt::white;
049 painter.fillRect(QRect(BLOCKSIZE * i, BLOCKSIZE * j,
050 BLOCKSIZE, BLOCKSIZE), color);
051 }
052
053 painter.setPen(QPen(Qt::gray));
054 /* draw the grids */
055 for (int i = 0; i < LENGTH; i += BLOCKSIZE)
056 painter.drawLine(i, 0,
057 i, WIDTH);
058 for (int j = 0; j <= WIDTH; j += BLOCKSIZE)
059 painter.drawLine(0, j,
060 LENGTH, j);
061
062 painter.setFont(QFont("Arial", 50));
063 painter.drawText(rect(), Qt::AlignCenter, "path-finding demo");
064 }
065
066 /* 1. check the cursor's position,if on the start blocks,go to 2,
067 * else go to 3;
068 * 2. drag the start block,set onDrag=true
069 * 3. just draw the current block
070 */
071 void Widget::mousePressEvent(QMouseEvent* event)
072 {
073 cursorpos = QPoint(event->pos().x() / BLOCKSIZE,
074 event->pos().y() / BLOCKSIZE);
075 if (cursorpos == startBlock)
076 onDrag = true;
077 else
078 {
079 setWindowPixel(event->pos(), false);
080 onDrag = false;
081 }
082 }
083
084 void Widget::mouseMoveEvent(QMouseEvent* event)
085 {
086 /*
087 * check the cursor's position
088 * if out of the main window
089 * the application will faile
090 */
091 if (checkPosition(event->pos()))
092 {
093 QPoint tmppos = QPoint(event->pos().x() / BLOCKSIZE,
094 event->pos().y() / BLOCKSIZE);
095 if (cursorpos != tmppos &&
096 (event->buttons() & Qt::LeftButton) ||
097 (event->buttons() & Qt::RightButton))
098 // event->buttons() & Qt::RightButton)
099 {
100 //qDebug("Time elapsed: %d ms", time.msecsTo(curTime));
101 //setWindowPixel(event->pos(), true);
102 /* is on drag ,use this to change the start
103 * block's position,clear the old pos
104 * then take the new pos
105 */
106 if (onDrag)
107 {
108 //qDebug("tmppos:%d,%d", tmppos.x(), tmppos.y());
109 blocks[startBlock.x()][startBlock.y()] = true;
110 startBlock = tmppos;
111 blocks[tmppos.x()][tmppos.y()] = false;
112 }
113 setWindowPixel(event->pos(), onDrag);
114 }
115 }
116 //cursorpos=tmppos;
117 }
118
119 void Widget::setWindowPixel(const QPoint& pos, bool opaque)
120 {
121 int x = pos.x() / BLOCKSIZE;
122 int y = pos.y() / BLOCKSIZE;
123 cursorpos = QPoint(x, y);
124
125 if (!opaque)
126 blocks[x][y] = !blocks[x][y];
127 update();
128 }
129
130 bool Widget::checkPosition(const QPoint& pos)
131 {
132 QRect rect(0, 0, LENGTH, WIDTH);
133 return rect.contains(pos);
134 }
002
003 Widget::Widget(QWidget* parent)
004 : QWidget(parent)
005 {
006 for (int i = 0; i < LENGTH / BLOCKSIZE; ++i)
007 for (int j = 0; j < WIDTH / BLOCKSIZE; ++j)
008 blocks[i][j] = true;
009 startBlock = QPoint(0, 0);
010 blocks[0][0] = false;
011
012 endBlock = QPoint(10, 10);
013 blocks[10][10] = false;
014 onDrag = false;
015
016 setAttribute(Qt::WA_StaticContents);
017 setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
018 this->setMouseTracking(true);
019 this->setFixedSize(LENGTH, WIDTH);
020 }
021
022 void Widget::paintEvent(QPaintEvent* event)
023 {
024 QPainter painter(this);
025 painter.setOpacity(0.5);
026 QColor color = Qt::red;
027
028 /* draw the start block */
029 /*
030 painter.fillRect(QRect(BLOCKSIZE*startBlock.x(), BLOCKSIZE*startBlock.y(),
031 BLOCKSIZE, BLOCKSIZE), color);
032 */
033
034 //qDebug("The startblock pos:%d,%d", startBlock.x(), startBlock.y());
035 /* draw the simple blocks */
036 for (int i = 0; i < LENGTH / BLOCKSIZE; ++i)
037 for (int j = 0; j < WIDTH / BLOCKSIZE; ++j)
038 {
039 QPoint cur(i, j);
040 if (cur == startBlock)
041 color = Qt::red;
042 else if (cur == endBlock)
043 color = Qt::green;
044
045 else if (!blocks[i][j])
046 color = Qt::gray;
047 else
048 color = Qt::white;
049 painter.fillRect(QRect(BLOCKSIZE * i, BLOCKSIZE * j,
050 BLOCKSIZE, BLOCKSIZE), color);
051 }
052
053 painter.setPen(QPen(Qt::gray));
054 /* draw the grids */
055 for (int i = 0; i < LENGTH; i += BLOCKSIZE)
056 painter.drawLine(i, 0,
057 i, WIDTH);
058 for (int j = 0; j <= WIDTH; j += BLOCKSIZE)
059 painter.drawLine(0, j,
060 LENGTH, j);
061
062 painter.setFont(QFont("Arial", 50));
063 painter.drawText(rect(), Qt::AlignCenter, "path-finding demo");
064 }
065
066 /* 1. check the cursor's position,if on the start blocks,go to 2,
067 * else go to 3;
068 * 2. drag the start block,set onDrag=true
069 * 3. just draw the current block
070 */
071 void Widget::mousePressEvent(QMouseEvent* event)
072 {
073 cursorpos = QPoint(event->pos().x() / BLOCKSIZE,
074 event->pos().y() / BLOCKSIZE);
075 if (cursorpos == startBlock)
076 onDrag = true;
077 else
078 {
079 setWindowPixel(event->pos(), false);
080 onDrag = false;
081 }
082 }
083
084 void Widget::mouseMoveEvent(QMouseEvent* event)
085 {
086 /*
087 * check the cursor's position
088 * if out of the main window
089 * the application will faile
090 */
091 if (checkPosition(event->pos()))
092 {
093 QPoint tmppos = QPoint(event->pos().x() / BLOCKSIZE,
094 event->pos().y() / BLOCKSIZE);
095 if (cursorpos != tmppos &&
096 (event->buttons() & Qt::LeftButton) ||
097 (event->buttons() & Qt::RightButton))
098 // event->buttons() & Qt::RightButton)
099 {
100 //qDebug("Time elapsed: %d ms", time.msecsTo(curTime));
101 //setWindowPixel(event->pos(), true);
102 /* is on drag ,use this to change the start
103 * block's position,clear the old pos
104 * then take the new pos
105 */
106 if (onDrag)
107 {
108 //qDebug("tmppos:%d,%d", tmppos.x(), tmppos.y());
109 blocks[startBlock.x()][startBlock.y()] = true;
110 startBlock = tmppos;
111 blocks[tmppos.x()][tmppos.y()] = false;
112 }
113 setWindowPixel(event->pos(), onDrag);
114 }
115 }
116 //cursorpos=tmppos;
117 }
118
119 void Widget::setWindowPixel(const QPoint& pos, bool opaque)
120 {
121 int x = pos.x() / BLOCKSIZE;
122 int y = pos.y() / BLOCKSIZE;
123 cursorpos = QPoint(x, y);
124
125 if (!opaque)
126 blocks[x][y] = !blocks[x][y];
127 update();
128 }
129
130 bool Widget::checkPosition(const QPoint& pos)
131 {
132 QRect rect(0, 0, LENGTH, WIDTH);
133 return rect.contains(pos);
134 }