1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-18 17:40:48 +02:00

Delegate for player color is implemented

This commit is contained in:
nordsoft 2022-09-08 02:59:02 +04:00
parent c3355aa976
commit 07ba024201
4 changed files with 51 additions and 47 deletions

View File

@ -123,7 +123,13 @@ void Inspector::updateProperties(CArmedInstance * o)
{ {
if(!o) return; 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) void Inspector::updateProperties(CGDwelling * o)
@ -232,7 +238,7 @@ void Inspector::setProperty(const QString & key, const QVariant & value)
if(key == "Owner") 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") if(value == "NEUTRAL")
owner = PlayerColor::NEUTRAL; owner = PlayerColor::NEUTRAL;
if(value == "UNFLAGGABLE") if(value == "UNFLAGGABLE")
@ -374,8 +380,7 @@ QTableWidgetItem * Inspector::addProperty(const int3 & value)
QTableWidgetItem * Inspector::addProperty(const PlayerColor & value) QTableWidgetItem * Inspector::addProperty(const PlayerColor & value)
{ {
//auto str = QString("PLAYER %1").arg(value.getNum()); auto str = QString("PLAYER %1").arg(value.getNum());
auto str = QString::number(value.getNum());
if(value == PlayerColor::NEUTRAL) if(value == PlayerColor::NEUTRAL)
str = "NEUTRAL"; str = "NEUTRAL";
if(value == PlayerColor::UNFLAGGABLE) 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 * 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<int>()) return new QComboBox(parent);
{
auto *editor = new QComboBox(parent); //return QStyledItemDelegate::createEditor(parent, option, index);
connect(editor, SIGNAL(activated(int)), this, SLOT(commitAndCloseEditor(int))); //connect(editor, SIGNAL(activated(int)), this, SLOT(commitAndCloseEditor(int)));
return editor;
}
return QStyledItemDelegate::createEditor(parent, option, index);
} }
void PlayerColorDelegate::commitAndCloseEditor(int id) void InspectorDelegate::commitAndCloseEditor(int id)
{ {
QComboBox *editor = qobject_cast<QComboBox *>(sender()); //QComboBox *editor = qobject_cast<QComboBox *>(sender());
emit commitData(editor); //emit commitData(editor);
emit closeEditor(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<int>()) if(QComboBox *ed = qobject_cast<QComboBox *>(editor))
{ {
PlayerColor player(qvariant_cast<int>(index.data())); ed->addItems(options);
QComboBox *ed = qobject_cast<QComboBox *>(editor);
ed->addItem(QString::number(player.getNum()));
} }
else 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<int>()) if(QComboBox *ed = qobject_cast<QComboBox *>(editor))
{ {
QComboBox *ed = qobject_cast<QComboBox *>(editor); QMap<int, QVariant> data;
model->setData(index, QVariant::fromValue(ed->currentText())); data[0] = options[ed->currentIndex()];
model->setItemData(index, data);
} }
else else
{ {

View File

@ -70,7 +70,7 @@ protected:
//===============END OF DECLARATION======================================= //===============END OF DECLARATION=======================================
public: public:
Inspector(CGObjectInstance *, QTableWidget *); Inspector(CMap *, CGObjectInstance *, QTableWidget *);
void setProperty(const QString & key, const QVariant & value); void setProperty(const QString & key, const QVariant & value);
@ -79,7 +79,7 @@ public:
protected: protected:
template<class T> template<class T>
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); auto * itemValue = addProperty(value);
if(restricted) if(restricted)
@ -90,6 +90,8 @@ protected:
{ {
itemKey = keyItems[key]; itemKey = keyItems[key];
table->setItem(table->row(itemKey), 1, itemValue); table->setItem(table->row(itemKey), 1, itemValue);
if(delegate)
table->setItemDelegateForRow(table->row(itemKey), delegate);
} }
else else
{ {
@ -100,34 +102,46 @@ protected:
table->setRowCount(row + 1); table->setRowCount(row + 1);
table->setItem(row, 0, itemKey); table->setItem(row, 0, itemKey);
table->setItem(row, 1, itemValue); table->setItem(row, 1, itemValue);
if(delegate)
table->setItemDelegateForRow(row, delegate);
++row; ++row;
} }
} }
template<class T>
void addProperty(const QString & key, const T & value, bool restricted = true)
{
addProperty<T>(key, value, nullptr, restricted);
}
protected: protected:
int row = 0; int row = 0;
QTableWidget * table; QTableWidget * table;
CGObjectInstance * obj; CGObjectInstance * obj;
QMap<QString, QTableWidgetItem*> keyItems; QMap<QString, QTableWidgetItem*> keyItems;
CMap * map;
}; };
class PlayerColorDelegate : public QStyledItemDelegate class InspectorDelegate : public QStyledItemDelegate
{ {
Q_OBJECT Q_OBJECT
public: public:
InspectorDelegate(const QStringList &);
using QStyledItemDelegate::QStyledItemDelegate; 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; QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void setEditorData(QWidget *editor, 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; void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
QStringList options;
private slots: private slots:
void commitAndCloseEditor(int); void commitAndCloseEditor(int);
}; };
#endif // INSPECTOR_H #endif // INSPECTOR_H

View File

@ -119,6 +119,7 @@ MainWindow::MainWindow(QWidget *parent) :
ui->mapView->setScene(controller.scene(0)); ui->mapView->setScene(controller.scene(0));
ui->mapView->setController(&controller); ui->mapView->setController(&controller);
ui->mapView->setOptimizationFlags(QGraphicsView::DontSavePainterState | QGraphicsView::DontAdjustForAntialiasing);
connect(ui->mapView, &MapView::openObjectProperties, this, &MainWindow::loadInspector); connect(ui->mapView, &MapView::openObjectProperties, this, &MainWindow::loadInspector);
ui->minimapView->setScene(controller.miniScene(0)); ui->minimapView->setScene(controller.miniScene(0));
@ -146,19 +147,6 @@ void MainWindow::setStatusMessage(const QString & status)
statusBar()->showMessage(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() void MainWindow::setTitle()
{ {
QString title = QString("%1%2 - %3 (v%4)").arg(filename, unsaved ? "*" : "", VCMI_EDITOR_NAME, VCMI_EDITOR_VERSION); 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) catch(const std::exception & e)
{ {
QMessageBox::critical(this, "Failed to open map", e.what()); QMessageBox::critical(this, "Failed to open map", e.what());
return;
} }
@ -773,7 +762,7 @@ void MainWindow::on_actionFill_triggered()
void MainWindow::loadInspector(CGObjectInstance * obj) void MainWindow::loadInspector(CGObjectInstance * obj)
{ {
Inspector inspector(obj, ui->inspectorWidget); Inspector inspector(controller.map(), obj, ui->inspectorWidget);
inspector.updateProperties(); inspector.updateProperties();
} }
@ -800,7 +789,7 @@ void MainWindow::on_inspectorWidget_itemChanged(QTableWidgetItem *item)
auto param = tableWidget->item(r, c - 1)->text(); auto param = tableWidget->item(r, c - 1)->text();
//set parameter //set parameter
Inspector inspector(obj, tableWidget); Inspector inspector(controller.map(), obj, tableWidget);
inspector.setProperty(param, item->text()); inspector.setProperty(param, item->text());
controller.commitObjectChange(mapLevel); controller.commitObjectChange(mapLevel);
} }

View File

@ -25,7 +25,7 @@ public:
~MainWindow(); ~MainWindow();
void initializeMap(bool isNew); void initializeMap(bool isNew);
void reloadMap(int level = 0);
void saveMap(); void saveMap();
MapView * mapView(); MapView * mapView();