QCMakeWidgets.cxx 3.4 KB

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