QTfrontend/ui/widget/colorwidget.cpp
author unc0rr
Sun, 27 May 2012 23:42:43 +0400
changeset 7130 fcab1fd02bc6
child 7749 edad8a7bcaea
permissions -rw-r--r--
- Allow switching colors with mouse wheel - Use indices instead of color values - Introduce a model for colors - Some small fixes, .pro can be used to build again - Yay, tested it to work via network (and fixed a bug!) (TODO: pass colors to engine by index)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7130
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
     1
#include <QStandardItemModel>
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
     2
#include <QMouseEvent>
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
     3
#include <QWheelEvent>
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
     4
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
     5
#include "colorwidget.h"
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
     6
#include "hwconsts.h"
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
     7
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
     8
ColorWidget::ColorWidget(QStandardItemModel *colorsModel, QWidget *parent) :
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
     9
    QWidget(parent)
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    10
{
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    11
    m_colorsModel = colorsModel;
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    12
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    13
    setColor(0);
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    14
    setStyleSheet("");
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    15
    setAutoFillBackground(true);
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    16
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    17
    connect(m_colorsModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(dataChanged(QModelIndex,QModelIndex)));
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    18
}
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    19
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    20
ColorWidget::~ColorWidget()
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    21
{
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    22
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    23
}
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    24
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    25
void ColorWidget::setColor(int color)
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    26
{
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    27
    Q_ASSERT_X(color >= 0 && color < m_colorsModel->rowCount(), "ColorWidget::setColor", "Color index out of range");
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    28
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    29
    m_color = color;
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    30
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    31
    QStandardItem * item = m_colorsModel->item(m_color);
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    32
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    33
    QPalette p = palette();
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    34
    p.setColor(QPalette::Window, item->data().value<QColor>());
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    35
    setPalette(p);
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    36
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    37
    emit colorChanged(m_color);
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    38
}
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    39
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    40
int ColorWidget::getColor()
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    41
{
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    42
    return m_color;
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    43
}
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    44
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    45
void ColorWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    46
{
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    47
    if(m_color >= topLeft.row() && m_color <= bottomRight.row())
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    48
        setColor(m_color);
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    49
}
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    50
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    51
void ColorWidget::mousePressEvent(QMouseEvent * event)
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    52
{
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    53
    switch(event->button())
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    54
    {
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    55
        case Qt::LeftButton:
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    56
            setColor((m_color + 1) % m_colorsModel->rowCount());
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    57
            break;
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    58
        case Qt::RightButton:
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    59
            setColor((m_color + m_colorsModel->rowCount() - 1) % m_colorsModel->rowCount());
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    60
            break;
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    61
        default:;
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    62
    }
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    63
}
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    64
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    65
void ColorWidget::wheelEvent(QWheelEvent *event)
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    66
{
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    67
    if(event->delta() > 0)
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    68
        setColor((m_color + 1) % m_colorsModel->rowCount());
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    69
    else
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    70
        setColor((m_color + m_colorsModel->rowCount() - 1) % m_colorsModel->rowCount());
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    71
}