2019-12-14 12:57:16 +01:00
|
|
|
#include "widgets/BasePopup.hpp"
|
|
|
|
|
2021-08-15 15:59:52 +02:00
|
|
|
#include <QAbstractButton>
|
|
|
|
#include <QDialogButtonBox>
|
2021-05-08 15:57:00 +02:00
|
|
|
#include <QKeyEvent>
|
|
|
|
|
2019-12-14 12:57:16 +01:00
|
|
|
namespace chatterino {
|
|
|
|
|
|
|
|
BasePopup::BasePopup(FlagsEnum<Flags> _flags, QWidget *parent)
|
2020-10-31 16:42:48 +01:00
|
|
|
: BaseWindow(_flags | Dialog, parent)
|
2019-12-14 12:57:16 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void BasePopup::keyPressEvent(QKeyEvent *e)
|
|
|
|
{
|
|
|
|
if (e->key() == Qt::Key_Escape)
|
|
|
|
{
|
|
|
|
this->close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseWindow::keyPressEvent(e);
|
|
|
|
}
|
|
|
|
|
2021-08-15 15:59:52 +02:00
|
|
|
bool BasePopup::handleEscape(QKeyEvent *e, QDialogButtonBox *buttonBox)
|
|
|
|
{
|
|
|
|
assert(buttonBox != nullptr);
|
|
|
|
|
|
|
|
if (e->key() == Qt::Key_Escape)
|
|
|
|
{
|
|
|
|
auto buttons = buttonBox->buttons();
|
|
|
|
for (auto *button : buttons)
|
|
|
|
{
|
|
|
|
if (auto role = buttonBox->buttonRole(button);
|
|
|
|
role == QDialogButtonBox::ButtonRole::RejectRole)
|
|
|
|
{
|
|
|
|
button->click();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BasePopup::handleEnter(QKeyEvent *e, QDialogButtonBox *buttonBox)
|
|
|
|
{
|
|
|
|
assert(buttonBox != nullptr);
|
|
|
|
|
|
|
|
if (!e->modifiers() ||
|
|
|
|
(e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter))
|
|
|
|
{
|
|
|
|
switch (e->key())
|
|
|
|
{
|
|
|
|
case Qt::Key_Enter:
|
|
|
|
case Qt::Key_Return: {
|
|
|
|
auto buttons = buttonBox->buttons();
|
|
|
|
QAbstractButton *acceptButton = nullptr;
|
|
|
|
for (auto *button : buttons)
|
|
|
|
{
|
|
|
|
if (button->hasFocus())
|
|
|
|
{
|
|
|
|
button->click();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto role = buttonBox->buttonRole(button);
|
|
|
|
role == QDialogButtonBox::ButtonRole::AcceptRole)
|
|
|
|
{
|
|
|
|
acceptButton = button;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (acceptButton != nullptr)
|
|
|
|
{
|
|
|
|
acceptButton->click();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-14 12:57:16 +01:00
|
|
|
} // namespace chatterino
|