1 
2 //
3 // This source file is part of appleseed.
4 // Visit https://appleseedhq.net/ for additional information and resources.
5 //
6 // This software is released under the MIT license.
7 //
8 // Copyright (c) 2010-2013 Francois Beaune, Jupiter Jazz Limited
9 // Copyright (c) 2014-2018 Francois Beaune, The appleseedhq Organization
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining a copy
12 // of this software and associated documentation files (the "Software"), to deal
13 // in the Software without restriction, including without limitation the rights
14 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 // copies of the Software, and to permit persons to whom the Software is
16 // furnished to do so, subject to the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 // THE SOFTWARE.
28 //
29 
30 #pragma once
31 
32 // Qt headers.
33 #include <QFileDialog>
34 #include <QList>
35 
36 // Forward declarations.
37 namespace renderer  { class ParamArray; }
38 class QByteArray;
39 class QIcon;
40 class QLayout;
41 class QMessageBox;
42 class QShortcut;
43 class QString;
44 class QStringList;
45 class QWidget;
46 
47 namespace appleseed {
48 namespace studio {
49 
50 // File dialog filter string for bitmap files supported by Qt.
51 extern const QString g_qt_image_files_filter;
52 
53 // Return the file dialog filter string for image file formats supported by OpenImageIO.
54 QString get_oiio_image_files_filter();
55 
56 enum ProjectFilesFilter
57 {
58     ProjectFilesFilterAllProjects       = 1UL << 0,     // all project files extensions
59     ProjectFilesFilterPlainProjects     = 1UL << 1,     // .appleseed extension
60     ProjectFilesFilterPackedProjects    = 1UL << 2,     // .appleseedz extension
61     ProjectFilesFilterDefault           =
62           ProjectFilesFilterAllProjects
63         | ProjectFilesFilterPlainProjects
64         | ProjectFilesFilterPackedProjects
65 };
66 
67 // Return a file dialog filter string for appleseed projects.
68 // `filters` is a combination of ProjectFilesFilter values.
69 QString get_project_files_filter(const int filters = ProjectFilesFilterDefault);
70 
71 // Combine two filesystem paths and convert the result to native separators.
72 QString combine_paths(const QString& lhs, const QString& rhs);
73 
74 // Combine the application's base path and a given relative path.
75 QString make_app_path(const QString& path);
76 
77 // Combine the action tooltip's name and shortcut.
78 QString combine_name_and_shortcut(const QString& name, const QKeySequence& shortcut);
79 
80 // Check whether a file exists.
81 bool file_exists(const QString& path);
82 
83 // Load a GLSL shader from file into a QByteArray.
84 QByteArray load_gl_shader(const QString& base_name);
85 
86 // Load an icon and its variants (hover, disabled...) from the application's icons directory.
87 QIcon load_icons(const QString& base_name);
88 
89 QString get_open_filename(
90     QWidget*                parent,
91     const QString&          caption,
92     const QString&          filter,
93     renderer::ParamArray&   settings,
94     const QString&          settings_key,
95     QFileDialog::Options    options = 0);
96 
97 QStringList get_open_filenames(
98     QWidget*                parent,
99     const QString&          caption,
100     const QString&          filter,
101     renderer::ParamArray&   settings,
102     const QString&          settings_key,
103     QFileDialog::Options    options = 0);
104 
105 QString get_save_filename(
106     QWidget*                parent,
107     const QString&          caption,
108     const QString&          filter,
109     renderer::ParamArray&   settings,
110     const QString&          settings_key,
111     QFileDialog::Options    options = 0);
112 
113 // Disable the blue focus rectangle of certain widgets. macOS only.
114 void disable_osx_focus_rect(QWidget* widget);
115 
116 // Set the minimum width of a QMessageBox.
117 void set_minimum_width(QMessageBox& msgbox, const int minimum_width);
118 
119 // Create a keyboard shortcut that is active for a given window and its
120 // child widgets, but not for its top-level children like subwindows.
121 QShortcut* create_window_local_shortcut(QWidget* parent, const QKeySequence key_sequence);
122 
123 // Remove all widgets and sub-layouts from a layout.
124 void clear_layout(QLayout* layout);
125 
126 // Create a QList from a single item (workaround for pre-C++11 compilers).
127 template <typename T>
128 QList<T> make_qlist(const T& item);
129 
130 // Convert a QList<FromType> to a QList<ToType> by static_cast<>'ing items to type ToType.
131 template <typename ToType, typename FromType>
132 QList<ToType> qlist_static_cast(const QList<FromType>& list);
133 
134 
135 //
136 // Implementation.
137 //
138 
139 template <typename T>
make_qlist(const T & item)140 QList<T> make_qlist(const T& item)
141 {
142     QList<T> result;
143     result.append(item);
144     return result;
145 }
146 
147 template <typename ToType, typename FromType>
qlist_static_cast(const QList<FromType> & list)148 QList<ToType> qlist_static_cast(const QList<FromType>& list)
149 {
150     QList<ToType> result;
151     result.reserve(list.size());
152 
153     for (int i = 0, e = list.size(); i < e; ++i)
154         result.append(static_cast<ToType>(list[i]));
155 
156     return result;
157 }
158 
159 }   // namespace studio
160 }   // namespace appleseed
161