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

mcallegari / qlcplus / 7252848206

18 Dec 2023 07:26PM UTC coverage: 32.067% (+0.001%) from 32.066%
7252848206

push

github

mcallegari
Code style review #1427

199 of 628 new or added lines in 101 files covered. (31.69%)

8 existing lines in 2 files now uncovered.

15169 of 47304 relevant lines covered (32.07%)

23733.74 hits per line

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

0.0
/ui/src/addresstool.cpp
1
/*
2
  Q Light Controller Plus
3
  addresstool.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 <QPainter>
21
#include <QPixmap>
22
#include <QMouseEvent>
23

24
#include "addresstool.h"
25
#include "ui_addresstool.h"
26

27
AddressTool::AddressTool(QWidget *parent, int presetValue) :
×
28
    QDialog(parent)
29
  , ui(new Ui::AddressTool)
×
30
  , m_dipSwitch(NULL)
×
31
{
32
    ui->setupUi(this);
×
33
    QPixmap px(16, 16);
×
34

35
    px.fill(QColor("#E7354A"));
×
36
    ui->m_redBtn->setIcon(QIcon(px));
×
37

38
    px.fill(QColor("#0165DF"));
×
39
    ui->m_blueBtn->setIcon(QIcon(px));
×
40

41
    px.fill(Qt::black);
×
42
    ui->m_blackBtn->setIcon(QIcon(px));
×
43

44
    ui->m_addressSpin->setValue(presetValue);
×
45

46
    m_dipSwitch = new DIPSwitchWidget(this, presetValue);
×
47
    ui->m_gridLayout->addWidget(m_dipSwitch, 0, 0, 1, 5);
×
48
    m_dipSwitch->setMinimumHeight(80);
×
49

50
    connect(ui->m_addressSpin, SIGNAL(valueChanged(int)),
×
51
            m_dipSwitch, SLOT(slotSetValue(int)));
×
52
    connect(m_dipSwitch, SIGNAL(valueChanged(int)),
×
53
            ui->m_addressSpin, SLOT(setValue(int)));
×
54
    connect(ui->m_reverseVertCheck, SIGNAL(toggled(bool)),
×
55
            m_dipSwitch, SLOT(slotReverseVertically(bool)));
×
56
    connect(ui->m_reverseHorizCheck, SIGNAL(toggled(bool)),
×
57
            m_dipSwitch, SLOT(slotReverseHorizontally(bool)));
×
58

59
}
×
60

61
AddressTool::~AddressTool()
×
62
{
63
    delete ui;
×
64
}
×
65

66
int AddressTool::getAddress()
×
67
{
68
    return (ui->m_addressSpin->value());
×
69
}
70

71
void AddressTool::slotChangeColor()
×
72
{
73
    if (m_dipSwitch == NULL)
×
74
        return;
×
75

76
    if (sender() == ui->m_blueBtn)
×
77
        m_dipSwitch->setColor(QColor("#0165DF"));
×
78
    if (sender() == ui->m_redBtn)
×
79
        m_dipSwitch->setColor(QColor("#E7354A"));
×
80
    else if (sender() == ui->m_blackBtn)
×
81
        m_dipSwitch->setColor(Qt::black);
×
82
}
83

84
/***************************************************************************
85
 *
86
 * DIPSwitchWidget class implementation
87
 *
88
 ***************************************************************************/
89

90
DIPSwitchWidget::DIPSwitchWidget(QWidget *parent, int presetValue) :
×
91
    QWidget(parent)
×
92
{
93
    m_value = presetValue;
×
94
    m_backCol = QColor("#0165DF");
×
95
    m_verticalReverse = false;
×
96
    m_horizontalReverse = false;
×
97

98
    m_font = QApplication::font();
×
99
    m_font.setBold(true);
×
100
    m_font.setPixelSize(12);
×
101

102
    for (quint8 i=0; i < 10; ++i)
×
103
        m_sliders[i] = new DIPSwitchSlider(this);
×
104
}
×
105

106
DIPSwitchWidget::~DIPSwitchWidget()
×
107
{
108
}
×
109

110
void DIPSwitchWidget::slotReverseVertically(bool toggle)
×
111
{
112
    m_verticalReverse = toggle;
×
113
    update();
×
114
}
×
115

116
void DIPSwitchWidget::slotReverseHorizontally(bool toggle)
×
117
{
118
    m_horizontalReverse = toggle;
×
119
    updateSliders();
×
120
    update();
×
121
}
×
122

123
void DIPSwitchWidget::slotSetValue(int value)
×
124
{
125
    m_value = value;
×
126
    update();
×
127
}
×
128

129
void DIPSwitchWidget::setColor(QColor col)
×
130
{
131
    m_backCol = col;
×
132
    update();
×
133
}
×
134

135
void DIPSwitchWidget::updateSliders()
×
136
{
137
    int margin = 20;
×
138
    float minDiv = (width() - (margin * 2)) / 10;
×
139
    float dipW = (minDiv / 3) * 2;
×
140
    float xpos = margin + (minDiv / 3);
×
141

142
    for (quint8 i = 0; i < 10; i++)
×
143
    {
144
        quint8 slider_id = i;
×
145
        if (m_horizontalReverse) slider_id = 9 - i;
×
146

147
        m_sliders[slider_id]->setPosition(QPoint(xpos, 20), QSize(dipW, height() - 40));
×
148
        xpos += minDiv;
×
149
    }
150
}
×
151

152
void DIPSwitchWidget::resizeEvent(QResizeEvent *e)
×
153
{
154
    QWidget::resizeEvent(e);
×
155

156
    updateSliders();
×
157
}
×
158

159
void DIPSwitchWidget::mousePressEvent(QMouseEvent *e)
×
160
{
161
    QMap<quint8, DIPSwitchSlider*>::iterator it;
×
162
    for (it = m_sliders.begin(); it != m_sliders.end(); ++it)
×
163
    {
164
        if (it.value()->isClicked(e->pos()))
×
165
        {
166
            quint32 newvalue = m_value ^ (1<<it.key());
×
167

168
                        if (newvalue == 0 && m_value != 512) newvalue = m_value;
×
NEW
169
            if (newvalue == 0) newvalue = 1;
×
170
            if (newvalue > 512) newvalue = 512;
×
171

172
            m_value = newvalue;
×
173
            update();
×
174
            emit valueChanged(m_value);
×
175
        }
176
    }
177
}
×
178

179
void DIPSwitchWidget::paintEvent(QPaintEvent *e)
×
180
{
181
    QWidget::paintEvent(e);
×
182

183
    int i, j;
184
    int margin = 20;
×
185
    float minDiv = (width() - (margin * 2)) / 10;
×
186
    float xpos = margin + (minDiv / 3);
×
187
    int onPos = 15; // position of the "ON" string
×
188
    int numPos = height() - 5; // position of number labels
×
189

190
    QPainter painter(this);
×
191

192
    painter.setPen(QPen(Qt::black, 2));
×
193
    painter.setBrush(QBrush(m_backCol));
×
194
    painter.drawRect(0, 0, width(), height());
×
195

196
    // draw DIP switch sliders
197
    for (i = 0; i < 10; i++)
×
198
        m_sliders[i]->paint(&painter, (1<<i) & m_value, m_verticalReverse);
×
199

200
    // draw labels and value
201
    painter.setFont(m_font);
×
202
    painter.setPen(Qt::white);
×
203

204
    xpos = margin + (minDiv / 3);
×
205
    if (m_verticalReverse == true)
×
206
    {
207
        onPos = height() - 5;
×
208
        numPos = 15;
×
209
    }
210

211
    painter.drawText(xpos, onPos, "ON");
×
212

213
    if (m_horizontalReverse == false)
×
214
    {
215

216
        for (i = 0, j = 9; i < 10; i++, j--)
×
217
        {
218
            painter.drawText((i == 9)?(xpos-2):(xpos+2), numPos, QString("%1").arg(i + 1));
×
219
            xpos += minDiv;
×
220
        }
221
    }
222
    else
223
    {
224
        for (i = 10, j = 0; i > 0; i--, j++)
×
225
        {
226
            painter.drawText((i == 10)?(xpos-2):(xpos + 2), numPos, QString("%1").arg(i));
×
227
            xpos += minDiv;
×
228
        }
229
    }
230
}
×
231

232
/***************************************************************************
233
 *
234
 * DIPSwitchSlider class implementation
235
 *
236
 ***************************************************************************/
237
DIPSwitchSlider::DIPSwitchSlider(QObject *parent) :
×
238
    QObject(parent)
×
239
{
240
}
×
241

242
DIPSwitchSlider::~DIPSwitchSlider()
×
243
{
244
}
×
245

246
void DIPSwitchSlider::paint(QPainter *painter, bool value, bool vreverse)
×
247
{
248
    // Draw outer Rectangle
249
    painter->setBrush(Qt::darkGray);
×
250
    painter->setPen(QPen(Qt::black, 2));
×
251
    painter->drawRect(QRect(m_pos, m_size));
×
252

253
    // Draw inner Rectangle (slider position)
254
    painter->setPen(Qt::white);
×
255
    painter->setBrush(Qt::white);
×
256

257
    QPoint slider_pos(m_pos.x() + 1, m_pos.y() + 1);
×
258
    QSize slider_size(m_size.width() - 3, m_size.width() - 3);
×
259
    if (slider_size.height() > m_size.height() / 2)
×
260
        slider_size.setHeight(m_size.height() / 2);
×
261

262
    if (value == vreverse) // down
×
263
        slider_pos.setY(slider_pos.y() + m_size.height() - slider_size.height() - 3);
×
264

265
    painter->drawRect(QRect(slider_pos, slider_size));
×
266
}
×
267

268
void DIPSwitchSlider::setPosition(QPoint pos, QSize size)
×
269
{
270
    m_pos = pos;
×
271
    m_size = size;
×
272
}
×
273

274

275
bool DIPSwitchSlider::isClicked(QPoint click)
×
276
{
277
    return(QRect(m_pos, m_size).contains(click));
×
278
}
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