File: TimeTrackingTaskSelector.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 (300 lines) | stat: -rw-r--r-- 10,344 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
/*
  TimeTrackingTaskSelector.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>
  Author: Montel Laurent <laurent.montel@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 "TimeTrackingTaskSelector.h"
#include "CommentEditorPopup.h"
#include "Data.h"
#include "SelectTaskDialog.h"
#include "ViewHelpers.h"

#include "Core/Configuration.h"
#include "Core/Event.h"
#include "Core/Task.h"

#include <QAction>
#include <QDialogButtonBox>
#include <QHBoxLayout>
#include <QMap>
#include <QMenu>
#include <QMessageBox>
#include <QPointer>
#include <QPushButton>
#include <QTextEdit>
#include <QToolButton>

#ifdef Q_OS_WIN
#include <QtWinExtras/QWinJumpList>
#include <QtWinExtras/QWinJumpListCategory>
#include <QtWinExtras/QWinThumbnailToolBar>
#include <QtWinExtras/QWinThumbnailToolButton>
#endif

namespace {
const static char CUSTOM_TASK_PROPERTY_NAME[] = "CUSTOM_TASK_PROPERTY";

static QString escapeAmpersands(QString text)
{
    text.replace(QLatin1String("&"), QLatin1String("&&"));
    return text;
}
}

TimeTrackingTaskSelector::TimeTrackingTaskSelector(QWidget *parent)
    : QWidget(parent)
    , m_stopGoButton(new QToolButton(this))
    , m_stopGoAction(new QAction(this))
    , m_editCommentButton(new QToolButton(this))
    , m_editCommentAction(new QAction(this))
    , m_taskSelectorButton(new QToolButton(this))
    , m_startOtherTaskAction(new QAction(tr("Start Other Task..."), this))
    , m_menu(new QMenu(tr("Start Task"), this))
{
    QLayout *hbox = new QHBoxLayout(this);
    hbox->addWidget(m_stopGoButton);
    hbox->addWidget(m_editCommentButton);
    hbox->addWidget(m_taskSelectorButton);
    hbox->setSpacing(1);
    hbox->setContentsMargins(0, 0, 0, 0);
    m_taskSelectorButton->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);

    m_stopGoAction->setText(tr("Start Task"));
    m_stopGoAction->setIcon(Data::goIcon());
    m_stopGoAction->setShortcut(QKeySequence(Qt::Key_Space));
    m_stopGoAction->setCheckable(true);
    connect(m_stopGoAction, &QAction::triggered,
            this, &TimeTrackingTaskSelector::slotGoStopToggled);
    m_stopGoButton->setDefaultAction(m_stopGoAction);

    m_editCommentAction->setText(tr("Edit Comment"));
    m_editCommentAction->setIcon(Data::editEventIcon());
    m_editCommentAction->setShortcut(Qt::Key_E);
    m_editCommentAction->setToolTip(m_editCommentAction->text());
    connect(m_editCommentAction, &QAction::triggered,
            this, &TimeTrackingTaskSelector::slotEditCommentClicked);
    m_editCommentButton->setDefaultAction(m_editCommentAction);

    m_taskSelectorButton->setPopupMode(QToolButton::InstantPopup);
    m_taskSelectorButton->setMenu(m_menu);
    m_taskSelectorButton->setText(tr("Select Task"));

    m_startOtherTaskAction->setShortcut(Qt::Key_T);
    connect(m_startOtherTaskAction, &QAction::triggered,
            this, &TimeTrackingTaskSelector::slotManuallySelectTask);
}

void TimeTrackingTaskSelector::populateEditMenu(QMenu *menu)
{
    menu->addAction(m_stopGoAction);
    menu->addAction(m_editCommentAction);
    menu->addAction(m_startOtherTaskAction);
}

QMenu *TimeTrackingTaskSelector::menu() const
{
    return m_menu;
}

void TimeTrackingTaskSelector::populate(const QVector<WeeklySummary> &summaries)
{
    Q_UNUSED(summaries);

    // Don't repopulate while the menu is displayed; very ugly and it can wait.
    if (m_menu->isActiveWindow())
        return;

    const auto createTaskAction = [this](TaskId id) {
        auto action = new QAction(DATAMODEL->taskIdAndSmartNameString(id), m_menu);
        action->setProperty(CUSTOM_TASK_PROPERTY_NAME, QVariant::fromValue(id));
        connect(action, &QAction::triggered, this, &TimeTrackingTaskSelector::slotActionSelected);
        return action;
    };

    if (m_taskManuallySelected) {
        m_taskManuallySelected = false;
        createTaskAction(m_manuallySelectedTask)->trigger();
        return;
    }

    m_menu->clear();

    const TaskIdList interestingTasksToAdd = DATAMODEL->mostRecentlyUsedTasks();

    const int maxEntries = qMin(interestingTasksToAdd.size(),
                                CONFIGURATION.numberOfTaskSelectorEntries);
    for (int i = 0; i < maxEntries; ++i)
        m_menu->addAction(createTaskAction(interestingTasksToAdd.at(i)));
    m_menu->addSeparator();
    m_menu->addAction(m_startOtherTaskAction);
    m_taskSelectorButton->setDisabled(m_menu->actions().isEmpty());
}

void TimeTrackingTaskSelector::slotEditCommentClicked()
{
    const EventIdList events = DATAMODEL->activeEvents();
    Q_ASSERT(events.size() == 1);
    CommentEditorPopup popup;
    popup.loadEvent(events.first());
    popup.exec();
}

void TimeTrackingTaskSelector::handleActiveEvents()
{
    const int activeEventCount = DATAMODEL->activeEventCount();
    if (activeEventCount > 1) {
        m_stopGoAction->setIcon(Data::goIcon());
        m_stopGoAction->setText(tr("Start Task"));
        m_stopGoAction->setEnabled(false);
        m_stopGoAction->setChecked(true);
        m_editCommentAction->setEnabled(false);
    } else if (activeEventCount == 1) {
        m_stopGoAction->setIcon(Data::stopIcon());
        m_stopGoAction->setText(tr("Stop Task"));
        m_stopGoAction->setEnabled(true);
        m_stopGoAction->setChecked(true);
        m_editCommentAction->setEnabled(true);

        const Event &event = DATAMODEL->eventForId(DATAMODEL->activeEvents().first());
        const Task &task = DATAMODEL->getTask(event.taskId());
        m_taskSelectorButton->setText(escapeAmpersands(DATAMODEL->taskIdAndSmartNameString(
                                                           task.id())));
    } else {
        m_stopGoAction->setIcon(Data::goIcon());
        m_stopGoAction->setText(tr("Start Task"));
        if (m_selectedTask != 0) {
            const Task &task = DATAMODEL->getTask(m_selectedTask);
            m_stopGoAction->setEnabled(task.isCurrentlyValid());
        } else {
            m_stopGoAction->setEnabled(false);
        }
        m_stopGoAction->setChecked(false);
        m_editCommentAction->setEnabled(false);
    }

    updateThumbBar();
}

void TimeTrackingTaskSelector::slotActionSelected()
{
    auto action = qobject_cast<QAction *>(sender());
    const TaskId taskId = action->property(CUSTOM_TASK_PROPERTY_NAME).value<TaskId>();
    const Task &task = DATAMODEL->getTask(taskId);

    Q_ASSERT(task.isValid());
    if (!task.isValid())
        return;

    const bool expired = !task.isCurrentlyValid();
    const bool trackable = task.trackable();
    if (!trackable || expired) {
        const bool notTrackableAndExpired = (!trackable && expired);
        const QString expirationDate = QLocale::system().toString(task.validUntil().date(), QLocale::ShortFormat);
        const auto taskName = DATAMODEL->taskIdAndSmartNameString(task.id());

        const auto reason = notTrackableAndExpired
                ? tr("The task is not trackable and expired since %1.").arg(expirationDate)
                : expired ? tr("The task is expired since %1").arg(expirationDate)
                          : tr("The task is not trackable");

        const auto message = tr("Cannot select task <strong>%1</strong>: %2. Please choose another task.").arg(taskName.toHtmlEscaped(), reason);

        QMessageBox::information(this, tr("Please choose another task"), message);
        return;
    }
    taskSelected(taskId);
    handleActiveEvents();

    if (!DATAMODEL->isTaskActive(taskId)) {
        if (!DATAMODEL->activeEvents().isEmpty())
            emit stopEvents();
        emit startEvent(taskId);
    }
}

void TimeTrackingTaskSelector::updateThumbBar()
{
#ifdef Q_OS_WIN
    if (!m_stopGoThumbButton && window()->windowHandle()) {
        QWinThumbnailToolBar *toolBar = new QWinThumbnailToolBar(this);
        toolBar->setWindow(window()->windowHandle());

        m_stopGoThumbButton = new QWinThumbnailToolButton(toolBar);
        toolBar->addButton(m_stopGoThumbButton);
        connect(m_stopGoThumbButton, &QWinThumbnailToolButton::clicked, [this](){
            slotGoStopToggled(!m_stopGoButton->isChecked());
        });
    }
    if (m_stopGoThumbButton) {
        if (m_stopGoButton->isChecked()) {
            m_stopGoThumbButton->setToolTip(tr("Stop Task"));
            m_stopGoThumbButton->setIcon(Data::stopIcon());
        } else {
            m_stopGoThumbButton->setToolTip(tr("Start Task"));
            m_stopGoThumbButton->setIcon(Data::goIcon());
        }
        m_stopGoThumbButton->setEnabled(m_stopGoButton->isEnabled());
    }
#endif
}

void TimeTrackingTaskSelector::taskSelected(TaskId id)
{
    m_selectedTask = id;
    m_stopGoAction->setEnabled(true);
    const auto taskname = DATAMODEL->taskIdAndSmartNameString(id);
    m_taskSelectorButton->setText(escapeAmpersands(taskname));
}

void TimeTrackingTaskSelector::slotGoStopToggled(bool on)
{
    if (on) {
        Q_ASSERT(m_selectedTask);
        emit startEvent(m_selectedTask);
    } else {
        emit stopEvents();
    }
}

void TimeTrackingTaskSelector::taskSelected(const WeeklySummary &summary)
{
    taskSelected(summary.task);
}

void TimeTrackingTaskSelector::slotManuallySelectTask()
{
    SelectTaskDialog dialog(this);
    if (!dialog.exec())
        return;
    m_manuallySelectedTask = dialog.selectedTask();
    if (m_selectedTask <= 0)
        m_selectedTask = m_manuallySelectedTask;
    m_taskManuallySelected = true;
    handleActiveEvents();
    emit updateSummariesPlease();
}

void TimeTrackingTaskSelector::showEvent(QShowEvent *e)
{
    updateThumbBar();
    QWidget::showEvent(e);
}