diff --git a/limereport/lrgraphicsviewzoom.cpp b/limereport/lrgraphicsviewzoom.cpp new file mode 100644 index 0000000..58fc752 --- /dev/null +++ b/limereport/lrgraphicsviewzoom.cpp @@ -0,0 +1,58 @@ +#include "lrgraphicsviewzoom.h" +#include +#include +#include + +namespace LimeReport{ + +GraphicsViewZoomer::GraphicsViewZoomer(QGraphicsView* view) + : QObject(view), m_view(view) +{ + m_view->viewport()->installEventFilter(this); + m_view->setMouseTracking(true); + m_modifiers = Qt::ControlModifier; + m_zoomFactorBase = 1.0015; +} + +void GraphicsViewZoomer::gentleZoom(double factor) { + m_view->scale(factor, factor); + m_view->centerOn(m_targetScenePos); + QPointF delta_viewport_pos = m_targetViewportPos - QPointF(m_view->viewport()->width() / 2.0, + m_view->viewport()->height() / 2.0); + QPointF viewport_center = m_view->mapFromScene(m_targetScenePos) - delta_viewport_pos; + m_view->centerOn(m_view->mapToScene(viewport_center.toPoint())); + emit zoomed(); +} + +void GraphicsViewZoomer::setModifiers(Qt::KeyboardModifiers modifiers) { + m_modifiers = modifiers; +} + +void GraphicsViewZoomer::setZoomFactorBase(double value) { + m_zoomFactorBase = value; +} + +bool GraphicsViewZoomer::eventFilter(QObject *object, QEvent *event) { + if (event->type() == QEvent::MouseMove) { + QMouseEvent* mouse_event = static_cast(event); + QPointF delta = m_targetViewportPos - mouse_event->pos(); + if (qAbs(delta.x()) > 5 || qAbs(delta.y()) > 5) { + m_targetViewportPos = mouse_event->pos(); + m_targetScenePos = m_view->mapToScene(mouse_event->pos()); + } + } else if (event->type() == QEvent::Wheel) { + QWheelEvent* wheel_event = static_cast(event); + if (QApplication::keyboardModifiers() == m_modifiers) { + if (wheel_event->orientation() == Qt::Vertical) { + double angle = wheel_event->delta(); + double factor = qPow(m_zoomFactorBase, angle); + gentleZoom(factor); + return true; + } + } + } + Q_UNUSED(object) + return false; +} + +} //namespace LimeReport diff --git a/limereport/lrgraphicsviewzoom.h b/limereport/lrgraphicsviewzoom.h new file mode 100644 index 0000000..9d0a714 --- /dev/null +++ b/limereport/lrgraphicsviewzoom.h @@ -0,0 +1,28 @@ +#ifndef GRAPHICSVIEWZOOM_H +#define GRAPHICSVIEWZOOM_H + +#include +#include + +namespace LimeReport { + +class GraphicsViewZoomer : public QObject { + Q_OBJECT +public: + GraphicsViewZoomer(QGraphicsView* view); + void gentleZoom(double factor); + void setModifiers(Qt::KeyboardModifiers modifiers); + void setZoomFactorBase(double value); +private: + QGraphicsView* m_view; + Qt::KeyboardModifiers m_modifiers; + double m_zoomFactorBase; + QPointF m_targetScenePos, m_targetViewportPos; + bool eventFilter(QObject* object, QEvent* event); +signals: + void zoomed(); +}; + +} // namespace LimeReport + +#endif // GRAPHICSVIEWZOOM_H diff --git a/limereport/lrreportdesignwidget.cpp b/limereport/lrreportdesignwidget.cpp index 9e29e31..9c23915 100644 --- a/limereport/lrreportdesignwidget.cpp +++ b/limereport/lrreportdesignwidget.cpp @@ -42,62 +42,9 @@ #include #include - namespace LimeReport { -//GraphicsViewZoom -GraphicsViewZoom::GraphicsViewZoom(QGraphicsView* view) - : QObject(view), m_view(view) -{ - m_view->viewport()->installEventFilter(this); - m_view->setMouseTracking(true); - m_modifiers = Qt::ControlModifier; - m_zoomFactorBase = 1.0015; -} - -void GraphicsViewZoom::gentleZoom(double factor) { - m_view->scale(factor, factor); - m_view->centerOn(m_targetScenePos); - QPointF delta_viewport_pos = m_targetViewportPos - QPointF(m_view->viewport()->width() / 2.0, - m_view->viewport()->height() / 2.0); - QPointF viewport_center = m_view->mapFromScene(m_targetScenePos) - delta_viewport_pos; - m_view->centerOn(m_view->mapToScene(viewport_center.toPoint())); - emit zoomed(); -} - -void GraphicsViewZoom::setModifiers(Qt::KeyboardModifiers modifiers) { - m_modifiers = modifiers; -} - -void GraphicsViewZoom::setZoomFactorBase(double value) { - m_zoomFactorBase = value; -} - -bool GraphicsViewZoom::eventFilter(QObject *object, QEvent *event) { - if (event->type() == QEvent::MouseMove) { - QMouseEvent* mouse_event = static_cast(event); - QPointF delta = m_targetViewportPos - mouse_event->pos(); - if (qAbs(delta.x()) > 5 || qAbs(delta.y()) > 5) { - m_targetViewportPos = mouse_event->pos(); - m_targetScenePos = m_view->mapToScene(mouse_event->pos()); - } - } else if (event->type() == QEvent::Wheel) { - QWheelEvent* wheel_event = static_cast(event); - if (QApplication::keyboardModifiers() == m_modifiers) { - if (wheel_event->orientation() == Qt::Vertical) { - double angle = wheel_event->delta(); - double factor = qPow(m_zoomFactorBase, angle); - gentleZoom(factor); - return true; - } - } - } - Q_UNUSED(object) - return false; -} - // ReportDesignIntf -//ReportDesignWidget* ReportDesignWidget::m_instance=0; ReportDesignWidget::ReportDesignWidget(ReportEnginePrivate *report, QMainWindow *mainWindow, QWidget *parent) : QWidget(parent), m_mainWindow(mainWindow), m_verticalGridStep(10), m_horizontalGridStep(10), m_useGrid(false) @@ -128,7 +75,7 @@ ReportDesignWidget::ReportDesignWidget(ReportEnginePrivate *report, QMainWindow m_view->scale(0.5,0.5); //m_instance=this; //m_view->viewport()->installEventFilter(this); - m_zoomer = new GraphicsViewZoom(m_view); + m_zoomer = new GraphicsViewZoomer(m_view); #ifdef Q_OS_WIN m_defaultFont = QFont("Arial",10); #endif @@ -193,7 +140,9 @@ void ReportDesignWidget::loadState(QSettings* settings) } ReportDesignWidget::~ReportDesignWidget() -{ /*m_instance=0;*/} +{ + delete m_zoomer; +} void ReportDesignWidget::setActivePage(PageDesignIntf *page) { diff --git a/limereport/lrreportdesignwidget.h b/limereport/lrreportdesignwidget.h index 185b956..d247f2b 100644 --- a/limereport/lrreportdesignwidget.h +++ b/limereport/lrreportdesignwidget.h @@ -39,6 +39,7 @@ #include "lrdatasourcemanager.h" #include "lrcollection.h" #include "lrreportengine_p.h" +#include "lrgraphicsviewzoom.h" namespace LimeReport { @@ -47,24 +48,6 @@ class ReportEnginePrivate; class DataBrowser; class ReportDesignWindow; - -class GraphicsViewZoom : public QObject { - Q_OBJECT -public: - GraphicsViewZoom(QGraphicsView* view); - void gentleZoom(double factor); - void setModifiers(Qt::KeyboardModifiers modifiers); - void setZoomFactorBase(double value); -private: - QGraphicsView* m_view; - Qt::KeyboardModifiers m_modifiers; - double m_zoomFactorBase; - QPointF m_targetScenePos, m_targetViewportPos; - bool eventFilter(QObject* object, QEvent* event); -signals: - void zoomed(); -}; - class ReportDesignWidget : public QWidget { Q_OBJECT @@ -158,7 +141,7 @@ private: ReportEnginePrivate* m_report; QGraphicsView *m_view; QMainWindow *m_mainWindow; - GraphicsViewZoom* m_zoomer; + GraphicsViewZoomer* m_zoomer; QFont m_defaultFont; int m_verticalGridStep; int m_horizontalGridStep;