Hi, Oliver:
I send rework patch. pls, see.
that patch is really impressive! I was focused on doing a zoom by a zoom rectangle. But of course, using the same method like on the map is the way to go.

Just my 2 cents if you haven't thought of it anyways:

* use flipMouseWheel() form CResources to use the same mouse wheel settings like on the map.
fixed
* use CPlot::rectGraphArea and CPlot::rectIconArea as clipping rectangles (QPainter::setClipRect(..))
fixed
* add a menu entry to reset zoom.
fixed
* what about a small bar (line) at the x and y axis to show the total position of the current viewport within the track? Similar to a scrollbar but thiner and more slick.
add later.
* the y axis should lock to 0. At least if there is no value below 0. And maybe lock to the max value, too.
lock min and max value, lock to 0 will be fixed later.

diff --git a/src/CPlot.cpp b/src/CPlot.cpp
index 2795bca..6043788 100644
--- a/src/CPlot.cpp
+++ b/src/CPlot.cpp
@@ -37,6 +37,7 @@ CPlot::CPlot(QWidget * parent)
 {
     setSizePolicy(QSizePolicy::MinimumExpanding,QSizePolicy::MinimumExpanding);
     m_pData = new CPlotData(this);
+    createActions();
 }
 
 
@@ -72,6 +73,10 @@ void CPlot::setXLabel(const QString& str)
     update();
 }
 
+void CPlot::setLimits()
+{
+    m_pData->setLimits();
+}
 
 void CPlot::newLine(const QPolygonF& line, const QPointF& focus, const 
QString& label)
 {
@@ -84,7 +89,6 @@ void CPlot::newLine(const QPolygonF& line, const QPointF& 
focus, const QString&
     l.label     = label;
 
     m_pData->lines << l;
-    m_pData->setLimits();
     setSizes();
     m_pData->x().setScale( rectGraphArea.width() );
     m_pData->y().setScale( rectGraphArea.height() );
@@ -99,7 +103,6 @@ void CPlot::addLine(const QPolygonF& line, const QString& 
label)
     l.label     = label;
 
     m_pData->lines << l;
-    m_pData->setLimits();
     setSizes();
     m_pData->x().setScale( rectGraphArea.width() );
     m_pData->y().setScale( rectGraphArea.height() );
@@ -126,8 +129,6 @@ void CPlot::paintEvent(QPaintEvent * )
 void CPlot::resizeEvent(QResizeEvent * )
 {
     setSizes();
-    m_pData->x().setScale( rectGraphArea.width() );
-    m_pData->y().setScale( rectGraphArea.height() );
     update();
 }
 
@@ -246,8 +247,10 @@ void CPlot::draw(QPainter& p)
     if(m_pData->lines.isEmpty()) return;
 
     p.setFont(CResources::self().getMapFont());
-
+    p.setClipping(true);
+    p.setClipRect(rectGraphArea);
     drawData(p);
+    p.setClipping(false);
     drawLabels(p);
     drawXScale(p);
     drawYScale(p);
@@ -549,3 +552,83 @@ void CPlot::drawTags(QPainter& p)
         ++tag;
     }
 }
+
+void CPlot::contextMenuEvent(QContextMenuEvent *event)
+{
+    QMenu menu(this);
+    menu.addAction(hZoomAct);
+    menu.addAction(vZoomAct);
+    menu.addAction(resetZoomAct);
+
+    menu.exec(event->globalPos());
+}
+
+void CPlot::createActions()
+{
+    hZoomAct = new QAction("horizontal zoom", this);
+    hZoomAct->setCheckable(true);
+    hZoomAct->setChecked(true);
+
+    vZoomAct = new QAction(tr("vertical zoom"), this);
+    vZoomAct->setCheckable(true);
+    vZoomAct->setChecked(true);
+
+    resetZoomAct = new QAction(tr("reset zoom"), this);
+    connect(resetZoomAct, SIGNAL(triggered()), this, SLOT(resetZoom()));
+}
+
+void CPlot::resetZoom()
+{
+    m_pData->x().resetZoom();
+    m_pData->y().resetZoom();
+    setSizes();
+    update();
+}
+
+void CPlot::zoom(CPlotAxis &axis, bool in, int curInt)
+{
+    axis.zoom(in, curInt);
+    setSizes();
+    m_pData->x().setScale( rectGraphArea.width() );
+    m_pData->y().setScale( rectGraphArea.height() );
+    update();
+}
+
+void CPlot::wheelEvent ( QWheelEvent * e )
+{
+    bool in = CResources::self().flipMouseWheel() ? (e->delta() > 0) : 
(e->delta() < 0);
+    if (hZoomAct->isChecked())
+        zoom(m_pData->x(), in, e->pos().x() - left);
+    if (vZoomAct->isChecked())
+        zoom(m_pData->y(), in, - e->pos().y() + bottom);
+}
+
+void CPlot::mouseMoveEvent(QMouseEvent * e)
+{
+    checkClick = false;
+
+    CPlotAxis &xaxis = m_pData->x();
+    xaxis.move(startMovePos.x() - e->pos().x());
+
+    CPlotAxis &yaxis = m_pData->y();
+    yaxis.move(e->pos().y() - startMovePos.y());
+
+    startMovePos = e->pos();
+    update();
+}
+
+void CPlot::mouseReleaseEvent(QMouseEvent * e)
+{
+    if (checkClick && e->button() == Qt::LeftButton) {
+        QPoint pos = e->pos();
+        double dist = getXValByPixel(pos.x());
+        emit activePointSignal(dist);
+    }
+}
+
+void CPlot::mousePressEvent(QMouseEvent * e)
+{
+    startMovePos = e->pos();
+    checkClick = true;
+}
+
diff --git a/src/CPlot.h b/src/CPlot.h
index e1bc63d..c1a02a5 100644
--- a/src/CPlot.h
+++ b/src/CPlot.h
@@ -38,12 +38,21 @@ class CPlot : public QWidget
         void addLine(const QPolygonF& line, const QString& label);
         void newMarks(const QPolygonF& line);
         void addTag(CPlotData::point_t& tag);
+        void setLimits();
 
         void clear();
 
         double getXValByPixel(int px);
 
+    signals:
+        void activePointSignal(double dist);
+
     protected:
+        void contextMenuEvent(QContextMenuEvent *event);
+        void mousePressEvent(QMouseEvent * e);
+        void mouseMoveEvent(QMouseEvent * e);
+        void mouseReleaseEvent(QMouseEvent * e);
+        void wheelEvent ( QWheelEvent * e );
         void paintEvent(QPaintEvent * e);
         void resizeEvent(QResizeEvent * e);
 
@@ -66,6 +75,12 @@ class CPlot : public QWidget
         void setSizeXLabel();
         void setSizeYLabel();
         void setSizeDrawArea();
+        void zoom(CPlotAxis &axis, bool in, int cur = 0);
+
+        void createActions();
+        QAction *hZoomAct;
+        QAction *vZoomAct;
+        QAction *resetZoomAct;
 
         CPlotData * m_pData;
 
@@ -93,5 +108,9 @@ class CPlot : public QWidget
 
         QFontMetrics fm;
 
+        QPoint startMovePos;
+        int checkClick;
+    public slots:
+        void resetZoom();
 };
 #endif                           //CPLOT_H
diff --git a/src/CPlotAxis.cpp b/src/CPlotAxis.cpp
index 6a4736a..9e9c78b 100644
--- a/src/CPlotAxis.cpp
+++ b/src/CPlotAxis.cpp
@@ -26,10 +26,8 @@
 CPlotAxis::CPlotAxis( QObject * parent )
 : QObject( parent )
 , initialized( false )
-, autoscale( true )
+, autoscale( false )
 , scale( 1.0 )
-, given_min( 0.0 )
-, given_max( 0.0 )
 , used_min( 0.0 )
 , used_max( 0.0 )
 , interval( 0.0 )
@@ -44,12 +42,15 @@ CPlotAxis::CPlotAxis( QObject * parent )
 CPlotAxis::~CPlotAxis()
 {}
 
-void CPlotAxis::setMinMax( double min, double max )
+void CPlotAxis::setLimits(double min, double max)
 {
-    double tmp;
+    limit_min = min;
+    limit_max = max;
+}
 
-    given_min = min;
-    given_max = max;
+void CPlotAxis::setMinMax( double given_min, double given_max )
+{
+    double tmp;
 
     if ( given_min == given_max ) {
         if ( given_min != 0.0 ) {
@@ -302,3 +303,33 @@ void CPlotAxis::setScale( const unsigned int pts )
     points = pts;
     scale = pts / ( used_max - used_min );
 }
+
+void CPlotAxis::resetZoom()
+{
+    setMinMax(limit_min, limit_max);
+}
+
+void CPlotAxis::zoom(bool in, int point)
+{
+    double min, p, d, factor;
+    if (in)
+        factor = 1/1.1;
+    else
+    factor = 1.1;
+
+    p = pt2val(point);
+    min = (p - used_min) * (1 - factor) + used_min;
+    d = min - used_min * factor;
+    setMinMax(min, used_max * factor + d);
+}
+
+void CPlotAxis::move(int delta_pt)
+{
+    double delta_val = pt2val(delta_pt) - pt2val(0);
+    bool f = ! (used_max - used_min < limit_max - limit_min);
+    if (f ^ (used_min + delta_val < limit_min))
+        delta_val = (limit_min - used_min);
+    if (f ^ (used_max + delta_val > limit_max))
+        delta_val = (limit_max - used_max);
+    setMinMax(used_min + delta_val, used_max + delta_val);
+}
diff --git a/src/CPlotAxis.h b/src/CPlotAxis.h
index 9c05da3..56cf4b8 100644
--- a/src/CPlotAxis.h
+++ b/src/CPlotAxis.h
@@ -47,8 +47,16 @@ class CPlotAxis : public QObject
             full                 /**< minmax && norm*/
         };
 
+        ///zoom in/out with a given point as static
+        virtual void zoom(bool in, int point);
+        ///set the desired minimum and maximum value equal to limit values
+        virtual void resetZoom();
+        ///add delta_pt to min and max values
+        virtual void move(int delta_pt);
         ///set the desired minimum and maximum value
         virtual void setMinMax(double min, double max);
+        ///set the limit minimum and maximum value
+        virtual void setLimits(double min, double max);
         ///set the scale factor for a given size in points
         virtual void setScale(const unsigned int pts);
         ///calculate format for the given value
@@ -94,16 +102,14 @@ class CPlotAxis : public QObject
         ///scalefactor
         double scale;
 
-        ///the user applied min value
-        double given_min;
-        ///the user applied max value
-        double given_max;
-
         ///the actual applied min value
         double used_min;
         ///the actual applied max value
         double used_max;
 
+        double limit_min;
+        double limit_max;
+
         ///the intervall of the ticmarks
         double interval;
 
diff --git a/src/CPlotData.cpp b/src/CPlotData.cpp
index 1a39d7e..c0b6450 100644
--- a/src/CPlotData.cpp
+++ b/src/CPlotData.cpp
@@ -61,6 +61,6 @@ void CPlotData::setLimits()
         ++line;
     }
 
-    xaxis->setMinMax(xmin,xmax);
-    yaxis->setMinMax(ymin,ymax);
+    xaxis->setLimits(xmin,xmax);
+    yaxis->setLimits(ymin,ymax);
 }
diff --git a/src/CTrackStatProfileWidget.cpp b/src/CTrackStatProfileWidget.cpp
index 483c079..1bc9e21 100644
--- a/src/CTrackStatProfileWidget.cpp
+++ b/src/CTrackStatProfileWidget.cpp
@@ -39,7 +39,8 @@ CTrackStatProfileWidget::CTrackStatProfileWidget(QWidget * 
parent)
     connect(&CWptDB::self(),SIGNAL(sigChanged()),this,SLOT(slotChanged()));
 
     slotChanged();
-
+    plot->setLimits();
+    plot->resetZoom();
 }
 
 CTrackStatProfileWidget::~CTrackStatProfileWidget()
diff --git a/src/CTrackStatSpeedWidget.cpp b/src/CTrackStatSpeedWidget.cpp
index 0db3481..9d799c0 100644
--- a/src/CTrackStatSpeedWidget.cpp
+++ b/src/CTrackStatSpeedWidget.cpp
@@ -34,7 +34,8 @@ CTrackStatSpeedWidget::CTrackStatSpeedWidget(QWidget * parent)
     connect(&CTrackDB::self(),SIGNAL(sigChanged()),this,SLOT(slotChanged()));
 
     slotChanged();
-
+    plot->setLimits();
+    plot->resetZoom();
 }
 
 CTrackStatSpeedWidget::~CTrackStatSpeedWidget()
diff --git a/src/ITrackStat.cpp b/src/ITrackStat.cpp
index 79a93e5..33774a5 100644
--- a/src/ITrackStat.cpp
+++ b/src/ITrackStat.cpp
@@ -39,7 +39,8 @@ ITrackStat::ITrackStat(QWidget * parent)
 
     plot = new CPlot(this);
     layout()->addWidget(plot);
-
+    QObject::connect(plot, SIGNAL(activePointSignal(double)),
+                                this, SLOT(activePointEvent(double)));
 
 }
 
@@ -48,32 +49,26 @@ ITrackStat::~ITrackStat()
 
 }
 
-void ITrackStat::mousePressEvent(QMouseEvent * e)
+void ITrackStat::activePointEvent(double dist)
 {
+    qDebug() << "ITrackStat::activePointEvent" << endl;
     if(track.isNull()) return;
+    if(plot == 0) return;
+    QList<CTrack::pt_t>& trkpts = track->getTrackPoints();
+    QList<CTrack::pt_t>::const_iterator trkpt = trkpts.begin();
+    quint32 idx = 0;
+    while(trkpt != trkpts.end()) {
+        if(trkpt->flags & CTrack::pt_t::eDeleted) {
+            ++trkpt; continue;
+        }
 
-    if(e->button() == Qt::LeftButton) {
-        QPoint pos = e->pos();
-
-        if(plot == 0) return;
-
-        double dist = plot->getXValByPixel(pos.x() - SPACING);
-        QList<CTrack::pt_t>& trkpts = track->getTrackPoints();
-        QList<CTrack::pt_t>::const_iterator trkpt = trkpts.begin();
-        quint32 idx = 0;
-        while(trkpt != trkpts.end()) {
-            if(trkpt->flags & CTrack::pt_t::eDeleted) {
-                ++trkpt; continue;
-            }
-
-            if(dist < trkpt->distance) {
-                track->setPointOfFocus(idx);
-                break;
-            }
-            idx = trkpt->idx;
-
-            ++trkpt;
+        if(dist < trkpt->distance) {
+            track->setPointOfFocus(idx);
+            break;
         }
+        idx = trkpt->idx;
+
+        ++trkpt;
     }
 }
 
diff --git a/src/ITrackStat.h b/src/ITrackStat.h
index ce1710d..ce08f48 100644
--- a/src/ITrackStat.h
+++ b/src/ITrackStat.h
@@ -46,12 +46,12 @@ class ITrackStat : public QWidget, private 
Ui::ITrackStatWidget
             CTrack::pt_t trkpt;
         };
 
-        void mousePressEvent(QMouseEvent * e);
-
         void addWptTags(QVector<wpt_t>& wpts);
 
         CPlot * plot;
         QPointer<CTrack> track;
+    protected slots:
+        void activePointEvent(double x);
 };
 
 #endif //ITRACKSTAT_H
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
QLandkarte-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/qlandkarte-users

Reply via email to