The Following User Says Thank You to harriva For This Useful Post: | ||
|
2010-06-15
, 13:08
|
Posts: 341 |
Thanked: 57 times |
Joined on Nov 2009
|
#12
|
class Canvas : public QWidget { Q_OBJECT public: Canvas(QWidget *parent = 0); protected: void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event); void mouseDoubleClickEvent(QMouseEvent *); void paintEvent(QPaintEvent *event); private: void drawLineTo(const QPoint &endPoint); QImage image; //this is the image on which drawing is done QPoint lastPoint; QVector<QPoint> qPoints; //MainWindow *mainWindow; int minX; int minY; int maxX; int maxY; void connectingPointsCalculator3( QVector<QPoint>& inputqPoints, int x0, int y0, int x1, int y1); };
void Canvas::connectingPointsCalculator3( QVector<QPoint>& inputqPoints, int x0, int y0, int x1, int y1) { inputqPoints.clear(); bool steep; int i=2; if( qAbs(y1-y0)>qAbs(x1-x0)) { steep = true; } if(steep==true) { swap(x0,y0); swap(x1,y1); } if(x0>x1) { swap(x0,x1); swap(y0,y1); } int deltax = x1-x0; int deltay = qAbs(y1-y0); int error = deltax/2; int ystep=0; int y=y0; if(y0<y1) { ystep=1; } else { ystep=-1; } inputqPoints.resize(1); if(steep==true) { inputqPoints.insert(0,QPoint(y0-5,x0-5)); inputqPoints.insert(1,QPoint(y0+5,x0+5)); } else { inputqPoints.insert(0,QPoint(x0-5,y0-5)); inputqPoints.insert(1,QPoint(x0+5,y0+5)); } bool switchedLanes = false; int x= x0; while(x<(x1+1)) { if(switchedLanes==false) { error = error - deltay; if(error<0) { y=y+ystep; error = error + deltax; switchedLanes = true; } } else { switchedLanes=false; } if(steep==true) { inputqPoints.insert(i,QPoint(y-5,x-5)); inputqPoints.insert(i+1,QPoint(y+5,x+5)); if((y-5)<minX) { minX=y-5; } if((x-5)<minY) { minY=x-5; } if((y+5)>maxX) { maxX=y+5; } if((x+5)>maxY) { maxY=x+5; } } else { inputqPoints.insert(i,QPoint(x-5,y-5)); inputqPoints.insert(i+1,QPoint(x+5,y+5)); if((x-5)<minX) { minX=x-5; } if((y-5)<minY) { minY=y-5; } if((x+5)>maxX) { maxX=x+5; } if((y+5)>maxY) { maxY=y+5; } } if(switchedLanes==false) { x++; } i++; i++; } }
void Canvas::drawLineTo(const QPoint &endPoint) { QPainter painter(&image); painter.setPen(QPen(Qt::black,1,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin)); qPoints.clear(); minX =999999; minY =999999; maxX =-999999; maxY =-999999; painter.setPen(Qt::blue); painter.drawEllipse(lastPoint,8,8); painter.setPen(Qt::red); painter.drawEllipse(endPoint,4,4); connectingPointsCalculator3(qPoints,lastPoint.x(),lastPoint.y(),endPoint.x(),endPoint.y()); painter.setPen(Qt::black); painter.drawLines(qPoints); update (QRect(QPoint(minX,minY), QPoint(maxX,maxY)).normalized()); //update(); modified = true; lastPoint = endPoint; }
Canvas::Canvas(QWidget *parent) { setAttribute(Qt::WA_StaticContents); reset(); qPoints.clear(); } void Canvas::reset() { resizeImage(&image,QSize(800,400)); image.fill(qRgb(255, 255, 255)); update(); } void Canvas::resizeImage(QImage *image, const QSize &newSize) { if (image->size() == newSize) return; QImage newImage(newSize, QImage::Format_RGB32); QPainter painter(&newImage); painter.drawImage(QPoint(0, 0), *image); *image = newImage; }
void Canvas::mousePressEvent(QMouseEvent *event) { if(event->button() & Qt::LeftButton) { scribbling=true; lastPoint=event->pos(); } } void Canvas::mouseMoveEvent(QMouseEvent *event) { if((event->buttons() & Qt::LeftButton) && scribbling==true) { drawLineTo(event->pos()); } } void Canvas::mouseReleaseEvent(QMouseEvent *event) { if((event->button()&Qt::LeftButton)&&scribbling==true) { drawLineTo(event->pos()); scribbling=false; } }
void Canvas::paintEvent(QPaintEvent *event) { QPainter painter(this); QRect dirtyRect = event->rect(); painter.drawImage(dirtyRect, image, dirtyRect); }
|
2010-06-15
, 14:22
|
Posts: 341 |
Thanked: 57 times |
Joined on Nov 2009
|
#14
|
ConnectingPointsCalculator3 is interesting... "minX=y-5;"
How did you verify that it gave correct x points out?
|
2010-06-15
, 15:45
|
|
Posts: 1,366 |
Thanked: 1,185 times |
Joined on Jan 2006
|
#15
|
|
2010-06-17
, 18:19
|
Posts: 61 |
Thanked: 43 times |
Joined on Aug 2008
|
#16
|
output has been verified because I've checked the final list of points calculated for a number of different orientations .. its always correct ..
EDIT: Besides, I even tried updating the entire region using just update(), completely ignoring min/max X/Y values .. even then it was the same result ..
// Add if needed //#include <qmath.h> void connectingPointsCalculator4( QVector<QPoint>& inputqPoints, int x0, int y0, int x1, int y1) { inputqPoints.clear(); float i = qMax(qAbs(x0-x1), qAbs(y0-y1)); for(int j = 0; j<=i; j++) { inputqPoints.append(QPoint(qRound(x0-(((x0-x1)/i)*j)-5), qRound(y0-(((y0-y1)/i)*j)-5))); inputqPoints.append(QPoint(qRound(x0-(((x0-x1)/i)*j)+5), qRound(y0-(((y0-y1)/i)*j)+5))); } }
The Following User Says Thank You to harriva For This Useful Post: | ||
|
2010-06-19
, 00:07
|
Posts: 341 |
Thanked: 57 times |
Joined on Nov 2009
|
#17
|
|
2010-06-19
, 00:33
|
|
Posts: 1,055 |
Thanked: 4,107 times |
Joined on Oct 2009
@ Norway
|
#18
|
Without thinking a lot about it:
You could try to paint into a QPixmap. Store your points and only append them to the pixmap. Only if the widget is resized, you'll generate a new pixmap or resize it.
That would consume some (not a lot) memory and stops your n900 from slowing down. Maybe it even helps with the update problems.
EDIT: Oops, just saw you seem to already draw onto a image ...
|
2010-06-23
, 20:10
|
Posts: 341 |
Thanked: 57 times |
Joined on Nov 2009
|
#19
|
I had my doubts about your connectingPointsCalculator3 and it didn't seem to be giving me correct points when I tested it. Want to try my code?
Code:// Add if needed //#include <qmath.h> void connectingPointsCalculator4( QVector<QPoint>& inputqPoints, int x0, int y0, int x1, int y1) { inputqPoints.clear(); float i = qMax(qAbs(x0-x1), qAbs(y0-y1)); for(int j = 0; j<=i; j++) { inputqPoints.append(QPoint(qRound(x0-(((x0-x1)/i)*j)-5), qRound(y0-(((y0-y1)/i)*j)-5))); inputqPoints.append(QPoint(qRound(x0-(((x0-x1)/i)*j)+5), qRound(y0-(((y0-y1)/i)*j)+5))); } }
void Canvas::connectingPointsCalculator4( QVector<QPoint>& inputqPoints, int x0, int y0, int x1, int y1) { inputqPoints.clear(); float i = qMax(qAbs(x0-x1), qAbs(y0-y1)); int x=0, y=0; int xprevious,yprevious; bool oneRoundComplete = false; for(int j = 0; j<=i; j++) { xprevious = x; yprevious = y; x = qRound(x0-(((x0-x1)/i)*j)); y = qRound(y0-(((y0-y1)/i)*j)); if(oneRoundComplete==true) { if(x!=xprevious && y!=yprevious) { inputqPoints.append(QPoint(xprevious-5, y-5)); inputqPoints.append(QPoint(xprevious+5, y+5)); } } inputqPoints.append(QPoint(x-5, y-5)); inputqPoints.append(QPoint(x+5, y+5)); oneRoundComplete = true; } }
|
2010-06-23
, 22:53
|
Posts: 61 |
Thanked: 43 times |
Joined on Aug 2008
|
#20
|
void connectingPointsCalculator4( QVector<QPoint>& inputqPoints, int x0, int y0, int x1, int y1) { inputqPoints.clear(); float i = qAbs(x0-x1) + qAbs(y0-y1) ; if(!i) return; for(int j = 0; j<=i; j++) { inputqPoints.append(QPoint(qCeil(x0-(((x0-x1)/i)*j)+5), qFloor(y0-(((y0-y1)/i)*j)+5))); inputqPoints.append(QPoint(qCeil(x0-(((x0-x1)/i)*j)-5), qFloor(y0-(((y0-y1)/i)*j)-5))); } }
If vertical lines are always ok when doing partial or full update then I don't think it's performance issue (updating blocks input) and I won't recommend using low priority thread to handle updating.
I'm clueless, but please show latest code.