PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/plugins/messagewidgets/noticewidget.cpp

https://github.com/Rambler-ru/Contacts
C++ | 190 lines | 161 code | 29 blank | 0 comment | 17 complexity | 724d45ee259de626ed6b359647c32538 MD5 | raw file
  1. #include "noticewidget.h"
  2. #include <QPainter>
  3. #include <QPaintEvent>
  4. #include <QDesktopServices>
  5. ChatNoticeWidget::ChatNoticeWidget(IMessageWidgets *AMessageWidgets, const Jid &AStreamJid, const Jid &AContactJid)
  6. {
  7. ui.setupUi(this);
  8. setVisible(false);
  9. StyleStorage::staticStorage(RSR_STORAGE_STYLESHEETS)->insertAutoStyle(this,STS_MESSAGEWIDGETS_NOTICEWIDGET);
  10. StyleStorage::staticStorage(RSR_STORAGE_STYLESHEETS)->insertAutoStyle(ui.cbtClose,STS_MESSAGEWIDGETS_NOTICECLOSEBUTTON);
  11. #ifdef Q_WS_MAC
  12. ui.wdtButtons->layout()->setSpacing(16);
  13. #endif
  14. FMessageWidgets = AMessageWidgets;
  15. FStreamJid = AStreamJid;
  16. FContactJid = AContactJid;
  17. FActiveNotice = -1;
  18. FUpdateTimer.setSingleShot(true);
  19. connect(&FUpdateTimer,SIGNAL(timeout()),SLOT(onUpdateTimerTimeout()));
  20. FCloseTimer.setSingleShot(true);
  21. connect(&FCloseTimer,SIGNAL(timeout()),SLOT(onCloseTimerTimeout()));
  22. connect(ui.cbtClose,SIGNAL(clicked(bool)),SLOT(onCloseButtonClicked(bool)));
  23. connect(ui.lblMessage,SIGNAL(linkActivated(const QString &)),SLOT(onMessageLinkActivated(const QString &)));
  24. }
  25. ChatNoticeWidget::~ChatNoticeWidget()
  26. {
  27. foreach(int noticeId, FNotices.keys()) {
  28. removeNotice(noticeId); }
  29. }
  30. const Jid &ChatNoticeWidget::streamJid() const
  31. {
  32. return FStreamJid;
  33. }
  34. void ChatNoticeWidget::setStreamJid(const Jid &AStreamJid)
  35. {
  36. if (AStreamJid != FStreamJid)
  37. {
  38. Jid befour = FStreamJid;
  39. FStreamJid = AStreamJid;
  40. emit streamJidChanged(befour);
  41. }
  42. }
  43. const Jid & ChatNoticeWidget::contactJid() const
  44. {
  45. return FContactJid;
  46. }
  47. void ChatNoticeWidget::setContactJid(const Jid &AContactJid)
  48. {
  49. if (AContactJid != FContactJid)
  50. {
  51. Jid befour = FContactJid;
  52. FContactJid = AContactJid;
  53. emit contactJidChanged(befour);
  54. }
  55. }
  56. int ChatNoticeWidget::activeNotice() const
  57. {
  58. return FActiveNotice;
  59. }
  60. QList<int> ChatNoticeWidget::noticeQueue() const
  61. {
  62. return FNoticeQueue.values();
  63. }
  64. IChatNotice ChatNoticeWidget::noticeById(int ANoticeId) const
  65. {
  66. return FNotices.value(ANoticeId);
  67. }
  68. int ChatNoticeWidget::insertNotice(const IChatNotice &ANotice)
  69. {
  70. int noticeId = -1;
  71. if (ANotice.priority>0)
  72. {
  73. while (noticeId<=0 || FNotices.contains(noticeId))
  74. noticeId = qrand();
  75. FNotices.insert(noticeId,ANotice);
  76. FNoticeQueue.insertMulti(ANotice.priority,noticeId);
  77. emit noticeInserted(noticeId);
  78. updateNotice();
  79. }
  80. return noticeId;
  81. }
  82. void ChatNoticeWidget::removeNotice(int ANoticeId)
  83. {
  84. if (FNotices.contains(ANoticeId))
  85. {
  86. IChatNotice notice = FNotices.take(ANoticeId);
  87. FNoticeQueue.remove(notice.priority,ANoticeId);
  88. qDeleteAll(notice.actions);
  89. emit noticeRemoved(ANoticeId);
  90. updateNotice();
  91. }
  92. }
  93. void ChatNoticeWidget::updateNotice()
  94. {
  95. FUpdateTimer.start();
  96. }
  97. void ChatNoticeWidget::updateWidgets(int ANoticeId)
  98. {
  99. if (FActiveNotice != ANoticeId)
  100. {
  101. FButtonsCleanup.clear();
  102. if (ANoticeId > 0)
  103. {
  104. const IChatNotice &notice = FNotices.value(ANoticeId);
  105. if (!notice.iconKey.isEmpty() && !notice.iconStorage.isEmpty())
  106. IconStorage::staticStorage(notice.iconStorage)->insertAutoIcon(ui.lblIcon,notice.iconKey,0,0,"pixmap");
  107. else if (!notice.icon.isNull())
  108. ui.lblIcon->setPixmap(notice.icon.pixmap(notice.icon.availableSizes().value(0)));
  109. else
  110. ui.lblIcon->setVisible(false);
  111. ui.lblMessage->setText(notice.message);
  112. if (notice.timeout > 0)
  113. FCloseTimer.start(notice.timeout);
  114. else
  115. FCloseTimer.stop();
  116. foreach(Action *action, notice.actions)
  117. {
  118. ActionButton *button = new ActionButton(action, ui.wdtButtons);
  119. button->addTextFlag(TF_LIGHTSHADOW);
  120. button->setObjectName(action->property("actionName").toString());
  121. ui.hltButtonsLayout->insertWidget(ui.hltButtonsLayout->count()-1,button);
  122. FButtonsCleanup.add(button);
  123. }
  124. setVisible(true);
  125. }
  126. else
  127. {
  128. FCloseTimer.stop();
  129. setVisible(false);
  130. }
  131. FActiveNotice = ANoticeId;
  132. emit noticeActivated(ANoticeId);
  133. }
  134. }
  135. void ChatNoticeWidget::paintEvent(QPaintEvent *AEvent)
  136. {
  137. QStyleOption opt;
  138. opt.init(this);
  139. QPainter p(this);
  140. p.setClipRect(AEvent->rect());
  141. style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
  142. }
  143. void ChatNoticeWidget::onUpdateTimerTimeout()
  144. {
  145. updateWidgets(!FNoticeQueue.isEmpty() ? FNoticeQueue.values().first() : -1);
  146. }
  147. void ChatNoticeWidget::onCloseTimerTimeout()
  148. {
  149. if (!underMouse())
  150. removeNotice(FActiveNotice);
  151. else
  152. FCloseTimer.start(500);
  153. }
  154. void ChatNoticeWidget::onCloseButtonClicked(bool)
  155. {
  156. removeNotice(FActiveNotice);
  157. }
  158. void ChatNoticeWidget::onMessageLinkActivated(const QString &ALink)
  159. {
  160. QDesktopServices::openUrl(ALink);
  161. }