ctkCompleter.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #ifndef __ctkCompleter_h
  15. #define __ctkCompleter_h
  16. // Qt includes
  17. #include <QCompleter>
  18. #include <QMetaType>
  19. // CTK includes
  20. #include "ctkWidgetsExport.h"
  21. class ctkCompleterPrivate;
  22. /// ctkCompleter is a QCompleter that allows different way of filtering
  23. /// the model, not just by filtering strings that start with the
  24. /// \sa completionPrefix (default behavior).
  25. /// ctkCompleter is a bit hackish as it reimplements a methods (splitPath)
  26. /// from QCompleter in a way that is not intended.
  27. /// Disclaimer, it might not work in all contexts, but seems to work
  28. /// fine with a QLineEdit.
  29. /// e.g.:
  30. /// QStringList model;
  31. /// model << "toto tata tutu";
  32. /// model << "tata toto tutu";
  33. /// ctkCompleter completer(model);
  34. /// completer.setModelFiltering(ctkCompleter::FilterWordStartsWith);
  35. /// QLineEdit lineEdit;
  36. /// lineEdit.setCompleter(&completer);
  37. /// ...
  38. /// If the user types "ta", both entries will show up in the completer
  39. /// If the user types "ot", no entries will show up in the completer
  40. /// however using \sa FilterContains would have shown both.
  41. class CTK_WIDGETS_EXPORT ctkCompleter: public QCompleter
  42. {
  43. Q_OBJECT
  44. Q_ENUMS(ModelFiltering)
  45. /// FilterStartsWith is the default behavior (same as QCompleter).The
  46. /// completer filters out strings that don't start with \sa completionPrefix
  47. /// FilterContains is the most permissive filter, the completer filters out
  48. /// only strings that don't contain the characters from \sa completionPrefix
  49. /// FilterWordStartsWith is useful when strings contain space separated words
  50. /// and \sa completionPrefix applies to the beginnig of any of the words in the
  51. /// string.
  52. Q_PROPERTY(ModelFiltering modelFiltering READ modelFiltering WRITE setModelFiltering)
  53. public:
  54. ctkCompleter(QObject* parent = 0);
  55. ctkCompleter(QAbstractItemModel* model, QObject* parent = 0);
  56. ctkCompleter(const QStringList& list, QObject* parent = 0 );
  57. virtual ~ctkCompleter();
  58. enum ModelFiltering
  59. {
  60. FilterStartsWith=0,
  61. FilterContains,
  62. FilterWordStartsWith
  63. };
  64. ModelFiltering modelFiltering()const;
  65. void setModelFiltering(ModelFiltering filter);
  66. virtual QStringList splitPath(const QString& s)const;
  67. /// ctkCompleter::model() might return a filtered model
  68. /// (QSortFilterAbstractModel) different from the one that was set.
  69. /// QCompleter::setModel should not be used and setSourceModel used
  70. /// instead.
  71. QAbstractItemModel* sourceModel()const;
  72. void setSourceModel(QAbstractItemModel* model);
  73. protected:
  74. QScopedPointer<ctkCompleterPrivate> d_ptr;
  75. private:
  76. Q_DECLARE_PRIVATE(ctkCompleter);
  77. Q_DISABLE_COPY(ctkCompleter);
  78. };
  79. Q_DECLARE_METATYPE(ctkCompleter::ModelFiltering)
  80. #endif // __ctkCompleter_h