useful sorting of resolutions
[lilass.git] / question_frontend.py
1 # DSL - easy Display Setup for Laptops
2 # Copyright (C) 2012      Ralf Jung <post@ralfj.de>
3 # Copyright (C) 2012-2015 Constantin Berhard<constantin@exxxtremesys.lu>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 from screen import RelativeScreenPosition, ScreenSetup, processOutputIt
20
21 from enum import Enum
22
23 class OperationMode(Enum):
24     INTERNAL_ONLY = ("Use internal display only")
25     EXTERNAL_ONLY = ("Use external display only")
26     USE_BOTH      = ("Use both displays")
27     def __init__(self, text):
28         # auto numbering
29         cls = self.__class__
30         self._value_ = len(cls.__members__)
31         # description
32         self.text = text
33
34 class QuestionFrontend:
35     def userChoose (self, title, choices, returns, fallback):
36         raise Exception("The abstract method 'userChoose' has not been implemented by %s"%str(self.__class__))
37
38     def selectResolution(self, displayname, availablemodes):
39         modedescs = list(map(str, availablemodes))
40         return self.userChoose("Select resolution for %s"%displayname, modedescs, availablemodes, None)
41
42     def setup (self, situation):
43         operationmodes = list(OperationMode)
44         operationmodedescs = list(map(lambda x: x.text, operationmodes))
45         operationmode = self.userChoose ("Display setup", operationmodedescs, operationmodes, None)
46         if operationmode is None:
47             return None
48         elif operationmode is OperationMode.INTERNAL_ONLY:
49             intres = self.selectResolution("the internal screen", situation.internalResolutions())
50             if intres is None:
51                 return None
52             else:
53                 return ScreenSetup(intres, None, None, False)
54         elif operationmode is OperationMode.EXTERNAL_ONLY:
55             extres = self.selectResolution("the external screen", situation.externalResolutions())
56             if extres is None:
57                 return None
58             else:
59                 return ScreenSetup(None, extres, None, True)
60         else:
61             assert operationmode is OperationMode.USE_BOTH
62             relscrpositions = list(RelativeScreenPosition)
63             relscrdescs = list(map(lambda x: x.text+" internal screen", relscrpositions))
64             relpos = self.userChoose ("Position of external screen", relscrdescs, relscrpositions, None)
65             if relpos == None:
66                 return None
67             elif relpos == RelativeScreenPosition.MIRROR:
68                 # for mirroring only ask for common resolutions
69                 commonres = self.selectResolution("both screens", situation.commonResolutions())
70                 if commonres is None:
71                     return None
72                 return ScreenSetup(commonres,commonres,relpos,False)
73             # select resolutions independently
74             intres = self.selectResolution("the internal screen", situation.internalResolutions())
75             if intres is None:
76                 return None
77             extres = self.selectResolution("the external screen", situation.externalResolutions())
78             if extres is None:
79                 return None
80             extprim = self.userChoose("Select primary screen", ["Internal screen is primary","External screen is primary"], [False,True], None)
81             if extprim is None:
82                 return None
83             return ScreenSetup(intres,extres,relpos,extprim)