ctkPathLineEdit.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. }
  98. //-----------------------------------------------------------------------------
  99. ctkPathLineEdit::ctkPathLineEdit(const QString& label,
  100. const QStringList& nameFilters,
  101. Filters filters,
  102. QWidget *parentWidget)
  103. : QWidget(parentWidget)
  104. , d_ptr(new ctkPathLineEditPrivate(*this))
  105. {
  106. Q_D(ctkPathLineEdit);
  107. d->init();
  108. this->setLabel(label);
  109. this->setNameFilters(nameFilters);
  110. this->setFilters(filters);
  111. }
  112. //-----------------------------------------------------------------------------
  113. ctkPathLineEdit::~ctkPathLineEdit()
  114. {
  115. }
  116. //-----------------------------------------------------------------------------
  117. void ctkPathLineEdit::setLabel(const QString &label)
  118. {
  119. Q_D(ctkPathLineEdit);
  120. d->Label = label;
  121. }
  122. //-----------------------------------------------------------------------------
  123. const QString& ctkPathLineEdit::label()const
  124. {
  125. Q_D(const ctkPathLineEdit);
  126. return d->Label;
  127. }
  128. //-----------------------------------------------------------------------------
  129. void ctkPathLineEdit::setNameFilters(const QStringList &nameFilters)
  130. {
  131. Q_D(ctkPathLineEdit);
  132. d->NameFilters = nameFilters;
  133. d->updateFilter();
  134. d->ComboBox->lineEdit()->setValidator(
  135. new QRegExpValidator(
  136. ctk::nameFiltersToRegExp(d->NameFilters), this));
  137. }
  138. //-----------------------------------------------------------------------------
  139. const QStringList& ctkPathLineEdit::nameFilters()const
  140. {
  141. Q_D(const ctkPathLineEdit);
  142. return d->NameFilters;
  143. }
  144. //-----------------------------------------------------------------------------
  145. void ctkPathLineEdit::setFilters(const Filters &filters)
  146. {
  147. Q_D(ctkPathLineEdit);
  148. d->Filters = QFlags<QDir::Filter>(static_cast<int>(filters));
  149. d->updateFilter();
  150. }
  151. //-----------------------------------------------------------------------------
  152. ctkPathLineEdit::Filters ctkPathLineEdit::filters()const
  153. {
  154. Q_D(const ctkPathLineEdit);
  155. return QFlags<ctkPathLineEdit::Filter>(static_cast<int>(d->Filters));
  156. }
  157. //-----------------------------------------------------------------------------
  158. #ifdef USE_QFILEDIALOG_OPTIONS
  159. void ctkPathLineEdit::setOptions(const QFileDialog::Options& dialogOptions)
  160. #else
  161. void ctkPathLineEdit::setOptions(const Options& dialogOptions)
  162. #endif
  163. {
  164. Q_D(ctkPathLineEdit);
  165. d->DialogOptions = dialogOptions;
  166. }
  167. //-----------------------------------------------------------------------------
  168. #ifdef USE_QFILEDIALOG_OPTIONS
  169. const QFileDialog::Options& ctkPathLineEdit::options()const
  170. #else
  171. const ctkPathLineEdit::Options& ctkPathLineEdit::options()const
  172. #endif
  173. {
  174. Q_D(const ctkPathLineEdit);
  175. return d->DialogOptions;
  176. }
  177. //-----------------------------------------------------------------------------
  178. void ctkPathLineEdit::browse()
  179. {
  180. Q_D(ctkPathLineEdit);
  181. QString path = "";
  182. if ( d->Filters & QDir::Files ) //file
  183. {
  184. if ( d->Filters & QDir::Writable) // load or save
  185. {
  186. path = QFileDialog::getSaveFileName(
  187. this,
  188. tr("Select a file to save "),
  189. this->currentPath().isEmpty() ? ctkPathLineEditPrivate::sCurrentDirectory :
  190. this->currentPath(),
  191. d->NameFilters.join(";;"),
  192. 0,
  193. #ifdef USE_QFILEDIALOG_OPTIONS
  194. d->DialogOptions);
  195. #else
  196. QFlags<QFileDialog::Option>(int(d->DialogOptions)));
  197. #endif
  198. }
  199. else
  200. {
  201. path = QFileDialog::getOpenFileName(
  202. this,
  203. QString("Open a file"),
  204. this->currentPath().isEmpty()? ctkPathLineEditPrivate::sCurrentDirectory :
  205. this->currentPath(),
  206. d->NameFilters.join(";;"),
  207. 0,
  208. #ifdef USE_QFILEDIALOG_OPTIONS
  209. d->DialogOptions);
  210. #else
  211. QFlags<QFileDialog::Option>(int(d->DialogOptions)));
  212. #endif
  213. }
  214. }
  215. else //directory
  216. {
  217. path = QFileDialog::getExistingDirectory(
  218. this,
  219. QString("Select a directory..."),
  220. this->currentPath().isEmpty() ? ctkPathLineEditPrivate::sCurrentDirectory :
  221. this->currentPath(),
  222. #ifdef USE_QFILEDIALOG_OPTIONS
  223. d->DialogOptions);
  224. #else
  225. QFlags<QFileDialog::Option>(int(d->DialogOptions)));
  226. #endif
  227. }
  228. if (path.isEmpty())
  229. {
  230. return;
  231. }
  232. this->setCurrentPath(path);
  233. }
  234. //-----------------------------------------------------------------------------
  235. void ctkPathLineEdit::retrieveHistory()
  236. {
  237. Q_D(ctkPathLineEdit);
  238. d->ComboBox->clear();
  239. // fill the combobox using the QSettings
  240. QSettings settings;
  241. QString key = "ctkPathLineEdit/" + this->objectName();
  242. QStringList history = settings.value(key).toStringList();
  243. foreach(QString path, history)
  244. {
  245. d->ComboBox->addItem(path);
  246. if (d->ComboBox->count() >= ctkPathLineEditPrivate::sMaxHistory)
  247. {
  248. break;
  249. }
  250. }
  251. //select the most recent file location
  252. if (this->currentPath().isEmpty())
  253. {
  254. d->ComboBox->setCurrentIndex(0);
  255. }
  256. }
  257. //-----------------------------------------------------------------------------
  258. void ctkPathLineEdit::addCurrentPathToHistory()
  259. {
  260. Q_D(ctkPathLineEdit);
  261. if (this->currentPath().isEmpty())
  262. {
  263. return;
  264. }
  265. QSettings settings;
  266. //keep the same values, add the current value
  267. //if more than m_MaxHistory entrees, drop the oldest.
  268. QString key = "ctkPathLineEdit/" + this->objectName();
  269. QStringList history = settings.value(key).toStringList();
  270. if (history.contains(this->currentPath()))
  271. {
  272. history.removeAll(this->currentPath());
  273. }
  274. history.push_front(this->currentPath());
  275. settings.setValue(key, history);
  276. int index =d->ComboBox->findText(this->currentPath());
  277. if (index >= 0)
  278. {
  279. d->ComboBox->removeItem(index);
  280. }
  281. while (d->ComboBox->count() >= ctkPathLineEditPrivate::sMaxHistory)
  282. {
  283. d->ComboBox->removeItem(d->ComboBox->count() - 1);
  284. }
  285. d->ComboBox->insertItem(0, this->currentPath());
  286. }
  287. //------------------------------------------------------------------------------
  288. void ctkPathLineEdit::setCurrentFileExtension(const QString& extension)
  289. {
  290. QString filename = this->currentPath();
  291. QFileInfo fileInfo(filename);
  292. if (!fileInfo.suffix().isEmpty())
  293. {
  294. filename.replace(fileInfo.suffix(), extension);
  295. }
  296. else
  297. {
  298. filename.append(QString(".") + extension);
  299. }
  300. this->setCurrentPath(filename);
  301. }
  302. //------------------------------------------------------------------------------
  303. QComboBox* ctkPathLineEdit::comboBox() const
  304. {
  305. Q_D(const ctkPathLineEdit);
  306. return d->ComboBox;
  307. }
  308. //------------------------------------------------------------------------------
  309. QString ctkPathLineEdit::currentPath()const
  310. {
  311. Q_D(const ctkPathLineEdit);
  312. return d->ComboBox->currentText();
  313. }
  314. //------------------------------------------------------------------------------
  315. void ctkPathLineEdit::setCurrentPath(const QString& path)
  316. {
  317. Q_D(ctkPathLineEdit);
  318. d->ComboBox->setEditText(path);
  319. }
  320. //------------------------------------------------------------------------------
  321. void ctkPathLineEdit::setCurrentDirectory(const QString& directory)
  322. {
  323. ctkPathLineEditPrivate::sCurrentDirectory = directory;
  324. }
  325. //------------------------------------------------------------------------------
  326. void ctkPathLineEdit::updateHasValidInput()
  327. {
  328. Q_D(ctkPathLineEdit);
  329. bool oldHasValidInput = d->HasValidInput;
  330. d->HasValidInput = d->ComboBox->lineEdit()->hasAcceptableInput();
  331. if (d->HasValidInput)
  332. {
  333. QFileInfo fileInfo(d->ComboBox->currentText());
  334. ctkPathLineEditPrivate::sCurrentDirectory =
  335. fileInfo.isFile() ? fileInfo.absolutePath() : fileInfo.absoluteFilePath();
  336. emit currentPathChanged(d->ComboBox->currentText());
  337. }
  338. if ( d->HasValidInput != oldHasValidInput)
  339. {
  340. emit validInputChanged(d->HasValidInput);
  341. }
  342. }