-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
324 lines (274 loc) · 10.6 KB
/
mainwindow.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtDebug>
#include <QDateTime>
#include <QMessageBox>
#include <QSqlError>
#include <QTableView>
#include <QCalendar>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete model;
delete query;
delete reg;
delete ui;
}
void MainWindow::AddNewItemToTable()
{
int rows = model->rowCount();
QSqlRecord record = model->record(rows);
record.setValue("Item","");
record.setValue("SellPrice",double(0));
record.setValue("Timestamp",QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss" ));
model->insertRecord(rows-1,record);
if(ui->addToDate->isChecked() && ui->calendarWidget->selectedDate() != QDate::currentDate()){
int id = -1;
query->clear();
bool x = query->exec("SELECT MAX(id) FROM Sellings");
while (query->next()) {
id = query->value(0).toInt();
}
query->finish();
if (id > 0 && x){
query->clear();
QString timestamp = QString("%1 %2").arg(ui->calendarWidget->selectedDate().toString("yyyy-MM-dd"),QTime::currentTime().toString("hh:mm:ss"));
QString qText = QString("UPDATE Sellings SET Timestamp='%1' WHERE id='%2';").arg(timestamp,QString::number(id));
query->exec(qText);
query->finish();
}else{
QMessageBox::information(this,tr("Error"),tr("Error 7"));
}
}
model->select();
ui->tableView->setFocus(Qt::FocusReason::OtherFocusReason);
ui->tableView->scrollToBottom();
ui->tableView->setCurrentIndex(model->index(rows, 1));
ui->new_button->setDisabled(!model->isDirty());
}
void MainWindow::DelNewItemToTable()
{
QMessageBox messageBox(QMessageBox::Question,
tr("Caution!"),
tr("Are you sure that you want to delete this record!"),
QMessageBox::Yes | QMessageBox::No,
this);
messageBox.setButtonText(QMessageBox::Yes, tr("Yes"));
messageBox.setButtonText(QMessageBox::No, tr("No"));
if(messageBox.exec() == QMessageBox::Yes){
int row = ui->tableView->currentIndex().row();
model->removeRow(row);
model->select();
ui->del_button->setEnabled(false);
}
}
void MainWindow::Refresh()
{
model->select();
}
void MainWindow::Filter()
{
model->setFilter(getFilterString());
model->select();
calculate();
ui->new_button->setEnabled(
ui->addToDate->isChecked()
|| ui->calendarWidget->selectedDate() == QDate::currentDate());
}
void MainWindow::ToggleDelButton(const QModelIndex &index)
{
ui->del_button->setEnabled(index.isValid());
}
void MainWindow::TranslateApp(int index)
{
const QString baseName = "SamarSimple_" + ilocales[index].Locale;
if (translator.load(":/i18n/" + baseName)) {
qApp->installTranslator(&translator);
ui->retranslateUi(this);
tweakTableView();
if(ilocales[index].Lang != "عربي"){
setLayoutDirection(Qt::LayoutDirection::LeftToRight);
ui->calendarWidget->setLocale(QLocale(QLocale::English,QLocale::UnitedStates));
}else{
setLayoutDirection(Qt::LayoutDirection::RightToLeft);
ui->calendarWidget->setLocale(QLocale(QLocale::Arabic,QLocale::Egypt));
}
}
calculate();
}
void MainWindow::resizeEvent(QResizeEvent *event)
{
resizeTableColumn();
QMainWindow::resizeEvent(event);
}
void MainWindow::showEvent(QShowEvent *event)
{
QMainWindow::showEvent(event);
readRegistery();
if(isWindowMaximized)
setWindowState(Qt::WindowMaximized);
else
resize(initialWindowWidth,initialWindowHeight);
ui->new_button->setFocus(Qt::FocusReason::OtherFocusReason);
ui->calendarWidget->setSelectedDate(QDate::currentDate());
qApp->processEvents();
setupDatabase();
connectSignals();
setlocales();
setupModel();
Filter();
resizeTableColumn();
calculate();
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if(model->isDirty())
model->submitAll();
if(!this->isMaximized()){
reg->setValue(WINDOW_WIDTH,this->width());
reg->setValue(WINDOW_HEIGHT,this->height());
}
reg->setValue(SEARCH_MONTH,ui->searchByMonth->isChecked());
reg->setValue(CURRENT_LOCALE,ui->setLang->currentIndex());
reg->setValue(ISWINDOWMAXIMIZED,this->isMaximized());
QMainWindow::closeEvent(event);
}
QString MainWindow::getFilterString() const
{
QString filterName = ui->filter->text().simplified();
int year = ui->calendarWidget->selectedDate().year();
int month = ui->calendarWidget->selectedDate().month();
int day = ui->calendarWidget->selectedDate().day();
QString nameFilterString = filterName.isEmpty()? "":QString("Name Like '%%1%' AND ").arg(filterName);
if(ui->searchByMonth->isChecked()){
return QString("%1 Timestamp BETWEEN '%2-%4-01 00:00:00' AND '%3-%5-01 00:00:00'")
.arg(nameFilterString,
QString::number(year),
QString::number(month < 12 ? year:year+1),
QString("%1").arg(ui->calendarWidget->selectedDate().month(),2,10,QChar('0')),
QString("%1").arg(ui->calendarWidget->selectedDate().addMonths(1).month(),2,10,QChar('0')));
}else{
return QString("%1 Timestamp BETWEEN '%2-%3-%4 00:00:00' AND '%2-%3-%4 23:59:59'")
.arg(nameFilterString,
QString::number(year),
QString("%1").arg(month,2,10,QChar('0')),
QString("%1").arg(day,2,10,QChar('0')));
}
}
void MainWindow::connectSignals()
{
connect(ui->new_button,&QPushButton::clicked,this,&::MainWindow::AddNewItemToTable);
connect(ui->del_button,&QPushButton::clicked,this,&::MainWindow::DelNewItemToTable);
connect(ui->refresh_button,&QPushButton::clicked,this,&::MainWindow::Refresh);
connect(ui->filter,&QLineEdit::textChanged,this,&::MainWindow::Filter);
connect(ui->searchByMonth,&Switch::clicked,this,&::MainWindow::Filter);
connect(ui->addToDate,&Switch::clicked,this,&::MainWindow::Filter);
connect(model,&QSqlTableModel::dataChanged,this,&::MainWindow::calculate);
connect(model,&QSqlTableModel::primeInsert,this,&::MainWindow::calculate);
connect(model,&QSqlTableModel::dataChanged,this,&::MainWindow::resizeTableColumn);
connect(model,&QSqlTableModel::primeInsert,this,&::MainWindow::resizeTableColumn);
connect(ui->resetDate_button,&QPushButton::clicked,this,[=](){
ui->calendarWidget->setSelectedDate(QDate::currentDate());
});
connect(ui->tableView,&QTableView::pressed,this,&::MainWindow::ToggleDelButton);
connect(ui->setLang,static_cast<void(QComboBox::*)(int)> (&QComboBox::currentIndexChanged),this,&::MainWindow::TranslateApp);
connect(ui->info_button,&QToolButton::clicked,this,[=](){
QMessageBox::information(this,tr("About"),tr("This app is being developed by Dr. Amr Osman"));
});
connect(ui->tableView->itemDelegate(),&QAbstractItemDelegate::closeEditor,this,[=](){
ui->new_button->setEnabled(!model->isDirty() || ui->tableView->selectionModel()->currentIndex().column()==3);
model->submit();
ui->new_button->setFocus(Qt::FocusReason::OtherFocusReason);
});
connect(ui->calendarWidget,&QCalendarWidget::selectionChanged,this,&::MainWindow::Filter);
}
void MainWindow::setupDatabase()
{
if ( QSqlDatabase::contains(CONNECTIONNAME)){
db = QSqlDatabase::database(CONNECTIONNAME);
}else{
db = QSqlDatabase::addDatabase("QSQLITE",CONNECTIONNAME);
}
db.setHostName("127.0.0.1");
db.setDatabaseName(DATABASEFILE);
model = new CustomModel(this,db);
query = new QSqlQuery(db);
if(db.open()){
bool x = query->exec("CREATE TABLE IF NOT EXISTS Sellings("
"id INTEGER NOT NULL PRIMARY KEY,"
"Name TEXT,"
"SellPrice REAL,"
"Timestamp DATETIME DEFAULT (datetime('now','localtime')));");
if(!x){
qDebug() << query->executedQuery();
qDebug() << query->lastError();
}
query->finish();
}else{
QMessageBox::information(this,tr("Fatal Error"),tr("Failed to connect to database"));
QMessageBox messageBox(QMessageBox::Information,
tr("Fatal Error"),
tr("Failed to connect to database"),
QMessageBox::Ok , this);
messageBox.setButtonText(QMessageBox::Ok, tr("Ok"));
messageBox.exec();
exit(1);
}
}
void MainWindow::setupModel()
{
model->setTable("Sellings");
model->select();
tweakTableView();
}
void MainWindow::setlocales()
{
for(uint i=0;i< sizeof(ilocales) / sizeof(ilocales[0]) ;i++){
ui->setLang->insertItem(i,ilocales[i].Lang);
}
ui->setLang->setCurrentIndex(currentLocale);
}
void MainWindow::calculate()
{
double sum = 0;
int rows = model->rowCount();
for(int i = 0 ;i < rows; i++ ){
sum += model->index(i,2).data().toDouble();
}
ui->sum->setText(QString::number(sum));
}
void MainWindow::resizeTableColumn()
{
ui->tableView->setColumnWidth(1,ui->tableView->viewport()->width()*0.66);
ui->tableView->setColumnWidth(2,ui->tableView->viewport()->width()*0.10);
ui->tableView->setColumnWidth(3,ui->tableView->viewport()->width()*0.24);
}
void MainWindow::readRegistery()
{
reg = new QSettings(regPath, QSettings::NativeFormat);
initialWindowWidth = reg->value(WINDOW_WIDTH).toInt();
initialWindowHeight = reg->value(WINDOW_HEIGHT).toInt();
searchMonth = reg->value(SEARCH_MONTH).toBool();
currentLocale = reg->value(CURRENT_LOCALE).toInt();
isWindowMaximized = reg->value(ISWINDOWMAXIMIZED).toBool();
ui->searchByMonth->setChecked(searchMonth);
}
void MainWindow::tweakTableView()
{
model->setHeaderData(0, Qt::Orientation::Horizontal, tr("ID"));
model->setHeaderData(1, Qt::Orientation::Horizontal, tr("Item"));
model->setHeaderData(2, Qt::Orientation::Horizontal, tr("Price"));
model->setHeaderData(3, Qt::Orientation::Horizontal, tr("Date Time"));
model->setEditStrategy(QSqlTableModel::OnRowChange);
ui->tableView->horizontalHeader()->setStretchLastSection(true);
ui->tableView->setSelectionBehavior(QTableView::SelectionBehavior::SelectRows);
ui->tableView->setAlternatingRowColors(true);
ui->tableView->setModel(model);
ui->tableView->selectRow(0);
ui->tableView->hideColumn(0);
}