879a09aaf46b904b1bb7b6b678d4a8e86268552a
[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; 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, ScreenSetup, res2user
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, internalResolutions, externalResolutions):
30         # set up main window
31         super(PositionSelection, self).__init__()
32         self.setWindowTitle('DSL - easy Display Setup for Laptops')
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 = externalResolutions
55         self.extResolutionsBox = QtGui.QComboBox(resBox)
56         for res in externalResolutions:
57             self.extResolutionsBox.addItem(res2user(res))
58         self.extResolutionsBox.setCurrentIndex(0) # select first resolution
59         extRow = makeLayout(QtGui.QHBoxLayout(), [extResLabel, self.extResolutionsBox])
60         intResLabel = QtGui.QLabel('Resolution of internal screen:', resBox)
61         self.extOnly.toggled.connect(intResLabel.setDisabled) # disable the label if there's just one screen in use
62         self.intResolutions = internalResolutions
63         self.intResolutionsBox = QtGui.QComboBox(resBox)
64         for res in internalResolutions:
65             self.intResolutionsBox.addItem(res2user(res))
66         self.intResolutionsBox.setCurrentIndex(0) # select first resolution
67         self.extOnly.toggled.connect(self.intResolutionsBox.setDisabled) # disable the box if there's just one screen in use
68         intRow = makeLayout(QtGui.QHBoxLayout(), [intResLabel, self.intResolutionsBox])
69         resBox.setLayout(makeLayout(QtGui.QVBoxLayout(), [extRow, intRow]))
70         
71         # last row: buttons
72         buttons = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Cancel, QtCore.Qt.Horizontal, self)
73         buttons.accepted.connect(self.accept)
74         buttons.rejected.connect(self.reject)
75         
76         # add them all to the window
77         self.setLayout(makeLayout(QtGui.QVBoxLayout(), [posBox, primBox, resBox, buttons]))
78     
79     def run(self):
80         self.exec_()
81         if not self.result(): return None
82         return ScreenSetup(self.getRelativeScreenPosition(),
83             self.intResolutions[self.intResolutionsBox.currentIndex()],
84             self.extResolutions[self.extResolutionsBox.currentIndex()],
85             self.primExt.isChecked())
86     
87     def getRelativeScreenPosition(self):
88         if self.posLeft.isChecked():
89             return RelativeScreenPosition.LEFT
90         elif self.posRight.isChecked():
91             return RelativeScreenPosition.RIGHT
92         else:
93             return RelativeScreenPosition.EXTERNAL_ONLY