Editor: fix handling of colour columns

Colours are stored as ints in the backend but the conversion from
QColor to int was broken.
pull/251/head^2
Rob Cutmore 8 years ago
parent 9920418947
commit 0082f5a2dc

@ -127,22 +127,24 @@ void CSVWorld::CommandDelegate::setModelDataImp (QWidget *editor, QAbstractItemM
if (!mCommandDispatcher) if (!mCommandDispatcher)
return; return;
QVariant new_; QVariant variant;
// Color columns use a custom editor, so we need explicitly extract a data from it
// Color columns use a custom editor, so we need to fetch selected color from it.
CSVWidget::ColorEditor *colorEditor = qobject_cast<CSVWidget::ColorEditor *>(editor); CSVWidget::ColorEditor *colorEditor = qobject_cast<CSVWidget::ColorEditor *>(editor);
if (colorEditor != NULL) if (colorEditor != NULL)
{ {
new_ = colorEditor->color(); QColor color = colorEditor->color();
variant = (color.blue() << 16) | (color.green() << 8) | (color.red());
} }
else else
{ {
NastyTableModelHack hack (*model); NastyTableModelHack hack (*model);
QStyledItemDelegate::setModelData (editor, &hack, index); QStyledItemDelegate::setModelData (editor, &hack, index);
new_ = hack.getData(); variant = hack.getData();
} }
if ((model->data (index)!=new_) && (model->flags(index) & Qt::ItemIsEditable)) if ((model->data (index)!=variant) && (model->flags(index) & Qt::ItemIsEditable))
mCommandDispatcher->executeModify (model, index, new_); mCommandDispatcher->executeModify (model, index, variant);
} }
CSVWorld::CommandDelegate::CommandDelegate (CSMWorld::CommandDispatcher *commandDispatcher, CSVWorld::CommandDelegate::CommandDelegate (CSMWorld::CommandDispatcher *commandDispatcher,
@ -179,7 +181,9 @@ QWidget *CSVWorld::CommandDelegate::createEditor (QWidget *parent, const QStyleO
// (the third parameter of ColorEditor's constructor) // (the third parameter of ColorEditor's constructor)
else if (display == CSMWorld::ColumnBase::Display_Colour) else if (display == CSMWorld::ColumnBase::Display_Colour)
{ {
return new CSVWidget::ColorEditor(index.data().value<QColor>(), parent, true); int colorInt = index.data().toInt();
QColor color = QColor(colorInt & 0xff, (colorInt >> 8) & 0xff, (colorInt >> 16) & 0xff);
return new CSVWidget::ColorEditor(color, parent, true);
} }
return createEditor (parent, option, index, display); return createEditor (parent, option, index, display);
} }
@ -202,9 +206,11 @@ QWidget *CSVWorld::CommandDelegate::createEditor (QWidget *parent, const QStyleO
switch (display) switch (display)
{ {
case CSMWorld::ColumnBase::Display_Colour: case CSMWorld::ColumnBase::Display_Colour:
{
return new CSVWidget::ColorEditor(index.data().value<QColor>(), parent); int colorInt = variant.toInt();
QColor color = QColor(colorInt & 0xff, (colorInt >> 8) & 0xff, (colorInt >> 16) & 0xff);
return new CSVWidget::ColorEditor(color, parent);
}
case CSMWorld::ColumnBase::Display_Integer: case CSMWorld::ColumnBase::Display_Integer:
{ {
DialogueSpinBox *sb = new DialogueSpinBox(parent); DialogueSpinBox *sb = new DialogueSpinBox(parent);
@ -291,13 +297,13 @@ void CSVWorld::CommandDelegate::setEditorData (QWidget *editor, const QModelInde
void CSVWorld::CommandDelegate::setEditorData (QWidget *editor, const QModelIndex& index, bool tryDisplay) const void CSVWorld::CommandDelegate::setEditorData (QWidget *editor, const QModelIndex& index, bool tryDisplay) const
{ {
QVariant v = index.data(Qt::EditRole); QVariant variant = index.data(Qt::EditRole);
if (tryDisplay) if (tryDisplay)
{ {
if (!v.isValid()) if (!variant.isValid())
{ {
v = index.data(Qt::DisplayRole); variant = index.data(Qt::DisplayRole);
if (!v.isValid()) if (!variant.isValid())
{ {
return; return;
} }
@ -305,7 +311,7 @@ void CSVWorld::CommandDelegate::setEditorData (QWidget *editor, const QModelInde
QPlainTextEdit* plainTextEdit = qobject_cast<QPlainTextEdit*>(editor); QPlainTextEdit* plainTextEdit = qobject_cast<QPlainTextEdit*>(editor);
if(plainTextEdit) //for some reason it is easier to brake the loop here if(plainTextEdit) //for some reason it is easier to brake the loop here
{ {
if(plainTextEdit->toPlainText() == v.toString()) if (plainTextEdit->toPlainText() == variant.toString())
{ {
return; return;
} }
@ -316,23 +322,27 @@ void CSVWorld::CommandDelegate::setEditorData (QWidget *editor, const QModelInde
CSVWidget::ColorEditor *colorEditor = qobject_cast<CSVWidget::ColorEditor *>(editor); CSVWidget::ColorEditor *colorEditor = qobject_cast<CSVWidget::ColorEditor *>(editor);
if (colorEditor != NULL) if (colorEditor != NULL)
{ {
colorEditor->setColor(index.data().value<QColor>()); int colorInt = variant.toInt();
QColor color = QColor(colorInt & 0xff, (colorInt >> 8) & 0xff, (colorInt >> 16) & 0xff);
colorEditor->setColor(color);
return; return;
} }
QByteArray n = editor->metaObject()->userProperty().name(); QByteArray n = editor->metaObject()->userProperty().name();
if (n == "dateTime") { if (n == "dateTime")
{
if (editor->inherits("QTimeEdit")) if (editor->inherits("QTimeEdit"))
n = "time"; n = "time";
else if (editor->inherits("QDateEdit")) else if (editor->inherits("QDateEdit"))
n = "date"; n = "date";
} }
if (!n.isEmpty()) { if (!n.isEmpty())
if (!v.isValid()) {
v = QVariant(editor->property(n).userType(), (const void *)0); if (!variant.isValid())
editor->setProperty(n, v); variant = QVariant(editor->property(n).userType(), (const void *)0);
editor->setProperty(n, variant);
} }
} }

Loading…
Cancel
Save