ctkCheckableHeaderView.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. /*=========================================================================
  15. Program: ParaView
  16. Module: pqCheckableHeaderView.h
  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 http://www.paraview.org/paraview/project/license.html 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 "ctkWidgetsExport.h"
  45. class ctkCheckableHeaderViewPrivate;
  46. class ctkCheckableModelHelper;
  47. /// \ingroup Widgets
  48. ///
  49. /// ctkCheckableHeaderView is a QHeaderView that can display a checkbox
  50. /// for any header section.
  51. /// If propageteToItems, the check state of the header section is set to
  52. /// all items in the header row/column of the QAbstractItemModel if the
  53. /// items are checkable.
  54. /// ctkCheckableHeaderView also supports row/column sorting.
  55. /// TBD: It should probably be a QSortFilterProxyModel that adds a checkability
  56. /// data on top of the indexes.
  57. class CTK_WIDGETS_EXPORT ctkCheckableHeaderView : public QHeaderView
  58. {
  59. Q_OBJECT;
  60. public:
  61. ctkCheckableHeaderView(Qt::Orientation orient, QWidget *parent=0);
  62. virtual ~ctkCheckableHeaderView();
  63. ///
  64. /// When setting the model, if PropagateToItems is true (by default), the check
  65. /// state of the checkable headers is updated from the check state of the items
  66. /// If you want to make sure of the check state of a header, after setting the
  67. /// (done by myView.setHeader(myCheckableHeaderView)), you can call
  68. /// myModel.setHeaderData(0, Qt::Horizontal, Qt::Checked, Qt::CheckStateRole)
  69. /// or myCheckableHeaderView->setCheckState(0, Qt::Checked)
  70. virtual void setModel(QAbstractItemModel *model);
  71. /// Reimplemented for internal reasons
  72. virtual void setRootIndex(const QModelIndex &index);
  73. ///
  74. /// Used to listen for focus in/out events.
  75. /// \param object The object receiving the event.
  76. /// \param e Event specific data.
  77. /// \return
  78. /// True if the event should be filtered out.
  79. virtual bool eventFilter(QObject *object, QEvent *e);
  80. ///
  81. /// Utility function that returns the checkState of the section.
  82. /// One can access the same value through the model:
  83. /// model->headerData(orientation, section, Qt::CheckStateRole)
  84. Qt::CheckState checkState(int section)const;
  85. ///
  86. /// Utility function that returns the checkState of the section.
  87. /// One can access the same value through the model:
  88. /// model->headerData(orientation, section, Qt::CheckStateRole)
  89. bool checkState(int section,Qt::CheckState& checkState )const;
  90. ctkCheckableModelHelper* checkableModelHelper()const;
  91. public Q_SLOTS:
  92. ///
  93. /// Warning, setting the check state automatically set the
  94. /// header section checkable
  95. void setCheckState(int section, Qt::CheckState checkState);
  96. private Q_SLOTS:
  97. void onHeaderDataChanged(Qt::Orientation orient, int first, int last);
  98. void onHeaderSectionInserted();
  99. inline void updateHeaderPixmaps();
  100. protected:
  101. virtual void updateHeaderPixmaps(int first, int last);
  102. virtual void initStyleSectionOption(QStyleOptionHeader *option, int section, QRect rect)const;
  103. virtual void mousePressEvent(QMouseEvent *e);
  104. virtual void mouseReleaseEvent(QMouseEvent *e);
  105. bool isPointInCheckBox(int section, QPoint pos)const;
  106. protected:
  107. QScopedPointer<ctkCheckableHeaderViewPrivate> d_ptr;
  108. private:
  109. Q_DECLARE_PRIVATE(ctkCheckableHeaderView);
  110. Q_DISABLE_COPY(ctkCheckableHeaderView);
  111. };
  112. //-----------------------------------------------------------------------------
  113. void ctkCheckableHeaderView::updateHeaderPixmaps()
  114. {
  115. this->updateHeaderPixmaps(0, this->count()-1);
  116. }
  117. #endif