Debian Codegraph @

src/gui/modelessWindows/pinWindow/PinWindow.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 Damir Porobic <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "PinWindow.h"
21
22 PinWindow::PinWindow(const QPixmap &pixmap, const QString &title) :
23 mLayout(new QVBoxLayout(this)),
24 mCentralWidget(new QLabel(this)),
25 mDropShadowEffect(new QGraphicsDropShadowEffect(this)),
26 mMargin(10),
27 mMinSize(50),
28 mImage(pixmap),
29 mIsMoving(false)
30 {
31 setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::CoverWindow);
32 setAttribute(Qt::WA_TranslucentBackground);
33 setModal(false);
34 setWindowTitle(title);
35 setAttribute(Qt::WA_DeleteOnClose);
36 setMouseTracking(true);
37 setCursor(Qt::SizeAllCursor);
38
39 mCentralWidget->setPixmap(mImage);
40 mLayout->addWidget(mCentralWidget);
41
42 setContentsMargins(mMargin, mMargin, mMargin, mMargin);
43
44 addDropShadow();
45 }
46
47 PinWindow::~PinWindow()
48 {
49 delete mLayout;
50 delete mCentralWidget;
51 delete mDropShadowEffect;
52 }
53
54 void PinWindow::addDropShadow()
55 {
56 mDropShadowEffect->setColor(QColor(160, 160, 160));
57 mDropShadowEffect->setBlurRadius(mMargin * 2);
58 mDropShadowEffect->setOffset(0);
59 setGraphicsEffect(mDropShadowEffect);
60 }
61
62 void PinWindow::mouseDoubleClickEvent(QMouseEvent *event)
63 {
64 Q_UNUSED(event)
65 emit close();
66 }
67
68 void PinWindow::mousePressEvent(QMouseEvent *event)
69 {
70 if(event->button() == Qt::LeftButton) {
71 mIsMoving = true;
72 mMoveOffset = event->globalPos() - pos();
73 }
74 QWidget::mousePressEvent(event);
75 }
76
77 void PinWindow::mouseReleaseEvent(QMouseEvent *event)
78 {
79 if(event->button() == Qt::LeftButton) {
80 mIsMoving = false;
81 mMoveOffset = {};
82 }
83 QWidget::mouseReleaseEvent(event);
84 }
85
86 void PinWindow::mouseMoveEvent(QMouseEvent *event)
87 {
88 if(mIsMoving) {
89 move(event->globalPos() - mMoveOffset);
90 }
91 QWidget::mouseMoveEvent(event);
92 }
93
94 void PinWindow::keyPressEvent(QKeyEvent *event)
95 {
96 if(event->key() == Qt::Key_Escape) {
97 emit close();
98 }
99 }
100
101 void PinWindow::contextMenuEvent(QContextMenuEvent *event)
102 {
103 QMenu menu;
104 menu.addAction(tr("Close"), this, &PinWindow::closeRequest);
105 menu.addAction(tr("Close Other"), this, &PinWindow::closeOtherRequest);
106 menu.addAction(tr("Close All"), this, &PinWindow::closeAllRequest);
107 menu.exec(event->globalPos());
108 }
109
110 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
111 void PinWindow::enterEvent(QEnterEvent *event)
112 #else
113 void PinWindow::enterEvent(QEvent *event)
114 #endif
115 {
116 mDropShadowEffect->setBlurRadius(mDropShadowEffect->blurRadius() + 4);
117 QWidget::enterEvent(event);
118 }
119
120 void PinWindow::leaveEvent(QEvent *event)
121 {
122 mDropShadowEffect->setBlurRadius(mDropShadowEffect->blurRadius() - 4);
123 QWidget::leaveEvent(event);
124 }
125
126 void PinWindow::wheelEvent(QWheelEvent *event)
127 {
128 auto delta = event->pixelDelta().y() / 10;
129 auto scaledSize = QSize(mCentralWidget->width() + delta, mCentralWidget->height() + delta);
130
131 if(scaledSize.width() > mMinSize && scaledSize.height() > mMinSize) {
132 auto scaledImage = mImage.scaled(scaledSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
133 mCentralWidget->setPixmap(scaledImage);
134 adjustSize();
135 }
136 }