QCMakeWidgets.cxx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "QCMakeWidgets.h"
  14. #include <QDirModel>
  15. #include <QFileInfo>
  16. #include <QFileDialog>
  17. #include <QToolButton>
  18. #include <QResizeEvent>
  19. QCMakeFileEditor::QCMakeFileEditor(QWidget* p, const QString& var)
  20. : QLineEdit(p), Variable(var)
  21. {
  22. this->ToolButton = new QToolButton(this);
  23. this->ToolButton->setText("...");
  24. this->ToolButton->setCursor(QCursor(Qt::ArrowCursor));
  25. QObject::connect(this->ToolButton, SIGNAL(clicked(bool)),
  26. this, SLOT(chooseFile()));
  27. }
  28. QCMakeFilePathEditor::QCMakeFilePathEditor(QWidget* p, const QString& var)
  29. : QCMakeFileEditor(p, var)
  30. {
  31. this->setCompleter(new QCMakeFileCompleter(this, false));
  32. }
  33. QCMakePathEditor::QCMakePathEditor(QWidget* p, const QString& var)
  34. : QCMakeFileEditor(p, var)
  35. {
  36. this->setCompleter(new QCMakeFileCompleter(this, true));
  37. }
  38. void QCMakeFileEditor::resizeEvent(QResizeEvent* e)
  39. {
  40. // make the tool button fit on the right side
  41. int h = e->size().height();
  42. // move the line edit to make room for the tool button
  43. this->setContentsMargins(0, 0, h, 0);
  44. // put the tool button in its place
  45. this->ToolButton->resize(h, h);
  46. this->ToolButton->move(this->width() - h, 0);
  47. }
  48. void QCMakeFilePathEditor::chooseFile()
  49. {
  50. // choose a file and set it
  51. QString path;
  52. QFileInfo info(this->text());
  53. QString title;
  54. if(this->Variable.isEmpty())
  55. {
  56. title = tr("Select File");
  57. }
  58. else
  59. {
  60. title = tr("Select File for %1");
  61. title = title.arg(this->Variable);
  62. }
  63. this->fileDialogExists(true);
  64. path = QFileDialog::getOpenFileName(this, title, info.absolutePath());
  65. this->fileDialogExists(false);
  66. if(!path.isEmpty())
  67. {
  68. this->setText(QDir::fromNativeSeparators(path));
  69. }
  70. }
  71. void QCMakePathEditor::chooseFile()
  72. {
  73. // choose a file and set it
  74. QString path;
  75. QString title;
  76. if(this->Variable.isEmpty())
  77. {
  78. title = tr("Select Path");
  79. }
  80. else
  81. {
  82. title = tr("Select Path for %1");
  83. title = title.arg(this->Variable);
  84. }
  85. this->fileDialogExists(true);
  86. path = QFileDialog::getExistingDirectory(this, title, this->text());
  87. this->fileDialogExists(false);
  88. if(!path.isEmpty())
  89. {
  90. this->setText(QDir::fromNativeSeparators(path));
  91. }
  92. }
  93. QCMakeFileCompleter::QCMakeFileCompleter(QObject* o, bool dirs)
  94. : QCompleter(o)
  95. {
  96. QDirModel* model = new QDirModel(this);
  97. if(dirs)
  98. {
  99. model->setFilter(QDir::AllDirs | QDir::Drives | QDir::NoDotAndDotDot);
  100. }
  101. this->setModel(model);
  102. }
  103. QString QCMakeFileCompleter::pathFromIndex(const QModelIndex& idx) const
  104. {
  105. return QDir::fromNativeSeparators(QCompleter::pathFromIndex(idx));
  106. }