//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/Widget/FileDialog.cpp
//! @brief     Implements functions in namespace GUI::Dialog.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2021
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "GUI/View/Widget/FileDialog.h"
#include <QFileDialog>

QString GUI::FileDialog::w1_1f(const QString& caption, QString& default_directory,
                               const QString& name_filter, const QString& suggested_name)
{
    QFileDialog dialog(nullptr, caption, default_directory, name_filter);
    dialog.setAcceptMode(QFileDialog::AcceptSave);
    if (!suggested_name.isEmpty())
        dialog.selectFile(suggested_name);
    if (!dialog.exec())
        return {};
    QStringList selected_files = dialog.selectedFiles();
    if (selected_files.size() != 1)
        return {};
    QString fname = selected_files[0];
    default_directory = dialog.directory().dirName();
    return fname;
}

QString GUI::FileDialog::r1_1f(const QString& caption, QString& default_directory,
                               const QString& name_filter, const QString& suggested_name)
{
    QFileDialog dialog(nullptr, caption, default_directory, name_filter);
    dialog.setAcceptMode(QFileDialog::AcceptOpen);
    dialog.setFileMode(QFileDialog::ExistingFile);
    if (!suggested_name.isEmpty())
        dialog.selectFile(suggested_name);
    if (!dialog.exec())
        return {};
    QStringList selected_files = dialog.selectedFiles();
    if (selected_files.size() != 1)
        return {};
    QString fname = selected_files[0];
    default_directory = dialog.directory().dirName();
    return fname;
}
