ctkAddRemoveComboBox.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. All rights reserved.
  5. Distributed under a BSD License. See LICENSE.txt file.
  6. This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the above copyright notice for more information.
  9. =========================================================================*/
  10. #ifndef __ctkAddRemoveComboBox_h
  11. #define __ctkAddRemoveComboBox_h
  12. // Qt includes
  13. #include <QWidget>
  14. #include <QVariant>
  15. #include <QModelIndex>
  16. // CTK includes
  17. #include <ctkPimpl.h>
  18. #include "CTKWidgetsExport.h"
  19. class QComboBox;
  20. class ctkAddRemoveComboBoxPrivate;
  21. class CTK_WIDGETS_EXPORT ctkAddRemoveComboBox : public QWidget
  22. {
  23. Q_OBJECT
  24. Q_PROPERTY(QString emptyText READ emptyText WRITE setEmptyText)
  25. Q_PROPERTY(bool addEnabled READ addEnabled WRITE setAddEnabled)
  26. Q_PROPERTY(bool removeEnabled READ removeEnabled WRITE setRemoveEnabled)
  27. Q_PROPERTY(bool editEnabled READ editEnabled WRITE setEditEnabled)
  28. public:
  29. /// Superclass typedef
  30. typedef QWidget Superclass;
  31. /// Constructors
  32. explicit ctkAddRemoveComboBox(QWidget* parent = 0);
  33. virtual ~ctkAddRemoveComboBox(){}
  34. virtual void printAdditionalInfo();
  35. ///
  36. /// Set text that should be displayed in the comboBox when it is empty
  37. void setEmptyText(const QString& text);
  38. QString emptyText()const;
  39. ///
  40. /// Enable/Disable the add button.
  41. void setComboBoxEnabled(bool enable);
  42. bool comboBoxEnabled()const;
  43. ///
  44. /// Enable/Disable the add button.
  45. void setAddEnabled(bool enable);
  46. bool addEnabled()const;
  47. ///
  48. /// Enable/Disable the add button.
  49. void setRemoveEnabled(bool enable);
  50. bool removeEnabled()const;
  51. ///
  52. /// Enable/Disable the edit button.
  53. void setEditEnabled(bool enable);
  54. bool editEnabled()const;
  55. inline void addItem(const QString &text, const QVariant &userDataVariable = QVariant() )
  56. {this->insertItem(this->count(), text, userDataVariable);}
  57. inline void addItem(const QIcon &icon, const QString &text, const QVariant &userDataVariable = QVariant() )
  58. {this->insertItem(this->count(), icon, text, userDataVariable);}
  59. inline void addItems(const QStringList &texts )
  60. {this->insertItems(this->count(), texts);}
  61. void insertItem(int index, const QString &text, const QVariant &userDataVariable = QVariant() );
  62. void insertItem(int index, const QIcon &icon, const QString &text, const QVariant &userDataVariable = QVariant() );
  63. void insertItems(int index, const QStringList &texts);
  64. ///
  65. /// Return the number of item
  66. int count()const;
  67. bool empty()const;
  68. ///
  69. /// Returns the index of the item containing the given text; otherwise returns -1.
  70. /// The flags specify how the items in the combobox are searched.
  71. int findText(const QString& text, Qt::MatchFlags flags = Qt::MatchExactly | Qt::MatchCaseSensitive ) const;
  72. int findData(const QVariant & data, int role = Qt::UserRole, Qt::MatchFlags flags = Qt::MatchExactly | Qt::MatchCaseSensitive ) const;
  73. ///
  74. QString itemText(int index) const;
  75. QVariant itemData(int index, int role = Qt::UserRole) const;
  76. void setItemText(int index, const QString& text);
  77. void setItemData(int index, const QVariant& data, int role = Qt::UserRole);
  78. ///
  79. /// Return the current item
  80. int currentIndex() const;
  81. inline QString currentText() const
  82. {return this->itemText(this->currentIndex());}
  83. inline QVariant currentData(int role = Qt::UserRole) const
  84. {return this->itemData(this->currentIndex(), role);}
  85. ///
  86. /// Remove the item currently selected. See signal 'itemRemoved'
  87. void removeItem(int index);
  88. inline void removeCurrentItem()
  89. {this->removeItem(this->currentIndex());}
  90. ///
  91. /// Remove all the items
  92. void clear();
  93. signals:
  94. void currentIndexChanged(int index);
  95. void activated(int index);
  96. ///
  97. /// This signal is sent after the method 'addItem' has been called programmatically
  98. void itemAdded(int index);
  99. ///
  100. void itemAboutToBeRemoved(int index);
  101. void itemRemoved(int index);
  102. public slots:
  103. ///
  104. /// Select the current index
  105. void setCurrentIndex(int index);
  106. protected slots:
  107. ///
  108. virtual void onAdd();
  109. virtual void onRemove();
  110. virtual void onEdit();
  111. protected:
  112. void setComboBox(QComboBox* comboBox);
  113. QModelIndex rootModelIndex()const;
  114. void setRootModelIndex(const QModelIndex& root);
  115. int modelColumn()const;
  116. QAbstractItemModel* model()const;
  117. private slots:
  118. //void onRowsAboutToBeInserted(const QModelIndex & parent, int start, int end );
  119. void onRowsAboutToBeRemoved(const QModelIndex & parent, int start, int end);
  120. void onRowsInserted(const QModelIndex & parent, int start, int end);
  121. void onRowsRemoved(const QModelIndex & parent, int start, int end);
  122. private:
  123. CTK_DECLARE_PRIVATE(ctkAddRemoveComboBox);
  124. };
  125. #endif