aboutsummaryrefslogblamecommitdiff
path: root/mapman/src/sliceview.cpp
blob: 10830280c301c50832f9addc5c50362cae1a61d8 (plain) (tree)
1
2
3
4
5
6
7
8
9







                             
                                
                    
                      



                      
                    
                       




                                     





                                                                       


                                                                            
                              
                           




                                         

                                                                   
                                     














                                                                                 
                                                                                                 
















































                                                                                                                
                                            







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