summary refs log tree commit diff
path: root/include/Utils.h
blob: fa825b7cd70215e01c45957a71277aa18c9e07a3 (plain) (blame)
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
#pragma once

#include "RoomInfoListItem.h"

#include <QDateTime>
#include <mtx/events/collections.hpp>

namespace utils {

using TimelineEvent = mtx::events::collections::TimelineEvents;

//! Human friendly timestamp representation.
QString
descriptiveTime(const QDateTime &then);

//! Generate a message description from the event to be displayed
//! in the RoomList.
DescInfo
getMessageDescription(const TimelineEvent &event, const QString &localUser);

//! Get the first character of a string, taking into account that
//! surrogate pairs might be in use.
QString
firstChar(const QString &input);

//! Get a human readable file size with the appropriate units attached.
QString
humanReadableFileSize(uint64_t bytes);

//! Scale down an image to fit to the given width & height limitations.
template<class ImageType>
ImageType
scaleDown(uint64_t max_width, uint64_t max_height, const ImageType &source)
{
        if (source.isNull())
                return source;

        auto width_ratio  = (double)max_width / (double)source.width();
        auto height_ratio = (double)max_height / (double)source.height();

        auto min_aspect_ratio = std::min(width_ratio, height_ratio);

        int final_width  = 0;
        int final_height = 0;

        if (min_aspect_ratio > 1) {
                final_width  = source.width();
                final_height = source.height();
        } else {
                final_width  = source.width() * min_aspect_ratio;
                final_height = source.height() * min_aspect_ratio;
        }

        return source.scaled(
          final_width, final_height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
}
}