dtkWidgetsLayoutWriter.cpp 3.01 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
#include "dtkWidgetsLayoutWriter.h"
19 20
#include "dtkWidgetsWidget.h"

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

25 26 27
// ///////////////////////////////////////////////////////////////////
//
// ///////////////////////////////////////////////////////////////////
WINTZ Julien's avatar
WINTZ Julien committed
28 29 30

class dtkWidgetsLayoutWriterPrivate
{
31 32 33
public:
    QString locate(const QString& file);

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

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
QString dtkWidgetsLayoutWriterPrivate::locate(const QString& file)
{
    QString path = QStandardPaths::locate(QStandardPaths::AppDataLocation, file);

    if (path.isEmpty()) {
        path += QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).first();

        QDir dummy; dummy.mkdir(path);

        path += QDir::separator();
        path += file;
    }

    return path;
}

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


WINTZ Julien's avatar
WINTZ Julien committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
dtkWidgetsLayoutWriter::dtkWidgetsLayoutWriter(void)
{
    d = new dtkWidgetsLayoutWriterPrivate;
}

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

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

void dtkWidgetsLayoutWriter::write(void)
{
76 77 78 79 80 81
    QFile file(d->locate("layout.dtk"));

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

// ///////////////////////////////////////////////////////////////////
82
// Build document
83 84
// ///////////////////////////////////////////////////////////////////

85 86 87
    QDomDocument document;

    QDomElement r = document.createElement("r");
88

89 90
    if (d->layout->root()->d->a)
        r.setAttribute("o", d->layout->root()->d->splitter->orientation() == Qt::Horizontal ? "h" : "v");
91

92 93 94
    std::function<void (QDomNode&, dtkWidgetsLayoutItem *)> fill;

    fill = [&] (QDomNode& node, dtkWidgetsLayoutItem *item) {
95 96

        if (item->d->a) {
97 98 99 100 101 102 103 104 105

            QDomElement a = document.createElement("a");

            if (item->d->a->d->a && item->d->a->d->b)
                a.setAttribute("o", item->d->splitter->orientation() == Qt::Horizontal ? "v" : "h");

            fill(a, item->d->a);

            node.appendChild(a);
106 107 108 109
        }

        if (item->d->b) {

110 111 112 113 114 115 116 117
            QDomElement b = document.createElement("b");

            if (item->d->b->d->a && item->d->b->d->b)
                b.setAttribute("o", item->d->splitter->orientation() == Qt::Horizontal ? "v" : "h");

            fill(b, item->d->b);

            node.appendChild(b);
118
        }
119

120
        if(!item->d->a && !item->d->b && item->proxy()->d->view) {
121

122
            QDomNode p = document.createTextNode(item->proxy()->d->view->objectName());
123

124 125 126
            node.appendChild(p);
        }
    };
127

128
    fill(r, d->layout->root());
129

130
    document.appendChild(r);
131

132
    QTextStream stream(&file);
133

134
    stream << document.toString();
135 136

    file.close();
WINTZ Julien's avatar
WINTZ Julien committed
137 138 139 140
}

//
// dtkWidgetsLayoutWriter.cpp ends here