• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mcallegari / qlcplus / 13633248611

03 Mar 2025 02:31PM UTC coverage: 31.871% (+0.4%) from 31.5%
13633248611

push

github

web-flow
actions: add chrpath to profile

14689 of 46089 relevant lines covered (31.87%)

26426.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/ui/src/knobwidget.cpp
1
/*
2
  Q Light Controller Plus
3
  knobwidget.cpp
4

5
  Copyright (c) Massimo Callegari
6

7
  Licensed under the Apache License, Version 2.0 (the "License");
8
  you may not use this file except in compliance with the License.
9
  You may obtain a copy of the License at
10

11
      http://www.apache.org/licenses/LICENSE-2.0.txt
12

13
  Unless required by applicable law or agreed to in writing, software
14
  distributed under the License is distributed on an "AS IS" BASIS,
15
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
  See the License for the specific language governing permissions and
17
  limitations under the License.
18
*/
19

20
#include <QDebug>
21
#include <QPainter>
22
#include <QTransform>
23

24
#include "qlcmacros.h"
25
#include "knobwidget.h"
26

27
KnobWidget::KnobWidget(QWidget *parent) : QDial(parent)
×
28
{
29
    m_background = new QPixmap();
×
30
    m_cursor = new QPixmap();
×
31
    setWrapping(false);
×
32
    setMinimum(0);
×
33
    setMaximum(UCHAR_MAX);
×
34
    m_gradStartColor = Qt::darkGray;
×
35
    m_gradEndColor = Qt::gray;
×
36
}
×
37

38
KnobWidget::~KnobWidget()
×
39
{
40
    delete m_background;
×
41
    delete m_cursor;
×
42
}
×
43

44
void KnobWidget::setEnabled(bool status)
×
45
{
46
    QDial::setEnabled(status);
×
47
    prepareCursor();
×
48
}
×
49

50
void KnobWidget::setColor(QColor color)
×
51
{
52
    m_gradStartColor = color;
×
53
    m_gradEndColor = color.lighter(150);
×
54
    prepareBody();
×
55
    update();
×
56
}
×
57

58
void KnobWidget::prepareCursor()
×
59
{
60
    int shortSide = height();
61
    if (width() < shortSide)
×
62
        shortSide = width();
63
    float arcWidth = shortSide / 15;
×
64
    float dialSize = shortSide - (arcWidth * 2);
×
65
    float cursor_radius = dialSize / 15;
×
66
    if (cursor_radius < 3)
×
67
        cursor_radius = 3;
68

69
    QPainter fgP(m_cursor);
×
70
    fgP.setRenderHints(QPainter::Antialiasing);
×
71
    //fgP.setCompositionMode(QPainter::CompositionMode_DestinationIn);
72
    fgP.fillRect(m_cursor->rect(), Qt::transparent);
×
73

74
    //fgP.setCompositionMode(QPainter::CompositionMode_Source);
75
    if (isEnabled() == false)
×
76
        fgP.setBrush(Qt::gray);
×
77
    else
78
        fgP.setBrush(Qt::green);
×
79
    fgP.drawEllipse(QPointF(dialSize / 2 - (arcWidth * 1.5), dialSize - (arcWidth * 2.2)),
×
80
                    cursor_radius, cursor_radius);
81
}
×
82

83
void KnobWidget::prepareBody()
×
84
{
85
    int shortSide = height();
86
    if (width() < shortSide)
×
87
        shortSide = width();
88
    float arcWidth = shortSide / 15;
×
89
    float dialSize = shortSide - (arcWidth * 2);
×
90
    float radius = dialSize / 2;
×
91

92
    QLinearGradient linearGrad(QPointF(0,0), QPointF(0, dialSize));
×
93
    linearGrad.setColorAt(0, m_gradStartColor);
×
94
    linearGrad.setColorAt(1, m_gradEndColor);
×
95
    QLinearGradient linearGrad2(QPointF(0,0), QPointF(0, dialSize));
×
96
    linearGrad2.setColorAt(0, m_gradEndColor);
×
97
    linearGrad2.setColorAt(1, m_gradStartColor);
×
98

99
    m_background = new QPixmap(dialSize, dialSize);
×
100
    m_background->fill(Qt::transparent);
×
101
    m_cursor = new QPixmap(dialSize, dialSize);
×
102
    m_cursor->fill(Qt::transparent);
×
103

104
    QPainter bgP(m_background);
×
105
    bgP.setRenderHints(QPainter::Antialiasing);
×
106
    bgP.fillRect(m_background->rect(), Qt::transparent);
×
107

108
    bgP.setBrush(linearGrad);
×
109
    bgP.drawEllipse(QPointF(dialSize / 2, radius), radius, radius);
×
110
    bgP.setBrush(linearGrad2);
×
111
    bgP.setPen(Qt::NoPen);
×
112
    bgP.drawEllipse(QPointF(dialSize / 2, radius), radius - (arcWidth * 2), radius - (arcWidth * 2));
×
113
}
×
114

115
void KnobWidget::resizeEvent(QResizeEvent *e)
×
116
{
117
    QDial::resizeEvent(e);
×
118

119
    prepareBody();
×
120
    prepareCursor();
×
121
}
×
122

123
void KnobWidget::paintEvent(QPaintEvent *e)
×
124
{
125
    Q_UNUSED(e)
126

127
    int shortSide = height();
128
    if (width() < shortSide)
×
129
        shortSide = width();
130
    float arcWidth = shortSide / 15;
×
131
    QPointF pixPoint = QPointF(((width() - m_background->width()) / 2), arcWidth);
×
132

133
    QPainter painter(this);
×
134
    float degrees = 0.0;
135
    if (invertedAppearance())
×
136
        degrees = SCALE(value(), minimum(), maximum(), 330.0, 0.0);
×
137
    else
138
        degrees = SCALE(value(), minimum(), maximum(), 0.0, 330.0);
×
139

140
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
×
141
    painter.drawPixmap(pixPoint, *m_background);
×
142

143
    QPixmap rotNeedle = rotatePix(m_cursor, degrees);
×
144
    painter.drawPixmap(pixPoint, rotNeedle);
×
145

146
    QRectF valRect = QRectF(pixPoint.x() - (arcWidth / 2) + 1, arcWidth / 2 + 1,
×
147
                             m_background->width() + arcWidth - 2, m_background->height() + arcWidth - 2);
×
148

149
    int penWidth = arcWidth;
×
150
    if (arcWidth <= 5)
×
151
        penWidth = 5;
152

153
    painter.setPen(QPen(QColor(100, 100, 100, 255), penWidth - 1));
×
154
    painter.drawArc(valRect, -105 * 16, -330 * 16);
×
155
    QColor col = isEnabled() ? Qt::green : Qt::lightGray;
×
156
    painter.setPen(QPen(col, penWidth - 3));
×
157
    if (invertedAppearance())
×
158
        painter.drawArc(valRect, -75 * 16, (330-degrees) * 16);
×
159
    else
160
        painter.drawArc(valRect, -105 * 16, -degrees * 16);
×
161
}
×
162

163
void KnobWidget::wheelEvent(QWheelEvent *e)
×
164
{
165
    setSliderDown(true);
×
166
    QDial::wheelEvent(e);
×
167
    setSliderDown(false);
×
168
}
×
169

170
QPixmap KnobWidget::rotatePix(QPixmap *p_pix, float p_deg)
×
171
{
172
    // perform rotation, transforming around the center of the image
173
    QTransform trans;
×
174
    trans.translate(p_pix->width()/2.0 , p_pix->height()/2.0);
×
175
    trans.rotate(p_deg);
×
176
    trans.translate(-p_pix->width()/2.0 , -p_pix->height()/2.0);
×
177
    QPixmap outPix = p_pix->transformed(trans, Qt::SmoothTransformation);
×
178

179
    // re-crop to original size
180
    int xOffset = (outPix.width() - p_pix->width()) / 2;
×
181
    int yOffset = (outPix.height() - p_pix->height()) / 2;
×
182
    outPix = outPix.copy(xOffset, yOffset, p_pix->width(), p_pix->height());
×
183

184
    return outPix;
×
185

186
}
×
187

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc