File: CharmConstants.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 (132 lines) | stat: -rw-r--r-- 6,236 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
/*
  CharmConstants.cpp

  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: Olivier JG <olivier.de.gaalon@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 "CharmConstants.h"
#include "CharmDataModel.h"
#include "Controller.h"

#include <QObject>
#include <QTextStream>

const QString MetaKey_EventsInLeafsOnly = QStringLiteral("EventsInLeafsOnly");
const QString MetaKey_OneEventAtATime = QStringLiteral("OneEventAtATime");
const QString MetaKey_MainWindowGeometry = QStringLiteral("MainWindowGeometry");
const QString MetaKey_MainWindowVisible = QStringLiteral("MainWindowVisible");
const QString MetaKey_MainWindowGUIStateSelectedTask = QStringLiteral(
    "MainWindowGUIStateSelectedTask");
const QString MetaKey_MainWindowGUIStateExpandedTasks = QStringLiteral(
    "MainWindowGUIStateExpandedTasks");
const QString MetaKey_MainWindowGUIStateShowExpiredTasks = QStringLiteral(
    "MainWindowGUIStateShowExpiredTasks");
const QString MetaKey_MainWindowGUIStateShowCurrentTasks = QStringLiteral(
    "MainWindowGUIStateShowCurrentTasks");
const QString MetaKey_TimeTrackerGeometry = QStringLiteral("TimeTrackerGeometry");
const QString MetaKey_TimeTrackerVisible = QStringLiteral("TimeTrackerVisible");
const QString MetaKey_EventEditorGeometry = QStringLiteral("EventEditorGeometry");
const QString MetaKey_TaskEditorGeometry = QStringLiteral("TaskEditorGeometry");
const QString MetaKey_ReportsRecentSavePath = QStringLiteral("ReportsRecentSavePath");
const QString MetaKey_ExportToXmlRecentSavePath = QStringLiteral("ExportToXmlSavePath");
const QString MetaKey_TimesheetSubscribedOnly = QStringLiteral("TimesheetSubscribedOnly");
const QString MetaKey_TimesheetActiveOnly = QStringLiteral("TimesheetActiveOnly");
const QString MetaKey_TimesheetRootTask = QStringLiteral("TimesheetRootTask");
const QString MetaKey_LastEventEditorDateTime = QStringLiteral("LastEventEditorDateTime");
const QString MetaKey_Key_InstallationId = QStringLiteral("InstallationId");
const QString MetaKey_Key_UserName = QStringLiteral("UserName");
const QString MetaKey_Key_UserId = QStringLiteral("UserId");
const QString MetaKey_Key_LocalStorageDatabase = QStringLiteral("LocalStorageDatabase");
const QString MetaKey_Key_LocalStorageType = QStringLiteral("LocalStorageType");
const QString MetaKey_Key_SubscribedTasksOnly = QStringLiteral("SubscribedTasksOnly");
const QString MetaKey_Key_TimeTrackerFontSize = QStringLiteral("TimeTrackerFontSize");
const QString MetaKey_Key_24hEditing = QStringLiteral("Key24hEditing");
const QString MetaKey_Key_DurationFormat = QStringLiteral("DurationFormat");
const QString MetaKey_Key_IdleDetection = QStringLiteral("IdleDetection");
const QString MetaKey_Key_WarnUnuploadedTimesheets = QStringLiteral("WarnUnuploadedTimesheets");
const QString MetaKey_Key_RequestEventComment = QStringLiteral("RequestEventComment");
const QString MetaKey_Key_ToolButtonStyle = QStringLiteral("ToolButtonStyle");
const QString MetaKey_Key_ShowStatusBar = QStringLiteral("ShowStatusBar");
const QString MetaKey_Key_EnableCommandInterface = QStringLiteral("EnableCommandInterface");
const QString MetaKey_Key_NumberOfTaskSelectorEntries = QStringLiteral("NumberOfTaskSelectorEntries");

const QString TrueString(QStringLiteral("true"));
const QString FalseString(QStringLiteral("false"));

const QString &stringForBool(bool val)
{
    return val ? TrueString : FalseString;
}

void connectControllerAndModel(Controller *controller, CharmDataModel *model)
{
    QObject::connect(controller, SIGNAL(eventAdded(Event)),
                     model, SLOT(addEvent(Event)));
    QObject::connect(controller, SIGNAL(eventModified(Event)),
                     model, SLOT(modifyEvent(Event)));
    QObject::connect(controller, SIGNAL(eventDeleted(Event)),
                     model, SLOT(deleteEvent(Event)));
    QObject::connect(controller, SIGNAL(allEvents(EventList)),
                     model, SLOT(setAllEvents(EventList)));
    QObject::connect(controller, SIGNAL(definedTasks(TaskList)),
                     model, SLOT(setAllTasks(TaskList)));
    QObject::connect(controller, SIGNAL(taskAdded(Task)),
                     model, SLOT(addTask(Task)));
    QObject::connect(controller, SIGNAL(taskUpdated(Task)),
                     model, SLOT(modifyTask(Task)));
    QObject::connect(controller, SIGNAL(taskDeleted(Task)),
                     model, SLOT(deleteTask(Task)));
}

static QString formatDecimal(double d)
{
    const QString s = QLocale::system().toString(d, 'f', 2);
    if (d > -10 && d < 10) { //hack to get the hours always have two decimals: e.g. 00.50 instead of 0.50
        return QStringLiteral("0") + s;
    } else {
        return s;
    }
}

QString hoursAndMinutes(int duration)
{
    if (duration == 0) {
        if (CONFIGURATION.durationFormat == Configuration::Minutes) {
            return QObject::tr("00:00");
        } else {
            return formatDecimal(0.0);
        }
    }
    int minutes = duration / 60;
    int hours = minutes / 60;
    minutes = minutes % 60;

    if (CONFIGURATION.durationFormat == Configuration::Minutes) {
        QString text;
        QTextStream stream(&text);
        stream << qSetFieldWidth(2) << qSetPadChar(QLatin1Char('0'))
               << hours << qSetFieldWidth(0) << ":" << qSetFieldWidth(2) << minutes;
        return text;
    } else { //Decimal
        return formatDecimal(hours + minutes / 60.0);
    }
}