#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()); } }