1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
| #include "qfloatwidget.h" #include "ui_qfloatwidget.h"
#include <QGraphicsDropShadowEffect> #include <QHBoxLayout> #include <QPoint> #include <QPainter> #include <QImage> #include <QVariant> #include <QPropertyAnimation>
qFloatWidget::qFloatWidget(QWidget *parent) : QWidget(parent), m_offset(50), m_triangleWidth(TRIANGLE_WIDTH), m_triangleHeight(TRIANGLE_HEIGHT), ui(new Ui::qFloatWidget) { ui->setupUi(this); setWindowFlags(Qt::FramelessWindowHint); setAttribute(Qt::WA_TranslucentBackground); setAttribute(Qt::WA_NoSystemBackground);
QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(this); shadow_effect->setOffset(0, 0); shadow_effect->setColor(QColor(0, 133, 255)); shadow_effect->setBlurRadius(5); this->setGraphicsEffect(shadow_effect);
}
qFloatWidget::~qFloatWidget() { delete ui; }
void qFloatWidget::setStartPos(int startX) { m_offset = startX; repaint(); }
void qFloatWidget::setTriangleInfo(int width, int height) { m_triangleWidth = width; m_triangleHeight = height; }
void qFloatWidget::setDerection(Derection d) { derect = d; }
void qFloatWidget::myMove(int x, int y) { int top_left_x, top_left_y; switch (derect) { case down: top_left_x = x - m_offset - m_triangleWidth / 2 - ui->stackedWidget->x(); top_left_y = y - m_triangleHeight - ui->stackedWidget->height() - ui->stackedWidget->y(); move(QPoint(top_left_x, top_left_y)); break; case up: top_left_x = x - m_offset - m_triangleWidth / 2 - ui->stackedWidget->x(); top_left_y = y + m_triangleHeight - ui->stackedWidget->y(); move(QPoint(top_left_x, top_left_y)); break; case left: top_left_x = x + m_triangleHeight - ui->stackedWidget->x(); top_left_y = y - m_offset - m_triangleWidth / 2 - ui->stackedWidget->y(); move(QPoint(top_left_x, top_left_y)); break; case right: top_left_x = x - m_triangleHeight - ui->stackedWidget->width() - ui->stackedWidget->x(); top_left_y = y - m_triangleWidth / 2 - m_offset - ui->stackedWidget->y(); move(QPoint(top_left_x, top_left_y)); break; default: break; } }
void qFloatWidget::setWidgetIndex(int i) { ui->stackedWidget->setCurrentIndex(i); }
void qFloatWidget::paintEvent(QPaintEvent *) { QPainter painter(this); QPainterPath drawPath;
painter.setRenderHint(QPainter::Antialiasing, true); painter.setPen(Qt::NoPen); painter.setBrush(Qt::white);
QPolygon trianglePolygon;
QRect myRect(ui->stackedWidget->x(), ui->stackedWidget->y(), ui->stackedWidget->width(), ui->stackedWidget->height());
int tri_pos_x, tri_pos_y;
m_offset = ui->stackedWidget->width() / 2 - m_triangleWidth / 2; switch (derect) { case up:{ tri_pos_x = myRect.x() + m_offset; tri_pos_y = myRect.y(); trianglePolygon << QPoint(tri_pos_x, tri_pos_y); trianglePolygon << QPoint(tri_pos_x + m_triangleWidth, tri_pos_y); trianglePolygon << QPoint(tri_pos_x + m_triangleWidth / 2, tri_pos_y - m_triangleHeight); } break; case left:{ tri_pos_x = myRect.x(); tri_pos_y = myRect.y() + m_offset; trianglePolygon << QPoint(tri_pos_x, tri_pos_y); trianglePolygon << QPoint(tri_pos_x - m_triangleHeight, tri_pos_y + m_triangleWidth / 2); trianglePolygon << QPoint(tri_pos_x, tri_pos_y + m_triangleWidth); } break; case right:{ tri_pos_x = myRect.x() + myRect.width(); tri_pos_y = myRect.y() + m_offset; trianglePolygon << QPoint(tri_pos_x, tri_pos_y); trianglePolygon << QPoint(tri_pos_x + m_triangleHeight, tri_pos_y + m_triangleWidth / 2); trianglePolygon << QPoint(tri_pos_x, tri_pos_y + m_triangleWidth); }
break; case down:{ tri_pos_x = myRect.x() + m_offset; tri_pos_y = myRect.y() + myRect.height(); trianglePolygon << QPoint(tri_pos_x, tri_pos_y); trianglePolygon << QPoint(tri_pos_x + m_triangleWidth / 2, tri_pos_y + m_triangleHeight); trianglePolygon << QPoint(tri_pos_x + m_triangleWidth, tri_pos_y); } break; default: break; } drawPath.addRoundedRect(myRect, BORDER_RADIUS, BORDER_RADIUS); drawPath.addPolygon(trianglePolygon); painter.drawPath(drawPath); }
|