Move things round a bit.
[dcpomatic.git] / src / lib / screen.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <boost/algorithm/string.hpp>
21 #include "screen.h"
22 #include "format.h"
23 #include "exceptions.h"
24
25 using namespace std;
26 using namespace boost;
27
28 Screen::Screen (string n)
29         : _name (n)
30 {
31         vector<Format const *> f = Format::all ();
32         for (vector<Format const *>::iterator i = f.begin(); i != f.end(); ++i) {
33                 set_geometry (*i, Position (0, 0), Size (2048, 1080));
34         }
35 }
36
37 void
38 Screen::set_geometry (Format const * f, Position p, Size s)
39 {
40         _geometries[f] = Geometry (p, s);
41 }
42
43 Position
44 Screen::position (Format const * f) const
45 {
46         GeometryMap::const_iterator i = _geometries.find (f);
47         if (i == _geometries.end ()) {
48                 throw PlayError ("format not found for screen");
49         }
50
51         return i->second.position;
52 }
53
54 Size
55 Screen::size (Format const * f) const
56 {
57         GeometryMap::const_iterator i = _geometries.find (f);
58         if (i == _geometries.end ()) {
59                 throw PlayError ("format not found for screen");
60         }
61
62         return i->second.size;
63 }
64
65 string
66 Screen::as_metadata () const
67 {
68         stringstream s;
69         s << "\"" << _name << "\"";
70
71         for (GeometryMap::const_iterator i = _geometries.begin(); i != _geometries.end(); ++i) {
72                 s << " " << i->first->as_metadata()
73                   << " " << i->second.position.x << " " << i->second.position.y
74                   << " " << i->second.size.width << " " << i->second.size.height;
75         }
76
77         return s.str ();
78 }
79
80 shared_ptr<Screen>
81 Screen::create_from_metadata (string v)
82 {
83         vector<string> b = split_at_spaces_considering_quotes (v);
84
85         if (b.size() < 1) {
86                 return shared_ptr<Screen> ();
87         }
88
89         shared_ptr<Screen> s (new Screen (b[0]));
90
91         vector<string>::size_type i = 1;
92         while (b.size() > i) {
93                 if (b.size() >= (i + 5)) {
94                         s->set_geometry (
95                                 Format::from_metadata (b[i].c_str()),
96                                 Position (atoi (b[i+1].c_str()), atoi (b[i+2].c_str())),
97                                 Size (atoi (b[i+3].c_str()), atoi (b[i+4].c_str()))
98                                 );
99                 }
100                 i += 5;
101         }
102
103         return s;
104 }