Salome HOME
05e264c9a551ff47da5f513a218c1e0446a04097
[modules/gui.git] / src / TreeData / Test / guitester.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
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
20 #include <QApplication>
21 #include <QMainWindow>
22 #include <QDockWidget>
23 #include <QTreeView>
24
25 #include <Basics_Utils.hxx>
26
27 //
28 // =================================================================
29 // Generic helper functions
30 // =================================================================
31 //
32 /*!
33  * This functions displays a main window that embeds the specified
34  * widget. A dockwidget is used to create a context similar to as the
35  * SALOME target context.
36  */
37 void showWidget(QWidget * widget) {
38
39   QMainWindow * window = new QMainWindow();
40
41   // Prepare a gui framework for testing the widget. We use a
42   // dockwidget, just to be in a context similar to as the SALOME
43   // target context.
44   QDockWidget * dwDataPanel = new QDockWidget(window);
45   dwDataPanel->setVisible(true);
46   dwDataPanel->setWindowTitle("XCAD data model");
47   window->addDockWidget(Qt::LeftDockWidgetArea, dwDataPanel);
48
49   // Then plug the widget in the dock widget framework:
50   widget->setParent(dwDataPanel);
51   widget->setMinimumHeight(300);
52   dwDataPanel->setWidget(widget);
53
54   window->show();
55 }
56
57 //
58 // =================================================================
59 // Tests functions for TreeModel
60 // =================================================================
61 //
62 #include "TreeModel.hxx"
63 #include "MyDataModel.hxx"
64 #include "testhelper.hxx"
65
66
67 /*!
68  * This function fills the specified tree with data that show
69  * different levels of path in the tree.
70  */
71 void _TEST_treemodel_addData_01(TreeModel * dataTreeModel) {
72   // We can first add categories (for example to set categories
73   // properties)
74   QStringList path;
75   DataObject * folder;
76
77   path << "folder_1";
78   folder = TESTHELPER_dummyObject("folder_1.1");
79   dataTreeModel->addData(folder, path);
80   folder = TESTHELPER_dummyObject("folder_1.2");
81   dataTreeModel->addData(folder, path);
82
83   path.clear();
84   path << "folder_2";
85   folder = TESTHELPER_dummyObject("folder_2.1");
86   dataTreeModel->addData(folder, path);
87   
88   // Then we can add data
89   DataObject * data;
90   path.clear();
91   path << "folder_1" << "folder_1.1";
92   data = TESTHELPER_dummyObject("data_1.1.1");
93   dataTreeModel->addData(data, path);
94   data = TESTHELPER_dummyObject("data_1.1.2");
95   dataTreeModel->addData(data, path);
96   // You can notice that there is no conceptual difference between a
97   // folder and an item, as in the QTreeModel.
98   
99   // No limit to the depth
100   path.clear();
101   path << "xcad" << "data1" << "x" << "y";
102   data = TESTHELPER_dummyObject("z");
103   dataTreeModel->addData(data,path);
104 }
105
106 #define LOOPSIZE 15
107 /*!
108  * This function fills the specified tree with a huge amount of data
109  */
110 void _TEST_treemodel_addData_02(TreeModel * dataTreeModel) {
111   QStringList path;
112   DataObject * data;
113   
114   START_TIMING(treemodel);
115   for (int i=0; i<LOOPSIZE; i++) {
116     END_TIMING(treemodel,1);
117     for (int j=0; j<LOOPSIZE; j++) {
118       for (int k=0; k<LOOPSIZE; k++) {
119         // The data list corresponds to the path of the item in the tree
120         path << QString("folder_%0").arg(i)
121              << QString("subfolder_%0_%1").arg(i).arg(j);
122         data = TESTHELPER_dummyObject(QString("item_%0_%1_%2").arg(i).arg(j).arg(k));
123         dataTreeModel->addData(data,path);
124         path.clear();
125       } 
126     }
127   }
128   END_TIMING(treemodel,1);
129 }
130
131 void _TEST_treemodel_addData_03(TreeModel * dataTreeModel) {
132   MyDataObject * dataObject = new MyDataObject();
133   dataObject->setProperty(MyDataObject::PROPERTY_KEY_TYPE,    "Tuyauterie");
134   dataObject->setProperty(MyDataObject::PROPERTY_KEY_CIRCUIT, "RCP");
135   dataObject->setProperty(MyDataObject::PROPERTY_KEY_REPFONC, "RF1");
136   dataTreeModel->addData(dataObject);
137
138   dataObject = new MyDataObject();
139   dataObject->setProperty(MyDataObject::PROPERTY_KEY_TYPE,    "Tuyauterie");
140   dataObject->setProperty(MyDataObject::PROPERTY_KEY_CIRCUIT, "RCP");
141   dataObject->setProperty(MyDataObject::PROPERTY_KEY_REPFONC, "RF1");
142   dataTreeModel->addData(dataObject);
143
144   dataObject = new MyDataObject();
145   dataObject->setProperty(MyDataObject::PROPERTY_KEY_TYPE,    "Tuyauterie");
146   dataObject->setProperty(MyDataObject::PROPERTY_KEY_CIRCUIT, "RCP");
147   dataObject->setProperty(MyDataObject::PROPERTY_KEY_REPFONC, "RF2");
148   dataTreeModel->addData(dataObject);
149
150   dataObject = new MyDataObject();
151   dataObject->setProperty(MyDataObject::PROPERTY_KEY_TYPE,    "Tuyauterie");
152   dataObject->setProperty(MyDataObject::PROPERTY_KEY_CIRCUIT, "RRA");
153   dataObject->setProperty(MyDataObject::PROPERTY_KEY_REPFONC, "RF1");
154   dataTreeModel->addData(dataObject);
155
156   dataObject = new MyDataObject();
157   dataObject->setProperty(MyDataObject::PROPERTY_KEY_TYPE,    "GĂ©nie civil");
158   dataObject->setProperty(MyDataObject::PROPERTY_KEY_CIRCUIT, "RRA");
159   dataObject->setProperty(MyDataObject::PROPERTY_KEY_REPFONC, "RF1");
160   dataTreeModel->addData(dataObject);
161 }
162
163 /*!
164  * This test function shows how it's possible to load data from a file
165  * to populate the tree model.
166  */
167 void _TEST_treemodel_loadDataFromFile(TreeModel * dataTreeModel, const QString &filename) {
168   TESTHELPER_loadDataFromFile(dataTreeModel, filename);
169 }
170
171 /*!
172  * Main test function for the tree model demo.
173  */
174 #include "TreeModel.hxx"
175 #include "TreeView.hxx"
176 void TEST_treemodel() {
177
178   START_TIMING(treemodel);
179
180   // We first prepare a data view embedding a tree model
181   TreeView * dataView = new TreeView();
182   QStringList headers;
183   headers << QObject::tr("Name") << QObject::tr("Value");
184   TreeModel * dataTreeModel = new TreeModel(headers);
185   dataView->setModel(dataTreeModel);
186   END_TIMING(treemodel,1);
187
188   // Then we can fill the tree model with data. Can proceed with
189   // different ways (comment/uncomment which you want to test):
190   _TEST_treemodel_loadDataFromFile(dataTreeModel, TESTHELPER_testfilename(DATAFILENAME));
191   //_TEST_treemodel_addData_01(dataTreeModel);
192   //_TEST_treemodel_addData_02(dataTreeModel);
193   //_TEST_treemodel_addData_03(dataTreeModel);
194   // Finally, show the widget in a main window
195   END_TIMING(treemodel,1);
196
197   showWidget(dataView);
198   END_TIMING(treemodel,1);
199 }
200
201 //
202 // =================================================================
203 // Tests functions for TreeModel with interactive changes
204 // =================================================================
205 //
206 #include "mainwindow.hxx"
207 void TEST_treemodel_interactif() {
208   MainWindow * window = new MainWindow();
209   window->show();
210 }
211
212 //
213 // =================================================================
214 //
215 int main(int argc, char * argv[ ])
216 {
217   QApplication app(argc, argv);
218   TEST_treemodel();
219   //TST_treemodel_interactif();
220   return app.exec();
221 }