forked from BGTU/computer-graphics-0
130 lines
4.7 KiB
C++
130 lines
4.7 KiB
C++
#include <QWidget>
|
|
#include <QGridLayout>
|
|
#include <QLabel>
|
|
#include <QResizeEvent>
|
|
#include <QPainter>
|
|
#include <QDebug>
|
|
#include <iostream>
|
|
|
|
#include "ui.hpp"
|
|
|
|
const QString StatusBar::_scale_label_template{"[F2]/[F3] Масштаб: %1 пикселей"};
|
|
const QString StatusBar::_painter_label_template{"[Q] Отображаемая фигура: %1/%2"};
|
|
const QString StatusBar::_x_template{"X=%1"};
|
|
const QString StatusBar::_y_template{"Y=%1"};
|
|
|
|
StatusBar::StatusBar(QWidget *parent) : QWidget(parent) {
|
|
this->_layout = new QGridLayout(parent);
|
|
this->setLayout(this->_layout);
|
|
|
|
auto add_label = [&](QLabel **dst, QString text, int row, int column, QFlags<Qt::AlignmentFlag> alignment) {
|
|
*dst = new QLabel(text, this);
|
|
this->_layout->addWidget(*dst, row, column, alignment);
|
|
};
|
|
|
|
add_label(&(this->_scale_label), StatusBar::_scale_label_template.arg("??"), 0, 0, Qt::AlignRight);
|
|
add_label(&(this->_painter_label), StatusBar::_painter_label_template.arg("??", "??"), 1, 0, Qt::AlignRight);
|
|
|
|
add_label(&(this->_screen_coordinates.title), "Экранные координаты:", 0, 1, Qt::AlignRight);
|
|
add_label(&(this->_screen_coordinates.x), StatusBar::_x_template.arg("??"), 0, 2, Qt::AlignHCenter);
|
|
add_label(&(this->_screen_coordinates.y), StatusBar::_y_template.arg("??"), 0, 3, Qt::AlignHCenter);
|
|
|
|
add_label(&(this->_scaled_coordinates.title), "Системные координаты:", 1, 1, Qt::AlignRight);
|
|
add_label(&(this->_scaled_coordinates.x), StatusBar::_x_template.arg("??"), 1, 2, Qt::AlignHCenter);
|
|
add_label(&(this->_scaled_coordinates.y), StatusBar::_y_template.arg("??"), 1, 3, Qt::AlignHCenter);
|
|
|
|
this->_layout->setColumnStretch(4, 1);
|
|
}
|
|
|
|
void StatusBar::set_coordinates(unsigned int screen_x, unsigned int screen_y, unsigned int scaled_x, unsigned int scaled_y) {
|
|
this->_screen_coordinates.x->setText(StatusBar::_x_template.arg(screen_x));
|
|
this->_screen_coordinates.y->setText(StatusBar::_y_template.arg(screen_y));
|
|
|
|
this->_scaled_coordinates.x->setText(StatusBar::_x_template.arg(scaled_x));
|
|
this->_scaled_coordinates.y->setText(StatusBar::_y_template.arg(scaled_y));
|
|
}
|
|
|
|
void StatusBar::set_scale(unsigned int current_scale) {
|
|
this->_scale_label->setText(StatusBar::_scale_label_template.arg(current_scale));
|
|
}
|
|
|
|
void StatusBar::set_painter_index(unsigned int index, unsigned int painters_count) {
|
|
this->_painter_label->setText(StatusBar::_painter_label_template.arg(index).arg(painters_count));
|
|
}
|
|
|
|
Canvas::Canvas(QWidget *parent) : QWidget(parent), _current_frame{}, _sync{} {
|
|
this->setMouseTracking(true);
|
|
this->grabKeyboard();
|
|
|
|
this->_mouse_processing.is_pressed = false;
|
|
this->_mouse_processing.pressed_at = QPoint(0, 0);
|
|
this->_mouse_processing.is_moved_while_pressed = false;
|
|
}
|
|
|
|
void Canvas::set_frame(QImage &f) {
|
|
this->_sync.lock();
|
|
this->_current_frame = std::move(f);
|
|
this->_sync.unlock();
|
|
}
|
|
|
|
void Canvas::paintEvent(QPaintEvent *event) {
|
|
QPainter painter{};
|
|
painter.begin(this);
|
|
this->_sync.lock();
|
|
painter.drawImage(this->_current_frame.rect(), this->_current_frame);
|
|
this->_sync.unlock();
|
|
painter.end();
|
|
}
|
|
|
|
void Canvas::resizeEvent(QResizeEvent *event) {
|
|
QWidget::resizeEvent(event);
|
|
emit this->resized(event->size());
|
|
}
|
|
|
|
MainWindow::MainWindow() : QWidget{} {
|
|
this->_layout = new QGridLayout(this);
|
|
this->setLayout(this->_layout);
|
|
|
|
this->_status_bar = new StatusBar(this);
|
|
this->_layout->addWidget(this->_status_bar, 0, 0);
|
|
|
|
this->_canvas = new Canvas(this);
|
|
this->_layout->addWidget(this->_canvas, 1, 0);
|
|
|
|
this->_layout->setColumnStretch(0, 1);
|
|
this->_layout->setRowStretch(1, 1);
|
|
}
|
|
|
|
void Canvas::keyPressEvent(QKeyEvent *event) {
|
|
emit this->key_pressed(static_cast<Qt::Key>(event->key()));
|
|
std::cout << "key pressed" << std::endl;
|
|
}
|
|
|
|
void Canvas::mousePressEvent(QMouseEvent *event) {
|
|
this->_mouse_processing.is_pressed = true;
|
|
this->_mouse_processing.pressed_at = event->pos();
|
|
this->_mouse_processing.is_moved_while_pressed = false;
|
|
std::cout << "mouse press" << std::endl;
|
|
}
|
|
|
|
void Canvas::mouseMoveEvent(QMouseEvent *event) {
|
|
if (this->_mouse_processing.is_pressed) {
|
|
this->_mouse_processing.is_moved_while_pressed = true;
|
|
emit this->pressed_mouse_moved(event->pos());
|
|
} else {
|
|
emit this->pressed_mouse_moved(event->pos());
|
|
}
|
|
std::cout << "mouse move" << std::endl;
|
|
}
|
|
|
|
void Canvas::mouseReleaseEvent(QMouseEvent *event) {
|
|
this->_mouse_processing.is_pressed = false;
|
|
if (this->_mouse_processing.is_moved_while_pressed) {
|
|
emit this->mouse_dragged(this->_mouse_processing.pressed_at, event->pos());
|
|
} else {
|
|
emit this->mouse_clicked(event->pos());
|
|
}
|
|
std::cout << "mouse release" << std::endl;
|
|
}
|
|
|