-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callsigns.cpp
282 lines (236 loc) · 11 KB
/
callsigns.cpp
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "callsigns.h"
#include "ui_callsigns.h"
Callsigns::Callsigns(QSqlDatabase db, HttpApi *api, QWidget *parent) :
QDialog(parent),
ui(new Ui::Callsigns),
db(db),
api(api)
{
ui->setupUi(this);
setWindowTitle("Управление позывными");
ui->saveButton->setEnabled(false);
ui->cancelButton->setEnabled(false);
connect(ui->addButton, SIGNAL(clicked()), this, SLOT(onAddPressed()));
connect(ui->removeButton, SIGNAL(clicked()), this, SLOT(onRemovePressed()));
connect(ui->saveButton, SIGNAL(clicked()), this, SLOT(onSavePressed()));
connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(onCancelPressed()));
connect(ui->updateFromQsosu, SIGNAL(clicked(bool)), this, SLOT(requestQsosu()));
connect(api, SIGNAL(callsignStatus(int)), this, SLOT(callsignStatus(int)));
init();
updateTable();
}
Callsigns::~Callsigns()
{
delete ui;
}
void Callsigns::init() {
CallsignsModel = new QSqlTableModel(this);
CallsignsModel->setTable("callsigns");
CallsignsModel->setEditStrategy(QSqlTableModel::OnFieldChange);
CallsignsModel->setHeaderData(3, Qt::Horizontal, tr("Позывной"));
CallsignsModel->setHeaderData(6, Qt::Horizontal, tr("QTH локатор"));
CallsignsModel->setHeaderData(7, Qt::Horizontal, tr("RDA"));
CallsignsModel->setHeaderData(8, Qt::Horizontal, tr("ITU зона"));
CallsignsModel->setHeaderData(9, Qt::Horizontal, tr("CQ зона"));
ui->callTable->setModel(CallsignsModel);
ui->callTable->setColumnHidden(0, true);
ui->callTable->setColumnHidden(1, true);
ui->callTable->setColumnHidden(2, true);
ui->callTable->setColumnHidden(4, true);
ui->callTable->setColumnHidden(5, true);
ui->callTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
ui->callTable->setStyleSheet("selection-background-color: rgb(201, 217, 233); selection-color: rgb(0, 0, 0);");
ui->callTable->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->callTable->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
ui->callTable->verticalHeader()->setDefaultSectionSize(20);
}
void Callsigns::updateTable() {
CallsignsModel->select();
}
void Callsigns::onAddPressed() {
add_cs = new Addcallsign(this);
connect(add_cs, SIGNAL(addCallsign()), this, SLOT(addCallsigng()));
add_cs->exec();
}
void Callsigns::addCallsigng()
{
int rowCount = CallsignsModel->rowCount(QModelIndex());
CallsignsModel->insertRow(rowCount);
QModelIndex index = CallsignsModel->index(rowCount, 1);
ui->callTable->setCurrentIndex(index);
//ui->callTable->edit(index);
CallsignsModel->setData(CallsignsModel->index(rowCount, 2), add_cs->add_CallsignType);
CallsignsModel->setData(CallsignsModel->index(rowCount, 3), add_cs->add_Callsign);
CallsignsModel->setData(CallsignsModel->index(rowCount, 4), add_cs->add_validity_start);
CallsignsModel->setData(CallsignsModel->index(rowCount, 5), add_cs->add_validity_stop);
CallsignsModel->setData(CallsignsModel->index(rowCount, 6), add_cs->add_location);
CallsignsModel->setData(CallsignsModel->index(rowCount, 7), add_cs->add_rda);
CallsignsModel->setData(CallsignsModel->index(rowCount, 8), add_cs->add_ituz);
CallsignsModel->setData(CallsignsModel->index(rowCount, 9), add_cs->add_cqz);
ui->addButton->setEnabled(false);
ui->saveButton->setEnabled(true);
ui->cancelButton->setEnabled(true);
QVariantList data;
data << add_cs->add_Callsign << add_cs->add_CallsignType << add_cs->add_location << add_cs->add_rda << add_cs->add_ituz << add_cs->add_cqz << add_cs->add_validity_start << add_cs->add_validity_stop;
api->addCallsign(data);
}
void Callsigns::onRemovePressed() {
QModelIndexList indexList = ui->callTable->selectionModel()->selectedIndexes();
if (!indexList.isEmpty()) {
int rowIndex = indexList.first().row();
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Подтверждение действия", QString("Вы уверены что хотите удалить позывной \"%1\"?").arg(CallsignsModel->index(rowIndex, 3).data().toString()), QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes) {
QSqlQuery query;
if (query.exec(QString("DELETE FROM records WHERE callsign_id='%1'").arg(CallsignsModel->index(rowIndex, 1).data().toInt()))) {
CallsignsModel->removeRow(rowIndex);
} else {
QMessageBox::critical(0, "Ошибка", "Не получилось удалить позывной. Ошибка базы данных!", QMessageBox::Ok);
return;
}
updateTable();
emit updated();
} else {
return;
}
} else {
QMessageBox::critical(0, "Ошибка", "Не выбран позывной для удаления.", QMessageBox::Ok);
return;
}
}
void Callsigns::onSavePressed() {
int newRowIndex = CallsignsModel->rowCount(QModelIndex()) - 1;
QString name = CallsignsModel->index(newRowIndex, 3).data().toString();
QString gridsquare = CallsignsModel->index(newRowIndex, 6).data().toString();
if (name.length() < 3) {
QMessageBox::critical(0, "Ошибка", "Позывной сигнал не может быть меньше 3 символов", QMessageBox::Ok);
return;
}
if (gridsquare.length() < 4) {
QMessageBox::critical(0, "Ошибка", "QTH локатор не может быть меньше 4 символов", QMessageBox::Ok);
return;
}
if (CallsignsModel->submitAll()) {
updateTable();
ui->addButton->setEnabled(true);
ui->saveButton->setEnabled(false);
ui->cancelButton->setEnabled(false);
emit updated();
} else {
QMessageBox::critical(0, "Ошибка", "Ошибка базы данных!", QMessageBox::Ok);
return;
}
}
void Callsigns::onCancelPressed() {
updateTable();
ui->addButton->setEnabled(true);
ui->saveButton->setEnabled(false);
ui->cancelButton->setEnabled(false);
}
void Callsigns::requestQsosu() {
api->getCallsign();
}
void Callsigns::onCallsignsUpdated() {
for(QVariantMap &call : api->callsigns) {
QString name = call["name"].toString();
int qsosu_id = call["id"].toInt();
int type = call["type"].toInt();
int validity_start = call["validity_start"].toInt();
int validity_stop = call["validity_stop"].toInt();
QString location = call["location"].toString();
QString rda = call["rda"].toString();
int ituz = call["ituz"].toInt();
int cqz = call["cqz"].toInt();
QSqlQuery query;
query.prepare("SELECT * FROM callsigns WHERE name=:name");
query.bindValue(":name", name);
query.exec();
query.first();
if (query.at() + 1 == 1) {
int dbid = query.value(0).toInt();
if (QMessageBox::question(this,
"Обновление позывного",
QString("Обновить данные для позывного %1?").arg(name),
QMessageBox::Yes|QMessageBox::No
) == QMessageBox::No) continue;
QSqlQuery update;
update.prepare("UPDATE callsigns SET qsosu_id=:qsosu_id, type=:type, validity_start=:validity_start, validity_stop=:validity_stop, gridsquare=:gridsquare, gridsquare=:gridsquare, cnty=:cnty, ituz=:ituz, cqz=:cqz WHERE id=:id");
update.bindValue(":qsosu_id", qsosu_id);
update.bindValue(":type", type);
update.bindValue(":validity_start", validity_start);
update.bindValue(":validity_stop", validity_stop);
update.bindValue(":gridsquare", location);
update.bindValue(":cnty", rda);
update.bindValue(":ituz", ituz);
update.bindValue(":cqz", cqz);
update.bindValue(":id", dbid);
update.exec();
if (!update.exec()) qDebug() << "Error while updateing callsing in DB";
} else {
if (QMessageBox::question(this,
"Добавление позывного",
QString("Позывной %1 отсутствует в системе. Добавить?").arg(name),
QMessageBox::Yes|QMessageBox::No
) == QMessageBox::No) continue;
QSqlQuery insert;
insert.prepare("INSERT INTO callsigns (id, qsosu_id, type, name, validity_start, validity_stop, gridsquare, cnty, ituz, cqz) VALUES (NULL, :qsosu_id, :type, :name, :validity_start, :validity_stop, :gridsquare, :cnty, :ituz, :cqz)");
insert.bindValue(":qsosu_id", qsosu_id);
insert.bindValue(":type", type);
insert.bindValue(":name", name);
insert.bindValue(":validity_start", validity_start);
insert.bindValue(":validity_stop", validity_stop);
insert.bindValue(":gridsquare", location);
insert.bindValue(":cnty", rda);
insert.bindValue(":ituz", ituz);
insert.bindValue(":cqz", cqz);
if (!insert.exec()) qDebug() << "Error while inserting new callsing in DB";
}
query.clear();
updateTable();
}
QMessageBox::information(0, "Обновление данных", "Позывные обновлены с сервиса QSO.SU", QMessageBox::Ok);
emit updated();
}
//---------------------------------------------------------------------------------------------------------------------
void Callsigns::on_checkCallsignBtn_clicked()
{
QModelIndexList indexList = ui->callTable->selectionModel()->selectedIndexes();
if (!indexList.isEmpty()) {
int rowIndex = indexList.first().row();
QString callsign = CallsignsModel->index(rowIndex, 3).data().toString();
api->checkStatusCallsign(callsign);
} else {
QMessageBox::critical(0, "Ошибка", "Не выбран позывной для проверки.", QMessageBox::Ok);
return;
}
}
//---------------------------------------------------------------------------------------------------------------------
void Callsigns::callsignStatus(int status)
{
if(status == 0) {
QMessageBox::information(0, "Проверка статуса позывного.", "Позывной на проверке!", QMessageBox::Ok);
return;
}
if(status == 1) {
QMessageBox::information(0, "Проверка статуса позывного.", "Позывной добавлен!", QMessageBox::Ok);
return;
}
if(status == 2) {
QMessageBox::information(0, "Проверка статуса позывного.", "Позывной отклонен!", QMessageBox::Ok);
return;
}
}
//---------------------------------------------------------------------------------------------------------------------
//CREATE TABLE "callsigns" (
// "id" INTEGER NOT NULL,
// "qsosu_id" INTEGER NOT NULL DEFAULT 0,
// "type" INTEGER NOT NULL DEFAULT 10,
// "name" TEXT NOT NULL,
// "validity_start" INTEGER NOT NULL DEFAULT 0,
// "validity_stop" INTEGER NOT NULL DEFAULT 0,
// "gridsquare" TEXT NOT NULL,
// "cnty" TEXT,
// "ituz" INTEGER NOT NULL DEFAULT 0,
// "cqz" INTEGER NOT NULL DEFAULT 0,
// PRIMARY KEY("id" AUTOICREMENT)
//)