forked from avartak/TkCommissioner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmsavetags.h
279 lines (223 loc) · 11.2 KB
/
frmsavetags.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
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
#ifndef FRMTAGUPLOAD_H
#define FRMTAGUPLOAD_H
#include <sstream>
// Qt includes
#include <QValidator>
#include <QRegExpValidator>
#include <QRegExp>
#include <QMessageBox>
#include <QTextStream>
#include <QString>
#include <QVector>
#include <QPushButton>
#include <QProgressDialog>
#include <QInputDialog>
#include <QDialogButtonBox>
#include <QStandardItemModel>
#include <QtSql/QSqlQuery>
// Debug output
#include "Debug.h"
#include "DbConnection.h"
// UI file
#include "ui_frmsavetags.h"
class TagUpload : public QDialog, private Ui::TagUpload {
Q_OBJECT
public:
typedef std::multimap<int,std::pair<int, QStandardItem*> > iiQMultimap;
TagUpload(QWidget *parent = 0) {
Q_UNUSED(parent);
setupUi(this);
closeTicket_ = false;
currentDevice = NULL;
currentComment = NULL;
buttonOk->setEnabled(false);
tagsModel = new QStandardItemModel(0, 2,this);
tagsModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Device Id"));
tagsModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Comment"));
uploadList->setModel(tagsModel);
uploadList->setSelectionMode(QAbstractItemView::SingleSelection);
uploadList->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
uploadList->setSelectionBehavior(QAbstractItemView::SelectRows);
connect(uploadList->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), this, SLOT(devChanged(QModelIndex,QModelIndex)));
}
~TagUpload() {
}
void setCloseTicketFlag(bool flag) {
closeTicket_ = flag;
if(closeTicket_) topLabel->setText("You want to close tickets for the following items in the database");
}
void setUploadTag(int tagNumber, QString tagDescription) {
tagNumber_ = tagNumber;
tagDescription_ = tagDescription;
displayTag->setText(tagDescription_);
}
void setRunNumber(int runNumber) {
runNumber_= runNumber;
}
void setOpenTicketAndTagList( std::multimap< int, std::pair<int, QStandardItem*> > openTickets, std::map<int, std::string> tagList) {
openTickets_ = openTickets;
tagList_ = tagList;
}
void setTagDeviceList(QVector<QStandardItem*> list) {
for (int i = 0; i < list.size(); i++) {
QList<QStandardItem*> itemrow;
itemrow.append(list[i]);
QStandardItem* comm = new QStandardItem("");
comm->setEditable(false);
itemrow.append(comm);
tagsModel->appendRow(itemrow);
}
}
private:
QStandardItemModel* tagsModel;
bool closeTicket_;
int runNumber_;
int tagNumber_;
QString tagDescription_;
std::multimap<int, std::pair<int,QStandardItem*> > openTickets_;
std::map<int, std::string> tagList_;
QStandardItem* currentDevice;
QStandardItem* currentComment;
int openTicket(int runNumber, int deviceId, const char* tagDescription, const char* comment, const char* author) {
int ticketId = 0;
std::stringstream myQuery;
myQuery << "select PkgTkAnalysisLog.openTicket(" << runNumber << "," << deviceId << ",'" << tagDescription << "','" << comment << "','" << author << "') from dual";
if (DbConnection::Inst()->dbConnected()) {
QSqlQuery query(myQuery.str().c_str());
bool rset = query.exec();
if (rset) {
while(query.next()) ticketId = query.value(0).toInt();
}
}
else {
if(Debug::Inst()->getEnabled()) qDebug() << "ERROR: Unable to find DB connection\n";
}
return ticketId;
}
bool insertComment(int runNumber, int deviceId, const char* tagDescription, const char* comment, const char* author) {
std::stringstream myQuery;
myQuery << "BEGIN PkgTkAnalysisLog.insertTkAnalysisLog(" << runNumber << "," << deviceId << ",'" << tagDescription << "','" << comment << "','" << author << "'); END;";
if (DbConnection::Inst()->dbConnected()) {
QSqlQuery query(myQuery.str().c_str());
bool rset = query.exec();
return rset;
}
else {
if(Debug::Inst()->getEnabled()) qDebug() << "ERROR: Unable to find DB connection\n";
return false;
}
}
bool updateComment(int runNumber, int deviceId, const char* tagDescription, const char* comment, const char* author) {
std::stringstream myQuery;
myQuery << "BEGIN PkgTkAnalysisLog.updateTkAnalysisLog(" << runNumber << "," << deviceId << ",'" << tagDescription << "','" << comment << "','" << author << "'); END;";
if (DbConnection::Inst()->dbConnected()) {
QSqlQuery query(myQuery.str().c_str());
bool rset = query.exec();
return rset;
}
else {
if(Debug::Inst()->getEnabled()) qDebug() << "ERROR: Unable to find DB connection\n";
return false;
}
}
bool closeTicket(int deviceId, const char* tagDescription) {
std::stringstream myQuery;
myQuery << "BEGIN PkgTkAnalysisLog.closeTicket(" << deviceId << ",'" << tagDescription << "'); END;";
if (DbConnection::Inst()->dbConnected()) {
QSqlQuery query(myQuery.str().c_str());
bool rset = query.exec();
return rset;
}
else {
if(Debug::Inst()->getEnabled()) qDebug() << "ERROR: Unable to find DB connection\n";
return false;
}
}
bool deleteTicket(int runNumber, int deviceId, const char* tagDescription) {
std::stringstream myQuery;
myQuery << "BEGIN PkgTkAnalysisLog.deleteTkAnalysisLog(" << runNumber << "," << deviceId << ",'" << tagDescription << "'); END;";
if (DbConnection::Inst()->dbConnected()) {
QSqlQuery query(myQuery.str().c_str());
bool rset = query.exec();
return rset;
}
else {
if(Debug::Inst()->getEnabled()) qDebug() << "ERROR: Unable to find DB connection\n";
return false;
}
}
public Q_SLOTS:
void devChanged(QModelIndex current, QModelIndex) {
currentDevice = tagsModel->item(current.row(),0);
currentComment = tagsModel->item(current.row(),1);
}
void on_confirmCheck_toggled (bool) {
buttonOk->setEnabled( static_cast<bool>(confirmCheck->isChecked() ) );
}
void on_buttonCancel_clicked() {
done(0);
}
void on_btnAddComment_clicked() {
bool ok;
QString text = QInputDialog::getText(this, "DB Upload", "Enter Comment", QLineEdit::Normal, QString::null, &ok);
if (currentComment) currentComment->setText(text);
}
void on_buttonOk_clicked() {
bool ok;
QString text = QInputDialog::getText(this, "DB Upload", "Enter Your Name (author)", QLineEdit::Normal, QString::null, &ok);
if (ok && !text.isEmpty()) {
QProgressDialog progress("Upload to DB", "&Cancel", 0, tagsModel->rowCount(), this);
progress.show();
QString label;
int i = 0;
while (i < tagsModel->rowCount()) {
std::pair<iiQMultimap::iterator, iiQMultimap::iterator> itp = openTickets_.equal_range(static_cast<int>(tagsModel->item(i, 0)->text().toInt() ) );
int nrOpenTickets = 0;
bool hasOpenTicket = false;
bool hasCommentInRun = false;
iiQMultimap::iterator itTicket;
for (itTicket = itp.first; itTicket != itp.second; ++itTicket) {
if ( itTicket != openTickets_.end() && tagList_[((*itTicket).second.first)].c_str() == tagDescription_ ) {
hasOpenTicket = true;
++nrOpenTickets;
QStandardItem *child = (*itTicket).second.second;
QString help("Run Nr: ");
int pos = (child->text().toStdString().find(help.toStdString()))+ help.length();
int pos2 = child->text().toStdString().find("\n",pos);
QString runNr = child->text().mid(pos,(pos2-pos));
bool worked = false;
int irunNr = runNr.toInt(&worked);
if(worked && irunNr == runNumber_ ) hasCommentInRun = true;
}
}
progress.setLabelText(label.sprintf("Creating Ticket for device %d", +static_cast<int>(tagsModel->item(i, 0)->text().toInt() ) ) );
progress.setValue(i);
if (progress.wasCanceled()) break;
int ticketId = 0;
int localDeviceId = tagsModel->item(i, 0)->text().toInt();
if(closeTicket_ && hasOpenTicket) {
QString str = "CLOSED:" + tagsModel->item(i, 1)->text();
if(hasCommentInRun && nrOpenTickets == 1) deleteTicket(runNumber_, localDeviceId, tagDescription_.toStdString().c_str());
else if(hasCommentInRun) {
updateComment(runNumber_,localDeviceId, tagDescription_.toStdString().c_str(), str.toStdString().c_str() , text.toStdString().c_str());
closeTicket(localDeviceId, tagDescription_.toStdString().c_str() );
}
else {
insertComment(runNumber_,localDeviceId, tagDescription_.toStdString().c_str(), str.toStdString().c_str() , text.toStdString().c_str());
closeTicket( localDeviceId , tagDescription_.toStdString().c_str() );
}
}
else {
if(hasOpenTicket) {
if(hasCommentInRun) updateComment(runNumber_,localDeviceId, tagDescription_.toStdString().c_str(), qPrintable(tagsModel->item(i, 1)->text()), text.toStdString().c_str());
else insertComment(runNumber_,localDeviceId, tagDescription_.toStdString().c_str(), qPrintable(tagsModel->item(i, 1)->text()), text.toStdString().c_str());
}
else ticketId = openTicket(runNumber_,localDeviceId, tagDescription_.toStdString().c_str(), qPrintable(tagsModel->item(i, 1)->text()), text.toStdString().c_str() );
}
++i;
}
}
else return;
}
};
#endif