ctkCheckableHeaderView.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "ctkWidgetsExport.h"
  45. class ctkCheckableHeaderViewPrivate;
  46. class ctkCheckableModelHelper;
  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. public:
  60. ctkCheckableHeaderView(Qt::Orientation orient, QWidget *parent=0);
  61. virtual ~ctkCheckableHeaderView();
  62. ///
  63. /// When setting the model, if PropagateToItems is true (by default), the check
  64. /// state of the checkable headers is updated from the check state of the items
  65. /// If you want to make sure of the check state of a header, after setting the
  66. /// (done by myView.setHeader(myCheckableHeaderView)), you can call
  67. /// myModel.setHeaderData(0, Qt::Horizontal, Qt::Checked, Qt::CheckStateRole)
  68. /// or myCheckableHeaderView->setCheckState(0, Qt::Checked)
  69. virtual void setModel(QAbstractItemModel *model);
  70. /// Reimplemented for internal reasons
  71. virtual void setRootIndex(const QModelIndex &index);
  72. ///
  73. /// Used to listen for focus in/out events.
  74. /// \param object The object receiving the event.
  75. /// \param e Event specific data.
  76. /// \return
  77. /// True if the event should be filtered out.
  78. virtual bool eventFilter(QObject *object, QEvent *e);
  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. ctkCheckableModelHelper* checkableModelHelper()const;
  90. public slots:
  91. ///
  92. /// Warning, setting the check state automatically set the
  93. /// header section checkable
  94. void setCheckState(int section, Qt::CheckState checkState);
  95. private slots:
  96. void onHeaderDataChanged(Qt::Orientation orient, int first, int last);
  97. void onHeaderSectionInserted();
  98. inline void updateHeaderPixmaps();
  99. protected:
  100. virtual void updateHeaderPixmaps(int first, int last);
  101. virtual void initStyleSectionOption(QStyleOptionHeader *option, int section, QRect rect)const;
  102. virtual void mousePressEvent(QMouseEvent *e);
  103. virtual void mouseReleaseEvent(QMouseEvent *e);
  104. bool isPointInCheckBox(int section, QPoint pos)const;
  105. protected:
  106. QScopedPointer<ctkCheckableHeaderViewPrivate> d_ptr;
  107. private:
  108. Q_DECLARE_PRIVATE(ctkCheckableHeaderView);
  109. Q_DISABLE_COPY(ctkCheckableHeaderView);
  110. };
  111. //-----------------------------------------------------------------------------
  112. void ctkCheckableHeaderView::updateHeaderPixmaps()
  113. {
  114. this->updateHeaderPixmaps(0, this->count()-1);
  115. }
  116. #endif