ctkFittedTextBrowser.cpp 3.4 KB

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