ctkDirectoryButton.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.apache.org/licenses/LICENSE-2.0.txt
  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. void updateDisplayText();
  31. QDir Directory;
  32. QPushButton* PushButton;
  33. QString DialogCaption;
  34. QString DisplayText;
  35. #ifdef USE_QFILEDIALOG_OPTIONS
  36. QFileDialog::Options DialogOptions;
  37. #else
  38. ctkDirectoryButton::Options DialogOptions;
  39. #endif
  40. // TODO expose DisplayAbsolutePath into the API
  41. bool DisplayAbsolutePath;
  42. };
  43. //-----------------------------------------------------------------------------
  44. ctkDirectoryButtonPrivate::ctkDirectoryButtonPrivate(ctkDirectoryButton& object)
  45. :q_ptr(&object)
  46. {
  47. #if USE_QFILEDIALOG_OPTIONS
  48. this->DialogOptions = QFileDialog::ShowDirsOnly;
  49. #else
  50. this->DialogOptions = ctkDirectoryButton::ShowDirsOnly;
  51. #endif
  52. this->DisplayAbsolutePath = true;
  53. }
  54. //-----------------------------------------------------------------------------
  55. void ctkDirectoryButtonPrivate::init()
  56. {
  57. Q_Q(ctkDirectoryButton);
  58. this->PushButton = new QPushButton(q);
  59. QObject::connect(this->PushButton, SIGNAL(clicked()), q, SLOT(browse()));
  60. QHBoxLayout* l = new QHBoxLayout(q);
  61. l->addWidget(this->PushButton);
  62. l->setContentsMargins(0,0,0,0);
  63. q->setLayout(l);
  64. q->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed, QSizePolicy::ButtonBox));
  65. }
  66. //-----------------------------------------------------------------------------
  67. void ctkDirectoryButtonPrivate::updateDisplayText()
  68. {
  69. QString buttonText = this->DisplayText;
  70. if (buttonText.isNull())
  71. {
  72. buttonText = this->DisplayAbsolutePath ?
  73. this->Directory.absolutePath() : this->Directory.path();
  74. }
  75. this->PushButton->setText(buttonText);
  76. }
  77. //-----------------------------------------------------------------------------
  78. ctkDirectoryButton::ctkDirectoryButton(QWidget * parentWidget)
  79. :QWidget(parentWidget)
  80. , d_ptr(new ctkDirectoryButtonPrivate(*this))
  81. {
  82. Q_D(ctkDirectoryButton);
  83. d->init();
  84. d->PushButton->setText(d->DisplayAbsolutePath ? d->Directory.absolutePath() : d->Directory.path());
  85. d->PushButton->setIcon(this->style()->standardIcon(QStyle::SP_DirIcon));
  86. }
  87. //-----------------------------------------------------------------------------
  88. ctkDirectoryButton::ctkDirectoryButton(const QString& dir,
  89. QWidget * parentWidget)
  90. :QWidget(parentWidget)
  91. , d_ptr(new ctkDirectoryButtonPrivate(*this))
  92. {
  93. Q_D(ctkDirectoryButton);
  94. d->init();
  95. d->Directory = QDir(dir);
  96. d->PushButton->setText(d->DisplayAbsolutePath ? d->Directory.absolutePath() : d->Directory.path());
  97. d->PushButton->setIcon(this->style()->standardIcon(QStyle::SP_DirIcon));
  98. }
  99. //-----------------------------------------------------------------------------
  100. ctkDirectoryButton::ctkDirectoryButton(
  101. const QIcon & icon, const QString& dir, QWidget * parentWidget)
  102. :QWidget(parentWidget)
  103. , d_ptr(new ctkDirectoryButtonPrivate(*this))
  104. {
  105. Q_D(ctkDirectoryButton);
  106. d->init();
  107. d->Directory = QDir(dir);
  108. d->PushButton->setText(d->DisplayAbsolutePath ? d->Directory.absolutePath() : d->Directory.path());
  109. d->PushButton->setIcon(icon);
  110. }
  111. //-----------------------------------------------------------------------------
  112. ctkDirectoryButton::~ctkDirectoryButton()
  113. {
  114. }
  115. //-----------------------------------------------------------------------------
  116. void ctkDirectoryButton::setDirectory(const QString& dir)
  117. {
  118. Q_D(ctkDirectoryButton);
  119. QDir newDirectory(dir);
  120. if (d->Directory == newDirectory)
  121. {
  122. emit directorySelected(d->DisplayAbsolutePath ?
  123. newDirectory.absolutePath() :
  124. newDirectory.path());
  125. return;
  126. }
  127. d->Directory = newDirectory;
  128. d->updateDisplayText();
  129. emit directorySelected(d->DisplayAbsolutePath ?
  130. newDirectory.absolutePath() :
  131. newDirectory.path());
  132. emit directoryChanged(d->DisplayAbsolutePath ? d->Directory.absolutePath() : d->Directory.path());
  133. }
  134. //-----------------------------------------------------------------------------
  135. QString ctkDirectoryButton::directory()const
  136. {
  137. Q_D(const ctkDirectoryButton);
  138. return d->Directory.path();
  139. }
  140. //-----------------------------------------------------------------------------
  141. void ctkDirectoryButton::setCaption(const QString& caption)
  142. {
  143. Q_D(ctkDirectoryButton);
  144. d->DialogCaption = caption;
  145. }
  146. //-----------------------------------------------------------------------------
  147. const QString& ctkDirectoryButton::caption()const
  148. {
  149. Q_D(const ctkDirectoryButton);
  150. return d->DialogCaption;
  151. }
  152. //-----------------------------------------------------------------------------
  153. void ctkDirectoryButton::setText(const QString& text)
  154. {
  155. Q_D(ctkDirectoryButton);
  156. d->DisplayText = text;
  157. d->updateDisplayText();
  158. }
  159. //-----------------------------------------------------------------------------
  160. const QString& ctkDirectoryButton::text()const
  161. {
  162. Q_D(const ctkDirectoryButton);
  163. return d->DisplayText;
  164. }
  165. //-----------------------------------------------------------------------------
  166. void ctkDirectoryButton::setIcon(const QIcon& newIcon)
  167. {
  168. Q_D(const ctkDirectoryButton);
  169. return d->PushButton->setIcon(newIcon);
  170. }
  171. //-----------------------------------------------------------------------------
  172. QIcon ctkDirectoryButton::icon()const
  173. {
  174. Q_D(const ctkDirectoryButton);
  175. return d->PushButton->icon();
  176. }
  177. //-----------------------------------------------------------------------------
  178. #ifdef USE_QFILEDIALOG_OPTIONS
  179. void ctkDirectoryButton::setOptions(const QFileDialog::Options& dialogOptions)
  180. #else
  181. void ctkDirectoryButton::setOptions(const Options& dialogOptions)
  182. #endif
  183. {
  184. Q_D(ctkDirectoryButton);
  185. d->DialogOptions = dialogOptions;
  186. }
  187. //-----------------------------------------------------------------------------
  188. #ifdef USE_QFILEDIALOG_OPTIONS
  189. const QFileDialog::Options& ctkDirectoryButton::options()const
  190. #else
  191. const ctkDirectoryButton::Options& ctkDirectoryButton::options()const
  192. #endif
  193. {
  194. Q_D(const ctkDirectoryButton);
  195. return d->DialogOptions;
  196. }
  197. //-----------------------------------------------------------------------------
  198. void ctkDirectoryButton::browse()
  199. {
  200. Q_D(ctkDirectoryButton);
  201. QString dir =
  202. QFileDialog::getExistingDirectory(
  203. this,
  204. d->DialogCaption.isEmpty() ? this->toolTip() : d->DialogCaption,
  205. d->Directory.path(),
  206. #ifdef USE_QFILEDIALOG_OPTIONS
  207. d->DialogOptions);
  208. #else
  209. QFlags<QFileDialog::Option>(int(d->DialogOptions)));
  210. #endif
  211. // An empty directory means that the user cancelled the dialog.
  212. if (dir.isEmpty())
  213. {
  214. return;
  215. }
  216. this->setDirectory(dir);
  217. }