summaryrefslogtreecommitdiffstats
path: root/ui/qt/widgets/qcp_axis_ticker_elided.cpp
blob: 154a750ad5bccc4a7f0fce30701969d4d0237bee (plain)
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
/** @file
 *
 * QCustomPlot QCPAxisTickerText subclass that elides labels to the
 * width of the parent QCPAxis's QCPAxisRect's margin for the appropriate
 * side, for use when the margin is fixed.
 *
 * Copyright 2024 John Thacker <johnthacker@gmail.com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include <ui/qt/widgets/qcp_axis_ticker_elided.h>

#include <QFontMetrics>

QCPAxisTickerElided::QCPAxisTickerElided(QCPAxis *parent) :
    mParent(parent)
{
}

QCP::MarginSide QCPAxisTickerElided::axisTypeToMarginSide(const QCPAxis::AxisType axis)
{
    switch (axis) {
    case QCPAxis::atLeft:
        return QCP::msLeft;
    case QCPAxis::atRight:
        return QCP::msRight;
    case QCPAxis::atTop:
        return QCP::msTop;
    case QCPAxis::atBottom:
        return QCP::msBottom;
    }
    return QCP::msNone;
}

QString QCPAxisTickerElided::elidedText(const QString& text)
{
    QCP::MarginSides autoMargins = mParent->axisRect()->autoMargins();
    if (autoMargins & axisTypeToMarginSide(mParent->axisType())) {
        return text;
    }
    int elide_w;
    QMargins margins = mParent->axisRect()->margins();
    switch (mParent->axisType()) {
    case QCPAxis::atLeft:
        elide_w = margins.left();
        break;
    case QCPAxis::atRight:
        elide_w = margins.right();
        break;
    case QCPAxis::atTop:
        elide_w = margins.top();
        break;
    case QCPAxis::atBottom:
        elide_w = margins.bottom();
        break;
    default:
        // ??
        elide_w = margins.left();
    }

    return QFontMetrics(mParent->tickLabelFont()).elidedText(text,
                                                             Qt::ElideRight,
                                                             elide_w);
}

QString QCPAxisTickerElided::getTickLabel(double tick, const QLocale& , QChar , int)
{
    return elidedText(mTicks.value(tick));
}