AddCacheEntry.cxx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 (int i = 0; i < NumTypes; i++) {
  22. this->Type->addItem(TypeStrings[i]);
  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. this->setTabOrder(this->Name, this->Type);
  33. this->setTabOrder(this->Type, cb);
  34. this->setTabOrder(cb, path);
  35. this->setTabOrder(path, filepath);
  36. this->setTabOrder(filepath, string);
  37. this->setTabOrder(string, this->Description);
  38. QCompleter* completer = new QCompleter(this->VarNames, this);
  39. this->Name->setCompleter(completer);
  40. connect(completer, SIGNAL(activated(const QString&)), this,
  41. SLOT(onCompletionActivated(const QString&)));
  42. }
  43. QString AddCacheEntry::name() const
  44. {
  45. return this->Name->text().trimmed();
  46. }
  47. QVariant AddCacheEntry::value() const
  48. {
  49. QWidget* w = this->StackedWidget->currentWidget();
  50. if (qobject_cast<QLineEdit*>(w)) {
  51. return static_cast<QLineEdit*>(w)->text();
  52. }
  53. if (qobject_cast<QCheckBox*>(w)) {
  54. return static_cast<QCheckBox*>(w)->isChecked();
  55. }
  56. return QVariant();
  57. }
  58. QString AddCacheEntry::description() const
  59. {
  60. return this->Description->text();
  61. }
  62. QCMakeProperty::PropertyType AddCacheEntry::type() const
  63. {
  64. int idx = this->Type->currentIndex();
  65. if (idx >= 0 && idx < NumTypes) {
  66. return Types[idx];
  67. }
  68. return Types[DefaultTypeIndex];
  69. }
  70. QString AddCacheEntry::typeString() const
  71. {
  72. int idx = this->Type->currentIndex();
  73. if (idx >= 0 && idx < NumTypes) {
  74. return TypeStrings[idx];
  75. }
  76. return TypeStrings[DefaultTypeIndex];
  77. }
  78. void AddCacheEntry::onCompletionActivated(const QString& text)
  79. {
  80. int idx = this->VarNames.indexOf(text);
  81. if (idx != -1) {
  82. QString vartype = this->VarTypes[idx];
  83. for (int i = 0; i < NumTypes; i++) {
  84. if (TypeStrings[i] == vartype) {
  85. this->Type->setCurrentIndex(i);
  86. break;
  87. }
  88. }
  89. }
  90. }