#include <QApplication>
#include <QStylePainter>
#include <QStyleOption>
#include <QMouseEvent>
#include <QDebug>
#include <cmath>
#include <QWidget>
#include <QList>
#include <QRect>
class RgbBitMaskWidget
: public QWidget {
Q_OBJECT
public:
RgbBitMaskWidget
(QWidget* parent
= 0);
~RgbBitMaskWidget();
uchar red() const;
uchar green() const;
uchar blue() const;
virtual int heightForWidth(int) const;
virtual QSize sizeHint
() const;
protected:
signals:
void rgbBitMaskChanged(int, int, int) const;
private:
uchar mBits[3];
QList<QList<QRect> > mGeometryRects;
void recalculateRectGeometries();
};
RgbBitMaskWidget
::RgbBitMaskWidget(QWidget* parent
) {
// initialize array
for (int i = 0; i < 3; ++i)
mBits[i] = 255;
// initialize list of arrays
for (int i = 0; i < 3; ++i) {
QList<QRect> l;
for (int j = 0; j < 8; ++j)
mGeometryRects << l;
}
// set sizePolicy (important for heightForWidth)
sizePolicy.setHeightForWidth(true);
setSizePolicy(sizePolicy);
const int minWidth = 150;
setMinimumSize(minWidth, minWidth / (8 / 3.0));
}
RgbBitMaskWidget::~RgbBitMaskWidget()
{
}
void RgbBitMaskWidget::recalculateRectGeometries()
{
const int MARGIN = 2;
QRect r
= contentsRect
().
adjusted(MARGIN, MARGIN,
-MARGIN,
-MARGIN
);
int x = 0;
int y = 0;
int w = r.width() / 8;
int h = r.height() / 3;
for (int i = 0; i < mGeometryRects.size(); ++i) {
QList<QRect> rectList = mGeometryRects[i];
x = 0;
for (int j = 0; j < rectList.size(); ++j) {
mGeometryRects
[i
][j
] = QRect(x, y, w, h
);
x += w;
}
y += h;
}
}
{
Q_UNUSED(event);
recalculateRectGeometries();
}
void RgbBitMaskWidget
::mousePressEvent(QMouseEvent* event
) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 8; ++j) {
if (mGeometryRects[i][j].contains(pos)) {
uchar flag = std::pow(2, 7 - j);
if (flag == (mBits[i] & flag))
mBits[i] = mBits[i] & (~flag);
else
mBits[i] = mBits[i] | flag ;
}
}
}
update();
}
{
Q_UNUSED(event);
opt.init(this);
style
()->drawPrimitive
(QStyle::PE_Widget,
&opt,
&painter,
this);
QList<QColor> colors;
// paint rects
pen.setWidth(3);
painter.setPen(pen);
for(int i = 0; i < 3; ++i) {
QList<QRect> l = mGeometryRects[i];
for (int j = 0; j < 8; ++j) {
uchar flags = std::pow(2, 7 - j);
if ((mBits[i] & flags) == flags)
painter.setBrush(c);
else
painter.setBrush(c.lighter(185));
painter.drawRect(mGeometryRects[i][j]);
}
}
}
int RgbBitMaskWidget::heightForWidth(int) const
{
double ratio = 8 / 3;
return static_cast<int>(height() / ratio);
}
QSize RgbBitMaskWidget
::sizeHint() const {
int w = width();
return QSize(w, heightForWidth
(w
));
}
int main(int argc, char *argv[])
{
RgbBitMaskWidget rgb;
rgb.show();
return a.exec();
}
#include "main.moc"
#include <QApplication>
#include <QStylePainter>
#include <QStyleOption>
#include <QMouseEvent>
#include <QDebug>
#include <cmath>
#include <QWidget>
#include <QList>
#include <QRect>
class RgbBitMaskWidget : public QWidget
{
Q_OBJECT
public:
RgbBitMaskWidget(QWidget* parent = 0);
~RgbBitMaskWidget();
uchar red() const;
uchar green() const;
uchar blue() const;
virtual int heightForWidth(int) const;
virtual QSize sizeHint() const;
protected:
virtual void resizeEvent(QResizeEvent*);
virtual void paintEvent(QPaintEvent*);
virtual void mousePressEvent(QMouseEvent* );
signals:
void rgbBitMaskChanged(int, int, int) const;
private:
uchar mBits[3];
QList<QList<QRect> > mGeometryRects;
void recalculateRectGeometries();
};
RgbBitMaskWidget::RgbBitMaskWidget(QWidget* parent)
: QWidget(parent)
{
// initialize array
for (int i = 0; i < 3; ++i)
mBits[i] = 255;
// initialize list of arrays
for (int i = 0; i < 3; ++i) {
QList<QRect> l;
for (int j = 0; j < 8; ++j)
l << QRect();
mGeometryRects << l;
}
// set sizePolicy (important for heightForWidth)
QSizePolicy sizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
sizePolicy.setHeightForWidth(true);
setSizePolicy(sizePolicy);
const int minWidth = 150;
setMinimumSize(minWidth, minWidth / (8 / 3.0));
}
RgbBitMaskWidget::~RgbBitMaskWidget()
{
}
void RgbBitMaskWidget::recalculateRectGeometries()
{
const int MARGIN = 2;
QRect r = contentsRect().adjusted(MARGIN, MARGIN, -MARGIN, -MARGIN);
int x = 0;
int y = 0;
int w = r.width() / 8;
int h = r.height() / 3;
for (int i = 0; i < mGeometryRects.size(); ++i) {
QList<QRect> rectList = mGeometryRects[i];
x = 0;
for (int j = 0; j < rectList.size(); ++j) {
mGeometryRects[i][j] = QRect(x, y, w, h);
x += w;
}
y += h;
}
}
void RgbBitMaskWidget::resizeEvent(QResizeEvent* event)
{
Q_UNUSED(event);
recalculateRectGeometries();
}
void RgbBitMaskWidget::mousePressEvent(QMouseEvent* event)
{
QPoint pos = event->pos();
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 8; ++j) {
if (mGeometryRects[i][j].contains(pos)) {
uchar flag = std::pow(2, 7 - j);
if (flag == (mBits[i] & flag))
mBits[i] = mBits[i] & (~flag);
else
mBits[i] = mBits[i] | flag ;
}
}
}
QWidget::mousePressEvent(event);
update();
}
void RgbBitMaskWidget::paintEvent(QPaintEvent* event)
{
Q_UNUSED(event);
QStyleOption opt;
opt.init(this);
QStylePainter painter(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
QList<QColor> colors;
colors << QColor(Qt::red) << QColor(Qt::green) << QColor(Qt::blue);
// paint rects
QPen pen(Qt::white);
pen.setWidth(3);
painter.setPen(pen);
for(int i = 0; i < 3; ++i) {
QList<QRect> l = mGeometryRects[i];
QColor c = colors[i];
for (int j = 0; j < 8; ++j) {
uchar flags = std::pow(2, 7 - j);
if ((mBits[i] & flags) == flags)
painter.setBrush(c);
else
painter.setBrush(c.lighter(185));
painter.drawRect(mGeometryRects[i][j]);
}
}
}
int RgbBitMaskWidget::heightForWidth(int) const
{
double ratio = 8 / 3;
return static_cast<int>(height() / ratio);
}
QSize RgbBitMaskWidget::sizeHint() const
{
int w = width();
return QSize(w, heightForWidth(w));
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
RgbBitMaskWidget rgb;
rgb.show();
return a.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks