QCMakeWidgets.cxx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. QString(), NULL, QFileDialog::DontResolveSymlinks);
  63. this->fileDialogExists(false);
  64. if(!path.isEmpty())
  65. {
  66. this->setText(QDir::fromNativeSeparators(path));
  67. }
  68. }
  69. void QCMakePathEditor::chooseFile()
  70. {
  71. // choose a file and set it
  72. QString path;
  73. QString title;
  74. if(this->Variable.isEmpty())
  75. {
  76. title = tr("Select Path");
  77. }
  78. else
  79. {
  80. title = tr("Select Path for %1");
  81. title = title.arg(this->Variable);
  82. }
  83. this->fileDialogExists(true);
  84. path = QFileDialog::getExistingDirectory(this, title, this->text(),
  85. QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
  86. this->fileDialogExists(false);
  87. if(!path.isEmpty())
  88. {
  89. this->setText(QDir::fromNativeSeparators(path));
  90. }
  91. }
  92. // use same QDirModel for all completers
  93. static QDirModel* fileDirModel()
  94. {
  95. static QDirModel* m = NULL;
  96. if(!m)
  97. {
  98. m = new QDirModel();
  99. }
  100. return m;
  101. }
  102. static QDirModel* pathDirModel()
  103. {
  104. static QDirModel* m = NULL;
  105. if(!m)
  106. {
  107. m = new QDirModel();
  108. m->setFilter(QDir::AllDirs | QDir::Drives | QDir::NoDotAndDotDot);
  109. }
  110. return m;
  111. }
  112. QCMakeFileCompleter::QCMakeFileCompleter(QObject* o, bool dirs)
  113. : QCompleter(o)
  114. {
  115. QDirModel* m = dirs ? pathDirModel() : fileDirModel();
  116. this->setModel(m);
  117. }
  118. QString QCMakeFileCompleter::pathFromIndex(const QModelIndex& idx) const
  119. {
  120. return QDir::fromNativeSeparators(QCompleter::pathFromIndex(idx));
  121. }