QCMakeWidgets.cxx 3.2 KB

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