Commit 014319ab authored by Tzvetelin Katchov's avatar Tzvetelin Katchov Committed by Ralf Habacker

Refactoring UMLView

Fix centering of scene view after scroll wheel zoom or window resize.
Remove internal properties for zoom level and center point.
Instead of them get actual value from matrix().m11().
Remove unnecesary and dupliicate methods.
Fix setting of zoom in diagram properties page.

BUG:358097
FIXED-IN:2.18.2 (KDE Applications 15.12.2)
REVIEW:126773
parent 15117e4c
......@@ -33,7 +33,7 @@ DiagramPropertiesPage::DiagramPropertiesPage(QWidget *parent, UMLScene *scene)
setupUi(this);
ui_diagramName->setText(scene->name());
ui_zoom->setValue(scene->activeView()->currentZoom());
ui_zoom->setValue(scene->activeView()->zoom());
ui_checkBoxShowGrid->setChecked(scene->isSnapGridVisible());
ui_snapToGrid->setChecked(scene->snapToGrid());
......@@ -104,7 +104,7 @@ bool DiagramPropertiesPage::checkUniqueDiagramName()
void DiagramPropertiesPage::apply()
{
checkUniqueDiagramName();
//:TODO: m_pScene->setZoom(m_diagramProperties->ui_zoom->value());
m_scene->activeView()->setZoom(ui_zoom->value());
m_scene->setDocumentation(ui_documentation->toPlainText());
m_scene->setSnapSpacing(ui_gridSpaceX->value(), ui_gridSpaceY->value());
m_scene->setSnapToGrid(ui_snapToGrid->isChecked());
......
......@@ -756,7 +756,7 @@ void UMLApp::setupZoomMenu()
{
m_zoomSelect->clear();
int currentZoom = currentView()->currentZoom();
int currentZoom = currentView()->zoom();
m_zoomSelect->addAction(createZoomAction(33, currentZoom));
m_zoomSelect->addAction(createZoomAction(50, currentZoom));
......
......@@ -31,8 +31,7 @@ DEBUG_REGISTER(UMLView)
* Constructor.
*/
UMLView::UMLView(UMLFolder *parentFolder)
: QGraphicsView(UMLApp::app()->mainViewWidget()),
m_nZoom(100)
: QGraphicsView(UMLApp::app()->mainViewWidget())
{
setAcceptDrops(true);
setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
......@@ -61,7 +60,7 @@ UMLScene* UMLView::umlScene() const
*/
int UMLView::zoom() const
{
return m_nZoom;
return (int)(matrix().m11()*100.0);
}
/**
......@@ -79,18 +78,9 @@ void UMLView::setZoom(int zoom)
wm.scale(zoom / 100.0, zoom / 100.0);
setMatrix(wm);
m_nZoom = currentZoom();
umlScene()->resizeSceneToItems();
}
/**
* Return the current zoom factor.
*/
int UMLView::currentZoom()
{
return (int)(matrix().m11()*100.0);
}
/**
* Shows the properties dialog for the view.
*/
......@@ -137,20 +127,21 @@ void UMLView::wheelEvent(QWheelEvent* event)
// get the position of the mouse before scaling, in scene coords
QPointF pointBeforeScale(mapToScene(event->pos()));
// get the original screen centerpoint
QPointF screenCenter = center();
// scale the view ie. do the zoom
double scaleFactor = 1.15;
if (event->delta() > 0) {
// zoom in
if (currentZoom() < 500) {
if (zoom() < 500) {
scale(scaleFactor, scaleFactor);
} else {
return;
}
} else {
// zooming out
if (currentZoom() > 10) {
if (zoom() > 10) {
scale(1.0 / scaleFactor, 1.0 / scaleFactor);
} else {
return;
}
}
......@@ -161,25 +152,11 @@ void UMLView::wheelEvent(QWheelEvent* event)
QPointF offset = pointBeforeScale - pointAfterScale;
// adjust to the new center for correct zooming
QPointF newCenter = screenCenter + offset;
setCenter(newCenter);
QPointF newCenter = mapToScene(rect()).boundingRect().center() + offset;
centerOn(newCenter);
DEBUG(DBG_SRC) << "currentZoom=" << currentZoom();
UMLApp::app()->slotZoomSliderMoved(currentZoom());
}
/**
* Need to update the center so there is no jolt in the
* interaction after resizing the widget.
*/
void UMLView::resizeEvent(QResizeEvent* event)
{
// get the rectangle of the visible area in scene coords
QRectF visibleArea = mapToScene(rect()).boundingRect();
setCenter(visibleArea.center());
// call the subclass resize so the scrollbars are updated correctly
QGraphicsView::resizeEvent(event);
DEBUG(DBG_SRC) << "currentZoom=" << zoom();
UMLApp::app()->slotZoomSliderMoved(zoom());
}
/**
......@@ -211,14 +188,6 @@ void UMLView::hideEvent(QHideEvent* he)
us->hideEvent(he);
}
/**
* Override standard method.
*/
void UMLView::closeEvent(QCloseEvent* ce)
{
QWidget::closeEvent(ce);
}
/**
* Override standard method.
*/
......@@ -246,66 +215,3 @@ void UMLView::mouseReleaseEvent(QMouseEvent* event)
} else
QGraphicsView::mouseReleaseEvent(event);
}
/**
* Sets the current centerpoint. Also updates the scene's center point.
* Unlike centerOn, which has no way of getting the floating point center
* back, setCenter() stores the center point. It also handles the special
* sidebar case. This function will claim the centerPoint to sceneRec ie.
* the centerPoint must be within the sceneRec.
*/
void UMLView::setCenter(const QPointF& centerPoint)
{
// get the rectangle of the visible area in scene coords
QRectF visibleArea = mapToScene(rect()).boundingRect();
// get the scene area
QRectF sceneBounds = sceneRect();
double boundX = visibleArea.width() / 2.0;
double boundY = visibleArea.height() / 2.0;
double boundWidth = sceneBounds.width() - 2.0 * boundX;
double boundHeight = sceneBounds.height() - 2.0 * boundY;
// the max boundary that the centerPoint can be to
QRectF bounds(boundX, boundY, boundWidth, boundHeight);
if (bounds.contains(centerPoint)) {
// we are within the bounds
m_currentCenterPoint = centerPoint;
} else {
// we need to clamp or use the center of the screen
if(visibleArea.contains(sceneBounds)) {
// use the center of scene ie. we can see the whole scene
m_currentCenterPoint = sceneBounds.center();
} else {
m_currentCenterPoint = centerPoint;
// we need to clamp the center. The centerPoint is too large
if (centerPoint.x() > bounds.x() + bounds.width()) {
m_currentCenterPoint.setX(bounds.x() + bounds.width());
} else if (centerPoint.x() < bounds.x()) {
m_currentCenterPoint.setX(bounds.x());
}
if (centerPoint.y() > bounds.y() + bounds.height()) {
m_currentCenterPoint.setY(bounds.y() + bounds.height());
} else if (centerPoint.y() < bounds.y()) {
m_currentCenterPoint.setY(bounds.y());
}
}
}
// update the scrollbars
centerOn(m_currentCenterPoint);
}
/**
* Get the center.
*/
QPointF UMLView::center()
{
return m_currentCenterPoint;
}
......@@ -42,7 +42,6 @@ public:
int zoom() const ;
void setZoom(int zoom);
int currentZoom();
virtual bool showPropertiesDialog(QWidget *parent = 0);
......@@ -53,18 +52,10 @@ public slots:
protected:
virtual void wheelEvent(QWheelEvent* event);
virtual void resizeEvent(QResizeEvent* event);
virtual void showEvent(QShowEvent *se);
virtual void hideEvent(QHideEvent *he);
virtual void closeEvent(QCloseEvent* ce);
virtual void mousePressEvent(QMouseEvent* event);
virtual void mouseReleaseEvent(QMouseEvent* event);
void setCenter(const QPointF& centerPoint);
QPointF center();
QPointF m_currentCenterPoint; ///< holds the current centerpoint for the view, used for panning and zooming
int m_nZoom; ///< zoom level in percent, default 100
};
#endif // UMLVIEW_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment