1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2020 CEA/DEN, EDF R&D
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 # Author : A. Bruneton
23 from pyqtside.QtCore import Qt, QTimer, pyqtSlot
24 from pyqtside.QtWidgets import QMainWindow,QMenu
31 from curveplot.utils import Logger
33 from PlotController import PlotController as curveplot
34 from utils import Logger
36 class TestDesktop(QMainWindow):
37 """ Dummy desktop for testing purposes.
39 def __init__(self, sgPyQt):
40 QMainWindow.__init__(self)
47 """ Initialize is called later than __init__() so that the Desktop and the SgPyQt
48 objects can be properly initialized.
52 self._sgDesktop = self
54 self._plotController = curveplot.PlotController.GetInstance(self._sgPyQt)
56 self._plotController = curveplot.GetInstance(self._sgPyQt)
60 context_actions = [self.itemDelAction]
62 for a in context_actions:
64 self._plotController.setBrowserContextualMenu(menu)
70 self.curveSameFigAction.triggered.connect(self.curveSameFig)
71 self.curveNewFigAction.triggered.connect(self.curveNewFig)
72 self.itemDelAction.triggered.connect(self.itemDel)
73 self.cpsAction.triggered.connect(self.clearPlotSet)
74 self.plotTableAction.triggered.connect(self.plotTable)
75 self.addPSAction.triggered.connect(self.addPS)
76 self.addTabAction.triggered.connect(self.addTab)
77 self.memAction.triggered.connect(self.memPrint)
78 self.perfTestAction.triggered.connect(self.perfTest)
86 self.curveSameFigActionID = self.generateID()
87 self.curveNewFigActionID = self.generateID()
88 self.itemDelActionID = self.generateID()
89 self.cpsActionID = self.generateID()
90 self.plotTableActionID = self.generateID()
91 self.addPSActionID = self.generateID()
92 self.addTabActionID = self.generateID()
93 self.memActionID = self.generateID()
94 self.perfActionID = self.generateID()
97 self.etudeMenuID = self.generateID()
98 self.dummyMenuID = self.generateID()
100 def createActions(self):
101 ca = self._sgPyQt.createAction
102 self.curveSameFigAction = ca(self.curveSameFigActionID, "Curve on same fig", "Curve on same fig", "", "")
103 self.curveNewFigAction = ca(self.curveNewFigActionID, "Curve on new fig", "Curve on new fig", "", "")
104 self.itemDelAction = ca(self.itemDelActionID, "Delete selected", "Delete selected", "", "")
105 self.cpsAction = ca(self.cpsActionID, "Clear plot set", "Clear plot set", "", "")
106 self.plotTableAction = ca(self.plotTableActionID, "Plot from table", "Plot from table", "", "")
107 self.addPSAction = ca(self.addPSActionID, "Add plot set", "Add plot set", "", "")
108 self.addTabAction = ca(self.addTabActionID, "Add tab", "Add tab", "", "")
109 self.memAction = ca(self.memActionID, "Display used mem", "Display used mem", "", "")
110 self.perfTestAction = ca(self.perfActionID, "Perf test", "Perf test", "", "")
112 def createToolbars(self):
114 # self.Toolbar = self._sgPyQt.createTool(self.tr("Toolbar"))
115 # self._sgPyQt.createTool(self.fileNewAction, self.Toolbar)
116 # self._sgPyQt.createTool(self.filePrintAction, self.Toolbar)
117 # sep = self._sgPyQt.createSeparator()
118 # self._sgPyQt.createTool(sep, self.Toolbar)
119 # self._sgPyQt.createTool(self.editUndoAction, self.Toolbar)
120 # self._sgPyQt.createTool(self.editRedoAction, self.Toolbar)
122 def createMenus(self):
123 curveMenu = self._sgPyQt.createMenu( "Curve test", -1, self.etudeMenuID, self._sgPyQt.defaultMenuGroup() )
124 self._sgPyQt.createMenu(self.curveSameFigAction, curveMenu)
125 self._sgPyQt.createMenu(self.curveNewFigAction, curveMenu)
126 self._sgPyQt.createMenu(self.itemDelAction, curveMenu)
127 self._sgPyQt.createMenu(self.cpsAction, curveMenu)
128 self._sgPyQt.createMenu(self.plotTableAction, curveMenu)
129 self._sgPyQt.createMenu(self.addPSAction, curveMenu)
130 self._sgPyQt.createMenu(self.memAction, curveMenu)
131 self._sgPyQt.createMenu(self.perfTestAction, curveMenu)
133 dummyMenu = self._sgPyQt.createMenu( "Dummy", -1, self.dummyMenuID, self._sgPyQt.defaultMenuGroup() )
134 self._sgPyQt.createMenu(self.addTabAction, dummyMenu)
136 def createView(self):
139 def showCurveTreeView(self) :
140 self._dockCurveBrowserView = self._plotController._curveBrowserView
141 self._sgDesktop.addDockWidget(Qt.LeftDockWidgetArea, self._dockCurveBrowserView)
143 def __generateRandomData(self, nPoints=100):
144 from random import random
145 x = np.arange(nPoints) / 5.0
146 ampl = 20.0*random() + 1.0
147 y = ampl * np.sin(x*random())
153 def curveSameFig(self):
154 x, y = self.__generateRandomData()
155 _, ps_id = curveplot.AddCurve(x, y, x_label="the x axis", y_label="the y axis", append=True)
156 curveplot.SetLegendVisible(ps_id, True)
158 QTimer.singleShot(self.timeLap, self, SLOT("itemDel()"))
160 def curveNewFig(self):
161 x, y = self.__generateRandomData()
162 curveplot.AddCurve(x, y, x_label="the x axis", y_label="the y axis", append=False)
166 curveplot.DeleteCurrentItem()
168 QTimer.singleShot(self.timeLap, self, SLOT("memPrint()"))
175 x, y = self.__generateRandomData(1000)
176 lx.append(x); ly.append(y)
177 print("Done generating")
178 from time import time
180 curveplot.LockRepaint()
182 curveplot.AddCurve(lx[i], ly[i], append=True)
183 curveplot.UnlockRepaint()
184 print("Elapsed: %.2f" % ( time() - t0))
186 def clearPlotSet(self):
187 curveplot.ClearPlotSet()
190 # Also a test for unicode!
191 curveplot.AddPlotSet('ça m embête')
195 # from PyQt5.QtWidgets import QPushButton
196 # self.qp = QPushButton("Hi!")
197 # self._sgPyQt.createView("Dummy", self.qp)
200 from curveplot import TableModel
203 t.addColumn([1.0,2.0,3.0,4.0])
204 t.addColumn([1.0,2.0,3.0,4.0])
205 t.addColumn([1.0,4.0,9.0,16.0])
206 t.setColumnTitle(0, "X-s")
207 t.setColumnTitle(1, "Identity")
208 t.setColumnTitle(2, "Square")
209 cont = curveplot.PlotController.GetInstance()
210 cont.plotCurveFromTable(t, y_col_index=1, append=False)
211 cont.plotCurveFromTable(t, y_col_index=2, append=True)
215 i, t = curveplot.GetAllPlotSets()
216 print(list(zip(i, t)))
217 new_id = curveplot.CopyCurve(curve_id=0, plot_set_id=1)
218 print("created curve: %d" % new_id)
220 m = resource.getrusage(resource.RUSAGE_SELF)[2]*resource.getpagesize()/1e6
221 print("** Used memory: %.2f Mb" % m)
222 if self.cnt >= 0 and self.cnt < self.MAX_CNT:
224 QTimer.singleShot(self.timeLap, self.curveSameFig)