Merge pull request #4 from ConnyOnny/master
[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         if situation.previousSetup:
44             applyPrevious = self.userChoose("This display is known. The last setup for it was like this:\n%s.\nApply the last used configuration?" % str(situation.previousSetup), ("Apply last setup", "Enter different setup"), (True,False), None)
45             if applyPrevious is None:
46                 return None
47             if applyPrevious is True:
48                 return situation.previousSetup
49             assert applyPrevious is False
50         operationmodes = list(OperationMode)
51         operationmodedescs = list(map(lambda x: x.text, operationmodes))
52         operationmode = self.userChoose ("Display setup", operationmodedescs, operationmodes, None)
53         if operationmode is None:
54             return None
55         elif operationmode is OperationMode.INTERNAL_ONLY:
56             intres = self.selectResolution("the internal screen", situation.internalConnector.getResolutionList())
57             if intres is None:
58                 return None
59             else:
60                 return ScreenSetup(intres, None, None, False)
61         elif operationmode is OperationMode.EXTERNAL_ONLY:
62             extres = self.selectResolution("the external screen", situation.externalConnector.getResolutionList())
63             if extres is None:
64                 return None
65             else:
66                 return ScreenSetup(None, extres, None, True)
67         else:
68             assert operationmode is OperationMode.USE_BOTH
69             relscrpositions = list(RelativeScreenPosition)
70             relscrdescs = list(map(lambda x: x.text+" internal screen", relscrpositions))
71             relpos = self.userChoose ("Position of external screen", relscrdescs, relscrpositions, None)
72             if relpos == None:
73                 return None
74             elif relpos == RelativeScreenPosition.MIRROR:
75                 # for mirroring only ask for common resolutions
76                 commonres = self.selectResolution("both screens", situation.commonResolutions())
77                 if commonres is None:
78                     return None
79                 return ScreenSetup(commonres,commonres,relpos,False)
80             # select resolutions independently
81             intres = self.selectResolution("the internal screen", situation.internalConnector.getResolutionList())
82             if intres is None:
83                 return None
84             extres = self.selectResolution("the external screen", situation.externalConnector.getResolutionList())
85             if extres is None:
86                 return None
87             extprim = self.userChoose("Select primary screen", ["Internal screen is primary","External screen is primary"], [False,True], None)
88             if extprim is None:
89                 return None
90             return ScreenSetup(intres,extres,relpos,extprim)