/src/gui/TrayIcon.cpp

https://github.com/ksnip/ksnip · C++ · 135 lines · 100 code · 17 blank · 18 comment · 7 complexity · 0637a2c22e9627351a91a4868a271109 MD5 · raw file

  1. /*
  2. * Copyright (C) 2019 Damir Porobic <damir.porobic@gmx.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA.
  18. */
  19. #include "TrayIcon.h"
  20. TrayIcon::TrayIcon(QObject *parent) :
  21. QSystemTrayIcon(parent),
  22. mOpenAction(nullptr),
  23. mSaveAction(nullptr),
  24. mCopyAction(nullptr),
  25. mUploadAction(nullptr),
  26. mShowEditorAction(nullptr),
  27. mQuitAction(nullptr)
  28. {
  29. auto ksnipIcon = QPixmap(":/icons/ksnip.svg");
  30. setIcon(ksnipIcon);
  31. mShowEditorAction = new QAction(tr("Show Editor"), this);
  32. mShowEditorAction->setIcon(ksnipIcon);
  33. connect(mShowEditorAction, &QAction::triggered, this, &TrayIcon::showEditorTriggered);
  34. connect(this, &QSystemTrayIcon::activated, this, &TrayIcon::activated);
  35. connect(this, &QSystemTrayIcon::messageClicked, this, &TrayIcon::openContentUrl);
  36. }
  37. void TrayIcon::setupMenu()
  38. {
  39. mMenu.addAction(mShowEditorAction);
  40. mMenu.addSeparator();
  41. for(auto captureAction : mCaptureActions) {
  42. mMenu.addAction(captureAction);
  43. }
  44. mMenu.addSeparator();
  45. mMenu.addAction(mOpenAction);
  46. mMenu.addAction(mSaveAction);
  47. mMenu.addAction(mCopyAction);
  48. mMenu.addAction(mUploadAction);
  49. mMenu.addSeparator();
  50. mMenu.addAction(mQuitAction);
  51. setContextMenu(&mMenu);
  52. }
  53. TrayIcon::~TrayIcon()
  54. {
  55. delete mShowEditorAction;
  56. }
  57. void TrayIcon::setCaptureActions(const QList<QAction*> &captureActions)
  58. {
  59. mCaptureActions = captureActions;
  60. }
  61. void TrayIcon::setOpenAction(QAction *action)
  62. {
  63. mOpenAction = action;
  64. }
  65. void TrayIcon::setSaveAction(QAction *action)
  66. {
  67. mSaveAction = action;
  68. }
  69. void TrayIcon::setCopyAction(QAction *action)
  70. {
  71. mCopyAction = action;
  72. }
  73. void TrayIcon::setUploadAction(QAction *action)
  74. {
  75. mUploadAction = action;
  76. }
  77. void TrayIcon::setQuitAction(QAction *action)
  78. {
  79. mQuitAction = action;
  80. }
  81. void TrayIcon::setEnabled(bool enabled)
  82. {
  83. if(enabled) {
  84. setupMenu();
  85. show();
  86. } else {
  87. hide();
  88. }
  89. }
  90. void TrayIcon::showInfoToast(const QString &title, const QString &message, const QString &contentUrl)
  91. {
  92. showMessage(title, message, contentUrl, QSystemTrayIcon::Information);
  93. }
  94. void TrayIcon::showWarningToast(const QString &title, const QString &message, const QString &contentUrl)
  95. {
  96. showMessage(title, message, contentUrl, QSystemTrayIcon::Warning);
  97. }
  98. void TrayIcon::showCriticalToast(const QString &title, const QString &message, const QString &contentUrl)
  99. {
  100. showMessage(title, message, contentUrl, QSystemTrayIcon::Critical);
  101. }
  102. void TrayIcon::showMessage(const QString &title, const QString &message, const QString &contentUrl, QSystemTrayIcon::MessageIcon messageIcon)
  103. {
  104. mToastContentUrl = contentUrl;
  105. QSystemTrayIcon::showMessage(title, message, messageIcon);
  106. }
  107. void TrayIcon::activated(ActivationReason reason) const
  108. {
  109. if(reason != ActivationReason::Context) {
  110. emit showEditorTriggered();
  111. }
  112. }
  113. void TrayIcon::openContentUrl()
  114. {
  115. if(!mToastContentUrl.isEmpty() && !mToastContentUrl.isNull()) {
  116. QDesktopServices::openUrl(mToastContentUrl);
  117. }
  118. }