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

mcallegari / qlcplus / 13763878004

10 Mar 2025 11:47AM UTC coverage: 31.887%. Remained the same
13763878004

push

github

mcallegari
4.14.1 release to date

14701 of 46103 relevant lines covered (31.89%)

26418.09 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
#include <QSettings>
24

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

28
#define SETTINGS_GEOMETRY "addresstool/geometry"
29

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

38
    px.fill(QColor("#E7354A"));
×
39
    ui->m_redBtn->setIcon(QIcon(px));
×
40

41
    px.fill(QColor("#0165DF"));
×
42
    ui->m_blueBtn->setIcon(QIcon(px));
×
43

44
    px.fill(Qt::black);
×
45
    ui->m_blackBtn->setIcon(QIcon(px));
×
46

47
    ui->m_addressSpin->setValue(presetValue);
×
48

49
    m_dipSwitch = new DIPSwitchWidget(this, presetValue);
×
50
    ui->m_gridLayout->addWidget(m_dipSwitch, 0, 0, 1, 5);
×
51
    m_dipSwitch->setMinimumHeight(80);
×
52

53
    QSettings settings;
×
54
    QVariant geometrySettings = settings.value(SETTINGS_GEOMETRY);
×
55
    if (geometrySettings.isValid() == true)
×
56
        restoreGeometry(geometrySettings.toByteArray());
×
57

58
    connect(ui->m_addressSpin, SIGNAL(valueChanged(int)),
×
59
            m_dipSwitch, SLOT(slotSetValue(int)));
×
60
    connect(m_dipSwitch, SIGNAL(valueChanged(int)),
×
61
            ui->m_addressSpin, SLOT(setValue(int)));
×
62
    connect(ui->m_reverseVertCheck, SIGNAL(toggled(bool)),
×
63
            m_dipSwitch, SLOT(slotReverseVertically(bool)));
×
64
    connect(ui->m_reverseHorizCheck, SIGNAL(toggled(bool)),
×
65
            m_dipSwitch, SLOT(slotReverseHorizontally(bool)));
×
66

67
}
×
68

69
AddressTool::~AddressTool()
×
70
{
71
    QSettings settings;
×
72
    settings.setValue(SETTINGS_GEOMETRY, saveGeometry());
×
73

74
    delete ui;
×
75
}
×
76

77
int AddressTool::getAddress()
×
78
{
79
    return (ui->m_addressSpin->value());
×
80
}
81

82
void AddressTool::slotChangeColor()
×
83
{
84
    if (m_dipSwitch == NULL)
×
85
        return;
86

87
    if (sender() == ui->m_blueBtn)
×
88
        m_dipSwitch->setColor(QColor("#0165DF"));
×
89
    if (sender() == ui->m_redBtn)
×
90
        m_dipSwitch->setColor(QColor("#E7354A"));
×
91
    else if (sender() == ui->m_blackBtn)
×
92
        m_dipSwitch->setColor(Qt::black);
×
93
}
94

95
/***************************************************************************
96
 *
97
 * DIPSwitchWidget class implementation
98
 *
99
 ***************************************************************************/
100

101
DIPSwitchWidget::DIPSwitchWidget(QWidget *parent, int presetValue) :
×
102
    QWidget(parent)
×
103
{
104
    m_value = presetValue;
×
105
    m_backCol = QColor("#0165DF");
×
106
    m_verticalReverse = false;
×
107
    m_horizontalReverse = false;
×
108

109
    m_font = QApplication::font();
×
110
    m_font.setBold(true);
111
    m_font.setPixelSize(12);
×
112

113
    for (quint8 i=0; i < 10; ++i)
×
114
        m_sliders[i] = new DIPSwitchSlider(this);
×
115
}
×
116

117
DIPSwitchWidget::~DIPSwitchWidget()
×
118
{
119
}
×
120

121
void DIPSwitchWidget::slotReverseVertically(bool toggle)
×
122
{
123
    m_verticalReverse = toggle;
×
124
    update();
×
125
}
×
126

127
void DIPSwitchWidget::slotReverseHorizontally(bool toggle)
×
128
{
129
    m_horizontalReverse = toggle;
×
130
    updateSliders();
×
131
    update();
×
132
}
×
133

134
void DIPSwitchWidget::slotSetValue(int value)
×
135
{
136
    m_value = value;
×
137
    update();
×
138
}
×
139

140
void DIPSwitchWidget::setColor(QColor col)
×
141
{
142
    m_backCol = col;
×
143
    update();
×
144
}
×
145

146
void DIPSwitchWidget::updateSliders()
×
147
{
148
    int margin = 20;
149
    float minDiv = (width() - (margin * 2)) / 10;
×
150
    float dipW = (minDiv / 3) * 2;
×
151
    float xpos = margin + (minDiv / 3);
×
152

153
    for (quint8 i = 0; i < 10; i++)
×
154
    {
155
        quint8 slider_id = i;
×
156
        if (m_horizontalReverse) slider_id = 9 - i;
×
157

158
        m_sliders[slider_id]->setPosition(QPoint(xpos, 20), QSize(dipW, height() - 40));
×
159
        xpos += minDiv;
×
160
    }
161
}
×
162

163
void DIPSwitchWidget::resizeEvent(QResizeEvent *e)
×
164
{
165
    QWidget::resizeEvent(e);
×
166

167
    updateSliders();
×
168
}
×
169

170
void DIPSwitchWidget::mousePressEvent(QMouseEvent *e)
×
171
{
172
    QMap<quint8, DIPSwitchSlider*>::iterator it;
173
    for (it = m_sliders.begin(); it != m_sliders.end(); ++it)
×
174
    {
175
        if (it.value()->isClicked(e->pos()))
×
176
        {
177
            quint32 newvalue = m_value ^ (1<<it.key());
×
178

179
                        if (newvalue == 0 && m_value != 512) newvalue = m_value;
×
180
            if (newvalue == 0) newvalue = 1;
181
            if (newvalue > 512) newvalue = 512;
182

183
            m_value = newvalue;
×
184
            update();
×
185
            emit valueChanged(m_value);
×
186
        }
187
    }
188
}
×
189

190
void DIPSwitchWidget::paintEvent(QPaintEvent *e)
×
191
{
192
    QWidget::paintEvent(e);
×
193

194
    int i, j;
195
    int margin = 20;
196
    float minDiv = (width() - (margin * 2)) / 10;
×
197
    float xpos = margin + (minDiv / 3);
×
198
    int onPos = 15; // position of the "ON" string
199
    int numPos = height() - 5; // position of number labels
×
200

201
    QPainter painter(this);
×
202

203
    painter.setPen(QPen(Qt::black, 2));
×
204
    painter.setBrush(QBrush(m_backCol));
×
205
    painter.drawRect(0, 0, width(), height());
×
206

207
    // draw DIP switch sliders
208
    for (i = 0; i < 10; i++)
×
209
        m_sliders[i]->paint(&painter, (1<<i) & m_value, m_verticalReverse);
×
210

211
    // draw labels and value
212
    painter.setFont(m_font);
×
213
    painter.setPen(Qt::white);
×
214

215
    xpos = margin + (minDiv / 3);
216
    if (m_verticalReverse == true)
×
217
    {
218
        onPos = height() - 5;
×
219
        numPos = 15;
220
    }
221

222
    painter.drawText(xpos, onPos, "ON");
×
223

224
    if (m_horizontalReverse == false)
×
225
    {
226

227
        for (i = 0, j = 9; i < 10; i++, j--)
×
228
        {
229
            painter.drawText((i == 9)?(xpos-2):(xpos+2), numPos, QString("%1").arg(i + 1));
×
230
            xpos += minDiv;
×
231
        }
232
    }
233
    else
234
    {
235
        for (i = 10, j = 0; i > 0; i--, j++)
×
236
        {
237
            painter.drawText((i == 10)?(xpos-2):(xpos + 2), numPos, QString("%1").arg(i));
×
238
            xpos += minDiv;
×
239
        }
240
    }
241
}
×
242

243
/***************************************************************************
244
 *
245
 * DIPSwitchSlider class implementation
246
 *
247
 ***************************************************************************/
248
DIPSwitchSlider::DIPSwitchSlider(QObject *parent) :
×
249
    QObject(parent)
×
250
{
251
}
×
252

253
DIPSwitchSlider::~DIPSwitchSlider()
×
254
{
255
}
×
256

257
void DIPSwitchSlider::paint(QPainter *painter, bool value, bool vreverse)
×
258
{
259
    // Draw outer Rectangle
260
    painter->setBrush(Qt::darkGray);
×
261
    painter->setPen(QPen(Qt::black, 2));
×
262
    painter->drawRect(QRect(m_pos, m_size));
×
263

264
    // Draw inner Rectangle (slider position)
265
    painter->setPen(Qt::white);
×
266
    painter->setBrush(Qt::white);
×
267

268
    QPoint slider_pos(m_pos.x() + 1, m_pos.y() + 1);
×
269
    QSize slider_size(m_size.width() - 3, m_size.width() - 3);
×
270
    if (slider_size.height() > m_size.height() / 2)
×
271
        slider_size.setHeight(m_size.height() / 2);
272

273
    if (value == vreverse) // down
×
274
        slider_pos.setY(slider_pos.y() + m_size.height() - slider_size.height() - 3);
×
275

276
    painter->drawRect(QRect(slider_pos, slider_size));
×
277
}
×
278

279
void DIPSwitchSlider::setPosition(QPoint pos, QSize size)
×
280
{
281
    m_pos = pos;
×
282
    m_size = size;
×
283
}
×
284

285

286
bool DIPSwitchSlider::isClicked(QPoint click)
×
287
{
288
    return(QRect(m_pos, m_size).contains(click));
×
289
}
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