first commit
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
#include "picscene.h"
|
||||
#include <QDebug>
|
||||
#include <QRectF>
|
||||
#include <QPainter>
|
||||
#include <QGraphicsView>
|
||||
PicScene::PicScene(QObject *parent) :
|
||||
QGraphicsScene(parent),IsdrawBackground(false),
|
||||
currFocusItem(nullptr)
|
||||
{
|
||||
connect(this, &QGraphicsScene::focusItemChanged, this, &PicScene::onFocusItemChanged);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
QPen pen;
|
||||
|
||||
// 使用sceneRect()而不是参数rect,因为rect是由view设置的,其依赖于当前view的大小
|
||||
const qreal left = sceneRect().left();
|
||||
const qreal right = sceneRect().right();
|
||||
const qreal top = sceneRect().top();
|
||||
const qreal bottom = sceneRect().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;
|
||||
}
|
||||
|
||||
void PicScene::onFocusItemChanged(QGraphicsItem *newFocusItem, QGraphicsItem *oldFocusItem, Qt::FocusReason reason)
|
||||
{
|
||||
// 断开旧的连接
|
||||
if(currFocusItem) {
|
||||
disconnect(currFocusItem, &QGraphicsObject::xChanged, this, &PicScene::onFocusedItemPosChanged);
|
||||
disconnect(currFocusItem, &QGraphicsObject::yChanged, this, &PicScene::onFocusedItemPosChanged);
|
||||
}
|
||||
|
||||
// 只对对所有继承于QGraphicsObject的Item有效
|
||||
// 仅有继承于QGraphicsObject的item实现了x,y changed信号
|
||||
currFocusItem = dynamic_cast<QGraphicsObject*>(newFocusItem);
|
||||
if(!currFocusItem){
|
||||
// Nothing
|
||||
return;
|
||||
}
|
||||
|
||||
QPoint pos(currFocusItem->x(), currFocusItem->y());
|
||||
emit focusedItemPosChanged(pos);
|
||||
|
||||
connect(currFocusItem, &QGraphicsObject::xChanged, this, &PicScene::onFocusedItemPosChanged);
|
||||
connect(currFocusItem, &QGraphicsObject::yChanged, this, &PicScene::onFocusedItemPosChanged);
|
||||
}
|
||||
|
||||
void PicScene::onFocusedItemPosChanged()
|
||||
{
|
||||
if(currFocusItem)
|
||||
emit focusedItemPosChanged(currFocusItem->pos().toPoint());
|
||||
}
|
||||
Reference in New Issue
Block a user