/src/gui/MessageWidget.cpp

https://github.com/keepassxreboot/keepassxc · C++ · 106 lines · 69 code · 14 blank · 23 comment · 9 complexity · afc0b795d199be76c95a7d5aecf04928 MD5 · raw file

  1. /*
  2. * Copyright (C) 2015 Pedro Alves <devel@pgalves.com>
  3. * Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 or (at your option)
  8. * version 3 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "MessageWidget.h"
  19. #include <QDesktopServices>
  20. #include <QTimer>
  21. #include <QUrl>
  22. const int MessageWidget::DefaultAutoHideTimeout = 6000;
  23. const int MessageWidget::LongAutoHideTimeout = 15000;
  24. const int MessageWidget::DisableAutoHide = -1;
  25. MessageWidget::MessageWidget(QWidget* parent)
  26. : KMessageWidget(parent)
  27. , m_autoHideTimer(new QTimer(this))
  28. , m_autoHideTimeout(DefaultAutoHideTimeout)
  29. {
  30. m_autoHideTimer->setSingleShot(true);
  31. connect(m_autoHideTimer, SIGNAL(timeout()), this, SLOT(animatedHide()));
  32. connect(this, SIGNAL(hideAnimationFinished()), m_autoHideTimer, SLOT(stop()));
  33. }
  34. void MessageWidget::setAnimate(bool state)
  35. {
  36. m_animate = state;
  37. }
  38. int MessageWidget::autoHideTimeout() const
  39. {
  40. return m_autoHideTimeout;
  41. }
  42. void MessageWidget::showMessage(const QString& text, MessageWidget::MessageType type)
  43. {
  44. showMessage(text, type, m_autoHideTimeout);
  45. }
  46. void MessageWidget::showMessage(const QString& text, KMessageWidget::MessageType type, int autoHideTimeout)
  47. {
  48. setMessageType(type);
  49. setText(text);
  50. emit showAnimationStarted();
  51. if (m_animate) {
  52. animatedShow();
  53. } else {
  54. show();
  55. emit showAnimationFinished();
  56. }
  57. if (autoHideTimeout > 0) {
  58. m_autoHideTimer->start(autoHideTimeout);
  59. } else {
  60. m_autoHideTimer->stop();
  61. }
  62. }
  63. void MessageWidget::hideMessage()
  64. {
  65. emit hideAnimationStarted();
  66. if (m_animate) {
  67. animatedHide();
  68. } else {
  69. hide();
  70. emit hideAnimationFinished();
  71. }
  72. m_autoHideTimer->stop();
  73. }
  74. void MessageWidget::setAutoHideTimeout(int autoHideTimeout)
  75. {
  76. m_autoHideTimeout = autoHideTimeout;
  77. if (autoHideTimeout <= 0) {
  78. m_autoHideTimer->stop();
  79. }
  80. }
  81. /**
  82. * Open a link using the system's default handler.
  83. * Links that are not HTTP(S) links are ignored.
  84. *
  85. * @param link link URL
  86. */
  87. void MessageWidget::openHttpUrl(const QString& link)
  88. {
  89. if (link.startsWith("http://") || link.startsWith("https://")) {
  90. QDesktopServices::openUrl(QUrl(link));
  91. }
  92. }