Commit 9e12e2bc authored by abbycin's avatar abbycin

add popup

parent 01a90d3c
...@@ -12,9 +12,11 @@ ...@@ -12,9 +12,11 @@
#include <QApplication> #include <QApplication>
#include <QPushButton> #include <QPushButton>
#include <QMessageBox> #include <QMessageBox>
#include <QTimer>
#include "popup.h"
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), mEdit{}, mHide{}, mFull{} : QMainWindow(parent), mEdit{}, mHide{}, mFull{}, mPopup{}
{ {
auto menu = this->menuBar(); auto menu = this->menuBar();
auto preference = menu->addAction("Preference"); auto preference = menu->addAction("Preference");
...@@ -42,6 +44,8 @@ void MainWindow::showEditor() ...@@ -42,6 +44,8 @@ void MainWindow::showEditor()
void MainWindow::initEditor() void MainWindow::initEditor()
{ {
mPopup = new PopUp{this};
mEdit = new QWidget{}; mEdit = new QWidget{};
QLabel* hide = new QLabel{}; QLabel* hide = new QLabel{};
hide->setText("hide/show"); hide->setText("hide/show");
...@@ -89,7 +93,7 @@ void MainWindow::initEditor() ...@@ -89,7 +93,7 @@ void MainWindow::initEditor()
} }
}); });
connect(mHide, &KeyEdit::error, [this](QString msg) { connect(mHide, &KeyEdit::error, [this](QString msg) {
QMessageBox::critical(this, "Error", msg); this->showPopUp(msg);
mHide->clear(); mHide->clear();
}); });
connect(mFull, &KeyEdit::actived, [this] { connect(mFull, &KeyEdit::actived, [this] {
...@@ -103,7 +107,7 @@ void MainWindow::initEditor() ...@@ -103,7 +107,7 @@ void MainWindow::initEditor()
} }
}); });
connect(mFull, &KeyEdit::error, [this](QString msg) { connect(mFull, &KeyEdit::error, [this](QString msg) {
QMessageBox::critical(this, "Error", msg); this->showPopUp(msg);
}); });
mEdit->setLayout(g); mEdit->setLayout(g);
...@@ -111,13 +115,21 @@ void MainWindow::initEditor() ...@@ -111,13 +115,21 @@ void MainWindow::initEditor()
mEdit->hide(); mEdit->hide();
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
// test conflit with QQ // test conflit with QQ
QTimer::singleShot(1000, [this] {
mHide->setKey(QKeySequence::fromString("Ctrl+Alt+A")); mHide->setKey(QKeySequence::fromString("Ctrl+Alt+A"));
});
#else #else
// test conflit with search // test conflit with search
mHide->setKey(QKeySequence::fromString("Alt+Space")); mHide->setKey(QKeySequence::fromString("Alt+Space"));
#endif #endif
} }
void MainWindow::showPopUp(QString msg)
{
mPopup->setPopupText(msg);
mPopup->show();
}
KeyEdit::KeyEdit(QWidget *parent) KeyEdit::KeyEdit(QWidget *parent)
: QKeySequenceEdit{parent} : QKeySequenceEdit{parent}
{ {
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include <QKeySequenceEdit> #include <QKeySequenceEdit>
class QGlobalShortcut; class QGlobalShortcut;
class PopUp;
class KeyEdit : public QKeySequenceEdit class KeyEdit : public QKeySequenceEdit
{ {
...@@ -40,8 +41,11 @@ private: ...@@ -40,8 +41,11 @@ private:
QWidget* mEdit; QWidget* mEdit;
KeyEdit* mHide; KeyEdit* mHide;
KeyEdit* mFull; KeyEdit* mFull;
PopUp* mPopup;
void initEditor(); void initEditor();
void showPopUp(QString msg);
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H
#include "popup.h"
#include <QLabel>
#include <QGridLayout>
#include <QPropertyAnimation>
#include <QTimer>
#include <QApplication>
#include <QDesktopWidget>
#include <QPainter>
#include <QRect>
PopUp::PopUp(QWidget *parent)
: QWidget{parent}
{
mLabel = new QLabel{};
mLayout = new QGridLayout{this};
mAnimation = new QPropertyAnimation{this};
mTimer = new QTimer{this};
setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint);
setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_ShowWithoutActivating);
mAnimation->setTargetObject(this);
mAnimation->setPropertyName("popUpOpacity");
connect(mAnimation, &QAbstractAnimation::finished, this, &PopUp::hide);
mLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
mLabel->setStyleSheet("QLabel { color : white; "
"margin-top: 10px;"
"margin-bottom: 10px;"
"margin-left: 10px;"
"margin-right: 10px; }");
mLayout->addWidget(mLabel, 0, 0);
setLayout(mLayout);
connect(mTimer, &QTimer::timeout, this, &PopUp::hideAnimation);
}
void PopUp::setOpacity(float opacity)
{
mOpacity = opacity;
setWindowOpacity(opacity);
}
float PopUp::opacity() const
{
return mOpacity;
}
void PopUp::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QRect roundedRect;
roundedRect.setX(rect().x() + 5);
roundedRect.setY(rect().y() + 5);
roundedRect.setWidth(rect().width() - 10);
roundedRect.setHeight(rect().height() - 10);
painter.setBrush(QBrush(QColor(0,0,0,180)));
painter.setPen(Qt::NoPen);
painter.drawRoundedRect(roundedRect, 10, 10);
}
void PopUp::setPopupText(const QString &text)
{
mLabel->setText(text);
this->adjustSize();
}
void PopUp::show()
{
setWindowOpacity(0.0);
mAnimation->setDuration(150);
mAnimation->setStartValue(0.0);
mAnimation->setEndValue(1.0);
auto geo = QApplication::desktop()->availableGeometry();
setGeometry(geo.width() - 30 - width() + geo.x(),
geo.height() - 30 - height() + geo.y(),
width(),
height());
QWidget::show();
mAnimation->start();
mTimer->start(3000);
}
void PopUp::hideAnimation()
{
mTimer->stop();
mAnimation->setDuration(1000);
mAnimation->setStartValue(1.0);
mAnimation->setEndValue(0.0);
mAnimation->start();
}
void PopUp::hide()
{
if(mOpacity < 0.01) {
QWidget::hide();
}
}
#ifndef POPUP_H
#define POPUP_H
#include <QWidget>
class QLabel;
class QGridLayout;
class QPropertyAnimation;
class QTimer;
class PopUp : public QWidget
{
Q_OBJECT
Q_PROPERTY(float popUpOpacity READ opacity WRITE setOpacity)
public:
explicit PopUp(QWidget *parent = 0);
void setOpacity(float opacity);
float opacity() const;
protected:
void paintEvent(QPaintEvent *event);
public slots:
void setPopupText(const QString& text);
void show();
private slots:
void hideAnimation();
void hide();
private:
QLabel* mLabel{nullptr};
QGridLayout* mLayout{nullptr};
QPropertyAnimation* mAnimation{nullptr};
QTimer *mTimer{nullptr};
float mOpacity{0.5};
};
#endif // POPUP_H
...@@ -16,8 +16,10 @@ DESTDIR = $$PWD/../dest ...@@ -16,8 +16,10 @@ DESTDIR = $$PWD/../dest
INCLUDEPATH += $$PWD/../lib INCLUDEPATH += $$PWD/../lib
SOURCES += main.cpp\ SOURCES += main.cpp\
mainwindow.cpp mainwindow.cpp \
popup.cpp
HEADERS += mainwindow.h HEADERS += mainwindow.h \
popup.h
LIBS += -L$$PWD/../dest -lghk LIBS += -L$$PWD/../dest -lghk
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment