Qt学习之路(31): 一个简易画板的实现(QWidget)


#ifndef MAINWINDOW_H

#define MAINWINDOW_H


#include <QtGui>


#include "shape.h"

#include "paintwidget.h"


class MainWindow : public QMainWindow

{

Q_OBJECT


public:

MainWindow(QWidget *parent = 0);


signals:

void changeCurrentShape(Shape::Code newShape);


private slots:

void drawLineActionTriggered();

void drawRectActionTriggered();


};


#endif // MAINWINDOW_H

#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);

setCentralWidget(paintWidget);


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 QWidget

{

Q_OBJECT


public:

PaintWidget(QWidget *parent = 0);


public slots:

void setCurrentShape(Shape::Code s)

{

if(s != currShapeCode) {

currShapeCode = s;

}

}


protected:

void paintEvent(QPaintEvent *event);

void mousePressEvent(QMouseEvent *event);

void mouseMoveEvent(QMouseEvent *event);

void mouseReleaseEvent(QMouseEvent *event);


private:

Shape::Code currShapeCode;

Shape *shape;

bool perm;

QList<Shape*> shapeList;

};


#endif // PAINTWIDGET_H

#include "paintwidget.h"


PaintWidget::PaintWidget(QWidget *parent)

: QWidget(parent), currShapeCode(Shape::Line), shape(NULL), perm(false)

{

setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

}


void PaintWidget::paintEvent(QPaintEvent *event)

{

QPainter painter(this);

painter.setBrush(Qt::white);

painter.drawRect(0, 0, size().width(), size().height());

foreach(Shape * shape, shapeList) {

shape->paint(painter);

}

if(shape) {

shape->paint(painter);

}

}


void PaintWidget::mousePressEvent(QMouseEvent *event)

{

switch(currShapeCode)

{

case Shape::Line:

{

shape = new Line;

break;

}

case Shape::Rect:

{

shape = new Rect;

break;

}

}

if(shape != NULL) {

perm = false;

shapeList<<shape;

shape->setStart(event->pos());

shape->setEnd(event->pos());

}

}


void PaintWidget::mouseMoveEvent(QMouseEvent *event)

{

if(shape && !perm) {

shape->setEnd(event->pos());

update();

}

}


void PaintWidget::mouseReleaseEvent(QMouseEvent *event)

{

perm = true;

}

#ifndef SHAPE_H

#define SHAPE_H


#include <QtGui>


class Shape

{

public:


enum Code {

Line,

Rect

};


Shape();


void setStart(QPoint s)

{

start = s;

}


void setEnd(QPoint e)

{

end = e;

}


QPoint startPoint()

{

return start;

}


QPoint endPoint()

{

return end;

}


void virtual paint(QPainter & painter) = 0;


protected:

QPoint start;

QPoint end;

};


#endif // SHAPE_H

#include "shape.h"


Shape::Shape()

{

}

#ifndef LINE_H

#define LINE_H


#include "shape.h"


class Line : public Shape

{

public:

Line();


void paint(QPainter &painter);

};


#endif // LINE_H

#include "line.h"


Line::Line()

{

}


void Line::paint(QPainter &painter)

{

painter.drawLine(start, end);

}

#ifndef RECT_H

#define RECT_H


#include "shape.h"


class Rect : public Shape

{

public:

Rect();


void paint(QPainter &painter);

};


#endif // RECT_H

#include "rect.h"


Rect::Rect()

{

}


void Rect::paint(QPainter &painter)

{

painter.drawRect(start.x(), start.y(),

end.x() - start.x(), end.y() - start.y());

}
