AddCacheEntry.cxx 3.2 KB

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