QTfrontend/ui/widget/colorwidget.cpp
author Wuzzy <Wuzzy2@mail.ru>
Fri, 24 May 2019 22:07:44 +0200
changeset 15048 35932896ce84
parent 15005 d597a851f5d8
child 15166 d20e52a646e7
permissions -rw-r--r--
Prevent crates from being collected twice
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>
7749
edad8a7bcaea Convert ColorWidget from QWidget to QFrame and make use of The Box Model(tm) in stylesheet
unc0rr
parents: 7130
diff changeset
     4
#include <QColor>
7130
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
     5
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
     6
#include "colorwidget.h"
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
     7
#include "hwconsts.h"
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
     8
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
     9
ColorWidget::ColorWidget(QStandardItemModel *colorsModel, QWidget *parent) :
7749
edad8a7bcaea Convert ColorWidget from QWidget to QFrame and make use of The Box Model(tm) in stylesheet
unc0rr
parents: 7130
diff changeset
    10
    QFrame(parent)
7130
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    11
{
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    12
    m_colorsModel = colorsModel;
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    13
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    14
    setColor(0);
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
15005
d597a851f5d8 Frontend: Highlight button when pressed
Wuzzy <Wuzzy2@mail.ru>
parents: 13233
diff changeset
    33
    setStyleSheet(QString("* { border: 2px solid #ffcc00; border-radius: 8px; background: %1 } :disabled { border-color: #a0a0a0; } :hover { border-color: #ffff00; }").arg(item->data().value<QColor>().name()));
7749
edad8a7bcaea Convert ColorWidget from QWidget to QFrame and make use of The Box Model(tm) in stylesheet
unc0rr
parents: 7130
diff changeset
    34
    /*
7130
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    35
    QPalette p = palette();
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    36
    p.setColor(QPalette::Window, item->data().value<QColor>());
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    37
    setPalette(p);
7749
edad8a7bcaea Convert ColorWidget from QWidget to QFrame and make use of The Box Model(tm) in stylesheet
unc0rr
parents: 7130
diff changeset
    38
    */
7130
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    39
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    40
    emit colorChanged(m_color);
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    41
}
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    42
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    43
int ColorWidget::getColor()
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    44
{
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    45
    return m_color;
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    46
}
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    47
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    48
void ColorWidget::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    49
{
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    50
    if(m_color >= topLeft.row() && m_color <= bottomRight.row())
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    51
        setColor(m_color);
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    52
}
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    53
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    54
void ColorWidget::mousePressEvent(QMouseEvent * event)
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    55
{
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    56
    switch(event->button())
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    57
    {
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    58
        case Qt::LeftButton:
7750
31e4f6c1834b small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents: 7749
diff changeset
    59
            nextColor();
7130
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
        case Qt::RightButton:
7750
31e4f6c1834b small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents: 7749
diff changeset
    62
            previousColor();
7130
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    63
            break;
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    64
        default:;
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    65
    }
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    66
}
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    67
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    68
void ColorWidget::wheelEvent(QWheelEvent *event)
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    69
{
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    70
    if(event->delta() > 0)
7750
31e4f6c1834b small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents: 7749
diff changeset
    71
        previousColor();
7130
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    72
    else
7750
31e4f6c1834b small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents: 7749
diff changeset
    73
        nextColor();
7130
fcab1fd02bc6 - Allow switching colors with mouse wheel
unc0rr
parents:
diff changeset
    74
}
7750
31e4f6c1834b small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents: 7749
diff changeset
    75
31e4f6c1834b small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents: 7749
diff changeset
    76
void ColorWidget::nextColor()
31e4f6c1834b small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents: 7749
diff changeset
    77
{
31e4f6c1834b small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents: 7749
diff changeset
    78
    setColor((m_color + 1) % m_colorsModel->rowCount());
31e4f6c1834b small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents: 7749
diff changeset
    79
}
31e4f6c1834b small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents: 7749
diff changeset
    80
31e4f6c1834b small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents: 7749
diff changeset
    81
void ColorWidget::previousColor()
31e4f6c1834b small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents: 7749
diff changeset
    82
{
31e4f6c1834b small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents: 7749
diff changeset
    83
    setColor((m_color + m_colorsModel->rowCount() - 1) % m_colorsModel->rowCount());
31e4f6c1834b small tweak. (reverse mouse wheel directions for color change)
sheepluva
parents: 7749
diff changeset
    84
}