Editor: fix handling of colour columns

Colours are stored as ints in the backend but the conversion from
QColor to int was broken.
0.6.1
Rob Cutmore 8 years ago
parent 9920418947
commit 0082f5a2dc

@ -127,22 +127,24 @@ void CSVWorld::CommandDelegate::setModelDataImp (QWidget *editor, QAbstractItemM
if (!mCommandDispatcher)
return;
QVariant new_;
// Color columns use a custom editor, so we need explicitly extract a data from it
QVariant variant;
// Color columns use a custom editor, so we need to fetch selected color from it.
CSVWidget::ColorEditor *colorEditor = qobject_cast<CSVWidget::ColorEditor *>(editor);
if (colorEditor != NULL)
{
new_ = colorEditor->color();
QColor color = colorEditor->color();
variant = (color.blue() << 16) | (color.green() << 8) | (color.red());
}
else
{
NastyTableModelHack hack (*model);
QStyledItemDelegate::setModelData (editor, &hack, index);
new_ = hack.getData();
variant = hack.getData();
}
if ((model->data (index)!=new_) && (model->flags(index) & Qt::ItemIsEditable))
mCommandDispatcher->executeModify (model, index, new_);
if ((model->data (index)!=variant) && (model->flags(index) & Qt::ItemIsEditable))
mCommandDispatcher->executeModify (model, index, variant);
}
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)
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);
}
@ -202,9 +206,11 @@ QWidget *CSVWorld::CommandDelegate::createEditor (QWidget *parent, const QStyleO
switch (display)
{
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:
{
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
{
QVariant v = index.data(Qt::EditRole);
QVariant variant = index.data(Qt::EditRole);
if (tryDisplay)
{
if (!v.isValid())
if (!variant.isValid())
{
v = index.data(Qt::DisplayRole);
if (!v.isValid())
variant = index.data(Qt::DisplayRole);
if (!variant.isValid())
{
return;
}
@ -305,7 +311,7 @@ void CSVWorld::CommandDelegate::setEditorData (QWidget *editor, const QModelInde
QPlainTextEdit* plainTextEdit = qobject_cast<QPlainTextEdit*>(editor);
if(plainTextEdit) //for some reason it is easier to brake the loop here
{
if(plainTextEdit->toPlainText() == v.toString())
if (plainTextEdit->toPlainText() == variant.toString())
{
return;
}
@ -316,23 +322,27 @@ void CSVWorld::CommandDelegate::setEditorData (QWidget *editor, const QModelInde
CSVWidget::ColorEditor *colorEditor = qobject_cast<CSVWidget::ColorEditor *>(editor);
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;
}
QByteArray n = editor->metaObject()->userProperty().name();
if (n == "dateTime") {
if (n == "dateTime")
{
if (editor->inherits("QTimeEdit"))
n = "time";
else if (editor->inherits("QDateEdit"))
n = "date";
}
if (!n.isEmpty()) {
if (!v.isValid())
v = QVariant(editor->property(n).userType(), (const void *)0);
editor->setProperty(n, v);
if (!n.isEmpty())
{
if (!variant.isValid())
variant = QVariant(editor->property(n).userType(), (const void *)0);
editor->setProperty(n, variant);
}
}

Loading…
Cancel
Save