commit 2d924798929d6e54bc129b8bc78e97460edfeebf Author: Gary Wang Date: Sat Sep 28 01:18:08 2019 +0800 init commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fb1a0d8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# User files +*.user diff --git a/PineapplePictures.pro b/PineapplePictures.pro new file mode 100644 index 0000000..22bcef5 --- /dev/null +++ b/PineapplePictures.pro @@ -0,0 +1,41 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2019-09-26T23:36:07 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = PineapplePictures +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which has been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +CONFIG += c++11 + +SOURCES += \ + main.cpp \ + mainwindow.cpp \ + graphicsview.cpp + +HEADERS += \ + mainwindow.h \ + graphicsview.h + +FORMS += + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/graphicsview.cpp b/graphicsview.cpp new file mode 100644 index 0000000..1142956 --- /dev/null +++ b/graphicsview.cpp @@ -0,0 +1,59 @@ +#include "graphicsview.h" + +#include +#include +#include + +GraphicsView::GraphicsView(QWidget *parent) + : QGraphicsView (parent) +{ + setDragMode(QGraphicsView::ScrollHandDrag); + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setStyleSheet("background-color: rgba(0, 0, 0, 180);" + "border-radius: 3px;"); +} + +void GraphicsView::mousePressEvent(QMouseEvent *event) +{ + QGraphicsItem *item = itemAt(event->pos()); + if (!item) { + event->ignore(); + // blumia: return here, or the QMouseEvent event transparency won't + // work if we set a QGraphicsView::ScrollHandDrag drag mode. + return; + } + + qDebug() << item; + + return QGraphicsView::mousePressEvent(event); +} + +void GraphicsView::mouseMoveEvent(QMouseEvent *event) +{ + QGraphicsItem *item = itemAt(event->pos()); + if (!item) { + event->ignore(); + } + + return QGraphicsView::mouseMoveEvent(event); +} + +void GraphicsView::mouseReleaseEvent(QMouseEvent *event) +{ + QGraphicsItem *item = itemAt(event->pos()); + if (!item) { + event->ignore(); + } + + return QGraphicsView::mouseReleaseEvent(event); +} + +void GraphicsView::wheelEvent(QWheelEvent *event) +{ + if(event->delta() > 0) { + scale(1.25, 1.25); + } else { + scale(0.8, 0.8); + } +} diff --git a/graphicsview.h b/graphicsview.h new file mode 100644 index 0000000..046663b --- /dev/null +++ b/graphicsview.h @@ -0,0 +1,18 @@ +#ifndef GRAPHICSVIEW_H +#define GRAPHICSVIEW_H + +#include + +class GraphicsView : public QGraphicsView +{ +public: + GraphicsView(QWidget *parent = nullptr); + +private: + void mousePressEvent(QMouseEvent * event) override; + void mouseMoveEvent(QMouseEvent * event) override; + void mouseReleaseEvent(QMouseEvent * event) override; + void wheelEvent(QWheelEvent *event) override; +}; + +#endif // GRAPHICSVIEW_H diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..aab39bb --- /dev/null +++ b/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000..9e85830 --- /dev/null +++ b/mainwindow.cpp @@ -0,0 +1,108 @@ +#include "mainwindow.h" + +#include "graphicsview.h" + +#include +#include +#include +#include + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent) +{ + this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint); + this->setAttribute(Qt::WA_TranslucentBackground, true); + this->setMinimumSize(610, 410); + + m_fadeOutAnimation = new QPropertyAnimation(this, "windowOpacity"); + m_fadeOutAnimation->setDuration(300); + m_fadeOutAnimation->setStartValue(1); + m_fadeOutAnimation->setEndValue(0); + m_floatUpAnimation = new QPropertyAnimation(this, "geometry"); + m_floatUpAnimation->setDuration(300); + m_floatUpAnimation->setEasingCurve(QEasingCurve::OutCirc); + m_exitAnimationGroup = new QParallelAnimationGroup; + m_exitAnimationGroup->addAnimation(m_fadeOutAnimation); + m_exitAnimationGroup->addAnimation(m_floatUpAnimation); + connect(m_exitAnimationGroup, &QParallelAnimationGroup::finished, + this, &QMainWindow::close); + + QGraphicsScene * scene = new QGraphicsScene(this); + QGraphicsTextItem * textItem = scene->addText("Hello, world!"); + textItem->setDefaultTextColor(QColor("White")); + + GraphicsView * test = new GraphicsView(this); + test->setScene(scene); + this->setCentralWidget(test); + + m_closeButton = new QPushButton(test); + m_closeButton->setFlat(true); + m_closeButton->setFixedSize(50, 50); + m_closeButton->setStyleSheet("QPushButton {" + "background: transparent;" + "}" + "QPushButton:hover {" + "background: red;" + "}"); + + connect(m_closeButton, &QAbstractButton::clicked, + this, &MainWindow::closeWindow); +} + +MainWindow::~MainWindow() +{ + +} + +void MainWindow::mousePressEvent(QMouseEvent *event) +{ + if (event->buttons() & Qt::LeftButton) { + m_clickedOnWindow = true; + m_oldMousePos = event->pos(); + qDebug() << m_oldMousePos; + event->accept(); + } + + return QMainWindow::mousePressEvent(event); +} + +void MainWindow::mouseMoveEvent(QMouseEvent *event) +{ + if (event->buttons() & Qt::LeftButton && m_clickedOnWindow) { + move(event->globalPos() - m_oldMousePos); + event->accept(); + } + + return QMainWindow::mouseMoveEvent(event); +} + +void MainWindow::mouseReleaseEvent(QMouseEvent *event) +{ + m_clickedOnWindow = false; + + return QMainWindow::mouseReleaseEvent(event); +} + +void MainWindow::mouseDoubleClickEvent(QMouseEvent *event) +{ + closeWindow(); + + return QMainWindow::mouseDoubleClickEvent(event); +} + +void MainWindow::resizeEvent(QResizeEvent *event) +{ + m_closeButton->move(width() - m_closeButton->width(), 0); + + return QMainWindow::resizeEvent(event); +} + +void MainWindow::closeWindow() +{ + QRect windowRect(this->geometry()); + m_floatUpAnimation->setStartValue(windowRect); + m_floatUpAnimation->setEndValue(windowRect.adjusted(0, -80, 0, 0)); + m_floatUpAnimation->setStartValue(QRect(this->geometry().x(), this->geometry().y(), 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(); +} diff --git a/mainwindow.h b/mainwindow.h new file mode 100644 index 0000000..d31bec6 --- /dev/null +++ b/mainwindow.h @@ -0,0 +1,35 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include +#include +#include + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = nullptr); + ~MainWindow() override; + +protected slots: + void mousePressEvent(QMouseEvent *event) override; + void mouseMoveEvent(QMouseEvent *event) override; + void mouseReleaseEvent(QMouseEvent *event) override; + void mouseDoubleClickEvent(QMouseEvent *event) override; + void resizeEvent(QResizeEvent *event) override; + + void closeWindow(); + +private: + QPoint m_oldMousePos; + QPropertyAnimation *m_fadeOutAnimation; + QPropertyAnimation *m_floatUpAnimation; + QParallelAnimationGroup *m_exitAnimationGroup; + QPushButton *m_closeButton; + bool m_clickedOnWindow = false; +}; + +#endif // MAINWINDOW_H