#include "picscene.h" #include #include #include #include PicScene::PicScene(QObject *parent) : QGraphicsScene(parent),IsdrawBackground(false) { } void PicScene::setMode(Mode mode) { myMode = mode; } void PicScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) { if (mouseEvent->button() != Qt::LeftButton) return; QGraphicsScene::mousePressEvent(mouseEvent); } //! [9] //! [10] void PicScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent) { update();//edit@12-12 QGraphicsScene::mouseMoveEvent(mouseEvent); } //! [10] //! [11] void PicScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) { QGraphicsScene::mouseReleaseEvent(mouseEvent); } //! [13] //! [14] bool PicScene::isItemChange(int type) { foreach (QGraphicsItem *item, selectedItems()) { if (item->type() == type) return true; } return false; } //! [14] void PicScene::keyPressEvent(QKeyEvent *event) { QGraphicsScene::keyPressEvent(event); } //edit@12-09 void PicScene::drawBackground(QPainter *painter, const QRectF &rect) { QGraphicsScene::drawBackground(painter,rect); if(IsdrawBackground) { Q_UNUSED(rect); QPen pen; const qreal left=rect.left(); const qreal right=rect.right(); const qreal top=rect.top(); const qreal bottom=rect.bottom(); const int step=1; //pen.setColor(QColor(60,60,60)); pen.setColor(Qt::lightGray); //pen.setColor(Qt::darkCyan); pen.setWidth(0); //pen.setStyle(Qt::DashLine); painter->setPen(pen); //绘制横线 //绘制Y轴正半轴 for(int i=0;i>=top;i-=step) { painter->drawLine(left,i,right,i); } //绘制Y轴负半轴 for(int i=0;i<=bottom;i+=step) { painter->drawLine(left,i,right,i); } //绘制竖线 //绘制X轴正半轴 for(int i=0;i<=right;i+=step) { painter->drawLine(i,top,i,bottom); } //绘制X轴负半轴 for(int i=0;i>=left;i-=step) { painter->drawLine(i,top,i,bottom); } //edit@12-11 pen.setColor(Qt::green); painter->setPen(pen); QRectF BoundingRect=this->itemsBoundingRect(); BoundingRect.setHeight(32); painter->drawRect(BoundingRect); } emit showTextWidthHeight(); } //edit@12-09 void PicScene::setIsdrawBackground(bool newIsdrawBackground) { IsdrawBackground = newIsdrawBackground; }