This repository was archived by the owner on Jun 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
388 lines (343 loc) · 12.8 KB
/
mainwindow.cpp
File metadata and controls
388 lines (343 loc) · 12.8 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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
tray = new QSystemTrayIcon;
trayMenu = new QMenu;
trayMenu->addAction("Hide");
trayMenu->addAction("Show");
trayMenu->addAction("Quit");
tray->setContextMenu(trayMenu);
tray->setIcon(QIcon(":/images/icon.png"));
tray->show();
ui->editTask_button->setDisabled(true);
ui->removeTask_button->setDisabled(true);
connect(ui->tableView, SIGNAL(clicked(QModelIndex)), this, SLOT(enableRemoveTask_button()));
connect(ui->tableView, SIGNAL(clicked(QModelIndex)), this, SLOT(enableEditTask_button()));
connect(trayMenu, SIGNAL(triggered(QAction*)), this, SLOT(trayMenu_triggered(QAction*)));
restoreGUI();
init_db();
}
void MainWindow::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::closeEvent(QCloseEvent *event)
{
qDebug() << "closeEvent(): triggered()";
db.exec("VACUUM");
db.close();
settings->setValue("mainWindowGeometry", saveGeometry());
settings->setValue("mainWindowState", saveState());
settings->sync();
tray->hide();
event->accept();
}
void MainWindow::restoreGUI()
{
bool portable = false;
if(QFile("portable.dat").exists())
{
qDebug() << "portable.dat detected!";
portable = true;
DataLoc << "portable";
}
else
{
DataLoc = QStandardPaths::standardLocations(QStandardPaths::DataLocation);
qDebug() << "Application Data Folder: " << QStandardPaths::standardLocations(QStandardPaths::DataLocation);
}
bool ok = dir.mkpath(DataLoc.at(0));
if(!ok)
{
QMessageBox::critical(this, "Warning", "Could Not Create Storage Path");
this->close();
}
settings = new QSettings(DataLoc.at(0) + "/settings.ini", QSettings::IniFormat);
if(portable)
{
settings->setValue("portableMode", true);
settings->sync();
}
restoreGeometry(settings->value("mainWindowGeometry").toByteArray());
restoreState(settings->value("mainWindowGeometry").toByteArray());
}
// private slots
void MainWindow::on_actionAbout_triggered()
{
QMessageBox::information(this, "About", "To Do List Manager \nCreated By Computerfr33k \nVersion: " + QApplication::applicationVersion());
}
void MainWindow::on_actionReport_A_Bug_triggered()
{
QDesktopServices::openUrl(QUrl("https://github.com/computerfr33k/To-Do-List-Manager/issues"));
}
void MainWindow::on_actionQuit_triggered()
{
this->close();
}
void MainWindow::on_actionAbout_Qt_2_triggered()
{
QMessageBox::aboutQt(this, "About Qt");
}
void MainWindow::on_actionPreferences_triggered()
{
Preferences *Dialog = new Preferences;
connect(Dialog, SIGNAL(finished(int)), Dialog, SLOT(deleteLater()));
search_model.setQuery("SELECT id,row,type FROM sort_preferences", db);
/*
qDebug() << search_model.record(0).value(1).toInt();
qDebug() << search_model.record(0).value(2).toInt();
*/
Dialog->set_sortRow(settings->value("Sorting/row", 0).toInt());
Dialog->set_sortType(settings->value("Sorting/type", 0).toInt());
Dialog->set_autoUpdate(settings->value("AutoUpdate/enabled", true).toBool());
Dialog->set_portableMode(settings->value("portableMode", false).toBool());
Dialog->set_dataLoc(QStandardPaths::standardLocations(QStandardPaths::DataLocation).at(0));
if(Dialog->exec())
{
qDebug() << "sortRow(): " << Dialog->get_sortRow();
qDebug() << "sortType(): " << Dialog->get_sortType();
qDebug() << "autoUpdate(): " << Dialog->get_autoUpdate();
settings->setValue("Sorting/row", Dialog->get_sortRow());
settings->setValue("Sorting/type", Dialog->get_sortType());
settings->setValue("AutoUpdate/enabled", Dialog->get_autoUpdate());
settings->sync();
QSqlQuery query("insert or replace into sort_preferences (id, row, type) VALUES ('1', :row, :type)");
query.bindValue(0, Dialog->get_sortRow());
query.bindValue(1, Dialog->get_sortType());
if(query.exec())
updateTable();
if(Dialog->get_portableMode())
{
QFile file("portable.dat");
file.open(QIODevice::WriteOnly);
file.write("");
file.close();
}
else
{
QFile::remove("portable.dat");
}
}
}
void MainWindow::on_actionWebsite_triggered()
{
QDesktopServices::openUrl(QUrl("http://to-do-list-manager.computerfr33k.com"));
}
void MainWindow::on_actionCheck_For_Update_triggered()
{
QMessageBox::information(this, "Checking for updates...", "Coming Soon");
}
void MainWindow::on_actionDonate_triggered()
{
QDesktopServices::openUrl(QUrl("http://to-do-list-manager.computerfr33k.com/index.php?title=Donate"));
}
void MainWindow::on_actionLicense_triggered()
{
LicenseDialog *licenseDialog = new LicenseDialog;
licenseDialog->exec();
licenseDialog->deleteLater();
}
void MainWindow::trayMenu_triggered(QAction *a)
{
if(a->text().compare("Show") == 0)
this->show();
else if(a->text().compare("Hide") == 0)
this->hide();
else if(a->text().compare("Quit") == 0)
this->close();
}
void MainWindow::on_addTask_button_clicked()
{
insertItem *dialogBox = new insertItem;
connect(dialogBox, SIGNAL(finished(int)), dialogBox, SLOT(deleteLater()));
if( dialogBox->exec() )
{
//db stuff
QSqlQuery query ("insert into tasks (Task, DueDate, Completed, Priority, Notes) values (:name, :dueDate, :completed, :priority, :notes)");
query.bindValue(0, dialogBox->get_task());
query.bindValue(1, dialogBox->get_dueDate());
query.bindValue(2, dialogBox->get_isCompleted());
query.bindValue(3, dialogBox->get_priority());
query.bindValue(4, dialogBox->get_notes());
if(query.exec())
{
updateTable();
}
ui->editTask_button->setDisabled(true);
ui->removeTask_button->setDisabled(true);
}
}
void MainWindow::on_removeTask_button_clicked()
{
if(QMessageBox::No == QMessageBox::question(this, "Remove Task", "Are you sure you want to remove this task?"))
{
return;
}
QSqlQuery query;
query.prepare("delete from tasks where Task = \'" + ui->tableView->model()->data(ui->tableView->model()->index(ui->tableView->currentIndex().row(), 0)).toString().replace("'", "''") +"\'");
if(!query.exec())
{
QMessageBox::warning(0,"Error", "Could Not Delete The Selected Task: " + ui->tableView->model()->data(ui->tableView->model()->index(ui->tableView->currentIndex().row(), 0)).toString() + ".");
return;
}
updateTable();
//disable the edit button because we just deleted a task so we are no longer on a task making this button invalid currently
ui->editTask_button->setDisabled(true);
ui->removeTask_button->setDisabled(true);
}
void MainWindow::on_editTask_button_clicked()
{
editTask *dialog = new editTask;
connect(dialog, SIGNAL(finished(int)), dialog, SLOT(deleteLater()));
//set Task Name For Edit Task Dialog
dialog->set_taskName(ui->tableView->model()->data(ui->tableView->model()->index(ui->tableView->currentIndex().row(), 0)).toString());
//set Due Date
dialog->set_dueDate(QDateTime::fromString(ui->tableView->model()->data(ui->tableView->model()->index(ui->tableView->currentIndex().row(), 1)).toString(), "MM/dd/yyyy hh:mm:ss AP"));
//copy completed to variable for easier comparing
QString completed = ui->tableView->model()->data(ui->tableView->model()->index(ui->tableView->currentIndex().row(), 2)).toString();
//set completed to yes or no
if(completed.compare("No") == 0)
dialog->set_completed(0);
else
dialog->set_completed(1);
dialog->set_priority(ui->tableView->model()->data(ui->tableView->model()->index(ui->tableView->currentIndex().row(), 3)).toInt()); //set Priority
dialog->set_notes(ui->tableView->model()->data(ui->tableView->model()->index(ui->tableView->currentIndex().row(), 4)).toString()); //set Notes
if(dialog->exec())
{
QSqlQuery query("update tasks set Task=:name,DueDate=:dueDate,Completed=:completed,Priority=:priority,Notes=:notes WHERE Task=\'" + ui->tableView->model()->data(ui->tableView->model()->index(ui->tableView->currentIndex().row(), 0)).toString().replace("'", "''") + "\'");
query.bindValue(0, dialog->get_taskName());
query.bindValue(1, dialog->get_dueDate());
query.bindValue(2, dialog->get_completed());
query.bindValue(3, dialog->get_priority());
query.bindValue(4, dialog->get_notes());
if(!query.exec())
{
qDebug() << "*** ERROR: line 255; mainwindow.cpp";
qDebug() << "SQL Error! ***";
}
}
updateTable();
}
void MainWindow::on_tableView_doubleClicked(QModelIndex index)
{
on_editTask_button_clicked();
}
void MainWindow::on_actionRestore_Tasks_Settings_triggered()
{
QFileDialog fd;
QString dir = fd.getExistingDirectory(this, tr("Choose Directory To Restore From"), "", QFileDialog::ShowDirsOnly);
if(!dir.isEmpty())
{
QFile::remove(DataLoc.at(0) + "/settings.ini");
QFile::remove(DataLoc.at(0) + "/tasks.db");
QFile::copy(dir + "/settings.ini", DataLoc.at(0) + "/settings.ini");
QFile::copy(dir + "/tasks.db", DataLoc.at(0) + "/tasks.db");
QMessageBox::information(this, tr("Status"), tr("Restore Complete!"));
updateTable();
}
}
void MainWindow::on_actionExport_Tasks_Settings_triggered()
{
QFileDialog fileDialog;
fileDialog.setFileMode(QFileDialog::DirectoryOnly);
QString dir = fileDialog.getExistingDirectory(this, tr("Open Directory"), "", QFileDialog::ShowDirsOnly);
if(!dir.isEmpty())
{
QFile::remove(dir + "/settings.ini");
QFile::remove(dir + "/tasks.db");
if(!QFile::copy(DataLoc.at(0) + "/settings.ini", dir + "/settings.ini"))
{
QMessageBox::warning(this, tr("Warning"), tr("settings.ini could not be copied"));
QFile::remove(dir + "/settings.ini");
}
if(!QFile::copy(DataLoc.at(0) + "/tasks.db", dir + "/tasks.db"))
{
QMessageBox::warning(this, tr("Warning"), tr("tasks.db could not be copied"));
QFile::remove(dir + "/tasks.db");
}
QMessageBox::information(this, tr("Export"), tr("Export Complete!"));
}
}
void MainWindow::on_actionHide_triggered()
{
this->hide();
}
void MainWindow::loadTasks()
{
}
void MainWindow::saveTasks()
{
}
//button hooks
void MainWindow::enableRemoveTask_button()
{
if(ui->tableView->currentIndex().isValid())
ui->removeTask_button->setEnabled(true);
}
void MainWindow::enableEditTask_button()
{
if(ui->tableView->currentIndex().isValid())
ui->editTask_button->setEnabled(true);
}
//table
void MainWindow::init_db()
{
// create settings.ini file if it doesn't exist
if(!QFile(DataLoc.at(0) + "/settings.ini").exists())
{
settings->setValue("mainWindowGeometry", saveGeometry());
settings->setValue("mainWindowState", saveState());
settings->setValue("Sorting/row", 0);
settings->setValue("Sorting/type", 0);
settings->setValue("AutoUpdate/enabled", true);
settings->setValue("portableMode", false);
settings->sync();
}
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(DataLoc.at(0) + "/tasks.db");
db.open();
db.exec("create table if not exists tasks (Task, DueDate, Completed, Priority, Notes)");
db.exec("create table if not exists sort_preferences (id PRIMARY KEY, row, type)");
all_model = new QSqlTableModel(this, db);
ui->tableView->setModel(all_model);
updateTable();
}
void MainWindow::updateTable()
{
all_model->setTable("tasks");
all_model->select();
ui->tableView->setModel(all_model);
ui->tableView->resizeColumnsToContents();
search_model.setQuery("SELECT id,row,type FROM sort_preferences", db);
// ini settings test
if(settings->value("sorting/type", 0).toInt() == 0)
ui->tableView->sortByColumn(settings->value("sorting/row", 0).toInt(), Qt::AscendingOrder);
else
ui->tableView->sortByColumn(settings->value("sorting/row", 0).toInt(), Qt::DescendingOrder);
//
/*
*if(search_model.record(0).value(2).toInt() == 0)
ui->tableView->sortByColumn(search_model.record(0).value(1).toInt(), Qt::AscendingOrder);
else if(search_model.record(0).value(2).toInt() == 1)
ui->tableView->sortByColumn(search_model.record(0).value(1).toInt(), Qt::DescendingOrder);
*/
}
// end private slots
MainWindow::~MainWindow()
{
delete all_model;
delete ui;
delete settings;
delete tray;
}