Salome HOME
Updated copyright comment
[modules/gui.git] / src / TreeData / Test / testhelper.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, 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 "testhelper.hxx"
21
22 #include <QFile>
23 #include <QString>
24
25 #include "QtHelper.hxx"
26 #include "MyDataModel.hxx"
27
28 // Standard C include (for getenv)
29 #include <stdlib.h>
30
31 QString TESTHELPER_testfilename(const char * basefilename) {
32   QString aFile;
33   char * GUI_ROOT_DIR = getenv("GUI_ROOT_DIR");
34   QString * root;
35   if ( GUI_ROOT_DIR != NULL ) {
36     root = new QString(GUI_ROOT_DIR);
37   }
38   else {
39     root = new QString("/home/gboulant/development/projets/salome/devel/XSALOME/install");
40   }
41   QString relativePathName = "/share/salome/resources/gui/testdata/";
42   aFile.append(*root + relativePathName + basefilename);
43
44   QLOG("The test file is : "<<aFile);
45   return aFile;
46 }
47
48 /*!
49  * This creates a dummy data object for the needs of the test
50  * functions. The label is the basename of the spĂ©cified pathname.
51  */
52 DataObject * TESTHELPER_dummyObject(QString label) {
53   MyDataObject * dataObject = new MyDataObject();
54   dataObject->setLabel(QCHARSTAR(label));
55   return dataObject;
56 }
57
58
59 #define SEP ";"
60 /*!
61  * This test function shows how it's possible to load data from a file
62  * to populate the tree model.
63  */
64 void TESTHELPER_loadDataFromFile(TreeModel * dataTreeModel, const QString &filename) {
65   QFile file ( filename );
66   file.open ( QIODevice::ReadOnly );
67   
68   MyDataObject * dataObject;
69   while ( 1 ) {
70     QByteArray byteArray = file.readLine();
71
72     if ( byteArray.isEmpty() )
73       break;
74     
75     QString data = (QString ( byteArray.mid(0, byteArray.size()-1))).trimmed();
76     QStringList dataList = data.split ( SEP );
77     // The data list is used here to set properties (and then the path
78     // of location in the tree model).
79
80     dataObject = new MyDataObject();
81     // The label is autogenerated, but we may specify here a custom
82     // one. We just fill the properties with data values read in the
83     // file.
84     dataObject->setProperty(MyDataObject::PROPERTY_KEY_TYPE,    QCHARSTAR(dataList[0]));
85     dataObject->setProperty(MyDataObject::PROPERTY_KEY_REPFONC, QCHARSTAR(dataList[1]));
86     dataObject->setProperty(MyDataObject::PROPERTY_KEY_CIRCUIT, QCHARSTAR(dataList[2]));
87     if ( ! dataTreeModel->addData(dataObject) ) {
88       QLOG("ERR: data not added");
89     }
90   }
91
92   file.close();
93 }