ctkCheckableModelHelper.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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: pqCheckableModelHelper.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 _ctkCheckableModelHelper_h
  40. #define _ctkCheckableModelHelper_h
  41. // Qt includes
  42. #include <QModelIndex>
  43. #include <QObject>
  44. class QAbstractItemModel;
  45. // CTK includes
  46. #include "ctkWidgetsExport.h"
  47. class ctkCheckableModelHelperPrivate;
  48. /// \ingroup Widgets
  49. ///
  50. /// ctkCheckableModelHelper expose functions to handle checkable models
  51. class CTK_WIDGETS_EXPORT ctkCheckableModelHelper : public QObject
  52. {
  53. Q_OBJECT;
  54. Q_PROPERTY(bool forceCheckability READ forceCheckability WRITE setForceCheckability);
  55. Q_PROPERTY(int propagateDepth READ propagateDepth WRITE setPropagateDepth);
  56. Q_PROPERTY(Qt::CheckState defaultCheckState READ defaultCheckState WRITE setDefaultCheckState);
  57. public:
  58. ctkCheckableModelHelper(Qt::Orientation orientation, QObject *parent=0);
  59. virtual ~ctkCheckableModelHelper();
  60. Qt::Orientation orientation()const;
  61. ///
  62. /// When setting the model, if PropagateToItems is true (by default), the check
  63. /// state of the checkable headers is updated from the check state of the items
  64. /// If you want to make sure of the check state of a header, after setting the
  65. /// (done by myView.setHeader(myCheckableModelHelper)), you can call
  66. /// myModel.setHeaderData(0, Qt::Horizontal, Qt::Checked, Qt::CheckStateRole)
  67. /// or myCheckableModelHelper->setCheckState(0, Qt::Checked)
  68. QAbstractItemModel *model()const;
  69. virtual void setModel(QAbstractItemModel *model);
  70. /// Reimplemented for internal reasons
  71. QModelIndex rootIndex()const;
  72. virtual void setRootIndex(const QModelIndex &index);
  73. ///
  74. /// A section is checkable if its CheckStateRole data is non null.
  75. /// One can access the same value through the model:
  76. /// model->headerData(orientation, section, Qt::CheckStateRole).isEmpty()
  77. bool isHeaderCheckable(int section)const;
  78. bool isCheckable(const QModelIndex& index)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 headerCheckState(int section)const;
  84. Qt::CheckState checkState(const QModelIndex&)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 headerCheckState(int section, Qt::CheckState& checkState )const;
  90. bool checkState(const QModelIndex&, Qt::CheckState& checkState )const;
  91. /// How deep in the model(tree) do you want the check state to be propagated
  92. /// A value of -1 correspond to the deepest level of the model.
  93. /// -1 by default
  94. void setPropagateDepth(int depth);
  95. int propagateDepth()const;
  96. /// When true, the new items are automatically set to checkable
  97. void setForceCheckability(bool force);
  98. bool forceCheckability()const;
  99. Qt::CheckState defaultCheckState()const;
  100. void setDefaultCheckState(Qt::CheckState);
  101. public Q_SLOTS:
  102. void setCheckState(const QModelIndex& modelIndex, Qt::CheckState checkState);
  103. ///
  104. /// Warning, setting the check state automatically set the
  105. /// header section checkable
  106. void setHeaderCheckState(int section, Qt::CheckState checkState);
  107. /// Utility function to toggle the checkstate of an index
  108. void toggleCheckState(const QModelIndex& modelIndex);
  109. void toggleHeaderCheckState(int section);
  110. private Q_SLOTS:
  111. void onHeaderDataChanged(Qt::Orientation orient, int first, int last);
  112. void onDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight);
  113. void updateHeadersFromItems();
  114. void onColumnsInserted(const QModelIndex& parent, int start, int end);
  115. void onRowsInserted(const QModelIndex& parent, int start, int end);
  116. protected:
  117. QScopedPointer<ctkCheckableModelHelperPrivate> d_ptr;
  118. private:
  119. Q_DECLARE_PRIVATE(ctkCheckableModelHelper);
  120. Q_DISABLE_COPY(ctkCheckableModelHelper);
  121. };
  122. #endif