AddCacheEntry.cxx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <QMetaProperty>
  12. #include <QCompleter>
  13. static const int NumTypes = 4;
  14. static const int DefaultTypeIndex = 0;
  15. static const QByteArray TypeStrings[NumTypes] =
  16. { "BOOL", "PATH", "FILEPATH", "STRING" };
  17. static const QCMakeProperty::PropertyType Types[NumTypes] =
  18. { QCMakeProperty::BOOL, QCMakeProperty::PATH,
  19. QCMakeProperty::FILEPATH, QCMakeProperty::STRING};
  20. AddCacheEntry::AddCacheEntry(QWidget* p, const QStringList& varNames,
  21. const QStringList& varTypes)
  22. : QWidget(p), VarNames(varNames), VarTypes(varTypes)
  23. {
  24. this->setupUi(this);
  25. for(int i=0; i<NumTypes; i++)
  26. {
  27. this->Type->addItem(TypeStrings[i]);
  28. }
  29. QWidget* cb = new QCheckBox();
  30. QWidget* path = new QCMakePathEditor();
  31. QWidget* filepath = new QCMakeFilePathEditor();
  32. QWidget* string = new QLineEdit();
  33. this->StackedWidget->addWidget(cb);
  34. this->StackedWidget->addWidget(path);
  35. this->StackedWidget->addWidget(filepath);
  36. this->StackedWidget->addWidget(string);
  37. this->setTabOrder(this->Name, this->Type);
  38. this->setTabOrder(this->Type, cb);
  39. this->setTabOrder(cb, path);
  40. this->setTabOrder(path, filepath);
  41. this->setTabOrder(filepath, string);
  42. this->setTabOrder(string, this->Description);
  43. QCompleter *completer = new QCompleter(this->VarNames, this);
  44. this->Name->setCompleter(completer);
  45. connect(completer, SIGNAL(activated(const QString&)),
  46. this, SLOT(onCompletionActivated(const QString&)));
  47. }
  48. QString AddCacheEntry::name() const
  49. {
  50. return this->Name->text();
  51. }
  52. QVariant AddCacheEntry::value() const
  53. {
  54. QWidget* w = this->StackedWidget->currentWidget();
  55. if(qobject_cast<QLineEdit*>(w))
  56. {
  57. return static_cast<QLineEdit*>(w)->text();
  58. }
  59. else if(qobject_cast<QCheckBox*>(w))
  60. {
  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. {
  74. return Types[idx];
  75. }
  76. return Types[DefaultTypeIndex];
  77. }
  78. QString AddCacheEntry::typeString() const
  79. {
  80. int idx = this->Type->currentIndex();
  81. if(idx >= 0 && idx < NumTypes)
  82. {
  83. return TypeStrings[idx];
  84. }
  85. return TypeStrings[DefaultTypeIndex];
  86. }
  87. void AddCacheEntry::onCompletionActivated(const QString &text)
  88. {
  89. int idx = this->VarNames.indexOf(text);
  90. if (idx != -1)
  91. {
  92. QString vartype = this->VarTypes[idx];
  93. for (int i = 0; i < NumTypes; i++)
  94. {
  95. if (TypeStrings[i] == vartype)
  96. {
  97. this->Type->setCurrentIndex(i);
  98. break;
  99. }
  100. }
  101. }
  102. }