QCMakeWidgets.cxx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "QCMakeWidgets.h"
  4. #include <utility>
  5. #include <QDirModel>
  6. #include <QFileDialog>
  7. #include <QFileInfo>
  8. #include <QResizeEvent>
  9. #include <QToolButton>
  10. QCMakeFileEditor::QCMakeFileEditor(QWidget* p, QString var)
  11. : QLineEdit(p)
  12. , Variable(std::move(var))
  13. {
  14. this->ToolButton = new QToolButton(this);
  15. this->ToolButton->setText("...");
  16. this->ToolButton->setCursor(QCursor(Qt::ArrowCursor));
  17. QObject::connect(this->ToolButton, SIGNAL(clicked(bool)), this,
  18. SLOT(chooseFile()));
  19. }
  20. QCMakeFilePathEditor::QCMakeFilePathEditor(QWidget* p, const QString& var)
  21. : QCMakeFileEditor(p, var)
  22. {
  23. this->setCompleter(new QCMakeFileCompleter(this, false));
  24. }
  25. QCMakePathEditor::QCMakePathEditor(QWidget* p, const QString& var)
  26. : QCMakeFileEditor(p, var)
  27. {
  28. this->setCompleter(new QCMakeFileCompleter(this, true));
  29. }
  30. void QCMakeFileEditor::resizeEvent(QResizeEvent* e)
  31. {
  32. // make the tool button fit on the right side
  33. int h = e->size().height();
  34. // move the line edit to make room for the tool button
  35. this->setContentsMargins(0, 0, h, 0);
  36. // put the tool button in its place
  37. this->ToolButton->resize(h, h);
  38. this->ToolButton->move(this->width() - h, 0);
  39. }
  40. void QCMakeFilePathEditor::chooseFile()
  41. {
  42. // choose a file and set it
  43. QString path;
  44. QFileInfo info(this->text());
  45. QString title;
  46. if (this->Variable.isEmpty()) {
  47. title = tr("Select File");
  48. } else {
  49. title = tr("Select File for %1");
  50. title = title.arg(this->Variable);
  51. }
  52. emit this->fileDialogExists(true);
  53. path =
  54. QFileDialog::getOpenFileName(this, title, info.absolutePath(), QString(),
  55. nullptr, QFileDialog::DontResolveSymlinks);
  56. emit this->fileDialogExists(false);
  57. if (!path.isEmpty()) {
  58. this->setText(QDir::fromNativeSeparators(path));
  59. }
  60. }
  61. void QCMakePathEditor::chooseFile()
  62. {
  63. // choose a file and set it
  64. QString path;
  65. QString title;
  66. if (this->Variable.isEmpty()) {
  67. title = tr("Select Path");
  68. } else {
  69. title = tr("Select Path for %1");
  70. title = title.arg(this->Variable);
  71. }
  72. emit this->fileDialogExists(true);
  73. path = QFileDialog::getExistingDirectory(this, title, this->text(),
  74. QFileDialog::ShowDirsOnly |
  75. QFileDialog::DontResolveSymlinks);
  76. emit this->fileDialogExists(false);
  77. if (!path.isEmpty()) {
  78. this->setText(QDir::fromNativeSeparators(path));
  79. }
  80. }
  81. // use same QDirModel for all completers
  82. static QDirModel* fileDirModel()
  83. {
  84. static QDirModel* m = nullptr;
  85. if (!m) {
  86. m = new QDirModel();
  87. }
  88. return m;
  89. }
  90. static QDirModel* pathDirModel()
  91. {
  92. static QDirModel* m = nullptr;
  93. if (!m) {
  94. m = new QDirModel();
  95. m->setFilter(QDir::AllDirs | QDir::Drives | QDir::NoDotAndDotDot);
  96. }
  97. return m;
  98. }
  99. QCMakeFileCompleter::QCMakeFileCompleter(QObject* o, bool dirs)
  100. : QCompleter(o)
  101. {
  102. QDirModel* m = dirs ? pathDirModel() : fileDirModel();
  103. this->setModel(m);
  104. }
  105. QString QCMakeFileCompleter::pathFromIndex(const QModelIndex& idx) const
  106. {
  107. return QDir::fromNativeSeparators(QCompleter::pathFromIndex(idx));
  108. }