diff --git a/src/gui/mrview/qthelpers.cpp b/src/gui/mrview/qthelpers.cpp new file mode 100644 index 0000000000..aa4627e82c --- /dev/null +++ b/src/gui/mrview/qthelpers.cpp @@ -0,0 +1,33 @@ +/* Copyright (c) 2008-2024 the MRtrix3 contributors. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Covered Software is provided under this License on an "as is" + * basis, without warranty of any kind, either expressed, implied, or + * statutory, including, without limitation, warranties that the + * Covered Software is free of defects, merchantable, fit for a + * particular purpose or non-infringing. + * See the Mozilla Public License v. 2.0 for more details. + * + * For more details, see http://www.mrtrix.org/. + */ + +#include "gui/mrview/qthelpers.h" + +#include +#include + +#include + +namespace MR::GUI::MRView::QtHelpers { + +std::string url_to_std_string(const QUrl &url) { + const bool isLocal = url.isLocalFile(); + const std::string str = isLocal ? url.toLocalFile().toStdString() : url.toString().toStdString(); + + return str; +} + +} // namespace MR::GUI::MRView::QtHelpers diff --git a/src/gui/mrview/qthelpers.h b/src/gui/mrview/qthelpers.h new file mode 100644 index 0000000000..b5b8136290 --- /dev/null +++ b/src/gui/mrview/qthelpers.h @@ -0,0 +1,26 @@ +/* Copyright (c) 2008-2024 the MRtrix3 contributors. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Covered Software is provided under this License on an "as is" + * basis, without warranty of any kind, either expressed, implied, or + * statutory, including, without limitation, warranties that the + * Covered Software is free of defects, merchantable, fit for a + * particular purpose or non-infringing. + * See the Mozilla Public License v. 2.0 for more details. + * + * For more details, see http://www.mrtrix.org/. + */ + +#pragma once +#include + +class QUrl; + +namespace MR::GUI::MRView::QtHelpers { + +std::string url_to_std_string(const QUrl &url); + +} // namespace MR::GUI::MRView::QtHelpers diff --git a/src/gui/mrview/tool/fixel/fixel.cpp b/src/gui/mrview/tool/fixel/fixel.cpp index aed8a5b189..d8a749a253 100644 --- a/src/gui/mrview/tool/fixel/fixel.cpp +++ b/src/gui/mrview/tool/fixel/fixel.cpp @@ -17,6 +17,7 @@ #include "gui/mrview/tool/fixel/fixel.h" #include "gui/dialog/file.h" +#include "gui/mrview/qthelpers.h" #include "gui/mrview/tool/fixel/base_fixel.h" #include "gui/mrview/tool/fixel/directory.h" #include "gui/mrview/tool/fixel/image4D.h" @@ -337,7 +338,7 @@ void Fixel::dropEvent(QDropEvent *event) { std::vector list; QList urlList = mimeData->urls(); for (int i = 0; i < urlList.size() && i < max_files; ++i) { - list.push_back(urlList.at(i).path().toUtf8().constData()); + list.push_back(QtHelpers::url_to_std_string(urlList.at(i))); } try { add_images(list); diff --git a/src/gui/mrview/tool/overlay.cpp b/src/gui/mrview/tool/overlay.cpp index a7e71e1221..c9613cd12e 100644 --- a/src/gui/mrview/tool/overlay.cpp +++ b/src/gui/mrview/tool/overlay.cpp @@ -19,6 +19,7 @@ #include "gui/dialog/file.h" #include "gui/mrview/gui_image.h" #include "gui/mrview/mode/slice.h" +#include "gui/mrview/qthelpers.h" #include "gui/mrview/tool/list_model_base.h" #include "gui/mrview/window.h" #include "mrtrix.h" @@ -204,7 +205,7 @@ void Overlay::dropEvent(QDropEvent *event) { QList urlList = mimeData->urls(); for (int i = 0; i < urlList.size() && i < max_files; ++i) { try { - list.push_back(std::make_unique(MR::Header::open(urlList.at(i).path().toUtf8().constData()))); + list.push_back(std::make_unique(MR::Header::open(QtHelpers::url_to_std_string(urlList.at(i))))); } catch (Exception &e) { e.display(); } @@ -775,5 +776,4 @@ bool Overlay::process_commandline_option(const MR::App::ParsedOption &opt) { return false; } - } // namespace MR::GUI::MRView::Tool diff --git a/src/gui/mrview/tool/roi_editor/roi.cpp b/src/gui/mrview/tool/roi_editor/roi.cpp index 9c160e2a03..df5adfb7ea 100644 --- a/src/gui/mrview/tool/roi_editor/roi.cpp +++ b/src/gui/mrview/tool/roi_editor/roi.cpp @@ -16,6 +16,7 @@ #include +#include "gui/mrview/qthelpers.h" #include "gui/mrview/tool/roi_editor/roi.h" #include "gui/cursor.h" @@ -289,7 +290,7 @@ void ROI::dropEvent(QDropEvent *event) { QList urlList = mimeData->urls(); for (int i = 0; i < urlList.size() && i < max_files; ++i) { try { - list.push_back(std::make_unique(MR::Header::open(urlList.at(i).path().toUtf8().constData()))); + list.push_back(std::make_unique(MR::Header::open(QtHelpers::url_to_std_string(urlList.at(i))))); } catch (Exception &e) { e.display(); } diff --git a/src/gui/mrview/tool/tractography/tractography.cpp b/src/gui/mrview/tool/tractography/tractography.cpp index 2db3b73cff..fb9e3aa7f0 100644 --- a/src/gui/mrview/tool/tractography/tractography.cpp +++ b/src/gui/mrview/tool/tractography/tractography.cpp @@ -17,6 +17,7 @@ #include "gui/mrview/tool/tractography/tractography.h" #include "gui/dialog/file.h" #include "gui/lighting_dock.h" +#include "gui/mrview/qthelpers.h" #include "gui/mrview/tool/list_model_base.h" #include "gui/mrview/tool/tractography/track_scalar_file.h" #include "gui/mrview/tool/tractography/tractogram.h" @@ -369,7 +370,7 @@ void Tractography::dropEvent(QDropEvent *event) { std::vector list; QList urlList = mimeData->urls(); for (int i = 0; i < urlList.size() && i < max_files; ++i) { - list.push_back(urlList.at(i).path().toUtf8().constData()); + list.push_back(QtHelpers::url_to_std_string(urlList.at(i))); } try { tractogram_list_model->add_items(list, *this); diff --git a/src/gui/mrview/window.cpp b/src/gui/mrview/window.cpp index 115e4f0ee0..d0e9ccdcbf 100644 --- a/src/gui/mrview/window.cpp +++ b/src/gui/mrview/window.cpp @@ -23,6 +23,7 @@ #include "gui/dialog/progress.h" #include "gui/mrview/mode/base.h" #include "gui/mrview/mode/list.h" +#include "gui/mrview/qthelpers.h" #include "gui/mrview/tool/base.h" #include "gui/mrview/tool/list.h" #include "gui/opengl/gl.h" @@ -154,7 +155,9 @@ void Window::GLArea::dropEvent(QDropEvent *event) { QList urlList = mimeData->urls(); for (int i = 0; i < urlList.size() && i < 32; ++i) { try { - list.push_back(std::make_unique(MR::Header::open(urlList.at(i).path().toUtf8().constData()))); + const auto &url = urlList.at(i); + const auto filePath = QtHelpers::url_to_std_string(url); + list.push_back(std::make_unique(MR::Header::open(filePath))); } catch (Exception &e) { e.display(); }