add bottom buttons

This commit is contained in:
Gary Wang 2019-09-29 01:40:19 +08:00
parent 2d92479892
commit 6960b2cb2f
5 changed files with 182 additions and 7 deletions

View File

@ -27,11 +27,13 @@ CONFIG += c++11
SOURCES += \ SOURCES += \
main.cpp \ main.cpp \
mainwindow.cpp \ mainwindow.cpp \
graphicsview.cpp graphicsview.cpp \
bottombuttongroup.cpp
HEADERS += \ HEADERS += \
mainwindow.h \ mainwindow.h \
graphicsview.h graphicsview.h \
bottombuttongroup.h
FORMS += FORMS +=

42
bottombuttongroup.cpp Normal file
View File

@ -0,0 +1,42 @@
#include "bottombuttongroup.h"
#include <QPushButton>
#include <QVBoxLayout>
#include <QDebug>
BottomButtonGroup::BottomButtonGroup(QWidget *parent)
: QGroupBox (parent)
{
QHBoxLayout * mainLayout = new QHBoxLayout(this);
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
this->setLayout(mainLayout);
this->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
this->setStyleSheet("BottomButtonGroup {"
"border: 1px solid gray;"
"border-top-left-radius: 10px;"
"border-top-right-radius: 10px;"
"border-bottom: none;"
"background-color:rgba(0,0,0,180)"
"}"
"QPushButton {"
"background-color:rgba(225,255,255,0);"
"color: white;"
"}");
auto newBtn = [](QString text) -> QPushButton * {
QPushButton * btn = new QPushButton(text);
btn->setFixedSize(40, 40);
return btn;
};
addButton(newBtn("1:1"));
addButton(newBtn("Full"));
addButton(newBtn("Zoom+"));
addButton(newBtn("Zoom-"));
addButton(newBtn("Rorate"));
}
void BottomButtonGroup::addButton(QAbstractButton *button)
{
layout()->addWidget(button);
updateGeometry();
}

16
bottombuttongroup.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef BOTTOMBUTTONGROUP_H
#define BOTTOMBUTTONGROUP_H
#include <QAbstractButton>
#include <QGroupBox>
class BottomButtonGroup : public QGroupBox
{
Q_OBJECT
public:
explicit BottomButtonGroup(QWidget *parent = nullptr);
void addButton(QAbstractButton *button);
};
#endif // BOTTOMBUTTONGROUP_H

View File

@ -1,11 +1,17 @@
#include "mainwindow.h" #include "mainwindow.h"
#include "bottombuttongroup.h"
#include "graphicsview.h" #include "graphicsview.h"
#include <QMouseEvent> #include <QMouseEvent>
#include <QMovie> #include <QMovie>
#include <QDebug> #include <QDebug>
#include <QGraphicsTextItem> #include <QGraphicsTextItem>
#include <QApplication>
#ifdef _WIN32
#include <windows.h>
#endif // _WIN32
MainWindow::MainWindow(QWidget *parent) : MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent) QMainWindow(parent)
@ -31,11 +37,11 @@ MainWindow::MainWindow(QWidget *parent) :
QGraphicsTextItem * textItem = scene->addText("Hello, world!"); QGraphicsTextItem * textItem = scene->addText("Hello, world!");
textItem->setDefaultTextColor(QColor("White")); textItem->setDefaultTextColor(QColor("White"));
GraphicsView * test = new GraphicsView(this); GraphicsView * pictureView = new GraphicsView(this);
test->setScene(scene); pictureView->setScene(scene);
this->setCentralWidget(test); this->setCentralWidget(pictureView);
m_closeButton = new QPushButton(test); m_closeButton = new QPushButton(pictureView);
m_closeButton->setFlat(true); m_closeButton->setFlat(true);
m_closeButton->setFixedSize(50, 50); m_closeButton->setFixedSize(50, 50);
m_closeButton->setStyleSheet("QPushButton {" m_closeButton->setStyleSheet("QPushButton {"
@ -47,6 +53,8 @@ MainWindow::MainWindow(QWidget *parent) :
connect(m_closeButton, &QAbstractButton::clicked, connect(m_closeButton, &QAbstractButton::clicked,
this, &MainWindow::closeWindow); this, &MainWindow::closeWindow);
m_bottomButtonGroup = new BottomButtonGroup(this);
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
@ -54,6 +62,13 @@ MainWindow::~MainWindow()
} }
void MainWindow::showEvent(QShowEvent *event)
{
updateWidgetsPosition();
return QMainWindow::showEvent(event);
}
void MainWindow::mousePressEvent(QMouseEvent *event) void MainWindow::mousePressEvent(QMouseEvent *event)
{ {
if (event->buttons() & Qt::LeftButton) { if (event->buttons() & Qt::LeftButton) {
@ -92,11 +107,98 @@ void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)
void MainWindow::resizeEvent(QResizeEvent *event) void MainWindow::resizeEvent(QResizeEvent *event)
{ {
m_closeButton->move(width() - m_closeButton->width(), 0); updateWidgetsPosition();
return QMainWindow::resizeEvent(event); 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() void MainWindow::closeWindow()
{ {
QRect windowRect(this->geometry()); 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_floatUpAnimation->setEndValue(QRect(this->geometry().x(), this->geometry().y()-80, this->geometry().width(), this->geometry().height()));
m_exitAnimationGroup->start(); 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());
}

View File

@ -6,6 +6,7 @@
#include <QPropertyAnimation> #include <QPropertyAnimation>
#include <QPushButton> #include <QPushButton>
class BottomButtonGroup;
class MainWindow : public QMainWindow class MainWindow : public QMainWindow
{ {
Q_OBJECT Q_OBJECT
@ -15,13 +16,17 @@ public:
~MainWindow() override; ~MainWindow() override;
protected slots: protected slots:
void showEvent(QShowEvent *event) override;
void mousePressEvent(QMouseEvent *event) override; void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override;
void mouseDoubleClickEvent(QMouseEvent *event) override; void mouseDoubleClickEvent(QMouseEvent *event) override;
void resizeEvent(QResizeEvent *event) override; void resizeEvent(QResizeEvent *event) override;
bool nativeEvent(const QByteArray& eventType, void* message, long* result) override;
void closeWindow(); void closeWindow();
void updateWidgetsPosition();
private: private:
QPoint m_oldMousePos; QPoint m_oldMousePos;
@ -29,6 +34,7 @@ private:
QPropertyAnimation *m_floatUpAnimation; QPropertyAnimation *m_floatUpAnimation;
QParallelAnimationGroup *m_exitAnimationGroup; QParallelAnimationGroup *m_exitAnimationGroup;
QPushButton *m_closeButton; QPushButton *m_closeButton;
BottomButtonGroup *m_bottomButtonGroup;
bool m_clickedOnWindow = false; bool m_clickedOnWindow = false;
}; };