messagewidget.cpp 1.22 KB
Newer Older
1 2 3
/*
  This file is part of KOrganizer.

4
  SPDX-FileCopyrightText: 2013 Sérgio Martins <iamsergio@gmail.com>
5

6
  SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
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
*/

#include "messagewidget.h"

#include <QApplication>
#include <QKeyEvent>

using namespace CalendarSupport;

MessageWidget::MessageWidget(QWidget *parent) : KMessageWidget(parent)
{
    hide();
    setCloseButtonVisible(false);
    setWordWrap(true);
}

MessageWidget::~MessageWidget()
{
}

void MessageWidget::showEvent(QShowEvent *event)
{
    qApp->installEventFilter(this);
    KMessageWidget::showEvent(event);
}

void MessageWidget::hideEvent(QHideEvent *event)
{
    // No need to spend cycles on an event-filter when this is going to
    // me hidden most of the time
    qApp->removeEventFilter(this);
    KMessageWidget::hideEvent(event);
}

bool MessageWidget::eventFilter(QObject *, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonPress) {
        hide();
    }

    if (event->type() == QEvent::KeyPress) {
Laurent Montel's avatar
Laurent Montel committed
48
        auto *ev = static_cast<QKeyEvent *>(event);
49 50 51 52 53 54 55 56
        hide();
        if (ev->key() == Qt::Key_Escape) {
            return true; // We eat this one, it's for us
        }
    }

    return false; // we don't want it
}