-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerictablepage.cpp
More file actions
163 lines (125 loc) · 4.21 KB
/
generictablepage.cpp
File metadata and controls
163 lines (125 loc) · 4.21 KB
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
#include "generictablepage.h"
#include "ui_generictablepage.h"
#include "macros.hpp"
#include <QMessageBox>
#include <golem_string.h>
#include <limits.h>
#include <QDebug>
GenericTablePage::GenericTablePage(QWidget *parent) :
QWidget(parent),
ui(new Ui::GenericTablePage)
{
ui->setupUi(this);
pTable = ui->tableW;
}
GenericTablePage::~GenericTablePage()
{
delete ui;
}
void GenericTablePage::on_tableW_windowIconChanged(const QIcon &icon)
{
icon;
}
void GenericTablePage::on_tableW_itemChanged(QTableWidgetItem *item)
{
if (!Eval(item->text().toStdWString(),mDBTable.Columns()[item->column()].dataType)){
FindUndo(item);
}else{
Edit edtAdd;
edtAdd.pos = make_pair(item->row(),item->column());
edtAdd.txt = item->text();
Edits.push_back(edtAdd);
}
}
#define TRYCONV_ERR(call,errcaption) try{call;bret = true;}catch(const std::invalid_argument& ivex){QMessageBox(QMessageBox::Icon::Critical,"Input Error",QString(errcaption + QString(": ") + QString::fromLatin1(ivex.what(),(int)strlen(ivex.what()))),QMessageBox::Ok,this).exec();bret = false;break;}
bool GenericTablePage::Eval(const GString &vstr, const EDataType &type)
{
bool bret = false;
switch(type)
{
case EDataType::e_Decimal: {
try {
stoll(vstr);
bret = true;
}
catch (const std::invalid_argument& ivex) {
QMessageBox(QMessageBox::Icon::Critical,"Error",QString("Cannot convert to 64 bit integer." + QString(": ") + QString::fromLatin1(ivex.what(),(int)strlen(ivex.what()))),QMessageBox::Ok,this).exec();
// Stop the compiler whining about unused variables with this useless function call.
ivex.what();
}
break;
}
case EDataType::e_Double:{
TRYCONV_ERR(stod(vstr), "Cannot convert to double precision float");
break;
}
case EDataType::e_Float: {
TRYCONV_ERR(stof(vstr), "Cannot convert to floating point number.");
break;
}
case EDataType::e_Int: {
TRYCONV_ERR(stoi(vstr), "Cannot convert to number");
break;
}
case EDataType::e_Numeric:
{
TRYCONV_ERR(stod(vstr), "Cannot convert to numerical");
break;
}
case EDataType::e_SmallInt:
{
int num = 0;
TRYCONV_ERR(num = stoi(vstr), "Cannot convert to short int, invalid argument");
if (num > SHRT_MAX) {
bret = false;
QMessageBox(QMessageBox::Icon::Critical,"Error",QString("Invalid input, number must be below " + QString::number(SHRT_MAX)),QMessageBox::Ok,this).exec();
}
break;
}
case EDataType::e_Timestamp:
{
GDateTime TimeP;
bret = true;
if (!TimeP.Parse(vstr)) {
bret = false;
QMessageBox(QMessageBox::Icon::Critical,"Error",QString("Input is not a valid date"),QMessageBox::Ok,this).exec();
}
break;
}
default:
return true;
}
return bret;
}
void GenericTablePage::FindUndo(QTableWidgetItem *pItem)
{
pTable->blockSignals(true);
// 1 : Search in the undo list
// 2: Search in the table
// 3: Make it an empty one
// 1
vector<Edit>::iterator Eit = Edits.begin();
while (Eit != Edits.end())
{
Edit& edt = *Eit;
if (make_pair(pItem->row(),pItem->column()) == edt.pos){
pItem->setText(edt.txt);
pTable->blockSignals(false);
return;
}
++Eit;
}
if ((int)mDBTable.Columns()[pItem->column()].Items().size() > pItem->row()){
pItem->setText(QString::fromStdWString(mDBTable.Columns()[pItem->column()].Items()[pItem->row()].sData));
pTable->blockSignals(false);
return;
}
pItem->setText("");
pTable->blockSignals(false);
}
void GenericTablePage::on_tableW_currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *previous)
{
if (!previous || !current)
return;
// qDebug() << previous->text() << " -> " << current->text();
}