structurelibraryview.cpp 3.87 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * <32021 by Stellaris. Copying Art is an act of love. Love is not subject to law.
 */
#include "structurelibraryview.hpp"
#include "ui_structurelibraryview.h"

#include "structurereader.hpp"

#include <QFile>
#include <QTextStream>
#include <QDebug>

StructureLibraryView::StructureLibraryView(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::StructureLibraryView)
{
    ui->setupUi(this);

    load_structures();

    connect(ui->tree, &QTreeWidget::itemClicked, this, &StructureLibraryView::update_info);
    connect(ui->button_copy, &QPushButton::pressed, this, &StructureLibraryView::copy_button_clicked);
}

StructureLibraryView::~StructureLibraryView()
{
    delete ui;
}

void StructureLibraryView::load_structures()
{
    QTreeWidgetItem* pattern_list = add_directory_contents(QDir("patterns/"));
    pattern_list->setText(0, "Patterns");
    ui->tree->addTopLevelItem(pattern_list);
    pattern_list->setExpanded(true);
}

QTreeWidgetItem *StructureLibraryView::add_directory_contents(const QDir &dir)
{
    QTreeWidgetItem* root = new QTreeWidgetItem;

    QFileInfoList dirs = dir.entryInfoList(QStringList(), QDir::Dirs | QDir::NoDotAndDotDot);
    Q_FOREACH (QFileInfo dir, dirs)
    {
        QTreeWidgetItem *child = add_directory_contents(QDir(dir.absoluteFilePath()));
        // empty directory
        if (child->childCount() == 0)
            continue;

        child->setText(0, dir.fileName());
        root->addChild(child);
    }

    QFileInfoList files = dir.entryInfoList(QStringList() << "*.rle" << "*.json", QDir::Files);
    Q_FOREACH (QFileInfo file, files)
    {
        Structure s;
        bool valid = load_structure(file.absoluteFilePath(), s);
        if (!valid)
            continue;

        QTreeWidgetItem *child = new QTreeWidgetItem();
        child->setText(0, file.fileName());
        // data is the complete filepath of the structure, store it so it can be easily accessed when an item is selected
        child->setData(0, Qt::UserRole, file.absoluteFilePath());

        root->addChild(child);
    }

    return root;
}

bool StructureLibraryView::load_structure(const QString &filename, Structure &s)
{
    QFile f(filename);
    if (!f.open(QFile::ReadOnly | QFile::Text))
        return false;
    QTextStream in(&f);

    std::string data = in.readAll().toStdString();

    if (QFileInfo(filename).suffix() == "json")
    {
        JSONStructureReader json(data);
        try
        {
            s = json.read_structure();
            return true;
        }
        catch (const StructureReaderException& e)
        {
            qDebug() << "StructureReaderException : " << e.what() << "\n";
            return false;
        }
    }
    else if (QFileInfo(filename).suffix() == "rle")
    {
        RLEStructureReader rle(data);
        try
        {
            s = rle.read_structure();
            return true;
        }
        catch (const StructureReaderException& e)
        {
            qDebug() << "StructureReaderException : " << e.what() << "\n";
            return false;
        }
    }
    else
        return false;
}

void StructureLibraryView::update_info(QTreeWidgetItem *item, int column)
{
    (void)column;

    Structure s;
    if (!load_structure(item->data(0, Qt::UserRole).toString(), s))
    {
        ui->button_copy->setEnabled(false);
        return;
    }

    ui->struct_author->setText(QString::fromStdString(s.author));
    ui->struct_date->setText(QString::fromStdString(s.date));
    ui->struct_title->setText(QString::fromStdString(s.title));
    ui->struct_desc->setPlainText(QString::fromStdString(s.desc));

    ui->button_copy->setEnabled(true);
}

void StructureLibraryView::copy_button_clicked()
{
    if (ui->tree->selectedItems().size() == 0)
        return;

    QTreeWidgetItem* item = ui->tree->selectedItems()[0];

    Structure s;
    if (!load_structure(item->data(0, Qt::UserRole).toString(), s))
        return;

    emit structure_copied(s);
}