PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/traverso/widgets/MessageWidget.cpp

#
C++ | 243 lines | 166 code | 51 blank | 26 comment | 24 complexity | 75c6dc113bd83971c2025c7a50089eca MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-2.0
  1. /*
  2. Copyright (C) 2005-2007 Remon Sijrier
  3. This file is part of Traverso
  4. Traverso 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. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "MessageWidget.h"
  17. #include <QPainter>
  18. #include <QPushButton>
  19. #include <QTextBrowser>
  20. #include <QHBoxLayout>
  21. #include <QStyle>
  22. #include <QDialog>
  23. #include <Utils.h>
  24. #include "Themer.h"
  25. #include <QDir>
  26. // Always put me below _all_ includes, this is needed
  27. // in case we run with memory leak detection enabled!
  28. #include "Debugger.h"
  29. MessageWidget::MessageWidget( QWidget * parent )
  30. : QWidget(parent)
  31. {
  32. QHBoxLayout* lay = new QHBoxLayout;
  33. lay->setMargin(1);
  34. m_button = new QPushButton;
  35. m_button->setIcon(style()->standardIcon(QStyle::SP_MessageBoxInformation).pixmap(14, 14));
  36. m_button->setMaximumHeight(20);
  37. m_button->setFocusPolicy(Qt::NoFocus);
  38. MessageWidgetPrivate* message = new MessageWidgetPrivate(this);
  39. lay->addSpacing(6);
  40. lay->addWidget(message);
  41. lay->addWidget(m_button);
  42. lay->addSpacing(6);
  43. setLayout(lay);
  44. connect(m_button, SIGNAL(clicked( bool )), message, SLOT(show_history()));
  45. }
  46. QSize MessageWidget::sizeHint() const
  47. {
  48. return QSize(300, 20);
  49. }
  50. MessageWidgetPrivate::MessageWidgetPrivate( QWidget * parent )
  51. : QWidget(parent)
  52. {
  53. QHBoxLayout* lay = new QHBoxLayout;
  54. lay->addStretch(5);
  55. setLayout(lay);
  56. m_log = 0;
  57. connect(&info(), SIGNAL(message(InfoStruct)), this, SLOT(queue_message(InfoStruct)));
  58. connect(&m_messageTimer, SIGNAL(timeout()), this, SLOT(dequeue_messagequeue()));
  59. }
  60. void MessageWidgetPrivate::paintEvent(QPaintEvent* )
  61. {
  62. QPainter painter(this);
  63. if (m_infoStruct.message.isEmpty() ) {
  64. // painter.fillRect(0, 0, width(), height(), themer()->get_color("InfoWidget:background") );
  65. return;
  66. }
  67. QPixmap pm;
  68. switch(m_infoStruct.type) {
  69. case INFO :
  70. pm = style()->standardIcon(QStyle::SP_MessageBoxInformation).pixmap(16, 16);
  71. painter.fillRect(0, 0, width(), height(), QColor("#F4FFF4"));
  72. break;
  73. case WARNING :
  74. pm = style()->standardIcon(QStyle::SP_MessageBoxWarning).pixmap(16, 16);
  75. painter.fillRect(0, 0, width(), height(), QColor("#FDFFD1"));
  76. break;
  77. case CRITICAL :
  78. pm = style()->standardIcon(QStyle::SP_MessageBoxCritical).pixmap(16, 16);
  79. painter.fillRect(0, 0, width(), height(), QColor("#FFC8C8"));
  80. break;
  81. default :
  82. pm = style()->standardIcon(QStyle::SP_MessageBoxInformation).pixmap(16, 16);
  83. }
  84. int stringLength = m_infoStruct.message.length();
  85. int begin = ( width() / 2 ) - ( (stringLength * 8) / 2 ) ;
  86. if (begin < 40)
  87. begin = 40;
  88. painter.setFont(themer()->get_font("MessageWidget:fontscale:log"));
  89. painter.setRenderHint(QPainter::TextAntialiasing);
  90. painter.drawText(begin, 16, m_infoStruct.message);
  91. painter.drawPixmap(begin - 35, 3, pm);
  92. }
  93. void MessageWidgetPrivate::resizeEvent(QResizeEvent* )
  94. {
  95. PENTER3;
  96. update();
  97. }
  98. void MessageWidgetPrivate::queue_message( InfoStruct infostruct)
  99. {
  100. m_messageQueue.enqueue(infostruct);
  101. if (!m_messageTimer.isActive()) {
  102. m_infoStruct = m_messageQueue.dequeue();
  103. update();
  104. }
  105. if (m_messageQueue.size() <= 1) {
  106. m_messageTimer.start(10000);
  107. }
  108. // If Queue size is >= 1 start to dequeue faster.
  109. if (m_messageQueue.size() >= 1) {
  110. m_messageTimer.start(1000);
  111. }
  112. if (m_messageQueue.size() > 3) {
  113. int skip = m_messageQueue.size() - 3;
  114. for (int i=0; i<skip; ++i) {
  115. dequeue_messagequeue();
  116. }
  117. }
  118. log(infostruct);
  119. }
  120. void MessageWidgetPrivate::dequeue_messagequeue( )
  121. {
  122. if (m_messageQueue.isEmpty()) {
  123. m_infoStruct.message = "";
  124. m_messageTimer.stop();
  125. }
  126. if (!m_messageQueue.isEmpty()) {
  127. // If Queue size == 1 it means, it's the last message.Display it the "normal time duration"
  128. if (m_messageQueue.size() == 1) {
  129. m_messageTimer.start(3000);
  130. }
  131. m_infoStruct = m_messageQueue.dequeue();
  132. }
  133. update();
  134. }
  135. void MessageWidgetPrivate::log(InfoStruct infostruct)
  136. {
  137. QString time = "<td width=65>" + QTime::currentTime().toString().append(" :") + " </td>";
  138. QString color;
  139. QString iconname;
  140. if (infostruct.type == INFO) {
  141. iconname = "iconinfo";
  142. color = "bgcolor=#F4FFF4";
  143. } else if (infostruct.type == WARNING) {
  144. iconname = "iconwarning";
  145. color = "bgcolor=#FDFFD1";
  146. } else if (infostruct.type == CRITICAL) {
  147. iconname = "iconcritical";
  148. color = "bgcolor=#FFC8C8";
  149. }
  150. QString image = "<td width=20><img src=\"" + iconname +"\"/></td>";
  151. QString string = "<table width=100% " + color + " cellspacing=5><tr>" +
  152. image + time + "<td>" + infostruct.message + "</td></tr></table>";
  153. if (m_log) {
  154. m_log->append(string);
  155. } else {
  156. m_stringLog.append(string);
  157. }
  158. }
  159. QSize MessageWidgetPrivate::sizeHint() const
  160. {
  161. return QSize(300, 22);
  162. }
  163. void MessageWidgetPrivate::show_history()
  164. {
  165. if (!m_log) {
  166. m_logDialog = new QDialog(this);
  167. m_log = new QTextBrowser(m_logDialog);
  168. QImage img = style()->standardIcon(QStyle::SP_MessageBoxInformation).pixmap(15, 15).toImage();
  169. m_log->document()->addResource(QTextDocument::ImageResource, QUrl("iconinfo"), img);
  170. img = style()->standardIcon(QStyle::SP_MessageBoxWarning).pixmap(15, 15).toImage();
  171. m_log->document()->addResource(QTextDocument::ImageResource, QUrl("iconwarning"), img);
  172. img = style()->standardIcon(QStyle::SP_MessageBoxCritical).pixmap(15, 15).toImage();
  173. m_log->document()->addResource(QTextDocument::ImageResource, QUrl("iconcritical"), img);
  174. QHBoxLayout* lay = new QHBoxLayout(m_logDialog);
  175. m_logDialog->setLayout(lay);
  176. lay->addWidget(m_log);
  177. m_logDialog->resize(500, 200);
  178. m_log->append(m_stringLog);
  179. m_stringLog.clear();
  180. }
  181. if (m_logDialog->isHidden()) {
  182. m_logDialog->show();
  183. } else {
  184. m_logDialog->hide();
  185. }
  186. }
  187. //eof