Visual Computing Library
Loading...
Searching...
No Matches
file_format.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_QT_UTILS_FILE_FORMAT_H
24#define VCL_QT_UTILS_FILE_FORMAT_H
25
26#include <vclib/io/file_format.h>
27
28#include <QString>
29
30namespace vcl::qt {
31
32inline QString filterFormatToQString(const FileFormat& format)
33{
34 QString filter = QString::fromStdString(format.description());
35 filter += " (";
36 for (const auto& ext : format.extensions()) {
37 filter += "*." + QString::fromStdString(ext) + " ";
38 }
39 filter.chop(1);
40 filter += ")";
41 return filter;
42}
43
44inline QString filterFormatsToQString(
45 const std::vector<FileFormat>& formats,
46 bool allFormats = false)
47{
48 QString filter;
49 if (allFormats) {
50 filter += "All supported formats (";
51 for (const auto& format : formats) {
52 for (const auto& ext : format.extensions()) {
53 filter += "*." + QString::fromStdString(ext) + " ";
54 }
55 }
56 filter.chop(1);
57 filter += ");;";
58 }
59 for (const auto& format : formats) {
60 filter += filterFormatToQString(format);
61 filter += ";;";
62 }
63 // remove last two chars
64 filter.chop(2);
65 return filter;
66}
67
68inline FileFormat formatFromQStringFilter(const QString& filter)
69{
70 // find the first "(*."
71 int start = filter.indexOf("(*.") + 3;
72 // find the first ")" after start
73 int end = filter.indexOf(")", start);
74 // extract the extension between start and end
75 QString ext = filter.mid(start, end - start);
76 return FileFormat(ext.toStdString());
77}
78
79} // namespace vcl::qt
80
81#endif // VCL_QT_UTILS_FILE_FORMAT_H