-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
595 lines (483 loc) · 19.5 KB
/
mainwindow.cpp
File metadata and controls
595 lines (483 loc) · 19.5 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/*SDOC:**********************************************************************
File: mainwindow.cpp
Action: Implementation of the MainWindow window.
Copyright © 2009, Ian Prest
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**********************************************************************:EDOC*/
#include "stdafx.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "addcomics.h"
#include <tchar.h>
struct QueryHelper
{
QSqlQuery query_;
QueryHelper(const QString& sql) { query_.prepare(sql); }
QueryHelper& arg(const QVariant& val) { query_.addBindValue(val); return *this; }
operator QVariant() { return exec(); }
QVariant exec() { if(!query_.exec() || !query_.next()) return QVariant(); return query_.value(0); }
};
/////////////////////////////////////////////////////////////////////////////
// MainWindow window class
/////////////////////////////////////////////////////////////////////////////
/*SDOC:**********************************************************************
Name: MainWindow::MainWindow
MainWindow::~MainWindow
Action: Constructor / destructor
**********************************************************************:EDOC*/
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWindow),
settings_("GCDCollector", "1.0"),
addComicsDialog(0),
connected_(false)
{
// Initialize UI
ui->setupUi(this);
// File menu
connect(ui->action_New, SIGNAL(triggered()), this, SLOT(newDatabase()));
connect(ui->action_Open, SIGNAL(triggered()), this, SLOT(openDatabase()));
connect(ui->action_Close, SIGNAL(triggered()), this, SLOT(closeDatabase()));
connect(ui->action_Exit, SIGNAL(triggered()), this, SLOT(close()));
// Edit menu
connect(ui->action_AddComics, SIGNAL(triggered()), this, SLOT(addComics()));
connect(this, SIGNAL(connected(bool)), ui->action_AddComics, SLOT(setEnabled(bool)));
connect(ui->action_AddCustomSeries, SIGNAL(triggered()), this, SLOT(addCustomSeries()));
connect(this, SIGNAL(connected(bool)), ui->action_AddCustomSeries, SLOT(setEnabled(bool)));
connect(ui->action_AddCustomIssue, SIGNAL(triggered()), this, SLOT(addCustomIssue()));
connect(this, SIGNAL(connected(bool)), ui->action_AddCustomIssue, SLOT(setEnabled(bool)));
connect(ui->action_DuplicateComic, SIGNAL(triggered()), ui->issueList, SLOT(duplicate()));
connect(this, SIGNAL(connected(bool)), ui->action_DuplicateComic, SLOT(setEnabled(bool)));
connect(ui->action_Cut, SIGNAL(triggered()), ui->issueList, SLOT(cut())); ui->action_Cut->setShortcuts(QKeySequence::Cut);
connect(this, SIGNAL(connected(bool)), ui->action_Cut, SLOT(setEnabled(bool)));
connect(ui->action_Copy, SIGNAL(triggered()), ui->issueList, SLOT(copy())); ui->action_Copy->setShortcuts(QKeySequence::Copy);
connect(this, SIGNAL(connected(bool)), ui->action_Copy, SLOT(setEnabled(bool)));
connect(ui->action_Paste, SIGNAL(triggered()), ui->issueList, SLOT(paste())); ui->action_Paste->setShortcuts(QKeySequence::Paste);
connect(this, SIGNAL(connected(bool)), ui->action_Paste, SLOT(setEnabled(bool)));
connect(ui->action_Delete, SIGNAL(triggered()), ui->issueList, SLOT(del())); ui->action_Delete->setShortcuts(QKeySequence::Delete);
connect(this, SIGNAL(connected(bool)), ui->action_Delete, SLOT(setEnabled(bool)));
connect(this, SIGNAL(connected(bool)), ui->action_Undo, SLOT(setEnabled(bool)));
connect(this, SIGNAL(connected(bool)), ui->action_Redo, SLOT(setEnabled(bool)));
// View menu
connect(ui->action_ShowOwnedIssues, SIGNAL(toggled(bool)), ui->issueList, SLOT(setShowOwned(bool)));
connect(ui->action_ShowWantedIssues, SIGNAL(toggled(bool)), ui->issueList, SLOT(setShowWanted(bool)));
connect(ui->action_ShowSoldIssues, SIGNAL(toggled(bool)), ui->issueList, SLOT(setShowSold(bool)));
connect(ui->action_ShowUntrackedIssues, SIGNAL(toggled(bool)), ui->issueList, SLOT(setShowUntracked(bool)));
// Help Menu
connect(ui->action_About, SIGNAL(triggered()), this, SLOT(about()));
connect(ui->action_AboutQt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
// Other
connect(ui->issueList, SIGNAL(selectionChanged(int)), this, SLOT(setCoverId(int)));
connect(ui->issueList, SIGNAL(selectionChanged(int)), ui->coverView, SLOT(setImageId(int)));
connect(ui->coverView, SIGNAL(downloadProgress(int,int)), ui->coverProgress, SLOT(setRange(int,int)));
connect(ui->coverView, SIGNAL(downloadInProgress(bool)), ui->coverProgress, SLOT(setVisible(bool)));
ui->coverProgress->setVisible(false);
// Set the series-list to only show series we own
ui->comicTitles->setOnlyShowOwned(true);
// Handle command-line arguments
for(int i = 1; i < QCoreApplication::arguments().size(); ++i)
{
QFileInfo fileInfo(QCoreApplication::arguments().at(i));
if(fileInfo.exists() && fileInfo.completeSuffix() == "comicdb")
{
connectDatabase(fileInfo.absoluteFilePath());
}
}
// Restore UI state
readSettings();
// Make sure UI state is consistent
connected(connected_);
// The window icon (used by the about box)
QIcon icon(":/GCDCollector/Resources/short-box.png");
setWindowIcon(icon);
}
MainWindow::~MainWindow()
{
delete ui;
delete addComicsDialog;
}
/*SDOC:**********************************************************************
Name: MainWindow::executeSql
Action: Prepare & execute a simple SQL statement
**********************************************************************:EDOC*/
bool MainWindow::executeSql(QString sql, QSqlDatabase& db)
{
QSqlQuery query(db);
query.prepare(sql);
if( !query.exec() )
{
QMessageBox::critical(0, tr("Database Error"), query.lastError().text());
return false;
}
return true;
}
/*SDOC:**********************************************************************
Name: MainWindow::createDatabase
Action: Creates the necessary tables in the user-database.
**********************************************************************:EDOC*/
bool MainWindow::createDatabase(const QString& filename)
{
// Create the database
{
QSqlDatabase newDb = QSqlDatabase::addDatabase("QSQLITE", "createDatabase");
newDb.setDatabaseName(filename);
if (!newDb.open())
{
QMessageBox::critical(0, QObject::tr("Database Error"), newDb.lastError().text());
return false;
}
if(!executeSql("PRAGMA encoding = \"UTF-8\";", newDb))
return false;
// comics
if(!executeSql("CREATE TABLE comics (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, issue_id INTEGER NOT NULL, condition VARCHAR(32), store VARCHAR(32), price DOUBLE, notes VARCHAR, owned TINYINT(1) NOT NULL DEFAULT ('false'), sold_price DOUBLE, user_id VARCHAR(32));", newDb))
return false;
if(!executeSql("CREATE INDEX comics_issueid on comics (issue_id ASC)", newDb))
return false;
// images
if(!executeSql("CREATE TABLE images (id INTEGER PRIMARY KEY NOT NULL, data BLOB);", newDb))
return false;
// custom_issues / custom_series
if(!executeSql("CREATE TABLE custom_issues (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, number INTEGER, series_id INTEGER, sort_code INTEGER, publication_date VARCHAR);", newDb))
return false;
if(!executeSql("CREATE TABLE custom_series (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR);", newDb))
return false;
}
QSqlDatabase::removeDatabase("createDatabase");
return connectDatabase(filename);
}
/*SDOC:**********************************************************************
Name: MainWindow::connectDatabase
Action: Attach to a user-database.
**********************************************************************:EDOC*/
bool MainWindow::connectDatabase(const QString& filename)
{
// We ATTACH the user-database to the master database (instead of just
// opening it normally) so that we can do JOINs between the two.
QSqlQuery attach;
attach.prepare(QString("ATTACH DATABASE '%1' AS document;").arg(filename));
if( !attach.exec() )
{
QMessageBox::critical(0, tr("Database Error"), attach.lastError().text());
return false;
}
setWindowTitle(tr("Comic Collector - %1").arg(filename));
// refresh the main window's series list
refresh();
connected(connected_ = true);
databaseFile_ = filename;
return true;
}
/*SDOC:**********************************************************************
Name: MainWindow::closeDatabase
Action: Detach from a user-database.
**********************************************************************:EDOC*/
void MainWindow::closeDatabase()
{
if(!connected_)
return;
QSqlQuery detach;
detach.prepare("DETACH DATABASE document;");
if( !detach.exec() )
{
QMessageBox::critical(0, tr("Database Error"), detach.lastError().text());
return;
}
setWindowTitle(tr("Comic Collector"));
connected(connected_ = false);
databaseFile_.clear();
// Clear the filter text and refresh the UI
ui->filterEdit->setText("");
// Clear the cover view
ui->coverLink->setText("");
ui->coverView->setImageId(0);
ui->coverProgress->setVisible(false);
// Clear the series detail view
ui->groupBox->setSeries(0);
// Refresh the rest of the UI
refresh();
}
/*SDOC:**********************************************************************
Name: MainWindow::newDatabase
Action: Handles the File/New command
**********************************************************************:EDOC*/
bool MainWindow::newDatabase()
{
// Get a new filename
QString filename = QFileDialog::getSaveFileName(this, tr("New Database..."), ".", tr("Database files (*.comicdb);;All files (*.*)"));
if(!filename.isEmpty())
{
// Close current database if one is open
closeDatabase();
// Delete any existing file
if(QFile::exists(filename) && !QFile::remove(filename))
{
QMessageBox::critical(this, tr("Database Error"), tr("Could not overwrite existing file."));
return false;
}
// Create a new database
if(!createDatabase(filename))
{
closeDatabase();
return false;
}
return true;
}
return false;
}
/*SDOC:**********************************************************************
Name: MainWindow::openDatabase
Action: Handles the File/Open command
**********************************************************************:EDOC*/
bool MainWindow::openDatabase()
{
// Get a new filename
QString filename = QFileDialog::getOpenFileName(this, tr("Open Database..."), ".", tr("Database files (*.comicdb);;All files (*.*)"));
if(!filename.isEmpty())
{
// Close current database if one is open
closeDatabase();
// Open the new database
return connectDatabase(filename);
}
return false;
}
/*SDOC:**********************************************************************
Name: MainWindow::about
Action: Handles the Help/About command
**********************************************************************:EDOC*/
void MainWindow::about()
{
QMessageBox::about(this, tr("About Comic Collector"),
tr( "<h2>Comic Collector</h2>"
"<p>Copyright © 2009, Ian Prest<br/>All rights reserved.</p>"
"<p>This software is licensed under the terms of a modified BSD license; "
"please see <a href='http://ijprest.github.com/GCDCollector/license.html'>"
"http://ijprest.github.com/GCDCollector/license.html</a> for details.</p>"
"<p>All comic data is copyright © 1994-2009, GCD and GCD contributors; "
"please see <a href='http://www.comics.org'>http://www.comics.org</a> for details.</p>"
));
}
/*SDOC:**********************************************************************
Name: MainWindow::closeEvent
Action: Called when the user closes the window; will save user
preferences.
**********************************************************************:EDOC*/
void MainWindow::closeEvent(QCloseEvent* event)
{
writeSettings();
event->accept();
}
/*SDOC:**********************************************************************
Name: MainWindow::writeSettings
MainWindow::readSettings
Action: Save/restore user preferences.
**********************************************************************:EDOC*/
void MainWindow::writeSettings()
{
// Save window position
settings().setValue("geometry", saveGeometry());
// Save docker positions
settings().setValue("dockerState", saveState());
// Sub-dialogs
if(addComicsDialog) addComicsDialog->writeSettings(settings());
// Save show/hide flags
settings().setValue("showOwned", ui->action_ShowOwnedIssues->isChecked());
settings().setValue("showWanted", ui->action_ShowWantedIssues->isChecked());
settings().setValue("showSold", ui->action_ShowSoldIssues->isChecked());
settings().setValue("showUntracked", ui->action_ShowUntrackedIssues->isChecked());
// Save last opened file
settings().setValue("lastDatabase", databaseFile_);
}
void MainWindow::readSettings()
{
// Restore window position
restoreGeometry(settings().value("geometry").toByteArray());
// Restore docker positions
restoreState(settings().value("dockerState").toByteArray());
// Restore show/hide flags
ui->action_ShowOwnedIssues->setChecked(settings().value("showOwned", true).toBool());
ui->action_ShowWantedIssues->setChecked(settings().value("showWanted", true).toBool());
ui->action_ShowSoldIssues->setChecked(settings().value("showSold", true).toBool());
ui->action_ShowUntrackedIssues->setChecked(settings().value("showUntracked", false).toBool());
// Restore last opened file
if(!connected_)
{
databaseFile_ = settings().value("lastDatabase",QString()).toString();
if(!databaseFile_.isEmpty())
connectDatabase(databaseFile_);
}
}
/*SDOC:**********************************************************************
Name: MainWindow::addComics
Action: Handles the Edit/Add Comics command
**********************************************************************:EDOC*/
void MainWindow::addComics()
{
// The "add comics" dialog is modeless; if it hasn't been created
// yet, do so.
if(!addComicsDialog)
{
addComicsDialog = new AddComics(this);
addComicsDialog->readSettings(settings());
connect(addComicsDialog, SIGNAL(addItems(QList<int>)), this, SLOT(addItems(QList<int>)));
}
// Show the dialog
addComicsDialog->show();
addComicsDialog->activateWindow();
}
/*SDOC:**********************************************************************
Name: MainWindow::addItems
Action: SLOT called by the "add items" dialog when the user wants to
add comics to his collection.
**********************************************************************:EDOC*/
void MainWindow::addItems(const QList<int>& items)
{
QSqlDatabase db = QSqlDatabase::database();
if(db.isValid() && db.transaction())
{
for(QList<int>::const_iterator i = items.begin(); i != items.end(); ++i)
{
if(!executeSql(QString("INSERT INTO document.comics(issue_id) VALUES (%1);").arg(*i)))
{
db.rollback();
return;
}
}
db.commit();
// refresh the main window's series list
refresh();
}
}
void MainWindow::setCoverId(int id)
{
QSqlQuery query;
query.prepare("SELECT issues.series_id, issues.sort_code, covers.has_image, covers.has_small, "
"covers.has_medium, covers.has_large FROM issues INNER JOIN covers ON issues.id=covers.issue_id "
"WHERE issues.id=?");
query.addBindValue(id);
query.exec();
if(query.next()) // only expecting one row
{
int zoom = 0;
if(query.value(5).toBool()) // large image
zoom = 4;
else if(query.value(4).toBool()) // medium image
zoom = 2;
else if(query.value(3).toBool()) // small image
zoom = 1;
if(zoom > 0)
{
ui->coverLink->setText(QString("<a href='http://www.comics.org/coverview.lasso?id=%1&zoom=%2'>%3</a>").arg(id).arg(zoom).arg(tr("See Cover Online")));
return;
}
}
// cover wasn't available
ui->coverLink->setText(tr("No online cover available"));
}
/*SDOC:**********************************************************************
Name: MainWindow::addCustomSeries (SLOT)
MainWindow::addCustomIssue (SLOT)
Action: Prompts the user for a name and inserts a custom series/issue
**********************************************************************:EDOC*/
void MainWindow::addCustomSeries()
{
bool ok = false;
QString name = QInputDialog::getText(this, tr("Add Custom Series"), tr("Enter name for custom series:"), QLineEdit::Normal, QString(), &ok);
if(ok && !name.isEmpty())
{
QSqlQuery query;
query.prepare("INSERT INTO document.custom_series(name) VALUES (?);");
query.addBindValue(name);
if(!query.exec())
{
QMessageBox::critical(this, tr("Database Error"), query.lastError().text());
return;
}
// Re-filter the series list to (hopefully) show the new series.
ui->comicTitles->filterList(ui->filterEdit->text());
}
}
void MainWindow::addCustomIssue()
{
int seriesId = ui->comicTitles->selectedSeries();
if(seriesId != 0)
{
// Try the lookup in the main issue list & custom issue list
int currentMaxValue = std::max(
QueryHelper("SELECT MAX(CAST(number AS INTEGER)) FROM issues WHERE series_id=?").arg(seriesId).exec().toInt(),
QueryHelper("SELECT MAX(CAST(number AS INTEGER)) FROM document.custom_issues WHERE series_id=?").arg(seriesId).exec().toInt());
bool ok = false;
QString number = QInputDialog::getText(this, tr("Add Custom Issue"), tr("Enter number for custom issue:"), QLineEdit::Normal, QString("%1").arg(currentMaxValue + 1), &ok);
if(ok && !number.isEmpty())
{
QRegExp re("(.*:?)(\\d+)-(\\d+)");
if(re.exactMatch(number))
{
// Adding a range of numbers
QSqlDatabase db = QSqlDatabase::database();
if(db.isValid() && db.transaction())
{
QString volume = re.cap(1);
int start = re.cap(2).toInt();
int end = re.cap(3).toInt();
for(int i = start; i <= end; ++i)
{
number = QString("%1%2").arg(volume).arg(i);
addSingleCustomIssue(seriesId, number);
}
}
db.commit();
}
else
{
// Adding a single issue
addSingleCustomIssue(seriesId, number);
}
// Refresh view
ui->issueList->refresh();
}
}
}
void MainWindow::addSingleCustomIssue(int seriesId, QString number)
{
QSqlQuery query;
query.prepare("INSERT INTO document.custom_issues(series_id,number) VALUES (?,?);");
query.addBindValue(seriesId);
query.addBindValue(number);
if(!query.exec())
{
QMessageBox::critical(this, tr("Database Error"), query.lastError().text());
return;
}
}
/*SDOC:**********************************************************************
Name: MainWindow::refresh
Action: Helper function to refresh the main UI
**********************************************************************:EDOC*/
void MainWindow::refresh()
{
// Refresh the filter-list
ui->comicTitles->filterList(ui->filterEdit->text());
// Refresh the issue-list
ui->issueList->refresh();
}
/* end of file */