aboutsummaryrefslogtreecommitdiff
path: root/mapman/src/groupview.cpp
blob: 32917d470e1ad1df0e75820024bdf6ebf06105c9 (plain) (blame)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "groupview.hpp" 
#include "library.hpp"
#include "painter.hpp"

#include <algorithm>

#include <QTableView>
#include <QStandardItemModel>
#include <QPushButton>
#include <QLineEdit>
#include <QSpinBox>
#include <QLabel>
#include <QSplitter>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGraphicsView>
#include <QSpacerItem>
#include <QHeaderView>
#include <QMessageBox>
#include <qmessagebox.h>

group_view::group_view() : QMdiSubWindow()
{
    auto sp = new QSplitter(this);
    this->setWidget(sp);
    auto leftpane = new QWidget();
    auto l1 = new QVBoxLayout();
    leftpane->setLayout(l1);
    tv = new QTableView();
    m = new QStandardItemModel();
    tv->setModel(m);
    tv->setColumnHidden(3, true);
    tv->setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection);
    tv->setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectRows);
    connect(tv->selectionModel(), &QItemSelectionModel::currentRowChanged,
        [this](const QModelIndex &curidx, const QModelIndex &oldidx) {
            if (oldidx.isValid() && curidx.row() == oldidx.row()) return;
            bool dirty = oldidx.isValid() && this->dirty;
            if (oldidx.isValid())
            {
                int64_t oldgid = m->item(oldidx.row(), 3)->data(Qt::ItemDataRole::DisplayRole).toLongLong();
                auto oldgroup = l->get_group(oldgid);
                dirty |= tetitle->text() != QString::fromStdString(oldgroup.title);
                dirty |= teauthor->text() != QString::fromStdString(oldgroup.author);
                dirty |= sbh->value() != oldgroup.hc;
                dirty |= sbv->value() != oldgroup.vc;
            }
            if (dirty)
            {
                if (QMessageBox::question(this, "Unsaved changes", "Changes to map art not saved! Discard and switch to another map art?") != QMessageBox::StandardButton::Yes)
                {
                    int row = oldidx.row();
                    QMetaObject::invokeMethod(this, [this, row]() {
                        QSignalBlocker b(tv->selectionModel());
                        tv->selectRow(row);
                    }, Qt::ConnectionType::QueuedConnection);
                    return;
                }
            }
            this->update_fields();
    });
    l1->addWidget(tv);
    auto l2 = new QHBoxLayout();
    pbadd = new QPushButton("+");
    pbrem = new QPushButton("-");
    connect(pbadd, &QPushButton::pressed, this, &group_view::add_group);
    connect(pbrem, &QPushButton::pressed, this, &group_view::rem_group);
    l2->addWidget(pbadd);
    l2->addWidget(pbrem);
    l1->addLayout(l2);
    sp->addWidget(leftpane);

    auto rightpane = new QWidget();
    auto l3 = new QVBoxLayout();
    rightpane->setLayout(l3);
    p = new map_painter();
    l3->addWidget(p->view());
    connect(p, &map_painter::map_id_changed, this, &group_view::painter_drop);
    tetitle = new QLineEdit();
    teauthor = new QLineEdit();
    tetitle->setPlaceholderText("Title");
    teauthor->setPlaceholderText("Author(s)");
    l3->addWidget(tetitle);
    l3->addWidget(teauthor);
    auto l4 = new QHBoxLayout();
    sbv = new QSpinBox();
    sbh = new QSpinBox();
    sbv->setValue(1);
    sbv->setMinimum(1);
    sbv->setMaximum(100);
    connect(sbv, QOverload<int>::of(&QSpinBox::valueChanged), this, &group_view::reset_dim);
    sbh->setValue(1);
    sbh->setMinimum(1);
    sbh->setMaximum(100);
    connect(sbh, QOverload<int>::of(&QSpinBox::valueChanged), this, &group_view::reset_dim);
    l4->addWidget(sbh);
    l4->addWidget(new QLabel("x"));
    l4->addWidget(sbv);
    l4->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding));
    pbapply = new QPushButton("Save");
    connect(pbapply, &QPushButton::pressed, this, &group_view::update_library);

    l4->addWidget(pbapply);
    l3->addLayout(l4);
    sp->addWidget(rightpane);

    sp->setStretchFactor(0, 1);
    sp->setStretchFactor(1, 3);
    sp->setCollapsible(0, false);
    sp->setCollapsible(1, false);
    l = nullptr;
    dirty = false;
    this->setWindowTitle("Map art listings");
    this->setAttribute(Qt::WA_DeleteOnClose, false);
}

group_view::~group_view()
{
    delete p;
}

void group_view::set_library(map_library *lib)
{
    l = lib;
    p->set_map_library(l);
    refresh_list();
}

void group_view::add_group()
{
    map_group_t g {
        std::string(),
        std::string(),
        1, 1,
        {0},
        {false}
    };
    l->new_group(g);
    refresh_list();
}

void group_view::rem_group()
{
    if (!tv->currentIndex().isValid())
        return;
    int64_t curgid = m->item(tv->currentIndex().row(), 3)->data(Qt::ItemDataRole::DisplayRole).toLongLong();
    l->remove_group(curgid);
    refresh_list();
}

void group_view::update_fields()
{
    if (!tv->currentIndex().isValid())
        return;
    int64_t curgid = m->item(tv->currentIndex().row(), 3)->data(Qt::ItemDataRole::DisplayRole).toLongLong();
    current_group = l->get_group(curgid);
    auto &g = current_group;
    tetitle->setText(QString::fromStdString(g.title));
    teauthor->setText(QString::fromStdString(g.author));
    QSignalBlocker bh(sbh);
    QSignalBlocker bv(sbv);
    sbh->setValue(g.hc);
    sbv->setValue(g.vc);
    update_map_view();
    dirty = false;
}

void group_view::painter_drop(int pos, bool populated, int id)
{
    if (!tv->currentIndex().isValid())
        return;
    auto &g = current_group;
    g.populated[pos] = populated;
    g.ids[pos] = id;
    dirty = true;
}

void group_view::update_library()
{
    if (!tv->currentIndex().isValid())
        return;
    int64_t curgid = m->item(tv->currentIndex().row(), 3)->data(Qt::ItemDataRole::DisplayRole).toLongLong();
    auto &g = current_group;
    g.title = tetitle->text().toStdString();
    g.author = teauthor->text().toStdString();
    g.hc = sbh->value();
    g.vc = sbv->value();
    l->set_group(curgid, g);
    refresh_list();
    dirty = false;
}

void group_view::refresh_list()
{
    int64_t curgid = -1;
    if (tv->currentIndex().isValid())
        curgid = m->item(tv->currentIndex().row(), 3)->data(Qt::ItemDataRole::DisplayRole).toLongLong();
    m->clear();
    m->setHorizontalHeaderLabels({"Title", "Author(s)", "Dimension", "id"});
    tv->setColumnHidden(3, true);
    auto gids = l->groups();
    for (auto gid : gids)
    {
        map_group_t g = l->get_group(gid);
        QStandardItem *t = new QStandardItem(QString::fromStdString(g.title));
        QStandardItem *a = new QStandardItem(QString::fromStdString(g.author));
        QStandardItem *d = new QStandardItem(QString("%1x%2").arg(g.hc).arg(g.vc));
        QStandardItem *i = new QStandardItem();
        i->setData(QVariant((qlonglong)gid), Qt::ItemDataRole::DisplayRole);
        m->appendRow({t, a, d, i});
        if (gid == curgid)
            tv->setCurrentIndex(t->index());
    }
    tv->resizeColumnsToContents();
}

void group_view::reset_dim()
{
    p->set_dimension(sbh->value(), sbv->value());
    if (!tv->currentIndex().isValid())
        return;
    auto &g = current_group;
    g.hc = sbh->value();
    g.vc = sbv->value();
    g.ids.resize(g.hc * g.vc);
    g.populated.resize(g.hc * g.vc);
    std::fill(g.populated.begin(), g.populated.end(), false);
    dirty = true;
}

void group_view::update_map_view()
{
    auto &g = current_group;
    p->set_dimension(g.hc, g.vc);
    for (int i = 0; i < g.hc * g.vc; ++i)
        p->set_map_id(i, g.populated[i], g.ids[i]);
}