QGraphicsView resizing after scaling or moving item.

--HG--
branch : feature
This commit is contained in:
dismine 2014-01-15 00:08:34 +02:00
parent 666dd664eb
commit 0996dab823
5 changed files with 101 additions and 42 deletions

View File

@ -920,6 +920,7 @@ void MainWindow::ActionDraw(bool checked)
currentScene = sceneDraw; currentScene = sceneDraw;
view->setScene(currentScene); view->setScene(currentScene);
connect(view, &VMainGraphicsView::NewFactor, sceneDraw, &VMainGraphicsScene::SetFactor);
RestoreCurrentScene(); RestoreCurrentScene();
mode = Draw::Calculation; mode = Draw::Calculation;
@ -951,6 +952,7 @@ void MainWindow::ActionDetails(bool checked)
currentScene = sceneDetails; currentScene = sceneDetails;
view->setScene(sceneDetails); view->setScene(sceneDetails);
disconnect(view, &VMainGraphicsView::NewFactor, sceneDraw, &VMainGraphicsScene::SetFactor);
RestoreCurrentScene(); RestoreCurrentScene();
drawMode = false; drawMode = false;

View File

@ -115,23 +115,31 @@ QVariant VToolSinglePoint::itemChange(QGraphicsItem::GraphicsItemChange change,
QGraphicsScene *sc = this->scene(); QGraphicsScene *sc = this->scene();
QRectF rect = sc->itemsBoundingRect(); QRectF rect = sc->itemsBoundingRect();
//Correct BoundingRect
rect = QRectF(0, 0, rect.width() + rect.x(), rect.height() + rect.y());
QList<QGraphicsView*> list = sc->views(); QList<QGraphicsView*> list = sc->views();
QRect rec = list[0]->contentsRect(); QRect rec0 = list[0]->rect();
//Correct contentsRect rec0 = QRect(0, 0, rec0.width()-2, rec0.height()-2);
rec = QRect(0, 0, rec.width() - rec.x(), rec.height() - rec.y());
if(rec.contains(rect.toRect())) QTransform t = list[0]->transform();
QRectF rec1;
if(t.m11() < 1)
{ {
sc->setSceneRect(rec); rec1 = QRect(0, 0, rec0.width()/t.m11(), rec0.height()/t.m22());
rec1.translate(rec0.center().x()-rec1.center().x(), rec0.center().y()-rec1.center().y());
QPolygonF polygone = list[0]->mapToScene(rec1.toRect());
rec1 = polygone.boundingRect();
} }
else else
{ {
rect = rect.united(rec); rec1 = rec0;
sc->setSceneRect(rect);
} }
rec1 = rec1.united(rect.toRect());
sc->setSceneRect(rec1);
//I don't now why but signal does not work. //I don't now why but signal does not work.
doc->FullUpdateTree(); doc->FullUpdateTree();
} }

View File

@ -261,22 +261,30 @@ QVariant VToolDetail::itemChange(QGraphicsItem::GraphicsItemChange change, const
QGraphicsScene *sc = this->scene(); QGraphicsScene *sc = this->scene();
QRectF rect = sc->itemsBoundingRect(); QRectF rect = sc->itemsBoundingRect();
//Correct BoundingRect
//rect = QRectF(0, 0, rect.width() + rect.x(), rect.height() + rect.y());
qDebug()<<"rect"<<rect;
QList<QGraphicsView*> list = sc->views(); QList<QGraphicsView*> list = sc->views();
QRect rec = list[0]->contentsRect(); QRect rec0 = list[0]->rect();
rec0 = QRect(0, 0, rec0.width()-2, rec0.height()-2);
QTransform t = list[0]->transform(); QTransform t = list[0]->transform();
qDebug()<<"m11="<<t.m11();
qDebug()<<"m22="<<t.m22(); QRectF rec1;
//Correct contentsRect if(t.m11() < 1)
rec = QRect(0, 0, rec.width()/t.m11() - rec.x(), rec.height()/t.m22() - rec.y()); {
// rec = QRectF(rec.x()/t.m11(), rec.y()/t.m22(), rec.width()/t.m11(), rec.height()/t.m22()); rec1 = QRect(0, 0, rec0.width()/t.m11(), rec0.height()/t.m22());
qDebug()<<"rec"<<rec;
rec = rec.united(rect.toRect()); rec1.translate(rec0.center().x()-rec1.center().x(), rec0.center().y()-rec1.center().y());
qDebug()<<"rec1"<<rec; QPolygonF polygone = list[0]->mapToScene(rec1.toRect());
sc->setSceneRect(rec); rec1 = polygone.boundingRect();
}
else
{
rec1 = rec0;
}
rec1 = rec1.united(rect.toRect());
sc->setSceneRect(rec1);
doc->haveLiteChange(); doc->haveLiteChange();
} }

View File

@ -43,48 +43,88 @@ VMainGraphicsView::VMainGraphicsView(QWidget *parent)
void VMainGraphicsView::wheelEvent(QWheelEvent *event) void VMainGraphicsView::wheelEvent(QWheelEvent *event)
{ {
int numDegrees = event->delta() / 8; int numSteps = event->delta() / 8 / 15; // see QWheelEvent documentation
int numSteps = numDegrees / 15; // see QWheelEvent documentation
_numScheduledScalings += numSteps; _numScheduledScalings += numSteps;
if (_numScheduledScalings * numSteps < 0) if (_numScheduledScalings * numSteps < 0)
{ // if user moved the wheel in another direction, we reset { // if user moved the wheel in another direction, we reset
_numScheduledScalings = numSteps; // previously scheduled scalings _numScheduledScalings = numSteps; // previously scheduled scalings
} }
QTimeLine *anim = new QTimeLine(350, this); QTimeLine *anim = new QTimeLine(300, this);
Q_ASSERT(anim != 0); Q_ASSERT(anim != 0);
anim->setUpdateInterval(20); anim->setUpdateInterval(20);
connect(anim, &QTimeLine::valueChanged, this, &VMainGraphicsView::scalingTime); if (QApplication::keyboardModifiers() == Qt::ControlModifier)
{// If you press CTRL this code will be executed
connect(anim, &QTimeLine::valueChanged, this, &VMainGraphicsView::scalingTime);
}
else
{
connect(anim, &QTimeLine::valueChanged, this, &VMainGraphicsView::scrollingTime);
}
connect(anim, &QTimeLine::finished, this, &VMainGraphicsView::animFinished); connect(anim, &QTimeLine::finished, this, &VMainGraphicsView::animFinished);
anim->start(); anim->start();
} }
void VMainGraphicsView::scalingTime(qreal x) void VMainGraphicsView::scalingTime(qreal x)
{
Q_UNUSED(x);
const QPointF p0scene = mapToScene(mapFromGlobal(QCursor::pos()));
qreal factor = 1.0 + static_cast<qreal>(_numScheduledScalings) / 50.0;
scale(factor, factor);
const QPointF p1mouse = mapFromScene(p0scene);
const QPointF move = p1mouse - this->mapFromGlobal(QCursor::pos()); // The move
horizontalScrollBar()->setValue(move.x() + horizontalScrollBar()->value());
verticalScrollBar()->setValue(move.y() + verticalScrollBar()->value());
QGraphicsScene *sc = this->scene();
QRectF rect = sc->itemsBoundingRect();
QRect rec0 = this->rect();
rec0 = QRect(0, 0, rec0.width()-2, rec0.height()-2);
QTransform t = this->transform();
QRectF rec1;
if(t.m11() < 1)
{
rec1 = QRect(0, 0, rec0.width()/t.m11(), rec0.height()/t.m22());
rec1.translate(rec0.center().x()-rec1.center().x(), rec0.center().y()-rec1.center().y());
QPolygonF polygone = this->mapToScene(rec1.toRect());
rec1 = polygone.boundingRect();
}
else
{
rec1 = rec0;
}
rec1 = rec1.united(rect.toRect());
sc->setSceneRect(rec1);
emit NewFactor(factor);
}
void VMainGraphicsView::scrollingTime(qreal x)
{ {
Q_UNUSED(x); Q_UNUSED(x);
qreal factor = 1.0; qreal factor = 1.0;
if (QApplication::keyboardModifiers() == Qt::ControlModifier) if (_numScheduledScalings < 0)
{// If you press CTRL this code will be executed {
factor = 1.0 + static_cast<qreal>(_numScheduledScalings) / 300.0; verticalScrollBar()->setValue(qRound(verticalScrollBar()->value() + factor*3.5));
scale(factor, factor);
emit NewFactor(factor); emit NewFactor(factor);
} }
else else
{ {
if (_numScheduledScalings < 0) // if (verticalScrollBar()->value() > 0)
{ // {
verticalScrollBar()->setValue(qRound(verticalScrollBar()->value() + factor*3.5)); verticalScrollBar()->setValue(qRound(verticalScrollBar()->value() - factor*3.5));
emit NewFactor(factor); emit NewFactor(factor);
} // }
else
{
if (verticalScrollBar()->value() > 0)
{
verticalScrollBar()->setValue(qRound(verticalScrollBar()->value() - factor*3.5));
emit NewFactor(factor);
}
}
} }
} }

View File

@ -55,6 +55,7 @@ public slots:
* @param x * @param x
*/ */
void scalingTime(qreal x); void scalingTime(qreal x);
void scrollingTime(qreal x);
/** /**
* @brief animFinished * @brief animFinished
*/ */