add bottom buttons
This commit is contained in:
119
mainwindow.cpp
119
mainwindow.cpp
@@ -1,11 +1,17 @@
|
||||
#include "mainwindow.h"
|
||||
|
||||
#include "bottombuttongroup.h"
|
||||
#include "graphicsview.h"
|
||||
|
||||
#include <QMouseEvent>
|
||||
#include <QMovie>
|
||||
#include <QDebug>
|
||||
#include <QGraphicsTextItem>
|
||||
#include <QApplication>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#endif // _WIN32
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent) :
|
||||
QMainWindow(parent)
|
||||
@@ -31,11 +37,11 @@ MainWindow::MainWindow(QWidget *parent) :
|
||||
QGraphicsTextItem * textItem = scene->addText("Hello, world!");
|
||||
textItem->setDefaultTextColor(QColor("White"));
|
||||
|
||||
GraphicsView * test = new GraphicsView(this);
|
||||
test->setScene(scene);
|
||||
this->setCentralWidget(test);
|
||||
GraphicsView * pictureView = new GraphicsView(this);
|
||||
pictureView->setScene(scene);
|
||||
this->setCentralWidget(pictureView);
|
||||
|
||||
m_closeButton = new QPushButton(test);
|
||||
m_closeButton = new QPushButton(pictureView);
|
||||
m_closeButton->setFlat(true);
|
||||
m_closeButton->setFixedSize(50, 50);
|
||||
m_closeButton->setStyleSheet("QPushButton {"
|
||||
@@ -47,6 +53,8 @@ MainWindow::MainWindow(QWidget *parent) :
|
||||
|
||||
connect(m_closeButton, &QAbstractButton::clicked,
|
||||
this, &MainWindow::closeWindow);
|
||||
|
||||
m_bottomButtonGroup = new BottomButtonGroup(this);
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
@@ -54,6 +62,13 @@ MainWindow::~MainWindow()
|
||||
|
||||
}
|
||||
|
||||
void MainWindow::showEvent(QShowEvent *event)
|
||||
{
|
||||
updateWidgetsPosition();
|
||||
|
||||
return QMainWindow::showEvent(event);
|
||||
}
|
||||
|
||||
void MainWindow::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
if (event->buttons() & Qt::LeftButton) {
|
||||
@@ -92,11 +107,98 @@ void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)
|
||||
|
||||
void MainWindow::resizeEvent(QResizeEvent *event)
|
||||
{
|
||||
m_closeButton->move(width() - m_closeButton->width(), 0);
|
||||
updateWidgetsPosition();
|
||||
|
||||
return QMainWindow::resizeEvent(event);
|
||||
}
|
||||
|
||||
bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
// https://stackoverflow.com/questions/6649936/c-compiling-on-windows-and-linux-ifdef-switch
|
||||
// Too lazy to do this now.. just stackoverflow it and did a copy and paste..
|
||||
Q_UNUSED(eventType);
|
||||
MSG* msg = static_cast<MSG*>(message);
|
||||
|
||||
if (msg->message == WM_NCHITTEST) {
|
||||
if (isMaximized()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*result = 0;
|
||||
const LONG borderWidth = 8;
|
||||
RECT winrect;
|
||||
GetWindowRect(reinterpret_cast<HWND>(winId()), &winrect);
|
||||
|
||||
// must be short to correctly work with multiple monitors (negative coordinates)
|
||||
short x = msg->lParam & 0x0000FFFF;
|
||||
short y = (msg->lParam & 0xFFFF0000) >> 16;
|
||||
|
||||
bool resizeWidth = minimumWidth() != maximumWidth();
|
||||
bool resizeHeight = minimumHeight() != maximumHeight();
|
||||
if (resizeWidth) {
|
||||
//left border
|
||||
if (x >= winrect.left && x < winrect.left + borderWidth) {
|
||||
*result = HTLEFT;
|
||||
}
|
||||
//right border
|
||||
if (x < winrect.right && x >= winrect.right - borderWidth) {
|
||||
*result = HTRIGHT;
|
||||
}
|
||||
}
|
||||
if (resizeHeight) {
|
||||
//bottom border
|
||||
if (y < winrect.bottom && y >= winrect.bottom - borderWidth) {
|
||||
*result = HTBOTTOM;
|
||||
}
|
||||
//top border
|
||||
if (y >= winrect.top && y < winrect.top + borderWidth) {
|
||||
*result = HTTOP;
|
||||
}
|
||||
}
|
||||
if (resizeWidth && resizeHeight) {
|
||||
//bottom left corner
|
||||
if (x >= winrect.left && x < winrect.left + borderWidth &&
|
||||
y < winrect.bottom && y >= winrect.bottom - borderWidth)
|
||||
{
|
||||
*result = HTBOTTOMLEFT;
|
||||
}
|
||||
//bottom right corner
|
||||
if (x < winrect.right && x >= winrect.right - borderWidth &&
|
||||
y < winrect.bottom && y >= winrect.bottom - borderWidth)
|
||||
{
|
||||
*result = HTBOTTOMRIGHT;
|
||||
}
|
||||
//top left corner
|
||||
if (x >= winrect.left && x < winrect.left + borderWidth &&
|
||||
y >= winrect.top && y < winrect.top + borderWidth)
|
||||
{
|
||||
*result = HTTOPLEFT;
|
||||
}
|
||||
//top right corner
|
||||
if (x < winrect.right && x >= winrect.right - borderWidth &&
|
||||
y >= winrect.top && y < winrect.top + borderWidth)
|
||||
{
|
||||
*result = HTTOPRIGHT;
|
||||
}
|
||||
}
|
||||
|
||||
if (*result != 0)
|
||||
return true;
|
||||
|
||||
QWidget *action = QApplication::widgetAt(QCursor::pos());
|
||||
if (action == this) {
|
||||
*result = HTCAPTION;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
#else
|
||||
return QMainWindow::nativeEvent(eventType, *message, *result);
|
||||
#endif // _WIN32
|
||||
}
|
||||
|
||||
void MainWindow::closeWindow()
|
||||
{
|
||||
QRect windowRect(this->geometry());
|
||||
@@ -106,3 +208,10 @@ void MainWindow::closeWindow()
|
||||
m_floatUpAnimation->setEndValue(QRect(this->geometry().x(), this->geometry().y()-80, this->geometry().width(), this->geometry().height()));
|
||||
m_exitAnimationGroup->start();
|
||||
}
|
||||
|
||||
void MainWindow::updateWidgetsPosition()
|
||||
{
|
||||
m_closeButton->move(width() - m_closeButton->width(), 0);
|
||||
m_bottomButtonGroup->move((width() - m_bottomButtonGroup->width()) / 2,
|
||||
height() - m_bottomButtonGroup->height());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user