QCMakeWidgets.cxx 3.4 KB

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