File: ViewHelpers.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 (185 lines) | stat: -rw-r--r-- 6,038 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
/*
  ViewHelpers.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>

  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 "ViewHelpers.h"

#include <QtAlgorithms>
#include <QFile>
#include <QCollator>

namespace {
static QCollator collator()
{
    QCollator c;
    c.setCaseSensitivity(Qt::CaseInsensitive);
    c.setNumericMode(true);
    return c;
}
}

void Charm::connectControllerAndView(Controller *controller, CharmWindow *view)
{
    // connect view and controller:
    // make controller process commands send by the view:
    QObject::connect(view, SIGNAL(emitCommand(CharmCommand*)),
                     controller, SLOT(executeCommand(CharmCommand*)));
    QObject::connect(view, SIGNAL(emitCommandRollback(CharmCommand*)),
                     controller, SLOT(rollbackCommand(CharmCommand*)));
    // make view receive done commands from the controller:
    QObject::connect(controller, SIGNAL(commandCompleted(CharmCommand*)),
                     view, SLOT(commitCommand(CharmCommand*)));
}

class EventSorter
{
public:
    EventSorter(const Charm::SortOrderList &orders)
        : m_orders(orders)
    {
        Q_ASSERT(!m_orders.contains(Charm::SortOrder::None));
        Q_ASSERT(!m_orders.isEmpty());
    }

    template<typename T>
    int compare(const T &left, const T &right) const
    {
        if (left < right) {
            return -1;
        } else if (left > right) {
            return 1;
        }
        return 0;
    }

    bool operator()(const EventId &leftId, const EventId &rightId) const
    {
        const Event &left = DATAMODEL->eventForId(leftId);
        const Event &right = DATAMODEL->eventForId(rightId);
        int result = -1;

        foreach (const auto order, m_orders) {
            switch (order) {
            case Charm::SortOrder::None:
                Q_UNREACHABLE();

            case Charm::SortOrder::StartTime:
                result = compare(left.startDateTime(), right.startDateTime());
                break;

            case Charm::SortOrder::EndTime:
                result = compare(left.endDateTime(), right.endDateTime());
                break;

            case Charm::SortOrder::TaskId:
                result = compare(left.taskId(), right.taskId());
                break;

            case Charm::SortOrder::Comment:
                result = Charm::collatorCompare(left.comment(), right.comment());
                break;
            }

            if (result != 0)
                break;

            result = -1;
        }

        return result < 0;
    }

private:
    const Charm::SortOrderList &m_orders;
};

int Charm::collatorCompare(const QString &left, const QString &right)
{
    static const auto collator(::collator());
    return collator.compare(left, right);
}

EventIdList Charm::eventIdsSortedBy(EventIdList ids, const Charm::SortOrderList &orders)
{
    if (!orders.isEmpty())
        qStableSort(ids.begin(), ids.end(), EventSorter(orders));

    return ids;
}

EventIdList Charm::eventIdsSortedBy(EventIdList ids, SortOrder order)
{
    return eventIdsSortedBy(ids, SortOrderList(1) << order);
}

EventIdList Charm::filteredBySubtree(EventIdList ids, TaskId parent, bool exclude)
{
    EventIdList result;
    bool isParent = false;
    Q_FOREACH (EventId id, ids) {
        const Event &event = DATAMODEL->eventForId(id);
        isParent = (parent == event.taskId() || DATAMODEL->isParentOf(parent, event.taskId()));
        if (isParent != exclude)
            result << id;
    }
    return result;
}

QString Charm::elidedTaskName(const QString &text, const QFont &font, int width)
{
    QFontMetrics metrics(font);
    const QString &projectCode
        = text.section(QLatin1Char(' '), 0, 0, QString::SectionIncludeTrailingSep);
    const int projectCodeWidth = metrics.width(projectCode);
    if (width > projectCodeWidth) {
        const QString &taskName = text.section(QLatin1Char(' '), 1);
        const int taskNameWidth = width - projectCodeWidth;
        const QString &taskNameElided
            = metrics.elidedText(taskName, Qt::ElideLeft, taskNameWidth);
        return projectCode + taskNameElided;
    }

    return metrics.elidedText(text, Qt::ElideMiddle, width);
}

QString Charm::reportStylesheet(const QPalette &palette)
{
    QString style;
    QFile stylesheet(QStringLiteral(":/Charm/report_stylesheet.sty"));
    if (stylesheet.open(QIODevice::ReadOnly | QIODevice::Text)) {
        style = QString::fromUtf8(stylesheet.readAll());
        style.replace(QLatin1String("@header_row_background_color@"),
                      palette.highlight().color().name());
        style.replace(QLatin1String("@header_row_foreground_color@"),
                      palette.highlightedText().color().name());
        style.replace(QLatin1String("@alternate_row_background_color@"),
                      palette.alternateBase().color().name());
        style.replace(QLatin1String("@event_attributes_row_background_color@"),
                      palette.midlight().color().name());
        if (style.isEmpty())
            qWarning() << "reportStylesheet: default style sheet is empty, too bad";
    } else {
        qCritical() << "reportStylesheet: cannot load report style sheet:"
                    << stylesheet.errorString();
    }
    return style;
}