aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/componentcore/zoomaction.cpp
blob: c3373f0e519eb975674519899877159c0805d7b4 (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
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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "zoomaction.h"
#include "formeditorwidget.h"
#include <algorithm>
#include <iterator>
#include <utility>

#include <QAbstractItemView>
#include <QComboBox>
#include <QToolBar>

#include <cmath>

namespace QmlDesigner {

// Order matters!
std::array<double, 27> ZoomAction::m_zooms = {
    0.01, 0.02, 0.05, 0.0625, 0.1, 0.125, 0.2, 0.25, 0.33, 0.5,  0.66, 0.75, 0.9,
    1.0, 1.1, 1.25, 1.33, 1.5, 1.66, 1.75, 2.0, 3.0, 4.0, 6.0, 8.0, 10.0, 16.0
};

bool isValidIndex(int index)
{
    if (index >= 0 && index < static_cast<int>(ZoomAction::zoomLevels().size()))
        return true;

    return false;
}

ZoomAction::ZoomAction(QObject *parent)
    : QWidgetAction(parent)
    , m_combo(nullptr)
{}

std::array<double, 27> ZoomAction::zoomLevels()
{
    return m_zooms;
}

int ZoomAction::indexOf(double zoom)
{
    auto finder = [zoom](double val) { return qFuzzyCompare(val, zoom); };
    if (auto iter = std::find_if(m_zooms.begin(), m_zooms.end(), finder); iter != m_zooms.end())
        return static_cast<int>(std::distance(m_zooms.begin(), iter));

    return -1;
}

void ZoomAction::setZoomFactor(double zoom)
{
    if (int index = indexOf(zoom); index >= 0) {
        m_combo->setCurrentIndex(index);
        m_combo->setToolTip(m_combo->currentText());
        return;
    }
    int rounded = static_cast<int>(std::round(zoom * 100));
    m_combo->setEditable(true);
    m_combo->setEditText(QString::number(rounded) + " %");
    m_combo->setToolTip(m_combo->currentText());
}

double ZoomAction::setNextZoomFactor(double zoom)
{
    if (zoom >= m_zooms.back())
        return zoom;

    auto greater = [zoom](double val) { return val > zoom; };
    if (auto iter = std::find_if(m_zooms.begin(), m_zooms.end(), greater); iter != m_zooms.end()) {
        auto index = std::distance(m_zooms.begin(), iter);
        m_combo->setCurrentIndex(static_cast<int>(index));
        m_combo->setToolTip(m_combo->currentText());
        return *iter;
    }
    return zoom;
}

double ZoomAction::setPreviousZoomFactor(double zoom)
{
    if (zoom <= m_zooms.front())
        return zoom;

    auto smaller = [zoom](double val) { return val < zoom; };
    if (auto iter = std::find_if(m_zooms.rbegin(), m_zooms.rend(), smaller); iter != m_zooms.rend()) {
        auto index = std::distance(iter, m_zooms.rend() - 1);
        m_combo->setCurrentIndex(static_cast<int>(index));
        m_combo->setToolTip(m_combo->currentText());
        return *iter;
    }
    return zoom;
}

bool parentIsToolBar(QWidget *parent)
{
    return qobject_cast<QToolBar *>(parent) != nullptr;
}

QComboBox *createZoomComboBox(QWidget *parent)
{
    auto *combo = new QComboBox(parent);
    for (double z : ZoomAction::zoomLevels()) {
        const QString name = QString::number(z * 100., 'g', 4) + " %";
        combo->addItem(name, z);
    }
    return combo;
}

QWidget *ZoomAction::createWidget(QWidget *parent)
{
    if (!m_combo && parentIsToolBar(parent)) {
        m_combo = createZoomComboBox(parent);
        m_combo->setProperty("hideborder", true);
        m_combo->setCurrentIndex(indexOf(1.0));
        m_combo->setToolTip(m_combo->currentText());

        connect(m_combo, &QComboBox::currentIndexChanged, this, &ZoomAction::emitZoomLevelChanged);

        return m_combo.data();
    }
    return nullptr;
}

void ZoomAction::emitZoomLevelChanged(int index)
{
    if (index >= 0 && index < static_cast<int>(m_zooms.size()))
        emit zoomLevelChanged(m_zooms[static_cast<size_t>(index)]);
}

} // namespace QmlDesigner