ctkDirectoryButton.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 <QFileSystemModel>
  17. #include <QHBoxLayout>
  18. #include <QPushButton>
  19. #include <QSortFilterProxyModel>
  20. #include <QStyle>
  21. // CTK includes
  22. #include "ctkDirectoryButton.h"
  23. #include "ctkFileDialog.h"
  24. //-----------------------------------------------------------------------------
  25. class ctkDirectoryButtonPrivate
  26. {
  27. Q_DECLARE_PUBLIC(ctkDirectoryButton);
  28. protected:
  29. ctkDirectoryButton* const q_ptr;
  30. public:
  31. ctkDirectoryButtonPrivate(ctkDirectoryButton& object);
  32. void init();
  33. void updateDisplayText();
  34. QDir Directory;
  35. QPushButton* PushButton;
  36. QString DialogCaption;
  37. QString DisplayText;
  38. #ifdef USE_QFILEDIALOG_OPTIONS
  39. QFileDialog::Options DialogOptions;
  40. #else
  41. ctkDirectoryButton::Options DialogOptions;
  42. #endif
  43. // TODO expose DisplayAbsolutePath into the API
  44. bool DisplayAbsolutePath;
  45. QFileDialog::AcceptMode AcceptMode;
  46. };
  47. //-----------------------------------------------------------------------------
  48. ctkDirectoryButtonPrivate::ctkDirectoryButtonPrivate(ctkDirectoryButton& object)
  49. :q_ptr(&object)
  50. {
  51. #if USE_QFILEDIALOG_OPTIONS
  52. this->DialogOptions = QFileDialog::ShowDirsOnly;
  53. #else
  54. this->DialogOptions = ctkDirectoryButton::ShowDirsOnly;
  55. #endif
  56. this->DisplayAbsolutePath = true;
  57. this->AcceptMode = QFileDialog::AcceptOpen;
  58. }
  59. //-----------------------------------------------------------------------------
  60. void ctkDirectoryButtonPrivate::init()
  61. {
  62. Q_Q(ctkDirectoryButton);
  63. this->PushButton = new QPushButton(q);
  64. QObject::connect(this->PushButton, SIGNAL(clicked()), q, SLOT(browse()));
  65. QHBoxLayout* l = new QHBoxLayout(q);
  66. l->addWidget(this->PushButton);
  67. l->setContentsMargins(0,0,0,0);
  68. q->setLayout(l);
  69. q->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed, QSizePolicy::ButtonBox));
  70. }
  71. //-----------------------------------------------------------------------------
  72. void ctkDirectoryButtonPrivate::updateDisplayText()
  73. {
  74. QString buttonText = this->DisplayText;
  75. if (buttonText.isNull())
  76. {
  77. buttonText = this->DisplayAbsolutePath ?
  78. this->Directory.absolutePath() : this->Directory.path();
  79. }
  80. this->PushButton->setText(buttonText);
  81. }
  82. //-----------------------------------------------------------------------------
  83. ctkDirectoryButton::ctkDirectoryButton(QWidget * parentWidget)
  84. :QWidget(parentWidget)
  85. , d_ptr(new ctkDirectoryButtonPrivate(*this))
  86. {
  87. Q_D(ctkDirectoryButton);
  88. d->init();
  89. d->PushButton->setText(d->DisplayAbsolutePath ? d->Directory.absolutePath() : d->Directory.path());
  90. d->PushButton->setIcon(this->style()->standardIcon(QStyle::SP_DirIcon));
  91. }
  92. //-----------------------------------------------------------------------------
  93. ctkDirectoryButton::ctkDirectoryButton(const QString& dir,
  94. QWidget * parentWidget)
  95. :QWidget(parentWidget)
  96. , d_ptr(new ctkDirectoryButtonPrivate(*this))
  97. {
  98. Q_D(ctkDirectoryButton);
  99. d->init();
  100. d->Directory = QDir(dir);
  101. d->PushButton->setText(d->DisplayAbsolutePath ? d->Directory.absolutePath() : d->Directory.path());
  102. d->PushButton->setIcon(this->style()->standardIcon(QStyle::SP_DirIcon));
  103. }
  104. //-----------------------------------------------------------------------------
  105. ctkDirectoryButton::ctkDirectoryButton(
  106. const QIcon & icon, const QString& dir, QWidget * parentWidget)
  107. :QWidget(parentWidget)
  108. , d_ptr(new ctkDirectoryButtonPrivate(*this))
  109. {
  110. Q_D(ctkDirectoryButton);
  111. d->init();
  112. d->Directory = QDir(dir);
  113. d->PushButton->setText(d->DisplayAbsolutePath ? d->Directory.absolutePath() : d->Directory.path());
  114. d->PushButton->setIcon(icon);
  115. }
  116. //-----------------------------------------------------------------------------
  117. ctkDirectoryButton::~ctkDirectoryButton()
  118. {
  119. }
  120. //-----------------------------------------------------------------------------
  121. void ctkDirectoryButton::setDirectory(const QString& dir)
  122. {
  123. Q_D(ctkDirectoryButton);
  124. QDir newDirectory(dir);
  125. if (d->Directory == newDirectory)
  126. {
  127. emit directorySelected(d->DisplayAbsolutePath ?
  128. newDirectory.absolutePath() :
  129. newDirectory.path());
  130. return;
  131. }
  132. d->Directory = newDirectory;
  133. d->updateDisplayText();
  134. emit directorySelected(d->DisplayAbsolutePath ?
  135. newDirectory.absolutePath() :
  136. newDirectory.path());
  137. emit directoryChanged(d->DisplayAbsolutePath ? d->Directory.absolutePath() : d->Directory.path());
  138. }
  139. //-----------------------------------------------------------------------------
  140. QString ctkDirectoryButton::directory()const
  141. {
  142. Q_D(const ctkDirectoryButton);
  143. return d->Directory.path();
  144. }
  145. //-----------------------------------------------------------------------------
  146. void ctkDirectoryButton::setCaption(const QString& caption)
  147. {
  148. Q_D(ctkDirectoryButton);
  149. d->DialogCaption = caption;
  150. }
  151. //-----------------------------------------------------------------------------
  152. const QString& ctkDirectoryButton::caption()const
  153. {
  154. Q_D(const ctkDirectoryButton);
  155. return d->DialogCaption;
  156. }
  157. //-----------------------------------------------------------------------------
  158. void ctkDirectoryButton::setText(const QString& text)
  159. {
  160. Q_D(ctkDirectoryButton);
  161. d->DisplayText = text;
  162. d->updateDisplayText();
  163. }
  164. //-----------------------------------------------------------------------------
  165. const QString& ctkDirectoryButton::text()const
  166. {
  167. Q_D(const ctkDirectoryButton);
  168. return d->DisplayText;
  169. }
  170. //-----------------------------------------------------------------------------
  171. void ctkDirectoryButton::setIcon(const QIcon& newIcon)
  172. {
  173. Q_D(const ctkDirectoryButton);
  174. return d->PushButton->setIcon(newIcon);
  175. }
  176. //-----------------------------------------------------------------------------
  177. QIcon ctkDirectoryButton::icon()const
  178. {
  179. Q_D(const ctkDirectoryButton);
  180. return d->PushButton->icon();
  181. }
  182. //-----------------------------------------------------------------------------
  183. #ifdef USE_QFILEDIALOG_OPTIONS
  184. void ctkDirectoryButton::setOptions(const QFileDialog::Options& dialogOptions)
  185. #else
  186. void ctkDirectoryButton::setOptions(const Options& dialogOptions)
  187. #endif
  188. {
  189. Q_D(ctkDirectoryButton);
  190. d->DialogOptions = dialogOptions;
  191. }
  192. //-----------------------------------------------------------------------------
  193. #ifdef USE_QFILEDIALOG_OPTIONS
  194. const QFileDialog::Options& ctkDirectoryButton::options()const
  195. #else
  196. const ctkDirectoryButton::Options& ctkDirectoryButton::options()const
  197. #endif
  198. {
  199. Q_D(const ctkDirectoryButton);
  200. return d->DialogOptions;
  201. }
  202. //-----------------------------------------------------------------------------
  203. QFileDialog::AcceptMode ctkDirectoryButton::acceptMode() const
  204. {
  205. Q_D(const ctkDirectoryButton);
  206. return d->AcceptMode;
  207. }
  208. //-----------------------------------------------------------------------------
  209. void ctkDirectoryButton::setAcceptMode(QFileDialog::AcceptMode mode)
  210. {
  211. Q_D(ctkDirectoryButton);
  212. d->AcceptMode = mode;
  213. }
  214. //-----------------------------------------------------------------------------
  215. void ctkDirectoryButton::browse()
  216. {
  217. // See https://bugreports.qt-project.org/browse/QTBUG-10244
  218. class ExcludeReadOnlyFilterProxyModel : public QSortFilterProxyModel
  219. {
  220. public:
  221. ExcludeReadOnlyFilterProxyModel(QPalette palette, QObject *parent)
  222. : QSortFilterProxyModel(parent)
  223. , Palette(palette)
  224. {
  225. }
  226. virtual Qt::ItemFlags flags(const QModelIndex& index)const
  227. {
  228. QString filePath =
  229. this->sourceModel()->data(this->mapToSource(index),
  230. QFileSystemModel::FilePathRole).toString();
  231. if (!QFileInfo(filePath).isWritable())
  232. {
  233. // Double clickable (to open) but can't be "choosen".
  234. return Qt::ItemIsSelectable;
  235. }
  236. return this->QSortFilterProxyModel::flags(index);
  237. }
  238. QPalette Palette;
  239. };
  240. Q_D(ctkDirectoryButton);
  241. // Use a ctkFileDialog (vs QFileDialog) for the AcceptSave mode so it does not
  242. // select non writable folders.
  243. QScopedPointer<ctkFileDialog> fileDialog(
  244. new ctkFileDialog(this, d->DialogCaption.isEmpty() ? this->toolTip() :
  245. d->DialogCaption, d->Directory.path()));
  246. #ifdef USE_QFILEDIALOG_OPTIONS
  247. fileDialog->setOptions(d->DialogOptions);
  248. #else
  249. fileDialog->setOptions(QFlags<QFileDialog::Option>(int(d->DialogOptions)));
  250. #endif
  251. fileDialog->setAcceptMode(d->AcceptMode);
  252. fileDialog->setFileMode(QFileDialog::DirectoryOnly);
  253. if (d->AcceptMode == QFileDialog::AcceptSave)
  254. {
  255. // Gray out the non-writable folders. They are still openable with double click,
  256. // but they can't be selected because they don't have the ItemIsEnabled
  257. // flag and because ctkFileDialog would not let it to be selected.
  258. fileDialog->setProxyModel(
  259. new ExcludeReadOnlyFilterProxyModel(this->palette(), fileDialog.data()));
  260. }
  261. QString dir;
  262. if (fileDialog->exec())
  263. {
  264. dir = fileDialog->selectedFiles().at(0);
  265. }
  266. // An empty directory means either that the user cancelled the dialog or the selected directory is readonly
  267. if (dir.isEmpty())
  268. {
  269. return;
  270. }
  271. this->setDirectory(dir);
  272. }