Files

173 lines
4.6 KiB
C++

#if 1
#include "picscene.h"
#include <QDebug>
#include <QRectF>
#include <QPainter>
#include <QGraphicsView>
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
#if 0
if (myMode == InsertLine && line != 0) {
QLineF newLine(line->line().p1(), mouseEvent->scenePos());
line->setLine(newLine);
} else if (myMode == MoveItem) {
QGraphicsScene::mouseMoveEvent(mouseEvent);
}
#else
QGraphicsScene::mouseMoveEvent(mouseEvent);
#endif
}
//! [10]
//! [11]
void PicScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
#if 0 //不需要这个功能
if (line != 0 && myMode == InsertLine) {
QList<QGraphicsItem *> startItems = items(line->line().p1());
if (startItems.count() && startItems.first() == line)
startItems.removeFirst();
QList<QGraphicsItem *> endItems = items(line->line().p2());
if (endItems.count() && endItems.first() == line)
endItems.removeFirst();
removeItem(line);
delete line;
//! [11] //! [12]
if (startItems.count() > 0 && endItems.count() > 0 &&
startItems.first()->type() == DiagramItem::Type &&
endItems.first()->type() == DiagramItem::Type &&
startItems.first() != endItems.first()) {
DiagramItem *startItem =
qgraphicsitem_cast<DiagramItem *>(startItems.first());
DiagramItem *endItem =
qgraphicsitem_cast<DiagramItem *>(endItems.first());
Arrow *arrow = new Arrow(startItem, endItem);
arrow->setColor(myLineColor);
startItem->addArrow(arrow);
endItem->addArrow(arrow);
arrow->setZValue(-1000.0);
addItem(arrow);
arrow->updatePosition();
}
}
//! [12] //! [13]
line = 0;
#endif
QGraphicsScene::mouseReleaseEvent(mouseEvent);
//QRectF Rect=QGraphicsScene.itemsBoundingRect();
//qDebug()<<"GraphicsScene itemsBoundingRect "<<itemsBoundingRect();
//qDebug()<<"GraphicsScene toRect "<<itemsBoundingRect().pos();
//qDebug()<<"GraphicsScene rect "<<sceneRect();
//qDebug()<<"RectF.y "<<itemsBoundingRect().y();
//qDebug()<<"RectF.height "<<itemsBoundingRect().height();
//qDebug()<<"RectF.width "<<itemsBoundingRect().width();
//qDebug()<<"asdfasdf";
//qDebug()<<"GraphicsScene "<<sceneRect();
//emit xmlContentChanged();
}
//! [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;
}
#endif