===== Čtení dat ve formátu JSON =====
Opět používáme **QJsonObject**, \\
předefinované **[ ]** používáme s indexem obsahujícím jméno položky, \\
výsledkem //"indexace"// je pomocný objekt s metodami **toString** a **toInt**, \\
pokud položka v objektu neexistuje je výsledkem prázdný řetězec, nula nebo hodnota zadaná jako parametr.
void readItems (QJsonObject obj, QGraphicsScene * scene, QGraphicsItem * target);
void readItem (QJsonObject obj, QGraphicsScene * scene, QGraphicsItem * target)
{
QString type = obj ["type"].toString ();
QGraphicsItem * item = createItem (type);
if (item != nullptr)
{
item->setToolTip (obj ["name"].toString ());
int x = obj ["x"].toInt ();
int y = obj ["y"].toInt ();
item->setPos (x, y);
if (QAbstractGraphicsShapeItem * shape = dynamic_cast < QAbstractGraphicsShapeItem * > (item))
{
QColor c = getColor (obj ["pen"].toString (), "red");
QColor d = getColor (obj ["brush"].toString (), "yellow");
shape->setPen (c);
shape->setBrush (d);
if (QGraphicsRectItem * r = dynamic_cast < QGraphicsRectItem * > (shape))
{
int w = obj ["width"].toInt (100);
int h = obj ["height"].toInt (80);
r->setRect (0, 0, w, h);
}
if (QGraphicsEllipseItem * e = dynamic_cast < QGraphicsEllipseItem * > (shape))
{
int w = obj ["width"].toInt (100);
int h = obj ["height"].toInt (80);
e->setRect (0, 0, w, h);
}
}
if (QGraphicsLineItem * t = dynamic_cast < QGraphicsLineItem * > (item))
{
int w = obj ["width"].toInt (100);
int h = obj ["height"].toInt (80);
t->setLine (0, 0, w, h);
}
readItems (obj, scene, item); // read inner items, add to this item
setupItem (item); // set movable flag
if (target != nullptr)
item->setParentItem (target);
else
scene->addItem (item);
}
}
V případě položky //items// zkusíme zda ji právě čtený JSON objekt obsahuje a zda je to pole/seznam. \\
Metoda **toArray** vyzvedne seznam do proměnné typu **QJsonArray** a **for** cyklus zpracuje jednotlivé prvky.
void readItems (QJsonObject obj, QGraphicsScene * scene, QGraphicsItem * target)
{
if (obj.contains ("items") && obj ["items"].isArray())
{
QJsonArray list = obj ["items"].toArray ();
for (QJsonValue item : list)
{
if (item.isObject ())
readItem (item.toObject (), scene, target);
}
}
}
**QJsonDocument** převede vstupní řetězec znaků (obsah vstupního souboru) na **QJsonObject**.
void readJson (QByteArray code, QGraphicsScene * scene, QGraphicsItem * target)
{
QJsonDocument doc = QJsonDocument::fromJson (code);
QJsonObject obj = doc.object ();
readItems (obj, scene, target);
}
Pozměníme metodu on_actionOpen_triggered volanou z menu.
void MainWindow::loadFile (QString fileName, bool json)
{
QFile f (fileName);
if (f.open (QFile::ReadOnly))
{
if (json)
{
QByteArray code = f.readAll ();
readJson (code, scene);
}
else
{
QXmlStreamReader r (&f);
readXml (r, scene);
}
refreshTree ();
}
else
{
QMessageBox::warning (NULL, "Open File Error", "Cannot read file: " + fileName);
}
}
void MainWindow::on_actionOpen_triggered ()
{
QString selectedFilter;
QString fileName = QFileDialog::getOpenFileName (this, "Open file", dir, filter, &selectedFilter);
if (fileName != "")
// loadFile (fileName, selectedFilter.startsWith (jsonFilter));
loadFile (fileName, fileName.endsWith (".json"));
}