1 # DSL - easy Display Setup for Laptops
2 # Copyright (C) 2012-2015 Ralf Jung <post@ralfj.de>
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.
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.
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.
18 from screen import RelativeScreenPosition, ScreenSetup
19 from PyQt4 import QtCore, QtGui, uic
21 class PositionSelection(QtGui.QDialog):
22 def __init__(self, situation):
24 super(PositionSelection, self).__init__()
25 self._situation = situation
26 uifile = os.path.join(os.path.dirname(__file__), 'qt_dialogue.ui')
27 uic.loadUi(uifile, self)
29 # fill relative position box
30 for pos in RelativeScreenPosition:
31 self.relPos.addItem(pos.text, pos)
33 # keep resolutions in sync when in mirror mode
34 def syncIfMirror(source, target):
37 target.setCurrentIndex(idx)
38 source.currentIndexChanged.connect(_slot)
39 syncIfMirror(self.intRes, self.extRes)
40 syncIfMirror(self.extRes, self.intRes)
42 # connect the update function, and make sure we are in a correct state
43 self.intEnabled.toggled.connect(self.updateEnabledControls)
44 self.extEnabled.toggled.connect(self.updateEnabledControls)
45 self.relPos.currentIndexChanged.connect(self.updateEnabledControls)
46 self.updateEnabledControls()
48 def getRelativeScreenPosition(self):
49 idx = self.relPos.currentIndex()
50 return self.relPos.itemData(idx)
52 def fillResolutionBox(self, box, resolutions):
53 # if the count did not change, update in-place (this avoids flicker)
54 if box.count() == len(resolutions):
55 for idx, res in enumerate(resolutions):
56 box.setItemText(idx, str(res))
57 box.setItemData(idx, res)
60 while box.count() > 0:
63 for res in resolutions:
64 box.addItem(str(res), res)
66 def updateEnabledControls(self):
67 intEnabled = self.intEnabled.isChecked()
68 extEnabled = self.extEnabled.isChecked()
69 bothEnabled = intEnabled and extEnabled
70 self.isMirror = bothEnabled and self.getRelativeScreenPosition() == RelativeScreenPosition.MIRROR # only if both are enabled, we can really mirror
71 # configure screen controls
72 self.intRes.setEnabled(intEnabled)
73 self.intPrimary.setEnabled(intEnabled and not self.isMirror)
74 self.extRes.setEnabled(extEnabled)
75 self.extPrimary.setEnabled(extEnabled and not self.isMirror)
76 if not intEnabled and extEnabled:
77 self.extPrimary.setChecked(True)
78 elif not extEnabled and intEnabled:
79 self.intPrimary.setChecked(True)
80 # which resolutions do we offer?
82 commonRes = self._situation.commonResolutions()
83 self.fillResolutionBox(self.intRes, commonRes)
84 self.fillResolutionBox(self.extRes, commonRes)
85 self.intRes.setCurrentIndex(self.extRes.currentIndex())
87 self.fillResolutionBox(self.intRes, self._situation.internalResolutions())
88 self.fillResolutionBox(self.extRes, self._situation.externalResolutions())
89 # configure position control
90 self.posGroup.setEnabled(bothEnabled)
91 self.posLabel1.setEnabled(bothEnabled)
92 self.posLabel2.setEnabled(bothEnabled)
93 self.relPos.setEnabled(bothEnabled)
94 # avoid having no screen
95 self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setEnabled(intEnabled or extEnabled)
99 if not self.result(): return None
100 intRes = self.intRes.itemData(self.intRes.currentIndex()) if self.intEnabled.isChecked() else None
101 extRes = self.extRes.itemData(self.extRes.currentIndex()) if self.extEnabled.isChecked() else None
102 return ScreenSetup(intRes, extRes, self.getRelativeScreenPosition(), self.extPrimary.isChecked())