From 770c8a831583f84a52db7c66910a2fdf572eddd9 Mon Sep 17 00:00:00 2001 From: Iceyer Date: Thu, 21 Dec 2017 19:36:51 +0800 Subject: [PATCH] feat: add setFileMode/fileMode to dfilechooseredit Change-Id: I8c22a3a44fbab3f6f3b1345eee3bdd566dc285da --- src/widgets/dfilechooseredit.cpp | 25 ++++++++++++++++++------ src/widgets/dfilechooseredit.h | 4 ++++ src/widgets/private/dfilechooseredit_p.h | 1 + 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/widgets/dfilechooseredit.cpp b/src/widgets/dfilechooseredit.cpp index 3b76a5151..490f2a555 100644 --- a/src/widgets/dfilechooseredit.cpp +++ b/src/widgets/dfilechooseredit.cpp @@ -49,6 +49,19 @@ void DFileChooserEdit::setDialogDisplayPosition(DFileChooserEdit::DialogDisplayP d->dialogDisplayPosition = dialogDisplayPosition; } + +void DFileChooserEdit::setFileMode(QFileDialog::FileMode mode) +{ + D_D(DFileChooserEdit); + d->fileMode = mode; +} + +QFileDialog::FileMode DFileChooserEdit::fileMode() const +{ + D_DC(DFileChooserEdit); + return d->fileMode; +} + DFileChooserEditPrivate::DFileChooserEditPrivate(DFileChooserEdit *q) : DLineEditPrivate(q) { @@ -58,7 +71,7 @@ void DFileChooserEditPrivate::init() { D_Q(DFileChooserEdit); - q->setTextMargins(0, 0, 16, 0); + q->setTextMargins(0, 0, 24, 0); q->setReadOnly(true); q->setIconVisible(true); q->connect(q, SIGNAL(iconClicked()), q, SLOT(_q_showFileChooserDialog())); @@ -71,13 +84,13 @@ void DFileChooserEditPrivate::_q_showFileChooserDialog() QFileDialog dialog(q); dialog.setAcceptMode(QFileDialog::AcceptOpen); - dialog.setFileMode(QFileDialog::ExistingFile); + dialog.setFileMode(fileMode); - if(dialogDisplayPosition == DFileChooserEdit::CurrentMonitorCenter) { + if (dialogDisplayPosition == DFileChooserEdit::CurrentMonitorCenter) { QPoint pos = QCursor::pos(); - for(QScreen *screen : qApp->screens()) { - if(screen->geometry().contains(pos)) { + for (QScreen *screen : qApp->screens()) { + if (screen->geometry().contains(pos)) { QRect rect = dialog.geometry(); rect.moveCenter(screen->geometry().center()); dialog.move(rect.topLeft()); @@ -90,7 +103,7 @@ void DFileChooserEditPrivate::_q_showFileChooserDialog() int code = dialog.exec(); - if(code == QDialog::Accepted && !dialog.selectedFiles().isEmpty()) { + if (code == QDialog::Accepted && !dialog.selectedFiles().isEmpty()) { const QString fileName = dialog.selectedFiles().first(); q->setText(fileName); diff --git a/src/widgets/dfilechooseredit.h b/src/widgets/dfilechooseredit.h index 3a261af23..b604f4da6 100644 --- a/src/widgets/dfilechooseredit.h +++ b/src/widgets/dfilechooseredit.h @@ -19,6 +19,7 @@ #define DFILECHOOSEREDIT_H #include "dlineedit.h" +#include DWIDGET_BEGIN_NAMESPACE @@ -41,6 +42,9 @@ class LIBDTKWIDGETSHARED_EXPORT DFileChooserEdit : public DLineEdit DialogDisplayPosition dialogDisplayPosition() const; + void setFileMode(QFileDialog::FileMode mode); + QFileDialog::FileMode fileMode() const; + public Q_SLOTS: void setDialogDisplayPosition(DialogDisplayPosition dialogDisplayPosition); diff --git a/src/widgets/private/dfilechooseredit_p.h b/src/widgets/private/dfilechooseredit_p.h index 5360f9f7b..4e0e1426b 100644 --- a/src/widgets/private/dfilechooseredit_p.h +++ b/src/widgets/private/dfilechooseredit_p.h @@ -37,6 +37,7 @@ class DFileChooserEditPrivate : DLineEditPrivate void _q_showFileChooserDialog(); DFileChooserEdit::DialogDisplayPosition dialogDisplayPosition = DFileChooserEdit::FoloowParentWindow; + QFileDialog::FileMode fileMode = QFileDialog::ExistingFile; }; DWIDGET_END_NAMESPACE