paysages3d/src/editing/inputcolor.cpp
2013-04-17 12:29:51 +00:00

66 lines
1.4 KiB
C++

#include "inputcolor.h"
#include <QLabel>
#include <QPushButton>
#include <QPainter>
#include <QColorDialog>
#include "tools.h"
class ColorPreview:public QWidget
{
public:
ColorPreview(QWidget* parent):
QWidget(parent)
{
}
void paintEvent(QPaintEvent*)
{
QPainter painter(this);
painter.fillRect(this->rect(), col);
}
QColor col;
};
InputColor::InputColor(QWidget* form, QString label, Color* value):
BaseInput(form, label),
_value(value)
{
_preview = new ColorPreview(form);
_preview->setMinimumSize(50, 20);
_control = new QPushButton(tr("Edit"), form);
_control->setMaximumWidth(150);
connect((QPushButton*)_control, SIGNAL(clicked()), this, SLOT(chooseColor()));
}
void InputColor::updatePreview()
{
_preview->update();
BaseInput::updatePreview();
}
void InputColor::applyValue()
{
_value->r = ((ColorPreview*)_preview)->col.redF();
_value->g = ((ColorPreview*)_preview)->col.greenF();
_value->b = ((ColorPreview*)_preview)->col.blueF();
_value->a = 1.0;
BaseInput::applyValue();
}
void InputColor::revert()
{
((ColorPreview*)_preview)->col = colorToQColor(*_value);
BaseInput::revert();
}
void InputColor::chooseColor()
{
QColor col = QColorDialog::getColor(((ColorPreview*)_preview)->col, _control);
if (col.isValid())
{
((ColorPreview*)_preview)->col = col;
applyValue();
}
}