Changeset - 54a8f1ad64ce
[Not reviewed]
Merge default
0 4 4
Hasan Yavuz ÖZDERYA - 9 years ago 2017-01-30 04:15:51
hy@ozderya.net
Merge with scrollbar
8 files changed with 833 insertions and 13 deletions:
0 comments (0 inline, 0 general)
CMakeLists.txt
Show inline comments
 
#
 
# Copyright © 2015-2016 Hasan Yavuz Özderya
 
#
 
# This file is part of serialplot.
 
#
 
# serialplot is free software: you can redistribute it and/or modify
 
# it under the terms of the GNU General Public License as published by
 
# the Free Software Foundation, either version 3 of the License, or
 
# (at your option) any later version.
 
#
 
# serialplot is distributed in the hope that it will be useful,
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
# GNU General Public License for more details.
 
#
 
# You should have received a copy of the GNU General Public License
 
# along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
#
 

	
 
cmake_minimum_required(VERSION 2.8.11)
 

	
 
project(serialplot)
 

	
 
set(PROGRAM_NAME ${CMAKE_PROJECT_NAME} CACHE STRING "Output program name")
 
set(PROGRAM_DISPLAY_NAME "SerialPlot" CACHE STRING "Display name (menus etc) of the program")
 

	
 
# Find includes in corresponding build directories
 
set(CMAKE_INCLUDE_CURRENT_DIR ON)
 

	
 
# Instruct CMake to run moc automatically when needed.
 
set(CMAKE_AUTOMOC ON)
 

	
 
# add local path for cmake modules
 
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/")
 

	
 
# Find the QtWidgets library
 
find_package(Qt5Widgets)
 

	
 
# If set, cmake will download Qwt over SVN, build and use it as a static library.
 
set(BUILD_QWT true CACHE BOOL "Download and build Qwt automatically.")
 
# Find QWT or use static manually provided by user
 
set(QWT_USE_STATIC false CACHE BOOL "Use a static version of Qwt provided by user.")
 
set(QWT_STATIC_LIBRARY "" CACHE FILEPATH "Path to the static Qwt library, libqwt.a.")
 
set(QWT_STATIC_INCLUDE "" CACHE PATH "Path to the Qwt include directory when building Qwt static.")
 

	
 
if (BUILD_QWT)
 
  include(BuildQwt)
 
else (BUILD_QWT)
 
  if (QWT_USE_STATIC)
 
	set(QWT_LIBRARY ${QWT_STATIC_LIBRARY})
 
	set(QWT_INCLUDE_DIR ${QWT_STATIC_INCLUDE})
 
  else (QWT_USE_STATIC)
 
	find_package(Qwt 6.1 REQUIRED)
 
  endif (QWT_USE_STATIC)
 
endif (BUILD_QWT)
 

	
 
include(BuildQColorWidgets)
 

	
 
# includes
 
include_directories("./src" ${QWT_INCLUDE_DIR} ${QCW_INCLUDE_DIR})
 

	
 
# wrap UI and resource files
 
qt5_wrap_ui(UI_FILES
 
  src/mainwindow.ui
 
  src/portcontrol.ui
 
  src/about_dialog.ui
 
  src/snapshotview.ui
 
  src/commandpanel.ui
 
  src/commandwidget.ui
 
  src/dataformatpanel.ui
 
  src/plotcontrolpanel.ui
 
  src/numberformatbox.ui
 
  src/endiannessbox.ui
 
  src/binarystreamreadersettings.ui
 
  src/asciireadersettings.ui
 
  src/framedreadersettings.ui
 
  )
 

	
 
if (WIN32)
 
  qt5_add_resources(RES_FILES misc/icons.qrc misc/winicons.qrc)
 
else (WIN32)
 
  qt5_add_resources(RES_FILES misc/icons.qrc)
 
endif (WIN32)
 

	
 
add_executable(${PROGRAM_NAME} WIN32
 
  src/main.cpp
 
  src/mainwindow.cpp
 
  src/portcontrol.cpp
 
  src/plot.cpp
 
  src/zoomer.cpp
 
  src/scrollzoomer.cpp
 
  src/scrollbar.cpp
 
  src/hidabletabwidget.cpp
 
  src/framebuffer.cpp
 
  src/scalepicker.cpp
 
  src/scalezoomer.cpp
 
  src/portlist.cpp
 
  src/snapshot.cpp
 
  src/snapshotview.cpp
 
  src/snapshotmanager.cpp
 
  src/plotsnapshotoverlay.cpp
 
  src/commandpanel.cpp
 
  src/commandwidget.cpp
 
  src/commandedit.cpp
 
  src/dataformatpanel.cpp
 
  src/plotcontrolpanel.cpp
 
  src/tooltipfilter.cpp
 
  src/sneakylineedit.cpp
 
  src/channelmanager.cpp
 
  src/channelinfomodel.cpp
 
  src/framebufferseries.cpp
 
  src/numberformatbox.cpp
 
  src/endiannessbox.cpp
 
  src/abstractreader.cpp
 
  src/binarystreamreader.cpp
 
  src/binarystreamreadersettings.cpp
 
  src/asciireader.cpp
 
  src/asciireadersettings.cpp
 
  src/demoreader.cpp
 
  src/framedreader.cpp
 
  src/framedreadersettings.cpp
 
  src/plotmanager.cpp
 
  src/numberformat.cpp
 
  misc/windows_icon.rc
 
  ${UI_FILES}
 
  ${RES_FILES}
 
  )
 

	
 
# Use the Widgets module from Qt 5.
 
target_link_libraries(${PROGRAM_NAME} ${QWT_LIBRARY} ${QCW_LIBRARY})
 
qt5_use_modules(${PROGRAM_NAME} Widgets SerialPort)
 

	
 
if (BUILD_QWT)
 
  add_dependencies(${PROGRAM_NAME} QWT)
 
endif (BUILD_QWT)
 

	
 
# set compiler flags
 
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
 

	
 
# Enable C++11 support, fail if not supported
 
include(CheckCXXCompilerFlag)
 
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
 
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
 
if(COMPILER_SUPPORTS_CXX11)
 
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
 
elseif(COMPILER_SUPPORTS_CXX0X)
 
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
 
else()
 
  message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
 
endif()
 

	
 
# version number
 
set(MAJOR_VERSION 0 CACHE INT "Program major version number.")
 
set(MINOR_VERSION 8 CACHE INT "Program minor version number.")
 
set(PATCH_VERSION 1 CACHE INT "Program patch version number.")
 
set(VERSION_STRING "${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION}")
 

	
 
# get revision number from mercurial
 
find_program(MERCURIAL hg)
 

	
 
if (MERCURIAL)
 
  execute_process(COMMAND ${MERCURIAL} id -i
 
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
 
    RESULT_VARIABLE MERCURIAL_RESULT
 
    OUTPUT_VARIABLE VERSION_REVISION
 
    OUTPUT_STRIP_TRAILING_WHITESPACE)
 
  if(NOT MERCURIAL_RESULT EQUAL 0)
 
    set(VERSION_SCM_REVISION false)
 
  endif(NOT MERCURIAL_RESULT EQUAL 0)
 
endif (MERCURIAL)
 

	
 
if (NOT VERSION_REVISION)
 
  set(VERSION_REVISION "0")
 
endif (NOT VERSION_REVISION)
 

	
 
message("SCM revision: ${VERSION_REVISION}")
 

	
 
# configure version file
 
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/version.h.in" "${CMAKE_CURRENT_BINARY_DIR}/version.h")
 

	
 
# add make run target
 
add_custom_target(run
 
    COMMAND ${PROGRAM_NAME}
 
    DEPENDS ${PROGRAM_NAME}
 
    WORKING_DIRECTORY ${CMAKE_PROJECT_DIR}
 
)
 

	
 
# installing
src/plot.cpp
Show inline comments
 
/*
 
  Copyright © 2015 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
  serialplot is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  serialplot is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU General Public License for more details.
 

	
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include <QRectF>
 
#include <QKeySequence>
 
#include <QColor>
 
#include <qwt_symbol.h>
 
#include <qwt_plot_curve.h>
 
#include <math.h>
 
#include <algorithm>
 

	
 
#include "plot.h"
 
#include "utils.h"
 

	
 
static const int SYMBOL_SHOW_AT_WIDTH = 5;
 
static const int SYMBOL_SIZE_MAX = 7;
 

	
 
Plot::Plot(QWidget* parent) :
 
    QwtPlot(parent),
 
    zoomer(this->canvas(), false),
 
    sZoomer(this, &zoomer)
 
{
 
    isAutoScaled = true;
 
    symbolSize = 0;
 

	
 
    QObject::connect(&zoomer, &Zoomer::unzoomed, this, &Plot::unzoomed);
 

	
 
    zoomer.setZoomBase();
 
    grid.attach(this);
 
    legend.attach(this);
 

	
 
    showGrid(false);
 
    darkBackground(false);
 

	
 
    snapshotOverlay = NULL;
 

	
 
    connect(&zoomer, &QwtPlotZoomer::zoomed,
 
            [this](const QRectF &rect)
 
            {
 
                onXScaleChanged();
 
            });
 

	
 
    connect(this, &QwtPlot::itemAttached,
 
            [this](QwtPlotItem *plotItem, bool on)
 
            {
 
                if (symbolSize) updateSymbols();
 
            });
 

	
 
    // init demo indicator
 
    QwtText demoText(" DEMO RUNNING ");  // looks better with spaces
 
    demoText.setColor(QColor("white"));
 
    demoText.setBackgroundBrush(Qt::darkRed);
 
    demoText.setBorderRadius(4);
 
    demoText.setRenderFlags(Qt::AlignLeft | Qt::AlignTop);
 
    demoText.setRenderFlags(Qt::AlignLeft | Qt::AlignBottom);
 
    demoIndicator.setText(demoText);
 
    demoIndicator.hide();
 
    demoIndicator.attach(this);
 
}
 

	
 
Plot::~Plot()
 
{
 
    if (snapshotOverlay != NULL) delete snapshotOverlay;
 
}
 

	
 
void Plot::setAxis(bool autoScaled, double yAxisMin, double yAxisMax)
 
{
 
    this->isAutoScaled = autoScaled;
 

	
 
    if (!autoScaled)
 
    {
 
        yMin = yAxisMin;
 
        yMax = yAxisMax;
 
    }
 

	
 
    zoomer.zoom(0);
 
    resetAxes();
 
}
 

	
 
void Plot::resetAxes()
 
{
 
    if (isAutoScaled)
 
    {
 
        setAxisAutoScale(QwtPlot::yLeft);
 
    }
 
    else
 
    {
 
        setAxisScale(QwtPlot::yLeft, yMin, yMax);
 
    }
 

	
 
    replot();
 
}
 

	
 
void Plot::unzoomed()
 
{
 
    setAxisAutoScale(QwtPlot::xBottom);
 
    resetAxes();
 
    onXScaleChanged();
 
}
 

	
 
void Plot::showGrid(bool show)
 
{
 
    grid.enableX(show);
 
    grid.enableY(show);
 
    replot();
 
}
 

	
 
void Plot::showMinorGrid(bool show)
 
{
 
    grid.enableXMin(show);
 
    grid.enableYMin(show);
 
    replot();
 
}
 

	
 
void Plot::showLegend(bool show)
 
{
 
    legend.setVisible(show);
 
    replot();
 
}
 

	
 
void Plot::showDemoIndicator(bool show)
 
{
 
    demoIndicator.setVisible(show);
 
    replot();
 
}
 

	
 
void Plot::unzoom()
 
{
 
    zoomer.zoom(0);
 
}
 

	
 
void Plot::darkBackground(bool enabled)
 
{
 
    QColor gridColor;
 
    if (enabled)
 
    {
 
        setCanvasBackground(QBrush(Qt::black));
 
        gridColor.setHsvF(0, 0, 0.25);
 
        grid.setPen(gridColor);
 
        zoomer.setRubberBandPen(QPen(Qt::white));
 
        zoomer.setTrackerPen(QPen(Qt::white));
 
        sZoomer.setPickerPen(QPen(Qt::white));
 
        legend.setTextPen(QPen(Qt::white));
 
    }
 
    else
 
    {
 
        setCanvasBackground(QBrush(Qt::white));
 
        gridColor.setHsvF(0, 0, 0.80);
 
        grid.setPen(gridColor);
 
        zoomer.setRubberBandPen(QPen(Qt::black));
 
        zoomer.setTrackerPen(QPen(Qt::black));
src/scrollbar.cpp
Show inline comments
 
new file 100644
 
/*
 
  Copyright © 2014 Uwe Rathmann
 

	
 
  This file is copied from Qwt project; you can redistribute it and/or modify it
 
  under the terms of the Qwt License, Version 1.0. You can obtain the original
 
  source code and the details of the Qwt License from the Qwt website:
 
  http://qwt.sourceforge.net/
 
*/
 

	
 
#include <qstyle.h>
 
#include <qstyleoption.h>
 
#include "scrollbar.h"
 

	
 
ScrollBar::ScrollBar( QWidget * parent ):
 
    QScrollBar( parent )
 
{
 
    init();
 
}
 

	
 
ScrollBar::ScrollBar( Qt::Orientation o,
 
        QWidget *parent ):
 
    QScrollBar( o, parent )
 
{
 
    init();
 
}
 

	
 
ScrollBar::ScrollBar( double minBase, double maxBase,
 
        Qt::Orientation o, QWidget *parent ):
 
    QScrollBar( o, parent )
 
{
 
    init();
 
    setBase( minBase, maxBase );
 
    moveSlider( minBase, maxBase );
 
}
 

	
 
void ScrollBar::init()
 
{
 
    d_inverted = orientation() == Qt::Vertical;
 
    d_baseTicks = 1000000;
 
    d_minBase = 0.0;
 
    d_maxBase = 1.0;
 
    moveSlider( d_minBase, d_maxBase );
 

	
 
    connect( this, SIGNAL( sliderMoved( int ) ), SLOT( catchSliderMoved( int ) ) );
 
    connect( this, SIGNAL( valueChanged( int ) ), SLOT( catchValueChanged( int ) ) );
 
}
 

	
 
void ScrollBar::setInverted( bool inverted )
 
{
 
    if ( d_inverted != inverted )
 
    {
 
        d_inverted = inverted;
 
        moveSlider( minSliderValue(), maxSliderValue() );
 
    }
 
}
 

	
 
bool ScrollBar::isInverted() const
 
{
 
    return d_inverted;
 
}
 

	
 
void ScrollBar::setBase( double min, double max )
 
{
 
    if ( min != d_minBase || max != d_maxBase )
 
    {
 
        d_minBase = min;
 
        d_maxBase = max;
 

	
 
        moveSlider( minSliderValue(), maxSliderValue() );
 
    }
 
}
 

	
 
void ScrollBar::moveSlider( double min, double max )
 
{
 
    const int sliderTicks = qRound( ( max - min ) /
 
        ( d_maxBase - d_minBase ) * d_baseTicks );
 

	
 
    // setRange initiates a valueChanged of the scrollbars
 
    // in some situations. So we block
 
    // and unblock the signals.
 

	
 
    blockSignals( true );
 

	
 
    setRange( sliderTicks / 2, d_baseTicks - sliderTicks / 2 );
 
    int steps = sliderTicks / 200;
 
    if ( steps <= 0 )
 
        steps = 1;
 

	
 
    setSingleStep( steps );
 
    setPageStep( sliderTicks );
 

	
 
    int tick = mapToTick( min + ( max - min ) / 2 );
 
    if ( isInverted() )
 
        tick = d_baseTicks - tick;
 

	
 
    setSliderPosition( tick );
 
    blockSignals( false );
 
}
 

	
 
double ScrollBar::minBaseValue() const
 
{
 
    return d_minBase;
 
}
 

	
 
double ScrollBar::maxBaseValue() const
 
{
 
    return d_maxBase;
 
}
 

	
 
void ScrollBar::sliderRange( int value, double &min, double &max ) const
 
{
 
    if ( isInverted() )
 
        value = d_baseTicks - value;
 

	
 
    const int visibleTicks = pageStep();
 

	
 
    min = mapFromTick( value - visibleTicks / 2 );
 
    max = mapFromTick( value + visibleTicks / 2 );
 
}
 

	
 
double ScrollBar::minSliderValue() const
 
{
 
    double min, dummy;
 
    sliderRange( value(), min, dummy );
 

	
 
    return min;
 
}
 

	
 
double ScrollBar::maxSliderValue() const
 
{
 
    double max, dummy;
 
    sliderRange( value(), dummy, max );
 

	
 
    return max;
 
}
 

	
 
int ScrollBar::mapToTick( double v ) const
 
{
 
    const double pos = ( v - d_minBase ) / ( d_maxBase - d_minBase ) * d_baseTicks;
 
    return static_cast<int>( pos );
 
}
 

	
 
double ScrollBar::mapFromTick( int tick ) const
 
{
 
    return d_minBase + ( d_maxBase - d_minBase ) * tick / d_baseTicks;
 
}
 

	
 
void ScrollBar::catchValueChanged( int value )
 
{
 
    double min, max;
 
    sliderRange( value, min, max );
 
    Q_EMIT valueChanged( orientation(), min, max );
 
}
 

	
 
void ScrollBar::catchSliderMoved( int value )
 
{
 
    double min, max;
 
    sliderRange( value, min, max );
 
    Q_EMIT sliderMoved( orientation(), min, max );
 
}
 

	
 
int ScrollBar::extent() const
 
{
 
    QStyleOptionSlider opt;
 
    opt.init( this );
 
    opt.subControls = QStyle::SC_None;
 
    opt.activeSubControls = QStyle::SC_None;
 
    opt.orientation = orientation();
 
    opt.minimum = minimum();
 
    opt.maximum = maximum();
 
    opt.sliderPosition = sliderPosition();
 
    opt.sliderValue = value();
 
    opt.singleStep = singleStep();
 
    opt.pageStep = pageStep();
 
    opt.upsideDown = invertedAppearance();
 
    if ( orientation() == Qt::Horizontal )
 
        opt.state |= QStyle::State_Horizontal;
 
    return style()->pixelMetric( QStyle::PM_ScrollBarExtent, &opt, this );
 
}
src/scrollbar.h
Show inline comments
 
new file 100644
 
/*
 
  Copyright © 2014 Uwe Rathmann
 

	
 
  This file is copied from Qwt project; you can redistribute it and/or modify it
 
  under the terms of the Qwt License, Version 1.0. You can obtain the original
 
  source code and the details of the Qwt License from the Qwt website:
 
  http://qwt.sourceforge.net/
 
*/
 

	
 
#ifndef _SCROLLBAR_H
 
#define _SCROLLBAR_H 1
 

	
 
#include <qscrollbar.h>
 

	
 
class ScrollBar: public QScrollBar
 
{
 
    Q_OBJECT
 

	
 
public:
 
    ScrollBar( QWidget *parent = NULL );
 
    ScrollBar( Qt::Orientation, QWidget *parent = NULL );
 
    ScrollBar( double minBase, double maxBase,
 
        Qt::Orientation o, QWidget *parent = NULL );
 

	
 
    void setInverted( bool );
 
    bool isInverted() const;
 

	
 
    double minBaseValue() const;
 
    double maxBaseValue() const;
 

	
 
    double minSliderValue() const;
 
    double maxSliderValue() const;
 

	
 
    int extent() const;
 

	
 
Q_SIGNALS:
 
    void sliderMoved( Qt::Orientation, double, double );
 
    void valueChanged( Qt::Orientation, double, double );
 

	
 
public Q_SLOTS:
 
    virtual void setBase( double min, double max );
 
    virtual void moveSlider( double min, double max );
 

	
 
protected:
 
    void sliderRange( int value, double &min, double &max ) const;
 
    int mapToTick( double ) const;
 
    double mapFromTick( int ) const;
 

	
 
private Q_SLOTS:
 
    void catchValueChanged( int value );
 
    void catchSliderMoved( int value );
 

	
 
private:
 
    void init();
 

	
 
    bool d_inverted;
 
    double d_minBase;
 
    double d_maxBase;
 
    int d_baseTicks;
 
};
 

	
 
#endif
src/scrollzoomer.cpp
Show inline comments
 
new file 100644
 
/*
 
  Copyright © 2014 Uwe Rathmann
 

	
 
  This file is copied from Qwt project; you can redistribute it and/or modify it
 
  under the terms of the Qwt License, Version 1.0. You can obtain the original
 
  source code and the details of the Qwt License from the Qwt website:
 
  http://qwt.sourceforge.net/
 
*/
 

	
 
#include <qevent.h>
 
#include <qwt_plot_canvas.h>
 
#include <qwt_plot_layout.h>
 
#include <qwt_scale_engine.h>
 
#include <qwt_scale_widget.h>
 
#include "scrollbar.h"
 
#include "scrollzoomer.h"
 

	
 
class ScrollData
 
{
 
public:
 
    ScrollData():
 
        scrollBar( NULL ),
 
        position( ScrollZoomer::OppositeToScale ),
 
        mode( Qt::ScrollBarAsNeeded )
 
    {
 
    }
 

	
 
    ~ScrollData()
 
    {
 
        delete scrollBar;
 
    }
 

	
 
    ScrollBar *scrollBar;
 
    ScrollZoomer::ScrollBarPosition position;
 
    Qt::ScrollBarPolicy mode;
 
};
 

	
 
ScrollZoomer::ScrollZoomer( QWidget *canvas ):
 
    QwtPlotZoomer( canvas ),
 
    d_cornerWidget( NULL ),
 
    d_hScrollData( NULL ),
 
    d_vScrollData( NULL ),
 
    d_inZoom( false )
 
{
 
    for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
 
        d_alignCanvasToScales[ axis ] = false;
 

	
 
    if ( !canvas )
 
        return;
 

	
 
    d_hScrollData = new ScrollData;
 
    d_vScrollData = new ScrollData;
 
}
 

	
 
ScrollZoomer::~ScrollZoomer()
 
{
 
    delete d_cornerWidget;
 
    delete d_vScrollData;
 
    delete d_hScrollData;
 
}
 

	
 
void ScrollZoomer::rescale()
 
{
 
    QwtScaleWidget *xScale = plot()->axisWidget( xAxis() );
 
    QwtScaleWidget *yScale = plot()->axisWidget( yAxis() );
 

	
 
    if ( zoomRectIndex() <= 0 )
 
    {
 
        if ( d_inZoom )
 
        {
 
            xScale->setMinBorderDist( 0, 0 );
 
            yScale->setMinBorderDist( 0, 0 );
 

	
 
            QwtPlotLayout *layout = plot()->plotLayout();
 

	
 
            for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
 
                layout->setAlignCanvasToScale( axis, d_alignCanvasToScales[ axis ] );
 

	
 
            d_inZoom = false;
 
        }
 
    }
 
    else
 
    {
 
        if ( !d_inZoom )
 
        {
 
            /*
 
             We set a minimum border distance.
 
             Otherwise the canvas size changes when scrolling,
 
             between situations where the major ticks are at
 
             the canvas borders (requiring extra space for the label)
 
             and situations where all labels can be painted below/top
 
             or left/right of the canvas.
 
             */
 
            int start, end;
 

	
 
            xScale->getBorderDistHint( start, end );
 
            xScale->setMinBorderDist( start, end );
 

	
 
            yScale->getBorderDistHint( start, end );
 
            yScale->setMinBorderDist( start, end );
 

	
 
            QwtPlotLayout *layout = plot()->plotLayout();
 
            for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
 
            {
 
                d_alignCanvasToScales[axis] = 
 
                    layout->alignCanvasToScale( axis );
 
            }
 

	
 
            layout->setAlignCanvasToScales( false );
 

	
 
            d_inZoom = true;
 
        }
 
    }
 

	
 
    QwtPlotZoomer::rescale();
 
    updateScrollBars();
 
}
 

	
 
ScrollBar *ScrollZoomer::scrollBar( Qt::Orientation orientation )
 
{
 
    ScrollBar *&sb = ( orientation == Qt::Vertical )
 
        ? d_vScrollData->scrollBar : d_hScrollData->scrollBar;
 

	
 
    if ( sb == NULL )
 
    {
 
        sb = new ScrollBar( orientation, canvas() );
 
        sb->hide();
 
        connect( sb,
 
            SIGNAL( valueChanged( Qt::Orientation, double, double ) ),
 
            SLOT( scrollBarMoved( Qt::Orientation, double, double ) ) );
 
    }
 
    return sb;
 
}
 

	
 
ScrollBar *ScrollZoomer::horizontalScrollBar() const
 
{
 
    return d_hScrollData->scrollBar;
 
}
 

	
 
ScrollBar *ScrollZoomer::verticalScrollBar() const
 
{
 
    return d_vScrollData->scrollBar;
 
}
 

	
 
void ScrollZoomer::setHScrollBarMode( Qt::ScrollBarPolicy mode )
 
{
 
    if ( hScrollBarMode() != mode )
 
    {
 
        d_hScrollData->mode = mode;
 
        updateScrollBars();
 
    }
 
}
 

	
 
void ScrollZoomer::setVScrollBarMode( Qt::ScrollBarPolicy mode )
 
{
 
    if ( vScrollBarMode() != mode )
 
    {
 
        d_vScrollData->mode = mode;
 
        updateScrollBars();
 
    }
 
}
 

	
 
Qt::ScrollBarPolicy ScrollZoomer::hScrollBarMode() const
 
{
 
    return d_hScrollData->mode;
 
}
 

	
 
Qt::ScrollBarPolicy ScrollZoomer::vScrollBarMode() const
 
{
 
    return d_vScrollData->mode;
 
}
 

	
 
void ScrollZoomer::setHScrollBarPosition( ScrollBarPosition pos )
 
{
 
    if ( d_hScrollData->position != pos )
 
    {
 
        d_hScrollData->position = pos;
 
        updateScrollBars();
 
    }
 
}
 

	
 
void ScrollZoomer::setVScrollBarPosition( ScrollBarPosition pos )
 
{
 
    if ( d_vScrollData->position != pos )
 
    {
 
        d_vScrollData->position = pos;
 
        updateScrollBars();
 
    }
 
}
 

	
 
ScrollZoomer::ScrollBarPosition ScrollZoomer::hScrollBarPosition() const
 
{
 
    return d_hScrollData->position;
 
}
 

	
 
ScrollZoomer::ScrollBarPosition ScrollZoomer::vScrollBarPosition() const
 
{
 
    return d_vScrollData->position;
 
}
 

	
 
void ScrollZoomer::setCornerWidget( QWidget *w )
 
{
 
    if ( w != d_cornerWidget )
 
    {
 
        if ( canvas() )
 
        {
 
            delete d_cornerWidget;
 
            d_cornerWidget = w;
 
            if ( d_cornerWidget->parent() != canvas() )
 
                d_cornerWidget->setParent( canvas() );
 

	
 
            updateScrollBars();
 
        }
 
    }
 
}
 

	
 
QWidget *ScrollZoomer::cornerWidget() const
 
{
 
    return d_cornerWidget;
 
}
 

	
 
bool ScrollZoomer::eventFilter( QObject *object, QEvent *event )
 
{
 
    if ( object == canvas() )
 
    {
 
        switch( event->type() )
 
        {
 
            case QEvent::Resize:
 
            {
 
                int left, top, right, bottom;
 
                canvas()->getContentsMargins( &left, &top, &right, &bottom );
 

	
 
                QRect rect;
 
                rect.setSize( static_cast<QResizeEvent *>( event )->size() );
 
                rect.adjust( left, top, -right, -bottom );
 

	
 
                layoutScrollBars( rect );
 
                break;
 
            }
 
            case QEvent::ChildRemoved:
 
            {
 
                const QObject *child =
 
                    static_cast<QChildEvent *>( event )->child();
 

	
 
                if ( child == d_cornerWidget )
 
                    d_cornerWidget = NULL;
 
                else if ( child == d_hScrollData->scrollBar )
 
                    d_hScrollData->scrollBar = NULL;
 
                else if ( child == d_vScrollData->scrollBar )
 
                    d_vScrollData->scrollBar = NULL;
 
                break;
 
            }
 
            default:
 
                break;
 
        }
 
    }
 
    return QwtPlotZoomer::eventFilter( object, event );
 
}
 

	
 
bool ScrollZoomer::needScrollBar( Qt::Orientation orientation ) const
 
{
 
    Qt::ScrollBarPolicy mode;
 
    double zoomMin, zoomMax, baseMin, baseMax;
 

	
 
    if ( orientation == Qt::Horizontal )
 
    {
 
        mode = d_hScrollData->mode;
 
        baseMin = zoomBase().left();
 
        baseMax = zoomBase().right();
 
        zoomMin = zoomRect().left();
 
        zoomMax = zoomRect().right();
 
    }
 
    else
 
    {
 
        mode = d_vScrollData->mode;
 
        baseMin = zoomBase().top();
 
        baseMax = zoomBase().bottom();
 
        zoomMin = zoomRect().top();
 
        zoomMax = zoomRect().bottom();
 
    }
 

	
 
    bool needed = false;
 
    switch( mode )
 
    {
 
        case Qt::ScrollBarAlwaysOn:
 
            needed = true;
 
            break;
 
        case Qt::ScrollBarAlwaysOff:
 
            needed = false;
 
            break;
 
        default:
 
        {
 
            if ( baseMin < zoomMin || baseMax > zoomMax )
 
                needed = true;
 
            break;
 
        }
 
    }
 
    return needed;
 
}
 

	
 
void ScrollZoomer::updateScrollBars()
 
{
 
    if ( !canvas() )
 
        return;
 

	
 
    const int xAxis = QwtPlotZoomer::xAxis();
 
    const int yAxis = QwtPlotZoomer::yAxis();
 

	
 
    int xScrollBarAxis = xAxis;
 
    if ( hScrollBarPosition() == OppositeToScale )
 
        xScrollBarAxis = oppositeAxis( xScrollBarAxis );
 

	
 
    int yScrollBarAxis = yAxis;
 
    if ( vScrollBarPosition() == OppositeToScale )
 
        yScrollBarAxis = oppositeAxis( yScrollBarAxis );
 

	
 

	
 
    QwtPlotLayout *layout = plot()->plotLayout();
 

	
 
    bool showHScrollBar = needScrollBar( Qt::Horizontal );
 
    if ( showHScrollBar )
 
    {
 
        ScrollBar *sb = scrollBar( Qt::Horizontal );
 
        sb->setPalette( plot()->palette() );
 
        sb->setInverted( !plot()->axisScaleDiv( xAxis ).isIncreasing() );
 
        sb->setBase( zoomBase().left(), zoomBase().right() );
 
        sb->moveSlider( zoomRect().left(), zoomRect().right() );
 

	
 
        if ( !sb->isVisibleTo( canvas() ) )
 
        {
 
            sb->show();
 
            layout->setCanvasMargin( layout->canvasMargin( xScrollBarAxis )
 
                + sb->extent(), xScrollBarAxis );
 
        }
 
    }
 
    else
 
    {
 
        if ( horizontalScrollBar() )
 
        {
 
            horizontalScrollBar()->hide();
 
            layout->setCanvasMargin( layout->canvasMargin( xScrollBarAxis )
 
                - horizontalScrollBar()->extent(), xScrollBarAxis );
 
        }
 
    }
 

	
 
    bool showVScrollBar = needScrollBar( Qt::Vertical );
 
    if ( showVScrollBar )
 
    {
 
        ScrollBar *sb = scrollBar( Qt::Vertical );
 
        sb->setPalette( plot()->palette() );
 
        sb->setInverted( plot()->axisScaleDiv( yAxis ).isIncreasing() );
 
        sb->setBase( zoomBase().top(), zoomBase().bottom() );
 
        sb->moveSlider( zoomRect().top(), zoomRect().bottom() );
 

	
 
        if ( !sb->isVisibleTo( canvas() ) )
 
        {
 
            sb->show();
 
            layout->setCanvasMargin( layout->canvasMargin( yScrollBarAxis )
 
                + sb->extent(), yScrollBarAxis );
 
        }
 
    }
 
    else
 
    {
 
        if ( verticalScrollBar() )
 
        {
 
            verticalScrollBar()->hide();
 
            layout->setCanvasMargin( layout->canvasMargin( yScrollBarAxis )
 
                - verticalScrollBar()->extent(), yScrollBarAxis );
 
        }
 
    }
 

	
 
    if ( showHScrollBar && showVScrollBar )
 
    {
 
        if ( d_cornerWidget == NULL )
 
        {
 
            d_cornerWidget = new QWidget( canvas() );
 
            d_cornerWidget->setAutoFillBackground( true );
 
            d_cornerWidget->setPalette( plot()->palette() );
 
        }
 
        d_cornerWidget->show();
 
    }
 
    else
 
    {
 
        if ( d_cornerWidget )
 
            d_cornerWidget->hide();
 
    }
 

	
 
    layoutScrollBars( canvas()->contentsRect() );
 
    plot()->updateLayout();
 
}
 

	
 
void ScrollZoomer::layoutScrollBars( const QRect &rect )
 
{
 
    int hPos = xAxis();
 
    if ( hScrollBarPosition() == OppositeToScale )
 
        hPos = oppositeAxis( hPos );
 

	
 
    int vPos = yAxis();
 
    if ( vScrollBarPosition() == OppositeToScale )
 
        vPos = oppositeAxis( vPos );
 

	
 
    ScrollBar *hScrollBar = horizontalScrollBar();
 
    ScrollBar *vScrollBar = verticalScrollBar();
 

	
 
    const int hdim = hScrollBar ? hScrollBar->extent() : 0;
 
    const int vdim = vScrollBar ? vScrollBar->extent() : 0;
 

	
 
    if ( hScrollBar && hScrollBar->isVisible() )
 
    {
 
        int x = rect.x();
 
        int y = ( hPos == QwtPlot::xTop )
 
            ? rect.top() : rect.bottom() - hdim + 1;
 
        int w = rect.width();
 

	
 
        if ( vScrollBar && vScrollBar->isVisible() )
 
        {
 
            if ( vPos == QwtPlot::yLeft )
 
                x += vdim;
 
            w -= vdim;
 
        }
 

	
 
        hScrollBar->setGeometry( x, y, w, hdim );
 
    }
 
    if ( vScrollBar && vScrollBar->isVisible() )
 
    {
 
        int pos = yAxis();
 
        if ( vScrollBarPosition() == OppositeToScale )
 
            pos = oppositeAxis( pos );
 

	
 
        int x = ( vPos == QwtPlot::yLeft )
 
            ? rect.left() : rect.right() - vdim + 1;
 
        int y = rect.y();
 

	
 
        int h = rect.height();
 

	
 
        if ( hScrollBar && hScrollBar->isVisible() )
 
        {
 
            if ( hPos == QwtPlot::xTop )
 
                y += hdim;
 

	
 
            h -= hdim;
 
        }
 

	
 
        vScrollBar->setGeometry( x, y, vdim, h );
 
    }
 
    if ( hScrollBar && hScrollBar->isVisible() &&
 
        vScrollBar && vScrollBar->isVisible() )
 
    {
 
        if ( d_cornerWidget )
 
        {
 
            QRect cornerRect(
 
                vScrollBar->pos().x(), hScrollBar->pos().y(),
 
                vdim, hdim );
 
            d_cornerWidget->setGeometry( cornerRect );
 
        }
 
    }
 
}
 

	
 
void ScrollZoomer::scrollBarMoved(
 
    Qt::Orientation o, double min, double max )
 
{
 
    Q_UNUSED( max );
 

	
 
    if ( o == Qt::Horizontal )
 
        moveTo( QPointF( min, zoomRect().top() ) );
 
    else
 
        moveTo( QPointF( zoomRect().left(), min ) );
 

	
 
    Q_EMIT zoomed( zoomRect() );
 
}
 

	
 
int ScrollZoomer::oppositeAxis( int axis ) const
 
{
 
    switch( axis )
 
    {
 
        case QwtPlot::xBottom:
 
            return QwtPlot::xTop;
 
        case QwtPlot::xTop:
 
            return QwtPlot::xBottom;
 
        case QwtPlot::yLeft:
 
            return QwtPlot::yRight;
 
        case QwtPlot::yRight:
 
            return QwtPlot::yLeft;
 
        default:
 
            break;
 
    }
 

	
 
    return axis;
 
}
src/scrollzoomer.h
Show inline comments
 
new file 100644
 
/*
 
  Copyright © 2014 Uwe Rathmann
 

	
 
  This file is copied from Qwt project; you can redistribute it and/or modify it
 
  under the terms of the Qwt License, Version 1.0. You can obtain the original
 
  source code and the details of the Qwt License from the Qwt website:
 
  http://qwt.sourceforge.net/
 
*/
 

	
 
#ifndef _SCROLLZOOMER_H
 
#define _SCROLLZOOMER_H
 

	
 
#include <qglobal.h>
 
#include <qwt_plot_zoomer.h>
 
#include <qwt_plot.h>
 

	
 
class ScrollData;
 
class ScrollBar;
 

	
 
class ScrollZoomer: public QwtPlotZoomer
 
{
 
    Q_OBJECT
 
public:
 
    enum ScrollBarPosition
 
    {
 
        AttachedToScale,
 
        OppositeToScale
 
    };
 

	
 
    ScrollZoomer( QWidget * );
 
    virtual ~ScrollZoomer();
 

	
 
    ScrollBar *horizontalScrollBar() const;
 
    ScrollBar *verticalScrollBar() const;
 

	
 
    void setHScrollBarMode( Qt::ScrollBarPolicy );
 
    void setVScrollBarMode( Qt::ScrollBarPolicy );
 

	
 
    Qt::ScrollBarPolicy vScrollBarMode () const;
 
    Qt::ScrollBarPolicy hScrollBarMode () const;
 

	
 
    void setHScrollBarPosition( ScrollBarPosition );
 
    void setVScrollBarPosition( ScrollBarPosition );
 

	
 
    ScrollBarPosition hScrollBarPosition() const;
 
    ScrollBarPosition vScrollBarPosition() const;
 

	
 
    QWidget* cornerWidget() const;
 
    virtual void setCornerWidget( QWidget * );
 

	
 
    virtual bool eventFilter( QObject *, QEvent * );
 

	
 
    virtual void rescale();
 

	
 
protected:
 
    virtual ScrollBar *scrollBar( Qt::Orientation );
 
    virtual void updateScrollBars();
 
    virtual void layoutScrollBars( const QRect & );
 

	
 
private Q_SLOTS:
 
    void scrollBarMoved( Qt::Orientation o, double min, double max );
 

	
 
private:
 
    bool needScrollBar( Qt::Orientation ) const;
 
    int oppositeAxis( int ) const;
 

	
 
    QWidget *d_cornerWidget;
 

	
 
    ScrollData *d_hScrollData;
 
    ScrollData *d_vScrollData;
 

	
 
    bool d_inZoom;
 
    bool d_alignCanvasToScales[ QwtPlot::axisCnt ];
 
};
 

	
 
#endif
src/zoomer.cpp
Show inline comments
 
/*
 
  Copyright © 2015 Hasan Yavuz Özderya
 
  Copyright © 2016 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
  serialplot is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  serialplot is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU General Public License for more details.
 

	
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include "zoomer.h"
 
#include <qwt_plot.h>
 
#include <QtDebug>
 

	
 
Zoomer::Zoomer(QWidget* widget, bool doReplot) :
 
    QwtPlotZoomer(widget, doReplot)
 
    ScrollZoomer(widget)
 
{
 
    // do nothing
 
    // set corner widget between the scrollbars with default background color
 
    auto cornerWidget = new QWidget();
 
    auto bgColor = cornerWidget->palette().color(QPalette::Window).name();
 
    auto styleSheet = QString("background-color:%1;").arg(bgColor);
 
    cornerWidget->setStyleSheet(styleSheet);
 
    ScrollZoomer::setCornerWidget(cornerWidget);
 
}
 

	
 
void Zoomer::zoom(int up)
 
{
 
    if (up == +1)
 
    {
 
        this->setZoomBase(this->plot());
 
    }
 

	
 
    QwtPlotZoomer::zoom(up);
 
    ScrollZoomer::zoom(up);
 

	
 
    if(zoomRectIndex() == 0)
 
    {
 
        emit unzoomed();
 
    }
 
}
 

	
 
void Zoomer::zoom( const QRectF & rect)
 
{
 
    // set the zoom base when user zooms in to first level
 
    if (zoomRectIndex() == 0)
 
    {
 
        this->setZoomBase(false);
 
    }
 

	
 
    ScrollZoomer::zoom(rect);
 
}
src/zoomer.h
Show inline comments
 
/*
 
  Copyright © 2015 Hasan Yavuz Özderya
 
  Copyright © 2016 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
  serialplot is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  serialplot is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU General Public License for more details.
 

	
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#ifndef ZOOMER_H
 
#define ZOOMER_H
 

	
 
#include <qwt_plot_zoomer.h>
 
#include <scrollzoomer.h>
 

	
 
class Zoomer : public QwtPlotZoomer
 
class Zoomer : public ScrollZoomer
 
{
 
    Q_OBJECT
 

	
 
public:
 
    Zoomer(QWidget *, bool doReplot=true);
 
    void zoom(int up);
 
    void zoom( const QRectF & );
 

	
 
signals:
 
    void unzoomed();
 
};
 

	
 
#endif // ZOOMER_H
0 comments (0 inline, 0 general)