1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Rubber band for selection

This commit is contained in:
nordsoft 2022-12-04 14:51:01 +04:00
parent 8dc0682d81
commit 20d1d346bb
7 changed files with 46 additions and 50 deletions

View File

@ -853,12 +853,11 @@ void MainWindow::on_toolErase_clicked()
ui->tabWidget->setCurrentIndex(0); ui->tabWidget->setCurrentIndex(0);
} }
void MainWindow::preparePreview(const QModelIndex &index, bool createNew) void MainWindow::preparePreview(const QModelIndex &index)
{ {
scenePreview->clear(); scenePreview->clear();
auto data = objectsModel.itemFromIndex(objectBrowser->mapToSource(index))->data().toJsonObject(); auto data = objectsModel.itemFromIndex(objectBrowser->mapToSource(index))->data().toJsonObject();
if(!data.empty()) if(!data.empty())
{ {
auto preview = data["preview"]; auto preview = data["preview"];
@ -866,29 +865,12 @@ void MainWindow::preparePreview(const QModelIndex &index, bool createNew)
{ {
QPixmap objPreview = pixmapFromJson(preview); QPixmap objPreview = pixmapFromJson(preview);
scenePreview->addPixmap(objPreview); scenePreview->addPixmap(objPreview);
auto objId = data["id"].toInt();
auto objSubId = data["subid"].toInt();
auto templateId = data["template"].toInt();
if(controller.discardObject(mapLevel) || createNew)
{
auto factory = VLC->objtypeh->getHandlerFor(objId, objSubId);
auto templ = factory->getTemplates()[templateId];
controller.createObject(mapLevel, factory->create(templ));
}
} }
} }
} }
void MainWindow::treeViewSelected(const QModelIndex & index, const QModelIndex & deselected) void MainWindow::treeViewSelected(const QModelIndex & index, const QModelIndex & deselected)
{
preparePreview(index, false);
}
void MainWindow::on_treeView_activated(const QModelIndex &index)
{ {
ui->toolBrush->setChecked(false); ui->toolBrush->setChecked(false);
ui->toolBrush2->setChecked(false); ui->toolBrush2->setChecked(false);
@ -896,11 +878,10 @@ void MainWindow::on_treeView_activated(const QModelIndex &index)
ui->toolArea->setChecked(false); ui->toolArea->setChecked(false);
ui->toolLasso->setChecked(false); ui->toolLasso->setChecked(false);
ui->mapView->selectionTool = MapView::SelectionTool::None; ui->mapView->selectionTool = MapView::SelectionTool::None;
preparePreview(index, true); preparePreview(index);
} }
void MainWindow::on_terrainFilterCombo_currentTextChanged(const QString &arg1) void MainWindow::on_terrainFilterCombo_currentTextChanged(const QString &arg1)
{ {
if(!objectBrowser) if(!objectBrowser)

View File

@ -74,8 +74,6 @@ private slots:
void on_toolErase_clicked(); void on_toolErase_clicked();
void on_treeView_activated(const QModelIndex &index);
void on_terrainFilterCombo_currentTextChanged(const QString &arg1); void on_terrainFilterCombo_currentTextChanged(const QString &arg1);
void on_filter_textChanged(const QString &arg1); void on_filter_textChanged(const QString &arg1);
@ -113,7 +111,7 @@ public slots:
void displayStatus(const QString& message, int timeout = 2000); void displayStatus(const QString& message, int timeout = 2000);
private: private:
void preparePreview(const QModelIndex &index, bool createNew); void preparePreview(const QModelIndex & index);
void addGroupIntoCatalog(const std::string & groupName, bool staticOnly); void addGroupIntoCatalog(const std::string & groupName, bool staticOnly);
void addGroupIntoCatalog(const std::string & groupName, bool useCustomName, bool staticOnly, int ID); void addGroupIntoCatalog(const std::string & groupName, bool useCustomName, bool staticOnly, int ID);

View File

@ -65,12 +65,10 @@ MapView::MapView(QWidget * parent):
void MapView::cameraChanged(const QPointF & pos) void MapView::cameraChanged(const QPointF & pos)
{ {
//ui->mapView->translate(pos.x(), pos.y());
horizontalScrollBar()->setValue(pos.x()); horizontalScrollBar()->setValue(pos.x());
verticalScrollBar()->setValue(pos.y()); verticalScrollBar()->setValue(pos.y());
} }
void MapView::setController(MapController * ctrl) void MapView::setController(MapController * ctrl)
{ {
controller = ctrl; controller = ctrl;
@ -86,7 +84,12 @@ void MapView::mouseMoveEvent(QMouseEvent *mouseEvent)
auto pos = mapToScene(mouseEvent->pos()); //TODO: do we need to check size? auto pos = mapToScene(mouseEvent->pos()); //TODO: do we need to check size?
int3 tile(pos.x() / 32, pos.y() / 32, sc->level); int3 tile(pos.x() / 32, pos.y() / 32, sc->level);
//if scene will be scrolled without mouse movement, selection, object moving and rubber band will not be updated
//to change this behavior, all this logic should be placed in viewportEvent
if(rubberBand)
rubberBand->setGeometry(QRect(mapFromScene(mouseStart), mouseEvent->pos()).normalized());
if(tile == tilePrev) //do not redraw if(tile == tilePrev) //do not redraw
return; return;
@ -162,7 +165,7 @@ void MapView::mouseMoveEvent(QMouseEvent *mouseEvent)
if(sh.x || sh.y) if(sh.x || sh.y)
{ {
if(sc->selectionObjectsView.newObject && (mouseEvent->buttons() & Qt::LeftButton)) if(!sc->selectionObjectsView.newObject && (mouseEvent->buttons() & Qt::LeftButton))
{ {
if(sc->selectionObjectsView.selectionMode == SelectionObjectsLayer::SELECTION) if(sc->selectionObjectsView.selectionMode == SelectionObjectsLayer::SELECTION)
{ {
@ -291,6 +294,11 @@ void MapView::mousePressEvent(QMouseEvent *event)
{ {
sc->selectionObjectsView.clear(); sc->selectionObjectsView.clear();
sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::SELECTION; sc->selectionObjectsView.selectionMode = SelectionObjectsLayer::SELECTION;
if(!rubberBand)
rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
rubberBand->setGeometry(QRect(mapFromScene(mouseStart), QSize()));
rubberBand->show();
} }
} }
sc->selectionObjectsView.shift = QPoint(0, 0); sc->selectionObjectsView.shift = QPoint(0, 0);
@ -309,6 +317,9 @@ void MapView::mouseReleaseEvent(QMouseEvent *event)
auto * sc = static_cast<MapScene*>(scene()); auto * sc = static_cast<MapScene*>(scene());
if(!sc || !controller->map()) if(!sc || !controller->map())
return; return;
if(rubberBand)
rubberBand->hide();
switch(selectionTool) switch(selectionTool)
{ {
@ -319,18 +330,6 @@ void MapView::mouseReleaseEvent(QMouseEvent *event)
bool tab = false; bool tab = false;
if(sc->selectionObjectsView.selectionMode == SelectionObjectsLayer::MOVEMENT) if(sc->selectionObjectsView.selectionMode == SelectionObjectsLayer::MOVEMENT)
{ {
/*if(sc->selectionObjectsView.newObject)
{
QString errorMsg;
if(controller->canPlaceObject(sc->level, sc->selectionObjectsView.newObject, errorMsg))
controller->commitObjectCreate(sc->level);
else
{
QMessageBox::information(this, "Can't place object", errorMsg);
break;
}
}
else*/
controller->commitObjectShift(sc->level); controller->commitObjectShift(sc->level);
} }
else else
@ -448,7 +447,6 @@ bool MapView::viewportEvent(QEvent *event)
{ {
if(auto * sc = static_cast<MapScene*>(scene())) if(auto * sc = static_cast<MapScene*>(scene()))
{ {
//auto rect = sceneRect();
auto rect = mapToScene(viewport()->geometry()).boundingRect(); auto rect = mapToScene(viewport()->geometry()).boundingRect();
controller->miniScene(sc->level)->viewport.setViewport(rect.x() / 32, rect.y() / 32, rect.width() / 32, rect.height() / 32); controller->miniScene(sc->level)->viewport.setViewport(rect.x() / 32, rect.y() / 32, rect.width() / 32, rect.height() / 32);
} }

View File

@ -114,6 +114,7 @@ protected:
private: private:
MapController * controller = nullptr; MapController * controller = nullptr;
QRubberBand * rubberBand = nullptr;
QPointF mouseStart; QPointF mouseStart;
int3 tileStart; int3 tileStart;
int3 tilePrev; int3 tilePrev;

View File

@ -101,17 +101,17 @@ QMimeData * ObjectBrowserProxyModel::mimeData(const QModelIndexList & indexes) c
assert(standardModel); assert(standardModel);
QModelIndex index = indexes.front(); QModelIndex index = indexes.front();
QMimeData * mimeData = new QMimeData;
QByteArray encodedData; QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly); QDataStream stream(&encodedData, QIODevice::WriteOnly);
if(index.isValid()) if(!index.isValid())
{ return nullptr;
auto text = standardModel->itemFromIndex(mapToSource(index))->data().toJsonObject();
stream << text; auto text = standardModel->itemFromIndex(mapToSource(index))->data().toJsonObject();
} stream << text;
QMimeData * mimeData = new QMimeData;
mimeData->setData("application/vcmi.object", encodedData); mimeData->setData("application/vcmi.object", encodedData);
return mimeData; return mimeData;
} }
@ -119,5 +119,21 @@ QMimeData * ObjectBrowserProxyModel::mimeData(const QModelIndexList & indexes) c
ObjectBrowser::ObjectBrowser(QWidget * parent): ObjectBrowser::ObjectBrowser(QWidget * parent):
QTreeView(parent) QTreeView(parent)
{ {
setDropIndicatorShown(false);
}
void ObjectBrowser::startDrag(Qt::DropActions supportedActions)
{
QDrag *drag = new QDrag(this);
auto indexes = selectedIndexes();
if(indexes.isEmpty())
return;
QMimeData * mimeData = model()->mimeData(indexes);
if(!mimeData)
return;
drag->setMimeData(mimeData);
Qt::DropAction dropAction = drag->exec();
} }

View File

@ -36,4 +36,7 @@ class ObjectBrowser : public QTreeView
{ {
public: public:
ObjectBrowser(QWidget * parent); ObjectBrowser(QWidget * parent);
protected:
void startDrag(Qt::DropActions supportedActions) override;
}; };

View File

@ -144,7 +144,6 @@ public:
void deselectObject(CGObjectInstance *); void deselectObject(CGObjectInstance *);
bool isSelected(const CGObjectInstance *) const; bool isSelected(const CGObjectInstance *) const;
std::set<CGObjectInstance*> getSelection() const; std::set<CGObjectInstance*> getSelection() const;
void moveSelection(int x, int y);
void clear(); void clear();
QPoint shift; QPoint shift;