-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbview.cpp
469 lines (366 loc) · 14.7 KB
/
dbview.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
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
/*
*
* qenes genealogy software
*
* Copyright (C) 2009-2010 Ari Tähti
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License veasion 2 as
* published by the Free Software Foundation.
*/
#include "dbview.h"
#include <QDebug>
#include "mainwindow.h"
#include <QSettings>
#include "macro.h"
extern GeneData * geneData;
extern QSettings sets;
int DbView::getId()
{
int id;
if ( !this->uiTableView->selectionModel()->selection().isEmpty() ) {
QModelIndex qmi = proxyModel->mapToSource(this->uiTableView->selectionModel()->selection().indexes().at(0));
id = qmi.row();
} else id = 0;
return id;
}
void DbView::slotToBrowseHistory()
{
int id = getId();
if (id) { GENE.browseHistory.removeAll(id); GENE.browseHistory.prepend(id); }
}
void DbView::slotGoTo()
{
int id = getId();
this->done(id);
}
void DbView::slotSearch(QString search)
{
proxyModel->setFilterRegExp(QRegExp(QRegExp(search, Qt::CaseInsensitive, QRegExp::FixedString)));
this->slotUpdate();
}
void DbView::slotUiSearchFamilyName()
{
if (uiSearchFamilyName->isChecked()) searchFamilyName = true; else searchFamilyName = false;
this->slotUpdate();
}
void DbView::slotUiSearchFirstName()
{
if (uiSearchFirstName->isChecked()) searchFirstName = true; else searchFirstName = false;
this->slotUpdate();
}
void DbView::slotUpdate()
{
personModel->slotUpdate();
familyModel->slotUpdate();
}
void DbView::slotUiSearchIsCaseSensitive(bool isit)
{
if (isit) proxyModel->setFilterCaseSensitivity(Qt::CaseSensitive);
else proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
this->update();
}
DbView::DbView(Date dateLimits, bool male, bool female, QWidget *parent) : QDialog(parent)
{
searchFamilyName = true;
searchFirstName = true;
setup(dateLimits, male, female);
}
DbView::DbView(QWidget *parent) : QDialog(parent)
{
Date date;
date.setDate1(GET_ALLMINDAY);
date.setDate2(GET_ALLMAXDAY);
searchFamilyName = true;
searchFirstName = true;
setFont(GeneData::panelFont);
this->setup(date, true, true);
}
bool DbView::acceptRow(int sourceRow)
{
QString firstNameSearh = this->uiSearch->text();
QString familyNameSearh = this->uiSearchFamilyName->text();
bool ancestorsOnly = this->uiAncestorsOnly->isChecked();
bool descentantsOnly = this->uiDescentantsOnly->isChecked();
bool ancDecOnly = this->uiAncDec->isChecked();
if (INDI(sourceRow).deleted) return false;
if (ancestorsOnly || descentantsOnly || ancDecOnly) {
if (!INDI(sourceRow).selected) return false;
}
bool firstNameMatch = INDI(sourceRow).nameFirst.contains(firstNameSearh)
|| (INDI(sourceRow).name2nd.contains(firstNameSearh))
|| (INDI(sourceRow).name3rd.contains(firstNameSearh));
bool familyNameMatch = INDI(sourceRow).nameFamily.contains(firstNameSearh);
if (firstNameSearh == "" && searchFirstName) firstNameMatch = true;
if (familyNameSearh == "" && searchFamilyName) familyNameMatch = true;
if (!uiFemale->isChecked() && INDI(sourceRow).sex == FEMALE) return false;
if (!uiMale->isChecked() && INDI(sourceRow).sex == MALE) return false;
if ( uiTwins->isChecked() && !(GENE.twinAlfa.contains(sourceRow) || GENE.twinBeta.contains(sourceRow))) return false;
if ( !( ( INDI(sourceRow).estimatedBirth.day1 >= uiDateMin->date() && INDI(sourceRow).estimatedBirth.day1 <= uiDateMax->date() )
|| ( INDI(sourceRow).estimatedBirth.day2 >= uiDateMin->date() && INDI(sourceRow).estimatedBirth.day2 <= uiDateMax->date() ) ) )
return false;
if ( ( searchFamilyName) && ( searchFirstName ) ) return (familyNameMatch || firstNameMatch);
if ( ( searchFamilyName) && (!searchFirstName ) ) return familyNameMatch;
if ( ( searchFirstName ) && (!searchFamilyName) ) return firstNameMatch;
if ( (!searchFamilyName) && (!searchFirstName ) ) return false;
return false;
}
void DbView::slotClose()
{
INDI(GENE.currentId).selectNone();
close();
}
void DbView::updateBegin()
{
this->familyModel->updateBegin();
this->personModel->updateBegin();
}
void DbView::slotSelection()
{
INDI(GENE.currentId).selectNone();
if (uiAncDec->isChecked()) INDI(GENE.currentId).selectAncestorsAll();
if (uiDescentantsOnly->isChecked()) INDI(GENE.currentId).selectDescendants();
if (uiAncestorsOnly->isChecked()) INDI(GENE.currentId).selectAncestors();
}
void DbView::setup(Date dateLimits, bool male, bool female)
{
setupUi(this);
uiPersonCount->setText(QString::number(GENE.indiCount));
uiPersonLastID->setText(QString::number(GENE.indiLastUsed));
uiFamilyCount->setText(QString::number(GENE.famiCount));
uiFamilyLastID->setText(QString::number(GENE.famiLastUsed));
if (male) uiMale->setChecked(true); else uiMale->setChecked(false);
if (female) uiFemale->setChecked(true); else uiFemale->setChecked(false);
connect(uiExit, SIGNAL(clicked()), this, SLOT(slotClose()));
// create a person view model and install it
personModel = new PersonTableModel(this);
proxyModel = new MyProxyModel(personModel, this);
proxyModel->setSourceModel(personModel);
uiTableView->setModel(proxyModel);
uiTableView->setColumnWidth(0, 80); // familyname
uiTableView->setColumnWidth(1, 100); // first name
uiTableView->setColumnWidth(2, 200); // birthday
for (quint16 i=1 ; i<=GENE.indiLastUsed+1 ; i++) idList.append(i);
proxyModel->setFilterKeyColumn(0);
proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
// Create a family view model and install it
familyModel = new FamilyTableModel();
familyProxyModel = new FamilyProxyModel(familyModel, this);
familyProxyModel->setSourceModel(familyModel);
uiFamilyView->setModel(familyProxyModel);
uiFamilyView->setColumnWidth(0, 100); // husb
uiFamilyView->setColumnWidth(1, 100); // wife
uiFamilyView->setColumnWidth(2, 70); // marr day
// connect signals & slots
connect(uiSearch, SIGNAL(textChanged(QString)), this, SLOT(updateBegin()));
connect(uiSearch, SIGNAL(textChanged(QString)), this, SLOT(slotSearch(QString)));
connect(uiSearchIsCaseSensitive, SIGNAL(toggled(bool)), this, SLOT(updateBegin()));
connect(uiSearchIsCaseSensitive, SIGNAL(toggled(bool)), this, SLOT(slotUiSearchIsCaseSensitive(bool)));
connect(uiSearchFamilyName, SIGNAL(toggled(bool)), this, SLOT(updateBegin()));
connect(uiSearchFamilyName, SIGNAL(toggled(bool)), this, SLOT(slotUiSearchFamilyName()));
connect(uiSearchFirstName, SIGNAL(toggled(bool)), this, SLOT(updateBegin()));
connect(uiSearchFirstName, SIGNAL(toggled(bool)), this, SLOT(slotUiSearchFirstName()));
connect(uiGoTo, SIGNAL(clicked()), this, SLOT(slotGoTo()));
connect(uiMale, SIGNAL(clicked()), this, SLOT(updateBegin()));
connect(uiMale, SIGNAL(clicked()), this, SLOT(slotUpdate()));
connect(uiFemale, SIGNAL(clicked()), this, SLOT(updateBegin()));
connect(uiFemale, SIGNAL(clicked()), this, SLOT(slotUpdate()));
connect(uiTwins, SIGNAL(clicked()), this, SLOT(updateBegin()));
connect(uiTwins, SIGNAL(clicked()), this, SLOT(slotUpdate()));
connect(uiDateMin, SIGNAL(dateChanged(QDate)), this, SLOT(updateBegin()));
connect(uiDateMin, SIGNAL(dateChanged(QDate)), this, SLOT(slotUpdate()));
connect(uiDateMax, SIGNAL(dateChanged(QDate)), this, SLOT(updateBegin()));
connect(uiDateMax, SIGNAL(dateChanged(QDate)), this, SLOT(slotUpdate()));
connect(uiToBrowseHistory, SIGNAL(clicked()), this, SLOT(slotToBrowseHistory()));
connect(uiDescentantsOnly, SIGNAL(toggled(bool)), this, SLOT(updateBegin()));
connect(uiDescentantsOnly, SIGNAL(toggled(bool)), this, SLOT(slotSelection()));
connect(uiDescentantsOnly, SIGNAL(toggled(bool)), this, SLOT(slotUpdate()));
connect(uiAncestorsOnly, SIGNAL(toggled(bool)), this, SLOT(updateBegin()));
connect(uiAncestorsOnly, SIGNAL(toggled(bool)), this, SLOT(slotSelection()));
connect(uiAncestorsOnly, SIGNAL(toggled(bool)), this, SLOT(slotUpdate()));
connect(uiAncDec, SIGNAL(toggled(bool)), this, SLOT(updateBegin()));
connect(uiAncDec, SIGNAL(toggled(bool)), this, SLOT(slotSelection()));
connect(uiAncDec, SIGNAL(toggled(bool)), this, SLOT(slotUpdate()));
// init check boxes
// if (searchFamilyName) uiSearchFamilyName->setChecked(true); else uiSearchFamilyName->setChecked(false);
// if (searchFirstName) uiSearchFirstName->setChecked(true); else uiSearchFirstName->setChecked(false);
uiDateMin->setMinimumDate(GET_ALLMINDAY);
uiDateMin->setMaximumDate(GET_ALLMAXDAY);
uiDateMax->setMinimumDate(GET_ALLMINDAY);
uiDateMax->setMaximumDate(GET_ALLMAXDAY);
uiDateMin->setDate(dateLimits.day1);
uiDateMax->setDate(dateLimits.day2);
slotUpdate(); //tämä jossain tapauksessa kaataa softan eikä näköjään edes tarvita, miksi tää oli tässä?
}
// -----------------------------------------------------------------------------------------------------------
int PersonTableModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
const int tmp = GENE.indiLastUsed + 1;
return tmp;
}
int PersonTableModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 3;
}
QVariant PersonTableModel::data(const QModelIndex &index, int role) const
{
int row = index.row();
QVariant result;
result = QVariant();
if (index.isValid()) {
if ( role == Qt::BackgroundColorRole ) {
if (INDI(row).sex == FEMALE ) {
//if (INDI(row).selected) result = ( QColor(255, 240, 240)); else
result = ( QColor(255, 199, 199) );
}
if (INDI(row).sex == MALE ) {
// if (INDI(row).selected) result = ( QColor(240, 240, 255)); else
result = ( QColor(199, 199, 225) );
}
if (INDI(row).sex == UNKNOWN) {
// if (INDI(row).selected) result = ( QColor(240, 240, 240)); else
result = ( QColor(180,180,180));// Qt::gray );
}
}
if ( role == Qt::TextAlignmentRole ) { result = ( Qt::AlignLeft ) ; }
if ( role == Qt::DisplayRole ) {
if (index.column() == 0) result = INDI(row).nameFamily;
if (index.column() == 1) result = INDI(row).nameFirst;
if (index.column() == 2) result = INDI(row).estimatedBirth.toString(true);
}
}
return result;
}
bool PersonTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.isValid() && role == Qt::EditRole) {
int row = index.row();
int column = index.column();
if ( column == 0 ) INDI(row).nameFamily = value.toString();
if ( column == 1 ) INDI(row).nameFirst = value.toString();
emit(dataChanged(index, index));
return true;
}
return false;
}
void PersonTableModel::updateBegin()
{
emit(this->layoutAboutToBeChanged());
}
void PersonTableModel::slotUpdate()
{
this->layoutChanged();
}
QModelIndex PersonTableModel::index( int row, int column, const QModelIndex & parent ) const
{
Q_UNUSED(parent);
return this->createIndex(row, column, row);
}
Qt::ItemFlags PersonTableModel::flags(const QModelIndex &index) const
{
if (!index.isValid()) return Qt::ItemIsEnabled;
return Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable;
}
QVariant PersonTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole) return QVariant();
if (orientation == Qt::Horizontal) {
switch (section) {
case 0: return "Family Name";
case 1: return "First Name";
case 2: return "Birth Day";
default:
return QVariant();
}
}
if (orientation == Qt::Vertical) {
return section;
}
return QVariant();
}
// -----------------------------------------------------------------------------------------------------------
void FamilyTableModel::updateBegin()
{
emit(this->layoutAboutToBeChanged());
}
int FamilyTableModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
const int tmp = GENE.famiLastUsed + 1;
return tmp;
}
int FamilyTableModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 3;
}
QVariant FamilyTableModel::data(const QModelIndex &index, int role) const
{
int row = index.row();
QVariant result = QVariant();
if (index.isValid()) {
if ( role == Qt::TextAlignmentRole ) { result = ( Qt::AlignLeft ) ; }
if ( role == Qt::DisplayRole ) {
if (index.column() == 0) result = INDI(FAM(row).husb).nameFamily + " " + INDI(FAM(row).husb).nameFirst;
if (index.column() == 1) result = INDI(FAM(row).wife).nameFamily + " " + INDI(FAM(row).wife).nameFirst;
if (index.column() == 2) {
Entry marr = FAM(row).fentry(MARRIAGE);
if (marr.day.known) result = marr.day.toString(true);
else result = "Not known";
}
}
}
return result;
}
bool FamilyTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
Q_UNUSED(index);
Q_UNUSED(value);
Q_UNUSED(role);
return false;
}
Qt::ItemFlags FamilyTableModel::flags(const QModelIndex &index) const
{
Q_UNUSED(index);
return Qt::ItemIsEnabled;
}
QVariant FamilyTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole) return QVariant();
if (orientation == Qt::Horizontal) {
switch (section) {
case 0: return "Husb";
case 1: return "Wife";
case 2: return "Marr day";
default:
return QVariant();
}
}
if (orientation == Qt::Vertical) {
return section;
}
return QVariant();
}
QModelIndex FamilyTableModel::index( int row, int column, const QModelIndex & parent ) const
{
Q_UNUSED(parent);
return this->createIndex(row, column, row);
}
void FamilyTableModel::slotUpdate()
{
this->layoutChanged();
}
// --------------------------------------------------------------------------------------------------------
bool MyProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
Q_UNUSED(sourceParent);
// sourcerow equals to id-number of the individual
return p->acceptRow(sourceRow);
}
bool FamilyProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
Q_UNUSED(sourceParent);
return (!FAM(sourceRow).deleted && (p->acceptRow(FAM(sourceRow).wife) || p->acceptRow(FAM(sourceRow).husb)));
}