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;
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<int>())
{
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<QComboBox *>(sender());
emit commitData(editor);
emit closeEditor(editor);
//QComboBox *editor = qobject_cast<QComboBox *>(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<int>())
if(QComboBox *ed = qobject_cast<QComboBox *>(editor))
{
PlayerColor player(qvariant_cast<int>(index.data()));
QComboBox *ed = qobject_cast<QComboBox *>(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<int>())
if(QComboBox *ed = qobject_cast<QComboBox *>(editor))
{
QComboBox *ed = qobject_cast<QComboBox *>(editor);
model->setData(index, QVariant::fromValue(ed->currentText()));
QMap<int, QVariant> data;
data[0] = options[ed->currentIndex()];
model->setItemData(index, data);
}
else
{

View File

@ -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<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);
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<class T>
void addProperty(const QString & key, const T & value, bool restricted = true)
{
addProperty<T>(key, value, nullptr, restricted);
}
protected:
int row = 0;
QTableWidget * table;
CGObjectInstance * obj;
QMap<QString, QTableWidgetItem*> 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

View File

@ -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);
}

View File

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