Skip to content

Commit

Permalink
src:Add custom file dialog implementation to replace
Browse files Browse the repository at this point in the history
QFileDialog

Signed-off-by: xiaoming <[email protected]>
  • Loading branch information
QQxiaoming committed Nov 6, 2023
1 parent 557d931 commit f5e3a07
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 9 deletions.
1 change: 1 addition & 0 deletions YUVviewer.pro
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ HEADERS += \
src/ImgViewer.h \
src/ImgExport.h \
src/YUVdecoder.h \
src/filedialog.h \
src/configFile.h

FORMS += \
Expand Down
10 changes: 5 additions & 5 deletions src/ImgExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <QFileDialog>
#include <QMessageBox>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgproc/types_c.h>

#include "filedialog.h"
#include "ImgExport.h"

#include "ui_UI_ImgExport.h"
Expand Down Expand Up @@ -127,15 +127,15 @@ void ImgExport::buttonBoxRejected(void)

void ImgExport::export_png(QImage *Img, const QString &name)
{
QString savefile_name = QFileDialog::getSaveFileName(this, "保存文件", name + "-png" + ".png", "Image files(*.png)");
QString savefile_name = FileDialog::getSaveFileName(this, "保存文件", name + "-png" + ".png", "Image files(*.png)");
if(savefile_name != nullptr) {
Img->save(savefile_name);
}
}

void ImgExport::export_yuv(QImage *Img, const QString &sequence, const QString &name)
{
QString savefile_name = QFileDialog::getSaveFileName(this, "保存文件", name + "-" + sequence + ".yuv", "YUV files(*.yuv)");
QString savefile_name = FileDialog::getSaveFileName(this, "保存文件", name + "-" + sequence + ".yuv", "YUV files(*.yuv)");
if(savefile_name != nullptr) {
QFile save(savefile_name);
if (save.open(QIODevice::WriteOnly)) {
Expand Down Expand Up @@ -240,7 +240,7 @@ void ImgExport::export_yuv(QImage *Img, const QString &sequence, const QString &

void ImgExport::export_rgb(QImage *Img, const QString &sequence, const QString &name)
{
QString savefile_name = QFileDialog::getSaveFileName(this, "保存文件", name + "-" + sequence + ".data", "Data files(*.data)");
QString savefile_name = FileDialog::getSaveFileName(this, "保存文件", name + "-" + sequence + ".data", "Data files(*.data)");
if(savefile_name != nullptr) {
QFile save(savefile_name);
if (save.open(QIODevice::WriteOnly)) {
Expand Down Expand Up @@ -289,7 +289,7 @@ void ImgExport::export_bayer(QImage *Img, const QString &sequence,int bit, const
QMessageBox::warning(this, "Warning",
"导出此格式将通过模拟抽样的方式,图像信息将会出现损失!",
QMessageBox::StandardButtons(QMessageBox::Ok));
QString savefile_name = QFileDialog::getSaveFileName(this, "保存文件", name + "-bayer" + sequence + QString::number(bit) +".raw", "RAW files(*.raw)");
QString savefile_name = FileDialog::getSaveFileName(this, "保存文件", name + "-bayer" + sequence + QString::number(bit) +".raw", "RAW files(*.raw)");
if(savefile_name != nullptr) {
QFile save(savefile_name);
if (save.open(QIODevice::WriteOnly)) {
Expand Down
2 changes: 1 addition & 1 deletion src/ImgViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
#include <QString>
#include <QPoint>
#include <QPainter>
#include <QFileDialog>
#include <QMessageBox>
#include <QDebug>
#include "filedialog.h"
#include "ImgViewer.h"
#include "YUVdecoder.h"
#include "ui_UI_ImgViewer.h"
Expand Down
6 changes: 3 additions & 3 deletions src/YUVviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <QApplication>
#include <QScreen>
#include <QMessageBox>
#include <QFileDialog>
#include <QToolTip>
#include <QPoint>
#include <QDebug>
Expand All @@ -30,6 +29,7 @@
#include <QXmlStreamReader>
#include <QTranslator>
#include <QLibraryInfo>
#include "filedialog.h"
#include "YUVviewer.h"
#include "ui_UI_YUVviewer.h"

Expand Down Expand Up @@ -709,7 +709,7 @@ void YUVviewer::openFile() {
if(lastPath.isDir()) {
openDir = YUVviewerConfigFile->config_dict.lastPath;
}
QStringList openfile_list = QFileDialog::getOpenFileNames(this, "选择文件", openDir, "files(*.yuv *.data *.raw *.png)");
QStringList openfile_list = FileDialog::getOpenFileNames(this, "选择文件", openDir, "files(*.yuv *.data *.raw *.png)");
if(openfile_list.size() != 0) {
QFileInfo file(openfile_list[0]);
YUVviewerConfigFile->config_dict.lastPath = file.absolutePath();
Expand All @@ -725,7 +725,7 @@ void YUVviewer::openFolder() {
if(lastPath.isDir()) {
openDir = YUVviewerConfigFile->config_dict.lastPath;
}
QString openfolder_name = QFileDialog::getExistingDirectory(this, "选择文件夹", openDir);
QString openfolder_name = FileDialog::getExistingDirectory(this, "选择文件夹", openDir);
if (!openfolder_name.isEmpty()) {
YUVviewerConfigFile->config_dict.lastPath = openfolder_name;
QDir dir(openfolder_name);
Expand Down
127 changes: 127 additions & 0 deletions src/filedialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* This file is part of the https://github.com/QQxiaoming/quardCRT.git
* project.
*
* Copyright (C) 2023 Quard <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FILEDIALOG_H
#define FILEDIALOG_H

#include <QFileDialog>
#include <QStandardPaths>

class FileDialog
{
private:
static QList<QUrl> getSiderbarUrls(void) {
QList<QUrl> sidebarUrls;
sidebarUrls.append(QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)));
sidebarUrls.append(QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)));
sidebarUrls.append(QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation)));
sidebarUrls.append(QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)));
sidebarUrls.append(QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DownloadLocation)));
sidebarUrls.append(QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::MusicLocation)));
sidebarUrls.append(QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation)));
sidebarUrls.append(QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::MoviesLocation)));
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
sidebarUrls.append(QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::PublicShareLocation)));
#endif
QFileInfoList drivesList = QDir::drives();
foreach (QFileInfo drive, drivesList) {
sidebarUrls.append(QUrl::fromLocalFile(drive.absoluteFilePath()));
}
return sidebarUrls;
}
public:
static QString getOpenFileName(QWidget *parent = nullptr,
const QString &caption = QString(),
const QString &dir = QString(),
const QString &filter = QString(),
QString *selectedFilter = nullptr,
QFileDialog::Options options = QFileDialog::Options()) {
QFileDialog dialog(parent, caption, dir, filter);
dialog.setFileMode(QFileDialog::ExistingFile);
dialog.setAcceptMode(QFileDialog::AcceptOpen);
dialog.setDefaultSuffix("txt");
dialog.setSidebarUrls(FileDialog::getSiderbarUrls());
dialog.setOptions(options);
if (selectedFilter && !selectedFilter->isEmpty())
dialog.selectNameFilter(*selectedFilter);
if (dialog.exec() == QDialog::Accepted) {
if (selectedFilter)
*selectedFilter = dialog.selectedNameFilter();
return dialog.selectedFiles().value(0);
}
return QString();
}

static QStringList getOpenFileNames(QWidget *parent = nullptr,
const QString &caption = QString(),
const QString &dir = QString(),
const QString &filter = QString(),
QString *selectedFilter = nullptr,
QFileDialog::Options options = QFileDialog::Options()) {
QFileDialog dialog(parent, caption, dir, filter);
dialog.setFileMode(QFileDialog::ExistingFiles);
dialog.setAcceptMode(QFileDialog::AcceptOpen);
dialog.setDefaultSuffix("txt");
dialog.setSidebarUrls(FileDialog::getSiderbarUrls());
dialog.setOptions(options);
if (selectedFilter && !selectedFilter->isEmpty())
dialog.selectNameFilter(*selectedFilter);
if (dialog.exec() == QDialog::Accepted) {
if (selectedFilter)
*selectedFilter = dialog.selectedNameFilter();
return dialog.selectedFiles();
}
return QStringList();
}

static QString getSaveFileName(QWidget *parent = nullptr,
const QString &caption = QString(),
const QString &dir = QString(),
const QString &filter = QString(),
QString *selectedFilter = nullptr,
QFileDialog::Options options = QFileDialog::Options()) {
QFileDialog dialog(parent, caption, dir, filter);
dialog.setFileMode(QFileDialog::AnyFile);
dialog.setAcceptMode(QFileDialog::AcceptSave);
dialog.setSidebarUrls(FileDialog::getSiderbarUrls());
dialog.setOptions(options);
if (selectedFilter && !selectedFilter->isEmpty())
dialog.selectNameFilter(*selectedFilter);
if (dialog.exec() == QDialog::Accepted) {
if (selectedFilter)
*selectedFilter = dialog.selectedNameFilter();
return dialog.selectedFiles().value(0);
}
return QString();
}
static QString getExistingDirectory(QWidget *parent = nullptr,
const QString &caption = QString(),
const QString &dir = QString(),
QFileDialog::Options options = QFileDialog::ShowDirsOnly) {
QFileDialog dialog(parent, caption, dir, QString());
dialog.setFileMode(QFileDialog::Directory);
dialog.setSidebarUrls(FileDialog::getSiderbarUrls());
dialog.setOptions(options);
if (dialog.exec() == QDialog::Accepted)
return dialog.selectedFiles().value(0);
return QString();
}
};

#endif // FILEDIALOG_H

0 comments on commit f5e3a07

Please sign in to comment.