ctkDirectoryButton.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.commontk.org/LICENSE
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QDebug>
  16. #include <QHBoxLayout>
  17. #include <QPushButton>
  18. #include <QStyle>
  19. // CTK includes
  20. #include "ctkDirectoryButton.h"
  21. //-----------------------------------------------------------------------------
  22. class ctkDirectoryButtonPrivate
  23. {
  24. Q_DECLARE_PUBLIC(ctkDirectoryButton);
  25. protected:
  26. ctkDirectoryButton* const q_ptr;
  27. public:
  28. ctkDirectoryButtonPrivate(ctkDirectoryButton& object);
  29. void init();
  30. QDir Directory;
  31. QPushButton* PushButton;
  32. QString DialogCaption;
  33. #ifdef USE_QFILEDIALOG_OPTIONS
  34. QFileDialog::Options DialogOptions;
  35. #else
  36. ctkDirectoryButton::Options DialogOptions;
  37. #endif
  38. // TODO expose DisplayAbsolutePath into the API
  39. bool DisplayAbsolutePath;
  40. };
  41. //-----------------------------------------------------------------------------
  42. ctkDirectoryButtonPrivate::ctkDirectoryButtonPrivate(ctkDirectoryButton& object)
  43. :q_ptr(&object)
  44. {
  45. #if USE_QFILEDIALOG_OPTIONS
  46. this->DialogOptions = QFileDialog::ShowDirsOnly;
  47. #else
  48. this->DialogOptions = ctkDirectoryButton::ShowDirsOnly;
  49. #endif
  50. this->DisplayAbsolutePath = true;
  51. }
  52. //-----------------------------------------------------------------------------
  53. void ctkDirectoryButtonPrivate::init()
  54. {
  55. Q_Q(ctkDirectoryButton);
  56. this->PushButton = new QPushButton(q);
  57. QObject::connect(this->PushButton, SIGNAL(clicked()), q, SLOT(browse()));
  58. QHBoxLayout* l = new QHBoxLayout(q);
  59. l->addWidget(this->PushButton);
  60. l->setContentsMargins(0,0,0,0);
  61. q->setLayout(l);
  62. q->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed, QSizePolicy::ButtonBox));
  63. }
  64. //-----------------------------------------------------------------------------
  65. ctkDirectoryButton::ctkDirectoryButton(QWidget * parentWidget)
  66. :QWidget(parentWidget)
  67. , d_ptr(new ctkDirectoryButtonPrivate(*this))
  68. {
  69. Q_D(ctkDirectoryButton);
  70. d->init();
  71. d->PushButton->setText(d->DisplayAbsolutePath ? d->Directory.absolutePath() : d->Directory.path());
  72. d->PushButton->setIcon(this->style()->standardIcon(QStyle::SP_DirIcon));
  73. }
  74. //-----------------------------------------------------------------------------
  75. ctkDirectoryButton::ctkDirectoryButton(const QString& dir,
  76. QWidget * parentWidget)
  77. :QWidget(parentWidget)
  78. , d_ptr(new ctkDirectoryButtonPrivate(*this))
  79. {
  80. Q_D(ctkDirectoryButton);
  81. d->init();
  82. d->Directory = QDir(dir);
  83. d->PushButton->setText(d->DisplayAbsolutePath ? d->Directory.absolutePath() : d->Directory.path());
  84. d->PushButton->setIcon(this->style()->standardIcon(QStyle::SP_DirIcon));
  85. }
  86. //-----------------------------------------------------------------------------
  87. ctkDirectoryButton::ctkDirectoryButton(
  88. const QIcon & icon, const QString& dir, QWidget * parentWidget)
  89. :QWidget(parentWidget)
  90. , d_ptr(new ctkDirectoryButtonPrivate(*this))
  91. {
  92. Q_D(ctkDirectoryButton);
  93. d->init();
  94. d->Directory = QDir(dir);
  95. d->PushButton->setText(d->DisplayAbsolutePath ? d->Directory.absolutePath() : d->Directory.path());
  96. d->PushButton->setIcon(icon);
  97. }
  98. //-----------------------------------------------------------------------------
  99. ctkDirectoryButton::~ctkDirectoryButton()
  100. {
  101. }
  102. //-----------------------------------------------------------------------------
  103. void ctkDirectoryButton::setDirectory(const QString& dir)
  104. {
  105. Q_D(ctkDirectoryButton);
  106. QDir newDirectory(dir);
  107. if (d->Directory == newDirectory)
  108. {
  109. emit directorySelected(d->DisplayAbsolutePath ?
  110. newDirectory.absolutePath() :
  111. newDirectory.path());
  112. return;
  113. }
  114. d->Directory = newDirectory;
  115. d->PushButton->setText(d->DisplayAbsolutePath ? d->Directory.absolutePath() : d->Directory.path());
  116. emit directorySelected(d->DisplayAbsolutePath ?
  117. newDirectory.absolutePath() :
  118. newDirectory.path());
  119. emit directoryChanged(d->DisplayAbsolutePath ? d->Directory.absolutePath() : d->Directory.path());
  120. }
  121. //-----------------------------------------------------------------------------
  122. QString ctkDirectoryButton::directory()const
  123. {
  124. Q_D(const ctkDirectoryButton);
  125. return d->Directory.path();
  126. }
  127. //-----------------------------------------------------------------------------
  128. void ctkDirectoryButton::setCaption(const QString& caption)
  129. {
  130. Q_D(ctkDirectoryButton);
  131. d->DialogCaption = caption;
  132. }
  133. //-----------------------------------------------------------------------------
  134. const QString& ctkDirectoryButton::caption()const
  135. {
  136. Q_D(const ctkDirectoryButton);
  137. return d->DialogCaption;
  138. }
  139. //-----------------------------------------------------------------------------
  140. #ifdef USE_QFILEDIALOG_OPTIONS
  141. void ctkDirectoryButton::setOptions(const QFileDialog::Options& dialogOptions)
  142. #else
  143. void ctkDirectoryButton::setOptions(const Options& dialogOptions)
  144. #endif
  145. {
  146. Q_D(ctkDirectoryButton);
  147. d->DialogOptions = dialogOptions;
  148. }
  149. //-----------------------------------------------------------------------------
  150. #ifdef USE_QFILEDIALOG_OPTIONS
  151. const QFileDialog::Options& ctkDirectoryButton::options()const
  152. #else
  153. const ctkDirectoryButton::Options& ctkDirectoryButton::options()const
  154. #endif
  155. {
  156. Q_D(const ctkDirectoryButton);
  157. return d->DialogOptions;
  158. }
  159. //-----------------------------------------------------------------------------
  160. void ctkDirectoryButton::browse()
  161. {
  162. Q_D(ctkDirectoryButton);
  163. QString dir =
  164. QFileDialog::getExistingDirectory(
  165. this,
  166. d->DialogCaption.isEmpty() ? this->toolTip() : d->DialogCaption,
  167. d->Directory.path(),
  168. #ifdef USE_QFILEDIALOG_OPTIONS
  169. d->DialogOptions);
  170. #else
  171. QFlags<QFileDialog::Option>(int(d->DialogOptions)));
  172. #endif
  173. // An empty directory means that the user cancelled the dialog.
  174. if (dir.isEmpty())
  175. {
  176. return;
  177. }
  178. this->setDirectory(dir);
  179. }