210 lines
7.4 KiB
C++
210 lines
7.4 KiB
C++
#include "Page5.h"
|
|
#include <QScreen>
|
|
#include <QGuiApplication>
|
|
#include <QMenu>
|
|
#include <QClipboard>
|
|
#include <QApplication>
|
|
#include <QPainter>
|
|
|
|
|
|
DraggableBox::DraggableBox(QWidget *parent, int initialOpacity) : QLabel(parent) {
|
|
setFixedSize(645, 365); // Set the box size
|
|
setStyleSheet("background-color: rgba(0, 0, 0, 0); "
|
|
"color: white; font-size: 20px; font-weight: bold;");
|
|
|
|
opacitySlider = new QSlider(Qt::Horizontal, this);
|
|
opacitySlider->setRange(0, 255); // Set the slider range
|
|
opacitySlider->setValue(initialOpacity);
|
|
opacitySlider->raise();
|
|
|
|
opacitySlider->setGeometry(10, height() - 20, width() - 20, 30); // Set the slider position and size
|
|
// Set the slider style
|
|
opacitySlider->setStyleSheet(R"(
|
|
QSlider::groove:horizontal {
|
|
border: 1px solid #bbb;
|
|
background: #ddd;
|
|
height: 8px;
|
|
border-radius: 4px;
|
|
}
|
|
QSlider::handle:horizontal {
|
|
background: red;
|
|
border: 1px solid #555;
|
|
width: 18px;
|
|
height: 18px;
|
|
margin: -5px 0;
|
|
border-radius: 9px;
|
|
}
|
|
)");
|
|
connect(opacitySlider, &QSlider::valueChanged, this, &DraggableBox::updateOpacity);
|
|
updateOpacity(initialOpacity);
|
|
|
|
coordLabel = new QLabel(parent);
|
|
coordLabel->setStyleSheet("color: white; font-size: 20px; font-weight: bold; background-color: rgba(0, 0, 0, 128); padding: 5px; border-radius: 5px;");
|
|
coordLabel->setAlignment(Qt::AlignCenter);
|
|
coordLabel->setFixedSize(120, 40);
|
|
coordLabel->show();
|
|
updateCoordLabel();
|
|
}
|
|
|
|
void DraggableBox::mousePressEvent(QMouseEvent *event) {
|
|
if (event->button() == Qt::LeftButton) {
|
|
dragStartPos = event->pos();
|
|
}
|
|
}
|
|
|
|
void DraggableBox::mouseMoveEvent(QMouseEvent *event) {
|
|
if (event->buttons() & Qt::LeftButton) {
|
|
QPoint newPos = pos() + event->pos() - dragStartPos;
|
|
|
|
newPos.setX(qMax(0, qMin(newPos.x(), parentWidget()->width() - width())));
|
|
newPos.setY(qMax(0, qMin(newPos.y(), parentWidget()->height() - height())));
|
|
|
|
QPoint screenPos = parentWidget()->mapToGlobal(newPos);
|
|
|
|
move(screenPos);
|
|
updateCoordLabel();
|
|
}
|
|
}
|
|
|
|
void DraggableBox::updateCoordLabel() {
|
|
coordLabel->setText(QString("%1, %2").arg(x() + 10).arg(y() + 10));
|
|
coordLabel->move(x(), y() - 40); // The coordinate box is placed above the box
|
|
}
|
|
|
|
void DraggableBox::contextMenuEvent(QContextMenuEvent *event) {
|
|
QMenu menu;
|
|
QAction *copyAction = new QAction("copy rectangle parameter", &menu);
|
|
connect(copyAction, &QAction::triggered, [this]() {
|
|
QString rectangle = QString("rectangle=%1,%2,%3,%4").arg(x()).arg(y()).arg(width()).arg(height());
|
|
QClipboard *clipboard = QApplication::clipboard();
|
|
clipboard->setText(rectangle);
|
|
});
|
|
|
|
menu.addAction(copyAction);
|
|
menu.exec(event->globalPos());
|
|
}
|
|
|
|
// function for updating transparency
|
|
void DraggableBox::updateOpacity(int value) {
|
|
QColor color = palette().color(QPalette::Window);
|
|
color.setAlpha(value);
|
|
QPalette palette;
|
|
palette.setColor(QPalette::Window, color);
|
|
setPalette(palette);
|
|
}
|
|
|
|
void DraggableBox::paintEvent(QPaintEvent *event) {
|
|
QLabel::paintEvent(event);
|
|
|
|
QPainter painter(this);
|
|
painter.setPen(QPen(Qt::green, 5));
|
|
painter.setBrush(QBrush(QColor(255, 255, 255, opacitySlider->value())));
|
|
painter.drawRect(0, 0, width() - 1, height() - 1);
|
|
}
|
|
|
|
Page5::Page5(QWidget *parent) : QWidget(parent) {
|
|
setWindowTitle("Page5 - Draggable Boxes");
|
|
setAttribute(Qt::WA_TranslucentBackground); // Set as transparent background
|
|
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
|
|
setAttribute(Qt::WA_TransparentForMouseEvents, false); // Don't make controls transparent to mouse events
|
|
|
|
QScreen *screen = QGuiApplication::primaryScreen();
|
|
QSize screenSize = screen->size();
|
|
|
|
QString buttonStylesheet = "QPushButton {"
|
|
" background-color: #34495e;"
|
|
" border: none;"
|
|
" color: white;"
|
|
" padding: 10px 20px;"
|
|
" text-align: center;"
|
|
" text-decoration: none;"
|
|
" display: inline-block;"
|
|
" font-size: 30px;"
|
|
" border-radius: 8px;"
|
|
" box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.1);"
|
|
"}"
|
|
"QPushButton:hover {"
|
|
" background-color: #2c3e50;"
|
|
" cursor: pointer;"
|
|
"}"
|
|
"QPushButton:pressed {"
|
|
" background-color: #273746;"
|
|
" box-shadow: inset 0px 2px 4px rgba(0, 0, 0, 0.1);"
|
|
"}";
|
|
|
|
DraggableBox *firstBox = new DraggableBox(this, 0);
|
|
firstBox->move(90, 90);
|
|
firstBox->show();
|
|
boxes.append(firstBox);
|
|
|
|
DraggableBox *box2 = new DraggableBox(this, 128);
|
|
QPoint globalPos(990, 90);
|
|
box2->setObjectName("box2");
|
|
QPoint localPos = mapFromGlobal(globalPos);
|
|
box2->move(localPos);
|
|
box2->raise();
|
|
box2->show();
|
|
boxes.append(box2);
|
|
box2->updateCoordLabel();
|
|
|
|
pButtonAddBox = new QPushButton("Add", this);
|
|
pButtonAddBox->setStyleSheet(buttonStylesheet);
|
|
pButtonAddBox->setGeometry(screenSize.width() / 4 * 2, screenSize.height() / 10 * 9, 80, 30);
|
|
pButtonAddBox->move(screenSize.width() / 4 * 1, screenSize.height() / 10 * 9);
|
|
pButtonAddBox->resize(screenSize.width() / 16 ,screenSize.height() / 108 * 3);
|
|
pButtonAddBox->setWindowFlags(Qt::WindowStaysOnTopHint);
|
|
pButtonAddBox->setAttribute(Qt::WA_AlwaysStackOnTop);
|
|
pButtonAddBox->raise();
|
|
connect(pButtonAddBox, &QPushButton::clicked, this, &Page5::addBox);
|
|
|
|
pButtonClear = new QPushButton("Clear", this);
|
|
pButtonClear->setStyleSheet(buttonStylesheet);
|
|
pButtonClear->setGeometry(screenSize.width() / 4 * 2, screenSize.height() / 10 * 9, 80, 30);
|
|
pButtonClear->move(screenSize.width() / 4 * 2 + 100, screenSize.height() / 10 * 9);
|
|
pButtonClear->resize(screenSize.width() / 16 ,screenSize.height() / 108 * 3);
|
|
pButtonClear->setWindowFlags(Qt::WindowStaysOnTopHint);
|
|
pButtonClear->setAttribute(Qt::WA_AlwaysStackOnTop);
|
|
pButtonClear->raise();
|
|
connect(pButtonClear, &QPushButton::clicked, this, &Page5::clearBoxes);
|
|
|
|
installEventFilter(this);
|
|
}
|
|
|
|
Page5::~Page5() {}
|
|
|
|
void Page5::addBox() {
|
|
int opacity = (boxes.isEmpty()) ? 0 : 128;
|
|
DraggableBox *box = new DraggableBox(this, opacity);
|
|
box->move(90, 90);
|
|
box->show();
|
|
boxes.append(box);
|
|
}
|
|
|
|
void DraggableBox::hideCoordLabel() {
|
|
coordLabel->hide();
|
|
}
|
|
|
|
void Page5::clearBoxes() {
|
|
for (DraggableBox *box : boxes) {
|
|
box->hideCoordLabel();
|
|
box->deleteLater();
|
|
}
|
|
boxes.clear();
|
|
}
|
|
|
|
bool Page5::eventFilter(QObject *obj, QEvent *event) {
|
|
if (event->type() == QEvent::MouseButtonPress ||
|
|
event->type() == QEvent::MouseButtonRelease ||
|
|
event->type() == QEvent::MouseMove) {
|
|
|
|
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
|
|
QWidget *child = childAt(mouseEvent->pos());
|
|
|
|
if (child) {
|
|
return QApplication::sendEvent(child, event);
|
|
}
|
|
}
|
|
return QWidget::eventFilter(obj, event);
|
|
}
|
|
|