Salome HOME
Updated copyright comment
[modules/gui.git] / tools / CurvePlot / src / python / test / TestDesktop.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2024  CEA, EDF
3 #
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.
8 #
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.
13 #
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
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 # Author : A. Bruneton
21 #
22
23 from pyqtside.QtCore import Qt, QTimer, pyqtSlot
24 from pyqtside.QtWidgets import QMainWindow,QMenu
25 import numpy as np
26
27 import curveplot
28
29 try:
30   import curveplot
31   from curveplot.utils import Logger
32 except:
33   from PlotController import PlotController as curveplot
34   from utils import Logger 
35
36 class TestDesktop(QMainWindow):
37     """ Dummy desktop for testing purposes.
38     """    
39     def __init__(self, sgPyQt):
40         QMainWindow.__init__(self)
41         self.cnt = -1
42         self.MAX_CNT = 20
43         self.timeLap = 100
44         self._sgPyQt = sgPyQt
45         
46     def initialize(self):
47         """ Initialize is called later than __init__() so that the Desktop and the SgPyQt
48         objects can be properly initialized.
49         """
50         self._currID = 1234
51         
52         self._sgDesktop = self
53         try:
54           self._plotController = curveplot.PlotController.GetInstance(self._sgPyQt)
55         except:
56           self._plotController = curveplot.GetInstance(self._sgPyQt)
57         self.createIDs()
58         self.createActions()
59         
60         context_actions = [self.itemDelAction]
61         menu = QMenu(self)
62         for a in context_actions:
63           menu.addAction(a)
64         self._plotController.setBrowserContextualMenu(menu)
65         
66         self.createToolbars()
67         self.createMenus()
68         self.createView()
69         
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)
79
80     def generateID(self):
81         self._currID += 1
82         return self._currID
83     
84     def createIDs(self):
85         # Actions
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()
95         
96         # Menus
97         self.etudeMenuID = self.generateID()
98         self.dummyMenuID = self.generateID()
99
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", "", "")
111
112     def createToolbars(self):
113         pass
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)
121
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)
132         
133         dummyMenu = self._sgPyQt.createMenu( "Dummy", -1, self.dummyMenuID, self._sgPyQt.defaultMenuGroup() )
134         self._sgPyQt.createMenu(self.addTabAction, dummyMenu)
135
136     def createView(self):
137         pass
138           
139     def showCurveTreeView(self) :
140         self._dockCurveBrowserView = self._plotController._curveBrowserView
141         self._sgDesktop.addDockWidget(Qt.LeftDockWidgetArea, self._dockCurveBrowserView)
142
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())
148 #       x = np.arange(5e5)
149 #       y = x
150       return x, y
151        
152     @pyqtSlot()
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)
157       if self.cnt >= 0:
158         QTimer.singleShot(self.timeLap, self, SLOT("itemDel()"))
159             
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)
163     
164     @pyqtSlot()
165     def itemDel(self):
166       curveplot.DeleteCurrentItem()
167       if self.cnt >= 0:
168         QTimer.singleShot(self.timeLap, self, SLOT("memPrint()"))
169
170     @pyqtSlot()
171     def perfTest(self):
172       lx, ly = [], []
173       nC = 200
174       for _ in range(nC):
175         x, y = self.__generateRandomData(1000)
176         lx.append(x); ly.append(y)
177       print("Done generating")
178       from time import time
179       t0 = time()
180       curveplot.LockRepaint()
181       for i in range(nC): 
182         curveplot.AddCurve(lx[i], ly[i], append=True)
183       curveplot.UnlockRepaint()
184       print("Elapsed: %.2f" % ( time() - t0))
185
186     def clearPlotSet(self):
187       curveplot.ClearPlotSet()
188       
189     def addPS(self):
190       # Also a test for unicode!
191       curveplot.AddPlotSet('ça m embête')
192       
193     def addTab(self):
194       pass
195 #      from PyQt5.QtWidgets import QPushButton
196 #      self.qp = QPushButton("Hi!")
197 #      self._sgPyQt.createView("Dummy", self.qp)  
198       
199     def plotTable(self):
200       from curveplot import TableModel
201       t = TableModel(None)
202       t.setTitle("coucou")
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)
212     
213     @pyqtSlot()
214     def memPrint(self):
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)
219       import resource
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:
223         self.cnt += 1
224         QTimer.singleShot(self.timeLap, self.curveSameFig)