summaryrefslogtreecommitdiffstats
path: root/tests/manual/stereographicsview/mainwindow.cpp
blob: 86269b096c0d656da3a142b0ba8b165b48c8c968 (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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QPushButton>
#include <QSlider>
#include <QLabel>
#include <QGraphicsEllipseItem>
#include <QRandomGenerator>
#include <QOpenGLWidget>

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

    ui->graphicsView->setViewport(new QOpenGLWidget);
    ui->graphicsView->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);

    QRect rect = ui->graphicsView->rect();
    QGraphicsScene *m_scene = new QGraphicsScene(rect);
    ui->graphicsView->setScene(m_scene);

    QSize initialSize(150, 150);
    m_ellipse = m_scene->addEllipse(
        rect.width() / 2 - initialSize.width() / 2,
        rect.height() / 2 - initialSize.height() / 2,
        initialSize.width(),
        initialSize.height(),
        QPen(Qt::magenta, 5),
        Qt::blue);

    QPushButton *button = new QPushButton();
    button->setGeometry(QRect(rect.width() / 2 - 75, 100, 150, 50));
    button->setText("Random ellipse color");
    QObject::connect(button, &QPushButton::pressed, [&]() {
        m_ellipse->setBrush(QColor::fromRgb(QRandomGenerator::global()->generate()));
        m_ellipse->setPen(QPen(QColor::fromRgb(QRandomGenerator::global()->generate()), 5));
    });
    m_scene->addWidget(button);

    m_label = new QLabel();
    m_label->setGeometry(QRect(rect.width() / 2 - 50, rect.height() - 100, 100, 50));
    m_label->setAlignment(Qt::AlignCenter);
    m_scene->addWidget(m_label);

    QSlider *slider = new QSlider(Qt::Horizontal);
    slider->setGeometry(QRect(rect.width() / 2 - 100, rect.height() - 50, 200, 50));
    slider->setMinimum(10);
    slider->setMaximum(300);
    slider->setValue(initialSize.width());
    QObject::connect(slider, &QAbstractSlider::valueChanged, [this](int value){
        QRectF rect = m_ellipse->rect();
        rect.setWidth(value);
        rect.setHeight(value);
        m_ellipse->setRect(rect);
        m_label->setText("Ellipse size: " + QString::number(value));
    });
    emit slider->valueChanged(initialSize.width());
    m_scene->addWidget(slider);

    QMenu *screenShotMenu = menuBar()->addMenu("&Screenshot");
    screenShotMenu->addAction("Left buffer", this, [this](){
        ui->graphicsView->saveImage(QOpenGLWidget::LeftBuffer);
    });

    screenShotMenu->addAction("Right buffer", this, [this](){
        ui->graphicsView->saveImage(QOpenGLWidget::RightBuffer);
    });

}

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