Repository navigation
Expand file tree
/
Copy pathMainWindow.cpp
More file actions
155 lines (133 loc) · 3.63 KB
/
Copy pathMainWindow.cpp
File metadata and controls
155 lines (133 loc) · 3.63 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// SPDX-FileCopyrightText: 2025 Denis Danilov
// SPDX-License-Identifier: GPL-3.0-only
#include "MainWindow.h"
#include "MetaData.h"
#include <QApplication>
#include <QKeyEvent>
#include <QLabel>
#include <QMediaCaptureSession>
#include <QPermissions>
#include <QPushButton>
MainWindow::MainWindow(QWidget* parent) :
QWidget(parent),
m_info_widget(new QTextEdit),
m_video_widget(new QVideoWidget),
m_camera(new QCamera(QCameraDevice::BackFace)),
m_control_layout(new QHBoxLayout)
{
auto* layout = new QVBoxLayout;
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(m_info_widget, 1);
layout->addWidget(m_video_widget, 1);
layout->addLayout(m_control_layout);
m_info_widget->hide();
setLayout(layout);
connectCamera();
addButtons();
setInfo();
connect(this, &MainWindow::fontPixelSizeChanged, this, &MainWindow::setFontPixelSize);
connect(this, &MainWindow::switchView, this, &MainWindow::switchBetweenVideoAndInfo);
}
void MainWindow::keyPressEvent(QKeyEvent* event)
{
if (event->key() == Qt::Key_Back || event->key() == Qt::Key_Backspace)
{
emit switchView();
event->ignore();
}
event->accept();
}
void MainWindow::resizeEvent(QResizeEvent* event)
{
auto pixel_size = static_cast<int>(0.4 * width() / (3 + 1));
emit fontPixelSizeChanged(pixel_size);
QWidget::resizeEvent(event);
}
void MainWindow::setFontPixelSize(int pixel_size)
{
auto widgets = findChildren<QWidget*>();
if (!widgets.empty())
{
QFont font(widgets.front()->font());
font.setPixelSize(pixel_size);
for (auto* widget : std::as_const(widgets))
{
widget->setFont(font);
}
}
}
void MainWindow::switchTorch()
{
if (m_camera->torchMode() != QCamera::TorchOff)
{
m_camera->setTorchMode(QCamera::TorchOff);
}
else
{
m_camera->setTorchMode(QCamera::TorchOn);
}
}
void MainWindow::switchBetweenVideoAndInfo()
{
if (m_video_widget->isHidden())
{
m_info_widget->hide();
m_video_widget->show();
}
else
{
m_video_widget->hide();
m_info_widget->show();
}
}
void MainWindow::connectCamera()
{
#if QT_CONFIG(permissions)
QCameraPermission cameraPermission;
switch (qApp->checkPermission(cameraPermission))
{
case Qt::PermissionStatus::Undetermined:
qApp->requestPermission(QCameraPermission{}, this, &MainWindow::connectCamera);
return;
case Qt::PermissionStatus::Denied:
return;
case Qt::PermissionStatus::Granted:
break;
}
#endif
auto* media_capture_session = new QMediaCaptureSession;
media_capture_session->setCamera(m_camera);
media_capture_session->setVideoOutput(m_video_widget);
m_camera->start();
}
void MainWindow::addButtons()
{
m_control_layout->setContentsMargins(0, 0, 0, 0);
constexpr int num_buttons = 3;
for (int i = 0; i < num_buttons; ++i)
{
const auto factor = static_cast<float>(i + 2);
auto* zoom_button = new QPushButton;
zoom_button->setText(QString("%1X").arg(factor, 0, 'f', 0));
connect(zoom_button, &QPushButton::pressed, this, [this, factor]() { setZoom(factor); });
m_control_layout->addWidget(zoom_button, 1);
}
auto* torch_button = new QPushButton("T");
connect(torch_button, &QPushButton::pressed, this, &MainWindow::switchTorch);
m_control_layout->addWidget(torch_button, 1);
}
void MainWindow::setInfo()
{
QString info;
info.append("<small>");
info.append(QString("App Version: %1").arg(LAST_TAG));
info.append("<br>");
info.append(QString("Qt Version: %1").arg(qVersion()));
info.append("</small>");
m_info_widget->setText(info);
m_info_widget->setReadOnly(true);
}
void MainWindow::setZoom(const float factor)
{
m_camera->setZoomFactor(factor);
}