]> sigrok.org Git - pulseview.git/commitdiff
Implemented zoom-fit
authorJoel Holdsworth <redacted>
Mon, 2 Dec 2013 21:03:14 +0000 (21:03 +0000)
committerJoel Holdsworth <redacted>
Mon, 2 Dec 2013 21:06:31 +0000 (21:06 +0000)
pulseview.qrc
pv/mainwindow.cpp
pv/mainwindow.h
pv/view/view.cpp
pv/view/view.h

index a7108b48b278ea93a86c4b66aa851e580a4b6a71..6830e00d66e3780df898d0464e9140f2edf5d269 100644 (file)
@@ -15,6 +15,7 @@
        <file>icons/trigger-low.svg</file>
        <file>icons/trigger-none.svg</file>
        <file>icons/trigger-rising.svg</file>
+       <file>icons/zoom-fit.png</file>
        <file>icons/zoom-in.png</file>
        <file>icons/zoom-out.png</file>
     </qresource>
index 45e804db7e6db33fcc1e911688b9cd1dd7e29b44..5485ea75b6e0fe49c329fb7bde00da944bf80d8b 100644 (file)
@@ -160,6 +160,16 @@ void MainWindow::setup_ui()
                QString::fromUtf8("actionViewZoomOut"));
        menu_view->addAction(action_view_zoom_out);
 
+       QAction *const action_view_zoom_fit = new QAction(this);
+       action_view_zoom_fit->setText(QApplication::translate(
+               "MainWindow", "Zoom to &Fit", 0, QApplication::UnicodeUTF8));
+       action_view_zoom_fit->setIcon(QIcon::fromTheme("zoom-fit",
+               QIcon(":/icons/zoom-fit.png")));
+       action_view_zoom_fit->setShortcut(QKeySequence(Qt::Key_F));
+       action_view_zoom_fit->setObjectName(
+               QString::fromUtf8("actionViewZoomFit"));
+       menu_view->addAction(action_view_zoom_fit);
+
        menu_view->addSeparator();
 
        QAction *action_view_show_cursors = new QAction(this);
@@ -211,6 +221,7 @@ void MainWindow::setup_ui()
        toolbar->addSeparator();
        toolbar->addAction(action_view_zoom_in);
        toolbar->addAction(action_view_zoom_out);
+       toolbar->addAction(action_view_zoom_fit);
        addToolBar(toolbar);
 
        // Setup the sampling bar
@@ -329,6 +340,11 @@ void MainWindow::on_actionViewZoomOut_triggered()
        _view->zoom(-1);
 }
 
+void MainWindow::on_actionViewZoomFit_triggered()
+{
+       _view->zoom_fit();
+}
+
 void MainWindow::on_actionViewShowCursors_triggered()
 {
        assert(_view);
index 88f62729e644931a3ee5a1291011901d6f34c5db..8895a5e7341242099557ba96782fc2cdd6532fc4 100644 (file)
@@ -89,6 +89,8 @@ private slots:
 
        void on_actionViewZoomOut_triggered();
 
+       void on_actionViewZoomFit_triggered();
+
        void on_actionViewShowCursors_triggered();
 
        void on_actionAbout_triggered();
index 4cfb39783319a258732b28e14a34b5f884d3b17f..abf522c0dfbc39d11d43988cb08bdab0d46d4812 100644 (file)
@@ -24,6 +24,8 @@
 #include <limits.h>
 #include <math.h>
 
+#include <set>
+
 #include <boost/foreach.hpp>
 
 #include <QEvent>
@@ -153,6 +155,43 @@ void View::zoom(double steps, int offset)
        set_scale_offset(new_scale, new_offset);
 }
 
+void View::zoom_fit()
+{
+       using pv::data::SignalData;
+
+       const vector< shared_ptr<Signal> > sigs(
+               session().get_signals());
+
+       // Make a set of all the visible data objects
+       set< shared_ptr<SignalData> > visible_data;
+       BOOST_FOREACH(const shared_ptr<Signal> sig, sigs)
+               if (sig->enabled())
+                       visible_data.insert(sig->data());
+
+       if (visible_data.empty())
+               return;
+
+       double left_time = DBL_MAX, right_time = DBL_MIN;
+       BOOST_FOREACH(const shared_ptr<SignalData> d, visible_data)
+       {
+               const double start_time = d->get_start_time();
+               left_time = min(left_time, start_time);
+               right_time = max(right_time, start_time +
+                       d->get_max_sample_count() / d->get_samplerate());
+       }
+
+       assert(left_time < right_time);
+       if (right_time - left_time < 1e-12)
+               return;
+
+       assert(_viewport);
+       const int w = _viewport->width();
+       if (w <= 0)
+               return;
+
+       set_scale_offset((right_time - left_time) / w, left_time);      
+}
+
 void View::set_scale_offset(double scale, double offset)
 {
        _scale = scale;
index c25ed78ada3f90a8cab3d6d4a9c7964c83c2a4a9..bc99bdd775ef88c26fc235fef299d5a5eebe6a7a 100644 (file)
@@ -86,6 +86,8 @@ public:
        void zoom(double steps);
        void zoom(double steps, int offset);
 
+       void zoom_fit();
+
        /**
         * Sets the scale and offset.
         * @param scale The new view scale in seconds per pixel.