ctkPathLineEdit.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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 <QAbstractItemView>
  16. #include <QApplication>
  17. #include <QComboBox>
  18. #include <QCompleter>
  19. #include <QDebug>
  20. #include <QDirModel>
  21. #include <QFileDialog>
  22. #include <QHBoxLayout>
  23. #include <QLineEdit>
  24. #include <QRegExp>
  25. #include <QRegExpValidator>
  26. #include <QSettings>
  27. #include <QStyleOptionComboBox>
  28. #include <QToolButton>
  29. // CTK includes
  30. #include "ctkPathLineEdit.h"
  31. #include "ctkUtils.h"
  32. //-----------------------------------------------------------------------------
  33. class ctkPathLineEditPrivate
  34. {
  35. Q_DECLARE_PUBLIC(ctkPathLineEdit);
  36. protected:
  37. ctkPathLineEdit* const q_ptr;
  38. public:
  39. ctkPathLineEditPrivate(ctkPathLineEdit& object);
  40. void init();
  41. QSize recomputeSizeHint(QSize& sh)const;
  42. void updateFilter();
  43. void adjustPathLineEditSize();
  44. void _q_recomputeCompleterPopupSize();
  45. void createPathLineEditWidget(bool useComboBox);
  46. QString settingKey()const;
  47. QLineEdit* LineEdit;
  48. QComboBox* ComboBox;
  49. QToolButton* BrowseButton; //!< "..." button
  50. int MinimumContentsLength;
  51. ctkPathLineEdit::SizeAdjustPolicy SizeAdjustPolicy;
  52. QString Label; //!< used in file dialogs
  53. QStringList NameFilters; //!< Regular expression (in wildcard mode) used to help the user to complete the line
  54. QDir::Filters Filters; //!< Type of path (file, dir...)
  55. #ifdef USE_QFILEDIALOG_OPTIONS
  56. QFileDialog::Options DialogOptions;
  57. #else
  58. ctkPathLineEdit::Options DialogOptions;
  59. #endif
  60. bool HasValidInput; //!< boolean that stores the old state of valid input
  61. QString SettingKey;
  62. static QString sCurrentDirectory; //!< Content the last value of the current directory
  63. static int sMaxHistory; //!< Size of the history, if the history is full and a new value is added, the oldest value is dropped
  64. mutable QSize SizeHint;
  65. mutable QSize MinimumSizeHint;
  66. };
  67. QString ctkPathLineEditPrivate::sCurrentDirectory = "";
  68. int ctkPathLineEditPrivate::sMaxHistory = 5;
  69. //-----------------------------------------------------------------------------
  70. ctkPathLineEditPrivate::ctkPathLineEditPrivate(ctkPathLineEdit& object)
  71. : q_ptr(&object)
  72. , LineEdit(0)
  73. , ComboBox(0)
  74. , BrowseButton(0)
  75. , MinimumContentsLength(0)
  76. , SizeAdjustPolicy(ctkPathLineEdit::AdjustToContentsOnFirstShow)
  77. , Filters(QDir::AllEntries|QDir::NoDotAndDotDot|QDir::Readable)
  78. , HasValidInput(false)
  79. {
  80. }
  81. //-----------------------------------------------------------------------------
  82. void ctkPathLineEditPrivate::init()
  83. {
  84. Q_Q(ctkPathLineEdit);
  85. QHBoxLayout* layout = new QHBoxLayout(q);
  86. layout->setContentsMargins(0,0,0,0);
  87. layout->setSpacing(0); // no space between the combobx and button
  88. this->createPathLineEditWidget(true);
  89. this->BrowseButton = new QToolButton(q);
  90. this->BrowseButton->setText("...");
  91. // Don't vertically stretch the path line edit unnecessary
  92. this->BrowseButton->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Ignored));
  93. this->BrowseButton->setToolTip(q->tr("Open a dialog"));
  94. QObject::connect(this->BrowseButton,SIGNAL(clicked()),
  95. q, SLOT(browse()));
  96. layout->addWidget(this->BrowseButton);
  97. q->setSizePolicy(QSizePolicy(
  98. QSizePolicy::Expanding, QSizePolicy::Fixed,
  99. QSizePolicy::LineEdit));
  100. }
  101. //------------------------------------------------------------------------------
  102. void ctkPathLineEditPrivate::createPathLineEditWidget(bool useComboBox)
  103. {
  104. Q_Q(ctkPathLineEdit);
  105. QString path = q->currentPath();
  106. if (useComboBox)
  107. {
  108. this->ComboBox = new QComboBox(q);
  109. this->ComboBox->setEditable(true);
  110. this->ComboBox->setInsertPolicy(QComboBox::NoInsert);
  111. this->LineEdit = this->ComboBox->lineEdit();
  112. }
  113. else
  114. {
  115. this->ComboBox = 0;
  116. this->LineEdit = new QLineEdit(q);
  117. }
  118. if (q->layout() && q->layout()->itemAt(0))
  119. {
  120. delete q->layout()->itemAt(0)->widget();
  121. }
  122. qobject_cast<QHBoxLayout*>(q->layout())->insertWidget(
  123. 0,
  124. this->ComboBox ? qobject_cast<QWidget*>(this->ComboBox) :
  125. qobject_cast<QWidget*>(this->LineEdit));
  126. this->updateFilter();
  127. q->retrieveHistory();
  128. q->setCurrentPath(path);
  129. QObject::connect(this->LineEdit, SIGNAL(textChanged(QString)),
  130. q, SLOT(setCurrentDirectory(QString)));
  131. QObject::connect(this->LineEdit, SIGNAL(textChanged(QString)),
  132. q, SLOT(updateHasValidInput()));
  133. q->updateGeometry();
  134. }
  135. //------------------------------------------------------------------------------
  136. QSize ctkPathLineEditPrivate::recomputeSizeHint(QSize& sh)const
  137. {
  138. Q_Q(const ctkPathLineEdit);
  139. if (!sh.isValid())
  140. {
  141. int frame = 0;
  142. if (this->ComboBox)
  143. {
  144. QStyleOptionComboBox option;
  145. int arrowWidth = this->ComboBox->style()->subControlRect(
  146. QStyle::CC_ComboBox, &option, QStyle::SC_ComboBoxArrow, this->ComboBox).width()
  147. + (this->ComboBox->hasFrame() ? 2 : 0);
  148. frame = 2 * (this->ComboBox->hasFrame() ? 3 : 0)
  149. + arrowWidth
  150. + 1; // for mac style, not sure why
  151. }
  152. else
  153. {
  154. QStyleOptionFrame option;
  155. int frameWidth = this->LineEdit->style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &option, q);
  156. int horizontalMargin = 2; // QLineEditPrivate::horizontalMargin
  157. // See QLineEdit::sizeHint
  158. frame = 2 * frameWidth
  159. + this->LineEdit->textMargins().left()
  160. + this->LineEdit->textMargins().right()
  161. + this->LineEdit->contentsMargins().left()
  162. + this->LineEdit->contentsMargins().right()
  163. + 2 * horizontalMargin;
  164. }
  165. int browseWidth = 0;
  166. if (q->showBrowseButton())
  167. {
  168. browseWidth = this->BrowseButton->minimumSizeHint().width();
  169. }
  170. // text width
  171. int textWidth = 0;
  172. if (&sh == &this->SizeHint || this->MinimumContentsLength == 0)
  173. {
  174. switch (SizeAdjustPolicy)
  175. {
  176. case ctkPathLineEdit::AdjustToContents:
  177. case ctkPathLineEdit::AdjustToContentsOnFirstShow:
  178. if (this->LineEdit->text().isEmpty())
  179. {
  180. textWidth = 7 * this->LineEdit->fontMetrics().width(QLatin1Char('x'));
  181. }
  182. else
  183. {
  184. textWidth = this->LineEdit->fontMetrics().boundingRect(this->LineEdit->text()).width() + 8;
  185. }
  186. break;
  187. case QComboBox::AdjustToMinimumContentsLength:
  188. default:
  189. ;
  190. }
  191. }
  192. if (this->MinimumContentsLength > 0)
  193. {
  194. textWidth = qMax(textWidth, this->MinimumContentsLength * this->LineEdit->fontMetrics().width(QLatin1Char('X')));
  195. }
  196. int height = (this->ComboBox ? this->ComboBox->minimumSizeHint() :
  197. this->LineEdit->minimumSizeHint()).height();
  198. sh.rwidth() = frame + textWidth + browseWidth;
  199. sh.rheight() = height;
  200. }
  201. return sh.expandedTo(QApplication::globalStrut());
  202. }
  203. //-----------------------------------------------------------------------------
  204. void ctkPathLineEditPrivate::updateFilter()
  205. {
  206. Q_Q(ctkPathLineEdit);
  207. // help completion for the QComboBox::QLineEdit
  208. QCompleter *newCompleter = new QCompleter(q);
  209. newCompleter->setModel(new QDirModel(
  210. ctk::nameFiltersToExtensions(this->NameFilters),
  211. this->Filters | QDir::NoDotAndDotDot | QDir::AllDirs,
  212. QDir::Name|QDir::DirsLast, newCompleter));
  213. this->LineEdit->setCompleter(newCompleter);
  214. QObject::connect(this->LineEdit->completer()->completionModel(), SIGNAL(layoutChanged()),
  215. q, SLOT(_q_recomputeCompleterPopupSize()));
  216. // don't accept invalid path
  217. QRegExpValidator* validator = new QRegExpValidator(
  218. ctk::nameFiltersToRegExp(this->NameFilters), q);
  219. this->LineEdit->setValidator(validator);
  220. }
  221. //-----------------------------------------------------------------------------
  222. void ctkPathLineEditPrivate::adjustPathLineEditSize()
  223. {
  224. Q_Q(ctkPathLineEdit);
  225. if (q->sizeAdjustPolicy() == ctkPathLineEdit::AdjustToContents)
  226. {
  227. q->updateGeometry();
  228. q->adjustSize();
  229. q->update();
  230. }
  231. }
  232. //-----------------------------------------------------------------------------
  233. void ctkPathLineEditPrivate::_q_recomputeCompleterPopupSize()
  234. {
  235. QSize lineEditSize = this->LineEdit->size();
  236. QAbstractItemView* view = this->LineEdit->completer()->popup();
  237. const QFontMetrics& fm = view->fontMetrics();
  238. int iconWidth = 0;
  239. int textWidth = 0;
  240. QStyleOptionFrame option;
  241. int frameWidth = view->style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &option, view);
  242. int frame = 2 * frameWidth
  243. + view->contentsMargins().left()
  244. + view->contentsMargins().right();
  245. QAbstractItemModel* model = this->LineEdit->completer()->completionModel();
  246. for (int i = 0; i < model->rowCount(); ++i)
  247. {
  248. QVariant icon = model->data(model->index(i, 0), Qt::DecorationRole);
  249. if (icon.isValid() && icon.canConvert<QIcon>())
  250. {
  251. iconWidth = qMax(iconWidth, icon.value<QIcon>().availableSizes().front().width() + 4);
  252. }
  253. textWidth = qMax(textWidth, fm.boundingRect(model->data(model->index(i, 0)).toString()).width());
  254. }
  255. view->setMinimumWidth(qMax(frame + iconWidth + textWidth, lineEditSize.width()));
  256. }
  257. //-----------------------------------------------------------------------------
  258. QString ctkPathLineEditPrivate::settingKey()const
  259. {
  260. Q_Q(const ctkPathLineEdit);
  261. return QString("ctkPathLineEdit/") +
  262. (this->SettingKey.isEmpty() ? q->objectName() : this->SettingKey);
  263. }
  264. //-----------------------------------------------------------------------------
  265. ctkPathLineEdit::ctkPathLineEdit(QWidget *parentWidget)
  266. : QWidget(parentWidget)
  267. , d_ptr(new ctkPathLineEditPrivate(*this))
  268. {
  269. Q_D(ctkPathLineEdit);
  270. d->init();
  271. this->setNameFilters(nameFilters());
  272. this->setFilters(filters());
  273. }
  274. //-----------------------------------------------------------------------------
  275. ctkPathLineEdit::ctkPathLineEdit(const QString& label,
  276. const QStringList& nameFilters,
  277. Filters filters,
  278. QWidget *parentWidget)
  279. : QWidget(parentWidget)
  280. , d_ptr(new ctkPathLineEditPrivate(*this))
  281. {
  282. Q_D(ctkPathLineEdit);
  283. d->init();
  284. this->setLabel(label);
  285. this->setNameFilters(nameFilters);
  286. this->setFilters(filters);
  287. }
  288. //-----------------------------------------------------------------------------
  289. ctkPathLineEdit::~ctkPathLineEdit()
  290. {
  291. }
  292. //-----------------------------------------------------------------------------
  293. void ctkPathLineEdit::setLabel(const QString &label)
  294. {
  295. Q_D(ctkPathLineEdit);
  296. d->Label = label;
  297. }
  298. //-----------------------------------------------------------------------------
  299. const QString& ctkPathLineEdit::label()const
  300. {
  301. Q_D(const ctkPathLineEdit);
  302. return d->Label;
  303. }
  304. //-----------------------------------------------------------------------------
  305. void ctkPathLineEdit::setNameFilters(const QStringList &nameFilters)
  306. {
  307. Q_D(ctkPathLineEdit);
  308. d->NameFilters = nameFilters;
  309. d->updateFilter();
  310. }
  311. //-----------------------------------------------------------------------------
  312. const QStringList& ctkPathLineEdit::nameFilters()const
  313. {
  314. Q_D(const ctkPathLineEdit);
  315. return d->NameFilters;
  316. }
  317. //-----------------------------------------------------------------------------
  318. void ctkPathLineEdit::setFilters(const Filters &filters)
  319. {
  320. Q_D(ctkPathLineEdit);
  321. d->Filters = QFlags<QDir::Filter>(static_cast<int>(filters));
  322. d->updateFilter();
  323. }
  324. //-----------------------------------------------------------------------------
  325. ctkPathLineEdit::Filters ctkPathLineEdit::filters()const
  326. {
  327. Q_D(const ctkPathLineEdit);
  328. return QFlags<ctkPathLineEdit::Filter>(static_cast<int>(d->Filters));
  329. }
  330. //-----------------------------------------------------------------------------
  331. #ifdef USE_QFILEDIALOG_OPTIONS
  332. void ctkPathLineEdit::setOptions(const QFileDialog::Options& dialogOptions)
  333. #else
  334. void ctkPathLineEdit::setOptions(const Options& dialogOptions)
  335. #endif
  336. {
  337. Q_D(ctkPathLineEdit);
  338. d->DialogOptions = dialogOptions;
  339. }
  340. //-----------------------------------------------------------------------------
  341. #ifdef USE_QFILEDIALOG_OPTIONS
  342. const QFileDialog::Options& ctkPathLineEdit::options()const
  343. #else
  344. const ctkPathLineEdit::Options& ctkPathLineEdit::options()const
  345. #endif
  346. {
  347. Q_D(const ctkPathLineEdit);
  348. return d->DialogOptions;
  349. }
  350. //-----------------------------------------------------------------------------
  351. void ctkPathLineEdit::browse()
  352. {
  353. Q_D(ctkPathLineEdit);
  354. QString path = "";
  355. if ( d->Filters & QDir::Files ) //file
  356. {
  357. if ( d->Filters & QDir::Writable) // load or save
  358. {
  359. path = QFileDialog::getSaveFileName(
  360. this,
  361. tr("Select a file to save "),
  362. this->currentPath().isEmpty() ? ctkPathLineEditPrivate::sCurrentDirectory :
  363. this->currentPath(),
  364. d->NameFilters.join(";;"),
  365. 0,
  366. #ifdef USE_QFILEDIALOG_OPTIONS
  367. d->DialogOptions);
  368. #else
  369. QFlags<QFileDialog::Option>(int(d->DialogOptions)));
  370. #endif
  371. }
  372. else
  373. {
  374. path = QFileDialog::getOpenFileName(
  375. this,
  376. QString("Open a file"),
  377. this->currentPath().isEmpty()? ctkPathLineEditPrivate::sCurrentDirectory :
  378. this->currentPath(),
  379. d->NameFilters.join(";;"),
  380. 0,
  381. #ifdef USE_QFILEDIALOG_OPTIONS
  382. d->DialogOptions);
  383. #else
  384. QFlags<QFileDialog::Option>(int(d->DialogOptions)));
  385. #endif
  386. }
  387. }
  388. else //directory
  389. {
  390. path = QFileDialog::getExistingDirectory(
  391. this,
  392. QString("Select a directory..."),
  393. this->currentPath().isEmpty() ? ctkPathLineEditPrivate::sCurrentDirectory :
  394. this->currentPath(),
  395. #ifdef USE_QFILEDIALOG_OPTIONS
  396. d->DialogOptions);
  397. #else
  398. QFlags<QFileDialog::Option>(int(d->DialogOptions)));
  399. #endif
  400. }
  401. if (path.isEmpty())
  402. {
  403. return;
  404. }
  405. this->setCurrentPath(path);
  406. }
  407. //-----------------------------------------------------------------------------
  408. void ctkPathLineEdit::retrieveHistory()
  409. {
  410. Q_D(ctkPathLineEdit);
  411. if (d->ComboBox == 0)
  412. {
  413. return;
  414. }
  415. QString path = this->currentPath();
  416. bool wasBlocking = this->blockSignals(true);
  417. d->ComboBox->clear();
  418. // fill the combobox using the QSettings
  419. QSettings settings;
  420. QString key = d->settingKey();
  421. const QStringList history = settings.value(key).toStringList();
  422. foreach(const QString& path, history)
  423. {
  424. d->ComboBox->addItem(path);
  425. if (d->ComboBox->count() >= ctkPathLineEditPrivate::sMaxHistory)
  426. {
  427. break;
  428. }
  429. }
  430. // Restore path or select the most recent file location if none set.
  431. if (path.isEmpty())
  432. {
  433. this->blockSignals(wasBlocking);
  434. d->ComboBox->setCurrentIndex(0);
  435. }
  436. else
  437. {
  438. this->setCurrentPath(path);
  439. this->blockSignals(wasBlocking);
  440. }
  441. }
  442. //-----------------------------------------------------------------------------
  443. void ctkPathLineEdit::addCurrentPathToHistory()
  444. {
  445. Q_D(ctkPathLineEdit);
  446. if (d->ComboBox == 0 ||
  447. this->currentPath().isEmpty())
  448. {
  449. return;
  450. }
  451. QSettings settings;
  452. //keep the same values, add the current value
  453. //if more than m_MaxHistory entrees, drop the oldest.
  454. QString key = d->settingKey();
  455. QStringList history = settings.value(key).toStringList();
  456. QString pathToAdd = this->currentPath();
  457. if (history.contains(pathToAdd))
  458. {
  459. history.removeAll(pathToAdd);
  460. }
  461. history.push_front(pathToAdd);
  462. settings.setValue(key, history);
  463. // don't fire intermediate events.
  464. bool wasBlocking = d->ComboBox->blockSignals(false);
  465. int index = d->ComboBox->findText(this->currentPath());
  466. if (index >= 0)
  467. {
  468. d->ComboBox->removeItem(index);
  469. }
  470. while (d->ComboBox->count() >= ctkPathLineEditPrivate::sMaxHistory)
  471. {
  472. d->ComboBox->removeItem(d->ComboBox->count() - 1);
  473. }
  474. d->ComboBox->insertItem(0, pathToAdd);
  475. d->ComboBox->setCurrentIndex(0);
  476. d->ComboBox->blockSignals(wasBlocking);
  477. }
  478. //------------------------------------------------------------------------------
  479. void ctkPathLineEdit::setCurrentFileExtension(const QString& extension)
  480. {
  481. QString filename = this->currentPath();
  482. QFileInfo fileInfo(filename);
  483. if (!fileInfo.suffix().isEmpty())
  484. {
  485. filename.replace(fileInfo.suffix(), extension);
  486. }
  487. else
  488. {
  489. filename.append(QString(".") + extension);
  490. }
  491. this->setCurrentPath(filename);
  492. }
  493. //------------------------------------------------------------------------------
  494. QComboBox* ctkPathLineEdit::comboBox() const
  495. {
  496. Q_D(const ctkPathLineEdit);
  497. return d->ComboBox;
  498. }
  499. //------------------------------------------------------------------------------
  500. QString ctkPathLineEdit::currentPath()const
  501. {
  502. Q_D(const ctkPathLineEdit);
  503. return d->LineEdit ? d->LineEdit->text() : QString();
  504. }
  505. //------------------------------------------------------------------------------
  506. void ctkPathLineEdit::setCurrentPath(const QString& path)
  507. {
  508. Q_D(ctkPathLineEdit);
  509. d->LineEdit->setText(path);
  510. }
  511. //------------------------------------------------------------------------------
  512. void ctkPathLineEdit::setCurrentDirectory(const QString& directory)
  513. {
  514. ctkPathLineEditPrivate::sCurrentDirectory = directory;
  515. }
  516. //------------------------------------------------------------------------------
  517. void ctkPathLineEdit::updateHasValidInput()
  518. {
  519. Q_D(ctkPathLineEdit);
  520. bool oldHasValidInput = d->HasValidInput;
  521. d->HasValidInput = d->LineEdit->hasAcceptableInput();
  522. if (d->HasValidInput)
  523. {
  524. QFileInfo fileInfo(this->currentPath());
  525. ctkPathLineEditPrivate::sCurrentDirectory =
  526. fileInfo.isFile() ? fileInfo.absolutePath() : fileInfo.absoluteFilePath();
  527. emit currentPathChanged(this->currentPath());
  528. }
  529. if (d->HasValidInput != oldHasValidInput)
  530. {
  531. emit validInputChanged(d->HasValidInput);
  532. }
  533. if (d->SizeAdjustPolicy == AdjustToContents)
  534. {
  535. d->SizeHint = QSize();
  536. d->adjustPathLineEditSize();
  537. this->updateGeometry();
  538. }
  539. }
  540. //------------------------------------------------------------------------------
  541. QString ctkPathLineEdit::settingKey()const
  542. {
  543. Q_D(const ctkPathLineEdit);
  544. return d->SettingKey;
  545. }
  546. //------------------------------------------------------------------------------
  547. void ctkPathLineEdit::setSettingKey(const QString& key)
  548. {
  549. Q_D(ctkPathLineEdit);
  550. d->SettingKey = key;
  551. this->retrieveHistory();
  552. }
  553. //------------------------------------------------------------------------------
  554. bool ctkPathLineEdit::showBrowseButton()const
  555. {
  556. Q_D(const ctkPathLineEdit);
  557. return d->BrowseButton->isVisibleTo(const_cast<ctkPathLineEdit*>(this));
  558. }
  559. //------------------------------------------------------------------------------
  560. void ctkPathLineEdit::setShowBrowseButton(bool visible)
  561. {
  562. Q_D(ctkPathLineEdit);
  563. d->BrowseButton->setVisible(visible);
  564. }
  565. //------------------------------------------------------------------------------
  566. bool ctkPathLineEdit::showHistoryButton()const
  567. {
  568. Q_D(const ctkPathLineEdit);
  569. return d->ComboBox ? true: false;
  570. }
  571. //------------------------------------------------------------------------------
  572. void ctkPathLineEdit::setShowHistoryButton(bool visible)
  573. {
  574. Q_D(ctkPathLineEdit);
  575. d->createPathLineEditWidget(visible);
  576. }
  577. //------------------------------------------------------------------------------
  578. ctkPathLineEdit::SizeAdjustPolicy ctkPathLineEdit::sizeAdjustPolicy() const
  579. {
  580. Q_D(const ctkPathLineEdit);
  581. return d->SizeAdjustPolicy;
  582. }
  583. //------------------------------------------------------------------------------
  584. void ctkPathLineEdit::setSizeAdjustPolicy(ctkPathLineEdit::SizeAdjustPolicy policy)
  585. {
  586. Q_D(ctkPathLineEdit);
  587. if (policy == d->SizeAdjustPolicy)
  588. return;
  589. d->SizeAdjustPolicy = policy;
  590. d->SizeHint = QSize();
  591. d->adjustPathLineEditSize();
  592. this->updateGeometry();
  593. }
  594. //------------------------------------------------------------------------------
  595. int ctkPathLineEdit::minimumContentsLength()const
  596. {
  597. Q_D(const ctkPathLineEdit);
  598. return d->MinimumContentsLength;
  599. }
  600. //------------------------------------------------------------------------------
  601. void ctkPathLineEdit::setMinimumContentsLength(int length)
  602. {
  603. Q_D(ctkPathLineEdit);
  604. if (d->MinimumContentsLength == length || length < 0) return;
  605. d->MinimumContentsLength = length;
  606. if (d->SizeAdjustPolicy == AdjustToContents ||
  607. d->SizeAdjustPolicy == AdjustToMinimumContentsLength)
  608. {
  609. d->SizeHint = QSize();
  610. d->adjustPathLineEditSize();
  611. this->updateGeometry();
  612. }
  613. }
  614. //------------------------------------------------------------------------------
  615. QSize ctkPathLineEdit::minimumSizeHint()const
  616. {
  617. Q_D(const ctkPathLineEdit);
  618. return d->recomputeSizeHint(d->MinimumSizeHint);
  619. }
  620. //------------------------------------------------------------------------------
  621. QSize ctkPathLineEdit::sizeHint()const
  622. {
  623. Q_D(const ctkPathLineEdit);
  624. return d->recomputeSizeHint(d->SizeHint);
  625. }
  626. #include "moc_ctkPathLineEdit.cpp"