/
progressmodel.cpp
1699 lines (1471 loc) · 49.4 KB
/
progressmodel.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
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
Copyright (C) 2021 Dr. Valentin Illich
This file is part of WorkTracker.
WorkTracker is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
WorkTracker is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with WorkTracker. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "progressmodel.h"
#include "../backup/Utilities.h"
#include "ressources/version.inc"
QQmlApplicationEngine *m_qmlEngine;
ProgressEntry::ProgressEntry()
{
QDateTime timestamp = m_timeStamp;
timestamp.time().setHMS(0,0,0);
m_timeStamp = timestamp;
}
ProgressEntry::ProgressEntry(int id, QDateTime timestamp, QString name, QString description, bool active, int currentAccount, QVector<quint64> workInSeconds)
: ProgressEntry()
{
m_id = id;
m_timeStamp = timestamp;
m_name = name;
m_description = description;
m_active = active;
m_account = currentAccount;
m_workInSeconds = workInSeconds;
if( m_workInSeconds.size()<2 )
m_workInSeconds.push_back(0);
}
ProgressEntry::ProgressEntry(int itemId, const QString &fromString)
: ProgressEntry()
{
int workIndex = 0;
int recIndex = 0;
m_id = itemId;
QStringList properties = fromString.split(";");
if( properties[0].contains("=") )
{
for( const auto &item : qAsConst(properties) )
{
if( item.contains("itmid=") )
m_id = item.midRef(6).toInt();
if( item.contains("title=") )
m_name = item.mid(6);
if( item.contains("descr=") )
m_description = item.mid(6);
if( item.contains("creat=") )
m_timeStamp = QDateTime::fromSecsSinceEpoch(item.mid(6).toLongLong());;
if( item.contains("recor=") )
{
qint64 secsSinceEpoch = item.mid(6).toLongLong();
QDateTime time = secsSinceEpoch>=0 ? QDateTime::fromSecsSinceEpoch(secsSinceEpoch) : QDateTime();
if( recIndex==m_recordingStart.size() )
m_recordingStart.push_back(time);
else
m_recordingStart[recIndex] = time;
recIndex++;
}
if( item.contains("spent=") )
{
if( workIndex==m_workInSeconds.size() )
m_workInSeconds.push_back(item.midRef(6).toULongLong());
else
m_workInSeconds[workIndex] = item.midRef(6).toULongLong();
workIndex++;
}
}
}
else
{
QDateTime time = QDateTime::fromSecsSinceEpoch(properties[3].toLongLong());
m_id = (itemId==-1 ? properties[0].toInt() : itemId),
m_timeStamp = time;
m_name = properties[1];
m_description = properties[2];
m_active = false;
m_workInSeconds[0] = properties[4].toULongLong();
}
}
QString ProgressEntry::toString() const
{
return "itmid=" + QString::number(m_id) + ";"
+ "title=" + m_name + ";"
+ "descr=" + m_description + ";"
+ "creat=" + QString::number(m_timeStamp.toSecsSinceEpoch()) + ";"
+ "spent=" + QString::number(m_workInSeconds[0]) + ";"
+ "spent=" + QString::number(m_workInSeconds[1]) + ";"
+ "recor=" + QString::number(m_recordingStart[0].isValid() ? m_recordingStart[0].toSecsSinceEpoch() : -1) + ";"
+ "recor=" + QString::number(m_recordingStart[1].isValid() ? m_recordingStart[1].toSecsSinceEpoch() : -1);// + ";"
//"" + (m_doneHome ? "h" : "o");
}
int ProgressEntry::getId() const
{
return m_id;
}
bool ProgressEntry::isRecreationItem() const
{
if( m_description.length()<9 )
return false;
if( m_description[2]==':' && m_description[5]=='-' && m_description[8]==':' )
return true;
else
return false;
}
QDateTime ProgressEntry::getTimeStamp() const
{
return m_timeStamp;
}
QDateTime ProgressEntry::getRecordingStart(const int &typeOfWork) const
{
return m_recordingStart[typeOfWork];
}
void ProgressEntry::setRecordingStart(const int &typeOfWork, const QDateTime &time)
{
m_recordingStart[typeOfWork] = time;
}
void ProgressEntry::setTimeStamp(const QDateTime &time)
{
m_timeStamp = time;
}
QString ProgressEntry::getItemName() const
{
return m_name;
}
void ProgressEntry::setItemName(const QString &name)
{
m_name = name;
}
QString ProgressEntry::getItemDescription() const
{
return m_description;
}
void ProgressEntry::setItemDescription(const QString &descr)
{
m_description = descr;
if( isRecreationItem() )
{
QTime startTime = QTime::fromString(descr.left(5));
QTime endTime = QTime::fromString(descr.mid(6,5));
m_timeStamp = QDateTime(m_timeStamp.date(),startTime);
m_workInSeconds[m_account] = startTime.secsTo(endTime);
}
}
bool ProgressEntry::getItemActive() const
{
return m_active;
}
void ProgressEntry::setItemActive(const bool &active)
{
m_active = active;
}
int ProgressEntry::getItemCurrentAccount() const
{
return m_account;
}
void ProgressEntry::setItemCurrentAccount(const int &account)
{
m_account = account;
}
QVector<quint64> ProgressEntry::getAllWorkInSeconds() const
{
return m_workInSeconds;
}
void ProgressEntry::addAllWorkInSeconds(const QVector<quint64> &work)
{
for( int i=0; i<work.size(); i++ )
m_workInSeconds[i] += work[i];
}
quint64 ProgressEntry::getWorkInSeconds(const int &typeOfWork) const
{
if (typeOfWork<m_workInSeconds.size())
return m_workInSeconds[typeOfWork];
else
return 0;
}
void ProgressEntry::setWorkInSeconds(const int &typeOfWork,const quint64 &work)
{
while (m_workInSeconds.size()<typeOfWork) m_workInSeconds.push_back(0);
m_workInSeconds[typeOfWork] = 0;
addWorkInSeconds(typeOfWork,work);
}
void ProgressEntry::addWorkInSeconds(const int &typeOfWork,const qint64 &work)
{
while (m_workInSeconds.size()<typeOfWork) m_workInSeconds.push_back(0);
QDateTime currentTime = QDateTime::currentDateTime();
if( work>0 )
{
if( m_workInSeconds[typeOfWork]==0 )
m_recordingStart[typeOfWork] = currentTime;
QTime firstRecordTime = currentTime.time().addSecs(-m_workInSeconds[typeOfWork] - work);
if( m_recordingStart[typeOfWork].isValid() )
{
if( m_recordingStart[typeOfWork].time()>=firstRecordTime )
m_recordingStart[typeOfWork].setTime(firstRecordTime);
}
// if( m_timeStamp.time()>=firstRecordTime )
// m_timeStamp.setTime(firstRecordTime);
}
qint64 current = m_workInSeconds[typeOfWork];
current += work;
if( current<0 )
current = 0;
m_workInSeconds[typeOfWork] = current;
if( isRecreationItem() )
{
QTime startTime = getTimeStamp().time();
m_description = startTime.toString("HH:mm")+"-"+startTime.addSecs(current).toString("HH:mm");
}
}
struct storageFileSettings
{
QMap<ProgressModel::StorageType,QString> m_storageFile =
{{ProgressModel::DataStorage, "workingOnProjects"},
{ProgressModel::DefaultListStorage,"defaultItems"}
};
QString m_storagPath = "";
};
static storageFileSettings *g_storage = nullptr;
static storageFileSettings &getFileSettings()
{
if( g_storage==nullptr )
g_storage = new storageFileSettings;
return *g_storage;
}
QString getConfigurationsPath(QString const &organization, QString const &application)
{
QString path = QDir::homePath()+"/"+organization+"/"+application+"/";
QDir dir(path);
if( dir.mkpath(path) )
return path;
else
return "";
}
QString getDataPath(QString const &organization, QString const &application)
{
QString path = getFileSettings().m_storagPath;
if( !path.isEmpty() )
return path+"/";
QString inifile = getConfigurationsPath(organization,application)+"/settings.ini";
QSettings settings(inifile,QSettings::IniFormat);
if( settings.contains("data-storage-path") )
{
path = settings.value("data-storage-path").toString();
if( !path.endsWith("/") ) path.append("/");
}
else
{
path = getConfigurationsPath(organization,application);
settings.setValue("data-storage-path",path);
}
return path;
}
ProgressModel::ProgressModel(QObject *parent) : QObject(parent)
{
connect(this,SIGNAL(languageChanged()),m_qmlEngine,SLOT(retranslate()));
QString inifile = getConfigurationsPath("config-valentins-qtsolutions","workTracking")+"/settings.ini";
QSettings settings(inifile,QSettings::IniFormat);
if( settings.contains("language-id") )
changeLanguage(settings.value("language-id").toInt());
if( settings.contains("window-geometry") )
m_windowGeometry = settings.value("window-geometry").toRect();
QList<QScreen*> screens = qApp->screens();
QSize screensize = screens[0]->size();
if( (m_windowGeometry.x()+m_windowGeometry.width())>screensize.width() )
m_windowGeometry.moveLeft(screensize.width()-m_windowGeometry.width());
if( (m_windowGeometry.y()+m_windowGeometry.height())>screensize.height() )
m_windowGeometry.moveTop(screensize.height()-m_windowGeometry.height());
if( settings.contains("yellow-alert-begin") )
m_yellowAlertLimit = settings.value("yellow-alert-begin").toULongLong();
if( settings.contains("red-alert-begin") )
m_redAlertLimit = settings.value("red-alert-begin").toULongLong();
if( settings.contains("inactivity-stop-limit") )
m_inactivityLimit = settings.value("inactivity-stop-limit").toULongLong();
m_actualDate = QDateTime::currentDateTime();
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(workingTimer()));
timer->start(200);
bool todayListIsEmpty = true;
QString dataFile = getDataPath("config-valentins-qtsolutions","workTracking")+"/"+getStorageBaseFileName(DataStorage)+".csv";
QFile file(dataFile);
file.open(QFile::ReadOnly);
QTextStream s(&file);
while( !s.atEnd() )
{
m_progressEntries << ProgressEntry(-1,s.readLine());
m_nextId = m_progressEntries.last().getId();
if( m_progressEntries.last().getTimeStamp().date()==m_actualDate.date() )
todayListIsEmpty = false;
}
m_nextId++;
if( todayListIsEmpty )
{
QString dataFile = getDataPath("config-valentins-qtsolutions","workTracking")+"/"+getStorageBaseFileName(DefaultListStorage)+".csv";
QFile file(dataFile);
file.open(QFile::ReadOnly);
QTextStream s(&file);
while( !s.atEnd() )
{
m_progressEntries << ProgressEntry(m_nextId++,s.readLine());
}
}
updateItemsList();
}
ProgressModel::~ProgressModel()
{
if( m_dataChanged )
saveData(getStorageBaseFileName(DataStorage), false);
QString inifile = getConfigurationsPath("config-valentins-qtsolutions","workTracking")+"/settings.ini";
QSettings settings(inifile,QSettings::IniFormat);
settings.setValue("window-geometry",m_windowGeometry);
settings.setValue("yellow-alert-begin",m_yellowAlertLimit);
settings.setValue("red-alert-begin",m_redAlertLimit);
settings.setValue("inactivity-stop-limit",m_inactivityLimit);
}
QDateTime ProgressModel::actualDate() const
{
return m_actualDate;
}
void ProgressModel::setActualDate(const QDateTime &date)
{
m_actualDate = date;
}
ProgressModel::OperatingMode ProgressModel::mode()
{
return m_operatingMode;
}
void ProgressModel::setMode(const ProgressModel::OperatingMode &mode)
{
m_operatingMode = mode;
updateItemsList();
emit titleChanged();
emit totalTimeChanged();
emit modeChanged();
emit showExportDaysChanged();
emit backgrundColorChanged();
}
int ProgressModel::currentRecordingAccount()
{
return m_currentRecordingAccount;
}
void ProgressModel::setCurrentRecordingAccount(const int &account)
{
m_currentRecordingAccount = account;
emit currentRecordingAccountChanged();
for( const auto item : qAsConst(m_progressItems) )
{
item->setSelectedAccount(account);
if( m_recordingRequestedItem>=0 && item->getId()==m_recordingRequestedItem )
item->setIsActive(true);
}
}
void ProgressModel::exportToClipboard(const QString &additionalMinutes,const QString &thresholdHours)
{
// this will ensure that m_totalWorkSeconds is correct
updateItemsList(false);
quint64 threashold = thresholdHours.toULong();
quint64 additional = additionalMinutes.toULong();
QClipboard *clipboard = QGuiApplication::clipboard();
QString data;
for( const auto item : qAsConst(m_progressItems) )
{
if( m_showSummariesInPercent || m_operatingMode!=DisplayWeek )
{
ProgressEntry summary;
summary.addWorkInSeconds(0,item->workInSeconds());
data += item->projectName() + "\t" + getSummaryText(summary,m_totalWorkSeconds,true) + "\n";
}
else
{
int dayofweek = item->timeStamp().date().dayOfWeek() - 1;
quint64 timespent = item->workInSeconds();
quint64 totalSpent = item->totalWorkInSeconds();
bool includingRecreation = false;
qDebug() << "+++ creation of" << item->projectName() << "is" << item->timeStamp().toString();
QDateTime startDateTime = item->getRecordingStart();
if( !startDateTime.isValid() )
startDateTime = item->timeStamp();
QTime startTime = startDateTime.time();
int HH = startTime.hour();
int MM = (startTime.minute()/5)*5;
startTime.setHMS(HH,MM,0);
startDateTime.setTime((startTime));
// list of interruptions for the day
QList<quint64> workingBlockLengths;
QList<quint64> workingBlocksInterrupt;
if( m_recreationEntries.contains(dayofweek) )
{
// ..........|..........................|---|...................................|------|..............|............
// first recording recr1 recr2 last recording
// +++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++ +++++++++++++++
// work block 1 work block 2 work block 3
for( const auto &entry : qAsConst(m_recreationEntries[dayofweek]) )
{
QDateTime firstrecordingOfDay = startDateTime;
qDebug() << "++++ found recreation at" << entry.getTimeStamp().toString() << "with" << getSummaryWorkInSeconds(entry) << "seconds";
qint64 workblocklen = entry.getTimeStamp().toSecsSinceEpoch() - firstrecordingOfDay.toSecsSinceEpoch(); // todo office?
qint64 recreationlen = getSummaryWorkInSeconds(entry);
workblocklen += 150;
workblocklen = (workblocklen / 300) * 300;
recreationlen += 150;
recreationlen = (recreationlen / 300) * 300;
qDebug() << "++++ next working block with" << workblocklen << "seconds, interrupt" << recreationlen;
workingBlockLengths << workblocklen;
workingBlocksInterrupt << recreationlen;
qint64 nextbegin = firstrecordingOfDay.toSecsSinceEpoch() + workblocklen + recreationlen;
startDateTime.setSecsSinceEpoch(nextbegin);
}
}
else
{
if( getWeekdaySelected(dayofweek) )
{
workingBlockLengths << (threashold>0 ? threashold : timespent/2)*3600;
workingBlocksInterrupt << (threashold>0 ? additional : 30)*60;
}
}
timespent += 150; // round in range of 5 minutes
timespent = (timespent / 300) * 300;
if( timespent>0 )
{
// if the list with working blocks is empty just use a block which is big enough for all needs
quint64 nextBlockLen = workingBlockLengths.isEmpty() ? 10*3600 : workingBlockLengths.takeFirst();
quint64 nextInterruptLen = workingBlocksInterrupt.isEmpty() ? 60*60 : workingBlocksInterrupt.takeFirst();
bool isCompleteTimeSpentInFIrstWorkingBlock = false;
// check if the complete spent time is within the first workin block
if( nextBlockLen+5*60 > timespent )
isCompleteTimeSpentInFIrstWorkingBlock = true;
// add additional rest time of specified by export settings
if( getWeekdaySelected(dayofweek) )
{
if( threashold>0 && totalSpent>(threashold*3600) )
{
timespent += additional*60;
includingRecreation = true;
//adjust the starting time of first block if needed
if( isCompleteTimeSpentInFIrstWorkingBlock )
{
startTime = startTime.addSecs(-additional*60);
nextBlockLen += additional*60;
}
}
}
while( timespent>nextBlockLen )
{
QTime endTime = startTime.addSecs(nextBlockLen);
//data += item->projectName() + "\t" + startTime.toString("hh:mm") + "\t" + endTime.toString("hh:mm") + "\n";
data += item->projectName() + "\tMobiles Arbeiten Beginn\t" + startTime.toString("hh:mm")+":00\n";
data += item->projectName() + "\tMobiles Arbeiten Ende\t" + endTime.toString("hh:mm")+":00\n";
startTime = startTime.addSecs(nextBlockLen + nextInterruptLen);
timespent -= nextBlockLen;
if( workingBlockLengths.isEmpty() )
break;
nextBlockLen = workingBlockLengths.takeFirst();
nextInterruptLen = workingBlocksInterrupt.takeFirst();
}
QTime endTime = startTime.addSecs(timespent);
QString comment = includingRecreation ? "inkl. Pause" : "";
//data += item->projectName() + "\t" + startTime.toString("hh:mm") + "\t" + endTime.toString("hh:mm") + "\t" + comment + "\n";
data += item->projectName() + "\tMobiles Arbeiten Beginn\t" + startTime.toString("hh:mm")+":00\n";
data += item->projectName() + "\tMobiles Arbeiten Ende\t" + endTime.toString("hh:mm")+":00\t" + comment + "\n";
}
}
}
clipboard->setText(data);
}
QString ProgressModel::humanReadableMonth(int month)
{
const QString s_months[] = {
tr("January"),tr("February"),tr("March"),tr("April"),tr("May"),tr("June"),
tr("July"),tr("August"),tr("September"),tr("October"),tr("November"),tr("December")
};
return s_months[month];
}
QString ProgressModel::humanReadableWeekDay(int weekday)
{
const QString s_days[] = {
tr("Monday"),tr("Tuesday"),tr("Wednesday"),tr("Thursday"),tr("Friday"),tr("Saturday"),tr("Sunday")
};
return s_days[weekday];
}
QString ProgressModel::humanReadableDate(QDateTime date)
{
QString format = tr("yyyy/MM/dd");
return date.date().toString(format);
}
QString ProgressModel::title()
{
switch( m_operatingMode )
{
case DisplayYear:
return QString::number(m_actualDate.date().year());
break;
case DisplayMonth:
return humanReadableMonth(m_actualDate.date().month()-1);
break;
case DisplayWeek:
return tr("week") +" " + QString::number(m_actualDate.date().weekNumber()) + " " + tr("of year");
break;
case DisplayRecordDay:
return humanReadableWeekDay(m_actualDate.date().dayOfWeek()-1) + " " + humanReadableDate(m_actualDate);
break;
}
return "";
}
bool ProgressModel::isChangeable() const
{
return m_operatingMode==DisplayRecordDay;
}
bool ProgressModel::alwaysShowWork() const
{
return m_alwaysShowWork;
}
void ProgressModel::setAlwaysShowWork(const bool &alwaysWork)
{
m_alwaysShowWork = alwaysWork;
updateItemsList();
emit alwaysShowWorkChanged();
emit showExportDaysChanged();
}
bool ProgressModel::showBreakTimes() const
{
return m_showBreakTimes;
}
void ProgressModel::setShowBreakTimes(const bool &show)
{
m_showBreakTimes = show;
updateItemsList();
emit showBreakTimesChanged();
}
QQmlListProperty<ProgressItem> ProgressModel::itemList()
{
return QQmlListProperty<ProgressItem>(this, m_progressItems);
}
bool ProgressModel::showSumInPercent() const
{
return m_showSummariesInPercent;
}
void ProgressModel::changeLanguage(int language)
{
static QTranslator translator;
bool loaded = false;
QCoreApplication::removeTranslator(&translator);
// 0 - english, just remove translation
// 1 - german
if( language==1 )
loaded = translator.load(":/translations/workTracking_de.qm");
// 2 - french
if( language==2 )
loaded = translator.load(":/translations/workTracking_fr.qm");
if ( loaded )
QCoreApplication::installTranslator(&translator);
}
bool ProgressModel::showExportDays()
{
return !m_alwaysShowWork && !m_showSummariesInPercent && m_operatingMode==DisplayWeek;
}
QString ProgressModel::totalTime() const
{
return getSummaryText(getTotalTime(),QVector<quint64>({0,0})); // todo m_totalWorkSeconds
}
void ProgressModel::itemStateChanged()
{
ProgressItem *item = static_cast<ProgressItem*>(sender());
for( int i=0; i<m_progressEntries.size(); i++ )
{
if( m_progressEntries[i].getId()==item->getId() )
{
m_dataChanged = true;
m_progressEntries[i].setItemActive(item->isActive());
item->setSummary(getSummaryText(m_progressEntries[i],m_totalWorkSeconds));
if( item->isActive() )
{
m_recordingRequestedItem = item->getId();
if( m_currentRecordingAccount==-1 )
{
item->setIsActive(false);
emit recordingDisabled();
}
}
}
}
}
void ProgressModel::itemNameChanged()
{
ProgressItem *item = static_cast<ProgressItem*>(sender());
for( int i=0; i<m_progressEntries.size(); i++ )
{
m_dataChanged = true;
if( m_progressEntries[i].getId()==item->getId() )
m_progressEntries[i].setItemName(item->projectName());
}
}
void ProgressModel::itemDescriptionChanged()
{
ProgressItem *item = static_cast<ProgressItem*>(sender());
for( int i=0; i<m_progressEntries.size(); i++ )
{
m_dataChanged = true;
if( m_progressEntries[i].getId()==item->getId() )
m_progressEntries[i].setItemDescription(item->description());
}
}
void ProgressModel::itemAccountChanged()
{
ProgressItem *item = static_cast<ProgressItem*>(sender());
for( int i=0; i<m_progressEntries.size(); i++ )
{
if( m_progressEntries[i].getId()==item->getId() )
m_progressEntries[i].setItemCurrentAccount(item->selectedAccount());
}
}
void ProgressModel::workingTimer()
{
QDateTime current = QDateTime::currentDateTime();
quint64 elapsed = current.currentSecsSinceEpoch();
if( elapsed==m_lastElapsed)
return;
qint64 delta = elapsed-m_lastElapsed;
m_lastElapsed = elapsed;
bool isCurrnetIdle = true;
for( int i=0; i<m_progressEntries.size(); i++ )
{
if( m_progressEntries[i].getItemActive() )
{
isCurrnetIdle = false;
m_dataChanged = true;
m_progressEntries[i].addWorkInSeconds(m_currentRecordingAccount,delta);
for( int j=0; j<m_progressItems.size(); j++ )
{
if( m_progressItems.at(j)->getId()==m_progressEntries[i].getId() )
{
m_progressItems.at(j)->setSummary(getSummaryText(m_progressEntries[i],m_totalWorkSeconds));
}
}
}
}
if( m_recordingRequestedItem>=0 && m_currentRecordingAccount>=0 )
{
for( const auto item : qAsConst(m_progressItems) )
{
if( item->getId()!=m_recordingRequestedItem )
item->setIsActive(false);
}
m_recordingRequestedItem = -1;
}
else if( isCurrnetIdle )
{
m_idleSinceSeconds++;
if( m_idleSinceSeconds>=300 && m_currentRecordingAccount>=0 )
{
m_currentRecordingAccount = -1;
emit currentRecordingAccountChanged();
}
}
else if( m_recordedSeconds>0 && m_idleSinceSeconds>300 ) // todo only with home ???
{
// create an item for the break
QDateTime startOfRest = QDateTime::fromSecsSinceEpoch(elapsed-m_idleSinceSeconds);
QDateTime endOfRest = startOfRest.addSecs(m_idleSinceSeconds);
ProgressEntry item {m_nextId++,startOfRest,"","",false,0,{0,0}};
item.setItemDescription(startOfRest.time().toString("HH:mm")+"-"+endOfRest.time().toString("HH:mm"));
//item.setWorkInSeconds(m_currentRecordingAccount,m_idleSinceSeconds);
item.setItemCurrentAccount(0);
m_progressEntries << item;
m_idleSinceSeconds = 0;
updateItemsList();
}
else
{
m_recordedSeconds++;
}
m_runningSeconds++;
if( (m_runningSeconds%5) == 0 )
{
QPoint globalCursorPos = QCursor::pos();
if( m_lastx==-1 && m_lasty==-1 )
{
m_lastx = globalCursorPos.x();
m_lasty = globalCursorPos.y();
m_lastActivity = m_runningSeconds;
}
else if( globalCursorPos.x()!=m_lastx || globalCursorPos.y()!=m_lasty )
{
m_lastx = globalCursorPos.x();
m_lasty = globalCursorPos.y();
m_lastActivity = m_runningSeconds;
if( isCurrnetIdle )
{
QObject *root = m_qmlEngine->rootObjects().at(0);
static_cast<QWindow*>(root)->alert(0);
qApp->beep();
}
}
else
{
if( !isCurrnetIdle )
{
if( (m_runningSeconds-m_lastActivity)>m_inactivityLimit )
{
for( int i=0; i<m_progressEntries.size(); i++ )
{
if( m_progressEntries[i].getItemActive() )
{
m_progressEntries[i].setItemActive(false);
m_progressEntries[i].addWorkInSeconds(m_currentRecordingAccount,-m_inactivityLimit);
m_lastRecordingItem = m_progressEntries[i].getId();
for( int j=0; j<m_progressItems.size(); j++ )
{
if( m_progressItems.at(j)->getId()==m_lastRecordingItem )
{
m_progressItems.at(j)->setIsActive(false);
m_progressItems.at(j)->setSummary(getSummaryText(m_progressEntries[i],m_totalWorkSeconds));
}
}
m_lastRecordingAccount = m_currentRecordingAccount;
m_lastRecordingSeconds = m_runningSeconds-m_inactivityLimit;
m_idleSinceSeconds = m_inactivityLimit;
m_currentRecordingAccount = -1;
emit totalTimeChanged();
emit recordingStopped();
emit currentRecordingAccountChanged();
QObject *root = m_qmlEngine->rootObjects().at(0);
static_cast<QWindow*>(root)->alert(0);
qApp->beep();
}
}
}
}
}
}
if( (m_runningSeconds % 300) == 0)
{
if( m_dataChanged )
{
saveData(getStorageBaseFileName(DataStorage), false);
m_dataChanged = false;
}
}
ProgressEntry summary = getTotalTime();
quint64 workedsofar = getSummaryWorkInSeconds(summary);
quint64 checkinSince = m_checkinTime==-1 ? 0 : m_lastElapsed - m_checkinTime;
quint64 value = std::max<quint64>(workedsofar,checkinSince);
if( value>m_redAlertLimit )
{
if( (m_runningSeconds % 30) == 0)
{
QObject *root = m_qmlEngine->rootObjects().at(0);
static_cast<QWindow*>(root)->alert(0);
qApp->beep();
}
}
if( value>m_yellowAlertLimit )
{
if( (m_runningSeconds % 300) == 0)
{
QObject *root = m_qmlEngine->rootObjects().at(0);
static_cast<QWindow*>(root)->alert(0);
qApp->beep();
}
}
emit totalTimeChanged();
emit backgrundColorChanged();
}
QString ProgressModel::getProgramVersion()
{
return TRACKING_STR_VERSION;
}
void ProgressModel::previous()
{
switch( m_operatingMode )
{
case DisplayYear:
m_actualDate = m_actualDate.addYears(-1);
break;
case DisplayMonth:
m_actualDate = m_actualDate.addMonths(-1);
break;
case DisplayWeek:
m_actualDate = m_actualDate.addDays(-7);
break;
case DisplayRecordDay:
m_actualDate = m_actualDate.addDays(-1);
break;
}
updateItemsList();
emit totalTimeChanged();
emit actualDateChanged();
emit titleChanged();
emit backgrundColorChanged();
}
void ProgressModel::next()
{
switch( m_operatingMode )
{
case DisplayYear:
m_actualDate = m_actualDate.addYears(1);
break;
case DisplayMonth:
m_actualDate = m_actualDate.addMonths(1);
break;
case DisplayWeek:
m_actualDate = m_actualDate.addDays(7);
break;
case DisplayRecordDay:
m_actualDate = m_actualDate.addDays(1);
break;
}
updateItemsList();
emit actualDateChanged();
emit titleChanged();
emit totalTimeChanged();
emit backgrundColorChanged();
}
void ProgressModel::jumpToDay(const int &day, const int &month, const int &year)
{
qDebug() << day << "/" << month << "/" << year;
m_actualDate = QDateTime::currentDateTime();
QDate actualdate = m_actualDate.date();
int actualyear = actualdate.year();
int actualmonth = actualdate.month();
int actualday = actualdate.day();
if( day>0 ) actualday = day;
if( month>0 ) actualmonth = month;
if( year>0 ) actualyear = year;
actualdate.setDate(actualyear,actualmonth,actualday);
m_actualDate.setDate(actualdate);
updateItemsList();
emit totalTimeChanged();
emit backgrundColorChanged();
emit actualDateChanged();
emit titleChanged();
}
void ProgressModel::enterCheckin(const QString &checkin)
{
qDebug() << checkin;
QDateTime actual = QDateTime::currentDateTime();
QTime time = actual.time();
time.setHMS(checkin.midRef(0,2).toInt(),checkin.midRef(3,2).toInt(),0);
actual.setTime(time);
m_checkinTime = actual.toSecsSinceEpoch();
for( int i=0; i<m_progressEntries.size(); i++ )
{
if( m_progressEntries[i].getItemActive() )
{
qint64 firstRecording = m_progressEntries[i].getRecordingStart(m_currentRecordingAccount).toSecsSinceEpoch();
if( firstRecording>m_checkinTime )
{
quint64 delta = firstRecording-m_checkinTime;
m_progressEntries[i].addWorkInSeconds(m_currentRecordingAccount,delta);
m_progressEntries[i].setRecordingStart(m_currentRecordingAccount,QDateTime::fromSecsSinceEpoch(m_checkinTime));
for( int j=0; j<m_progressItems.size(); j++ )
{
if( m_progressItems.at(j)->getId()==m_progressEntries[i].getId() )