File: TimeTrackingView.cpp

package info (click to toggle)
charmtimetracker 1.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,340 kB
  • sloc: cpp: 19,176; xml: 284; python: 120; makefile: 14
file content (438 lines) | stat: -rw-r--r-- 16,423 bytes parent folder | download | duplicates (2)
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
/*
  TimeTrackingView.cpp

  This file is part of Charm, a task-based time tracking application.

  Copyright (C) 2014-2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com

  Author: Frank Osterfeld <frank.osterfeld@kdab.com>

  This program 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 2 of the License, or
  (at your option) any later version.

  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "TimeTrackingView.h"
#include "Data.h"
#include "ViewHelpers.h"

#include "Core/CharmConstants.h"
#include "Core/Configuration.h"

#include <QFont>
#include <QFontMetrics>
#include <QMessageBox>
#include <QPaintEvent>
#include <QPainter>

#include <algorithm>
#include <functional>
#include <numeric>

const int Margin = 2;

TimeTrackingView::TimeTrackingView(QWidget *parent)
    : QWidget(parent)
    , m_taskSelector(new TimeTrackingTaskSelector(this))
{
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    // plumbing
    m_paintAttributes.initialize(palette());
    for (int i = 0; i < 7; ++i)
        m_shortDayNames[i] = QDate::shortDayName(i + 1);
    connect(m_taskSelector, &TimeTrackingTaskSelector::startEvent,
            this, &TimeTrackingView::startEvent);
    connect(m_taskSelector, &TimeTrackingTaskSelector::stopEvents,
            this, &TimeTrackingView::stopEvents);
    connect(m_taskSelector, &TimeTrackingTaskSelector::updateSummariesPlease,
            this, &TimeTrackingView::slotUpdateSummaries);

    setFocusProxy(m_taskSelector);
    setFocusPolicy(Qt::StrongFocus);
}

void TimeTrackingView::populateEditMenu(QMenu *menu)
{
    m_taskSelector->populateEditMenu(menu);
}

void TimeTrackingView::PaintAttributes::initialize(const QPalette &palette)
{
    headerBrush = palette.mid();
    taskBrushEven = palette.light();
    taskBrushOdd = palette.midlight();
    totalsRowBrush = headerBrush;
    totalsRowEvenDayBrush = QBrush(taskBrushEven.color().darker(125));
    headerEvenDayBrush = totalsRowEvenDayBrush;
    QColor dimHighlight = palette.highlight().color();
    dim = 0.25;
    dimHighlight.setAlphaF(dim * dimHighlight.alphaF());
    runningTaskColor = palette.highlight().color();
}

QSize TimeTrackingView::sizeHint() const
{
    if (!m_cachedSizeHint.isValid()) {
        // the sizeHint is like the minimum size hint, only it allows
        // for more text in the task name column
        const QFontMetrics narrowFontMetrics = QFontMetrics(m_narrowFont);
        const QRect textRect
            = narrowFontMetrics.boundingRect(tr("moremoremoremoremore"));
        const int widthHint = minimumSizeHint().width() + textRect.width();
        m_cachedSizeHint = QSize(widthHint, minimumSizeHint().height());
    }
    return m_cachedSizeHint;
}

QSize TimeTrackingView::minimumSizeHint() const
{
    if (!m_cachedMinimumSizeHint.isValid()) {
        // the header row, task rows, and totals row are all of the same height
        const QFontMetrics fixedFontMetrics(m_fixedFont);
        const QFontMetrics narrowFontMetrics(m_narrowFont);
        const QRect totalsColumnFieldRect(
            fixedFontMetrics.boundingRect(QStringLiteral("100:00"))
            .adjusted(0, 0, 2 * Margin, 2 * Margin));
        const int dayWidth = fixedFontMetrics.width(QStringLiteral("00:00")) + 2 * Margin;
        const int fieldHeight = qMax(fixedFontMetrics.lineSpacing(),
                                     narrowFontMetrics.lineSpacing())
                                + 2 * Margin;
        const QRect taskColumnFieldRect = narrowFontMetrics.boundingRect(
            tr("KDABStuffngy"))
                                          .adjusted(0, 0, 2 * Margin, 2 * Margin);
        // the tracking row needs to accommodate the task selector widget
        const QSize taskSelectorSizeHint = m_taskSelector->sizeHint();
        const int trackingRowHeight = qMax(fieldHeight, taskSelectorSizeHint.height() + 2 * Margin);

        const int minimumWidth
            = totalsColumnFieldRect.width()
              + 7 * dayWidth
              + taskColumnFieldRect.width();
        const int minimumHeight = (rowCount() - 1) * fieldHeight + trackingRowHeight;
        m_cachedMinimumSizeHint = QSize(minimumWidth, minimumHeight);
        m_cachedTotalsFieldRect = QRect(0, 0, totalsColumnFieldRect.width(), fieldHeight);
        m_cachedDayFieldRect = QRect(0, 0, dayWidth, fieldHeight);
    }
    return m_cachedMinimumSizeHint;
}

QMenu *TimeTrackingView::menu() const
{
    return m_taskSelector->menu();
}

void TimeTrackingView::paintEvent(QPaintEvent *e)
{
    m_activeFieldRects.clear();
    const int FieldHeight = m_cachedTotalsFieldRect.height();
    QPainter painter(this);
    // all attributes are determined in data(), we just paint the rects:
    for (int row = 0; row < rowCount() - 1; ++row) {
        for (int column = 0; column < columnCount(); ++column) {
            // get the rectangle of the field that will be drawn
            QRect fieldRect;
            const int y = row * FieldHeight;
            if (column == columnCount() - 1) {   // totals column
                fieldRect = QRect(width() - m_cachedTotalsFieldRect.width(), y,
                                  m_cachedTotalsFieldRect.width(), FieldHeight);
            } else if (column == 0) {   // task column
                fieldRect = QRect(0, y, taskColumnWidth(), FieldHeight);
            } else if (column > 0) {   //  a task
                fieldRect = QRect(width() - m_cachedTotalsFieldRect.width()
                                  - 8 * m_cachedDayFieldRect.width()
                                  + column * m_cachedDayFieldRect.width(), y,
                                  m_cachedDayFieldRect.width(), FieldHeight);
            }
            // paint the field, if it is in the dirty region
            if (e->rect().contains(fieldRect)) {
                DataField field = m_defaultField;
                data(field, column, row);
                int alignment = Qt::AlignRight | Qt::AlignVCenter;
                if (row == 0) {
                    alignment = Qt::AlignCenter | Qt::AlignVCenter;
                } else if (column == 0 && row < rowCount() - 1) {
                    alignment = Qt::AlignLeft | Qt::AlignVCenter;
                }
                if (column == 0)    // task column
                    field.text = elidedText(field.text, field.font, fieldRect.width() - 2*Margin);
                if (field.storeAsActive) m_activeFieldRects << fieldRect;
                const QRect textRect = fieldRect.adjusted(Margin, Margin, -Margin, -Margin);
                if (field.hasHighlight) {
                    painter.setBrush(field.highlight);
                    painter.setPen(Qt::NoPen);
                    painter.drawRect(fieldRect);
                } else {
                    painter.setBrush(field.background);
                    painter.setPen(Qt::NoPen);
                    painter.drawRect(fieldRect);
                }
                painter.setPen(palette().text().color());
                painter.setFont(field.font);
                painter.drawText(textRect, alignment, field.text);
            }
        }
    }
    // paint the tracking row
    const int top = (rowCount() - 1) * FieldHeight;
    const QRect fieldRect(0, top, width(), height() - top);
    if (e->rect().contains(fieldRect)) {
        DataField field = m_defaultField;
        data(field, 0, rowCount() - 1);
        painter.setBrush(field.background);
        painter.setPen(Qt::NoPen);
        painter.drawRect(fieldRect);
    }
}

void TimeTrackingView::resizeEvent(QResizeEvent *)
{
    sizeHint(); // make sure cached values are updated
    m_taskSelector->resize(width() - 2*Margin, m_taskSelector->sizeHint().height());
    m_taskSelector->move(Margin, height() - Margin - m_taskSelector->height());
    m_elidedTexts.clear();
}

void TimeTrackingView::mousePressEvent(QMouseEvent *event)
{
    const int position = getSummaryAt(event->pos());
    if (position < 0)
        return;
    const TaskId id = m_summaries.at(position).task;

    if (!taskIsValidAndTrackable(id))
        return;

    if (!isTracking()) {
        if (position >= 0 && position < m_summaries.size())
            m_taskSelector->taskSelected(m_summaries.at(position));
    }
}

void TimeTrackingView::mouseDoubleClickEvent(QMouseEvent *event)
{   // we rely on the mouse press event that was received before the doubleclick!
    // start tracking
    const int position = getSummaryAt(event->pos());
    if (position < 0)
        return;

    const TaskId id = m_summaries.at(position).task;

    if (!taskIsValidAndTrackable(id))
        return;

    if (!DATAMODEL->isTaskActive(id)) {
        emit stopEvents();
        emit startEvent(id);
    } else {
        emit stopEvents();
    }
}

int TimeTrackingView::getSummaryAt(const QPoint &position)
{
    const int left = 0;
    const int right = taskColumnWidth();
    const int fieldIndex = position.y() / m_cachedTotalsFieldRect.height();
    const int taskIndex = fieldIndex - 1;
    if (taskIndex < 0 || taskIndex >= m_summaries.count())
        return -1;
    if (position.x() < left || position.x() > right)
        return -1;
    return taskIndex;
}

int TimeTrackingView::taskColumnWidth() const
{
    return width() - m_cachedTotalsFieldRect.width() - 7 * m_cachedDayFieldRect.width();
}

void TimeTrackingView::data(DataField &field, int column, int row) const
{
    const int HeaderRow = 0;
    const int TotalsRow = rowCount() - 2;
    const int TrackingRow = rowCount() - 1;
    const int TaskColumn = 0;
    const int TotalsColumn = columnCount() - 1;
    const int Day = column - 1;

    field.font = m_fixedFont;
    if (row == HeaderRow) {
        field.font = m_narrowFont;
        if (column == TaskColumn) {
            field.text = tr("Task");
        } else if (column == TotalsColumn) {
            field.text = tr("Total");
        } else {
            field.text = m_shortDayNames[ column - 1 ];
        }
        field.background = (Day % 2)
                           ? m_paintAttributes.headerBrush
                           : m_paintAttributes.headerEvenDayBrush;
    } else if (row == TotalsRow) {
        field.background = m_paintAttributes.totalsRowBrush;
        if (column == TaskColumn) {
            // field.text = tr( "Total" );
        } else if (column == TotalsColumn) {
            int total = 0;
            Q_FOREACH (const WeeklySummary &s, m_summaries)
                total += std::accumulate(s.durations.begin(), s.durations.end(), 0);
            field.text = hoursAndMinutes(total);
        } else {
            int total = 0;
            Q_FOREACH (const WeeklySummary &s, m_summaries)
                total += s.durations[Day];
            field.text = hoursAndMinutes(total);
            field.background = (Day % 2)
                               ? m_paintAttributes.totalsRowBrush
                               : m_paintAttributes.totalsRowEvenDayBrush;
        }
    } else if (row == TrackingRow) {
        // we only return one value, the paint method will treat this
        // column as a special case
        field.background = m_paintAttributes.taskBrushOdd;
        // field.text = tr( " 00:45 2345 KDAB/HR/Project Time Bookkeeping" );
    } else { // a task row
        field.background = row % 2 ? m_paintAttributes.taskBrushEven
                           : m_paintAttributes.taskBrushOdd;
        if (m_summaries.size() > row - 1) {
            const int index = row - 1; // index into summaries
            const bool active = DATAMODEL->isTaskActive(m_summaries[index].task);
            if (active) {
                field.hasHighlight = true;
                field.highlight = m_paintAttributes.halfHighlight;
            }
            int day = column - 1;
            if (column == TaskColumn) {
                field.text = DATAMODEL->taskIdAndSmartNameString(m_summaries[index].task);
                field.font = m_narrowFont;
            } else if (column == TotalsColumn) {
                const QVector<int> &durations = m_summaries[index].durations;
                const int total = std::accumulate(durations.begin(), durations.end(), 0);
                field.text = hoursAndMinutes(total);
            } else {
                int duration = m_summaries[index].durations[day];
                field.text = duration > 0 ? hoursAndMinutes(duration) : QString();
                // highlight today as well, with the half highlight:
                if (day == m_dayOfWeek -1) {
                    field.hasHighlight = true;
                    field.storeAsActive = active;
                    field.highlight
                        = active ? QBrush(m_paintAttributes.runningTaskColor) : m_paintAttributes.
                          halfHighlight;
                }
            }
        }
    }
}

void TimeTrackingView::setSummaries(const QVector<WeeklySummary> &summaries)
{
    m_activeFieldRects.clear();
    m_summaries = summaries;
    m_cachedMinimumSizeHint = QSize();
    m_cachedSizeHint = QSize();
    m_dayOfWeek = QDate::currentDate().dayOfWeek();
    m_elidedTexts.clear();
    updateGeometry();
    update();
    // populate menu:
    m_taskSelector->populate(m_summaries);
    // FIXME maybe remember last selected task
    handleActiveEvents();
    emit taskMenuChanged();
}

bool TimeTrackingView::isTracking() const
{
    return DATAMODEL->activeEventCount() > 0;
}

void TimeTrackingView::configurationChanged()
{
    m_fixedFont = font();
#ifdef Q_OS_OSX
    m_fixedFont.setFamily(QStringLiteral("Andale Mono"));
    m_fixedFont.setPointSize(11);
#endif

    switch (CONFIGURATION.timeTrackerFontSize) {
    case Configuration::TimeTrackerFont_Small:
        m_fixedFont.setPointSizeF(0.9 * m_fixedFont.pointSize());
        break;
    case Configuration::TimeTrackerFont_Regular:
        break;
    case Configuration::TimeTrackerFont_Large:
        m_fixedFont.setPointSizeF(1.2 * m_fixedFont.pointSize());
        break;
    }

    // re-populate menu:
    m_taskSelector->populate(m_summaries);
    emit taskMenuChanged();

    m_narrowFont = font(); // stay with the desktop
    m_narrowFont.setPointSize(m_fixedFont.pointSize());

    /* invalidate cache and force recalc */
    m_cachedSizeHint = QSize();
    m_cachedMinimumSizeHint = QSize();
    updateGeometry();
    sizeHint();

    /* force repaint */
    repaint();
}

void TimeTrackingView::handleActiveEvents()
{
    m_activeFieldRects.clear();
    Q_ASSERT(DATAMODEL->activeEventCount() >= 0);

    m_taskSelector->handleActiveEvents();
}

QString TimeTrackingView::elidedText(const QString &text, const QFont &font, int width)
{
    if (!m_elidedTexts.contains(text))
        m_elidedTexts.insert(text, Charm::elidedTaskName(text, font, width));
    Q_ASSERT(m_elidedTexts.contains(text));
    return m_elidedTexts.value(text);
}

void TimeTrackingView::slotUpdateSummaries()
{
    setSummaries(m_summaries);
}

bool TimeTrackingView::taskIsValidAndTrackable(int taskId)
{
    const Task &task = DATAMODEL->getTask(taskId);

    bool expired = !task.isCurrentlyValid();
    bool trackable = task.trackable();
    bool notTrackableAndExpired = (!trackable && expired);
    const auto id = QString::number(task.id());
    const QString name = task.name();
    QString expirationDate = QLocale::system().toString(task.validUntil(), QLocale::ShortFormat);

    if (!trackable || expired) {
        QString message = notTrackableAndExpired ? tr(
            "The task %1 (%2) is not trackable and expired since %3").arg(id, name, expirationDate)
                          : expired ? tr("The task %1 (%2) is expired since %3").arg(id, name,
                                                                                     expirationDate)
                          : tr("The task %1 (%2) is not trackable").arg(id, name);

        QMessageBox::information(this, tr("Please choose another task"), message);
        return false;
    }
    return true;
}