QInfoListWidget.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include "QInfoListWidget.h"
  2. #include "BootGlobal.h"
  3. #include <QScrollBar>
  4. //static const QColor g_sLeftBackgroundColor = QColor(101, 105, 108, 255); //#65696C
  5. //static const QColor g_sRightBackgroundColor = QColor(161, 163, 165, 255); //#A1A3A5
  6. namespace {
  7. int ColorDodge(int primaryColor, int secondaryColors)
  8. {
  9. if(secondaryColors >= 255) {
  10. return 255;
  11. }
  12. //基色 +(基色 * 混合色)/(255 - 混合色)= 结果色
  13. int result = (primaryColor + (uint)(primaryColor * secondaryColors) / (255 - secondaryColors));
  14. if(result > 255) {
  15. result = 255;
  16. } else if(result < 0) {
  17. result = 0;
  18. }
  19. return result;
  20. }
  21. QColor GetSortedDisplayColor(const QListWidget* parent)
  22. {
  23. const int stepMax = 100;
  24. const int currItemsCount = parent->count();
  25. const int j = (stepMax - currItemsCount % stepMax);
  26. const int gradientR = ColorDodge(205, j);
  27. const int gradientG = ColorDodge(38, j);
  28. const int gradientB = ColorDodge(38, j);
  29. return QColor(gradientR, gradientG, gradientB, 255);
  30. }
  31. }
  32. QBaseListWidgetItem::QBaseListWidgetItem(const QString &text, InfoType it, LevelType lt)
  33. :QListWidgetItem(text),infoType(it),levelType(lt)
  34. {
  35. setFlags(Qt::ItemIsEnabled | Qt::NoItemFlags);
  36. }
  37. void QBaseListWidgetItem::AppentToWidget(QInfoListWidget* container, bool insertFront)
  38. {
  39. if(insertFront)
  40. {
  41. container->insertItem(0, this);
  42. container->setCurrentRow(0);
  43. } else {
  44. container->addItem(this);
  45. container->setCurrentRow(container->count()-1);
  46. }
  47. setForeground(FrontColor());
  48. setBackground(BackgroundColor());
  49. QFont font;
  50. font.setFamily(QStringLiteral("微软雅黑"));
  51. font.setPointSize(10);
  52. this->setFont(font);
  53. if(insertFront) {
  54. container->RemoveDuplicateItem();
  55. }
  56. }
  57. QColor QBaseListWidgetItem::FrontColor() const
  58. {
  59. auto colorValue = Qt::black;
  60. return QColor(Qt::GlobalColor(colorValue));
  61. }
  62. QColor QBaseListWidgetItem::BackgroundColor() const
  63. {
  64. return GetLeftBackgoundColor();
  65. }
  66. QInfoListWidgetItem::QInfoListWidgetItem(const QString &text, LevelType lt)
  67. :QBaseListWidgetItem(text, Info, lt)
  68. {
  69. }
  70. QColor QWarnListWidgetItem::FrontColor() const
  71. {
  72. auto colorValue = Qt::white;
  73. return QColor(colorValue);
  74. }
  75. QColor QLightWarnListWidgetItem::FrontColor() const
  76. {
  77. auto colorValue = Qt::yellow;
  78. return QColor(colorValue);
  79. }
  80. QColor QLightWarnListWidgetItem::BackgroundColor() const
  81. {
  82. auto colorValue = Qt::black;
  83. return QColor(colorValue);
  84. }
  85. QColor QErrorListWidgetItem::FrontColor() const
  86. {
  87. auto colorValue = Qt::darkRed;
  88. return QColor(colorValue);
  89. }
  90. QColor QLightErrorListWidgetItem::FrontColor() const
  91. {
  92. auto colorValue = Qt::red;
  93. return QColor(colorValue);
  94. }
  95. QColor QLightErrorListWidgetItem::BackgroundColor() const
  96. {
  97. auto colorValue = Qt::black;
  98. return QColor(colorValue);
  99. }
  100. QColor QTipErrorListWidgetItem::FrontColor() const
  101. {
  102. auto colorValue = Qt::red;
  103. return QColor(colorValue);
  104. }
  105. QColor QTipErrorListWidgetItem::BackgroundColor() const
  106. {
  107. return QColor(245, 245, 245, 255);
  108. }
  109. QColor QTipListWidgetItem::FrontColor() const
  110. {
  111. QListWidget* parent = listWidget();
  112. if(parent != nullptr)
  113. return GetSortedDisplayColor(parent);
  114. auto colorValue = Qt::darkRed;
  115. return QColor(colorValue);
  116. }
  117. QColor QTipListWidgetItem::BackgroundColor() const
  118. {
  119. return GetRightBackgoundColor();
  120. }
  121. QColor QConsoleListWidgetItem::FrontColor() const
  122. {
  123. return GetConsoleFrontColor();
  124. }
  125. QColor QConsoleListWidgetItem::BackgroundColor() const
  126. {
  127. auto colorValue = Qt::black;
  128. return QColor(colorValue);
  129. }
  130. QInfoListWidget::QInfoListWidget(QWidget* parent)
  131. :QListWidget(parent)
  132. {
  133. Init();
  134. }
  135. void QInfoListWidget::Init()
  136. {
  137. this->setViewMode(QListView::ListMode);
  138. this->setFlow(QListView::TopToBottom);
  139. this->setSortingEnabled(false);
  140. // Hide the scrollbar
  141. this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  142. this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  143. /*disable hightlight when cursor hover*/
  144. this->setStyleSheet("QListWidget::item:hover{background: transparent;}");
  145. /*hide border*/
  146. this->setFrameShape(QListWidget::NoFrame);
  147. }
  148. void QInfoListWidget::WheelEvent(QWheelEvent* event)
  149. {
  150. int degress = event->angleDelta().ry() * 0.5;
  151. if(event->orientation() == Qt::Vertical)
  152. verticalScrollBar()->setValue(verticalScrollBar()->value() - degress);
  153. event->accept();
  154. }
  155. void QLeftInfoListWidget::PostInit()
  156. {
  157. qDebug("QLeftInfoListWidget::PostInit()");
  158. this->setStyleSheet("QListWidget{background: #65696C;outline:0px;}"
  159. "QListWidget::item{padding-top:1px; padding-bottom:1px;}"
  160. "QListWidget::item:hover{background: #65696C;}");
  161. }
  162. void QRightInfoListWidget::PostInit()
  163. {
  164. qDebug("QRightInfoListWidget::PostInit()");
  165. //SetWidgetBackgroundColor(this, GetRightBackgoundColor());
  166. this->setStyleSheet("QListWidget{background: #A1A3A5;outline:0px;}" //去除选中时的虚线
  167. "QListWidget::item:selected,QListWidget::item:hover{background: #A1A3A5;}");
  168. }