Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PopupHelpWidget.cpp « widgets « gui « src - github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2a604dce9f02fe5fe66ee3ae7294072076863374 (plain)
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
/*
 *  Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 or (at your option)
 *  version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "PopupHelpWidget.h"

#include <QEvent>

#include "gui/MainWindow.h"

PopupHelpWidget::PopupHelpWidget(QWidget* parent)
    : QFrame(parent)
    , m_appWindow(getMainWindow())
    , m_offset({0, 0})
    , m_corner(Qt::BottomLeftCorner)
{
    Q_ASSERT(parent);

    setWindowFlags(Qt::FramelessWindowHint | Qt::Tool);
    hide();

    m_appWindow->installEventFilter(this);
    parentWidget()->installEventFilter(this);
}

PopupHelpWidget::~PopupHelpWidget()
{
    m_appWindow->removeEventFilter(this);
    parentWidget()->removeEventFilter(this);
}

void PopupHelpWidget::setOffset(const QPoint& offset)
{
    m_offset = offset;
    if (isVisible()) {
        alignWithParent();
    }
}

void PopupHelpWidget::setPosition(Qt::Corner corner)
{
    m_corner = corner;
    if (isVisible()) {
        alignWithParent();
    }
}

bool PopupHelpWidget::eventFilter(QObject* obj, QEvent* event)
{
    if (isVisible()) {
        if (obj == parentWidget() && event->type() == QEvent::FocusOut && qApp->focusWindow() != windowHandle()) {
            hide();
        } else if (obj == m_appWindow && (event->type() == QEvent::Move || event->type() == QEvent::Resize)) {
            alignWithParent();
        }
    }
    return QFrame::eventFilter(obj, event);
}

void PopupHelpWidget::showEvent(QShowEvent* event)
{
    alignWithParent();
    QFrame::showEvent(event);
}

void PopupHelpWidget::alignWithParent()
{
    QPoint pos = m_offset;
    switch (m_corner) {
    case Qt::TopLeftCorner:
        pos += QPoint(0, -height());
        break;
    case Qt::TopRightCorner:
        pos += QPoint(parentWidget()->width(), -height());
        break;
    case Qt::BottomRightCorner:
        pos += QPoint(parentWidget()->width(), parentWidget()->height());
        break;
    case Qt::BottomLeftCorner:
    default:
        pos += QPoint(0, parentWidget()->height());
        break;
    }

    move(parentWidget()->mapToGlobal(pos));
}