ctkFittedTextBrowser.h 3.9 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. #ifndef __ctkFittedTextBrowser_h
  15. #define __ctkFittedTextBrowser_h
  16. // Qt includes
  17. #include <QTextBrowser>
  18. // CTK includes
  19. #include "ctkWidgetsExport.h"
  20. class ctkFittedTextBrowserPrivate;
  21. /// \ingroup Widgets
  22. /// ctkFittedTextBrowser is a QTextBrowser that adapts its height depending
  23. /// on its contents and the width available. It always tries to show the whole
  24. /// contents. ctkFittedTextBrowser doesn't resize itself but acts on the
  25. /// sizeHint, minimumSizeHint and heightForWidth. Here sizeHint() and
  26. /// minimumSizeHint() are the same as ctkFittedTextBrowser always try to
  27. /// show the whole contents.
  28. ///
  29. /// The widget can further optimize use of available space by collapsing
  30. /// text. If the option is enabled then only a short teaser is shown
  31. /// and the user has to click on "More..." to see the full text.
  32. class CTK_WIDGETS_EXPORT ctkFittedTextBrowser : public QTextBrowser
  33. {
  34. Q_OBJECT
  35. Q_PROPERTY(bool collapsible READ collapsible WRITE setCollapsible)
  36. Q_PROPERTY(bool collapsed READ collapsed WRITE setCollapsed)
  37. Q_PROPERTY(QString showMoreText READ showMoreText WRITE setShowMoreText)
  38. Q_PROPERTY(QString showLessText READ showLessText WRITE setShowLessText)
  39. public:
  40. ctkFittedTextBrowser(QWidget* parent = 0);
  41. virtual ~ctkFittedTextBrowser();
  42. /// Show only first line with "More..." link to save space.
  43. /// When the user clicks on the link then the full text is displayed
  44. /// (and a "Less..." link).
  45. /// The teaser is the beginning of the text up to the first newline character
  46. /// (for plain text) or <br> tag (for html). The separator is removed when
  47. /// the text is expanded so that the full text can continue on the same line
  48. /// as the teaser.
  49. void setCollapsible(bool collapsible);
  50. /// Show only first line with "More..." link to save space.
  51. bool collapsible() const;
  52. /// Show only first line/the full text.
  53. /// Only has effect if collapsible = true.
  54. void setCollapsed(bool collapsed);
  55. /// Show only first line/the full text.
  56. bool collapsed() const;
  57. void setPlainText(const QString &text);
  58. #ifndef QT_NO_TEXTHTMLPARSER
  59. void setHtml(const QString &text);
  60. #endif
  61. void setText(const QString &text);
  62. /// Text that is displayed at the end of collapsed text.
  63. /// Clicking on the text expands the widget.
  64. void setShowMoreText(const QString &text);
  65. /// Text that is displayed at the end of collapsed text.
  66. QString showMoreText()const;
  67. /// Text that is displayed at the end of non-collapsed text.
  68. /// Clicking on the text collapses the widget.
  69. void setShowLessText(const QString &text);
  70. /// Text that is displayed at the end of non-collapsed text.
  71. QString showLessText()const;
  72. /// Reimplemented for internal reasons
  73. virtual QSize sizeHint() const;
  74. /// Reimplemented for internal reasons
  75. virtual QSize minimumSizeHint() const;
  76. /// Reimplemented for internal reasons
  77. virtual int heightForWidth(int width) const;
  78. protected Q_SLOTS:
  79. void heightForWidthMayHaveChanged();
  80. void anchorClicked(const QUrl &url);
  81. protected:
  82. QScopedPointer<ctkFittedTextBrowserPrivate> d_ptr;
  83. virtual void resizeEvent(QResizeEvent* e);
  84. private:
  85. Q_DECLARE_PRIVATE(ctkFittedTextBrowser);
  86. Q_DISABLE_COPY(ctkFittedTextBrowser);
  87. };
  88. #endif