-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThresholdWindow.h
168 lines (121 loc) · 3.57 KB
/
ThresholdWindow.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#ifndef _ThresholdWindow_H
#define _ThresholdWindow_H
#include <QWidget>
#include <QSlider>
#include <QLabel>
#include <QVector>
#include "ui_ThresholdWindow.h"
#include <vector>
#include <string>
class ThresholdWindow : public QWidget
{
Q_OBJECT
public:
ThresholdWindow(QWidget *parent = Q_NULLPTR) : QWidget(parent)
{
ui.setupUi(this);
initialButtom();
add();
QObject::connect(ui.sentData, SIGNAL(clicked()), this, SIGNAL(storeDataToOrgData()));
QObject::connect(ui.undo, SIGNAL(clicked()), this, SIGNAL(undoData()));
}
~ThresholdWindow()
{
for (int i = 0; i < slider_sets.size(); ++i)
{
delete slider_sets[i];
delete label_sets[i];
}
}
const std::vector<int>& getThresholds(void) const
{
return sliders_value;
}
signals:
void clickSentThresholdsBottum(void);
void storeDataToOrgData(void);
void undoData(void);
public slots:
void add(void)
{
slider_sets.push_back(new QSlider{ Qt::Horizontal,this });
slider_sets[slider_sets.size() - 1]->setRange(0, 255);
QObject::connect(slider_sets[slider_sets.size() - 1], SIGNAL(valueChanged(int)), this, SLOT(showText(int)));
QObject::connect(slider_sets[slider_sets.size() - 1], SIGNAL(valueChanged(int)), this, SIGNAL(clickSentThresholdsBottum()));
label_sets.push_back(new QLabel{ this });
sliders_value.push_back(0);
calculateSlidersValue();
int cnt = sliders_value.size() - 1;
ui.top_layout->addWidget(slider_sets[cnt], cnt, 0);
ui.top_layout->addWidget(label_sets[cnt], cnt, 1);
this->update();
}
void eliminate(void)
{
if (slider_sets.size() == 1)
return;
int cnt = slider_sets.size() - 1;
ui.top_layout->removeWidget(slider_sets[cnt]);
ui.top_layout->removeWidget(label_sets[cnt]);
delete slider_sets[cnt];
delete label_sets[cnt];
slider_sets.pop_back();
label_sets.pop_back();
sliders_value.pop_back();
calculateSlidersValue();
}
void showText(int v)
{
int idx;
bool flag = false;
for (idx = 0; idx < slider_sets.size(); ++idx)
if (slider_sets[idx]->hasFocus())
{
flag = true;
break;
}
if (flag)
{
if (idx > 0 && slider_sets[idx]->value() <= slider_sets[idx - 1]->value())
{
slider_sets[idx]->setValue(slider_sets[idx - 1]->value() + 1);
return;
}
if (idx < slider_sets.size() - 1 && slider_sets[idx]->value() >= slider_sets[idx + 1]->value())
{
slider_sets[idx]->setValue(slider_sets[idx + 1]->value() - 1);
return;
}
label_sets[idx]->setText(QString{ std::to_string(v).c_str() });
sliders_value[idx] = v;
}
}
private:
Ui::ThresholdWindow ui;
QVector<QSlider*> slider_sets;
QVector<QLabel*> label_sets;
std::vector<int> sliders_value;
private:
void initialButtom(void)
{
ui.addSlider->setText(QStringLiteral("¼W¥["));
ui.deleteSlider->setText(QStringLiteral("§R°£"));
ui.sentData->setText(QStringLiteral("½T©w"));
ui.undo->setText(QStringLiteral("¦^´_"));
}
void calculateSlidersValue(void)
{
int dif = 255 / (slider_sets.size() + 1);
int num;
for (unsigned i = 0; i < slider_sets.size(); ++i)
{
QObject::disconnect(slider_sets[i], SIGNAL(valueChanged(int)), this, SIGNAL(clickSentThresholdsBottum()));
num = (i + 1) * dif;
slider_sets[i]->setValue(num);
label_sets[i]->setText(QString{ std::to_string(num).c_str() });
sliders_value[i] = num;
QObject::connect(slider_sets[i], SIGNAL(valueChanged(int)), this, SIGNAL(clickSentThresholdsBottum()));
}
}
};
#endif // end _ThresholdWindow_H