ctkFittedTextBrowser.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QDebug>
  16. #include <QScrollBar>
  17. #include <QTextBlock>
  18. #include <QAbstractTextDocumentLayout>
  19. // CTK includes
  20. #include "ctkFittedTextBrowser.h"
  21. #include "ctkFittedTextBrowser_p.h"
  22. static const char moreAnchor[] = "show_details";
  23. static const char lessAnchor[] = "hide_details";
  24. //-----------------------------------------------------------------------------
  25. ctkFittedTextBrowserPrivate::ctkFittedTextBrowserPrivate(ctkFittedTextBrowser& object)
  26. :q_ptr(&object)
  27. {
  28. this->Collapsed = true;
  29. this->CollapsibleTextSetter = ctkFittedTextBrowserPrivate::Text;
  30. this->ShowDetailsText = object.tr("Show details...");
  31. this->HideDetailsText = object.tr("Hide details.");
  32. QString HideDetailsText;
  33. }
  34. //-----------------------------------------------------------------------------
  35. ctkFittedTextBrowserPrivate::~ctkFittedTextBrowserPrivate()
  36. {
  37. }
  38. //-----------------------------------------------------------------------------
  39. void ctkFittedTextBrowserPrivate::updateCollapsedText()
  40. {
  41. Q_Q(ctkFittedTextBrowser);
  42. bool html = (this->CollapsibleTextSetter == ctkFittedTextBrowserPrivate::Html || this->CollapsibleText.indexOf("<html>") >= 0);
  43. if (html)
  44. {
  45. q->setHtml(this->collapsedTextFromHtml());
  46. }
  47. else
  48. {
  49. q->setHtml(this->collapsedTextFromPlainText());
  50. }
  51. }
  52. //-----------------------------------------------------------------------------
  53. QString ctkFittedTextBrowserPrivate::collapsedTextFromPlainText() const
  54. {
  55. int teaserEndPosition = this->CollapsibleText.indexOf("\n");
  56. if (teaserEndPosition < 0)
  57. {
  58. return this->CollapsibleText;
  59. }
  60. QString finalText;
  61. finalText.append("<html>");
  62. finalText.append(this->Collapsed ? this->CollapsibleText.left(teaserEndPosition) : this->CollapsibleText);
  63. finalText.append(this->collapseLinkText());
  64. finalText.append("</html>");
  65. // Remove line break to allow continuation of line.
  66. finalText.replace(finalText.indexOf('\n'), 1, "");
  67. // In plain text line breaks were indicated by newline, but we now use html,
  68. // so line breaks must use <br>
  69. finalText.replace("\n", "<br>");
  70. return finalText;
  71. }
  72. //-----------------------------------------------------------------------------
  73. QString ctkFittedTextBrowserPrivate::collapsedTextFromHtml() const
  74. {
  75. const QString lineBreak("<br>");
  76. int teaserEndPosition = this->CollapsibleText.indexOf(lineBreak);
  77. if (teaserEndPosition < 0)
  78. {
  79. return this->CollapsibleText;
  80. }
  81. QString finalText = this->CollapsibleText;
  82. if (this->Collapsed)
  83. {
  84. finalText = finalText.left(teaserEndPosition) + this->collapseLinkText();
  85. // By truncating the full text we might have deleted the closing </html> tag
  86. // restore it now.
  87. if (finalText.contains("<html") && !finalText.contains("</html"))
  88. {
  89. finalText.append("</html>");
  90. }
  91. }
  92. else
  93. {
  94. // Remove <br> to allow continuation of line and avoid extra space
  95. // when <p> element is used as well.
  96. finalText.replace(finalText.indexOf(lineBreak), lineBreak.size(), "");
  97. // Add link text before closing </body> or </html> tag
  98. if (finalText.contains("</body>"))
  99. {
  100. finalText.replace("</body>", this->collapseLinkText() + "</body>");
  101. }
  102. else if (finalText.contains("</html>"))
  103. {
  104. finalText.replace("</html>", this->collapseLinkText() + "</html>");
  105. }
  106. else
  107. {
  108. finalText.append(this->collapseLinkText());
  109. }
  110. }
  111. return finalText;
  112. }
  113. //-----------------------------------------------------------------------------
  114. QString ctkFittedTextBrowserPrivate::collapseLinkText() const
  115. {
  116. if (this->Collapsed)
  117. {
  118. return QString("&hellip; <a href=\"#") + moreAnchor + "\">" + this->ShowDetailsText + "</a>";
  119. }
  120. else
  121. {
  122. return QString(" <a href=\"#") + lessAnchor + "\">" + this->HideDetailsText + "</a>";
  123. }
  124. }
  125. //-----------------------------------------------------------------------------
  126. ctkFittedTextBrowser::ctkFittedTextBrowser(QWidget* _parent)
  127. : QTextBrowser(_parent)
  128. , d_ptr(new ctkFittedTextBrowserPrivate(*this))
  129. {
  130. this->connect(this, SIGNAL(textChanged()), SLOT(heightForWidthMayHaveChanged()));
  131. QSizePolicy newSizePolicy = QSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
  132. this->setSizePolicy(newSizePolicy);
  133. this->connect(this, SIGNAL(anchorClicked(QUrl)), SLOT(anchorClicked(QUrl)));
  134. }
  135. //-----------------------------------------------------------------------------
  136. ctkFittedTextBrowser::~ctkFittedTextBrowser()
  137. {
  138. }
  139. //-----------------------------------------------------------------------------
  140. void ctkFittedTextBrowser::heightForWidthMayHaveChanged()
  141. {
  142. this->updateGeometry();
  143. }
  144. //-----------------------------------------------------------------------------
  145. int ctkFittedTextBrowser::heightForWidth(int _width) const
  146. {
  147. QTextDocument* doc = this->document();
  148. qreal savedWidth = doc->textWidth();
  149. // Fudge factor. This is the difference between the frame and the
  150. // viewport.
  151. int fudge = 2 * this->frameWidth();
  152. int horizontalScrollbarHeight = 0;
  153. if (this->horizontalScrollBar()->isVisible())
  154. {
  155. horizontalScrollbarHeight = this->horizontalScrollBar()->height() + fudge;
  156. }
  157. // Do the calculation assuming no scrollbars
  158. doc->setTextWidth(_width - fudge);
  159. int noScrollbarHeight =
  160. doc->documentLayout()->documentSize().height() + fudge;
  161. // (If noScrollbarHeight is greater than the maximum height we'll be
  162. // allowed, then there will be scrollbars, and the actual required
  163. // height will be even higher. But since in this case we've already
  164. // hit the maximum height, it doesn't matter that we underestimate.)
  165. // Get minimum height (even if string is empty): one line of text
  166. int _minimumHeight = QFontMetrics(doc->defaultFont()).lineSpacing() + fudge;
  167. int ret = qMax(noScrollbarHeight, _minimumHeight) + horizontalScrollbarHeight;
  168. doc->setTextWidth(savedWidth);
  169. return ret;
  170. }
  171. //-----------------------------------------------------------------------------
  172. QSize ctkFittedTextBrowser::minimumSizeHint() const {
  173. QSize s(this->size().width(), 0);
  174. if (s.width() == 0)
  175. {
  176. //s.setWidth(400); // arbitrary value
  177. return QTextBrowser::minimumSizeHint();
  178. }
  179. s.setHeight(this->heightForWidth(s.width()));
  180. return s;
  181. }
  182. //-----------------------------------------------------------------------------
  183. QSize ctkFittedTextBrowser::sizeHint() const {
  184. return this->minimumSizeHint();
  185. }
  186. //-----------------------------------------------------------------------------
  187. void ctkFittedTextBrowser::resizeEvent(QResizeEvent* e)
  188. {
  189. this->QTextBrowser::resizeEvent(e);
  190. if (e->size().height() != this->heightForWidth(e->size().width()))
  191. {
  192. this->heightForWidthMayHaveChanged();
  193. }
  194. }
  195. //-----------------------------------------------------------------------------
  196. void ctkFittedTextBrowser::setCollapsibleText(const QString &text)
  197. {
  198. Q_D(ctkFittedTextBrowser);
  199. d->CollapsibleTextSetter = ctkFittedTextBrowserPrivate::Text;
  200. d->CollapsibleText = text;
  201. d->updateCollapsedText();
  202. }
  203. //-----------------------------------------------------------------------------
  204. QString ctkFittedTextBrowser::collapsibleText() const
  205. {
  206. Q_D(const ctkFittedTextBrowser);
  207. return d->CollapsibleText;
  208. }
  209. //-----------------------------------------------------------------------------
  210. void ctkFittedTextBrowser::setCollapsiblePlainText(const QString &text)
  211. {
  212. Q_D(ctkFittedTextBrowser);
  213. d->CollapsibleTextSetter = ctkFittedTextBrowserPrivate::PlainText;
  214. d->CollapsibleText = text;
  215. d->updateCollapsedText();
  216. }
  217. //-----------------------------------------------------------------------------
  218. void ctkFittedTextBrowser::setCollapsibleHtml(const QString &text)
  219. {
  220. Q_D(ctkFittedTextBrowser);
  221. d->CollapsibleTextSetter = ctkFittedTextBrowserPrivate::Html;
  222. d->CollapsibleText = text;
  223. d->updateCollapsedText();
  224. }
  225. //-----------------------------------------------------------------------------
  226. void ctkFittedTextBrowser::anchorClicked(const QUrl &url)
  227. {
  228. if (url.path().isEmpty())
  229. {
  230. if (url.fragment() == moreAnchor)
  231. {
  232. this->setCollapsed(false);
  233. }
  234. else if (url.fragment() == lessAnchor)
  235. {
  236. this->setCollapsed(true);
  237. }
  238. }
  239. }
  240. //-----------------------------------------------------------------------------
  241. void ctkFittedTextBrowser::setCollapsed(bool collapsed)
  242. {
  243. Q_D(ctkFittedTextBrowser);
  244. if (d->Collapsed == collapsed)
  245. {
  246. // no change
  247. return;
  248. }
  249. d->Collapsed = collapsed;
  250. d->updateCollapsedText();
  251. }
  252. //-----------------------------------------------------------------------------
  253. bool ctkFittedTextBrowser::collapsed() const
  254. {
  255. Q_D(const ctkFittedTextBrowser);
  256. return d->Collapsed;
  257. }
  258. //-----------------------------------------------------------------------------
  259. void ctkFittedTextBrowser::setShowDetailsText(const QString &text)
  260. {
  261. Q_D(ctkFittedTextBrowser);
  262. if (d->ShowDetailsText == text)
  263. {
  264. // no change
  265. return;
  266. }
  267. d->ShowDetailsText = text;
  268. d->updateCollapsedText();
  269. }
  270. //-----------------------------------------------------------------------------
  271. QString ctkFittedTextBrowser::showDetailsText() const
  272. {
  273. Q_D(const ctkFittedTextBrowser);
  274. return d->ShowDetailsText;
  275. }
  276. //-----------------------------------------------------------------------------
  277. void ctkFittedTextBrowser::setHideDetailsText(const QString &text)
  278. {
  279. Q_D(ctkFittedTextBrowser);
  280. if (d->HideDetailsText == text)
  281. {
  282. // no change
  283. return;
  284. }
  285. d->HideDetailsText = text;
  286. d->updateCollapsedText();
  287. }
  288. //-----------------------------------------------------------------------------
  289. QString ctkFittedTextBrowser::hideDetailsText() const
  290. {
  291. Q_D(const ctkFittedTextBrowser);
  292. return d->HideDetailsText;
  293. }