From 07ba0242018c9eebaca6c44654593f540b50380b Mon Sep 17 00:00:00 2001 From: nordsoft Date: Thu, 8 Sep 2022 02:59:02 +0400 Subject: [PATCH] Delegate for player color is implemented --- mapeditor/inspector.cpp | 53 ++++++++++++++++++++-------------------- mapeditor/inspector.h | 24 ++++++++++++++---- mapeditor/mainwindow.cpp | 19 +++----------- mapeditor/mainwindow.h | 2 +- 4 files changed, 51 insertions(+), 47 deletions(-) diff --git a/mapeditor/inspector.cpp b/mapeditor/inspector.cpp index d78ef1e13..5632844e0 100644 --- a/mapeditor/inspector.cpp +++ b/mapeditor/inspector.cpp @@ -123,7 +123,13 @@ void Inspector::updateProperties(CArmedInstance * o) { if(!o) return; - addProperty("Owner", o->tempOwner); + auto * delegate = new InspectorDelegate; + delegate->options << "NEUTRAL"; + for(int p = 0; p < map->players.size(); ++p) + if(map->players[p].canAnyonePlay()) + delegate->options << QString("PLAYER %1").arg(p); + + addProperty("Owner", o->tempOwner, delegate, true); } void Inspector::updateProperties(CGDwelling * o) @@ -232,7 +238,7 @@ void Inspector::setProperty(const QString & key, const QVariant & value) if(key == "Owner") { - PlayerColor owner(value.toString().toInt()); + PlayerColor owner(value.toString().mid(6).toInt()); //receiving PLAYER N, N has index 6 if(value == "NEUTRAL") owner = PlayerColor::NEUTRAL; if(value == "UNFLAGGABLE") @@ -374,8 +380,7 @@ QTableWidgetItem * Inspector::addProperty(const int3 & value) QTableWidgetItem * Inspector::addProperty(const PlayerColor & value) { - //auto str = QString("PLAYER %1").arg(value.getNum()); - auto str = QString::number(value.getNum()); + auto str = QString("PLAYER %1").arg(value.getNum()); if(value == PlayerColor::NEUTRAL) str = "NEUTRAL"; if(value == PlayerColor::UNFLAGGABLE) @@ -441,7 +446,7 @@ QTableWidgetItem * Inspector::addProperty(CGCreature::Character value) //======================================================================== -Inspector::Inspector(CGObjectInstance * o, QTableWidget * t): obj(o), table(t) +Inspector::Inspector(CMap * m, CGObjectInstance * o, QTableWidget * t): obj(o), table(t), map(m) { } @@ -449,31 +454,26 @@ Inspector::Inspector(CGObjectInstance * o, QTableWidget * t): obj(o), table(t) * Delegates */ -QWidget * PlayerColorDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const +QWidget * InspectorDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { - if (index.data().canConvert()) - { - auto *editor = new QComboBox(parent); - connect(editor, SIGNAL(activated(int)), this, SLOT(commitAndCloseEditor(int))); - return editor; - } - return QStyledItemDelegate::createEditor(parent, option, index); + return new QComboBox(parent); + + //return QStyledItemDelegate::createEditor(parent, option, index); + //connect(editor, SIGNAL(activated(int)), this, SLOT(commitAndCloseEditor(int))); } -void PlayerColorDelegate::commitAndCloseEditor(int id) +void InspectorDelegate::commitAndCloseEditor(int id) { - QComboBox *editor = qobject_cast(sender()); - emit commitData(editor); - emit closeEditor(editor); + //QComboBox *editor = qobject_cast(sender()); + //emit commitData(editor); + //emit closeEditor(editor); } -void PlayerColorDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const +void InspectorDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { - if (index.data().canConvert()) + if(QComboBox *ed = qobject_cast(editor)) { - PlayerColor player(qvariant_cast(index.data())); - QComboBox *ed = qobject_cast(editor); - ed->addItem(QString::number(player.getNum())); + ed->addItems(options); } else { @@ -481,12 +481,13 @@ void PlayerColorDelegate::setEditorData(QWidget *editor, const QModelIndex &inde } } -void PlayerColorDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const +void InspectorDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { - if (index.data().canConvert()) + if(QComboBox *ed = qobject_cast(editor)) { - QComboBox *ed = qobject_cast(editor); - model->setData(index, QVariant::fromValue(ed->currentText())); + QMap data; + data[0] = options[ed->currentIndex()]; + model->setItemData(index, data); } else { diff --git a/mapeditor/inspector.h b/mapeditor/inspector.h index 31a59dafe..19de274ab 100644 --- a/mapeditor/inspector.h +++ b/mapeditor/inspector.h @@ -70,7 +70,7 @@ protected: //===============END OF DECLARATION======================================= public: - Inspector(CGObjectInstance *, QTableWidget *); + Inspector(CMap *, CGObjectInstance *, QTableWidget *); void setProperty(const QString & key, const QVariant & value); @@ -79,7 +79,7 @@ public: protected: template - void addProperty(const QString & key, const T & value, bool restricted = true) + void addProperty(const QString & key, const T & value, QAbstractItemDelegate * delegate, bool restricted) { auto * itemValue = addProperty(value); if(restricted) @@ -90,6 +90,8 @@ protected: { itemKey = keyItems[key]; table->setItem(table->row(itemKey), 1, itemValue); + if(delegate) + table->setItemDelegateForRow(table->row(itemKey), delegate); } else { @@ -100,34 +102,46 @@ protected: table->setRowCount(row + 1); table->setItem(row, 0, itemKey); table->setItem(row, 1, itemValue); + if(delegate) + table->setItemDelegateForRow(row, delegate); ++row; } } + + template + void addProperty(const QString & key, const T & value, bool restricted = true) + { + addProperty(key, value, nullptr, restricted); + } protected: int row = 0; QTableWidget * table; CGObjectInstance * obj; QMap keyItems; + CMap * map; }; -class PlayerColorDelegate : public QStyledItemDelegate +class InspectorDelegate : public QStyledItemDelegate { Q_OBJECT public: + InspectorDelegate(const QStringList &); + using QStyledItemDelegate::QStyledItemDelegate; - //void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; - //QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override; QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; void setEditorData(QWidget *editor, const QModelIndex &index) const override; void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; + + QStringList options; private slots: void commitAndCloseEditor(int); + }; #endif // INSPECTOR_H diff --git a/mapeditor/mainwindow.cpp b/mapeditor/mainwindow.cpp index de798a655..5392dccac 100644 --- a/mapeditor/mainwindow.cpp +++ b/mapeditor/mainwindow.cpp @@ -119,6 +119,7 @@ MainWindow::MainWindow(QWidget *parent) : ui->mapView->setScene(controller.scene(0)); ui->mapView->setController(&controller); + ui->mapView->setOptimizationFlags(QGraphicsView::DontSavePainterState | QGraphicsView::DontAdjustForAntialiasing); connect(ui->mapView, &MapView::openObjectProperties, this, &MainWindow::loadInspector); ui->minimapView->setScene(controller.miniScene(0)); @@ -146,19 +147,6 @@ void MainWindow::setStatusMessage(const QString & status) statusBar()->showMessage(status); } -void MainWindow::reloadMap(int level) -{ - //auto mapSizePx = mapHandler->surface.rect(); - //float ratio = std::fmin(mapSizePx.width() / 192., mapSizePx.height() / 192.);*/ - //minimap = mapHandler->surface; - //minimap.setDevicePixelRatio(ratio); - - controller.sceneForceUpdate(level); - - //sceneMini->clear(); - //sceneMini->addPixmap(minimap); -} - void MainWindow::setTitle() { QString title = QString("%1%2 - %3 (v%4)").arg(filename, unsaved ? "*" : "", VCMI_EDITOR_NAME, VCMI_EDITOR_VERSION); @@ -217,6 +205,7 @@ void MainWindow::on_actionOpen_triggered() catch(const std::exception & e) { QMessageBox::critical(this, "Failed to open map", e.what()); + return; } @@ -773,7 +762,7 @@ void MainWindow::on_actionFill_triggered() void MainWindow::loadInspector(CGObjectInstance * obj) { - Inspector inspector(obj, ui->inspectorWidget); + Inspector inspector(controller.map(), obj, ui->inspectorWidget); inspector.updateProperties(); } @@ -800,7 +789,7 @@ void MainWindow::on_inspectorWidget_itemChanged(QTableWidgetItem *item) auto param = tableWidget->item(r, c - 1)->text(); //set parameter - Inspector inspector(obj, tableWidget); + Inspector inspector(controller.map(), obj, tableWidget); inspector.setProperty(param, item->text()); controller.commitObjectChange(mapLevel); } diff --git a/mapeditor/mainwindow.h b/mapeditor/mainwindow.h index fa41ff4a9..2c137909a 100644 --- a/mapeditor/mainwindow.h +++ b/mapeditor/mainwindow.h @@ -25,7 +25,7 @@ public: ~MainWindow(); void initializeMap(bool isNew); - void reloadMap(int level = 0); + void saveMap(); MapView * mapView();