1+ /*
2+ * Copyright (C) 2017 KeePassXC Team <[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 or (at your option)
7+ * version 3 of the License.
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, see <http://www.gnu.org/licenses/>.
16+ */
17+
18+ #include " PopupHelpWidget.h"
19+
20+ #include < QEvent>
21+
22+ #include " gui/MainWindow.h"
23+
24+ PopupHelpWidget::PopupHelpWidget (QWidget* parent)
25+ : QWidget(parent)
26+ , m_parentWindow(parent->window ())
27+ , m_appWindow(getMainWindow())
28+ , m_offset({0 , 0 })
29+ , m_corner(Qt::BottomLeftCorner)
30+ {
31+ Q_ASSERT (parent);
32+
33+ setWindowFlags (Qt::FramelessWindowHint | Qt::Tool);
34+ hide ();
35+
36+ m_appWindow->installEventFilter (this );
37+ parent->installEventFilter (this );
38+ }
39+
40+ PopupHelpWidget::~PopupHelpWidget ()
41+ {
42+ m_parentWindow->removeEventFilter (this );
43+ parentWidget ()->removeEventFilter (this );
44+ }
45+
46+ void PopupHelpWidget::setOffset (const QPoint& offset)
47+ {
48+ m_offset = offset;
49+ if (isVisible ()) {
50+ alignWithParent ();
51+ }
52+ }
53+
54+ void PopupHelpWidget::setPosition (Qt::Corner corner)
55+ {
56+ m_corner = corner;
57+ if (isVisible ()) {
58+ alignWithParent ();
59+ }
60+ }
61+
62+ bool PopupHelpWidget::eventFilter (QObject* obj, QEvent* event)
63+ {
64+ if (obj == parentWidget () && event->type () == QEvent::FocusOut) {
65+ hide ();
66+ } else if (obj == m_appWindow && (event->type () == QEvent::Move || event->type () == QEvent::Resize)) {
67+ if (isVisible ()) {
68+ alignWithParent ();
69+ }
70+ }
71+ return QWidget::eventFilter (obj, event);
72+ }
73+
74+ void PopupHelpWidget::showEvent (QShowEvent* event)
75+ {
76+ alignWithParent ();
77+ QWidget::showEvent (event);
78+ }
79+
80+ void PopupHelpWidget::alignWithParent ()
81+ {
82+ QPoint pos;
83+ switch (m_corner) {
84+ case Qt::TopLeftCorner:
85+ pos = parentWidget ()->geometry ().topLeft () + m_offset - QPoint (0 , height ());
86+ break ;
87+ case Qt::TopRightCorner:
88+ pos = parentWidget ()->geometry ().topRight () + m_offset - QPoint (width (), height ());
89+ break ;
90+ case Qt::BottomRightCorner:
91+ pos = parentWidget ()->geometry ().bottomRight () + m_offset - QPoint (width (), 0 );
92+ break ;
93+ default :
94+ pos = parentWidget ()->geometry ().bottomLeft () + m_offset;
95+ break ;
96+ }
97+
98+ move (m_parentWindow->mapToGlobal (pos));
99+ }
0 commit comments