From 93cf929f29dea490ed60e5300cacdd99886c988e Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Sat, 9 Sep 2023 20:09:50 -0400 Subject: Add the standalone portion of mapman. --- mapman/src/sliceview.cpp | 101 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 mapman/src/sliceview.cpp (limited to 'mapman/src/sliceview.cpp') diff --git a/mapman/src/sliceview.cpp b/mapman/src/sliceview.cpp new file mode 100644 index 0000000..1dc385b --- /dev/null +++ b/mapman/src/sliceview.cpp @@ -0,0 +1,101 @@ +#include "sliceview.hpp" +#include "library.hpp" +#include "painter.hpp" +#include "utils.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +slice_view::slice_view() : l(nullptr) +{ + lv = new QListView(this); + m = new QStandardItemModel(this); + lv->setModel(m); + lv->setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection); + lv->setDragDropMode(QAbstractItemView::DragDropMode::NoDragDrop); + lv->viewport()->installEventFilter(this); + p = new map_painter(); + p->set_dimension(1, 1); + auto layout = new QSplitter(Qt::Orientation::Horizontal, this); + layout->setContentsMargins(6, 6, 6, 6); + layout->addWidget(lv); + 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; + }); + 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(e)->screenPos(); + if (e->type() == QEvent::MouseMove) + { + auto pos = static_cast(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(dragidx.data(Qt::ItemDataRole::DecorationRole)).pixmap(128, 128)); + auto *m = new QMimeData(); + m->setData("application/x-map-id", QByteArray(reinterpret_cast(&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.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()); + } +} -- cgit v1.2.3