Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) QwtExample8

 

QtQwtQt CreatorLubuntuWindows

 

Qwt example 8: adapted spectrogram from Qwt examples is a Qwt example that is adapted from a spectrogram from the Qwt documentation

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppQwtExample8/CppQwtExample8.pro

 

#Qwt does not go together with Qwt
include(../../DesktopApplicationNoWeffcpp.pri)
include(../../Libraries/Qwt.pri)

SOURCES += main.cpp \
    myplot.cpp \
    mydialog.cpp \
    myzoomer.cpp \
    mydata.cpp \
    mycolormap.cpp

HEADERS += \
    myplot.h \
    mydialog.h \
    myzoomer.h \
    mydata.h \
    mycolormap.h

FORMS += \
    mydialog.ui

 

 

 

 

 

./CppQwtExample8/main.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include <QApplication>
#include "mydialog.h"
#pragma GCC diagnostic pop

int main( int argc, char **argv )
{
  QApplication a( argc, argv );
  MyDialog d;
  d.show();
  d.setWindowTitle(argv[0]);
  return a.exec();
}

 

 

 

 

 

./CppQwtExample8/mycolormap.h

 

#ifndef MYCOLORMAP_H
#define MYCOLORMAP_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include "qwt_color_map.h"
#pragma GCC diagnostic pop

struct MyColorMap: public QwtLinearColorMap
{
  MyColorMap();
};

#endif // MYCOLORMAP_H

 

 

 

 

 

./CppQwtExample8/mycolormap.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include "mycolormap.h"
#pragma GCC diagnostic pop

MyColorMap::MyColorMap()
  : QwtLinearColorMap( Qt::darkCyan, Qt::red)
{
  addColorStop( 0.1, Qt::cyan );
  addColorStop( 0.6, Qt::green );
  addColorStop( 0.95, Qt::yellow );
}

 

 

 

 

 

./CppQwtExample8/mydata.h

 

#ifndef MYDATA_H
#define MYDATA_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include "qwt_matrix_raster_data.h"
#pragma GCC diagnostic pop

///The spectrogram data
struct MyData: public QwtRasterData
{
  MyData();

  double value( double x, double y ) const;
};

#endif // MYDATA_H

 

 

 

 

 

./CppQwtExample8/mydata.cpp

 

#include "mydata.h"

MyData::MyData()
{
  setInterval( Qt::XAxis, QwtInterval( -1.5, 1.5 ) );
  setInterval( Qt::YAxis, QwtInterval( -1.5, 1.5 ) );
  setInterval( Qt::ZAxis, QwtInterval( 0.0, 10.0 ) );
}

double MyData::value( double x, double y ) const
{
  const double c = 0.842;

  const double v1 = x * x + ( y - c ) * ( y + c );
  const double v2 = x * ( y + c ) + x * ( y + c );

  return 1.0 / ( v1 * v1 + v2 * v2 );
}

 

 

 

 

 

./CppQwtExample8/mydialog.h

 

#ifndef QTDIALOG_H
#define QTDIALOG_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include <QDialog>
#pragma GCC diagnostic pop

namespace Ui {
  class MyDialog;
}

struct MyPlot;

class MyDialog : public QDialog
{
  Q_OBJECT

public:
  explicit MyDialog(QWidget *parent = 0);
  MyDialog(const MyDialog&) = delete;
  MyDialog& operator=(const MyDialog&) = delete;
  ~MyDialog();

  void keyPressEvent(QKeyEvent *);

private:
  Ui::MyDialog *ui;
  MyPlot * const m_plot;
};

#endif // QTDIALOG_H

 

 

 

 

 

./CppQwtExample8/mydialog.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include "mydialog.h"

#include <cassert>

#include <QGridLayout>
#include <QKeyEvent>
#include "myplot.h"

#include "ui_mydialog.h"
#pragma GCC diagnostic pop

MyDialog::MyDialog(QWidget *parent) :
  QDialog(parent),
  ui(new Ui::MyDialog),
  m_plot(new MyPlot(this))
{
  ui->setupUi(this);

  assert(!this->layout());
  QGridLayout * const my_layout = new QGridLayout;
  this->setLayout(my_layout);
  my_layout->addWidget(m_plot);

  m_plot->showSpectrogram(true);
  m_plot->showContour(true);
}

MyDialog::~MyDialog()
{
  delete ui;
}

void MyDialog::keyPressEvent(QKeyEvent * e)
{
  switch (e->key())
  {
    case Qt::Key_Up: m_plot->showSpectrogram(true); break;
    case Qt::Key_Down: m_plot->showSpectrogram(false); break;
    case Qt::Key_Right: m_plot->showContour(true); break;
    case Qt::Key_Left: m_plot->showContour(false); break;
    case Qt::Key_Escape: close(); return;
  }
}

 

 

 

 

 

./CppQwtExample8/myplot.h

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include <qwt_plot.h>
#pragma GCC diagnostic pop

struct QwtPlotSpectrogram;

class MyPlot: public QwtPlot
{
    Q_OBJECT

public:
  MyPlot(QWidget * parent = nullptr);
  MyPlot(const MyPlot&) = delete;
  MyPlot& operator=(const MyPlot&) = delete;

public Q_SLOTS:
  void showContour(bool on);
  void showSpectrogram(bool on);

private:
  QwtPlotSpectrogram * const m_spectrogram;
};

 

 

 

 

 

./CppQwtExample8/myplot.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
//#include <qprinter.h>
//#include <qprintdialog.h>
//#include <qwt_color_map.h>
#include <qwt_plot_spectrogram.h>
//#include <qwt_plot_spectrogram.h>
#include <qwt_scale_widget.h>
//#include <qwt_scale_draw.h>
#include <qwt_plot_panner.h>
#include <qwt_plot_layout.h>
//#include <qwt_plot_renderer.h>
#include "myplot.h"
#include "myzoomer.h"
#include "mydata.h"
#include "mycolormap.h"
#pragma GCC diagnostic pop

MyPlot::MyPlot( QWidget *parent )
  : QwtPlot( parent ),
    m_spectrogram(new QwtPlotSpectrogram())
{
  m_spectrogram->setRenderThreadCount( 0 ); // use system specific thread count

  m_spectrogram->setColorMap( new MyColorMap() );
  m_spectrogram->setCachePolicy( QwtPlotRasterItem::PaintCache );

  m_spectrogram->setData( new MyData() );
  m_spectrogram->attach( this );

  QList<double> contourLevels;
  for ( double level = 0.5; level < 10.0; level += 1.0 )
      contourLevels += level;
  m_spectrogram->setContourLevels( contourLevels );

  const QwtInterval zInterval = m_spectrogram->data()->interval( Qt::ZAxis );
  // A color bar on the right axis
  QwtScaleWidget *rightAxis = axisWidget( QwtPlot::yRight );
  rightAxis->setTitle( "Intensity" );
  rightAxis->setColorBarEnabled( true );
  rightAxis->setColorMap( zInterval, new MyColorMap() );

  setAxisScale( QwtPlot::yRight, zInterval.minValue(), zInterval.maxValue() );
  enableAxis( QwtPlot::yRight );

  plotLayout()->setAlignCanvasToScales( true );
  replot();

  // LeftButton for the zooming
  // MidButton for the panning
  // RightButton: zoom out by 1
  // Ctrl+RighButton: zoom out to full size

  QwtPlotZoomer* zoomer = new MyZoomer( canvas() );
  zoomer->setMousePattern( QwtEventPattern::MouseSelect2,
      Qt::RightButton, Qt::ControlModifier );
  zoomer->setMousePattern( QwtEventPattern::MouseSelect3,
      Qt::RightButton );

  QwtPlotPanner *panner = new QwtPlotPanner( canvas() );
  panner->setAxisEnabled( QwtPlot::yRight, false );
  panner->setMouseButton( Qt::MidButton );

  // Avoid jumping when labels with more/less digits
  // appear/disappear when scrolling vertically

  const QFontMetrics fm( axisWidget( QwtPlot::yLeft )->font() );
  QwtScaleDraw *sd = axisScaleDraw( QwtPlot::yLeft );
  sd->setMinimumExtent( fm.width( "100.00" ) );

  const QColor c( Qt::darkBlue );
  zoomer->setRubberBandPen( c );
  zoomer->setTrackerPen( c );
}

void MyPlot::showContour( bool on )
{
  m_spectrogram->setDisplayMode(QwtPlotSpectrogram::ContourMode, on );
  replot();
}

void MyPlot::showSpectrogram( bool on )
{
  m_spectrogram->setDisplayMode( QwtPlotSpectrogram::ImageMode, on );
  m_spectrogram->setDefaultContourPen(
      on ? QPen(Qt::black, 0) : QPen(Qt::NoPen) );

  replot();
}

 

 

 

 

 

./CppQwtExample8/myzoomer.h

 

#ifndef MYZOOMER_H
#define MYZOOMER_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include <qwt_plot_zoomer.h>
#pragma GCC diagnostic pop

struct MyZoomer: public QwtPlotZoomer
{
  MyZoomer(QWidget * const canvas);
  QwtText trackerTextF(const QPointF &pos) const;
};

#endif // MYZOOMER_H

 

 

 

 

 

./CppQwtExample8/myzoomer.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#include "myzoomer.h"

#include "qwt_plot_canvas.h"
#pragma GCC diagnostic pop

MyZoomer::MyZoomer(QWidget * const canvas)
  : QwtPlotZoomer(dynamic_cast<QwtPlotCanvas *>(canvas))
{
  setTrackerMode(AlwaysOn);
}

QwtText MyZoomer::trackerTextF(const QPointF& pos) const
{
  QColor bg( Qt::white );
  bg.setAlpha( 200 );

  QwtText text = QwtPlotZoomer::trackerTextF( pos );
  text.setBackgroundBrush( QBrush( bg ) );
  return text;
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml