Alright here's the breakdown of my program ... There's a Main Window, which is just a main window .. Then there's another class I made called Canvas .. an object of Canvas is stored in Main Window and is initialized from Main Window's contructor .. Canvas is my main class where all the paint events and mouse events are handled .. 'image' is just a QImage object contained within the Canvas class .. Drawing is done on this image ..
Here's a gist of my code ... Thought, only stuff relevant to this issue is presented here:
Here's Canvas.h: (was previously called 'Board' .. by Canvas I mean the widget which contains the image on which drawing is done)
{
Q_OBJECT
public:
protected:
private:
void drawLineTo
(const QPoint &endPoint
);
QImage image;
[B
]//this is the image on which drawing is done[/B] 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);
};
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; [B]//this is the image on which drawing is done[/B]
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);
};
To copy to clipboard, switch view to plain text mode
ConnectingPointsCalculator3 function .. (used to output a QVector of all the points which need to be connected to make all those lines to give the slanted effect ... only the point coordinates are calculated in this function):
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::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++;
}
}
To copy to clipboard, switch view to plain text mode
Function used to actually do all the drawing on the QImage object named 'image':
void Canvas
::drawLineTo(const QPoint &endPoint
) {
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();
modified = true;
lastPoint = endPoint;
}
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;
}
To copy to clipboard, switch view to plain text mode
3 functions to do the initialization within the Canvas object:
{
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;
painter.
drawImage(QPoint(0,
0),
*image
);
*image = newImage;
}
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;
}
To copy to clipboard, switch view to plain text mode
Drawing event functions:
{
if(event->button() & Qt::LeftButton)
{
scribbling=true;
lastPoint=event->pos();
}
}
{
if((event->buttons() & Qt::LeftButton) && scribbling==true)
{
drawLineTo(event->pos());
}
}
{
if((event->button()&Qt::LeftButton)&&scribbling==true)
{
drawLineTo(event->pos());
scribbling=false;
}
}
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;
}
}
To copy to clipboard, switch view to plain text mode
Canvas paintEvent():
{
QRect dirtyRect
= event
->rect
();
painter.drawImage(dirtyRect, image, dirtyRect);
}
void Canvas::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QRect dirtyRect = event->rect();
painter.drawImage(dirtyRect, image, dirtyRect);
}
To copy to clipboard, switch view to plain text mode
if you need code for my Main Window as well, please let me know ... And please help me figure this silly bug out :P
Bookmarks