summaryrefslogtreecommitdiffstats
path: root/tests/manual/rhi/rhiwidgetproto/main.cpp
blob: a3b174185566d88b62612140ef07cf45fe9412a2 (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
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QApplication>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QSlider>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QCheckBox>
#include <QFileDialog>
#include "examplewidget.h"

static const bool TEST_OFFSCREEN_GRAB = false;

int main(int argc, char **argv)
{
    qputenv("QSG_INFO", "1");
    QApplication app(argc, argv);

    QVBoxLayout *layout = new QVBoxLayout;

    QLineEdit *edit = new QLineEdit(QLatin1String("Text on cube"));
    QSlider *slider = new QSlider(Qt::Horizontal);
    ExampleRhiWidget *rw = new ExampleRhiWidget;

    QObject::connect(edit, &QLineEdit::textChanged, edit, [edit, rw] {
        rw->setCubeTextureText(edit->text());
    });

    slider->setMinimum(0);
    slider->setMaximum(360);
    QObject::connect(slider, &QSlider::valueChanged, slider, [slider, rw] {
        rw->setCubeRotation(slider->value());
    });

    QPushButton *btn = new QPushButton(QLatin1String("Grab to image"));
    QObject::connect(btn, &QPushButton::clicked, btn, [rw] {
        QImage image = rw->grabTexture();
        qDebug() << image;
        if (!image.isNull()) {
            QFileDialog fd(rw->parentWidget());
            fd.setAcceptMode(QFileDialog::AcceptSave);
            fd.setDefaultSuffix("png");
            fd.selectFile("test.png");
            if (fd.exec() == QDialog::Accepted)
                image.save(fd.selectedFiles().first());
        }
    });
    QHBoxLayout *btnLayout = new QHBoxLayout;
    btnLayout->addWidget(btn);
    QCheckBox *cbExplicitSize = new QCheckBox(QLatin1String("Use explicit size"));
    QObject::connect(cbExplicitSize, &QCheckBox::stateChanged, cbExplicitSize, [cbExplicitSize, rw] {
        if (cbExplicitSize->isChecked())
            rw->setExplicitSize(QSize(128, 128));
        else
            rw->setExplicitSize(QSize());
    });
    btnLayout->addWidget(cbExplicitSize);
    QPushButton *btnMakeWindow = new QPushButton(QLatin1String("Make top-level window"));
    QObject::connect(btnMakeWindow, &QPushButton::clicked, btnMakeWindow, [rw, btnMakeWindow, layout] {
        if (rw->parentWidget()) {
            rw->setParent(nullptr);
            rw->setAttribute(Qt::WA_DeleteOnClose, true);
            rw->show();
            btnMakeWindow->setText(QLatin1String("Make child widget"));
        } else {
            rw->setAttribute(Qt::WA_DeleteOnClose, false);
            layout->addWidget(rw);
            btnMakeWindow->setText(QLatin1String("Make top-level window"));
        }
    });
    btnLayout->addWidget(btnMakeWindow);

    layout->addWidget(edit);
    QHBoxLayout *sliderLayout = new QHBoxLayout;
    sliderLayout->addWidget(new QLabel(QLatin1String("Cube rotation")));
    sliderLayout->addWidget(slider);
    layout->addLayout(sliderLayout);
    layout->addLayout(btnLayout);
    layout->addWidget(rw);

    rw->setCubeTextureText(edit->text());

    if (TEST_OFFSCREEN_GRAB) {
        rw->resize(320, 200);
        rw->grabTexture().save("offscreen_grab.png");
    }

    QWidget w;
    w.setLayout(layout);
    w.resize(1280, 720);
    w.show();

    return app.exec();
}