ctkFittedTextBrowser.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 collapsed READ collapsed WRITE setCollapsed)
  36. Q_PROPERTY(QString showDetailsText READ showDetailsText WRITE setShowDetailsText)
  37. Q_PROPERTY(QString hideDetailsText READ hideDetailsText WRITE setHideDetailsText)
  38. public:
  39. ctkFittedTextBrowser(QWidget* parent = 0);
  40. virtual ~ctkFittedTextBrowser();
  41. /// Show only first line/the full text.
  42. /// Only has effect if collapsible = true.
  43. void setCollapsed(bool collapsed);
  44. /// Show only first line/the full text.
  45. bool collapsed() const;
  46. /// Text that is displayed at the end of collapsed text.
  47. /// Clicking on the text expands the widget.
  48. void setShowDetailsText(const QString &text);
  49. /// Text that is displayed at the end of collapsed text.
  50. QString showDetailsText()const;
  51. /// Text that is displayed at the end of non-collapsed text.
  52. /// Clicking on the text collapses the widget.
  53. void setHideDetailsText(const QString &text);
  54. /// Text that is displayed at the end of non-collapsed text.
  55. QString hideDetailsText()const;
  56. /// Return text set by setCollapsibleText.
  57. Q_INVOKABLE QString collapsibleText() const;
  58. /// Reimplemented for internal reasons
  59. virtual QSize sizeHint() const;
  60. /// Reimplemented for internal reasons
  61. virtual QSize minimumSizeHint() const;
  62. /// Reimplemented for internal reasons
  63. virtual int heightForWidth(int width) const;
  64. public Q_SLOTS:
  65. /// Set text that can be displayed in a shortened form (collapsed) for saving space,
  66. /// by only showing first line with "More..." link appended.
  67. /// When the user clicks on the link then the full text is displayed
  68. /// (and a "Less..." link).
  69. /// The teaser is the beginning of the text up to the first newline character
  70. /// (for plain text) or <br> tag (for html). The separator is removed when
  71. /// the text is expanded so that the full text can continue on the same line
  72. /// as the teaser.
  73. ///
  74. /// The text can be plain text or HTML and the the right format will be guessed.
  75. /// Use setCollapsedHtml() or setCollapsedPlainText() directly to avoid guessing.
  76. void setCollapsibleText(const QString &text);
  77. #ifndef QT_NO_TEXTHTMLPARSER
  78. /// Set text that can be displayed in a shortened form (collapsed) for saving space.
  79. /// \sa setCollapsibleText
  80. void setCollapsibleHtml(const QString &text);
  81. #endif
  82. /// Set text that can be displayed in a shortened form (collapsed) for saving space.
  83. /// \sa setCollapsibleText
  84. void setCollapsiblePlainText(const QString &text);
  85. protected Q_SLOTS:
  86. void heightForWidthMayHaveChanged();
  87. void anchorClicked(const QUrl &url);
  88. protected:
  89. QScopedPointer<ctkFittedTextBrowserPrivate> d_ptr;
  90. virtual void resizeEvent(QResizeEvent* e);
  91. private:
  92. Q_DECLARE_PRIVATE(ctkFittedTextBrowser);
  93. Q_DISABLE_COPY(ctkFittedTextBrowser);
  94. };
  95. #endif