aboutsummaryrefslogtreecommitdiff
path: root/mapman/src/sliceview.cpp
blob: 10830280c301c50832f9addc5c50362cae1a61d8 (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
#include "sliceview.hpp"
#include "library.hpp"
#include "painter.hpp"
#include "utils.hpp"

#include <QListView>
#include <QGraphicsView>
#include <QStandardItemModel>
#include <QSortFilterProxyModel>
#include <QSplitter>
#include <QVBoxLayout>
#include <QDrag>
#include <QMimeData>
#include <QEvent>
#include <QMouseEvent>
#include <QLineEdit>
#include <QApplication>

slice_view::slice_view() : l(nullptr)
{
    lv = new QListView(this);
    m = new QStandardItemModel(this);
    mf = new QSortFilterProxyModel(this);
    mf->setSourceModel(m);
    mf->setFilterCaseSensitivity(Qt::CaseSensitivity::CaseInsensitive);
    tefilter = new QLineEdit(this);
    tefilter->setPlaceholderText("Filter");
    lv->setModel(mf);
    lv->setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection);
    lv->setDragDropMode(QAbstractItemView::DragDropMode::NoDragDrop);
    lv->viewport()->installEventFilter(this);
    p = new map_painter(this);
    p->set_dimension(1, 1);
    auto leftcontainer = new QWidget();
    auto leftlayout = new QVBoxLayout();
    leftcontainer->setLayout(leftlayout);
    leftlayout->addWidget(lv);
    leftlayout->addWidget(tefilter);
    auto layout = new QSplitter(Qt::Orientation::Horizontal, this);
    layout->setContentsMargins(6, 6, 6, 6);
    layout->addWidget(leftcontainer);
    layout->addWidget(p->view());
    layout->setStretchFactor(0, 1);
    layout->setStretchFactor(1, 3);
    layout->setCollapsible(0, false);
    layout->setCollapsible(1, false);
    this->setWidget(layout);
    connect(lv->selectionModel(), &QItemSelectionModel::currentChanged,
        [this](const QModelIndex &cur, const QModelIndex&) {
            if (this->l)
                this->p->set_map_id(0, true, cur.data(Qt::UserRole + 1).toInt());
        });
    connect(lv, &QAbstractItemView::pressed,
        [this](const QModelIndex &idx) {
            dragidx = idx;
        });
    connect(tefilter, &QLineEdit::textChanged, mf, &QSortFilterProxyModel::setFilterFixedString);
    this->setWindowTitle("Map listings");
    this->setAttribute(Qt::WA_DeleteOnClose, false);
}

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

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

bool slice_view::eventFilter(QObject *o, QEvent *e)
{
    if (e->type() == QEvent::MouseButtonRelease)
        dragidx = QModelIndex();
    if (e->type() == QEvent::MouseButtonPress)
        dragpos = static_cast<QMouseEvent*>(e)->screenPos();
    if (e->type() == QEvent::MouseMove)
    {
        auto pos = static_cast<QMouseEvent*>(e)->screenPos();
        if (dragidx.isValid() && (pos - dragpos).manhattanLength() >= QApplication::startDragDistance())
        {
            auto *d = new QDrag(lv);
            int mapid = dragidx.data(Qt::ItemDataRole::UserRole + 1).toInt();
            d->setPixmap(qvariant_cast<QIcon>(dragidx.data(Qt::ItemDataRole::DecorationRole)).pixmap(128, 128));
            auto *m = new QMimeData();
            m->setData("application/x-map-id", QByteArray(reinterpret_cast<char*>(&mapid), 4));
            d->setMimeData(m);
            d->exec(Qt::DropAction::CopyAction);
        }
    }
    return false;
}

void slice_view::refresh()
{
    int curid = lv->currentIndex().data().toInt();
    m->clear();
    auto ids = l->map_ids();
    for (auto id : ids)
    {
        map_t map = l->get_map(id);
        QPixmap pm = pixmap_of_map_data(map.map_data);
        QString text = QString("(%1)").arg(id);
        if (map.locked) text.append(" [L]");
        if (map.custom_name.length()) text = QString::fromStdString(map.custom_name) + " " + text;
        QStandardItem *itm = new QStandardItem(QIcon(pm), text);
        itm->setData(QVariant(id));
        m->appendRow(itm);
        if (id == curid)
            lv->setCurrentIndex(itm->index());
    }
}