Commit e90d3e15 authored by Tao Zhong's avatar Tao Zhong
Browse files

[WIP] #134 Update ini file reader

parent ef84e19f
......@@ -128,7 +128,6 @@ set( SRCS
src/XML/goalreader.cpp
src/XML/sourcereader.cpp
dxflib/src/dl_writer_ascii.cpp
dxflib/src/dl_dxf.cpp
src/UndoFramework/action.cpp
......
/***************************************************************
*
* \file inireader.cpp
* \date 2019-05-01
* \version v0.8.8
* \author Tao Zhong
* \copyright <2009-2019> Forschungszentrum Jülich GmbH. All rights reserved.
*
* \section Lincense
* This file is part of JuPedSim.
*
* JuPedSim is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* JuPedSim is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JuPedSim. If not, see <http://www.gnu.org/licenses/>.
*
* \section Description
*
* This class is used for read inifile in inifile widget
****************************************************************/
#include "inireader.h"
IniReader::IniReader(InifileWidget *inifileWidget)
{
widget = inifileWidget;
}
bool IniReader::read(QIODevice *device)
{
xml.setDevice(device);
if (xml.readNextStartElement()) {
if (xml.name() == QLatin1String("JuPedSim")
&& xml.attributes().value("version") == QLatin1String("0.8"))
{
readJuPedSim(widget);
} else {
xml.raiseError(QObject::tr("The file is not an JuPedSim version 0.8 file."));
}
}
return !xml.error();
}
QString IniReader::errorString() const
{
return QObject::tr("%1\nLine %2, column %3")
.arg(xml.errorString())
.arg(xml.lineNumber())
.arg(xml.columnNumber());
}
void IniReader::readJuPedSim(InifileWidget *inifileWidget)
{
Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("JuPedSim"));
while (xml.readNextStartElement()) {
if (xml.name() == QLatin1String("seed"))
readSeed(widget);
else if (xml.name() == QLatin1String("geometry"))
qDebug("read geometry!");
else if (xml.name() == QLatin1String("max_sim_time"))
qDebug("read max sim time!");
else
xml.skipCurrentElement();
}
}
void IniReader::readSeed(InifileWidget *widget)
{
Q_ASSERT(xml.isStartElement() && xml.name() == QLatin1String("seed"));
QString title = xml.readElementText();
widget.
}
//
// Created by sainho93 on 2019-05-01.
//
#ifndef JPSEDITOR_INIREADER_H
#define JPSEDITOR_INIREADER_H
#include <QXmlStreamWriter>
#include "src/widgets/inifilewidget.h"
class IniReader
{
public:
IniReader(InifileWidget *inifileWidget);
bool read(QIODevice *device);
QString errorString() const;
void readJuPedSim(InifileWidget *widget);
void readSeed(InifileWidget *widget);
private:
QXmlStreamReader xml;
InifileWidget *widget;
};
#endif //JPSEDITOR_INIREADER_H
......@@ -128,7 +128,6 @@ public:
void set_view(jpsGraphicsView* view);
jpsGraphicsView* get_view();
// Auto Assign
// void AutoAssignCrossings();
// void AutoAssignExits();
......
......@@ -108,9 +108,6 @@ MWindow :: MWindow()
statusBar()->addPermanentWidget(x_edit);
statusBar()->addPermanentWidget(label_y);
statusBar()->addPermanentWidget(y_edit);
// statusBar()->addPermanentWidget(label1);
// statusBar()->addPermanentWidget(length_edit);
// statusBar()->addPermanentWidget(label2);
//Timer needed for autosaving function
// timer will trigger autosave every 5th minute
......@@ -150,10 +147,6 @@ MWindow :: MWindow()
connect(actionRotate_90_deg_clockwise,SIGNAL(triggered(bool)),this,SLOT(rotate()));
connect(actionShow_Point_of_Origin,SIGNAL(triggered(bool)),this,SLOT(ShowOrigin()));
// Length edit
// connect(length_edit,SIGNAL(returnPressed()),this,SLOT(send_length()));
// connect(length_edit,SIGNAL(returnPressed()),this,SLOT(ScaleLines()));
// X Y edit
connect(x_edit,SIGNAL(returnPressed()),this,SLOT(send_xy()));
connect(y_edit,SIGNAL(returnPressed()),this,SLOT(send_xy()));
......@@ -206,7 +199,6 @@ MWindow :: MWindow()
drawingActionGroup->addAction(actionEditMode);
drawingActionGroup->addAction(actionGoal);
connect(actionSelect_Mode,SIGNAL(triggered(bool)),this,SLOT(en_selectMode()));
connect(actionWall,SIGNAL(triggered(bool)),this,SLOT(en_disableWall()));
connect(actionDoor,SIGNAL(triggered(bool)),this,SLOT(en_disableDoor()));
......@@ -217,10 +209,7 @@ MWindow :: MWindow()
connect(actionEditMode,SIGNAL(triggered(bool)),this,SLOT(editModeButtonClicked()));
connect(actionGoal,SIGNAL(triggered(bool)),this,SLOT(goalButtionClicked()));
// connect(actionWall,SIGNAL(triggered(bool)),this,SLOT(dis_selectMode()));
// connect(actionLandmark,SIGNAL(triggered(bool)),this,SLOT(dis_selectMode()));
// connect(actionDoor,SIGNAL(triggered(bool)),this,SLOT(dis_selectMode()));
// connect(actionExit,SIGNAL(triggered(bool)),this,SLOT(dis_selectMode()));
// right dock widget
propertyDockWidget = nullptr;
//object snapping
......@@ -233,7 +222,6 @@ MWindow :: MWindow()
objectsnapping.append(Intersections_point);
objectsnapping.append(Center_point);
objectsnapping.append(SelectedLine_point);
}
MWindow::~MWindow()
......@@ -1013,6 +1001,11 @@ QMap<QString, QString> MWindow::loadSettings()
void MWindow::on_actionNew_Inifile_triggered()
{
inifileWidget = new InifileWidget(this, dmanager);
// status bar
connect(inifileWidget, SIGNAL(inifileLoaded(QString, int)),
this, SLOT(showStatusBarMessage(QString, int)));
inifileWidget->show();
qDebug()<< "MWindow::on_actionNew_Inifile_triggered(): inifile widget is showed!";
}
......@@ -1163,3 +1156,13 @@ void MWindow::openTraffic(QString fileName)
{
}
}
/*
since v0.8.8
Receive message from other widgets, show on status bar
*/
void MWindow::showStatusBarMessage(QString msg, int duration)
{
statusBar()->showMessage(tr(msg.toStdString().data()), duration);
}
......@@ -177,7 +177,7 @@ protected slots:
//ESCAPE
void keyPressEvent(QKeyEvent *event);
void showStatusBarMessage(QString msg, int duration);
private slots:
void on_actionCopy_triggered();
......
......@@ -814,7 +814,6 @@ void InifileWidget::writeHeaderData(QXmlStreamWriter *stream, QFile &file)
stream->writeCharacters(ui->lineEdit_general_06->text());
stream->writeEndElement();
stream->writeComment("trajectories file and format");
stream->writeStartElement("trajectories");
stream->writeAttribute("format",ui->comboBox_general_02->currentText());
......@@ -3332,37 +3331,264 @@ void InifileWidget::ReadRouteChoiceData(TiXmlElement* JuPedSim)
}
}
// Read ini.xml on button push
void InifileWidget::on_pushButton_read_clicked()
{
QString file_name = QFileDialog::getOpenFileName(this,tr("Read ini"),"",tr("XML-Files (*.xml)"));
QFile file(file_name);
QString fileName = QFileDialog::getOpenFileName(this,tr("Read ini"),"",tr("XML-Files (*.xml)"));
if (fileName.isEmpty())
return;
QFile file(fileName);
auto reader = new QXmlStreamReader(&file);
if (!file.open(QIODevice::ReadOnly|QIODevice::Text))
{
QMessageBox::warning(this, tr("JPSeditor"),
tr("Cannot read file %1:\n%2.")
.arg(QDir::toNativeSeparators(fileName),
file.errorString()));
return;
}
//Load data
TiXmlDocument doc;
doc.LoadFile(file_name.toStdString());
TiXmlElement* JuPedSim = doc.RootElement();
if (!readInifile(reader)) {
QMessageBox::warning(this, tr("JPSeditor"),
tr("Parse error in file %1:\n\n%2")
.arg(QDir::toNativeSeparators(fileName),
errorString(reader)));
} else {
emit inifileLoaded(tr("inifile loaded"), 2000);
}
}
//JuPedSim
ReadJuPedSimData(JuPedSim);
bool InifileWidget::readInifile(QXmlStreamReader *reader)
{
if (reader->readNextStartElement())
{
if (reader->name() == QLatin1String("JuPedSim")
&& reader->attributes().value("version") == QLatin1String("0.8"))
{
readJuPedSim(reader);
}
else {
reader->raiseError(QObject::tr("The file is not an JuPedSim version 0.8 file."));
}
}
//header
ReadHeaderData(JuPedSim);
return reader->error();
}
//agents information and distribution
ReadAgentData(JuPedSim);
QString InifileWidget::errorString(QXmlStreamReader *reader) const
{
return QObject::tr("%1\nLine %2, column %3")
.arg(reader->errorString())
.arg(reader->lineNumber())
.arg(reader->columnNumber());
}
//operational model and agent parameters
ReadModelData(JuPedSim);
void InifileWidget::readJuPedSim(QXmlStreamReader *reader)
{
Q_ASSERT(reader->isStartElement() && reader->name() == QLatin1String("JuPedSim"));
QString project = reader->attributes().value("project").toString();
QString version = reader->attributes().value("version").toString();
ui->lineEdit_general_01->setText(project);
ui->lineEdit_general_03->setText(version);
while (reader->readNextStartElement()) {
if (reader->name() == QLatin1String("seed"))
readSeed(reader);
else if (reader->name() == QLatin1String("geometry"))
readGeometry(reader);
else if (reader->name() == QLatin1String("max_sim_time"))
readMaxSimTime(reader);
else if (reader->name() == QLatin1String("trajectories"))
readTrajectories(reader);
else if (reader->name() == QLatin1String("logfile"))
readLogfile(reader);
else if (reader->name() == QLatin1String("traffic_constraints"))
readTrafficConstraints(reader);
else if (reader->name() == QLatin1String("routing"))
readRouting(reader);
else if (reader->name() == QLatin1String("agents"))
readAgents(reader);
else
reader->skipCurrentElement();
}
}
void InifileWidget::readSeed(QXmlStreamReader *reader)
{
Q_ASSERT(reader->isStartElement() && reader->name() == QLatin1String("seed"));
QString title = reader->readElementText();
ui->lineEdit_general_05->setText(title);
}
void InifileWidget::readGeometry(QXmlStreamReader *reader)
{
Q_ASSERT(reader->isStartElement() && reader->name() == QLatin1String("geometry"));
QString title = reader->readElementText();
ui->lineEdit_general_07->setText(title);
}
void InifileWidget::readMaxSimTime(QXmlStreamReader *reader)
{
Q_ASSERT(reader->isStartElement() && reader->name() == QLatin1String("max_sim_time"));
QString title = reader->readElementText();
ui->lineEdit_general_06->setText(title);
}
void InifileWidget::readTrajectories(QXmlStreamReader *reader)
{
Q_ASSERT(reader->isStartElement() && reader->name() == QLatin1String("trajectories"));
QString format = reader->attributes().value("format").toString();
QString fps = reader->attributes().value("fps").toString();
QString color_mode = reader->attributes().value("color_mode").toString();
ui->comboBox_general_02->setCurrentText(format);
ui->lineEdit_general_10->setText(fps);
ui->comboBox_general_03->setCurrentText(color_mode);
while (reader->readNextStartElement()) {
if (reader->name() == QLatin1String("file"))
readTrajectoriesFile(reader);
else
reader->skipCurrentElement();
}
}
void InifileWidget::readTrajectoriesFile(QXmlStreamReader *reader)
{
Q_ASSERT(reader->isStartElement() && reader->name() == QLatin1String("file"));
QString file = reader->attributes().value("location").toString();
ui->lineEdit_general_12->setText(file);
reader->readElementText(); //move current token to end element, so that readNextStartElement can work fine.
}
void InifileWidget::readLogfile(QXmlStreamReader *reader)
{
Q_ASSERT(reader->isStartElement() && reader->name() == QLatin1String("logfile"));
QString logtxt = reader->readElementText();
ui->lineEdit_general_08->setText(logtxt);
}
/*
<traffic_constraints>
<doors>
<door trans_id="1" caption="NaN" state="open" />
</doors>
<file>traffic.xml</file>
</traffic_constraints>
*/
void InifileWidget::readTrafficConstraints(QXmlStreamReader *reader)
{
Q_ASSERT(reader->isStartElement() && reader->name() == QLatin1String("traffic_constraints"));
while (reader->readNextStartElement()) {
if (reader->name() == QLatin1String("file"))
readTrafficFile(reader);
else
reader->skipCurrentElement();
}
}
void InifileWidget::readTrafficFile(QXmlStreamReader *reader)
{
Q_ASSERT(reader->isStartElement() && reader->name() == QLatin1String("file"));
QString file = reader->readElementText();
ui->lineEdit_TrafficFile->setText(file);
}
/*
<routing>
<goals>
<goal id="3" final="true" caption="goal3">
<polygon>
<vertex px="3.2" py="12.0" />
<vertex px="3.2" py="13.0" />
</polygon>
</goal>
</goals>
<file>goals.xml</file>
</routing>
*/
void InifileWidget::readRouting(QXmlStreamReader *reader)
{
Q_ASSERT(reader->isStartElement() && reader->name() == QLatin1String("routing"));
while (reader->readNextStartElement()) {
if (reader->name() == QLatin1String("file"))
readRoutingFile(reader);
else
reader->skipCurrentElement();
}
}
void InifileWidget::readRoutingFile(QXmlStreamReader *reader)
{
Q_ASSERT(reader->isStartElement() && reader->name() == QLatin1String("file"));
QString file = reader->readElementText();
ui->lineEdit_GoalFile->setText(file);
}
/*
<agents operational_model_id="3">
<agents_distribution>
<group group_id="5" room_id="0" subroom_id="0" number="10" router_id="1" agent_parameter_id="1" x_min="3" x_max="5" y_min="3" y_max="5" />
<group group_id="0" room_id="0" subroom_id="0" number="0" router_id="1" agent_parameter_id="1" />
</agents_distribution>
<agents_sources>
<source_ id="10" caption="new-source" time_min="5" time_max="30" frequency="5" N_create="10" agents_max="300"
group_id="0" x_min="0" x_max="3" y_min="0" y_max="3" percent="0.5" rate="2" greedy="true"/>
<file>sources.xml</file>
</agents_sources>
</agents>
*/
void InifileWidget::readAgents(QXmlStreamReader *reader)
{
Q_ASSERT(reader->isStartElement() && reader->name() == QLatin1String("agents"));
while (reader->readNextStartElement()) {
if (reader->name() == QLatin1String("agents_distribution"))
readAgentsDistribution(reader);
else if (reader->name() == QLatin1String("agents_sources"))
readAgentsSources(reader);
else
reader->skipCurrentElement();
}
}
void InifileWidget::readAgentsDistribution(QXmlStreamReader *reader)
{
//TODO: work later from here!
}
void InifileWidget::readGroup(QXmlStreamReader *reader)
{
}
void InifileWidget::readAgentsSources(QXmlStreamReader *reader)
{
}
void InifileWidget::readSourceFile(QXmlStreamReader *reader)
{
//route_choice_models
ReadRouteChoiceData(JuPedSim);
}
/*
......@@ -3402,6 +3628,14 @@ void InifileWidget::pushButton_TrafficClicked()
ui->lineEdit_TrafficFile->setText(fileName);
}
void InifileWidget::pushButton_RoutingClicked()
{
QString fileName=QFileDialog::getOpenFileName(this,tr("Choose Routing"),""
,tr("XML-Files (*.xml)"));
ui->lineEdit_route_choice_03->setText(fileName);
}
/*
<routing>
<goals>
......@@ -3470,17 +3704,4 @@ void InifileWidget::writeTrafficData(QXmlStreamWriter *stream, QFile &file)
stream->writeEndElement(); //end files
stream->writeEndElement(); //end doors
stream->writeEndElement(); //end traffic_constraints
}
void InifileWidget::pushButton_RoutingClicked()
{
QString fileName=QFileDialog::getOpenFileName(this,tr("Choose Routing"),""
,tr("XML-Files (*.xml)"));
ui->lineEdit_route_choice_03->setText(fileName);
}
void InifileWidget::readTrafficFile(QFile &file)
{
//TODO: do this in v0.8.9
}
}
\ No newline at end of file
......@@ -42,6 +42,26 @@ public slots:
void pushButton_TrafficClicked();
void pushButton_RoutingClicked();
//Read inifile
bool readInifile(QXmlStreamReader *reader);
QString errorString(QXmlStreamReader *reader) const;
void readJuPedSim(QXmlStreamReader *reader);
void readSeed(QXmlStreamReader *reader);
void readGeometry(QXmlStreamReader *reader);
void readMaxSimTime(QXmlStreamReader *reader);
void readTrajectories(QXmlStreamReader *reader);
void readTrajectoriesFile(QXmlStreamReader *reader);
void readLogfile(QXmlStreamReader *reader);
void readTrafficConstraints(QXmlStreamReader *reader);
void readTrafficFile(QXmlStreamReader *reader);
void readRouting(QXmlStreamReader *reader);
void readRoutingFile(QXmlStreamReader *reader);
void readAgents(QXmlStreamReader *reader);
void readAgentsDistribution(QXmlStreamReader *reader);
void readAgentsSources(QXmlStreamReader *reader);
void readGroup(QXmlStreamReader *reader);
void readSourceFile(QXmlStreamReader *reader);
private:
Ui::InifileWidget *ui;
......@@ -83,7 +103,9 @@ private:
void ReadAgentData(TiXmlElement* JuPedSim);
void ReadModelData(TiXmlElement* JuPedSim);
void ReadRouteChoiceData(TiXmlElement* JuPedSim);
void readTrafficFile(QFile &file);
signals:
void inifileLoaded(QString, int);
};
#endif // INIFILEWIDGET_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment