Salome HOME
Copyright update 2022
[modules/gui.git] / tools / CurvePlot / src / cpp / test / test_curveplot.cxx
1 // Copyright (C) 2010-2022  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Adrien BRUNETON
20 //
21
22 #include <PyInterp_Utils.h>  // GUI - must come first to avoid conflict on the token "slots" defined in both Qt and Python ...
23
24 #include "test_curveplot.hxx"
25
26 #include <iostream>
27 #include <vector>
28 #include "CurvePlot.hxx"
29
30 #include <PyInterp_Interp.h>  // GUI
31
32 #include <QApplication>
33 #include <QDesktopWidget>
34 #include <QMainWindow>
35 #include <QList>
36 #include <QWidget>
37 #include <QVBoxLayout>
38 #include <QDockWidget>
39 #include <QPushButton>
40
41 using namespace CURVEPLOT;
42
43 /* The real test is in this function ! */
44 void TestCurvePlot::onClicked()
45 {
46   int ps_id = -1;
47   std::cout << "click\n" << std::endl;
48
49   /* Now the real test: */
50 //  std::vector<double> x = {1.0,2.0,3.0,4.0,5.0,6.0,7.0};
51   std::vector<double> x;
52   for (int i=1; i <= 7; i++)
53     x.push_back(double(i));
54   //std::vector<double> y = {1.0,4.0,9.0,16.0,25.0,36.0,49.0};
55   std::vector<double> y;
56   for (int i=0; i < 7; i++)
57       y.push_back(double(i*i));
58 //  std::vector<double> x(2000);
59 //  std::vector<double> y(2000);
60 //  for(int i = 0 ; i < x.size(); i++)
61 //    {
62 //      x[i] = i*1.0;
63 //      y[i] = i*2.0;
64 //    }
65   ColumnVector xx(ColumnVector::BuildFromStdVector(x));
66   ColumnVector yy(ColumnVector::BuildFromStdVector(y));
67 //  std::string s = xx.toStdString();
68 //  std::cout << "test xx: " << s << std::endl;
69   std::cout << "setting X label " << CurvePlot::SetXLabel("tôtô") << std::endl;
70   /*PlotID crv_id = */CurvePlot::AddCurve(xx, yy, ps_id, "the cérve", "th x", "the y-s", false);
71   std::cout << "setting X label " << CurvePlot::SetXLabel("tôtô") << std::endl;
72 }
73
74 /*!
75  * Similar to: SUIT_PYTHON::init_python() from GUI
76  *  or         KERNEL_PYTHON::init_python() from KERNEL
77  */
78 void initPython()
79 {
80   if (!Py_IsInitialized()){
81       // Python is not initialized
82       Py_Initialize(); // Initialize the interpreter
83
84       PyRun_SimpleString("import threading\n");
85
86       PyThreadState *pts = PyGILState_GetThisThreadState();
87       PyEval_ReleaseThread(pts);
88   }
89 }
90
91 /* Little hack to gather widgets created on the Python side  */
92 void getWigdets(QApplication * app, QWidget *& crvBrowser, QWidget *& tabWidget)
93 {
94   QList<QWidget *> lst(app->topLevelWidgets());
95   crvBrowser = NULL;
96   tabWidget = NULL;
97   foreach(QWidget * w, lst)
98   {
99     if (w->objectName() == QString("TabWidget"))
100       tabWidget = w;
101     if (w->objectName() == QString("CurveTreeDockWidget"))
102       crvBrowser = w;
103   }
104 }
105
106 int main(int argc, char ** argv)
107 {
108   int ret = -1;
109   /* The below part is done automatically in SALOME context */
110   QApplication app (argc, argv);
111   QDesktopWidget * dw = app.desktop();
112
113   QMainWindow mw;
114   mw.resize((int)(dw->width()*0.25), (int)(dw->height()*0.7));
115   mw.show();
116
117   initPython();   // mimic SALOME Python's initialisation.
118   InitializeCurvePlot();
119
120   {
121     // Make sure the first instanciation of CurvePlot is made in test mode!
122     CurvePlot::ToggleCurveBrowser(false);
123     CurvePlot::GetInstance(true);
124
125     QWidget * crvBrowser = 0, * tabWidget = 0;
126     getWigdets(&app, crvBrowser, tabWidget);
127     QDockWidget * dock = new QDockWidget(&mw);
128     QPushButton * but = new QPushButton("Hello");
129     TestCurvePlot * t2d = new TestCurvePlot();
130     but->connect(but, SIGNAL(clicked()), t2d, SLOT(onClicked()));
131     QWidget * w = new QWidget(dock);
132     dock->setWidget(w);
133     QVBoxLayout * vbl = new QVBoxLayout(w);
134     vbl->addWidget(but);
135     if (crvBrowser)
136       vbl->addWidget(crvBrowser);
137     mw.addDockWidget(Qt::LeftDockWidgetArea, dock);
138     mw.setCentralWidget(tabWidget);
139
140     /* Finalization */
141     ret = app.exec();
142   }
143
144   Py_Finalize(); // must be after GIL release
145   return ret;
146 }