File: CharmDataModel.h

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 (186 lines) | stat: -rw-r--r-- 6,594 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
/*
  CharmDataModel.h

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

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

  Author: Mirko Boehm <mirko.boehm@kdab.com>
  Author: Frank Osterfeld <frank.osterfeld@kdab.com>
  Author: Allen Winter <allen.winter@kdab.com>
  Author: David Faure <david.faure@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/>.
*/

#ifndef CHARMDATAMODEL_H
#define CHARMDATAMODEL_H

#include <QObject>
#include <QTimer>

#include "Task.h"
#include "State.h"
#include "Event.h"
#include "TimeSpans.h"
#include "TaskTreeItem.h"
#include "CharmDataModelAdapterInterface.h"
#include "SmartNameCache.h"

class QAbstractItemModel;

/** CharmDataModel is the application's model.
    CharmDataModel holds all data that makes up the application's
    current data space: the list of tasks, the list of events, and the
    list of active (currently timed) events.
    It will notify all registered CharmDataModelAdapterInterfaces
    about changes in the model. Those interfaces could, for example,
    implement QAbstractItemModel.
*/
class CharmDataModel : public QObject
{
    Q_OBJECT
    friend class ImportExportTests;

public:
    CharmDataModel();
    ~CharmDataModel() override;

    void stateChanged(State previous, State next);
    /** Register a CharmDataModelAdapterInterface. */
    void registerAdapter(CharmDataModelAdapterInterface *);
    /** Unregister a CharmDataModelAdapterInterface. */
    void unregisterAdapter(CharmDataModelAdapterInterface *);

    /** Retrieve a task for the given task id.
        If called with Zero as the task id, it will return the
        imaginary root that has all top-levels as it's children.
    */
    const TaskTreeItem &taskTreeItem(TaskId id) const;
    /** Convenience method: retrieve the task directly. */
    const Task &getTask(TaskId id) const;
    /** Get all tasks as a TaskList.
        Warning: this might be slow. */
    TaskList getAllTasks() const;
    /** Retrieve an event for the given event id. */
    const Event &eventForId(EventId id) const;
    /** Constant access to the map of events. */
    const EventMap &eventMap() const;
    /**
     * Get all events that start in a given time frame (e.g. a given day, a given week etc.)
     * More precisely, all events that start at or after @p start, and start before @p end (@p end excluded!)
     */
    EventIdList eventsThatStartInTimeFrame(const QDate &start, const QDate &end) const;
    // convenience overload
    EventIdList eventsThatStartInTimeFrame(const TimeSpan &timeSpan) const;
    const Event &activeEventFor(TaskId id) const;
    EventIdList activeEvents() const;
    int activeEventCount() const;
    TaskTreeItem &parentItem(const Task &task);   // FIXME const???
    bool taskExists(TaskId id);
    /** True if task is in the subtree below parent.
     * parent is not element of the subtree, and thus not it's own child. */
    bool isParentOf(TaskId parent, TaskId task) const;

    // handling of active events:
    /** Is an event active for the task with this id? */
    bool isTaskActive(TaskId id) const;
    /** Is this event active? */
    bool isEventActive(EventId id) const;
    /** Start a new event with this task. */
    void startEventRequested(const Task &);
    /** Stop the active event for this task. */
    void endEventRequested(const Task &);
    /** Stop all tasks. */
    void endAllEventsRequested();
    /** Activate this event. */
    bool activateEvent(const Event &);

    /** Provide a list of the most frequently used tasks.
      * Only tasks that have been used so far will be taken into account, so the list might be empty. */
    TaskIdList mostFrequentlyUsedTasks() const;
    /** Provide a list of the most recently used tasks.
      * Only tasks that have been used so far will be taken into account, so the list might be empty. */
    TaskIdList mostRecentlyUsedTasks() const;

    /** Create a full task name from the specified TaskId. */
    QString fullTaskName(const Task &) const;

    /** Create a "smart" task name (name and shortest path that makes the name unique) from the specified TaskId. */
    QString smartTaskName(const Task &) const;

    /** Get the task id and full name as a single string. */
    QString taskIdAndFullNameString(TaskId id) const;

    /** Get the task id and name as a single string. */
    QString taskIdAndNameString(TaskId id) const;

    /** Get the task id and smart name as a single string. */
    QString taskIdAndSmartNameString(TaskId id) const;

    bool operator==(const CharmDataModel &other) const;

Q_SIGNALS:
    // these need to be implemented in the respective application to
    // be able to track time:
    void makeAndActivateEvent(const Task &);
    void requestEventModification(const Event &, const Event &);
    void sysTrayUpdate(const QString &, bool);
    void resetGUIState();

public Q_SLOTS:
    void setAllTasks(const TaskList &tasks);
    void addTask(const Task &);
    void modifyTask(const Task &);
    void deleteTask(const Task &);
    void clearTasks();

    void setAllEvents(const EventList &events);
    void addEvent(const Event &);
    void modifyEvent(const Event &);
    void deleteEvent(const Event &);
    void clearEvents();

private:
    void determineTaskPaddingLength();
    bool eventExists(EventId id);

    Task &findTask(TaskId id);
    Event &findEvent(EventId id);

    int totalDuration() const;
    QString eventsString() const;
    QString totalDurationString() const;
    void updateToolTip();

    TaskTreeItem::Map m_tasks;
    TaskTreeItem m_rootItem;

    EventMap m_events;
    EventIdList m_activeEventIds;
    // adapters are notified when the model changes
    CharmDataModelAdapterList m_adapters;

    // event update timer:
    QTimer m_timer;
    SmartNameCache m_nameCache;

private Q_SLOTS:
    void eventUpdateTimerEvent();

private:
    // functions only used for testing:
    CharmDataModel *clone() const;
};
#endif