#include
"mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QToolBar *bar =
this->addToolBar(
"Tools");
QActionGroup *group =
new QActionGroup(bar);
QAction *drawLineAction =
new QAction(
"Line", bar);
drawLineAction->setIcon(QIcon(
":/line.png"));
drawLineAction->setToolTip(tr(
"Draw a line."));
drawLineAction->setStatusTip(tr(
"Draw a line."));
drawLineAction->setCheckable(
true);
drawLineAction->setChecked(
true);
group->addAction(drawLineAction);
bar->addAction(drawLineAction);
QAction *drawRectAction =
new QAction(
"Rectangle", bar);
drawRectAction->setIcon(QIcon(
":/rect.png"));
drawRectAction->setToolTip(tr(
"Draw a rectangle."));
drawRectAction->setStatusTip(tr(
"Draw a rectangle."));
drawRectAction->setCheckable(
true);
group->addAction(drawRectAction);
bar->addAction(drawRectAction);
QLabel *statusMsg =
new QLabel;
statusBar()->addWidget(statusMsg);
PaintWidget *paintWidget =
new PaintWidget(
this);
QGraphicsView *view =
new QGraphicsView(paintWidget,
this);
setCentralWidget(view);
connect(drawLineAction, SIGNAL(triggered()),
this, SLOT(drawLineActionTriggered()));
connect(drawRectAction, SIGNAL(triggered()),
this, SLOT(drawRectActionTriggered()));
connect(
this, SIGNAL(changeCurrentShape(Shape::Code)),
paintWidget, SLOT(setCurrentShape(Shape::Code)));
}
void MainWindow::drawLineActionTriggered()
{
emit changeCurrentShape(Shape::Line);
}
void MainWindow::drawRectActionTriggered()
{
emit changeCurrentShape(Shape::Rect);
}
#ifndef PAINTWIDGET_H
#define PAINTWIDGET_H
#include <QtGui>
#include <QDebug>
#include
"shape.h"
#include
"line.h"
#include
"rect.h"
class PaintWidget :
public QGraphicsScene
{
Q_OBJECT
public:
PaintWidget(QWidget *parent = 0);
public slots:
void setCurrentShape(Shape::Code s)
{
if(s != currShapeCode) {
currShapeCode = s;
}
}
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *
event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *
event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *
event);
private:
Shape::Code currShapeCode;
Shape *currItem;
bool perm;
};
#endif
// PAINTWIDGET_H
#include
"paintwidget.h"
PaintWidget::PaintWidget(QWidget *parent)
: QGraphicsScene(parent), currShapeCode(Shape::Line), currItem(NULL), perm(
false)
{
}
void PaintWidget::mousePressEvent(QGraphicsSceneMouseEvent *
event)
{
switch(currShapeCode)
{
case Shape::Line:
{
Line *line =
new Line;
currItem = line;
addItem(line);
break;
}
case Shape::Rect:
{
Rect *rect =
new Rect;
currItem = rect;
addItem(rect);
break;
}
}
if(currItem) {
currItem->startDraw(
event);
perm =
false;
}
QGraphicsScene::mousePressEvent(
event);
}
void PaintWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *
event)
{
if(currItem && !perm) {
currItem->drawing(
event);
}
QGraphicsScene::mouseMoveEvent(
event);
}
void PaintWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *
event)
{
perm =
true;
QGraphicsScene::mouseReleaseEvent(
event);
}
#ifndef SHAPE_H
#define SHAPE_H
#include <QtGui>
class Shape
{
public:
enum Code {
Line,
Rect
};
Shape();
virtual
void startDraw(QGraphicsSceneMouseEvent *
event) = 0;
virtual
void drawing(QGraphicsSceneMouseEvent *
event) = 0;
};
#endif
// SHAPE_H
#include
"shape.h"
Shape::Shape()
{
}
#ifndef LINE_H
#define LINE_H
#include <QGraphicsLineItem>
#include
"shape.h"
class Line :
public Shape,
public QGraphicsLineItem
{
public:
Line();
void startDraw(QGraphicsSceneMouseEvent *
event);
void drawing(QGraphicsSceneMouseEvent *
event);
};
#endif
// LINE_H
#include
"line.h"
Line::Line()
{
}
void Line::startDraw(QGraphicsSceneMouseEvent *
event)
{
setLine(QLineF(
event->scenePos(),
event->scenePos()));
}
void Line::drawing(QGraphicsSceneMouseEvent *
event)
{
QLineF newLine(line().p1(),
event->scenePos());
setLine(newLine);
}
#ifndef RECT_H
#define RECT_H
#include <QGraphicsRectItem>
#include
"shape.h"
class Rect :
public Shape,
public QGraphicsRectItem
{
public:
Rect();
void startDraw(QGraphicsSceneMouseEvent *
event);
void drawing(QGraphicsSceneMouseEvent *
event);
};
#endif
// RECT_H
#include
"rect.h"
Rect::Rect()
{
}
void Rect::startDraw(QGraphicsSceneMouseEvent *
event)
{
setRect(QRectF(
event->scenePos(), QSizeF(0, 0)));
}
void Rect::drawing(QGraphicsSceneMouseEvent *
event)
{
QRectF r(rect().topLeft(),
QSizeF(
event->scenePos().x() - rect().topLeft().x(),
event->scenePos().y() - rect().topLeft().y()));
setRect(r);
}
setFlag(QGraphicsItem::ItemIsMovable,
true);
setFlag(QGraphicsItem::ItemIsSelectable,
true);
本文出自 “豆子空间” 博客,请务必保留此出处http://devbean.blog.51cto.com/448512/244181
6447



被折叠的 条评论
为什么被折叠?



