ctkFittedTextBrowser.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. //-----------------------------------------------------------------------------
  22. ctkFittedTextBrowser::ctkFittedTextBrowser(QWidget* _parent)
  23. : QTextBrowser(_parent)
  24. {
  25. this->connect(this, SIGNAL(textChanged()), SLOT(heightForWidthMayHaveChanged()));
  26. QSizePolicy newSizePolicy = QSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
  27. this->setSizePolicy(newSizePolicy);
  28. }
  29. //-----------------------------------------------------------------------------
  30. ctkFittedTextBrowser::~ctkFittedTextBrowser()
  31. {
  32. }
  33. //-----------------------------------------------------------------------------
  34. void ctkFittedTextBrowser::heightForWidthMayHaveChanged()
  35. {
  36. this->updateGeometry();
  37. }
  38. //-----------------------------------------------------------------------------
  39. int ctkFittedTextBrowser::heightForWidth(int _width) const
  40. {
  41. QTextDocument* doc = this->document();
  42. qreal savedWidth = doc->textWidth();
  43. // Fudge factor. This is the difference between the frame and the
  44. // viewport.
  45. int fudge = 2 * this->frameWidth();
  46. int horizontalScrollbarHeight = 0;
  47. if (this->horizontalScrollBar()->isVisible())
  48. {
  49. horizontalScrollbarHeight = this->horizontalScrollBar()->height() + fudge;
  50. }
  51. // Do the calculation assuming no scrollbars
  52. doc->setTextWidth(_width - fudge);
  53. int noScrollbarHeight =
  54. doc->documentLayout()->documentSize().height() + fudge;
  55. // (If noScrollbarHeight is greater than the maximum height we'll be
  56. // allowed, then there will be scrollbars, and the actual required
  57. // height will be even higher. But since in this case we've already
  58. // hit the maximum height, it doesn't matter that we underestimate.)
  59. // Get minimum height (even if string is empty): one line of text
  60. int _minimumHeight = QFontMetrics(doc->defaultFont()).lineSpacing() + fudge;
  61. int ret = qMax(noScrollbarHeight, _minimumHeight) + horizontalScrollbarHeight;
  62. doc->setTextWidth(savedWidth);
  63. return ret;
  64. }
  65. //-----------------------------------------------------------------------------
  66. QSize ctkFittedTextBrowser::minimumSizeHint() const {
  67. QSize s(this->size().width(), 0);
  68. if (s.width() == 0)
  69. {
  70. //s.setWidth(400); // arbitrary value
  71. return QTextBrowser::minimumSizeHint();
  72. }
  73. s.setHeight(this->heightForWidth(s.width()));
  74. return s;
  75. }
  76. //-----------------------------------------------------------------------------
  77. QSize ctkFittedTextBrowser::sizeHint() const {
  78. return this->minimumSizeHint();
  79. }
  80. //-----------------------------------------------------------------------------
  81. void ctkFittedTextBrowser::resizeEvent(QResizeEvent* e)
  82. {
  83. this->QTextBrowser::resizeEvent(e);
  84. if (e->size().height() != this->heightForWidth(e->size().width()))
  85. {
  86. this->heightForWidthMayHaveChanged();
  87. }
  88. }