aboutsummaryrefslogtreecommitdiff
path: root/src/widgets/favouritepopup.cpp
blob: b7e11fa0a3505a1ba7899849b64decaa2a907ef1 (plain) (blame)
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
#include "favouritepopup.hpp"

#include <QToolButton>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QLabel>
#include <QKeyEvent>

FavouritePopup::FavouritePopup(QToolButton *button, QWidget *parent)
    : QMenu(parent), b(button)
{
    auto parent_layout = new QVBoxLayout();
    parent_layout->setContentsMargins(8, 8, 8, 8);

    auto layout = new QGridLayout();

    // Title
    auto title_lab = new QLabel("Title:");
    this->fav_title = new QLineEdit();
    layout->addWidget(title_lab, 0, 0);
    layout->addWidget(this->fav_title, 0, 1);

    // Unfavourite
    auto unfav_btn = new QPushButton("Unfavourite");
    layout->addWidget(unfav_btn);
    connect(unfav_btn, &QPushButton::clicked, this, [this]() {
        this->setVisible(false);
        emit this->unfavourited();
    });

    // Confirm
    this->confirm_btn = new QPushButton("Confirm");
    layout->addWidget(this->confirm_btn);
    connect(confirm_btn, &QPushButton::clicked, this, [this]() {
        this->confirmPressed();
    });

    parent_layout->addLayout(layout);

    this->setLayout(parent_layout);
    this->setMinimumWidth(250);
}

void FavouritePopup::confirmPressed()
{
    this->setVisible(false);
    emit this->confirmed();
}

void FavouritePopup::showEvent(QShowEvent *event)
{
    QPoint p = this->pos();
    QRect geo = b->geometry();
    this->move(
        p.x() + geo.width() - this->geometry().width(),
        p.y());
}

void FavouritePopup::keyPressEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Return)
    {
        this->confirmPressed();
        return;
    }
    else if (event->key() == Qt::Key_Escape)
    {
        this->setVisible(false);
        return;
    }
    QMenu::keyPressEvent(event);
}

void FavouritePopup::keyReleaseEvent(QKeyEvent *event)
{
    if (event->key() != Qt::Key_Escape)
    {
        QMenu::keyReleaseEvent(event);
    }
}