Yaa..
Here is the code:
CanvasMouse.cpp This file handles the mouse click events and then finds the point through the collision method and sends the info to the ImageDraw class..
#include "canvasmouse.h"
CanvasMouse
::CanvasMouse(Q3Canvas
*pCanvas,
QFrame *frame
): Q3CanvasView
(frame
){
m_pCanvas = pCanvas;
Q3CanvasView::setCanvas(m_pCanvas);
m_pCanvas->setBackgroundColor(Qt::white);
m_pCanvas->resize(400, 400);
imgdraw = new ImageDraw(m_pCanvas);
setDraw = false;
startDraw = false;
}
CanvasMouse::~CanvasMouse()
{
}
void CanvasMouse
::contentsMousePressEvent(QMouseEvent* e
) {
if(setDraw)
{
if(e->button() == Qt::LeftButton)
{
pPress = inverseWorldMatrix().map(e->pos());
startDraw = true;
}
}
}
void CanvasMouse
::contentsMouseReleaseEvent(QMouseEvent* e
) {
if(setDraw)
{
if((e->button() == Qt::LeftButton) && startDraw)
{
pRelease = inverseWorldMatrix().map(e->pos());
Q3CanvasItemList l = m_pCanvas->collisions(pRelease);;
imgdraw->selectLine(l);
}
}
}
#include "canvasmouse.h"
CanvasMouse::CanvasMouse(Q3Canvas *pCanvas, QFrame *frame): Q3CanvasView(frame)
{
m_pCanvas = pCanvas;
Q3CanvasView::setCanvas(m_pCanvas);
m_pCanvas->setBackgroundColor(Qt::white);
m_pCanvas->resize(400, 400);
imgdraw = new ImageDraw(m_pCanvas);
setDraw = false;
startDraw = false;
}
CanvasMouse::~CanvasMouse()
{
}
void CanvasMouse::contentsMousePressEvent(QMouseEvent* e)
{
if(setDraw)
{
if(e->button() == Qt::LeftButton)
{
pPress = inverseWorldMatrix().map(e->pos());
startDraw = true;
}
}
}
void CanvasMouse::contentsMouseReleaseEvent(QMouseEvent* e)
{
if(setDraw)
{
if((e->button() == Qt::LeftButton) && startDraw)
{
pRelease = inverseWorldMatrix().map(e->pos());
Q3CanvasItemList l = m_pCanvas->collisions(pRelease);;
imgdraw->selectLine(l);
}
}
}
To copy to clipboard, switch view to plain text mode
[B]ImageDraw.cpp [B]: This class does the finding of the line number and rect number. Its the selectLine function that finds the line number.
#include "imagedraw.h"
#include <Q3ValueList>
ImageDraw::ImageDraw(Q3Canvas *pCanvas): m_pCanvas(pCanvas)
{
drawLine();
drawRectangle();
}
ImageDraw::~ImageDraw()
{
}
void ImageDraw::drawLine()
{
int i;
int xxPoint = 2,xyPoint = 2,yxPoint = 202,yyPoint = 2;
lineNum = 20;
m_pCanvasLine = new Q3CanvasLine*[lineNum];
for(i=0;i<lineNum;i++)
{
xyPoint = (i+1)*4;
yyPoint = xyPoint;
m_pCanvasLine[i] = new Q3CanvasLine(m_pCanvas);
m_pCanvasLine[i]->setPoints(xxPoint,xyPoint,yxPoint,yyPoint);
m_pCanvasLine[i]->show();
}
m_pLineItem = new Q3CanvasLine(m_pCanvas);
lineList.append(m_pLineItem);
}
void ImageDraw::drawRectangle()
{
int xPoint = 4, yPoint = 100;
int height = 50, width = 80;
int i;
recNum = 5;
m_pCanvasRectangle = new Q3CanvasRectangle*[recNum];
for(i=0;i<recNum;i++)
{
m_pCanvasRectangle[i] = new Q3CanvasRectangle(xPoint, yPoint, width, height, m_pCanvas);
m_pCanvasRectangle[i]->show();
yPoint += 75;
}
}
void ImageDraw::selectLine(Q3CanvasItemList item)
{
int i=0;
m_itemLine = item;
for (Q3CanvasItemList::Iterator it= m_itemLine.begin(); it!= m_itemLine.end(); ++it)
{
if ( (*it)->rtti() == 7 )
{
for(i=0;i<lineList.size();i++)
{
if(lineList.at(i) == m_itemLine.at(i))
{
QMessageBox::information(this, tr
("Mouse Event"),tr
("Line %1 Mouse Event Handled ").
arg(i
));
}
}
}
else if ( (*it)->rtti() == 5 )
{
QMessageBox::information(this, tr
("Mouse Event"),tr
("Rectangle Mouse Event Handled "));
}
}
}
#include "imagedraw.h"
#include <Q3ValueList>
ImageDraw::ImageDraw(Q3Canvas *pCanvas): m_pCanvas(pCanvas)
{
drawLine();
drawRectangle();
}
ImageDraw::~ImageDraw()
{
}
void ImageDraw::drawLine()
{
int i;
int xxPoint = 2,xyPoint = 2,yxPoint = 202,yyPoint = 2;
lineNum = 20;
m_pCanvasLine = new Q3CanvasLine*[lineNum];
for(i=0;i<lineNum;i++)
{
xyPoint = (i+1)*4;
yyPoint = xyPoint;
m_pCanvasLine[i] = new Q3CanvasLine(m_pCanvas);
m_pCanvasLine[i]->setPoints(xxPoint,xyPoint,yxPoint,yyPoint);
m_pCanvasLine[i]->show();
}
m_pLineItem = new Q3CanvasLine(m_pCanvas);
lineList.append(m_pLineItem);
}
void ImageDraw::drawRectangle()
{
int xPoint = 4, yPoint = 100;
int height = 50, width = 80;
int i;
recNum = 5;
m_pCanvasRectangle = new Q3CanvasRectangle*[recNum];
for(i=0;i<recNum;i++)
{
m_pCanvasRectangle[i] = new Q3CanvasRectangle(xPoint, yPoint, width, height, m_pCanvas);
m_pCanvasRectangle[i]->show();
yPoint += 75;
}
}
void ImageDraw::selectLine(Q3CanvasItemList item)
{
int i=0;
m_itemLine = item;
for (Q3CanvasItemList::Iterator it= m_itemLine.begin(); it!= m_itemLine.end(); ++it)
{
if ( (*it)->rtti() == 7 )
{
for(i=0;i<lineList.size();i++)
{
if(lineList.at(i) == m_itemLine.at(i))
{
QMessageBox::information(this, tr("Mouse Event"),tr("Line %1 Mouse Event Handled ").arg(i));
}
}
}
else if ( (*it)->rtti() == 5 )
{
QMessageBox::information(this, tr("Mouse Event"),tr("Rectangle Mouse Event Handled "));
}
}
}
To copy to clipboard, switch view to plain text mode
ImageDraw.h: The header file where i have made the dec.
#ifndef IMAGEDRAW_H
#define IMAGEDRAW_H
#include <QtGui>
#include <Q3Canvas>
#include <qdir.h>
#include <QColor>
#include <Q3CanvasLine>
#include <qevent.h>
#include <QPoint>
#include <Q3CanvasView>
#include <Q3ScrollView>
#include <Q3CanvasItem>
#include <Q3CanvasItemList>
#include <QPen>
#include <QList>
class ImageDraw : public Q3CanvasView
{
public:
ImageDraw(Q3Canvas *pCanvas);
~ImageDraw();
void drawLine();
void drawRectangle();
void selectLine(Q3CanvasItemList item);
//void selectRect(Q3CanvasItemList item);
private:
Q3Canvas *m_pCanvas;
Q3CanvasLine **m_pCanvasLine;
Q3CanvasRectangle **m_pCanvasRectangle;
Q3CanvasItemList m_itemLine;
Q3CanvasItemList m_itemRect;
QList<Q3CanvasItem *> lineList;
Q3CanvasItem *m_pLineItem;
//const QPoint m_pointLine,m_pointRect;
int lineNum;
int recNum;
bool lineDraw,rectDraw;
};
#endif
#ifndef IMAGEDRAW_H
#define IMAGEDRAW_H
#include <QtGui>
#include <Q3Canvas>
#include <qdir.h>
#include <QColor>
#include <Q3CanvasLine>
#include <qevent.h>
#include <QPoint>
#include <Q3CanvasView>
#include <Q3ScrollView>
#include <Q3CanvasItem>
#include <Q3CanvasItemList>
#include <QPen>
#include <QList>
class ImageDraw : public Q3CanvasView
{
public:
ImageDraw(Q3Canvas *pCanvas);
~ImageDraw();
void drawLine();
void drawRectangle();
void selectLine(Q3CanvasItemList item);
//void selectRect(Q3CanvasItemList item);
private:
Q3Canvas *m_pCanvas;
Q3CanvasLine **m_pCanvasLine;
Q3CanvasRectangle **m_pCanvasRectangle;
Q3CanvasItemList m_itemLine;
Q3CanvasItemList m_itemRect;
QList<Q3CanvasItem *> lineList;
Q3CanvasItem *m_pLineItem;
//const QPoint m_pointLine,m_pointRect;
int lineNum;
int recNum;
bool lineDraw,rectDraw;
};
#endif
To copy to clipboard, switch view to plain text mode
Please tell me what can be the problem...
Bookmarks