ctkCompleter.h 3.4 KB

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