Newer
Older
import os
from qgis.core import *
from qgis.gui import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import numpy as np
from timeseriesviewer import *
from timeseriesviewer.utils import *
from timeseriesviewer.ui.widgets import loadUIFormClass
import pyqtgraph as pg
load = lambda p : loadUIFormClass(jp(DIR_UI,p))
MARKERSYMBOLS = [('o', u'Circle'),
('t',u'Triangle Down'),
('t1',u'Triangle Up'),
('t2',u'Triangle Right'),
('t3', u'Triangle Left'),
('p',u'Pentagon'),
('h', u'Hexagon'),
('s',u'Star'),
('+',u'Plus'),
('d',u'Diamond'),
(None, u'No Symbol')
]
PENSTYLES = [(Qt.SolidLine, '___'),
(Qt.DashLine, '_ _ _'),
(Qt.DotLine, '. . .'),
(Qt.DashDotLine, '_ .'),
(Qt.DashDotDotLine, '_ . .'),
(Qt.NoPen, 'No Pen')]
class PlotStyle(object):
def __init__(self, **kwds):
self.markerSymbol = MARKERSYMBOLS[0][0]
self.markerSize = 10
self.markerBrush = QBrush()
self.markerBrush.setColor(QColor(55,55,55))
self.markerBrush.setStyle(Qt.SolidPattern)
self.backgroundColor = Qt.black
self.markerPen = QPen()
self.markerPen.setStyle(Qt.SolidLine)
self.markerPen.setColor(Qt.white)
self.linePen = QPen()
self.linePen.setStyle(Qt.SolidLine)
self.linePen.setColor(QColor(74,75,75))
def createIcon(self, size=None):
if size is None:
size = QSize(60,60)
pm = QPixmap(size)
pm.fill(self.backgroundColor)
p = QPainter(pm)
#draw the line
p.setPen(self.linePen)
p.drawLine(2, pm.height()-2, pm.width()-2, 2)
p.translate(pm.width() / 2, pm.height() / 2)
from pyqtgraph.graphicsItems.ScatterPlotItem import drawSymbol
path = drawSymbol(p, self.markerSymbol, self.markerSize, self.markerPen, self.markerBrush)
p.end()
return QIcon(pm)
class PlotStyleWidget(QWidget, load('plotstylewidget.ui')):
sigPlotStyleChanged = pyqtSignal(PlotStyle)
def __init__(self, title='<#>', parent=None):
super(PlotStyleWidget, self).__init__(parent)
self.setupUi(self)
assert isinstance(self.plotWidget, pg.PlotWidget)
self.mBlockUpdates = False
#self.plotWidget.disableAutoRange()
#self.plotWidget.setAspectLocked()
self.plotWidget.setRange(xRange=[0,1], yRange=[0,1], update=True)
self.plotWidget.setLimits(xMin=0, xMax=1, yMin=0, yMax=1)
self.plotWidget.setMouseEnabled(x=False, y=False)
for ax in self.plotWidget.plotItem.axes:
self.plotWidget.plotItem.hideAxis(ax)
#self.plotWidget.disableAutoRange()
self.plotDataItem = self.plotWidget.plot(x=[0.1, 0.5, 0.9], y=[0.25, 0.9, 0.5])
self.legend = pg.LegendItem((100,60), offset=(70,30)) # args are (size, offset)
self.legend.setParentItem(self.plotDataItem.topLevelItem()) # Note we do NOT call plt.addItem in this case
self.legend.hide()
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
for t in MARKERSYMBOLS:
self.cbMarkerSymbol.addItem(t[1], t[0])
for t in PENSTYLES:
self.cbMarkerPenStyle.addItem(t[1], t[0])
self.cbLinePenStyle.addItem(t[1], t[0])
#connect signals
self.btnMarkerBrushColor.colorChanged.connect(self.refreshPreview)
self.btnMarkerPenColor.colorChanged.connect(self.refreshPreview)
self.btnLinePenColor.colorChanged.connect(self.refreshPreview)
self.cbMarkerSymbol.currentIndexChanged.connect(self.refreshPreview)
self.cbMarkerPenStyle.currentIndexChanged.connect(self.refreshPreview)
self.cbLinePenStyle.currentIndexChanged.connect(self.refreshPreview)
self.sbMarkerSize.valueChanged.connect(self.refreshPreview)
self.sbMarkerPenWidth.valueChanged.connect(self.refreshPreview)
self.sbLinePenWidth.valueChanged.connect(self.refreshPreview)
self.setPlotStyle(PlotStyle())
self.refreshPreview()
def refreshPreview(self, *args):
if not self.mBlockUpdates:
print('DEBUG: REFRESH NOW')
style = self.plotStyle()
#todo: set style to style preview
pi = self.plotDataItem
pi.setData(x=[0.25, 0.5, 0.75], y=[0.25, 0.75, 0.5],
symbol=style.markerSymbol, symbolBrush=style.markerBrush,
symbolPen=style.markerPen, symbolSize=style.markerSize,
pen = style.linePen, width=style.linePen.width())
#symbol='o', symbolBrush=sensorView.color, symbolPen='w', symbolSize=8
pi.update()
self.plotWidget.update()
self.sigPlotStyleChanged.emit(style)
def _setComboBoxToValue(self, cb, value):
assert isinstance(cb, QComboBox)
for i in range(cb.count()):
v = cb.itemData(i)
if type(value) in [str, unicode]:
v = str(v)
if v == value:
cb.setCurrentIndex(i)
break
s = ""
def setPlotStyle(self, style):
assert isinstance(style, PlotStyle)
#set widget values
self.mBlockUpdates = True
self.sbMarkerSize.setValue(style.markerSize)
self._setComboBoxToValue(self.cbMarkerSymbol, style.markerSymbol)
assert isinstance(style.markerPen, QPen)
assert isinstance(style.markerBrush, QBrush)
assert isinstance(style.linePen, QPen)
self.btnMarkerPenColor.setColor(style.markerPen.color())
self._setComboBoxToValue(self.cbMarkerPenStyle, style.markerPen.style())
self.sbMarkerPenWidth.setValue(style.markerPen.width())
self.btnMarkerBrushColor.setColor(style.markerBrush.color())
self.btnLinePenColor.setColor(style.linePen.color())
self._setComboBoxToValue(self.cbLinePenStyle, style.linePen.style())
self.sbLinePenWidth.setValue(style.linePen.width())
self.mBlockUpdates = False
self.refreshPreview()
def plotStyleIcon(self):
icon = QIcon()
#todo: get plot preview as 60x60 icon
return icon
def plotStyle(self):
style = PlotStyle()
#read plotstyle values from widgets
style.markerSize = self.sbMarkerSize.value()
symbol = self.cbMarkerSymbol.itemData(self.cbMarkerSymbol.currentIndex())
if isinstance(symbol, unicode):
symbol = str(symbol)
style.markerSymbol = symbol
assert isinstance(style.markerPen, QPen)
assert isinstance(style.markerBrush, QBrush)
assert isinstance(style.linePen, QPen)
style.markerPen = pg.mkPen(color=self.btnMarkerPenColor.color(),
width=self.sbMarkerPenWidth.value(),
style=self.cbMarkerPenStyle.itemData(self.cbMarkerPenStyle.currentIndex()))
style.markerBrush.setColor(self.btnMarkerBrushColor.color())
style.markerBrush.setColor(self.btnMarkerBrushColor.color())
style.linePen = pg.mkPen(color=self.btnLinePenColor.color(),
width=self.sbLinePenWidth.value(),
style=self.cbLinePenStyle.itemData(self.cbLinePenStyle.currentIndex()))
class PlotStyleButton(QPushButton):
def __init__(self, *args):
super(PlotStyleButton, self).__init__(*args)
self.clicked.connect(self.showDialog)
self.setPlotStyle(PlotStyle())
def plotStyle(self):
return self.mPlotStyle
def setPlotStyle(self, plotStyle):
#assert isinstance(plotStyle, PlotStyle)
self.mPlotStyle = plotStyle
self._updateIcon()
pass
def showDialog(self):
style = PlotStyleDialog.getPlotStyle(plotStyle=self.mPlotStyle)
if style:
self.setPlotStyle(style)
def resizeEvent(self, arg):
self._updateIcon()
def _updateIcon(self):
if self.mPlotStyle != None:
icon = self.mPlotStyle.createIcon(self.size())
#self.setIconSize(self.size())
self.setIcon(icon)
class PlotStyleDialog(QgsDialog):
@staticmethod
def getPlotStyle(*args, **kwds):
"""
Opens a CrosshairDialog.
:param args:
:param kwds:
:return: specified CrosshairStyle if accepted, else None
"""
d = PlotStyleDialog(*args, **kwds)
d.exec_()
if d.result() == QDialog.Accepted:
return d.plotStyle()
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
else:
return None
def __init__(self, parent=None, plotStyle=None, title='Specify Plot Style'):
super(PlotStyleDialog, self).__init__(parent=parent , \
buttons=QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
self.w = PlotStyleWidget(parent=self)
self.setWindowTitle(title)
self.btOk = QPushButton('Ok')
self.btCancel = QPushButton('Cancel')
buttonBar = QHBoxLayout()
#buttonBar.addWidget(self.btCancel)
#buttonBar.addWidget(self.btOk)
l = self.layout()
l.addWidget(self.w)
l.addLayout(buttonBar)
#self.setLayout(l)
def plotStyle(self):
return self.w.plotStyle()
def setPlotStyle(self, plotStyle):
assert isinstance(plotStyle, PlotStyle)
self.w.setPlotStyle(plotStyle)
if __name__ == '__main__':
import site, sys
#add site-packages to sys.path as done by enmapboxplugin.py
from timeseriesviewer import sandbox
qgsApp = sandbox.initQgisEnvironment()
btn = PlotStyleButton()
btn.show()
qgsApp.exec_()
qgsApp.exitQgis()