Mercurial > hg > orthanc-stone
annotate Applications/IBasicApplication.cpp @ 161:197a5ddaf68c wasm
FiniteProjectiveCamera
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 14 Feb 2018 11:29:26 +0100 |
parents | e2fe9352f240 |
children | fccffbf99ba1 |
rev | line source |
---|---|
0 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
135
e2fe9352f240
upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
122
diff
changeset
|
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium |
0 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
47 | 8 * modify it under the terms of the GNU Affero General Public License |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
0 | 11 * |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
47 | 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
0 | 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 **/ | |
20 | |
21 | |
22 #include "IBasicApplication.h" | |
23 | |
102 | 24 #include "../Framework/Toolbox/MessagingToolbox.h" |
25 #include "Sdl/SdlEngine.h" | |
26 | |
116 | 27 #include <Core/Logging.h> |
28 #include <Core/HttpClient.h> | |
29 #include <Plugins/Samples/Common/OrthancHttpConnection.h> | |
0 | 30 |
31 namespace OrthancStone | |
32 { | |
33 // Anonymous namespace to avoid clashes against other compilation modules | |
34 namespace | |
35 { | |
36 class LogStatusBar : public IStatusBar | |
37 { | |
38 public: | |
39 virtual void ClearMessage() | |
40 { | |
41 } | |
42 | |
43 virtual void SetMessage(const std::string& message) | |
44 { | |
45 LOG(WARNING) << message; | |
46 } | |
47 }; | |
48 } | |
49 | |
50 | |
51 #if ORTHANC_ENABLE_SDL == 1 | |
52 static void DeclareSdlCommandLineOptions(boost::program_options::options_description& options) | |
53 { | |
54 // Declare the supported parameters | |
55 boost::program_options::options_description generic("Generic options"); | |
56 generic.add_options() | |
57 ("help", "Display this help and exit") | |
58 ("verbose", "Be verbose in logs") | |
59 ("orthanc", boost::program_options::value<std::string>()->default_value("http://localhost:8042/"), | |
60 "URL to the Orthanc server") | |
61 ("username", "Username for the Orthanc server") | |
62 ("password", "Password for the Orthanc server") | |
63 ("https-verify", boost::program_options::value<bool>()->default_value(true), "Check HTTPS certificates") | |
64 ; | |
65 | |
66 options.add(generic); | |
67 | |
68 boost::program_options::options_description sdl("SDL options"); | |
69 sdl.add_options() | |
70 ("width", boost::program_options::value<int>()->default_value(1024), "Initial width of the SDL window") | |
71 ("height", boost::program_options::value<int>()->default_value(768), "Initial height of the SDL window") | |
72 ("opengl", boost::program_options::value<bool>()->default_value(true), "Enable OpenGL in SDL") | |
73 ; | |
74 | |
75 options.add(sdl); | |
76 } | |
77 | |
78 | |
79 int IBasicApplication::ExecuteWithSdl(IBasicApplication& application, | |
80 int argc, | |
81 char* argv[]) | |
82 { | |
83 /****************************************************************** | |
84 * Initialize all the subcomponents of Orthanc Stone | |
85 ******************************************************************/ | |
86 | |
87 Orthanc::Logging::Initialize(); | |
88 Orthanc::HttpClient::InitializeOpenSsl(); | |
89 Orthanc::HttpClient::GlobalInitialize(); | |
90 SdlEngine::GlobalInitialize(); | |
91 | |
92 | |
93 /****************************************************************** | |
94 * Declare and parse the command-line options of the application | |
95 ******************************************************************/ | |
96 | |
97 boost::program_options::options_description options; | |
98 DeclareSdlCommandLineOptions(options); | |
99 application.DeclareCommandLineOptions(options); | |
100 | |
101 boost::program_options::variables_map parameters; | |
102 bool error = false; | |
103 | |
104 try | |
105 { | |
106 boost::program_options::store(boost::program_options::command_line_parser(argc, argv). | |
107 options(options).run(), parameters); | |
108 boost::program_options::notify(parameters); | |
109 } | |
110 catch (boost::program_options::error& e) | |
111 { | |
112 LOG(ERROR) << "Error while parsing the command-line arguments: " << e.what(); | |
113 error = true; | |
114 } | |
115 | |
116 | |
117 /****************************************************************** | |
118 * Configure the application with the command-line parameters | |
119 ******************************************************************/ | |
120 | |
121 if (error || parameters.count("help")) | |
122 { | |
123 std::cout << std::endl | |
124 << "Usage: " << argv[0] << " [OPTION]..." | |
125 << std::endl | |
126 << "Orthanc, lightweight, RESTful DICOM server for healthcare and medical research." | |
127 << std::endl << std::endl | |
128 << "Demonstration application of Orthanc Stone using SDL." | |
129 << std::endl; | |
130 | |
131 std::cout << options << "\n"; | |
132 return error ? -1 : 0; | |
133 } | |
134 | |
135 if (parameters.count("https-verify") && | |
136 !parameters["https-verify"].as<bool>()) | |
137 { | |
138 LOG(WARNING) << "Turning off verification of HTTPS certificates (unsafe)"; | |
139 Orthanc::HttpClient::ConfigureSsl(false, ""); | |
140 } | |
141 | |
142 if (parameters.count("verbose")) | |
143 { | |
144 Orthanc::Logging::EnableInfoLevel(true); | |
145 } | |
146 | |
147 if (!parameters.count("width") || | |
148 !parameters.count("height") || | |
149 !parameters.count("opengl")) | |
150 { | |
151 LOG(ERROR) << "Parameter \"width\", \"height\" or \"opengl\" is missing"; | |
152 return -1; | |
153 } | |
154 | |
155 int w = parameters["width"].as<int>(); | |
156 int h = parameters["height"].as<int>(); | |
157 if (w <= 0 || h <= 0) | |
158 { | |
159 LOG(ERROR) << "Parameters \"width\" and \"height\" must be positive"; | |
160 return -1; | |
161 } | |
162 | |
163 unsigned int width = static_cast<unsigned int>(w); | |
164 unsigned int height = static_cast<unsigned int>(h); | |
165 LOG(WARNING) << "Initial display size: " << width << "x" << height; | |
166 | |
167 bool opengl = parameters["opengl"].as<bool>(); | |
168 if (opengl) | |
169 { | |
170 LOG(WARNING) << "OpenGL is enabled, disable it with option \"--opengl=off\" if the application crashes"; | |
171 } | |
172 else | |
173 { | |
174 LOG(WARNING) << "OpenGL is disabled, enable it with option \"--opengl=on\" for best performance"; | |
175 } | |
176 | |
177 bool success = true; | |
178 try | |
179 { | |
180 /**************************************************************** | |
181 * Initialize the connection to the Orthanc server | |
182 ****************************************************************/ | |
183 | |
184 Orthanc::WebServiceParameters webService; | |
185 | |
186 if (parameters.count("orthanc")) | |
187 { | |
188 webService.SetUrl(parameters["orthanc"].as<std::string>()); | |
189 } | |
190 | |
191 if (parameters.count("username")) | |
192 { | |
193 webService.SetUsername(parameters["username"].as<std::string>()); | |
194 } | |
195 | |
196 if (parameters.count("password")) | |
197 { | |
198 webService.SetPassword(parameters["password"].as<std::string>()); | |
199 } | |
200 | |
201 LOG(WARNING) << "URL to the Orthanc REST API: " << webService.GetUrl(); | |
81
8677d95753f8
switching to Oracle in SDL samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
69
diff
changeset
|
202 |
8677d95753f8
switching to Oracle in SDL samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
69
diff
changeset
|
203 { |
82
cee8f308a4bc
Getting rid of Orthanc*WebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
81
diff
changeset
|
204 OrthancPlugins::OrthancHttpConnection orthanc(webService); |
cee8f308a4bc
Getting rid of Orthanc*WebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
81
diff
changeset
|
205 if (!MessagingToolbox::CheckOrthancVersion(orthanc)) |
81
8677d95753f8
switching to Oracle in SDL samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
69
diff
changeset
|
206 { |
122
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
116
diff
changeset
|
207 LOG(ERROR) << "Your version of Orthanc is incompatible with Stone of Orthanc, please upgrade"; |
81
8677d95753f8
switching to Oracle in SDL samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
69
diff
changeset
|
208 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol); |
8677d95753f8
switching to Oracle in SDL samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
69
diff
changeset
|
209 } |
0 | 210 } |
211 | |
212 | |
213 /**************************************************************** | |
214 * Initialize the application | |
215 ****************************************************************/ | |
216 | |
66 | 217 LOG(WARNING) << "Creating the widgets of the application"; |
218 | |
0 | 219 LogStatusBar statusBar; |
81
8677d95753f8
switching to Oracle in SDL samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
69
diff
changeset
|
220 BasicApplicationContext context(webService); |
0 | 221 |
222 application.Initialize(context, statusBar, parameters); | |
53
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
51
diff
changeset
|
223 |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
51
diff
changeset
|
224 { |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
51
diff
changeset
|
225 BasicApplicationContext::ViewportLocker locker(context); |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
51
diff
changeset
|
226 locker.GetViewport().SetStatusBar(statusBar); |
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
51
diff
changeset
|
227 } |
0 | 228 |
229 std::string title = application.GetTitle(); | |
230 if (title.empty()) | |
231 { | |
232 title = "Stone of Orthanc"; | |
233 } | |
234 | |
235 { | |
236 /************************************************************** | |
237 * Run the application inside a SDL window | |
238 **************************************************************/ | |
239 | |
240 LOG(WARNING) << "Starting the application"; | |
241 | |
242 SdlWindow window(title.c_str(), width, height, opengl); | |
53
c2dc924f1a63
removing threading out of the framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
51
diff
changeset
|
243 SdlEngine sdl(window, context); |
0 | 244 |
61
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
245 { |
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
246 BasicApplicationContext::ViewportLocker locker(context); |
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
247 locker.GetViewport().Register(sdl); // (*) |
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
248 } |
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
249 |
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
250 context.Start(); |
0 | 251 sdl.Run(); |
252 | |
253 LOG(WARNING) << "Stopping the application"; | |
61
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
254 |
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
255 // Don't move the "Stop()" command below out of the block, |
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
256 // otherwise the application might crash, because the |
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
257 // "SdlEngine" is an observer of the viewport (*) and the |
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
258 // update thread started by "context.Start()" would call a |
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
259 // destructed object (the "SdlEngine" is deleted with the |
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
260 // lexical scope). |
ca644004d2ee
MAJOR - removal of Start/Stop and observers in IWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
57
diff
changeset
|
261 context.Stop(); |
0 | 262 } |
263 | |
264 | |
265 /**************************************************************** | |
266 * Finalize the application | |
267 ****************************************************************/ | |
268 | |
269 LOG(WARNING) << "The application has stopped"; | |
270 application.Finalize(); | |
271 } | |
272 catch (Orthanc::OrthancException& e) | |
273 { | |
274 LOG(ERROR) << "EXCEPTION: " << e.What(); | |
275 success = false; | |
276 } | |
277 | |
278 | |
279 /****************************************************************** | |
280 * Finalize all the subcomponents of Orthanc Stone | |
281 ******************************************************************/ | |
282 | |
283 SdlEngine::GlobalFinalize(); | |
284 Orthanc::HttpClient::GlobalFinalize(); | |
285 Orthanc::HttpClient::FinalizeOpenSsl(); | |
286 | |
287 return (success ? 0 : -1); | |
288 } | |
289 #endif | |
290 | |
291 } |