Salome HOME
Update copyrights
[modules/gui.git] / tools / CurvePlot / src / python / test / TestDesktop.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2019  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 #
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23 # Author : A. Bruneton
24 #
25
26 from pyqtside.QtCore import SIGNAL, SLOT, Slot, Qt, QTimer
27 from pyqtside.QtGui import QMainWindow,QMenu
28 import numpy as np
29
30 import curveplot
31
32 try:
33   import curveplot
34   from curveplot.utils import Logger
35 except:
36   from PlotController import PlotController as curveplot
37   from utils import Logger 
38
39 class TestDesktop(QMainWindow):
40     """ Dummy desktop for testing purposes.
41     """    
42     def __init__(self, sgPyQt):
43         QMainWindow.__init__(self)
44         self.cnt = -1
45         self.MAX_CNT = 20
46         self.timeLap = 100
47         self._sgPyQt = sgPyQt
48         
49     def initialize(self):
50         """ Initialize is called later than __init__() so that the Desktop and the SgPyQt
51         objects can be properly initialized.
52         """
53         self._currID = 1234
54         
55         self._sgDesktop = self
56         try:
57           self._plotController = curveplot.PlotController.GetInstance(self._sgPyQt)
58         except:
59           self._plotController = curveplot.GetInstance(self._sgPyQt)
60         self.createIDs()
61         self.createActions()
62         
63         context_actions = [self.itemDelAction]
64         menu = QMenu(self)
65         for a in context_actions:
66           menu.addAction(a)
67         self._plotController.setBrowserContextualMenu(menu)
68         
69         self.createToolbars()
70         self.createMenus()
71         self.createView()
72         
73         self.connect(self.curveSameFigAction,SIGNAL("activated()"),self.curveSameFig)
74         self.connect(self.curveNewFigAction,SIGNAL("activated()"),self.curveNewFig)
75         self.connect(self.itemDelAction,SIGNAL("activated()"),self.itemDel)
76         self.connect(self.cpsAction,SIGNAL("activated()"),self.clearPlotSet)
77         self.connect(self.plotTableAction,SIGNAL("activated()"),self.plotTable)
78         self.connect(self.addPSAction,SIGNAL("activated()"),self.addPS)
79         self.connect(self.addTabAction,SIGNAL("activated()"),self.addTab)
80         self.connect(self.memAction,SIGNAL("activated()"),self.memPrint)
81         self.connect(self.perfTestAction,SIGNAL("activated()"),self.perfTest)
82
83     def generateID(self):
84         self._currID += 1
85         return self._currID
86     
87     def createIDs(self):
88         # Actions
89         self.curveSameFigActionID = self.generateID()
90         self.curveNewFigActionID = self.generateID()
91         self.itemDelActionID = self.generateID()
92         self.cpsActionID = self.generateID()
93         self.plotTableActionID = self.generateID()
94         self.addPSActionID = self.generateID()
95         self.addTabActionID = self.generateID()
96         self.memActionID = self.generateID()
97         self.perfActionID = self.generateID()
98         
99         # Menus
100         self.etudeMenuID = self.generateID()
101         self.dummyMenuID = self.generateID()
102
103     def createActions(self):
104         ca = self._sgPyQt.createAction
105         self.curveSameFigAction = ca(self.curveSameFigActionID, "Curve on same fig", "Curve on same fig", "", "")
106         self.curveNewFigAction = ca(self.curveNewFigActionID, "Curve on new fig", "Curve on new fig", "", "")
107         self.itemDelAction = ca(self.itemDelActionID, "Delete selected", "Delete selected", "", "")
108         self.cpsAction = ca(self.cpsActionID, "Clear plot set", "Clear plot set", "", "")
109         self.plotTableAction = ca(self.plotTableActionID, "Plot from table", "Plot from table", "", "")
110         self.addPSAction = ca(self.addPSActionID, "Add plot set", "Add plot set", "", "")
111         self.addTabAction = ca(self.addTabActionID, "Add tab", "Add tab", "", "")
112         self.memAction = ca(self.memActionID, "Display used mem", "Display used mem", "", "")
113         self.perfTestAction = ca(self.perfActionID, "Perf test", "Perf test", "", "")
114
115     def createToolbars(self):
116         pass
117 #         self.Toolbar = self._sgPyQt.createTool(self.tr("Toolbar"))
118 #         self._sgPyQt.createTool(self.fileNewAction, self.Toolbar)
119 #         self._sgPyQt.createTool(self.filePrintAction, self.Toolbar)
120 #         sep = self._sgPyQt.createSeparator()
121 #         self._sgPyQt.createTool(sep, self.Toolbar)
122 #         self._sgPyQt.createTool(self.editUndoAction, self.Toolbar)
123 #         self._sgPyQt.createTool(self.editRedoAction, self.Toolbar)
124
125     def createMenus(self):
126         curveMenu = self._sgPyQt.createMenu( "Curve test", -1, self.etudeMenuID, self._sgPyQt.defaultMenuGroup() )
127         self._sgPyQt.createMenu(self.curveSameFigAction, curveMenu)
128         self._sgPyQt.createMenu(self.curveNewFigAction, curveMenu)
129         self._sgPyQt.createMenu(self.itemDelAction, curveMenu)
130         self._sgPyQt.createMenu(self.cpsAction, curveMenu)
131         self._sgPyQt.createMenu(self.plotTableAction, curveMenu)
132         self._sgPyQt.createMenu(self.addPSAction, curveMenu)
133         self._sgPyQt.createMenu(self.memAction, curveMenu)
134         self._sgPyQt.createMenu(self.perfTestAction, curveMenu)
135         
136         dummyMenu = self._sgPyQt.createMenu( "Dummy", -1, self.dummyMenuID, self._sgPyQt.defaultMenuGroup() )
137         self._sgPyQt.createMenu(self.addTabAction, dummyMenu)
138
139     def createView(self):
140         pass
141           
142     def showCurveTreeView(self) :
143         self._dockCurveBrowserView = self._plotController._curveBrowserView
144         self._sgDesktop.addDockWidget(Qt.LeftDockWidgetArea, self._dockCurveBrowserView)
145
146     def __generateRandomData(self, nPoints=100):
147       from random import random
148       x = np.arange(nPoints) / 5.0
149       ampl = 20.0*random() + 1.0
150       y = ampl * np.sin(x*random())
151 #       x = np.arange(5e5)
152 #       y = x
153       return x, y
154        
155     @Slot()  
156     def curveSameFig(self):
157       x, y = self.__generateRandomData()
158       _, ps_id = curveplot.AddCurve(x, y, x_label="the x axis", y_label="the y axis", append=True)
159       curveplot.SetLegendVisible(ps_id, True)
160       if self.cnt >= 0:
161         QTimer.singleShot(self.timeLap, self, SLOT("itemDel()"))
162             
163     def curveNewFig(self):
164       x, y = self.__generateRandomData()
165       curveplot.AddCurve(x, y, x_label="the x axis", y_label="the y axis", append=False)
166     
167     @Slot()
168     def itemDel(self):
169       curveplot.DeleteCurrentItem()
170       if self.cnt >= 0:
171         QTimer.singleShot(self.timeLap, self, SLOT("memPrint()"))
172
173     @Slot()
174     def perfTest(self):
175       lx, ly = [], []
176       nC = 200
177       for _ in range(nC):
178         x, y = self.__generateRandomData(1000)
179         lx.append(x); ly.append(y)
180       print("Done generating")
181       from time import time
182       t0 = time()
183       curveplot.LockRepaint()
184       for i in range(nC): 
185         curveplot.AddCurve(lx[i], ly[i], append=True)
186       curveplot.UnlockRepaint()
187       print("Elapsed: %.2f" % ( time() - t0))
188
189     def clearPlotSet(self):
190       curveplot.ClearPlotSet()
191       
192     def addPS(self):
193       # Also a test for unicode!
194       curveplot.AddPlotSet('ça m embête')
195       
196     def addTab(self):
197       pass
198 #      from PyQt4.QtGui import QPushButton
199 #      self.qp = QPushButton("Hi!")
200 #      self._sgPyQt.createView("Dummy", self.qp)  
201       
202     def plotTable(self):
203       from curveplot import TableModel
204       t = TableModel(None)
205       t.setTitle("coucou")
206       t.addColumn([1.0,2.0,3.0,4.0])
207       t.addColumn([1.0,2.0,3.0,4.0])
208       t.addColumn([1.0,4.0,9.0,16.0])
209       t.setColumnTitle(0, "X-s")
210       t.setColumnTitle(1, "Identity")
211       t.setColumnTitle(2, "Square")
212       cont = curveplot.PlotController.GetInstance()
213       cont.plotCurveFromTable(t, y_col_index=1, append=False)
214       cont.plotCurveFromTable(t, y_col_index=2, append=True)
215     
216     @Slot()
217     def memPrint(self):
218       i, t = curveplot.GetAllPlotSets()
219       print(list(zip(i, t)))
220       new_id = curveplot.CopyCurve(curve_id=0, plot_set_id=1)
221       print("created  curve: %d" % new_id)
222       import resource
223       m = resource.getrusage(resource.RUSAGE_SELF)[2]*resource.getpagesize()/1e6
224       print("** Used memory: %.2f Mb" % m)
225       if self.cnt >= 0 and self.cnt < self.MAX_CNT:
226         self.cnt += 1
227         QTimer.singleShot(self.timeLap, self, SLOT("curveSameFig()"))