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
21 # Be fine with PyQt4 not being installed
22 from PyQt4 import QtCore, QtGui, uic
24 class PositionSelection(QtGui.QDialog):
25 def __init__(self, situation):
27 super(PositionSelection, self).__init__()
28 self._situation = situation
29 uifile = os.path.join(os.path.dirname(__file__), 'qt_dialogue.ui')
30 uic.loadUi(uifile, self)
32 # fill relative position box
33 for pos in RelativeScreenPosition:
34 self.relPos.addItem(pos.text, pos)
36 # keep resolutions in sync when in mirror mode
37 def syncIfMirror(source, target):
40 target.setCurrentIndex(idx)
41 source.currentIndexChanged.connect(_slot)
42 syncIfMirror(self.intRes, self.extRes)
43 syncIfMirror(self.extRes, self.intRes)
45 # connect the update function, and make sure we are in a correct state
46 self.intEnabled.toggled.connect(self.updateEnabledControls)
47 self.extEnabled.toggled.connect(self.updateEnabledControls)
48 self.relPos.currentIndexChanged.connect(self.updateEnabledControls)
49 self.updateEnabledControls()
51 def getRelativeScreenPosition(self):
52 idx = self.relPos.currentIndex()
53 return self.relPos.itemData(idx)
55 def fillResolutionBox(self, box, resolutions):
56 # if the count did not change, update in-place (this avoids flicker)
57 if box.count() == len(resolutions):
58 for idx, res in enumerate(resolutions):
59 box.setItemText(idx, str(res))
60 box.setItemData(idx, res)
63 while box.count() > 0:
66 for res in resolutions:
67 box.addItem(str(res), res)
69 def updateEnabledControls(self):
70 intEnabled = self.intEnabled.isChecked()
71 extEnabled = self.extEnabled.isChecked()
72 bothEnabled = intEnabled and extEnabled
73 self.isMirror = bothEnabled and self.getRelativeScreenPosition() == RelativeScreenPosition.MIRROR # only if both are enabled, we can really mirror
74 # configure screen controls
75 self.intRes.setEnabled(intEnabled)
76 self.intPrimary.setEnabled(intEnabled and not self.isMirror)
77 self.extRes.setEnabled(extEnabled)
78 self.extPrimary.setEnabled(extEnabled and not self.isMirror)
79 if not intEnabled and extEnabled:
80 self.extPrimary.setChecked(True)
81 elif not extEnabled and intEnabled:
82 self.intPrimary.setChecked(True)
83 # which resolutions do we offer?
85 commonRes = self._situation.commonResolutions()
86 self.fillResolutionBox(self.intRes, commonRes)
87 self.fillResolutionBox(self.extRes, commonRes)
88 self.intRes.setCurrentIndex(self.extRes.currentIndex())
90 self.fillResolutionBox(self.intRes, self._situation.internalResolutions())
91 self.fillResolutionBox(self.extRes, self._situation.externalResolutions())
92 # configure position control
93 self.posGroup.setEnabled(bothEnabled)
94 self.posLabel1.setEnabled(bothEnabled)
95 self.posLabel2.setEnabled(bothEnabled)
96 self.relPos.setEnabled(bothEnabled)
97 # avoid having no screen
98 self.buttonBox.button(QtGui.QDialogButtonBox.Ok).setEnabled(intEnabled or extEnabled)
102 if not self.result(): return None
103 intRes = self.intRes.itemData(self.intRes.currentIndex()) if self.intEnabled.isChecked() else None
104 extRes = self.extRes.itemData(self.extRes.currentIndex()) if self.extEnabled.isChecked() else None
105 return ScreenSetup(intRes, extRes, self.getRelativeScreenPosition(), self.extPrimary.isChecked())
112 from PyQt4 import QtGui
113 self.app = QtGui.QApplication(sys.argv)
116 def error(self, message):
117 from PyQt4 import QtGui
118 QtGui.QMessageBox.critical(None, 'Fatal error', message)
120 def setup(self, situation):
121 return PositionSelection(situation).run()