| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2023 Abdullah Tahiri <[email protected]> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
#include <QApplication> |
| 25 |
#include <QEvent> |
| 26 |
#include <QLabel> |
| 27 |
#include <QScreen> |
| 28 |
#include <QStyleOption> |
| 29 |
#include <QStylePainter> |
| 30 |
#include <QTextDocument> |
| 31 |
#include <QTimer> |
| 32 |
#include <memory> |
| 33 |
#include <mutex> |
| 34 |
|
| 35 |
|
| 36 |
#include "NotificationBox.h" |
| 37 |
|
| 38 |
|
| 39 |
using namespace Gui; |
| 40 |
|
| 41 |
namespace Gui |
| 42 |
{ |
| 43 |
|
| 44 |
// https://stackoverflow.com/questions/41402152/stdunique-ptr-and-qobjectdeletelater |
| 45 |
struct QObjectDeleteLater |
| 46 |
{ |
| 47 |
void operator()(QObject* o) |
| 48 |
{ |
| 49 |
o->deleteLater(); |
| 50 |
} |
| 51 |
}; |
| 52 |
|
| 53 |
template<typename T> |
| 54 |
using qobject_delete_later_unique_ptr = std::unique_ptr<T, QObjectDeleteLater>; |
| 55 |
|
| 56 |
/** Class showing the notification as a label |
| 57 |
* */ |
| 58 |
class NotificationLabel: public QLabel |
| 59 |
{ |
| 60 |
Q_OBJECT |
| 61 |
public: |
| 62 |
NotificationLabel( |
| 63 |
const QString& text, |
| 64 |
const QPoint& pos, |
| 65 |
int displayTime, |
| 66 |
int minShowTime = 0, |
| 67 |
int width = 0 |
| 68 |
); |
| 69 |
/// Reuse existing notification to show a new notification (with a new text) |
| 70 |
void reuseNotification(const QString& text, int displayTime, const QPoint& pos, int width); |
| 71 |
/// Hide notification after a hiding timer. |
| 72 |
void hideNotification(); |
| 73 |
/// Update the size of the QLabel |
| 74 |
void updateSize(const QPoint& pos); |
| 75 |
/// Event filter |
| 76 |
bool eventFilter(QObject*, QEvent*) override; |
| 77 |
/// Return true if the text provided is the same as the one of an existing notification |
| 78 |
bool notificationLabelChanged(const QString& text); |
| 79 |
/// Place the notification at the given position |
| 80 |
void placeNotificationLabel(const QPoint& pos); |
| 81 |
/// Set the windowrect defining an area to which the label should be constrained |
| 82 |
void setTipRect(const QRect& restrictionarea); |
| 83 |
|
| 84 |
void setHideIfReferenceWidgetDeactivated(bool on); |
| 85 |
|
| 86 |
/// The instance |
| 87 |
static qobject_delete_later_unique_ptr<NotificationLabel> instance; |
| 88 |
|
| 89 |
protected: |
| 90 |
void paintEvent(QPaintEvent* e) override; |
| 91 |
void resizeEvent(QResizeEvent* e) override; |
| 92 |
|
| 93 |
private: |
| 94 |
/// Re-start the notification expiration timer |
| 95 |
void restartExpireTimer(int displayTime); |
| 96 |
/// Hide notification right away |
| 97 |
void hideNotificationImmediately(); |
| 98 |
|
| 99 |
private: |
| 100 |
int minShowTime; |
| 101 |
QTimer hideTimer; |
| 102 |
QTimer expireTimer; |
| 103 |
|
| 104 |
QRect restrictionArea; |
| 105 |
bool hideIfReferenceWidgetDeactivated; |
| 106 |
}; |
| 107 |
|
| 108 |
qobject_delete_later_unique_ptr<NotificationLabel> NotificationLabel::instance = nullptr; |
| 109 |
|
| 110 |
NotificationLabel::NotificationLabel( |
| 111 |
const QString& text, |
| 112 |
const QPoint& pos, |
| 113 |
int displayTime, |
| 114 |
int minShowTime, |
| 115 |
int width |
| 116 |
) |
| 117 |
: QLabel(nullptr, Qt::ToolTip | Qt::BypassGraphicsProxyWidget) |
| 118 |
, minShowTime(minShowTime) |
| 119 |
{ |
| 120 |
instance.reset(this); |
| 121 |
setForegroundRole(QPalette::ToolTipText); // defaults to ToolTip QPalette |
| 122 |
setBackgroundRole(QPalette::ToolTipBase); // defaults to ToolTip QPalette |
| 123 |
setPalette(NotificationBox::palette()); |
| 124 |
ensurePolished(); |
| 125 |
setMargin(1 + style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth, nullptr, this)); |
| 126 |
setFrameStyle(QFrame::NoFrame); |
| 127 |
setAlignment(Qt::AlignLeft); |
| 128 |
setIndent(1); |
| 129 |
qApp->installEventFilter(this); |
| 130 |
setWindowOpacity(style()->styleHint(QStyle::SH_ToolTipLabel_Opacity, nullptr, this) / 255.0); |
| 131 |
setMouseTracking(false); |
| 132 |
hideTimer.setSingleShot(true); |
| 133 |
expireTimer.setSingleShot(true); |
| 134 |
|
| 135 |
expireTimer.callOnTimeout([this]() { |
| 136 |
hideTimer.stop(); |
| 137 |
hideNotificationImmediately(); |
| 138 |
}); |
| 139 |
|
| 140 |
hideTimer.callOnTimeout([this]() { |
| 141 |
expireTimer.stop(); |
| 142 |
hideNotificationImmediately(); |
| 143 |
}); |
| 144 |
|
| 145 |
reuseNotification(text, displayTime, pos, width); |
| 146 |
} |
| 147 |
|
| 148 |
void NotificationLabel::restartExpireTimer(int displayTime) |
| 149 |
{ |
| 150 |
int time; |
| 151 |
|
| 152 |
if (displayTime > 0) { |
| 153 |
time = displayTime; |
| 154 |
} |
| 155 |
else { |
| 156 |
time = 10000 + 40 * qMax(0, text().length() - 100); |
| 157 |
} |
| 158 |
|
| 159 |
expireTimer.start(time); |
| 160 |
hideTimer.stop(); |
| 161 |
} |
| 162 |
|
| 163 |
void NotificationLabel::reuseNotification(const QString& text, int displayTime, const QPoint& pos, int width) |
| 164 |
{ |
| 165 |
if (width > 0) { |
| 166 |
setFixedWidth(width); |
| 167 |
} |
| 168 |
|
| 169 |
setText(text); |
| 170 |
updateSize(pos); |
| 171 |
restartExpireTimer(displayTime); |
| 172 |
} |
| 173 |
|
| 174 |
void NotificationLabel::updateSize(const QPoint& pos) |
| 175 |
{ |
| 176 |
// Ensure that we get correct sizeHints by placing this window on the right screen. |
| 177 |
QFontMetrics fm(font()); |
| 178 |
QSize extra(1, 0); |
| 179 |
// Make it look good with the default ToolTip font on Mac, which has a small descent. |
| 180 |
if (fm.descent() == 2 && fm.ascent() >= 11) { |
| 181 |
++extra.rheight(); |
| 182 |
} |
| 183 |
|
| 184 |
setWordWrap(Qt::mightBeRichText(text())); |
| 185 |
|
| 186 |
QSize sh = sizeHint(); |
| 187 |
|
| 188 |
// ### When the above WinRT code is fixed, windowhandle should be used to find the screen. |
| 189 |
QScreen* screen = QGuiApplication::screenAt(pos); |
| 190 |
|
| 191 |
if (!screen) { |
| 192 |
screen = QGuiApplication::primaryScreen(); |
| 193 |
} |
| 194 |
|
| 195 |
if (screen) { |
| 196 |
const qreal screenWidth = screen->geometry().width(); |
| 197 |
if (!wordWrap() && sh.width() > screenWidth) { |
| 198 |
setWordWrap(true); |
| 199 |
sh = sizeHint(); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
resize(sh + extra); |
| 204 |
} |
| 205 |
|
| 206 |
void NotificationLabel::paintEvent(QPaintEvent* ev) |
| 207 |
{ |
| 208 |
QStylePainter p(this); |
| 209 |
QStyleOptionFrame opt; |
| 210 |
opt.initFrom(this); |
| 211 |
p.drawPrimitive(QStyle::PE_PanelTipLabel, opt); |
| 212 |
p.end(); |
| 213 |
QLabel::paintEvent(ev); |
| 214 |
} |
| 215 |
|
| 216 |
void NotificationLabel::resizeEvent(QResizeEvent* e) |
| 217 |
{ |
| 218 |
QStyleHintReturnMask frameMask; |
| 219 |
QStyleOption option; |
| 220 |
|
| 221 |
option.initFrom(this); |
| 222 |
|
| 223 |
if (style()->styleHint(QStyle::SH_ToolTip_Mask, &option, this, &frameMask)) { |
| 224 |
setMask(frameMask.region); |
| 225 |
} |
| 226 |
|
| 227 |
QLabel::resizeEvent(e); |
| 228 |
} |
| 229 |
|
| 230 |
void NotificationLabel::hideNotification() |
| 231 |
{ |
| 232 |
if (!hideTimer.isActive()) { |
| 233 |
hideTimer.start(300); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
void NotificationLabel::hideNotificationImmediately() |
| 238 |
{ |
| 239 |
close(); // to trigger QEvent::Close which stops the animation |
| 240 |
instance = nullptr; |
| 241 |
} |
| 242 |
|
| 243 |
bool NotificationLabel::eventFilter(QObject* o, QEvent* e) |
| 244 |
{ |
| 245 |
Q_UNUSED(o) |
| 246 |
|
| 247 |
switch (e->type()) { |
| 248 |
case QEvent::MouseButtonPress: { |
| 249 |
// If minimum on screen time has already lapsed - hide the notification no matter where |
| 250 |
// the click was done |
| 251 |
auto total = expireTimer.interval(); |
| 252 |
auto remaining = expireTimer.remainingTime(); |
| 253 |
auto lapsed = total - remaining; |
| 254 |
// ... or if the click is inside the notification, hide it no matter if the minimum |
| 255 |
// onscreen time has lapsed or not |
| 256 |
auto insideclick = this->underMouse(); |
| 257 |
if (lapsed > minShowTime || insideclick) { |
| 258 |
hideNotification(); |
| 259 |
|
| 260 |
return insideclick; |
| 261 |
} |
| 262 |
} break; |
| 263 |
case QEvent::WindowDeactivate: |
| 264 |
if (hideIfReferenceWidgetDeactivated) { |
| 265 |
hideNotificationImmediately(); |
| 266 |
} |
| 267 |
break; |
| 268 |
|
| 269 |
default: |
| 270 |
break; |
| 271 |
} |
| 272 |
return false; |
| 273 |
} |
| 274 |
|
| 275 |
void NotificationLabel::placeNotificationLabel(const QPoint& pos) |
| 276 |
{ |
| 277 |
QPoint p = pos; |
| 278 |
const QScreen* screen = QGuiApplication::screenAt(pos); |
| 279 |
// a QScreen's handle *should* never be null, so this is a bit paranoid |
| 280 |
if (screen && screen->handle()) { |
| 281 |
const QSize cursorSize = QSize(16, 16); |
| 282 |
|
| 283 |
QPoint offset(2, cursorSize.height()); |
| 284 |
// assuming an arrow shape, we can just move to the side for very large cursors |
| 285 |
if (cursorSize.height() > 2 * this->height()) { |
| 286 |
offset = QPoint(cursorSize.width() / 2, 0); |
| 287 |
} |
| 288 |
|
| 289 |
p += offset; |
| 290 |
|
| 291 |
QRect actinglimit = screen->geometry(); |
| 292 |
|
| 293 |
if (!restrictionArea.isNull()) { |
| 294 |
actinglimit = restrictionArea; |
| 295 |
} |
| 296 |
|
| 297 |
const int standard_x_padding = 4; |
| 298 |
const int standard_y_padding = 24; |
| 299 |
|
| 300 |
if (p.x() + this->width() > actinglimit.x() + actinglimit.width()) { |
| 301 |
p.rx() -= standard_x_padding + this->width(); |
| 302 |
} |
| 303 |
if (p.y() + standard_y_padding + this->height() > actinglimit.y() + actinglimit.height()) { |
| 304 |
p.ry() -= standard_y_padding + this->height(); |
| 305 |
} |
| 306 |
if (p.y() < actinglimit.y()) { |
| 307 |
p.setY(actinglimit.y()); |
| 308 |
} |
| 309 |
if (p.x() + this->width() > actinglimit.x() + actinglimit.width()) { |
| 310 |
p.setX(actinglimit.x() + actinglimit.width() - this->width()); |
| 311 |
} |
| 312 |
if (p.x() < actinglimit.x()) { |
| 313 |
p.setX(actinglimit.x()); |
| 314 |
} |
| 315 |
if (p.y() + this->height() > actinglimit.y() + actinglimit.height()) { |
| 316 |
p.setY(actinglimit.y() + actinglimit.height() - this->height()); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
this->move(p); |
| 321 |
} |
| 322 |
|
| 323 |
void NotificationLabel::setTipRect(const QRect& restrictionarea) |
| 324 |
{ |
| 325 |
restrictionArea = restrictionarea; |
| 326 |
} |
| 327 |
|
| 328 |
void NotificationLabel::setHideIfReferenceWidgetDeactivated(bool on) |
| 329 |
{ |
| 330 |
hideIfReferenceWidgetDeactivated = on; |
| 331 |
} |
| 332 |
|
| 333 |
bool NotificationLabel::notificationLabelChanged(const QString& text) |
| 334 |
{ |
| 335 |
return NotificationLabel::instance->text() != text; |
| 336 |
} |
| 337 |
|
| 338 |
/***************************** NotificationBox **********************************/ |
| 339 |
|
| 340 |
bool NotificationBox::showText( |
| 341 |
const QPoint& pos, |
| 342 |
const QString& text, |
| 343 |
QWidget* referenceWidget, |
| 344 |
int displayTime, |
| 345 |
unsigned int minShowTime, |
| 346 |
Options options, |
| 347 |
int width |
| 348 |
) |
| 349 |
{ |
| 350 |
QRect restrictionarea = {}; |
| 351 |
|
| 352 |
if (referenceWidget) { |
| 353 |
if (options & Options::OnlyIfReferenceActive) { |
| 354 |
if (!referenceWidget->isActiveWindow()) { |
| 355 |
return false; |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
if (options & Options::RestrictAreaToReference) { |
| 360 |
// Calculate the main window QRect in global screen coordinates. |
| 361 |
auto mainwindowrect = referenceWidget->rect(); |
| 362 |
|
| 363 |
restrictionarea = QRect( |
| 364 |
referenceWidget->mapToGlobal(mainwindowrect.topLeft()), |
| 365 |
mainwindowrect.size() |
| 366 |
); |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
// a label does already exist |
| 371 |
if (NotificationLabel::instance && NotificationLabel::instance->isVisible()) { |
| 372 |
if (text.isEmpty()) { // empty text means hide current label |
| 373 |
NotificationLabel::instance->hideNotification(); |
| 374 |
return false; |
| 375 |
} |
| 376 |
else { |
| 377 |
// If the label has changed, reuse the one that is showing (removes flickering) |
| 378 |
if (NotificationLabel::instance->notificationLabelChanged(text)) { |
| 379 |
NotificationLabel::instance->setTipRect(restrictionarea); |
| 380 |
NotificationLabel::instance->setHideIfReferenceWidgetDeactivated( |
| 381 |
options & Options::HideIfReferenceWidgetDeactivated |
| 382 |
); |
| 383 |
NotificationLabel::instance->reuseNotification(text, displayTime, pos, width); |
| 384 |
NotificationLabel::instance->placeNotificationLabel(pos); |
| 385 |
} |
| 386 |
return true; |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
// no label can be reused, create new label: |
| 391 |
if (!text.isEmpty()) { |
| 392 |
// Note: The Label takes no parent, as on windows, we can't use the widget as parent |
| 393 |
// otherwise the window will be raised when the tooltip will be shown. We do not use |
| 394 |
// it on Linux either for consistency. |
| 395 |
new NotificationLabel( |
| 396 |
text, |
| 397 |
pos, |
| 398 |
displayTime, |
| 399 |
minShowTime, |
| 400 |
width |
| 401 |
); // sets NotificationLabel::instance to itself |
| 402 |
|
| 403 |
NotificationLabel::instance->setTipRect(restrictionarea); |
| 404 |
NotificationLabel::instance->setHideIfReferenceWidgetDeactivated( |
| 405 |
options & Options::HideIfReferenceWidgetDeactivated |
| 406 |
); |
| 407 |
NotificationLabel::instance->placeNotificationLabel(pos); |
| 408 |
NotificationLabel::instance->setObjectName(QLatin1String("NotificationBox_label")); |
| 409 |
|
| 410 |
NotificationLabel::instance->showNormal(); |
| 411 |
} |
| 412 |
|
| 413 |
return true; |
| 414 |
} |
| 415 |
|
| 416 |
bool NotificationBox::isVisible() |
| 417 |
{ |
| 418 |
return (NotificationLabel::instance != nullptr && NotificationLabel::instance->isVisible()); |
| 419 |
} |
| 420 |
|
| 421 |
QString NotificationBox::text() |
| 422 |
{ |
| 423 |
if (NotificationLabel::instance) { |
| 424 |
return NotificationLabel::instance->text(); |
| 425 |
} |
| 426 |
return {}; |
| 427 |
} |
| 428 |
|
| 429 |
Q_GLOBAL_STATIC(QPalette, notificationbox_palette) |
| 430 |
|
| 431 |
QPalette NotificationBox::palette() |
| 432 |
{ |
| 433 |
return *notificationbox_palette(); |
| 434 |
} |
| 435 |
|
| 436 |
QFont NotificationBox::font() |
| 437 |
{ |
| 438 |
return QApplication::font("NotificationLabel"); |
| 439 |
} |
| 440 |
|
| 441 |
void NotificationBox::setPalette(const QPalette& palette) |
| 442 |
{ |
| 443 |
*notificationbox_palette() = palette; |
| 444 |
if (NotificationLabel::instance) { |
| 445 |
NotificationLabel::instance->setPalette(palette); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
void NotificationBox::setFont(const QFont& font) |
| 450 |
{ |
| 451 |
QApplication::setFont(font, "NotificationLabel"); |
| 452 |
} |
| 453 |
|
| 454 |
} // namespace Gui |
| 455 |
|
| 456 |
#include "NotificationBox.moc" |