summaryrefslogtreecommitdiffstats
path: root/tests/manual/qglyphruns/controller.cpp
blob: d63c23de32f7ee96530dd161d72b12f5a836bb45 (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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "controller.h"
#include "ui_controller.h"

Controller::Controller(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Controller)
{
    ui->setupUi(this);

    connect(ui->cbWrap, &QCheckBox::toggled, this, &Controller::updateViews);
    connect(ui->gbRange, &QGroupBox::toggled, this, &Controller::updateViews);
    connect(ui->teSourceString, &QPlainTextEdit::textChanged, this, &Controller::updateViews);
    connect(ui->hsLineWidth, &QSlider::valueChanged, this, &Controller::updateViews);
    connect(ui->inspector, &GlyphRunInspector::updateBounds, ui->view, &View::setVisualizedBounds);
    connect(ui->sbFrom, &QSpinBox::valueChanged, this, &Controller::updateViews);
    connect(ui->sbTo, &QSpinBox::valueChanged, this, &Controller::updateViews);
    connect(ui->teSourceString, &QPlainTextEdit::selectionChanged, this, &Controller::updateRange);
    connect(ui->fcbFont, &QFontComboBox::currentFontChanged, this, &Controller::updateViews);
}

Controller::~Controller()
{
    delete ui;
}

void Controller::updateRange()
{
    if (ui->gbRange->isChecked()) {
        QTextCursor cursor = ui->teSourceString->textCursor();
        if (cursor.hasSelection()) {
            ui->sbFrom->setValue(cursor.selectionStart());
            ui->sbTo->setValue(cursor.selectionEnd() - 1);

            updateViews();
        }
    }
}

void Controller::updateViews()
{
    QString s = ui->teSourceString->toPlainText();
    ui->sbFrom->setMaximum(s.length());
    ui->sbTo->setMaximum(s.length());

    s.replace('\n', QChar::LineSeparator);
    ui->view->updateLayout(s,
                           qreal(ui->hsLineWidth->value()) * ui->hsLineWidth->width() / 100.0f,
                           ui->cbWrap ? QTextOption::WrapAtWordBoundaryOrAnywhere : QTextOption::ManualWrap,
                           ui->fcbFont->currentFont());

    int start = ui->gbRange->isChecked() ? ui->sbFrom->value() : -1;
    int length = ui->gbRange->isChecked() ? ui->sbTo->value() - start + 1 : -1;
    ui->inspector->updateLayout(ui->view->layout(), start, length);
}