/src/gui/src/tag-context-menu.cpp

https://github.com/Bionus/imgbrd-grabber · C++ · 128 lines · 109 code · 13 blank · 6 comment · 12 complexity · 61dbf3d703270be4871d31469089c824 MD5 · raw file

  1. #include "tag-context-menu.h"
  2. #include <QApplication>
  3. #include <QClipboard>
  4. #include <QDesktopServices>
  5. #include <QProcess>
  6. #include "functions.h"
  7. #include "models/profile.h"
  8. #include "tags/tag.h"
  9. TagContextMenu::TagContextMenu(QString tag, QList<Tag> allTags, QUrl browserUrl, Profile *profile, bool setImage, QWidget *parent)
  10. : QMenu(parent), m_tag(std::move(tag)), m_allTags(std::move(allTags)), m_browserUrl(std::move(browserUrl)), m_profile(profile)
  11. {
  12. // Favorites
  13. if (profile->getFavorites().contains(Favorite(m_tag))) {
  14. addAction(QIcon(":/images/icons/remove.png"), tr("Remove from favorites"), this, SLOT(unfavorite()));
  15. if (setImage) {
  16. addAction(QIcon(":/images/icons/save.png"), tr("Choose as image"), this, SLOT(setfavorite()));
  17. }
  18. } else {
  19. addAction(QIcon(":/images/icons/add.png"), tr("Add to favorites"), this, SLOT(favorite()));
  20. }
  21. // Keep for later
  22. if (profile->getKeptForLater().contains(m_tag, Qt::CaseInsensitive)) {
  23. addAction(QIcon(":/images/icons/remove.png"), tr("Don't keep for later"), this, SLOT(unviewitlater()));
  24. } else {
  25. addAction(QIcon(":/images/icons/add.png"), tr("Keep for later"), this, SLOT(viewitlater()));
  26. }
  27. // Blacklist
  28. if (profile->getBlacklist().contains(m_tag)) {
  29. addAction(QIcon(":/images/icons/eye-plus.png"), tr("Don't blacklist"), this, SLOT(unblacklist()));
  30. } else {
  31. addAction(QIcon(":/images/icons/eye-minus.png"), tr("Blacklist"), this, SLOT(blacklist()));
  32. }
  33. // Ignore
  34. if (profile->getIgnored().contains(m_tag, Qt::CaseInsensitive)) {
  35. addAction(QIcon(":/images/icons/eye-plus.png"), tr("Don't ignore"), this, SLOT(unignore()));
  36. } else {
  37. addAction(QIcon(":/images/icons/eye-minus.png"), tr("Ignore"), this, SLOT(ignore()));
  38. }
  39. addSeparator();
  40. // Copy
  41. addAction(QIcon(":/images/icons/copy.png"), tr("Copy tag"), this, SLOT(copyTagToClipboard()));
  42. if (!allTags.isEmpty()) {
  43. addAction(QIcon(":/images/icons/copy.png"), tr("Copy all tags"), this, SLOT(copyAllTagsToClipboard()));
  44. }
  45. addSeparator();
  46. // Tabs
  47. addAction(QIcon(":/images/icons/tab-plus.png"), tr("Open in a new tab"), this, SLOT(openInNewTab()));
  48. addAction(QIcon(":/images/icons/window.png"), tr("Open in new a window"), this, SLOT(openInNewWindow()));
  49. if (!browserUrl.isEmpty()) {
  50. addAction(QIcon(":/images/icons/browser.png"), tr("Open in browser"), this, SLOT(openInBrowser()));
  51. }
  52. }
  53. void TagContextMenu::favorite()
  54. {
  55. Favorite fav(m_tag);
  56. m_profile->addFavorite(fav);
  57. emit setFavoriteImage();
  58. }
  59. void TagContextMenu::setfavorite()
  60. {
  61. emit setFavoriteImage();
  62. }
  63. void TagContextMenu::unfavorite()
  64. {
  65. m_profile->removeFavorite(Favorite(m_tag));
  66. }
  67. void TagContextMenu::viewitlater()
  68. {
  69. m_profile->addKeptForLater(m_tag);
  70. }
  71. void TagContextMenu::unviewitlater()
  72. {
  73. m_profile->removeKeptForLater(m_tag);
  74. }
  75. void TagContextMenu::ignore()
  76. {
  77. m_profile->addIgnored(m_tag);
  78. }
  79. void TagContextMenu::unignore()
  80. {
  81. m_profile->removeIgnored(m_tag);
  82. }
  83. void TagContextMenu::blacklist()
  84. {
  85. m_profile->addBlacklistedTag(m_tag);
  86. }
  87. void TagContextMenu::unblacklist()
  88. {
  89. m_profile->removeBlacklistedTag(m_tag);
  90. }
  91. void TagContextMenu::openInNewTab()
  92. {
  93. emit openNewTab();
  94. }
  95. void TagContextMenu::openInNewWindow()
  96. {
  97. QProcess::startDetached(qApp->arguments().at(0), QStringList(m_tag));
  98. }
  99. void TagContextMenu::openInBrowser()
  100. {
  101. QDesktopServices::openUrl(m_browserUrl);
  102. }
  103. void TagContextMenu::copyTagToClipboard()
  104. {
  105. QApplication::clipboard()->setText(m_tag);
  106. }
  107. void TagContextMenu::copyAllTagsToClipboard()
  108. {
  109. QStringList tags;
  110. tags.reserve(m_allTags.count());
  111. for (const Tag &tag : qAsConst(m_allTags)) {
  112. tags.append(tag.text());
  113. }
  114. QApplication::clipboard()->setText(tags.join(' '));
  115. }