ctkFittedTextBrowser.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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[] = "more";
  23. static const char lessAnchor[] = "less";
  24. //-----------------------------------------------------------------------------
  25. ctkFittedTextBrowserPrivate::ctkFittedTextBrowserPrivate(ctkFittedTextBrowser& object)
  26. :q_ptr(&object)
  27. {
  28. this->Collapsible = false;
  29. this->Collapsed = true;
  30. this->FullTextSetter = ctkFittedTextBrowserPrivate::Text;
  31. this->ShowMoreText = object.tr("More...");
  32. this->ShowLessText = object.tr("Hide details.");
  33. QString ShowLessText;
  34. }
  35. //-----------------------------------------------------------------------------
  36. ctkFittedTextBrowserPrivate::~ctkFittedTextBrowserPrivate()
  37. {
  38. }
  39. //-----------------------------------------------------------------------------
  40. QString ctkFittedTextBrowserPrivate::collapsibleText()
  41. {
  42. Q_Q(ctkFittedTextBrowser);
  43. bool html = (this->FullTextSetter == ctkFittedTextBrowserPrivate::Html || this->FullText.indexOf("<html>") >= 0);
  44. if (html)
  45. {
  46. return this->collapsibleHtml();
  47. }
  48. else
  49. {
  50. return this->collapsiblePlainText();
  51. }
  52. }
  53. //-----------------------------------------------------------------------------
  54. QString ctkFittedTextBrowserPrivate::collapseLinkText()
  55. {
  56. Q_Q(ctkFittedTextBrowser);
  57. if (this->Collapsed)
  58. {
  59. return QString(" <a href=\"#") + moreAnchor + "\">" + this->ShowMoreText + "</a>";
  60. }
  61. else
  62. {
  63. return QString(" <a href=\"#") + lessAnchor + "\">" + this->ShowLessText + "</a>";
  64. }
  65. }
  66. //-----------------------------------------------------------------------------
  67. QString ctkFittedTextBrowserPrivate::collapsiblePlainText()
  68. {
  69. Q_Q(ctkFittedTextBrowser);
  70. int teaserEndPosition = this->FullText.indexOf("\n");
  71. if (teaserEndPosition < 0)
  72. {
  73. return this->FullText;
  74. }
  75. QString finalText;
  76. finalText.append("<html>");
  77. finalText.append(this->Collapsed ? this->FullText.left(teaserEndPosition) : this->FullText);
  78. finalText.append(this->collapseLinkText());
  79. finalText.append("</html>");
  80. // Remove line break to allow continuation of line.
  81. finalText.replace(finalText.indexOf('\n'), 1, ' ');
  82. // In plain text line breaks were indicated by newline, but we now use html,
  83. // so line breaks must use <br>
  84. finalText.replace("\n", "<br>");
  85. return finalText;
  86. }
  87. //-----------------------------------------------------------------------------
  88. QString ctkFittedTextBrowserPrivate::collapsibleHtml()
  89. {
  90. Q_Q(ctkFittedTextBrowser);
  91. const QString lineBreak("<br>");
  92. int teaserEndPosition = this->FullText.indexOf(lineBreak);
  93. if (teaserEndPosition < 0)
  94. {
  95. return this->FullText;
  96. }
  97. QString finalText = this->FullText;
  98. if (this->Collapsed)
  99. {
  100. finalText = finalText.left(teaserEndPosition) + this->collapseLinkText();
  101. // By truncating the full text we might have deleted the closing </html> tag
  102. // restore it now.
  103. if (finalText.contains("<html") && !finalText.contains("</html"))
  104. {
  105. finalText.append("</html>");
  106. }
  107. }
  108. else
  109. {
  110. // Remove <br> to allow continuation of line and avoid extra space
  111. // when <p> element is used as well.
  112. finalText.replace(finalText.indexOf(lineBreak), lineBreak.size(), " ");
  113. // Add link text before closing </body> or </html> tag
  114. if (finalText.contains("</body>"))
  115. {
  116. finalText.replace("</body>", this->collapseLinkText() + "</body>");
  117. }
  118. else if (finalText.contains("</html>"))
  119. {
  120. finalText.replace("</html>", this->collapseLinkText() + "</html>");
  121. }
  122. else
  123. {
  124. finalText.append(this->collapseLinkText());
  125. }
  126. }
  127. return finalText;
  128. }
  129. //-----------------------------------------------------------------------------
  130. ctkFittedTextBrowser::ctkFittedTextBrowser(QWidget* _parent)
  131. : QTextBrowser(_parent)
  132. , d_ptr(new ctkFittedTextBrowserPrivate(*this))
  133. {
  134. this->connect(this, SIGNAL(textChanged()), SLOT(heightForWidthMayHaveChanged()));
  135. QSizePolicy newSizePolicy = QSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
  136. this->setSizePolicy(newSizePolicy);
  137. this->connect(this, SIGNAL(anchorClicked(QUrl)), SLOT(anchorClicked(QUrl)));
  138. }
  139. //-----------------------------------------------------------------------------
  140. ctkFittedTextBrowser::~ctkFittedTextBrowser()
  141. {
  142. }
  143. //-----------------------------------------------------------------------------
  144. void ctkFittedTextBrowser::heightForWidthMayHaveChanged()
  145. {
  146. this->updateGeometry();
  147. }
  148. //-----------------------------------------------------------------------------
  149. int ctkFittedTextBrowser::heightForWidth(int _width) const
  150. {
  151. QTextDocument* doc = this->document();
  152. qreal savedWidth = doc->textWidth();
  153. // Fudge factor. This is the difference between the frame and the
  154. // viewport.
  155. int fudge = 2 * this->frameWidth();
  156. int horizontalScrollbarHeight = 0;
  157. if (this->horizontalScrollBar()->isVisible())
  158. {
  159. horizontalScrollbarHeight = this->horizontalScrollBar()->height() + fudge;
  160. }
  161. // Do the calculation assuming no scrollbars
  162. doc->setTextWidth(_width - fudge);
  163. int noScrollbarHeight =
  164. doc->documentLayout()->documentSize().height() + fudge;
  165. // (If noScrollbarHeight is greater than the maximum height we'll be
  166. // allowed, then there will be scrollbars, and the actual required
  167. // height will be even higher. But since in this case we've already
  168. // hit the maximum height, it doesn't matter that we underestimate.)
  169. // Get minimum height (even if string is empty): one line of text
  170. int _minimumHeight = QFontMetrics(doc->defaultFont()).lineSpacing() + fudge;
  171. int ret = qMax(noScrollbarHeight, _minimumHeight) + horizontalScrollbarHeight;
  172. doc->setTextWidth(savedWidth);
  173. return ret;
  174. }
  175. //-----------------------------------------------------------------------------
  176. QSize ctkFittedTextBrowser::minimumSizeHint() const {
  177. QSize s(this->size().width(), 0);
  178. if (s.width() == 0)
  179. {
  180. //s.setWidth(400); // arbitrary value
  181. return QTextBrowser::minimumSizeHint();
  182. }
  183. s.setHeight(this->heightForWidth(s.width()));
  184. return s;
  185. }
  186. //-----------------------------------------------------------------------------
  187. QSize ctkFittedTextBrowser::sizeHint() const {
  188. return this->minimumSizeHint();
  189. }
  190. //-----------------------------------------------------------------------------
  191. void ctkFittedTextBrowser::resizeEvent(QResizeEvent* e)
  192. {
  193. this->QTextBrowser::resizeEvent(e);
  194. if (e->size().height() != this->heightForWidth(e->size().width()))
  195. {
  196. this->heightForWidthMayHaveChanged();
  197. }
  198. }
  199. //-----------------------------------------------------------------------------
  200. void ctkFittedTextBrowser::setText(const QString &text)
  201. {
  202. Q_D(ctkFittedTextBrowser);
  203. d->FullTextSetter = ctkFittedTextBrowserPrivate::Text;
  204. if (d->Collapsible)
  205. {
  206. d->FullText = text;
  207. QTextBrowser::setHtml(d->collapsibleText());
  208. }
  209. else
  210. {
  211. QTextBrowser::setText(text);
  212. }
  213. }
  214. //-----------------------------------------------------------------------------
  215. void ctkFittedTextBrowser::setPlainText(const QString &text)
  216. {
  217. Q_D(ctkFittedTextBrowser);
  218. d->FullTextSetter = ctkFittedTextBrowserPrivate::PlainText;
  219. if (d->Collapsible)
  220. {
  221. d->FullText = text;
  222. QTextBrowser::setHtml(d->collapsibleText());
  223. }
  224. else
  225. {
  226. QTextBrowser::setPlainText(text);
  227. }
  228. }
  229. //-----------------------------------------------------------------------------
  230. void ctkFittedTextBrowser::setHtml(const QString &text)
  231. {
  232. Q_D(ctkFittedTextBrowser);
  233. d->FullTextSetter = ctkFittedTextBrowserPrivate::Html;
  234. // always save the original text as well because use may make the widget
  235. // collapsible at any time
  236. d->FullText = text;
  237. if (d->Collapsible)
  238. {
  239. QTextBrowser::setHtml(d->collapsibleText());
  240. }
  241. else
  242. {
  243. QTextBrowser::setHtml(text);
  244. }
  245. }
  246. //-----------------------------------------------------------------------------
  247. void ctkFittedTextBrowser::anchorClicked(const QUrl &url)
  248. {
  249. Q_D(ctkFittedTextBrowser);
  250. if (url.path().isEmpty())
  251. {
  252. if (url.fragment() == moreAnchor)
  253. {
  254. this->setCollapsed(false);
  255. }
  256. else if (url.fragment() == lessAnchor)
  257. {
  258. this->setCollapsed(true);
  259. }
  260. }
  261. }
  262. //-----------------------------------------------------------------------------
  263. void ctkFittedTextBrowser::setCollapsed(bool collapsed)
  264. {
  265. Q_D(ctkFittedTextBrowser);
  266. if (d->Collapsed == collapsed)
  267. {
  268. // no change
  269. return;
  270. }
  271. d->Collapsed = collapsed;
  272. if (d->Collapsible)
  273. {
  274. QTextBrowser::setHtml(d->collapsibleText());
  275. }
  276. }
  277. //-----------------------------------------------------------------------------
  278. bool ctkFittedTextBrowser::collapsed() const
  279. {
  280. Q_D(const ctkFittedTextBrowser);
  281. return d->Collapsed;
  282. }
  283. //-----------------------------------------------------------------------------
  284. void ctkFittedTextBrowser::setCollapsible(bool collapsible)
  285. {
  286. Q_D(ctkFittedTextBrowser);
  287. if (d->Collapsible == collapsible)
  288. {
  289. // no change
  290. return;
  291. }
  292. d->Collapsible = collapsible;
  293. if (collapsible)
  294. {
  295. QTextBrowser::setHtml(d->collapsibleText());
  296. }
  297. else
  298. {
  299. switch (d->FullTextSetter)
  300. {
  301. case ctkFittedTextBrowserPrivate::Text: QTextBrowser::setText(d->FullText); break;
  302. case ctkFittedTextBrowserPrivate::PlainText: QTextBrowser::setPlainText(d->FullText); break;
  303. case ctkFittedTextBrowserPrivate::Html: QTextBrowser::setHtml(d->FullText); break;
  304. default: QTextBrowser::setText(d->FullText); break;
  305. }
  306. }
  307. }
  308. //-----------------------------------------------------------------------------
  309. bool ctkFittedTextBrowser::collapsible() const
  310. {
  311. Q_D(const ctkFittedTextBrowser);
  312. return d->Collapsible;
  313. }
  314. //-----------------------------------------------------------------------------
  315. void ctkFittedTextBrowser::setShowMoreText(const QString &text)
  316. {
  317. Q_D(ctkFittedTextBrowser);
  318. if (d->ShowMoreText == text)
  319. {
  320. // no change
  321. return;
  322. }
  323. d->ShowMoreText = text;
  324. if (d->Collapsible)
  325. {
  326. QTextBrowser::setHtml(d->collapsibleText());
  327. }
  328. }
  329. //-----------------------------------------------------------------------------
  330. QString ctkFittedTextBrowser::showMoreText() const
  331. {
  332. Q_D(const ctkFittedTextBrowser);
  333. return d->ShowMoreText;
  334. }
  335. //-----------------------------------------------------------------------------
  336. void ctkFittedTextBrowser::setShowLessText(const QString &text)
  337. {
  338. Q_D(ctkFittedTextBrowser);
  339. if (d->ShowLessText == text)
  340. {
  341. // no change
  342. return;
  343. }
  344. d->ShowLessText = text;
  345. if (d->Collapsible)
  346. {
  347. QTextBrowser::setHtml(d->collapsibleText());
  348. }
  349. }
  350. //-----------------------------------------------------------------------------
  351. QString ctkFittedTextBrowser::showLessText() const
  352. {
  353. Q_D(const ctkFittedTextBrowser);
  354. return d->ShowLessText;
  355. }