""" qtgui/MouseBindings.py: CCP4MG Molecular Graphics Program Copyright (C) 2001-2008 University of York, CCLRC Copyright (C) 2009-2011 University of York This library is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 3, modified in accordance with the provisions of the license to address the requirements of UK law. You should have received a copy of the modified GNU Lesser General Public License along with this library. If not, copies may be downloaded from http://www.ccp4.ac.uk/ccp4license.php This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. """ from PyQt4 import QtCore, QtGui, QtOpenGL from global_definitions import * MODIFIERS = ['shift'] ORDER = ['rotate-xy','rotate-z','zoom','translate-xy','translate-z', 'object-rotate-xy','object-rotate-z','object-translate-xy', 'object-translate-z','clip-width'] DEFAULT= {'rotate-xy' : dict(mouse_button='left',shift=0,alt=0,ctrl=0,key=' '), 'rotate-z' : dict(mouse_button='left',shift=1,alt=0,ctrl=0,key=' '), 'zoom' : dict(mouse_button='middle',shift=0,alt=0,ctrl=0,key=' '), 'translate-xy' : dict(mouse_button='middle',shift=1,alt=0,ctrl=0,key=' '), 'translate-z' : dict(mouse_button='middle',shift=0,alt=0,ctrl=0,key='z'), 'object-rotate-xy' : dict(mouse_button='left',shift=0,alt=0,ctrl=0,key='x'), 'object-rotate-z' : dict(mouse_button='left',shift=1,alt=0,ctrl=0,key='x'), 'object-translate-xy' : dict(mouse_button='middle',shift=0,alt=0,ctrl=0,key='x'), 'object-translate-z' : dict(mouse_button='middle',shift=1,alt=0,ctrl=0,key='x'), 'clip-width' : dict(mouse_button='middle',shift=0,alt=0,ctrl=0,key='c') } # This seems not to work when I request it in MGGLWidget.py COOT = {'rotate-xy' : dict(mouse_button='left',shift=0,alt=0,ctrl=0,key=' '), 'rotate-z' : dict(mouse_button='left',shift=1,alt=0,ctrl=0,key=' '), 'zoom' : dict(mouse_button='right',shift=0,alt=0,ctrl=0,key=' '), 'translate-xy' : dict(mouse_button='left',shift=0,alt=0,ctrl=1,key=' '), 'translate-z' : dict(mouse_button='right',shift=0,alt=0,ctrl=1,key=' '), 'object-rotate-xy' : dict(mouse_button='left',shift=0,alt=0,ctrl=0,key='x'), 'object-rotate-z' : dict(mouse_button='left',shift=1,alt=0,ctrl=0,key='x'), 'object-translate-xy' : dict(mouse_button='middle',shift=0,alt=0,ctrl=0,key='x'), 'object-translate-z' : dict(mouse_button='middle',shift=1,alt=0,ctrl=0,key='x'), 'clip-width' : dict(mouse_button='middle',shift=0,alt=0,ctrl=0,key='c') } #------------------------------------------------------------------- #------------------------------------------------------------------- #------------------------------------------------------------------- class mouseBinding(QtGui.QWidget): #------------------------------------------------------------------- #------------------------------------------------------------------- #------------------------------------------------------------------- #keys_menu = [' ','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] # Some keys have bindings so just give limited menu keys_menu = [' ','b','c','d','e','f','g','h','i','j','k','l','r','s','t','u','v','w','x','y','z'] mouse_button_menu = ['left','middle','right'] mac_mouse_button_menu = ['button','Alt-button'] def __init__(self,parent,name='',label='',layout=None,row=0): QtGui.QWidget.__init__(self,parent) self.setObjectName(name) #layout = QtGui.QGridLayout() # Beware the widgets are in the parent objects grid layout # so they are not children of this object if not label: label = name widget = QtGui.QLabel(label) layout.addWidget(widget,row,0) widget = QtGui.QComboBox(self) widget.setObjectName(name+'mouse_button') self.mouse_button = widget for but in mouseBinding.mouse_button_menu: widget.addItem(but) layout.addWidget(widget,row,1) icol = 2 for modif in MODIFIERS: widget = QtGui.QCheckBox(self) widget.setObjectName(name+modif) layout.addWidget(widget,row,icol) icol = icol+1 self.shift_check = widget widget = QtGui.QComboBox(self) widget.setObjectName(name+'key') widget.addItems(QtCore.QStringList(mouseBinding.keys_menu)) layout.addWidget(widget,row,icol) self.key_combo = widget def setParams(self,params,**kw): params.update(kw) for key,value in params.items(): if key == 'key': if mouseBinding.keys_menu.count(value): self.key_combo.setCurrentIndex(mouseBinding.keys_menu.index(value)) elif key == 'mouse_button': if mouseBinding.mouse_button_menu.count(value): self.mouse_button.setCurrentIndex(mouseBinding.mouse_button_menu.index(value)) elif key == 'shift': self.shift_check.setChecked(value) def getParams(self): params = {} #print "mouseBinding.getParams",self.objectName(),self.findChildren(QtGui.QWidget) for item in MODIFIERS: params[item] = int(self.shift_check.isChecked()) idx = self.key_combo.currentIndex() params['key'] = mouseBinding.keys_menu[idx] idx = self.mouse_button.currentIndex() params['mouse_button'] = mouseBinding.mouse_button_menu[idx] return params #------------------------------------------------------------------- #------------------------------------------------------------------- #------------------------------------------------------------------- class MouseBindings(QtGui.QWidget): #------------------------------------------------------------------- #------------------------------------------------------------------- #------------------------------------------------------------------- def __init__(self,parent,params={}): QtGui.QWidget.__init__(self,parent) print "MouseBindings",params #self.setAttribute(QtCore.Qt.WA_DeleteOnClose) d_layout = QtGui.QVBoxLayout() gridWidget = QtGui.QWidget() self.grid = QtGui.QGridLayout() gridWidget.setLayout(self.grid) font = QtGui.QFont(self.font()) font.setItalic(1) label = QtGui.QLabel('Mouse button') label.setFont(font) self.grid.addWidget(label,0,1) icol = 2 for modif in MODIFIERS: label = QtGui.QLabel(modif) label.setFont(font) self.grid.addWidget(label,0,icol) icol = icol+1 label = QtGui.QLabel('key') label.setFont(font) self.grid.addWidget(label,0,icol) row = 0 for name in ORDER: if params.has_key(name): row = row +1 widget = mouseBinding(self,name=name,layout=self.grid,row=row) print "Setting params for",name,"to",params.get(name) widget.setParams(params.get(name)) line_layout = QtGui.QHBoxLayout() self.coot = QtGui.QCheckBox('Use Coot-like key mouse bondings',self) self.coot.setToolTip('Set mouse bindings similar to Coot - beware not fully compatible') line_layout.addWidget(self.coot) line_layout.addStretch(5) def setCoot(state): print state,state==QtCore.Qt.Checked if state == QtCore.Qt.Checked: gridWidget.setEnabled(False) else: gridWidget.setEnabled(True) self.connect(self.coot,QtCore.SIGNAL('stateChanged(int)'),setCoot) if params.has_key("coot_bindings"): value = params["coot_bindings"] val = value["coot_bindings"] if val > 0: self.coot.setCheckState(QtCore.Qt.Checked) else: self.coot.setCheckState(QtCore.Qt.Unchecked) d_layout.addLayout(line_layout) d_layout.addWidget(gridWidget) self.setLayout(d_layout) self.show def getParams(self): params = {} for mb in self.findChildren(mouseBinding): params[str(mb.objectName())] = mb.getParams() params["coot_bindings"] = {"coot_bindings":self.coot.isChecked(),"mouse_button":'middle',"shift":1,"alt":1,"ctrl":1,"key":'^'} print "getParams",params return params def setParams(self,params,**kw): params.update(kw) print "setPrams",params for name,value in params.items(): widget = self.findChild(mouseBinding,name) print widget if widget: widget.setParams(value) if name == "coot_bindings": val = value["coot_bindings"] if val > 0: self.coot.setCheckState(QtCore.Qt.Checked) else: self.coot.setCheckState(QtCore.Qt.Unchecked) def help(self): HELPBROWSER().loadMgHelp(file='graphics_window.html') def apply(self): params = self.getParams() # test that each binding is unique keys = params.keys() same_list = [] for i in range(0,len(keys)-1): for j in range(i+1,len(keys)): if params[keys[i]] == params[keys[j]]: same_list.append([keys[i],keys[j]]) if same_list and not self.coot.isChecked(): message = 'The following actions have the same bindings' for a1,a2 in same_list: message = message + '\n'+a1+' and '+a2 message = message + '\nPlease correct and Apply again' QtGui.QMessageBox.warning(self,'Error in mouse bindings', message) else: MAINWINDOW().setMouseBindings(self.getParams()) def resetDefaults(self): self.setParams(DEFAULT)