ctkPathLineEdit.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 <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. #ifdef USE_QFILEDIALOG_OPTIONS
  43. QFileDialog::Options DialogOptions;
  44. #else
  45. ctkPathLineEdit::Options DialogOptions;
  46. #endif
  47. bool HasValidInput; //!< boolean that stores the old state of valid input
  48. static QString sCurrentDirectory; //!< Content the last value of the current directory
  49. static int sMaxHistory; //!< Size of the history, if the history is full and a new value is added, the oldest value is dropped
  50. };
  51. QString ctkPathLineEditPrivate::sCurrentDirectory = "";
  52. int ctkPathLineEditPrivate::sMaxHistory = 5;
  53. //-----------------------------------------------------------------------------
  54. ctkPathLineEditPrivate::ctkPathLineEditPrivate(ctkPathLineEdit& object)
  55. :q_ptr(&object)
  56. {
  57. this->ComboBox = 0;
  58. this->HasValidInput = false;
  59. this->Filters = QDir::AllEntries|QDir::NoDotAndDotDot|QDir::Readable;
  60. }
  61. //-----------------------------------------------------------------------------
  62. void ctkPathLineEditPrivate::init()
  63. {
  64. Q_Q(ctkPathLineEdit);
  65. this->ComboBox = new QComboBox(q);
  66. QHBoxLayout* layout = new QHBoxLayout(q);
  67. layout->addWidget(this->ComboBox);
  68. layout->setContentsMargins(0,0,0,0);
  69. this->ComboBox->setEditable(true);
  70. q->setSizePolicy(QSizePolicy(
  71. QSizePolicy::Expanding, QSizePolicy::Fixed,
  72. QSizePolicy::LineEdit));
  73. QObject::connect(this->ComboBox,SIGNAL(editTextChanged(QString)),
  74. q, SLOT(setCurrentDirectory(QString)));
  75. QObject::connect(this->ComboBox,SIGNAL(editTextChanged(QString)),
  76. q, SLOT(updateHasValidInput()));
  77. }
  78. //-----------------------------------------------------------------------------
  79. void ctkPathLineEditPrivate::updateFilter()
  80. {
  81. Q_Q(ctkPathLineEdit);
  82. // help completion for the QComboBox::QLineEdit
  83. QCompleter *newCompleter = new QCompleter(q);
  84. newCompleter->setModel(new QDirModel(
  85. ctk::nameFiltersToExtensions(this->NameFilters),
  86. this->Filters | QDir::NoDotAndDotDot | QDir::AllDirs,
  87. QDir::Name|QDir::DirsLast, newCompleter));
  88. this->ComboBox->setCompleter(newCompleter);
  89. }
  90. //-----------------------------------------------------------------------------
  91. ctkPathLineEdit::ctkPathLineEdit(QWidget *parentWidget)
  92. : QWidget(parentWidget)
  93. , d_ptr(new ctkPathLineEditPrivate(*this))
  94. {
  95. Q_D(ctkPathLineEdit);
  96. d->init();
  97. this->setNameFilters(nameFilters());
  98. this->setFilters(filters());
  99. }
  100. //-----------------------------------------------------------------------------
  101. ctkPathLineEdit::ctkPathLineEdit(const QString& label,
  102. const QStringList& nameFilters,
  103. Filters filters,
  104. QWidget *parentWidget)
  105. : QWidget(parentWidget)
  106. , d_ptr(new ctkPathLineEditPrivate(*this))
  107. {
  108. Q_D(ctkPathLineEdit);
  109. d->init();
  110. this->setLabel(label);
  111. this->setNameFilters(nameFilters);
  112. this->setFilters(filters);
  113. }
  114. //-----------------------------------------------------------------------------
  115. ctkPathLineEdit::~ctkPathLineEdit()
  116. {
  117. }
  118. //-----------------------------------------------------------------------------
  119. void ctkPathLineEdit::setLabel(const QString &label)
  120. {
  121. Q_D(ctkPathLineEdit);
  122. d->Label = label;
  123. }
  124. //-----------------------------------------------------------------------------
  125. const QString& ctkPathLineEdit::label()const
  126. {
  127. Q_D(const ctkPathLineEdit);
  128. return d->Label;
  129. }
  130. //-----------------------------------------------------------------------------
  131. void ctkPathLineEdit::setNameFilters(const QStringList &nameFilters)
  132. {
  133. Q_D(ctkPathLineEdit);
  134. d->NameFilters = nameFilters;
  135. d->updateFilter();
  136. d->ComboBox->lineEdit()->setValidator(
  137. new QRegExpValidator(
  138. ctk::nameFiltersToRegExp(d->NameFilters), this));
  139. }
  140. //-----------------------------------------------------------------------------
  141. const QStringList& ctkPathLineEdit::nameFilters()const
  142. {
  143. Q_D(const ctkPathLineEdit);
  144. return d->NameFilters;
  145. }
  146. //-----------------------------------------------------------------------------
  147. void ctkPathLineEdit::setFilters(const Filters &filters)
  148. {
  149. Q_D(ctkPathLineEdit);
  150. d->Filters = QFlags<QDir::Filter>(static_cast<int>(filters));
  151. d->updateFilter();
  152. }
  153. //-----------------------------------------------------------------------------
  154. ctkPathLineEdit::Filters ctkPathLineEdit::filters()const
  155. {
  156. Q_D(const ctkPathLineEdit);
  157. return QFlags<ctkPathLineEdit::Filter>(static_cast<int>(d->Filters));
  158. }
  159. //-----------------------------------------------------------------------------
  160. #ifdef USE_QFILEDIALOG_OPTIONS
  161. void ctkPathLineEdit::setOptions(const QFileDialog::Options& dialogOptions)
  162. #else
  163. void ctkPathLineEdit::setOptions(const Options& dialogOptions)
  164. #endif
  165. {
  166. Q_D(ctkPathLineEdit);
  167. d->DialogOptions = dialogOptions;
  168. }
  169. //-----------------------------------------------------------------------------
  170. #ifdef USE_QFILEDIALOG_OPTIONS
  171. const QFileDialog::Options& ctkPathLineEdit::options()const
  172. #else
  173. const ctkPathLineEdit::Options& ctkPathLineEdit::options()const
  174. #endif
  175. {
  176. Q_D(const ctkPathLineEdit);
  177. return d->DialogOptions;
  178. }
  179. //-----------------------------------------------------------------------------
  180. void ctkPathLineEdit::browse()
  181. {
  182. Q_D(ctkPathLineEdit);
  183. QString path = "";
  184. if ( d->Filters & QDir::Files ) //file
  185. {
  186. if ( d->Filters & QDir::Writable) // load or save
  187. {
  188. path = QFileDialog::getSaveFileName(
  189. this,
  190. tr("Select a file to save "),
  191. this->currentPath().isEmpty() ? ctkPathLineEditPrivate::sCurrentDirectory :
  192. this->currentPath(),
  193. d->NameFilters.join(";;"),
  194. 0,
  195. #ifdef USE_QFILEDIALOG_OPTIONS
  196. d->DialogOptions);
  197. #else
  198. QFlags<QFileDialog::Option>(int(d->DialogOptions)));
  199. #endif
  200. }
  201. else
  202. {
  203. path = QFileDialog::getOpenFileName(
  204. this,
  205. QString("Open a file"),
  206. this->currentPath().isEmpty()? ctkPathLineEditPrivate::sCurrentDirectory :
  207. this->currentPath(),
  208. d->NameFilters.join(";;"),
  209. 0,
  210. #ifdef USE_QFILEDIALOG_OPTIONS
  211. d->DialogOptions);
  212. #else
  213. QFlags<QFileDialog::Option>(int(d->DialogOptions)));
  214. #endif
  215. }
  216. }
  217. else //directory
  218. {
  219. path = QFileDialog::getExistingDirectory(
  220. this,
  221. QString("Select a directory..."),
  222. this->currentPath().isEmpty() ? ctkPathLineEditPrivate::sCurrentDirectory :
  223. this->currentPath(),
  224. #ifdef USE_QFILEDIALOG_OPTIONS
  225. d->DialogOptions);
  226. #else
  227. QFlags<QFileDialog::Option>(int(d->DialogOptions)));
  228. #endif
  229. }
  230. if (path.isEmpty())
  231. {
  232. return;
  233. }
  234. this->setCurrentPath(path);
  235. }
  236. //-----------------------------------------------------------------------------
  237. void ctkPathLineEdit::retrieveHistory()
  238. {
  239. Q_D(ctkPathLineEdit);
  240. d->ComboBox->clear();
  241. // fill the combobox using the QSettings
  242. QSettings settings;
  243. QString key = "ctkPathLineEdit/" + this->objectName();
  244. QStringList history = settings.value(key).toStringList();
  245. foreach(QString path, history)
  246. {
  247. d->ComboBox->addItem(path);
  248. if (d->ComboBox->count() >= ctkPathLineEditPrivate::sMaxHistory)
  249. {
  250. break;
  251. }
  252. }
  253. //select the most recent file location
  254. if (this->currentPath().isEmpty())
  255. {
  256. d->ComboBox->setCurrentIndex(0);
  257. }
  258. }
  259. //-----------------------------------------------------------------------------
  260. void ctkPathLineEdit::addCurrentPathToHistory()
  261. {
  262. Q_D(ctkPathLineEdit);
  263. if (this->currentPath().isEmpty())
  264. {
  265. return;
  266. }
  267. QSettings settings;
  268. //keep the same values, add the current value
  269. //if more than m_MaxHistory entrees, drop the oldest.
  270. QString key = "ctkPathLineEdit/" + this->objectName();
  271. QStringList history = settings.value(key).toStringList();
  272. if (history.contains(this->currentPath()))
  273. {
  274. history.removeAll(this->currentPath());
  275. }
  276. history.push_front(this->currentPath());
  277. settings.setValue(key, history);
  278. int index =d->ComboBox->findText(this->currentPath());
  279. if (index >= 0)
  280. {
  281. d->ComboBox->removeItem(index);
  282. }
  283. while (d->ComboBox->count() >= ctkPathLineEditPrivate::sMaxHistory)
  284. {
  285. d->ComboBox->removeItem(d->ComboBox->count() - 1);
  286. }
  287. d->ComboBox->insertItem(0, this->currentPath());
  288. }
  289. //------------------------------------------------------------------------------
  290. void ctkPathLineEdit::setCurrentFileExtension(const QString& extension)
  291. {
  292. QString filename = this->currentPath();
  293. QFileInfo fileInfo(filename);
  294. if (!fileInfo.suffix().isEmpty())
  295. {
  296. filename.replace(fileInfo.suffix(), extension);
  297. }
  298. else
  299. {
  300. filename.append(QString(".") + extension);
  301. }
  302. this->setCurrentPath(filename);
  303. }
  304. //------------------------------------------------------------------------------
  305. QComboBox* ctkPathLineEdit::comboBox() const
  306. {
  307. Q_D(const ctkPathLineEdit);
  308. return d->ComboBox;
  309. }
  310. //------------------------------------------------------------------------------
  311. QString ctkPathLineEdit::currentPath()const
  312. {
  313. Q_D(const ctkPathLineEdit);
  314. return d->ComboBox->currentText();
  315. }
  316. //------------------------------------------------------------------------------
  317. void ctkPathLineEdit::setCurrentPath(const QString& path)
  318. {
  319. Q_D(ctkPathLineEdit);
  320. d->ComboBox->setEditText(path);
  321. }
  322. //------------------------------------------------------------------------------
  323. void ctkPathLineEdit::setCurrentDirectory(const QString& directory)
  324. {
  325. ctkPathLineEditPrivate::sCurrentDirectory = directory;
  326. }
  327. //------------------------------------------------------------------------------
  328. void ctkPathLineEdit::updateHasValidInput()
  329. {
  330. Q_D(ctkPathLineEdit);
  331. bool oldHasValidInput = d->HasValidInput;
  332. d->HasValidInput = d->ComboBox->lineEdit()->hasAcceptableInput();
  333. if (d->HasValidInput)
  334. {
  335. QFileInfo fileInfo(d->ComboBox->currentText());
  336. ctkPathLineEditPrivate::sCurrentDirectory =
  337. fileInfo.isFile() ? fileInfo.absolutePath() : fileInfo.absoluteFilePath();
  338. emit currentPathChanged(d->ComboBox->currentText());
  339. }
  340. if ( d->HasValidInput != oldHasValidInput)
  341. {
  342. emit validInputChanged(d->HasValidInput);
  343. }
  344. }