qsplistbox.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. #include "qsplistbox.h"
  2. #include <QListWidgetItem>
  3. #include <QLabel>
  4. #include <QList>
  5. #include <QFileInfo>
  6. #include <QScrollBar>
  7. #include <QPalette>
  8. #include "comtools.h"
  9. #include "qsptextbox.h"
  10. QspListBox::QspListBox(QWidget *parent) : QListWidget(parent)
  11. {
  12. setSelectionMode(QAbstractItemView::NoSelection);
  13. setFocusPolicy(Qt::NoFocus);
  14. setEditTriggers(QAbstractItemView::NoEditTriggers);
  15. setSizeAdjustPolicy(QListWidget::AdjustToContents);
  16. setContentsMargins(0,0,0,0);
  17. setSpacing(0);
  18. setFrameStyle(QFrame::NoFrame);
  19. setFrameShadow(QFrame::Plain);
  20. m_isUseHtml = false;
  21. m_isShowNums = false;
  22. showPlainText = false;
  23. //m_linkColor = palette().color(QPalette::Link);
  24. //m_textColor = palette().color(QPalette::Text);
  25. //m_backgroundColor = QColor(224, 224, 224);
  26. m_selectionColor = palette().color(QPalette::Highlight);
  27. m_font = font();
  28. oldSelection = -1;
  29. m_mouseTracking = false;
  30. }
  31. QspListBox::~QspListBox()
  32. {
  33. }
  34. void QspListBox::RefreshUI()
  35. {
  36. // RefreshAll();
  37. }
  38. void QspListBox::BeginItems()
  39. {
  40. m_newImages.clear();
  41. m_newDescs.clear();
  42. }
  43. void QspListBox::AddItem(const QString& image, const QString& desc)
  44. {
  45. m_newImages.append(image);
  46. m_newDescs.append(desc);
  47. }
  48. void QspListBox::EndItems()
  49. {
  50. size_t count;
  51. if (m_images != m_newImages || m_descs != m_newDescs)
  52. {
  53. m_images = m_newImages;
  54. m_descs = m_newDescs;
  55. count = m_descs.count();
  56. createList();
  57. RefreshUI();
  58. if (count) scrollToItem(item(0));
  59. }
  60. }
  61. void QspListBox::SetIsHtml(bool isHtml)
  62. {
  63. if (m_isUseHtml != isHtml)
  64. {
  65. m_isUseHtml = isHtml;
  66. createList();
  67. RefreshUI();
  68. }
  69. }
  70. void QspListBox::SetIsShowNums(bool isShow)
  71. {
  72. if (m_isShowNums != isShow)
  73. {
  74. m_isShowNums = isShow;
  75. createList();
  76. RefreshUI();
  77. }
  78. }
  79. void QspListBox::SetTextFont(const QFont& new_font)
  80. {
  81. if (m_font != new_font)
  82. {
  83. m_font = new_font;
  84. setFont(new_font);
  85. createList();
  86. }
  87. }
  88. bool QspListBox::SetLinkColor(const QColor &color)
  89. {
  90. if(m_linkColor != color)
  91. {
  92. m_linkColor = color;
  93. createList();
  94. //RefreshUI();
  95. return true;
  96. }
  97. return false;
  98. }
  99. QColor QspListBox::GetLinkColor()
  100. {
  101. return m_linkColor;
  102. }
  103. QColor QspListBox::GetBackgroundColor()
  104. {
  105. return m_backgroundColor;
  106. }
  107. QColor QspListBox::GetForegroundColor()
  108. {
  109. return m_textColor;
  110. }
  111. bool QspListBox::SetBackgroundColor(const QColor &color)
  112. {
  113. if(m_backgroundColor != color)
  114. {
  115. QPalette p = palette();
  116. p.setColor(QPalette::Base, color);
  117. setPalette(p);
  118. m_backgroundColor = color;
  119. createList();
  120. return true;
  121. }
  122. return false;
  123. }
  124. bool QspListBox::SetForegroundColor(const QColor &color)
  125. {
  126. if(m_textColor != color)
  127. {
  128. m_textColor = color;
  129. createList();
  130. return true;
  131. }
  132. return false;
  133. }
  134. void QspListBox::SetSelection(int selection)
  135. {
  136. if(selection != oldSelection)
  137. {
  138. if(selection != -1 && selection < (count() - 1))
  139. if(item(selection) != 0)
  140. scrollToItem(item(selection));
  141. if(selection != -1)
  142. {
  143. QListWidgetItem *curItem =item(selection);
  144. if (curItem != 0)
  145. qobject_cast<QspTextBox*>(itemWidget(curItem))->SetBackgroundColor(m_selectionColor);
  146. }
  147. if(oldSelection != -1)
  148. {
  149. QListWidgetItem *curItem =item(oldSelection);
  150. if (curItem != 0)
  151. qobject_cast<QspTextBox*>(itemWidget(curItem))->SetBackgroundColor(m_backgroundColor);
  152. }
  153. oldSelection = selection;
  154. emit SelectionChange(selection);
  155. }
  156. }
  157. void QspListBox::SetShowPlainText(bool isPlain)
  158. {
  159. showPlainText = isPlain;
  160. createList();
  161. RefreshUI();
  162. }
  163. void QspListBox::SetMouseTracking(bool trackMouse)
  164. {
  165. m_mouseTracking = trackMouse;
  166. viewport()->setMouseTracking(trackMouse);
  167. }
  168. void QspListBox::createList()
  169. {
  170. //clear(); //NOTE: clear() only deletes items but does not delete the widgets belonging to it. The widgets will be deleted if the QListWidget is deleted.
  171. //bool oldState = blockSignals(true);
  172. for(int i = 0; i<count(); i++)
  173. {
  174. removeItemWidget(item(i));
  175. }
  176. clear();
  177. for(int i = 0; i < qMin(m_images.size(), m_descs.size()); i++)
  178. {
  179. QString item_tmp;
  180. item_tmp = formatItem(i);
  181. QListWidgetItem* listItem;
  182. listItem = new QListWidgetItem(this);
  183. listItem->setBackground(m_backgroundColor);
  184. addItem(listItem);
  185. QspTextBox *item_widget;
  186. item_widget = new QspTextBox(this);
  187. //item_widget->setFrameStyle(QFrame::Box);
  188. item_widget->setLineWidth(0);
  189. item_widget->viewport()->setMouseTracking(false);
  190. item_widget->setAttribute(Qt::WA_TransparentForMouseEvents);
  191. item_widget->SetIsHtml(m_isUseHtml);
  192. item_widget->SetShowPlainText(showPlainText);
  193. item_widget->SetDisableVideo(true);
  194. item_widget->SetLinkColor(m_linkColor);
  195. item_widget->SetBackgroundColor(m_backgroundColor);
  196. item_widget->SetForegroundColor(m_textColor);
  197. item_widget->SetTextFont(m_font);
  198. item_widget->SetGamePath(m_path);
  199. //item_widget->setMaximumHeight(800);
  200. item_widget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  201. item_widget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  202. item_widget->verticalScrollBar()->setEnabled(false);
  203. item_widget->horizontalScrollBar()->setEnabled(false);
  204. item_widget->setReadOnly(true);
  205. item_widget->setBackgroundRole(QPalette::NoRole);
  206. item_widget->setTextInteractionFlags(Qt::NoTextInteraction);
  207. //item_widget->setWordWrapMode(QTextOption::NoWrap);
  208. item_widget->setWordWrapMode(QTextOption::WordWrap);
  209. //QFontMetrics font_metrics(item_widget->font());
  210. //item_widget->setFixedHeight(font_metrics.height() + 4* item_widget->frameWidth());
  211. item_widget->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContentsOnFirstShow);
  212. item_widget->sizePolicy().setHorizontalPolicy(QSizePolicy::Expanding);
  213. item_widget->sizePolicy().setVerticalPolicy(QSizePolicy::Expanding);
  214. item_widget->SetText(item_tmp);
  215. //QSize sizehint = QSize(item->sizeHint().width(), font_metrics.height()*2 + item_widget->frameWidth()*2);
  216. //QSize sizehint = item_widget->sizeHint();
  217. //sizehint.setHeight(item_widget->document()->size().toSize().height());
  218. //sizehint.setHeight(item_widget->heightForWidth(this->width()));
  219. item_widget->document()->setTextWidth(this->width() - style()->pixelMetric(QStyle::PM_ScrollBarExtent) - 4 - item_widget->frameWidth()*4);
  220. QSize sizehint = QSize(this->width() - style()->pixelMetric(QStyle::PM_ScrollBarExtent) - 4 - item_widget->frameWidth()*4, item_widget->document()->size().toSize().height() + item_widget->frameWidth()*2);
  221. listItem->setSizeHint(sizehint);
  222. setItemWidget(listItem, item_widget);
  223. }
  224. if(oldSelection != -1)
  225. {
  226. QListWidgetItem *curItem =item(oldSelection);
  227. if (curItem != 0)
  228. qobject_cast<QspTextBox*>(itemWidget(curItem))->SetBackgroundColor(m_selectionColor);
  229. }
  230. adjustSize();
  231. //resizeEvent(0);
  232. //blockSignals(oldState);
  233. }
  234. QString QspListBox::formatItem(int itemIndex)
  235. {
  236. if(itemIndex >= m_images.size() || itemIndex >= m_descs.size())
  237. return QString("");
  238. bool isImage = false;
  239. QString imgPath;
  240. if(!m_images.at(itemIndex).isEmpty())
  241. {
  242. QFileInfo imgFile(m_path + m_images.at(itemIndex));
  243. if (imgFile.exists() && imgFile.isFile())
  244. {
  245. imgPath = imgFile.absoluteFilePath();
  246. isImage = true;
  247. }
  248. }
  249. QString color(QSPTools::GetHexColor(GetForegroundColor()));
  250. QString formatedText;
  251. if (m_isShowNums && itemIndex < 9)
  252. {
  253. if (isImage)
  254. {
  255. return QString("<table cellspacing = 4 cellpadding = 0><td>[%1]</td><td><img src=\"%2\"></td><td WIDTH = 100%>%3</td></table>")
  256. .arg(itemIndex+1)
  257. .arg(imgPath)
  258. .arg(m_descs.at(itemIndex));
  259. }
  260. else
  261. {
  262. return QString("<table cellspacing = 4 cellpadding = 0><tr><td>[%1]</td><td width = 100%>%2</td></td></table>")
  263. .arg(itemIndex+1)
  264. .arg(m_descs.at(itemIndex));
  265. }
  266. }
  267. else
  268. {
  269. if(isImage)
  270. {
  271. return QString("<table cellspacing = 4 cellpadding = 0><td><img src=\"%2\"></td><td WIDTH = 100%>%3</td></table>")
  272. .arg(imgPath)
  273. .arg(m_descs.at(itemIndex));
  274. }
  275. if(!m_descs.at(itemIndex).isEmpty())
  276. {
  277. return m_descs.at(itemIndex);
  278. }
  279. }
  280. return formatedText;
  281. //TODO: make this variant work
  282. if(m_descs.at(itemIndex).isEmpty())
  283. {
  284. formatedText = "";
  285. }
  286. else
  287. {
  288. QString text(QSPTools::HtmlizeWhitespaces(m_isUseHtml ? m_descs.at(itemIndex) : QSPTools::ProceedAsPlain(m_descs.at(itemIndex))));
  289. formatedText = QString("<div style=\"padding:0px; margin-right:4px;\">%1</div>").arg(text);
  290. formatedText = m_descs.at(itemIndex);
  291. }
  292. if (m_isShowNums && itemIndex < 9)
  293. {
  294. if (isImage)
  295. {
  296. return QString("<div style=\"color : #%1; -qt-block-indent:0; text-indent:0px;\"><div style=\"padding:0px; margin-right:4px;\">[%2]</div><div style=\"padding:0px; margin-right:4px;\"><img src=\"%3\"></div>%4</div>")
  297. .arg(color)
  298. .arg(itemIndex+1)
  299. .arg(imgPath)
  300. .arg(formatedText);
  301. }
  302. else
  303. {
  304. return QString("<div style=\"color : #%1; -qt-block-indent:0; text-indent:0px;\"><div style=\"padding:0px; margin-right:4px;\">[%2]</div>%3</div>")
  305. .arg(color)
  306. .arg(itemIndex+1)
  307. .arg(formatedText);
  308. }
  309. }
  310. else
  311. {
  312. if (isImage)
  313. {
  314. return QString("<div style=\"color : #%1; -qt-block-indent:0; text-indent:0px;\"><div style=\"padding:0px; margin-right:4px;\"><img src=\"%2\"></div>%3</div>")
  315. .arg(color)
  316. .arg(imgPath)
  317. .arg(formatedText);
  318. }
  319. else
  320. {
  321. return QString("<div style=\"color : #%1; -qt-block-indent:0; text-indent:0px;\">%2</div>")
  322. .arg(color)
  323. .arg(formatedText);
  324. }
  325. }
  326. }
  327. void QspListBox::resizeEvent(QResizeEvent *e)
  328. {
  329. for(int i = 0; i<count(); i++)
  330. {
  331. QListWidgetItem* listItem = item(i);
  332. if(listItem != 0)
  333. {
  334. QspTextBox *item_widget = qobject_cast<QspTextBox*>(itemWidget(listItem));
  335. if(item_widget != 0)
  336. {
  337. item_widget->document()->setTextWidth(this->width() - style()->pixelMetric(QStyle::PM_ScrollBarExtent) - 4 - item_widget->frameWidth()*4);
  338. QSize sizehint = QSize(this->width() - style()->pixelMetric(QStyle::PM_ScrollBarExtent) - 4 - item_widget->frameWidth()*4, item_widget->document()->size().toSize().height() + item_widget->frameWidth()*2);
  339. listItem->setSizeHint(sizehint);
  340. }
  341. }
  342. }
  343. QListWidget::resizeEvent(e);
  344. }
  345. void QspListBox::mouseMoveEvent(QMouseEvent *event)
  346. {
  347. if(m_mouseTracking)
  348. {
  349. QListWidgetItem *curItem = itemAt(event->pos());
  350. if (curItem != 0)
  351. {
  352. SetSelection(row(curItem));
  353. }
  354. }
  355. QListWidget::mouseMoveEvent(event);
  356. }