ctkCheckableHeaderView.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 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
  52. /// ctkCheckableHeaderView also supports row/column sorting.
  53. class CTK_WIDGETS_EXPORT ctkCheckableHeaderView : public QHeaderView
  54. {
  55. Q_OBJECT;
  56. Q_PROPERTY(bool propagateToItems READ propagateToItems WRITE setPropagateToItems);
  57. public:
  58. ctkCheckableHeaderView(Qt::Orientation orient, QWidget *parent=0);
  59. virtual ~ctkCheckableHeaderView();
  60. virtual void setModel(QAbstractItemModel *model);
  61. virtual void setRootIndex(const QModelIndex &index);
  62. ///
  63. /// A section is checkable if its CheckStateRole data is non null.
  64. /// One can access the same value through the model:
  65. /// model->headerData(orientation, section, Qt::CheckStateRole).isEmpty()
  66. bool isCheckable(int section)const;
  67. ///
  68. /// Utility function that returns the checkState of the section.
  69. /// One can access the same value through the model:
  70. /// model->headerData(orientation, section, Qt::CheckStateRole)
  71. Qt::CheckState checkState(int section)const;
  72. ///
  73. /// Utility function that returns the checkState of the section.
  74. /// One can access the same value through the model:
  75. /// model->headerData(orientation, section, Qt::CheckStateRole)
  76. bool checkState(int section,Qt::CheckState& checkState )const;
  77. ///
  78. /// Used to listen for focus in/out events.
  79. /// \param object The object receiving the event.
  80. /// \param e Event specific data.
  81. /// \return
  82. /// True if the event should be filtered out.
  83. virtual bool eventFilter(QObject *object, QEvent *e);
  84. ///
  85. /// If true, the items check states in a row/column are synchronized
  86. /// with the check state of the corresponding header section.
  87. void setPropagateToItems(bool propagate);
  88. bool propagateToItems()const;
  89. public slots:
  90. ///
  91. /// if the check state is PartiallyChecked, the section becomes Checked
  92. void toggleCheckState(int section);
  93. ///
  94. /// Warning, setting the check state automatically set the
  95. /// header section checkable
  96. void setCheckState(int section, Qt::CheckState checkState);
  97. private slots:
  98. void updateHeaderData(Qt::Orientation orient, int first, int last);
  99. void insertHeaderSection(const QModelIndex &parent, int first, int last);
  100. inline void updateHeaders();
  101. void updateHeadersFromItems(const QModelIndex& topLeft, const QModelIndex& bottomRight);
  102. protected:
  103. virtual void updateHeaders(int first, int last);
  104. virtual void initStyleSectionOption(QStyleOptionHeader *option, int section, QRect rect)const;
  105. virtual void mousePressEvent(QMouseEvent *e);
  106. virtual void mouseReleaseEvent(QMouseEvent *e);
  107. bool isPointInCheckBox(int section, QPoint pos)const;
  108. private:
  109. CTK_DECLARE_PRIVATE(ctkCheckableHeaderView);
  110. };
  111. //-----------------------------------------------------------------------------
  112. void ctkCheckableHeaderView::updateHeaders()
  113. {
  114. this->updateHeaders(0, this->count()-1);
  115. }
  116. #endif