Editor: Do int to color conversion in ColorEditor

0.6.1
Rob Cutmore 8 years ago
parent 3fb7c42845
commit c7241c692f

@ -10,9 +10,20 @@
#include "colorpickerpopup.hpp" #include "colorpickerpopup.hpp"
CSVWidget::ColorEditor::ColorEditor(const QColor &color, QWidget *parent, bool popupOnStart) CSVWidget::ColorEditor::ColorEditor(const QColor &color, QWidget *parent, const bool popupOnStart)
: ColorEditor(parent, popupOnStart)
{
setColor(color);
}
CSVWidget::ColorEditor::ColorEditor(const int colorInt, QWidget *parent, const bool popupOnStart)
: ColorEditor(parent, popupOnStart)
{
setColor(colorInt);
}
CSVWidget::ColorEditor::ColorEditor(QWidget *parent, const bool popupOnStart)
: QPushButton(parent), : QPushButton(parent),
mColor(color),
mColorPicker(new ColorPickerPopup(this)), mColorPicker(new ColorPickerPopup(this)),
mPopupOnStart(popupOnStart) mPopupOnStart(popupOnStart)
{ {
@ -59,6 +70,14 @@ void CSVWidget::ColorEditor::setColor(const QColor &color)
update(); update();
} }
void CSVWidget::ColorEditor::setColor(const int colorInt)
{
// Color RGB values are stored in given integer.
// First byte is red, second byte is green, third byte is blue.
QColor color = QColor(colorInt & 0xff, (colorInt >> 8) & 0xff, (colorInt >> 16) & 0xff);
setColor(color);
}
void CSVWidget::ColorEditor::showPicker() void CSVWidget::ColorEditor::showPicker()
{ {
if (isChecked()) if (isChecked())

@ -22,15 +22,23 @@ namespace CSVWidget
QPoint calculatePopupPosition(); QPoint calculatePopupPosition();
public: public:
ColorEditor(const QColor &color, QWidget *parent = 0, bool popupOnStart = false); ColorEditor(const QColor &color, QWidget *parent = 0, const bool popupOnStart = false);
ColorEditor(const int colorInt, QWidget *parent = 0, const bool popupOnStart = false);
QColor color() const; QColor color() const;
void setColor(const QColor &color); void setColor(const QColor &color);
/// \brief Set color using given int value.
/// \param colorInt RGB color value encoded as an integer.
void setColor(const int colorInt);
protected: protected:
virtual void paintEvent(QPaintEvent *event); virtual void paintEvent(QPaintEvent *event);
virtual void showEvent(QShowEvent *event); virtual void showEvent(QShowEvent *event);
private:
ColorEditor(QWidget *parent = 0, const bool popupOnStart = false);
private slots: private slots:
void showPicker(); void showPicker();
void pickerHid(); void pickerHid();

@ -181,9 +181,7 @@ 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)
{ {
int colorInt = index.data().toInt(); return new CSVWidget::ColorEditor(index.data().toInt(), parent, true);
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);
} }
@ -207,9 +205,7 @@ QWidget *CSVWorld::CommandDelegate::createEditor (QWidget *parent, const QStyleO
{ {
case CSMWorld::ColumnBase::Display_Colour: case CSMWorld::ColumnBase::Display_Colour:
{ {
int colorInt = variant.toInt(); return new CSVWidget::ColorEditor(variant.toInt(), parent);
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:
{ {
@ -322,9 +318,7 @@ 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)
{ {
int colorInt = variant.toInt(); colorEditor->setColor(variant.toInt());
QColor color = QColor(colorInt & 0xff, (colorInt >> 8) & 0xff, (colorInt >> 16) & 0xff);
colorEditor->setColor(color);
return; return;
} }

Loading…
Cancel
Save