1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
#!/usr/bin/python
import sys
#import Qwt
from PyQt5 import Qwt
from PyQt5.QtCore import Qt, qIsNaN, qRound
from PyQt5.QtGui import QColor, QPen, QBrush, QFontMetrics
from PyQt5.QtWidgets import QApplication, QCheckBox, QToolBar, QToolButton, QLabel, QComboBox, QSlider, QSizePolicy, QMainWindow
from PyQt5.QtPrintSupport import QPrintDialog, QPrinter
class MyZoomer(Qwt.QwtPlotZoomer):
def __init__(self, canvas):
Qwt.QwtPlotZoomer.__init__(self, canvas )
self.setTrackerMode( Qwt.QwtPicker.AlwaysOn )
def trackerTextF( self, pos ):
bg = QColor( Qt.white )
bg.setAlpha( 200 )
text = Qwt.QwtPlotZoomer.trackerTextF( pos )
text.setBackgroundBrush( QBrush( bg ) )
return text
class SpectrogramData(Qwt.QwtRasterData):
def __init__(self):
Qwt.QwtRasterData.__init__(self)
self.setInterval( Qt.XAxis, Qwt.QwtInterval( -1.5, 1.5 ) )
self.setInterval( Qt.YAxis, Qwt.QwtInterval( -1.5, 1.5 ) )
self.setInterval( Qt.ZAxis, Qwt.QwtInterval( 0.0, 10.0 ) )
def value( self, x, y ):
c = 0.842
#c = 0.33
v1 = x * x + ( y - c ) * ( y + c )
v2 = x * ( y + c ) + x * ( y + c )
return 1.0 / ( v1 * v1 + v2 * v2 )
class LinearColorMapRGB(Qwt.QwtLinearColorMap):
def __init__(self):
Qwt.QwtLinearColorMap.__init__(self, Qt.darkCyan, Qt.red, Qwt.QwtColorMap.RGB )
self.addColorStop( 0.1, Qt.cyan )
self.addColorStop( 0.6, Qt.green )
self.addColorStop( 0.95, Qt.yellow )
class LinearColorMapIndexed(Qwt.QwtLinearColorMap):
def __init__(self):
Qwt.QwtLinearColorMap.__init__( self, Qt.darkCyan, Qt.red, Qwt.QwtColorMap.Indexed )
self.addColorStop( 0.1, Qt.cyan )
self.addColorStop( 0.6, Qt.green )
self.addColorStop( 0.95, Qt.yellow )
class HueColorMap(Qwt.QwtColorMap):
def __init__(self):
Qwt.QwtColorMap.__init__(self)
self.d_hue1 = 0
self.d_hue2 = 359
self.d_saturation = 150
self.d_value = 200
self.d_rgbTable = []
self.updateTable()
def rgb(self, interval, value ):
if ( qIsNaN(value) ):
return 0
width = interval.width()
if ( width <= 0 ):
return 0
if ( value <= interval.minValue() ):
return self.d_rgbMin
if ( value >= interval.maxValue() ):
return self.d_rgbMax
ratio = ( value - interval.minValue() ) / width
hue = self.d_hue1 + qRound( ratio * ( self.d_hue2 - self.d_hue1 ) )
if ( hue >= 360 ):
hue -= 360
if ( hue >= 360 ):
hue = hue % 360
return self.d_rgbTable[hue]
def colorIndex(self, a0, a1 ):
# we don't support indexed colors
return 0
def updateTable(self):
for i in range(360):
self.d_rgbTable.append(QColor.fromHsv( i, self.d_saturation, self.d_value ).rgb())
self.d_rgbMin = self.d_rgbTable[ self.d_hue1 % 360 ]
self.d_rgbMax = self.d_rgbTable[ self.d_hue2 % 360 ]
class AlphaColorMap(Qwt.QwtAlphaColorMap):
def __init__(self):
Qwt.QwtAlphaColorMap.__init__(self)
#setColor( QColor("DarkSalmon") )
self.setColor( QColor("SteelBlue") )
class Plot( Qwt.QwtPlot ):
def __init__(self, parent):
Qwt.QwtPlot.__init__(self, parent )
self.RGBMap = 0
self.IndexMap = 1
self.HueMap = 2
self.AlphaMap = 3
self.d_alpha = 255
self.d_spectrogram = Qwt.QwtPlotSpectrogram()
self.d_spectrogram.setRenderThreadCount( 0 ) # use system specific thread count
self.d_spectrogram.setCachePolicy( Qwt.QwtPlotRasterItem.PaintCache )
contourLevels=[]
for level in range(1,20,2):
contourLevels.append(0.5*level)
self.d_spectrogram.setContourLevels( contourLevels )
self.d_spectrogram.setData( SpectrogramData() )
self.d_spectrogram.attach( self )
zInterval = self.d_spectrogram.data().interval( Qt.ZAxis )
# A color bar on the right axis
self.rightAxis = self.axisWidget( Qwt.QwtPlot.yRight )
self.rightAxis.setTitle( "Intensity" )
self.rightAxis.setColorBarEnabled( True )
self.setAxisScale( Qwt.QwtPlot.yRight, zInterval.minValue(), zInterval.maxValue() )
self.enableAxis( Qwt.QwtPlot.yRight )
self.plotLayout().setAlignCanvasToScales( True )
self.setColorMap( self.RGBMap )
# LeftButton for the zooming
# MidButton for the panning
# RightButton: zoom out by 1
# Ctrl+RighButton: zoom out to full size
self.zoomer = MyZoomer( self.canvas() )
self.zoomer.setMousePattern( Qwt.QwtEventPattern.MouseSelect2, Qt.RightButton, Qt.ControlModifier )
self.zoomer.setMousePattern( Qwt.QwtEventPattern.MouseSelect3, Qt.RightButton )
self.panner = Qwt.QwtPlotPanner( self.canvas() )
self.panner.setAxisEnabled( Qwt.QwtPlot.yRight, False )
self.panner.setMouseButton( Qt.MidButton )
# Avoid jumping when labels with more/less digits
# appear/disappear when scrolling vertically
fm = QFontMetrics ( self.axisWidget( Qwt.QwtPlot.yLeft ).font() )
sd = self.axisScaleDraw( Qwt.QwtPlot.yLeft )
sd.setMinimumExtent( fm.width( "100.00" ) )
c = QColor( Qt.darkBlue )
self.zoomer.setRubberBandPen( c )
self.zoomer.setTrackerPen( c )
def showContour( self, on ):
self.d_spectrogram.setDisplayMode( Qwt.QwtPlotSpectrogram.ContourMode, on )
self.replot()
def showSpectrogram( self, on ):
self.d_spectrogram.setDisplayMode( Qwt.QwtPlotSpectrogram.ImageMode, on )
if (on):
self.d_spectrogram.setDefaultContourPen( QPen( Qt.black, 0 ) )
else:
self.d_spectrogram.setDefaultContourPen( QPen( Qt.NoPen ) )
self.replot()
def setColorMap( self, type ):
self.axis = self.axisWidget( Qwt.QwtPlot.yRight )
zInterval = self.d_spectrogram.data().interval( Qt.ZAxis )
self.d_mapType = type
alpha = self.d_alpha
if ( type == self.HueMap ):
self.d_spectrogram.setColorMap( HueColorMap() )
self.axis.setColorMap( zInterval, HueColorMap() )
elif ( type == self.AlphaMap):
self.alpha = 255
self.d_spectrogram.setColorMap( AlphaColorMap() )
self.axis.setColorMap( zInterval, AlphaColorMap() )
elif ( type == self.IndexMap):
self.d_spectrogram.setColorMap( LinearColorMapIndexed() )
self.axis.setColorMap( zInterval, LinearColorMapIndexed() )
elif ( type == self.RGBMap):
self.d_spectrogram.setColorMap( LinearColorMapRGB() )
self.axis.setColorMap( zInterval, LinearColorMapRGB() )
self.d_spectrogram.setAlpha( alpha )
self.replot()
def setAlpha( self, alpha ):
# setting an alpha value doesn't make sense in combination
# with a color map interpolating the alpha value
self.d_alpha = alpha
if ( self.d_mapType != self.AlphaMap ):
self.d_spectrogram.setAlpha( alpha )
self.replot()
def printPlot(self): # Creates an empty file XXXXX
printer = QPrinter( QPrinter.HighResolution )
printer.setOrientation( QPrinter.Landscape )
printer.setDocName( "spectrogram.pdf" )
printer.setCreator( "Spectrogram example" )
dialog = QPrintDialog ( printer )
if ( dialog.exec_() ):
renderer = Qwt.QwtPlotRenderer()
if ( printer.colorMode() == QPrinter.GrayScale ):
renderer.setDiscardFlag( Qwt.QwtPlotRenderer.DiscardBackground )
renderer.setDiscardFlag( Qwt.QwtPlotRenderer.DiscardCanvasBackground )
renderer.setDiscardFlag( Qwt.QwtPlotRenderer.DiscardCanvasFrame )
renderer.setLayoutFlag( Qwt.QwtPlotRenderer.FrameWithScales )
renderer.renderTo( self, printer )
class MainWindow( QMainWindow ):
def __init__(self, *args):
QMainWindow.__init__(self, *args )
self.d_plot = Plot( self )
self.setCentralWidget( self.d_plot )
self.toolBar = QToolBar( self )
self.btnPrint = QToolButton( self.toolBar )
self.btnPrint.setText( "Print" )
self.btnPrint.setToolButtonStyle( Qt.ToolButtonTextUnderIcon )
self.toolBar.addWidget( self.btnPrint )
self.btnPrint.clicked.connect(self.d_plot.printPlot )
self.toolBar.addSeparator()
self.toolBar.addWidget( QLabel("Color Map " ) )
self.mapBox = QComboBox( self.toolBar )
self.mapBox.addItem( "RGB" )
self.mapBox.addItem( "Indexed Colors" )
self.mapBox.addItem( "Hue" )
self.mapBox.addItem( "Alpha" )
self.mapBox.setSizePolicy( QSizePolicy.Fixed, QSizePolicy.Fixed )
self.toolBar.addWidget( self.mapBox )
self.mapBox.currentIndexChanged['int'].connect(self.d_plot.setColorMap )
self.toolBar.addWidget( QLabel( " Opacity " ) )
self.slider = QSlider( Qt.Horizontal )
self.slider.setRange( 0, 255 )
self.slider.setValue( 255 )
self.slider.valueChanged['int'].connect(self.d_plot.setAlpha )
self.toolBar.addWidget( self.slider )
self.toolBar.addWidget( QLabel(" " ) )
self.btnSpectrogram = QCheckBox( "Spectrogram", self.toolBar )
self.toolBar.addWidget( self.btnSpectrogram )
self.btnSpectrogram.toggled['bool'].connect(self.d_plot.showSpectrogram )
self.btnContour = QCheckBox( "Contour", self.toolBar )
self.toolBar.addWidget( self.btnContour )
self.btnContour.toggled['bool'].connect(self.d_plot.showContour )
self.addToolBar( self.toolBar )
self.btnSpectrogram.setChecked( True )
self.btnContour.setChecked( False )
if __name__ == '__main__':
a = QApplication(sys.argv)
a.setStyle( "Windows" )
mainWindow = MainWindow()
mainWindow.resize( 600, 400 )
mainWindow.show()
sys.exit(a.exec_())
|