4ebcd62dfb309150221d427d93de9dc3b73a026c
[lilass.git] / qt_dialogue.py
1 # DSL - easy Display Setup for Laptops
2 # Copyright (C) 2012 Ralf Jung <post@ralfj.de>
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 (gpl.txt); if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 from dsl import RelativeScreenPosition
18 from PyQt4 import QtCore, QtGui
19
20 def makeLayout(layout, members):
21         for m in members:
22                 if isinstance(m, QtGui.QLayout):
23                         layout.addLayout(m)
24                 else:
25                         layout.addWidget(m)
26         return layout
27
28 class PositionSelection(QtGui.QDialog):
29         def __init__(self, externalName, internalResolutions, externalResolutions):
30                 # set up main window
31                 super(PositionSelection, self).__init__()
32                 self.setWindowTitle('External screen setup (connector: %s)' % externalName)
33                 
34                 # position selection
35                 posBox = QtGui.QGroupBox('Position of external screen', self)
36                 self.posLeft = QtGui.QRadioButton('Left of internal screen', posBox)
37                 self.posRight = QtGui.QRadioButton('Right of internal screen', posBox)
38                 self.posRight.setChecked(True)
39                 self.posRight.setFocus()
40                 self.extOnly = QtGui.QRadioButton('Use external screen exclusively', posBox)
41                 posBox.setLayout(makeLayout(QtGui.QVBoxLayout(), [self.posLeft, self.posRight, self.extOnly]))
42                 
43                 # primary screen
44                 primBox = QtGui.QGroupBox('Which should be the primary screen?', self)
45                 self.extOnly.toggled.connect(primBox.setDisabled) # disable the box if there's just one screen in use
46                 self.primExt = QtGui.QRadioButton('The external screen', primBox)
47                 self.primInt = QtGui.QRadioButton('The internal screen', primBox)
48                 self.primInt.setChecked(True)
49                 primBox.setLayout(makeLayout(QtGui.QVBoxLayout(), [self.primExt, self.primInt]))
50                 
51                 # resolution selection
52                 resBox = QtGui.QGroupBox('Screen resolutions', self)
53                 extResLabel = QtGui.QLabel('Resolution of external screen:', resBox)
54                 self.extResolutions = QtGui.QComboBox(resBox)
55                 for res in externalResolutions:
56                         self.extResolutions.addItem(res)
57                 self.extResolutions.setCurrentIndex(0) # select first resolution
58                 extRow = makeLayout(QtGui.QHBoxLayout(), [extResLabel, self.extResolutions])
59                 intResLabel = QtGui.QLabel('Resolution of internal screen:', resBox)
60                 self.extOnly.toggled.connect(intResLabel.setDisabled) # disable the label if there's just one screen in use
61                 self.intResolutions = QtGui.QComboBox(resBox)
62                 for res in internalResolutions:
63                         self.intResolutions.addItem(res)
64                 self.intResolutions.setCurrentIndex(0) # select first resolution
65                 self.extOnly.toggled.connect(self.intResolutions.setDisabled) # disable the box if there's just one screen in use
66                 intRow = makeLayout(QtGui.QHBoxLayout(), [intResLabel, self.intResolutions])
67                 resBox.setLayout(makeLayout(QtGui.QVBoxLayout(), [extRow, intRow]))
68                 
69                 # last row: buttons
70                 buttons = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel, QtCore.Qt.Horizontal, self)
71                 buttons.accepted.connect(self.accept)
72                 buttons.rejected.connect(self.reject)
73                 
74                 # add them all to the window
75                 self.setLayout(makeLayout(QtGui.QVBoxLayout(), [posBox, primBox, resBox, buttons]))
76         
77         def run(self):
78                 self.exec_()
79                 return True if self.result() else False
80         
81         def getRelativeScreenPosition(self):
82                 if self.posLeft.isChecked():
83                         return RelativeScreenPosition.LEFT
84                 elif self.posRight.isChecked():
85                         return RelativeScreenPosition.RIGHT
86                 else:
87                         return RelativeScreenPosition.EXTERNAL_ONLY
88         
89         def getIntResolutionIndex(self):
90                 return self.intResolutions.currentIndex()
91         
92         def getExtResolutionIndex(self):
93                 return self.extResolutions.currentIndex()
94         
95         def externalIsPrimary(self):
96                 return self.primExt.isChecked()