23 #include <QDragEnterEvent>
24 #include <QDragLeaveEvent>
25 #include <QDragMoveEvent>
29 #include <QFileDialog>
31 #include <QHBoxLayout>
36 #include <QSizePolicy>
37 #include <QToolButton>
42 : QWidget(parent), CurrentFilePath(), ToolButton(nullptr), FileLineEdit(nullptr), FileDialogFilter(), FilterList(),
43 Directory(is_directory)
47 ToolButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Ignored);
57 FileLineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
62 QHBoxLayout* layout =
new QHBoxLayout(
this);
63 layout->setSpacing(0);
81 if (CurrentFilePath != value)
83 CurrentFilePath = value;
84 FileLineEdit->setText(CurrentFilePath);
88 emit dataChangedByUser(CurrentFilePath,
this);
89 emit commitData(
this);
97 FileDialogFilter = dialog_filter;
98 FilterList = filter_list;
108 return CurrentFilePath;
114 QString filepath = (Directory ? QFileDialog::getExistingDirectory(
nullptr, tr(
"Directory"), CurrentFilePath,
115 QFileDialog::ShowDirsOnly
116 | QFileDialog::DontUseNativeDialog)
117 : QFileDialog::getOpenFileName(
nullptr, tr(
"Open File"), CurrentFilePath,
118 FileDialogFilter,
nullptr,
119 QFileDialog::DontUseNativeDialog));
120 if (filepath.isNull() ==
false)
122 setFile(filepath,
true);
129 if (ev->type() == QEvent::DragEnter || ev->type() == QEvent::Drop)
132 if (ev->type() == QEvent::DragEnter)
134 dragEnterEvent(
static_cast<QDragEnterEvent*
>(ev));
136 else if (ev->type() == QEvent::Drop)
138 dropEvent(
static_cast<QDropEvent*
>(ev));
141 if (ev->isAccepted())
146 return QWidget::eventFilter(obj, ev);
148 else if (obj == ToolButton && ev->type() == QEvent::KeyPress)
154 else if (obj == FileLineEdit)
156 if (ev->type() == QEvent::FocusOut)
158 setFile(FileLineEdit->text(),
true);
164 return QWidget::eventFilter(obj, ev);
176 if (checkMimeData(event->mimeData(), tmpFileName))
179 event->acceptProposedAction();
186 event->acceptProposedAction();
198 if (checkMimeData(event->mimeData(), tmpFileName))
200 setFile(tmpFileName);
201 emit dataChangedByUser(getFile(),
this);
202 emit commitData(
this);
204 event->acceptProposedAction();
214 QFileInfo tmpFileInfo;
216 foreach(QUrl tmpUrl, tmpUrlList)
217 if (QFile::exists(tmpUrl.toLocalFile()))
219 tmpFileInfo = QFileInfo(tmpUrl.toLocalFile());
break;
222 if (checkFileFilter(tmpFileInfo.fileName()))
224 file = tmpFileInfo.absoluteFilePath();
234 if (FilterList.isEmpty())
239 QFileInfo tmpFileInfo(file);
241 if ((Directory && !tmpFileInfo.isDir()) || (!Directory && !tmpFileInfo.isFile()))
246 foreach(QString tmpFilter, FilterList)
248 QRegExp tmpRegExpFilter(tmpFilter, Qt::CaseInsensitive, QRegExp::Wildcard);
249 if (tmpRegExpFilter.exactMatch(file))