ctkCheckableHeaderView.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.commontk.org/LICENSE
  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. /*=========================================================================
  15. Program: ParaView
  16. Module: $RCSfile: pqCheckableHeaderView.h,v $
  17. Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
  18. All rights reserved.
  19. ParaView is a free software; you can redistribute it and/or modify it
  20. under the terms of the ParaView license version 1.2.
  21. See License_v1.2.txt for the full ParaView license.
  22. A copy of this license can be obtained by contacting
  23. Kitware Inc.
  24. 28 Corporate Drive
  25. Clifton Park, NY 12065
  26. USA
  27. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. =========================================================================*/
  39. #ifndef _ctkCheckableHeaderView_h
  40. #define _ctkCheckableHeaderView_h
  41. // Qt includes
  42. #include <QHeaderView>
  43. // CTK includes
  44. #include <ctkPimpl.h>
  45. #include "ctkWidgetsExport.h"
  46. class ctkCheckableHeaderViewPrivate;
  47. ///
  48. /// ctkCheckableHeaderView is a QHeaderView that can display a checkbox
  49. /// for any header section.
  50. /// If propageteToItems, the check state of the header section is set to
  51. /// all items in the header row/column of the QAbstractItemModel if the
  52. /// items are checkable.
  53. /// ctkCheckableHeaderView also supports row/column sorting.
  54. /// TBD: It should probably be a QSortFilterProxyModel that adds a checkability
  55. /// data on top of the indexes.
  56. class CTK_WIDGETS_EXPORT ctkCheckableHeaderView : public QHeaderView
  57. {
  58. Q_OBJECT;
  59. Q_PROPERTY(bool forceCheckability READ forceCheckability WRITE setForceCheckability);
  60. Q_PROPERTY(int propagateDepth READ propagateDepth WRITE setPropagateDepth);
  61. public:
  62. ctkCheckableHeaderView(Qt::Orientation orient, QWidget *parent=0);
  63. virtual ~ctkCheckableHeaderView();
  64. ///
  65. /// When setting the model, if PropagateToItems is true (by default), the check
  66. /// state of the checkable headers is updated from the check state of the items
  67. /// If you want to make sure of the check state of a header, after setting the
  68. /// (done by myView.setHeader(myCheckableHeaderView)), you can call
  69. /// myModel.setHeaderData(0, Qt::Horizontal, Qt::Checked, Qt::CheckStateRole)
  70. /// or myCheckableHeaderView->setCheckState(0, Qt::Checked)
  71. virtual void setModel(QAbstractItemModel *model);
  72. /// Reimplemented for internal reasons
  73. virtual void setRootIndex(const QModelIndex &index);
  74. ///
  75. /// A section is checkable if its CheckStateRole data is non null.
  76. /// One can access the same value through the model:
  77. /// model->headerData(orientation, section, Qt::CheckStateRole).isEmpty()
  78. bool isCheckable(int section)const;
  79. ///
  80. /// Utility function that returns the checkState of the section.
  81. /// One can access the same value through the model:
  82. /// model->headerData(orientation, section, Qt::CheckStateRole)
  83. Qt::CheckState checkState(int section)const;
  84. ///
  85. /// Utility function that returns the checkState of the section.
  86. /// One can access the same value through the model:
  87. /// model->headerData(orientation, section, Qt::CheckStateRole)
  88. bool checkState(int section,Qt::CheckState& checkState )const;
  89. ///
  90. /// Used to listen for focus in/out events.
  91. /// \param object The object receiving the event.
  92. /// \param e Event specific data.
  93. /// \return
  94. /// True if the event should be filtered out.
  95. virtual bool eventFilter(QObject *object, QEvent *e);
  96. /// How deep in the model(tree) do you want the check state to be propagated
  97. /// A value of -1 correspond to the deepest level of the model.
  98. /// -1 by default
  99. void setPropagateDepth(int depth);
  100. int propagateDepth()const;
  101. void setForceCheckability(bool force);
  102. bool forceCheckability()const;
  103. public slots:
  104. ///
  105. /// if the check state is PartiallyChecked, the section becomes Checked
  106. void toggleCheckState(int section);
  107. ///
  108. /// Warning, setting the check state automatically set the
  109. /// header section checkable
  110. void setCheckState(int section, Qt::CheckState checkState);
  111. private slots:
  112. void onHeaderDataChanged(Qt::Orientation orient, int first, int last);
  113. void onHeaderSectionInserted(const QModelIndex &parent, int first, int last);
  114. inline void updateHeaderPixmaps();
  115. void onDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight);
  116. void updateHeadersFromItems();
  117. protected:
  118. virtual void updateHeaderPixmaps(int first, int last);
  119. virtual void initStyleSectionOption(QStyleOptionHeader *option, int section, QRect rect)const;
  120. virtual void mousePressEvent(QMouseEvent *e);
  121. virtual void mouseReleaseEvent(QMouseEvent *e);
  122. bool isPointInCheckBox(int section, QPoint pos)const;
  123. protected:
  124. QScopedPointer<ctkCheckableHeaderViewPrivate> d_ptr;
  125. private:
  126. Q_DECLARE_PRIVATE(ctkCheckableHeaderView);
  127. Q_DISABLE_COPY(ctkCheckableHeaderView);
  128. };
  129. //-----------------------------------------------------------------------------
  130. void ctkCheckableHeaderView::updateHeaderPixmaps()
  131. {
  132. this->updateHeaderPixmaps(0, this->count()-1);
  133. }
  134. #endif