Skip to content
Snippets Groups Projects
mapvisualization.py 48.7 KiB
Newer Older
  • Learn to ignore specific revisions
  •         self.STV.MVC.sigMapViewAdded.connect(self.addMapView)
            self.STV.MVC.sigMapViewRemoved.connect(self.removeMapView)
    
    
    
        def tsdView(self, tsd):
            r = [v for v in self.views if v.TSD == tsd]
            if len(r) == 1:
                return r[0]
            else:
                raise Exception('TSD not in list')
    
        def addMapView(self, mapView):
            assert isinstance(mapView, MapView)
            w = self.ui
            w.setUpdatesEnabled(False)
            for tsdv in self.views:
                tsdv.ui.setUpdatesEnabled(False)
    
            for tsdv in self.views:
                tsdv.insertMapView(mapView)
    
            for tsdv in self.views:
                tsdv.ui.setUpdatesEnabled(True)
    
            #mapView.sigSensorRendererChanged.connect(lambda *args : self.setRasterRenderer(mapView, *args))
            w.setUpdatesEnabled(True)
            self.sigResizeRequired.emit()
    
        def removeMapView(self, mapView):
            assert isinstance(mapView, MapView)
            for tsdv in self.views:
                tsdv.removeMapView(mapView)
            self.sigResizeRequired.emit()
    
    
        def setFocusView(self, tsd):
            self.focusView = tsd
    
        def orderedViews(self):
            #returns the
            if self.focusView is not None:
    
                assert isinstance(self.focusView, DatumView)
    
                return sorted(self.views,key=lambda v: np.abs(v.TSD.date - self.focusView.TSD.date))
            else:
                return self.views
    
    
        def setSubsetSize(self, size):
            assert isinstance(size, QSize)
            self.subsetSize = size
    
            for tsdView in self.orderedViews():
                tsdView.blockSignals(True)
    
            for tsdView in self.orderedViews():
                tsdView.setSubsetSize(size)
    
            for tsdView in self.orderedViews():
                tsdView.blockSignals(False)
    
    
    
        def addDates(self, tsdList):
            """
            Create a new TSDView
            :param tsdList:
            :return:
            """
            for tsd in tsdList:
                assert isinstance(tsd, TimeSeriesDatum)
    
                DV = DatumView(tsd, self, self.STV.MVC, parent=self.ui)
    
                #tsdView.setSubsetSize(self.subsetSize)
    
                DV.sigLoadingStarted.connect(self.sigLoadingStarted.emit)
                DV.sigLoadingFinished.connect(self.sigLoadingFinished.emit)
                DV.sigVisibilityChanged.connect(lambda: self.STV.adjustScrollArea())
    
                for i, mapView in enumerate(self.STV.MVC):
                    DV.insertMapView(mapView)
    
                bisect.insort(self.views, DV)
                i = self.views.index(DV)
    
                DV.ui.setParent(self.STV.targetLayout.parentWidget())
                self.STV.targetLayout.insertWidget(i, DV.ui)
                DV.ui.show()
    
    
            if len(tsdList) > 0:
                self.sigResizeRequired.emit()
    
        def removeDates(self, tsdList):
            toRemove = [v for v in self.views if v.TSD in tsdList]
            removedDates = []
    
            for DV in toRemove:
                self.views.remove(DV)
                DV.ui.parent().layout().removeWidget(DV.ui)
                DV.ui.hide()
                DV.ui.close()
                removedDates.append(DV.TSD)
                del DV
    
    
            if len(removedDates) > 0:
                self.sigResizeRequired.emit()
    
        def __len__(self):
            return len(self.views)
    
        def __iter__(self):
            return iter(self.views)
    
        def __getitem__(self, slice):
            return self.views[slice]
    
        def __delitem__(self, slice):
            self.removeDates(self.views[slice])
    
    class MapViewCollection(QObject):
    
        sigMapViewAdded = pyqtSignal(MapView)
        sigMapViewRemoved = pyqtSignal(MapView)
        sigSetMapViewVisibility = pyqtSignal(MapView, bool)
    
        def __init__(self, spatialTemporalVisualization):
            assert isinstance(spatialTemporalVisualization, SpatialTemporalVisualization)
    
            self.STV = spatialTemporalVisualization
            self.STV.dockMapViews.actionApplyStyles.triggered.connect(self.applyStyles)
            self.STV.TS.sigSensorAdded.connect(self.addSensor)
            self.STV.TS.sigSensorRemoved.connect(self.removeSensor)
    
            self.ui = spatialTemporalVisualization.dockMapViews
            self.btnList = spatialTemporalVisualization.dockMapViews.BVButtonList
            self.scrollArea = spatialTemporalVisualization.dockMapViews.scrollAreaMapViews
            self.scrollAreaContent = spatialTemporalVisualization.dockMapViews.scrollAreaMapsViewDockContent
    
            self.mapViewsDefinitions = []
            self.mapViewButtons = dict()
            self.adjustScrollArea()
    
        def applyStyles(self):
            for mapView in self.mapViewsDefinitions:
                mapView.applyStyles()
    
        def setCrosshairStyle(self, crosshairStyle):
            for mapView in self.mapViewsDefinitions:
                mapView.setCrosshairStyle(crosshairStyle)
    
        def setShowCrosshair(self, b):
            for mapView in self.mapViewsDefinitions:
                mapView.setShowCrosshair(b)
    
        def index(self, mapView):
            assert isinstance(mapView, MapView)
            return self.mapViewsDefinitions.index(mapView)
    
        def adjustScrollArea(self):
            #adjust scroll area widget to fit all visible widgets
            l = self.scrollAreaContent.layout()
            from timeseriesviewer.ui.widgets import maxWidgetSizes
            newSize = maxWidgetSizes(l)
            #print(newSize)
            #newSize = self.scrollAreaContent.sizeHint()
            self.scrollAreaContent.setFixedSize(newSize)
    
        def setVectorLayer(self, lyr):
            for mapView in self.mapViewsDefinitions:
                assert isinstance(mapView, MapView)
                mapView.setVectorLayer(lyr)
    
        def addSensor(self, sensor):
            for mapView in self.mapViewsDefinitions:
                mapView.addSensor(sensor)
            self.adjustScrollArea()
    
        def removeSensor(self, sensor):
            for mapView in self.mapViewsDefinitions:
                mapView.removeSensor(sensor)
    
        def createMapView(self):
    
            btn = QToolButton(self.btnList)
            self.btnList.layout().insertWidget(self.btnList.layout().count() - 1, btn)
    
            mapView = MapView(self, parent=self.scrollArea)
            mapView.sigRemoveMapView.connect(self.removeMapView)
            mapView.sigShowProfiles.connect(self.sigShowProfiles.emit)
    
    
            for sensor in self.STV.TS.Sensors:
    
                mapView.addSensor(sensor)
    
            self.mapViewButtons[mapView] = btn
            self.mapViewsDefinitions.append(mapView)
    
    
            btn.clicked.connect(lambda : self.showMapViewDefinition(mapView))
            self.refreshMapViewTitles()
            if len(self) == 1:
                self.showMapViewDefinition(mapView)
            self.sigMapViewAdded.emit(mapView)
            self.adjustScrollArea()
    
        def removeMapView(self, mapView):
            assert isinstance(mapView, MapView)
            btn = self.mapViewButtons[mapView]
    
            idx = self.mapViewsDefinitions.index(mapView)
    
            self.mapViewsDefinitions.remove(mapView)
            self.mapViewButtons.pop(mapView)
    
            mapView.ui.setVisible(False)
            btn.setVisible(False)
            self.btnList.layout().removeWidget(btn)
            l = self.scrollAreaContent.layout()
    
            for d in self.recentMapViewDefinitions():
                d.ui.setVisible(False)
                l.removeWidget(d.ui)
            l.removeWidget(mapView.ui)
            mapView.ui.close()
            btn.close()
            self.refreshMapViewTitles()
            self.sigMapViewRemoved.emit(mapView)
            if len(self) > 0:
                #show previous mapViewDefinition
                idxNext = max([idx-1, 0])
                self.showMapViewDefinition(self.mapViewsDefinitions[idxNext])
    
        def refreshMapViewTitles(self):
    
            for i, mapView in enumerate(self.mapViewsDefinitions):
                number = i+1
                title = '#{}'.format(number)
                mapView.setTitle(title)
                btn = self.mapViewButtons[mapView]
                btn.setText('{}'.format(number))
                btn.setToolTip('Show definition for map view {}'.format(number))
                btn.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
    
    
    
        def showMapViewDefinition(self, mapViewDefinition):
            assert mapViewDefinition in self.mapViewsDefinitions
            assert isinstance(mapViewDefinition, MapView)
            l = self.scrollAreaContent.layout()
    
            for d in self.recentMapViewDefinitions():
                d.ui.setVisible(False)
                l.removeWidget(d.ui)
    
            l.insertWidget(l.count() - 1, mapViewDefinition.ui)
            mapViewDefinition.ui.setVisible(True)
            self.ui.setWindowTitle(self.ui.baseTitle + '|'+mapViewDefinition.title())
    
        def recentMapViewDefinitions(self):
            parent = self.scrollAreaContent
            return [ui.mapViewDefinition() for ui in parent.findChildren(MapViewDefinitionUI)]
    
    
        def setMapViewVisibility(self, bandView, isVisible):
            assert isinstance(bandView, MapView)
            assert isinstance(isVisible, bool)
    
    
    
    
    
        def __len__(self):
            return len(self.mapViewsDefinitions)
    
        def __iter__(self):
            return iter(self.mapViewsDefinitions)
    
        def __getitem__(self, key):
            return self.mapViewsDefinitions[key]
    
        def __contains__(self, mapView):
            return mapView in self.mapViewsDefinitions
    
    
    
    
    class MapViewDefinitionUI(QGroupBox, load('mapviewdefinition.ui')):
    
        sigHideMapView = pyqtSignal()
        sigShowMapView = pyqtSignal()
        sigVectorVisibility = pyqtSignal(bool)
    
        def __init__(self, mapViewDefinition,parent=None):
            super(MapViewDefinitionUI, self).__init__(parent)
    
            self.setupUi(self)
            self.mMapViewDefinition = mapViewDefinition
            self.btnRemoveMapView.setDefaultAction(self.actionRemoveMapView)
            self.btnMapViewVisibility.setDefaultAction(self.actionToggleVisibility)
            self.btnApplyStyles.setDefaultAction(self.actionApplyStyles)
            self.btnVectorOverlayVisibility.setDefaultAction(self.actionToggleVectorVisibility)
            self.btnShowCrosshair.setDefaultAction(self.actionShowCrosshair)
    
    
            self.actionToggleVisibility.toggled.connect(lambda: self.setVisibility(not self.actionToggleVisibility.isChecked()))
            self.actionToggleVectorVisibility.toggled.connect(lambda : self.sigVectorVisibility.emit(self.actionToggleVectorVisibility.isChecked()))
    
    
        def DEPRsizeHint(self):
    
    
            #m = self.layout().contentsMargins()
            #sl = maxWidgetSizes(self.sensorList)
            #sm = self.buttonList.size()
            #w = sl.width() + m.left()+ m.right() + sm.width()
            #h = sl.height() + m.top() + m.bottom() + sm.height()
    
            return maxWidgetSizes(self.sensorList.layout())
    
    
    
    
        def mapViewDefinition(self):
            return self.mMapViewDefinition
    
    
        def setVisibility(self, isVisible):
            if isVisible != self.actionToggleVisibility.isChecked():
                self.btnMapViewVisibility.setChecked(isVisible)
                if isVisible:
                    self.sigShowMapView.emit()
                else:
                    self.sigHideMapView.emit()
    
        def visibility(self):
            return self.actionToggleVisibility.isChecked()
    
    
    class MapViewDockUI(TsvDockWidgetBase, load('mapviewdock.ui')):
        def __init__(self, parent=None):
            super(MapViewDockUI, self).__init__(parent)
            self.setupUi(self)
    
            self.baseTitle = self.windowTitle()
            self.btnApplyStyles.setDefaultAction(self.actionApplyStyles)
    
            #self.dockLocationChanged.connect(self.adjustLayouts)
    
        def toggleLayout(self, p):
            newLayout = None
            l = p.layout()
            print('toggle layout {}'.format(str(p.objectName())))
            tmp = QWidget()
            tmp.setLayout(l)
            sMax = p.maximumSize()
            sMax.transpose()
            sMin = p.minimumSize()
            sMin.transpose()
            p.setMaximumSize(sMax)
            p.setMinimumSize(sMin)
            if isinstance(l, QVBoxLayout):
                newLayout = QHBoxLayout()
            else:
                newLayout = QVBoxLayout()
            print(l, '->', newLayout)
    
            while l.count() > 0:
                item = l.itemAt(0)
                l.removeItem(item)
    
                newLayout.addItem(item)
    
    
            p.setLayout(newLayout)
            return newLayout
    
        def adjustLayouts(self, area):
            return
            lOld = self.scrollAreaMapsViewDockContent.layout()
            if area in [Qt.LeftDockWidgetArea, Qt.RightDockWidgetArea] \
                and isinstance(lOld, QVBoxLayout) or \
            area in [Qt.TopDockWidgetArea, Qt.BottomDockWidgetArea] \
                            and isinstance(lOld, QHBoxLayout):
    
                #self.toogleLayout(self.scrollAreaMapsViewDockContent)
                self.toggleLayout(self.BVButtonList)