dtkWidgetsLayoutReader.cpp 2.43 KB
Newer Older
WINTZ Julien's avatar
WINTZ Julien committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Version: $Id$
//
//

// Commentary:
//
//

// Change Log:
//
//

// Code:

#include "dtkWidgetsLayout.h"
16 17
#include "dtkWidgetsLayoutItem.h"
#include "dtkWidgetsLayoutItem_p.h"
WINTZ Julien's avatar
WINTZ Julien committed
18 19
#include "dtkWidgetsLayoutReader.h"

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
20 21
#include <QtCore>
#include <QtWidgets>
22 23
#include <QtXml>

24 25 26 27
// ///////////////////////////////////////////////////////////////////
//
// ///////////////////////////////////////////////////////////////////

WINTZ Julien's avatar
WINTZ Julien committed
28 29
class dtkWidgetsLayoutReaderPrivate
{
30 31 32
public:
    QString locate(const QString& file);

WINTZ Julien's avatar
WINTZ Julien committed
33 34 35 36
public:
    dtkWidgetsLayout *layout;
};

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
QString dtkWidgetsLayoutReaderPrivate::locate(const QString& file)
{
    QString path = QStandardPaths::locate(QStandardPaths::AppDataLocation, file);

    if (path.isEmpty()) {
        path += QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).first();
        path += QDir::separator();
        path += file;
    }

    return path;
}

// ///////////////////////////////////////////////////////////////////
//
// ///////////////////////////////////////////////////////////////////

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
54
dtkWidgetsLayoutReader::dtkWidgetsLayoutReader(void) : d(new dtkWidgetsLayoutReaderPrivate)
WINTZ Julien's avatar
WINTZ Julien committed
55
{
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
56

WINTZ Julien's avatar
WINTZ Julien committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70
}

dtkWidgetsLayoutReader::~dtkWidgetsLayoutReader(void)
{
    delete d;
}

void dtkWidgetsLayoutReader::setLayout(dtkWidgetsLayout *layout)
{
    d->layout = layout;
}

void dtkWidgetsLayoutReader::read(void)
{
71 72 73 74 75
    QFile file(d->locate("layout.dtk"));

    if(!file.open(QIODevice::ReadOnly))
        return;

76
    QDomDocument document; document.setContent(&file);
77

78
    QDomNode root = document.firstChild();
79

80
    std::function<void (QDomNode&)> fill;
81

82 83 84 85 86 87 88 89 90
    fill = [&] (QDomNode& node) {

        if (node.childNodes().count() == 2) {

            dtkWidgetsLayoutItem *c = d->layout->current();

            Qt::Orientation orientation = node.toElement().attribute("o") == "h" ? Qt::Horizontal : Qt::Vertical;

            c->d->splitter->setOrientation(orientation);
91

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
            c->split(true);

            dtkWidgetsLayoutItem *a = c->d->a;
            dtkWidgetsLayoutItem *b = c->d->b;

            QDomNode node_a = node.firstChild();
            QDomNode node_b = node.lastChild();

            d->layout->setCurrent(a); fill(node_a);
            d->layout->setCurrent(b); fill(node_b);

        } else {

            QString name = node.toElement().text();

            if(!name.isEmpty())
                d->layout->create(name);

        }
    };

    fill(root);

    file.close();
WINTZ Julien's avatar
WINTZ Julien committed
116 117 118 119
}

//
// dtkWidgetsLayoutReader.cpp ends here