Commit 9e12e2bc authored by abbycin's avatar abbycin

add popup

parent 01a90d3c
......@@ -12,9 +12,11 @@
#include <QApplication>
#include <QPushButton>
#include <QMessageBox>
#include <QTimer>
#include "popup.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), mEdit{}, mHide{}, mFull{}
: QMainWindow(parent), mEdit{}, mHide{}, mFull{}, mPopup{}
{
auto menu = this->menuBar();
auto preference = menu->addAction("Preference");
......@@ -42,6 +44,8 @@ void MainWindow::showEditor()
void MainWindow::initEditor()
{
mPopup = new PopUp{this};
mEdit = new QWidget{};
QLabel* hide = new QLabel{};
hide->setText("hide/show");
......@@ -89,7 +93,7 @@ void MainWindow::initEditor()
}
});
connect(mHide, &KeyEdit::error, [this](QString msg) {
QMessageBox::critical(this, "Error", msg);
this->showPopUp(msg);
mHide->clear();
});
connect(mFull, &KeyEdit::actived, [this] {
......@@ -103,7 +107,7 @@ void MainWindow::initEditor()
}
});
connect(mFull, &KeyEdit::error, [this](QString msg) {
QMessageBox::critical(this, "Error", msg);
this->showPopUp(msg);
});
mEdit->setLayout(g);
......@@ -111,13 +115,21 @@ void MainWindow::initEditor()
mEdit->hide();
#ifdef Q_OS_WIN
// test conflit with QQ
QTimer::singleShot(1000, [this] {
mHide->setKey(QKeySequence::fromString("Ctrl+Alt+A"));
});
#else
// test conflit with search
mHide->setKey(QKeySequence::fromString("Alt+Space"));
#endif
}
void MainWindow::showPopUp(QString msg)
{
mPopup->setPopupText(msg);
mPopup->show();
}
KeyEdit::KeyEdit(QWidget *parent)
: QKeySequenceEdit{parent}
{
......
......@@ -5,6 +5,7 @@
#include <QKeySequenceEdit>
class QGlobalShortcut;
class PopUp;
class KeyEdit : public QKeySequenceEdit
{
......@@ -40,8 +41,11 @@ private:
QWidget* mEdit;
KeyEdit* mHide;
KeyEdit* mFull;
PopUp* mPopup;
void initEditor();
void showPopUp(QString msg);
};
#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
INCLUDEPATH += $$PWD/../lib
SOURCES += main.cpp\
mainwindow.cpp
mainwindow.cpp \
popup.cpp
HEADERS += mainwindow.h
HEADERS += mainwindow.h \
popup.h
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