1 /***************************************************************************
2  *   Copyright (C) 2021 by Abderrahman Taha                                *
3  *                                                                         *
4  *                                                                         *
5  *   This program is free software;you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation;either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY;without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program;if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
19  ***************************************************************************/
20 #include "ParisoMathObject.h"
21 #include <qmessagebox.h>
22 
ParisoMathObject()23 ParisoMathObject::ParisoMathObject()
24 {
25     NbIsoStruct = NbParamStruct = 0;
26     dotsymbol = ".";
27 }
28 
~ParisoMathObject()29 ParisoMathObject::~ParisoMathObject() {}
30 
31 
ReadJsonFile(QString JsonFile,QJsonObject & js)32 void ParisoMathObject::ReadJsonFile(QString JsonFile, QJsonObject &js)
33 {
34     QJsonParseError err;
35     QString sortie;
36     QFile file1(JsonFile);
37     if (!file1.exists())
38     {
39         JsonFile = QFileDialog::getOpenFileName(
40                        nullptr, QObject::tr("Open mathmodcollection.js File"), "",
41                        QObject::tr("Json (*.js)"));
42     }
43     QFile file(JsonFile);
44     if (file.open(QIODevice::ReadOnly | QIODevice::Text))
45     {
46         QJsonDocument doc = QJsonDocument::fromJson(
47                                 ((file.readAll()).trimmed())
48                                 .replace("\n", "")
49                                 .replace("\t", "")
50                                 .replace("DOTSYMBOL", dotsymbol.toStdString().c_str()),
51                                 &err);
52         if (err.error)
53         {
54             QMessageBox message;
55             message.setWindowTitle("Error at : " + JsonFile);
56             file.close();
57             file.open(QIODevice::ReadOnly | QIODevice::Text);
58             sortie = (file.readAll());
59             int before, after;
60             if (sortie.length() > (err.offset + 30))
61                 after = 30;
62             else
63                 after = sortie.length() - err.offset;
64             sortie.truncate(err.offset + after);
65             if (err.offset - 30 > 0)
66                 before = 30;
67             else
68                 before = 0;
69             sortie = sortie.remove(0, err.offset - before);
70             sortie.replace("\t", " ");
71             sortie.replace("\n", " ");
72             sortie.insert(before, " >>> Error <<< ");
73             message.setText("Error : " + err.errorString() +
74                             " at position: " + QString::number(err.offset) +
75                             "\n\n***********\n" + "..." + sortie + "...");
76             message.adjustSize();
77             message.exec();
78             file.close();
79             return;
80         }
81         js = doc.object();
82         file.close();
83     }
84     return;
85 }
86