ctkPathLineEdit.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 <QComboBox>
  16. #include <QCompleter>
  17. #include <QDebug>
  18. #include <QDirModel>
  19. #include <QFileDialog>
  20. #include <QHBoxLayout>
  21. #include <QLineEdit>
  22. #include <QRegExp>
  23. #include <QRegExpValidator>
  24. #include <QSettings>
  25. // CTK includes
  26. #include "ctkPathLineEdit.h"
  27. #include "ctkUtils.h"
  28. //-----------------------------------------------------------------------------
  29. class ctkPathLineEditPrivate
  30. {
  31. Q_DECLARE_PUBLIC(ctkPathLineEdit);
  32. protected:
  33. ctkPathLineEdit* const q_ptr;
  34. public:
  35. ctkPathLineEditPrivate(ctkPathLineEdit& object);
  36. void init();
  37. void updateFilter();
  38. QComboBox* ComboBox;
  39. QString Label; //!< used in file dialogs
  40. QStringList NameFilters; //!< Regular expression (in wildcard mode) used to help the user to complete the line
  41. QDir::Filters Filters; //!< Type of path (file, dir...)
  42. bool HasValidInput; //!< boolean that stores the old state of valid input
  43. static QString sCurrentDirectory; //!< Content the last value of the current directory
  44. static int sMaxHistory; //!< Size of the history, if the history is full and a new value is added, the oldest value is dropped
  45. };
  46. QString ctkPathLineEditPrivate::sCurrentDirectory = "";
  47. int ctkPathLineEditPrivate::sMaxHistory = 5;
  48. //-----------------------------------------------------------------------------
  49. ctkPathLineEditPrivate::ctkPathLineEditPrivate(ctkPathLineEdit& object)
  50. :q_ptr(&object)
  51. {
  52. this->ComboBox = 0;
  53. this->HasValidInput = false;
  54. this->Filters = QDir::AllEntries|QDir::NoDotAndDotDot|QDir::Readable;
  55. }
  56. //-----------------------------------------------------------------------------
  57. void ctkPathLineEditPrivate::init()
  58. {
  59. Q_Q(ctkPathLineEdit);
  60. this->ComboBox = new QComboBox(q);
  61. QHBoxLayout* layout = new QHBoxLayout(q);
  62. layout->addWidget(this->ComboBox);
  63. layout->setContentsMargins(0,0,0,0);
  64. this->ComboBox->setEditable(true);
  65. q->setSizePolicy(QSizePolicy(
  66. QSizePolicy::Expanding, QSizePolicy::Fixed,
  67. QSizePolicy::LineEdit));
  68. QObject::connect(this->ComboBox,SIGNAL(editTextChanged(const QString&)),
  69. q, SLOT(setCurrentDirectory(const QString&)));
  70. QObject::connect(this->ComboBox,SIGNAL(editTextChanged(const QString&)),
  71. q, SLOT(updateHasValidInput()));
  72. }
  73. //-----------------------------------------------------------------------------
  74. void ctkPathLineEditPrivate::updateFilter()
  75. {
  76. Q_Q(ctkPathLineEdit);
  77. // help completion for the QComboBox::QLineEdit
  78. QCompleter *newCompleter = new QCompleter(q);
  79. newCompleter->setModel(new QDirModel(
  80. ctk::nameFiltersToExtensions(this->NameFilters),
  81. this->Filters | QDir::NoDotAndDotDot | QDir::AllDirs,
  82. QDir::Name|QDir::DirsLast, newCompleter));
  83. this->ComboBox->setCompleter(newCompleter);
  84. }
  85. //-----------------------------------------------------------------------------
  86. ctkPathLineEdit::ctkPathLineEdit(QWidget *parentWidget)
  87. : QWidget(parentWidget)
  88. , d_ptr(new ctkPathLineEditPrivate(*this))
  89. {
  90. Q_D(ctkPathLineEdit);
  91. d->init();
  92. }
  93. //-----------------------------------------------------------------------------
  94. ctkPathLineEdit::ctkPathLineEdit(const QString& label,
  95. const QStringList& nameFilters,
  96. Filters filters,
  97. QWidget *parentWidget)
  98. : QWidget(parentWidget)
  99. , d_ptr(new ctkPathLineEditPrivate(*this))
  100. {
  101. Q_D(ctkPathLineEdit);
  102. d->init();
  103. this->setLabel(label);
  104. this->setNameFilters(nameFilters);
  105. this->setFilters(filters);
  106. }
  107. //-----------------------------------------------------------------------------
  108. ctkPathLineEdit::~ctkPathLineEdit()
  109. {
  110. }
  111. //-----------------------------------------------------------------------------
  112. void ctkPathLineEdit::setLabel(const QString &label)
  113. {
  114. Q_D(ctkPathLineEdit);
  115. d->Label = label;
  116. }
  117. //-----------------------------------------------------------------------------
  118. const QString& ctkPathLineEdit::label()const
  119. {
  120. Q_D(const ctkPathLineEdit);
  121. return d->Label;
  122. }
  123. //-----------------------------------------------------------------------------
  124. void ctkPathLineEdit::setNameFilters(const QStringList &nameFilters)
  125. {
  126. Q_D(ctkPathLineEdit);
  127. d->NameFilters = nameFilters;
  128. d->updateFilter();
  129. d->ComboBox->lineEdit()->setValidator(
  130. new QRegExpValidator(
  131. ctk::nameFiltersToRegExp(d->NameFilters), this));
  132. }
  133. //-----------------------------------------------------------------------------
  134. const QStringList& ctkPathLineEdit::nameFilters()const
  135. {
  136. Q_D(const ctkPathLineEdit);
  137. return d->NameFilters;
  138. }
  139. //-----------------------------------------------------------------------------
  140. void ctkPathLineEdit::setFilters(const Filters &filters)
  141. {
  142. Q_D(ctkPathLineEdit);
  143. d->Filters = QFlags<QDir::Filter>(static_cast<int>(filters));
  144. d->updateFilter();
  145. }
  146. //-----------------------------------------------------------------------------
  147. ctkPathLineEdit::Filters ctkPathLineEdit::filters()const
  148. {
  149. Q_D(const ctkPathLineEdit);
  150. return QFlags<ctkPathLineEdit::Filter>(static_cast<int>(d->Filters));
  151. }
  152. //-----------------------------------------------------------------------------
  153. void ctkPathLineEdit::browse()
  154. {
  155. Q_D(ctkPathLineEdit);
  156. QString path = "";
  157. if ( d->Filters & QDir::Files ) //file
  158. {
  159. if ( d->Filters & QDir::Writable) // load or save
  160. {
  161. path = QFileDialog::getSaveFileName(this,
  162. tr("Select a file to save "),
  163. this->currentPath().isEmpty() ? ctkPathLineEditPrivate::sCurrentDirectory : this->currentPath(),
  164. d->NameFilters.join(";;"));
  165. }
  166. else
  167. {
  168. path = QFileDialog::getOpenFileName(
  169. this,
  170. QString("Open a file"),
  171. this->currentPath().isEmpty()? ctkPathLineEditPrivate::sCurrentDirectory : this->currentPath(),
  172. d->NameFilters.join(";;"));
  173. }
  174. }
  175. else //directory
  176. {
  177. path = QFileDialog::getExistingDirectory(
  178. this,
  179. QString("Select a directory..."),
  180. this->currentPath().isEmpty() ? ctkPathLineEditPrivate::sCurrentDirectory : this->currentPath());
  181. }
  182. if (path.isEmpty())
  183. {
  184. return;
  185. }
  186. this->setCurrentPath(path);
  187. }
  188. //-----------------------------------------------------------------------------
  189. void ctkPathLineEdit::retrieveHistory()
  190. {
  191. Q_D(ctkPathLineEdit);
  192. d->ComboBox->clear();
  193. // fill the combobox using the QSettings
  194. QSettings settings;
  195. QString key = "ctkPathLineEdit/" + this->objectName();
  196. QStringList history = settings.value(key).toStringList();
  197. foreach(QString path, history)
  198. {
  199. d->ComboBox->addItem(path);
  200. if (d->ComboBox->count() >= ctkPathLineEditPrivate::sMaxHistory)
  201. {
  202. break;
  203. }
  204. }
  205. //select the most recent file location
  206. if (this->currentPath().isEmpty())
  207. {
  208. d->ComboBox->setCurrentIndex(0);
  209. }
  210. }
  211. //-----------------------------------------------------------------------------
  212. void ctkPathLineEdit::addCurrentPathToHistory()
  213. {
  214. Q_D(ctkPathLineEdit);
  215. if (this->currentPath().isEmpty())
  216. {
  217. return;
  218. }
  219. QSettings settings;
  220. //keep the same values, add the current value
  221. //if more than m_MaxHistory entrees, drop the oldest.
  222. QString key = "ctkPathLineEdit/" + this->objectName();
  223. QStringList history = settings.value(key).toStringList();
  224. if (history.contains(this->currentPath()))
  225. {
  226. history.removeAll(this->currentPath());
  227. }
  228. history.push_front(this->currentPath());
  229. settings.setValue(key, history);
  230. int index =d->ComboBox->findText(this->currentPath());
  231. if (index >= 0)
  232. {
  233. d->ComboBox->removeItem(index);
  234. }
  235. while (d->ComboBox->count() >= ctkPathLineEditPrivate::sMaxHistory)
  236. {
  237. d->ComboBox->removeItem(d->ComboBox->count() - 1);
  238. }
  239. d->ComboBox->insertItem(0, this->currentPath());
  240. }
  241. //------------------------------------------------------------------------------
  242. void ctkPathLineEdit::setCurrentFileExtension(const QString& extension)
  243. {
  244. QString filename = this->currentPath();
  245. QFileInfo fileInfo(filename);
  246. if (!fileInfo.suffix().isEmpty())
  247. {
  248. filename.replace(fileInfo.suffix(), extension);
  249. }
  250. else
  251. {
  252. filename.append(QString(".") + extension);
  253. }
  254. this->setCurrentPath(filename);
  255. }
  256. //------------------------------------------------------------------------------
  257. QString ctkPathLineEdit::currentPath()const
  258. {
  259. Q_D(const ctkPathLineEdit);
  260. return d->ComboBox->currentText();
  261. }
  262. //------------------------------------------------------------------------------
  263. void ctkPathLineEdit::setCurrentPath(const QString& path)
  264. {
  265. Q_D(ctkPathLineEdit);
  266. d->ComboBox->setEditText(path);
  267. }
  268. //------------------------------------------------------------------------------
  269. void ctkPathLineEdit::setCurrentDirectory(const QString& directory)
  270. {
  271. ctkPathLineEditPrivate::sCurrentDirectory = directory;
  272. }
  273. //------------------------------------------------------------------------------
  274. void ctkPathLineEdit::updateHasValidInput()
  275. {
  276. Q_D(ctkPathLineEdit);
  277. bool oldHasValidInput = d->HasValidInput;
  278. d->HasValidInput = d->ComboBox->lineEdit()->hasAcceptableInput();
  279. if (d->HasValidInput)
  280. {
  281. QFileInfo fileInfo(d->ComboBox->currentText());
  282. ctkPathLineEditPrivate::sCurrentDirectory =
  283. fileInfo.isFile() ? fileInfo.absolutePath() : fileInfo.absoluteFilePath();
  284. emit currentPathChanged(d->ComboBox->currentText());
  285. }
  286. if ( d->HasValidInput != oldHasValidInput)
  287. {
  288. emit validInputChanged(d->HasValidInput);
  289. }
  290. }