ctkFittedTextBrowser.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. Q_Q(const ctkFittedTextBrowser);
  56. int teaserEndPosition = this->CollapsibleText.indexOf("\n");
  57. if (teaserEndPosition < 0)
  58. {
  59. return this->CollapsibleText;
  60. }
  61. QString finalText;
  62. finalText.append("<html>");
  63. finalText.append(this->Collapsed ? this->CollapsibleText.left(teaserEndPosition) : this->CollapsibleText);
  64. finalText.append(this->collapseLinkText());
  65. finalText.append("</html>");
  66. // Remove line break to allow continuation of line.
  67. finalText.replace(finalText.indexOf('\n'), 1, "");
  68. // In plain text line breaks were indicated by newline, but we now use html,
  69. // so line breaks must use <br>
  70. finalText.replace("\n", "<br>");
  71. return finalText;
  72. }
  73. //-----------------------------------------------------------------------------
  74. QString ctkFittedTextBrowserPrivate::collapsedTextFromHtml() const
  75. {
  76. Q_Q(const ctkFittedTextBrowser);
  77. const QString lineBreak("<br>");
  78. int teaserEndPosition = this->CollapsibleText.indexOf(lineBreak);
  79. if (teaserEndPosition < 0)
  80. {
  81. return this->CollapsibleText;
  82. }
  83. QString finalText = this->CollapsibleText;
  84. if (this->Collapsed)
  85. {
  86. finalText = finalText.left(teaserEndPosition) + this->collapseLinkText();
  87. // By truncating the full text we might have deleted the closing </html> tag
  88. // restore it now.
  89. if (finalText.contains("<html") && !finalText.contains("</html"))
  90. {
  91. finalText.append("</html>");
  92. }
  93. }
  94. else
  95. {
  96. // Remove <br> to allow continuation of line and avoid extra space
  97. // when <p> element is used as well.
  98. finalText.replace(finalText.indexOf(lineBreak), lineBreak.size(), "");
  99. // Add link text before closing </body> or </html> tag
  100. if (finalText.contains("</body>"))
  101. {
  102. finalText.replace("</body>", this->collapseLinkText() + "</body>");
  103. }
  104. else if (finalText.contains("</html>"))
  105. {
  106. finalText.replace("</html>", this->collapseLinkText() + "</html>");
  107. }
  108. else
  109. {
  110. finalText.append(this->collapseLinkText());
  111. }
  112. }
  113. return finalText;
  114. }
  115. //-----------------------------------------------------------------------------
  116. QString ctkFittedTextBrowserPrivate::collapseLinkText() const
  117. {
  118. Q_Q(const ctkFittedTextBrowser);
  119. if (this->Collapsed)
  120. {
  121. return QString("&hellip; <a href=\"#") + moreAnchor + "\">" + this->ShowDetailsText + "</a>";
  122. }
  123. else
  124. {
  125. return QString(" <a href=\"#") + lessAnchor + "\">" + this->HideDetailsText + "</a>";
  126. }
  127. }
  128. //-----------------------------------------------------------------------------
  129. ctkFittedTextBrowser::ctkFittedTextBrowser(QWidget* _parent)
  130. : QTextBrowser(_parent)
  131. , d_ptr(new ctkFittedTextBrowserPrivate(*this))
  132. {
  133. this->connect(this, SIGNAL(textChanged()), SLOT(heightForWidthMayHaveChanged()));
  134. QSizePolicy newSizePolicy = QSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
  135. this->setSizePolicy(newSizePolicy);
  136. this->connect(this, SIGNAL(anchorClicked(QUrl)), SLOT(anchorClicked(QUrl)));
  137. }
  138. //-----------------------------------------------------------------------------
  139. ctkFittedTextBrowser::~ctkFittedTextBrowser()
  140. {
  141. }
  142. //-----------------------------------------------------------------------------
  143. void ctkFittedTextBrowser::heightForWidthMayHaveChanged()
  144. {
  145. this->updateGeometry();
  146. }
  147. //-----------------------------------------------------------------------------
  148. int ctkFittedTextBrowser::heightForWidth(int _width) const
  149. {
  150. QTextDocument* doc = this->document();
  151. qreal savedWidth = doc->textWidth();
  152. // Fudge factor. This is the difference between the frame and the
  153. // viewport.
  154. int fudge = 2 * this->frameWidth();
  155. int horizontalScrollbarHeight = 0;
  156. if (this->horizontalScrollBar()->isVisible())
  157. {
  158. horizontalScrollbarHeight = this->horizontalScrollBar()->height() + fudge;
  159. }
  160. // Do the calculation assuming no scrollbars
  161. doc->setTextWidth(_width - fudge);
  162. int noScrollbarHeight =
  163. doc->documentLayout()->documentSize().height() + fudge;
  164. // (If noScrollbarHeight is greater than the maximum height we'll be
  165. // allowed, then there will be scrollbars, and the actual required
  166. // height will be even higher. But since in this case we've already
  167. // hit the maximum height, it doesn't matter that we underestimate.)
  168. // Get minimum height (even if string is empty): one line of text
  169. int _minimumHeight = QFontMetrics(doc->defaultFont()).lineSpacing() + fudge;
  170. int ret = qMax(noScrollbarHeight, _minimumHeight) + horizontalScrollbarHeight;
  171. doc->setTextWidth(savedWidth);
  172. return ret;
  173. }
  174. //-----------------------------------------------------------------------------
  175. QSize ctkFittedTextBrowser::minimumSizeHint() const {
  176. QSize s(this->size().width(), 0);
  177. if (s.width() == 0)
  178. {
  179. //s.setWidth(400); // arbitrary value
  180. return QTextBrowser::minimumSizeHint();
  181. }
  182. s.setHeight(this->heightForWidth(s.width()));
  183. return s;
  184. }
  185. //-----------------------------------------------------------------------------
  186. QSize ctkFittedTextBrowser::sizeHint() const {
  187. return this->minimumSizeHint();
  188. }
  189. //-----------------------------------------------------------------------------
  190. void ctkFittedTextBrowser::resizeEvent(QResizeEvent* e)
  191. {
  192. this->QTextBrowser::resizeEvent(e);
  193. if (e->size().height() != this->heightForWidth(e->size().width()))
  194. {
  195. this->heightForWidthMayHaveChanged();
  196. }
  197. }
  198. //-----------------------------------------------------------------------------
  199. void ctkFittedTextBrowser::setCollapsibleText(const QString &text)
  200. {
  201. Q_D(ctkFittedTextBrowser);
  202. d->CollapsibleTextSetter = ctkFittedTextBrowserPrivate::Text;
  203. d->CollapsibleText = text;
  204. d->updateCollapsedText();
  205. }
  206. //-----------------------------------------------------------------------------
  207. QString ctkFittedTextBrowser::collapsibleText() const
  208. {
  209. Q_D(const ctkFittedTextBrowser);
  210. return d->CollapsibleText;
  211. }
  212. //-----------------------------------------------------------------------------
  213. void ctkFittedTextBrowser::setCollapsiblePlainText(const QString &text)
  214. {
  215. Q_D(ctkFittedTextBrowser);
  216. d->CollapsibleTextSetter = ctkFittedTextBrowserPrivate::PlainText;
  217. d->CollapsibleText = text;
  218. d->updateCollapsedText();
  219. }
  220. //-----------------------------------------------------------------------------
  221. void ctkFittedTextBrowser::setCollapsibleHtml(const QString &text)
  222. {
  223. Q_D(ctkFittedTextBrowser);
  224. d->CollapsibleTextSetter = ctkFittedTextBrowserPrivate::Html;
  225. d->CollapsibleText = text;
  226. d->updateCollapsedText();
  227. }
  228. //-----------------------------------------------------------------------------
  229. void ctkFittedTextBrowser::anchorClicked(const QUrl &url)
  230. {
  231. Q_D(ctkFittedTextBrowser);
  232. if (url.path().isEmpty())
  233. {
  234. if (url.fragment() == moreAnchor)
  235. {
  236. this->setCollapsed(false);
  237. }
  238. else if (url.fragment() == lessAnchor)
  239. {
  240. this->setCollapsed(true);
  241. }
  242. }
  243. }
  244. //-----------------------------------------------------------------------------
  245. void ctkFittedTextBrowser::setCollapsed(bool collapsed)
  246. {
  247. Q_D(ctkFittedTextBrowser);
  248. if (d->Collapsed == collapsed)
  249. {
  250. // no change
  251. return;
  252. }
  253. d->Collapsed = collapsed;
  254. d->updateCollapsedText();
  255. }
  256. //-----------------------------------------------------------------------------
  257. bool ctkFittedTextBrowser::collapsed() const
  258. {
  259. Q_D(const ctkFittedTextBrowser);
  260. return d->Collapsed;
  261. }
  262. //-----------------------------------------------------------------------------
  263. void ctkFittedTextBrowser::setShowDetailsText(const QString &text)
  264. {
  265. Q_D(ctkFittedTextBrowser);
  266. if (d->ShowDetailsText == text)
  267. {
  268. // no change
  269. return;
  270. }
  271. d->ShowDetailsText = text;
  272. d->updateCollapsedText();
  273. }
  274. //-----------------------------------------------------------------------------
  275. QString ctkFittedTextBrowser::showDetailsText() const
  276. {
  277. Q_D(const ctkFittedTextBrowser);
  278. return d->ShowDetailsText;
  279. }
  280. //-----------------------------------------------------------------------------
  281. void ctkFittedTextBrowser::setHideDetailsText(const QString &text)
  282. {
  283. Q_D(ctkFittedTextBrowser);
  284. if (d->HideDetailsText == text)
  285. {
  286. // no change
  287. return;
  288. }
  289. d->HideDetailsText = text;
  290. d->updateCollapsedText();
  291. }
  292. //-----------------------------------------------------------------------------
  293. QString ctkFittedTextBrowser::hideDetailsText() const
  294. {
  295. Q_D(const ctkFittedTextBrowser);
  296. return d->HideDetailsText;
  297. }