|
| 1 | +/* |
| 2 | + * Copyright 2012, 2013 Thomas Schöps |
| 3 | + * Copyright 2017, 2019 Kai Pastor |
| 4 | + * |
| 5 | + * This file is part of OpenOrienteering. |
| 6 | + * |
| 7 | + * OpenOrienteering is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * OpenOrienteering is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with OpenOrienteering. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | + |
| 22 | +#include "toast.h" |
| 23 | + |
| 24 | +#include <algorithm> |
| 25 | + |
| 26 | +#include <Qt> |
| 27 | +#include <QtGlobal> |
| 28 | +#include <QColor> |
| 29 | +#include <QHideEvent> |
| 30 | +#include <QLabel> |
| 31 | +#include <QPainter> |
| 32 | +#include <QPalette> |
| 33 | +#include <QRect> |
| 34 | +#include <QRgb> |
| 35 | +#include <QString> |
| 36 | +#include <QStyle> |
| 37 | +#include <QStyleOption> |
| 38 | +#include <QTimerEvent> |
| 39 | +#include <QVBoxLayout> |
| 40 | + |
| 41 | + |
| 42 | +namespace OpenOrienteering { |
| 43 | + |
| 44 | +namespace { |
| 45 | + |
| 46 | +constexpr auto min_timeout = 500; // ms |
| 47 | +constexpr auto max_timeout = 5000; // ms |
| 48 | + |
| 49 | +} // namespace |
| 50 | + |
| 51 | + |
| 52 | +Toast::Toast(QWidget* parent) |
| 53 | +: QWidget{ parent } |
| 54 | +{ |
| 55 | + setAttribute(Qt::WA_TranslucentBackground); |
| 56 | + if (!parent) |
| 57 | + setWindowFlags(Qt::FramelessWindowHint | Qt::WindowDoesNotAcceptFocus); |
| 58 | + |
| 59 | + QPalette text_palette; |
| 60 | + auto foreground = Qt::white; |
| 61 | + auto background = Qt::black; |
| 62 | + if (qGray(text_palette.color(QPalette::Window).rgb()) < 128) |
| 63 | + std::swap(foreground, background); |
| 64 | + text_palette.setColor(QPalette::WindowText, foreground); |
| 65 | + text_palette.setColor(QPalette::Window, background); |
| 66 | + setPalette(text_palette); |
| 67 | + |
| 68 | + label = new QLabel(); |
| 69 | + label->setAttribute(Qt::WA_TranslucentBackground); |
| 70 | + label->setWordWrap(true); |
| 71 | + connect(label, &QLabel::linkActivated, this, &Toast::linkActivated); |
| 72 | + |
| 73 | + auto* layout = new QVBoxLayout(); |
| 74 | + layout->addWidget(label); |
| 75 | + setLayout(layout); |
| 76 | + |
| 77 | + setVisible(false); |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +void Toast::showText(const QString& text, int timeout) |
| 82 | +{ |
| 83 | + label->setText(text); |
| 84 | + adjustSize(); |
| 85 | + if (!isWindow()) |
| 86 | + adjustPosition(window()->frameGeometry()); |
| 87 | + show(); |
| 88 | + raise(); |
| 89 | + startTimer(timeout); |
| 90 | +} |
| 91 | + |
| 92 | +QString Toast::text() const |
| 93 | +{ |
| 94 | + return label->text(); |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +void Toast::adjustPosition(const QRect& region) |
| 99 | +{ |
| 100 | + QStyleOption style_option; |
| 101 | + auto const margin = style()->pixelMetric(QStyle::PM_LayoutBottomMargin, &style_option); |
| 102 | + auto const x = region.left() + (region.width() - width()) / 2; |
| 103 | + auto const y = region.bottom() - height() - margin; |
| 104 | + move(x, y); |
| 105 | +} |
| 106 | + |
| 107 | + |
| 108 | +void Toast::startTimer(int timeout) |
| 109 | +{ |
| 110 | + if (Q_UNLIKELY(timeout < min_timeout)) |
| 111 | + { |
| 112 | + qWarning("Minimum toast timeout is %d, got: %d", min_timeout, timeout); |
| 113 | + timeout = min_timeout; |
| 114 | + } |
| 115 | + else if (Q_UNLIKELY(timeout > max_timeout)) |
| 116 | + { |
| 117 | + qWarning("Maximum toast timeout is %d, got: %d", max_timeout, timeout); |
| 118 | + timeout = max_timeout; |
| 119 | + } |
| 120 | + |
| 121 | + timer_id = QObject::startTimer(timeout, Qt::PreciseTimer); |
| 122 | + |
| 123 | + if (Q_UNLIKELY(timer_id == 0)) |
| 124 | + { |
| 125 | + // There is no way to hide the toast, except for doing it right now. |
| 126 | + qWarning("Toast suppressed: %s\n" |
| 127 | + "Reason: Failed to start a timer for hiding the toast.", |
| 128 | + qPrintable(text())); |
| 129 | + hide(); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +void Toast::stopTimer() |
| 134 | +{ |
| 135 | + if (timer_id != 0) |
| 136 | + { |
| 137 | + killTimer(timer_id); |
| 138 | + timer_id = 0; |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | + |
| 143 | +void Toast::timerEvent(QTimerEvent* event) |
| 144 | +{ |
| 145 | + if (event->timerId() == timer_id) |
| 146 | + { |
| 147 | + stopTimer(); |
| 148 | + hide(); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | + |
| 153 | +void Toast::hideEvent(QHideEvent* event) |
| 154 | +{ |
| 155 | + if (!event->spontaneous()) |
| 156 | + { |
| 157 | + stopTimer(); |
| 158 | + } |
| 159 | + QWidget::hideEvent(event); |
| 160 | +} |
| 161 | + |
| 162 | + |
| 163 | +void Toast::paintEvent(QPaintEvent* /*event*/) |
| 164 | +{ |
| 165 | + QPainter painter(this); |
| 166 | + painter.setRenderHint(QPainter::Antialiasing); |
| 167 | + painter.setPen(Qt::NoPen); |
| 168 | + auto color = QColor(palette().color(QPalette::Window)); |
| 169 | + painter.setBrush(color); |
| 170 | + painter.setOpacity(0.7); |
| 171 | + QStyleOption style_option; |
| 172 | + auto const radius = style()->pixelMetric(QStyle::PM_LayoutLeftMargin, &style_option) |
| 173 | + + style()->pixelMetric(QStyle::PM_LayoutTopMargin, &style_option); |
| 174 | + painter.drawRoundRect(0, 0, width(), height(), radius); |
| 175 | +} |
| 176 | + |
| 177 | + |
| 178 | +} // namespace OpenOrienteering |
0 commit comments