From b0e6d1d22b94facc556c1ae390fbe9394cd21e43 Mon Sep 17 00:00:00 2001 From: Herwig Birke Date: Mon, 21 Oct 2019 13:37:25 +0200 Subject: [PATCH] calculations --- cbooking.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++------ cbooking.h | 2 ++ cmonthlyview.cpp | 27 +++++++++++++++--- cmonthlyview.h | 1 + cmonthlyview.ui | 10 +++++++ common.h | 1 + 6 files changed, 100 insertions(+), 13 deletions(-) diff --git a/cbooking.cpp b/cbooking.cpp index 1846b48..c8cf2c8 100644 --- a/cbooking.cpp +++ b/cbooking.cpp @@ -24,7 +24,6 @@ cBooking::cBooking(const QDate& date, QObject* parent) : m_information(""), m_soll(QTime()), m_prevDiff(0) - { } @@ -203,12 +202,18 @@ QTime cBooking::soll() code() == "U" || code() == "SU") return(QTime(0, 0, 0)); + return(m_soll); } qint32 cBooking::diff() { - return(0); + int i = istSecs(); + QTime t = soll(); + int s = t.hour()*3600+t.minute()*60+t.second(); + int d = i-s; + + return(d); } QString cBooking::diffString() @@ -219,13 +224,13 @@ QString cBooking::diffString() if(d < 0) { - t = QTime(0, 0, 0, d*1000); + t = QTime(0, 0, 0).addSecs(-d); bNeg = true; } else - t = QTime(0, 0, 0, d*1000); + t = QTime(0, 0, 0).addSecs(d); - return(bNeg ? "-" : "" + t.toString("hh:mm:ss")); + return((bNeg ? "-" : "") + t.toString("hh:mm:ss")); } void cBooking::setPrevDiff(qint32 diff) @@ -240,7 +245,7 @@ qint32 cBooking::prevDiff() qint32 cBooking::currentDiff() { - return(0); + return(prevDiff()+diff()); } QString cBooking::currentDiffString() @@ -251,13 +256,15 @@ QString cBooking::currentDiffString() if(d < 0) { - t = QTime(0, 0, 0, d*1000); + t = QTime(0, 0, 0).addSecs(-d); bNeg = true; } else - t = QTime(0, 0, 0, d*1000); + t = QTime(0, 0, 0).addSecs(d); - return(bNeg ? "-" : "" + t.toString("hh:mm:ss")); + qint32 h = d / 3600; + + return(QString::number(h) + t.toString(":mm:ss")); } qreal cBooking::hoursDecimal() @@ -440,9 +447,16 @@ bool cBookingList::load() } } + recalculate(); + return(true); } +bool sortAsc(cBooking* &v1, cBooking* &v2) +{ + return(v1->date() < v2->date()); +} + cBooking* cBookingList::add(const QDate& date) { if(find(date)) @@ -450,6 +464,7 @@ cBooking* cBookingList::add(const QDate& date) cBooking* lpBooking = new cBooking(date); append(lpBooking); + return(lpBooking); } @@ -462,3 +477,42 @@ cBooking* cBookingList::find(const QDate& date) } return(nullptr); } + +void cBookingList::sort() +{ + std::sort(begin(), end(), sortAsc); +} + +void cBookingList::recalculate(const QDate& date) +{ + sort(); + + bool bFirst = false; + cBookingList::iterator i = begin(); + + if(!date.isNull() && date.isValid()) + { + for(;i != end(); i++) + { + if((*i)->date() >= date) + break; + } + } + + if(i == end()) + return; + + if(i == begin()) + bFirst = true; + + for(;i != end(); i++) + { + if(bFirst) + { + bFirst = false; + (*i)->setPrevDiff(0); + } + else + (*i)->setPrevDiff((*(i-1))->currentDiff()); + } +} diff --git a/cbooking.h b/cbooking.h index 89494ec..da82bbc 100644 --- a/cbooking.h +++ b/cbooking.h @@ -112,7 +112,9 @@ public: bool load(); cBooking* add(const QDate& date); cBooking* find(const QDate& date); + void sort(); + void recalculate(const QDate& date = QDate()); private: cPublicHoliday* m_lpPublicHoliday; cDailyWorkingList* m_lpDailyWorkingList; diff --git a/cmonthlyview.cpp b/cmonthlyview.cpp index eb18f71..25d1a46 100644 --- a/cmonthlyview.cpp +++ b/cmonthlyview.cpp @@ -11,13 +11,15 @@ cMonthlyView::cMonthlyView(const QDate& date, cPublicHoliday *lpPublicHoliday, c ui(new Ui::cMonthlyView), m_loading(true), m_lpPublicHoliday(lpPublicHoliday), - m_lpBookingList(lpBookingList) + m_lpBookingList(lpBookingList), + m_date(date) { m_code.insert(" ", tr("")); m_code.insert("G", tr("Gleitzeit")); m_code.insert("U", tr("Urlaub")); - m_code.insert("K", tr("Krank")); m_code.insert("SU", tr("Sonderurlaub")); + m_code.insert("K", tr("Krank")); + m_code.insert("T", tr("Training")); ui->setupUi(this); @@ -34,6 +36,8 @@ cMonthlyView::cMonthlyView(const QDate& date, cPublicHoliday *lpPublicHoliday, c ui->m_lpUrlaubLabel->setStyleSheet(QString("QLabel { background-color: rgba(%1, %2, %3, 255); }").arg(COLOR_URLAUB.red()).arg(COLOR_URLAUB.green()).arg(COLOR_URLAUB.blue())); ui->m_lpSonderurlaubLabel->setAutoFillBackground(true); ui->m_lpSonderurlaubLabel->setStyleSheet(QString("QLabel { background-color: rgba(%1, %2, %3, 255); }").arg(COLOR_SONDERURLAUB.red()).arg(COLOR_SONDERURLAUB.green()).arg(COLOR_SONDERURLAUB.blue())); + ui->m_lpTrainingLabel->setAutoFillBackground(true); + ui->m_lpTrainingLabel->setStyleSheet(QString("QLabel { background-color: rgba(%1, %2, %3, 255); }").arg(COLOR_TRAINING.red()).arg(COLOR_TRAINING.green()).arg(COLOR_TRAINING.blue())); ui->m_lpMonth->setDate(date); setDate(date); @@ -48,6 +52,8 @@ void cMonthlyView::setDate(const QDate& date) { m_loading = true; + m_date = date; + m_lpPublicHoliday->setYear(static_cast(date.year())); m_lpMonthlyListModel->clear(); @@ -148,9 +154,9 @@ void cMonthlyView::setDate(const QDate& date) items[COL_CODE]->setTextAlignment(Qt::AlignHCenter); items[COL_IS]->setTextAlignment(Qt::AlignHCenter); items[COL_SHOULD]->setTextAlignment(Qt::AlignHCenter); - items[COL_DIFF]->setTextAlignment(Qt::AlignHCenter); + items[COL_DIFF]->setTextAlignment(Qt::AlignRight); items[COL_INFORMATION]->setTextAlignment(Qt::AlignLeft); - items[COL_CURRENT]->setTextAlignment(Qt::AlignHCenter); + items[COL_CURRENT]->setTextAlignment(Qt::AlignRight); items[COL_HOURS_DEC]->setTextAlignment(Qt::AlignRight); items[COL_DAY2]->setTextAlignment(Qt::AlignLeft); @@ -301,6 +307,8 @@ void cMonthlyView::setBackground(const int day, const QString& code) brush = QBrush(COLOR_KRANK); else if(code == "SU") brush = QBrush(COLOR_SONDERURLAUB); + else if(code == "T") + brush = QBrush(COLOR_TRAINING); for(int x = 0;x < m_lpMonthlyListModel->columnCount();x++) { @@ -319,10 +327,13 @@ void cMonthlyView::setText(QStandardItem* lpItem, const QTime& time) void cMonthlyView::recalculate(int day, int /*field*/) { + m_lpBookingList->recalculate(QDate(m_date.year(), m_date.month(), day)); + QStandardItem* lpPauseItem = m_lpMonthlyListModel->item(day-1, COL_BREAK); QStandardItem* lpIstItem = m_lpMonthlyListModel->item(day-1, COL_IS); QStandardItem* lpSollItem = m_lpMonthlyListModel->item(day-1, COL_SHOULD); QStandardItem* lpDecItem = m_lpMonthlyListModel->item(day-1, COL_HOURS_DEC); + QStandardItem* lpDiffItem = m_lpMonthlyListModel->item(day-1, COL_DIFF); cBooking* lpBooking = lpPauseItem->data(DATA_BOOKING).value(); @@ -330,4 +341,12 @@ void cMonthlyView::recalculate(int day, int /*field*/) lpIstItem->setText(lpBooking->ist().toString("hh:mm:ss")); lpDecItem->setText(QString::number(lpBooking->hoursDecimal(), 'f', 2)); lpSollItem->setText(lpBooking->soll().toString("hh:mm:ss")); + lpDiffItem->setText(lpBooking->diffString()); + + for(int i = day;i <= m_date.daysInMonth();i++) + { + QStandardItem* lpCurrentDiffItem = m_lpMonthlyListModel->item(i-1, COL_CURRENT); + cBooking* lpCurrentBooking = lpCurrentDiffItem->data(DATA_BOOKING).value(); + lpCurrentDiffItem->setText(lpCurrentBooking->currentDiffString()); + } } diff --git a/cmonthlyview.h b/cmonthlyview.h index d536353..cf30ce9 100644 --- a/cmonthlyview.h +++ b/cmonthlyview.h @@ -33,6 +33,7 @@ private: QMap m_code; cPublicHoliday* m_lpPublicHoliday; cBookingList* m_lpBookingList; + QDate m_date; void setBackground(const int day, const QString& code); void setText(QStandardItem* lpItem, const QTime& time); diff --git a/cmonthlyview.ui b/cmonthlyview.ui index 2054654..b9dfcd2 100644 --- a/cmonthlyview.ui +++ b/cmonthlyview.ui @@ -356,6 +356,16 @@ + + + + + + + Training + + + diff --git a/common.h b/common.h index 16eb111..c41de68 100644 --- a/common.h +++ b/common.h @@ -34,6 +34,7 @@ #define COLOR_URLAUB QColor(127, 127, 255) #define COLOR_SONDERURLAUB QColor(127, 127, 255) #define COLOR_KRANK QColor(Qt::yellow) +#define COLOR_TRAINING QColor(Qt::red) #define COLOR_WEEKEND QColor(255, 165, 0) #endif // COMMON_H