AddCacheEntry.cxx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "AddCacheEntry.h"
  4. #include <QCompleter>
  5. #include <QMetaProperty>
  6. static const int NumTypes = 4;
  7. static const int DefaultTypeIndex = 0;
  8. static const QByteArray TypeStrings[NumTypes] = { "BOOL", "PATH", "FILEPATH",
  9. "STRING" };
  10. static const QCMakeProperty::PropertyType Types[NumTypes] = {
  11. QCMakeProperty::BOOL, QCMakeProperty::PATH, QCMakeProperty::FILEPATH,
  12. QCMakeProperty::STRING
  13. };
  14. AddCacheEntry::AddCacheEntry(QWidget* p, const QStringList& varNames,
  15. const QStringList& varTypes)
  16. : QWidget(p)
  17. , VarNames(varNames)
  18. , VarTypes(varTypes)
  19. {
  20. this->setupUi(this);
  21. for (auto const& elem : TypeStrings) {
  22. this->Type->addItem(elem);
  23. }
  24. QWidget* cb = new QCheckBox();
  25. QWidget* path = new QCMakePathEditor();
  26. QWidget* filepath = new QCMakeFilePathEditor();
  27. QWidget* string = new QLineEdit();
  28. this->StackedWidget->addWidget(cb);
  29. this->StackedWidget->addWidget(path);
  30. this->StackedWidget->addWidget(filepath);
  31. this->StackedWidget->addWidget(string);
  32. AddCacheEntry::setTabOrder(this->Name, this->Type);
  33. AddCacheEntry::setTabOrder(this->Type, cb);
  34. AddCacheEntry::setTabOrder(cb, path);
  35. AddCacheEntry::setTabOrder(path, filepath);
  36. AddCacheEntry::setTabOrder(filepath, string);
  37. AddCacheEntry::setTabOrder(string, this->Description);
  38. QCompleter* completer = new QCompleter(this->VarNames, this);
  39. this->Name->setCompleter(completer);
  40. connect(
  41. completer,
  42. static_cast<void (QCompleter::*)(const QString&)>(&QCompleter::activated),
  43. this, &AddCacheEntry::onCompletionActivated);
  44. }
  45. QString AddCacheEntry::name() const
  46. {
  47. return this->Name->text().trimmed();
  48. }
  49. QVariant AddCacheEntry::value() const
  50. {
  51. QWidget* w = this->StackedWidget->currentWidget();
  52. if (qobject_cast<QLineEdit*>(w)) {
  53. return static_cast<QLineEdit*>(w)->text();
  54. }
  55. if (qobject_cast<QCheckBox*>(w)) {
  56. return static_cast<QCheckBox*>(w)->isChecked();
  57. }
  58. return QVariant();
  59. }
  60. QString AddCacheEntry::description() const
  61. {
  62. return this->Description->text();
  63. }
  64. QCMakeProperty::PropertyType AddCacheEntry::type() const
  65. {
  66. int idx = this->Type->currentIndex();
  67. if (idx >= 0 && idx < NumTypes) {
  68. return Types[idx];
  69. }
  70. return Types[DefaultTypeIndex];
  71. }
  72. QString AddCacheEntry::typeString() const
  73. {
  74. int idx = this->Type->currentIndex();
  75. if (idx >= 0 && idx < NumTypes) {
  76. return TypeStrings[idx];
  77. }
  78. return TypeStrings[DefaultTypeIndex];
  79. }
  80. void AddCacheEntry::onCompletionActivated(const QString& text)
  81. {
  82. int idx = this->VarNames.indexOf(text);
  83. if (idx != -1) {
  84. QString vartype = this->VarTypes[idx];
  85. for (int i = 0; i < NumTypes; i++) {
  86. if (TypeStrings[i] == vartype) {
  87. this->Type->setCurrentIndex(i);
  88. break;
  89. }
  90. }
  91. }
  92. }