/usr/share/pypibrowser/PyPIBrowser/actioneditor.py is in pypibrowser 1.5-2.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | #!/usr/bin/env python
"""
actioneditor.py
Copyright (C) 2006 David Boddie
This file is part of PyPI Browser, a GUI browser for the Python Package Index.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""
from PyQt4.QtCore import QEvent, QRect, QString, Qt, QVariant, SIGNAL
from PyQt4.QtGui import qApp, QBrush, QColor, QDialog, QHBoxLayout, \
QItemDelegate, QKeySequence, QLabel, QPainter, QPalette, QPen, \
QPushButton, QStyle, QTableWidget, QTableWidgetItem, QVBoxLayout
class ActionEditorWidget(QLabel):
# Redefine the tr() function for this class.
def tr(self, text):
return qApp.translate("ActionEditorWidget", text)
def __init__(self, text, parent):
QLabel.__init__(self, text, parent)
self.key = ""
self.modifiers = {}
self.setAutoFillBackground(True)
palette = self.palette()
palette.setBrush(palette.Base, palette.brush(palette.AlternateBase))
self.setPalette(palette)
self.valid = False
def keyPressEvent(self, event):
other = None
if event.key() == Qt.Key_Shift:
self.modifiers[Qt.Key_Shift] = u"Shift"
elif event.key() == Qt.Key_Control:
self.modifiers[Qt.Key_Control] = u"Ctrl"
elif event.key() == Qt.Key_Meta:
self.modifiers[Qt.Key_Meta] = u"Meta"
elif event.key() == Qt.Key_Alt:
self.modifiers[Qt.Key_Alt] = u"Alt"
else:
other = QString(QKeySequence(event.key()))
if other:
key_string = u"+".join(self.modifiers.values() + [unicode(other),])
self.valid = True
else:
key_string = u"+".join(self.modifiers.values())
self.setText(key_string)
def keyReleaseEvent(self, event):
if self.valid:
return
if event.key() == Qt.Key_Shift:
if self.modifiers.has_key(Qt.Key_Shift):
del self.modifiers[Qt.Key_Shift]
elif event.key() == Qt.Key_Control:
if self.modifiers.has_key(Qt.Key_Control):
del self.modifiers[Qt.Key_Control]
elif event.key() == Qt.Key_Meta:
if self.modifiers.has_key(Qt.Key_Meta):
del self.modifiers[Qt.Key_Meta]
elif event.key() == Qt.Key_Alt:
if self.modifiers.has_key(Qt.Key_Alt):
del self.modifiers[Qt.Key_Alt]
self.setText(u"+".join(self.modifiers.values()))
if len(self.modifiers) == 0:
self.releaseKeyboard()
def mousePressEvent(self, event):
if event.button() != Qt.LeftButton:
return
size = self.height() / 2.0
rect = QRect(self.width() - size, size * 0.5, size, size)
if rect.contains(event.pos()):
self.clear()
self.valid = True
event.accept()
def paintEvent(self, event):
if not self.text().isEmpty():
painter = QPainter()
painter.begin(self)
painter.setRenderHint(QPainter.Antialiasing)
color = self.palette().color(QPalette.Highlight)
color.setAlpha(127)
painter.setBrush(QBrush(color))
color = self.palette().color(QPalette.HighlightedText)
color.setAlpha(127)
painter.setPen(QPen(color))
size = self.height() / 2.0
painter.drawRect(self.width() - size, size * 0.5, size, size)
painter.drawLine(self.width() - size * 0.75, size * 0.75,
self.width() - size * 0.25, size * 1.25)
painter.drawLine(self.width() - size * 0.25, size * 0.75,
self.width() - size * 0.75, size * 1.25)
painter.end()
QLabel.paintEvent(self, event)
def showEvent(self, event):
self.grabKeyboard()
class ActionEditorDelegate(QItemDelegate):
def __init__(self, parent = None):
QItemDelegate.__init__(self, parent)
def createEditor(self, parent, option, index):
self.editor = ActionEditorWidget(index.data().toString(), parent)
self.editor.installEventFilter(self)
return self.editor
def eventFilter(self, obj, event):
if obj == self.editor:
if event.type() == QEvent.KeyPress:
obj.keyPressEvent(event)
if obj.valid:
self.emit(SIGNAL("commitData(QWidget *)"), self.editor)
self.emit(SIGNAL("closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint)"),
self.editor, QItemDelegate.NoHint)
return True
elif event.type() == QEvent.KeyRelease:
obj.keyReleaseEvent(event)
if obj.text().isEmpty():
self.emit(SIGNAL("closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint)"),
self.editor, QItemDelegate.NoHint)
return True
elif event.type() == QEvent.MouseButtonPress:
obj.mousePressEvent(event)
if obj.valid:
self.emit(SIGNAL("commitData(QWidget *)"), self.editor)
self.emit(SIGNAL("closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint)"),
self.editor, QItemDelegate.NoHint)
return True
return False
def paint(self, painter, option, index):
if index.column() != 0:
QItemDelegate.paint(self, painter, option, index)
return
painter.fillRect(option.rect, option.palette.brush(QPalette.Base))
painter.setPen(QPen(option.palette.color(QPalette.Text)))
painter.drawText(option.rect.adjusted(4, 4, -4, -4),
Qt.TextShowMnemonic | Qt.AlignLeft | Qt.AlignVCenter,
index.data().toString())
def setEditorData(self, editor, index):
editor.setText(index.data().toString())
def setModelData(self, editor, model, index):
model.setData(index, QVariant(editor.text()))
def updateEditorGeometry(self, editor, option, index):
editor.setGeometry(option.rect)
class ActionEditorDialog(QDialog):
# Redefine the tr() function for this class.
def tr(self, text):
return qApp.translate("ActionEditorDialog", text)
def __init__(self, actions, parent):
QDialog.__init__(self, parent)
self.actions = filter(lambda action: action.parent() == parent, actions)
self.actionTable = QTableWidget(self)
self.actionTable.setColumnCount(2)
self.actionTable.setHorizontalHeaderLabels(
[self.tr("Description"), self.tr("Shortcut")]
)
self.actionTable.horizontalHeader().setStretchLastSection(True)
self.actionTable.verticalHeader().hide()
self.actionTable.setItemDelegate(ActionEditorDelegate(self))
self.connect(self.actionTable, SIGNAL("cellChanged(int, int)"),
self.validateAction)
row = 0
for action in self.actions:
if action.text().isEmpty():
continue
self.actionTable.insertRow(self.actionTable.rowCount())
item = QTableWidgetItem()
item.setText(action.text())
item.setFlags(Qt.ItemIsEnabled)
self.actionTable.setItem(row, 0, item)
item = QTableWidgetItem()
item.setText(action.shortcut().toString())
item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsEditable)
item.oldShortcutText = item.text()
self.actionTable.setItem(row, 1, item)
row += 1
self.actionTable.resizeColumnsToContents()
ok_button = QPushButton(self.tr("&OK"))
cancel_button = QPushButton(self.tr("&Cancel"))
self.connect(ok_button, SIGNAL("clicked()"), self.accept)
self.connect(cancel_button, SIGNAL("clicked()"), self.reject)
button_layout = QHBoxLayout()
button_layout.setSpacing(8)
button_layout.addStretch(1)
button_layout.addWidget(ok_button)
button_layout.addWidget(cancel_button)
mainLayout = QVBoxLayout()
mainLayout.setMargin(8)
mainLayout.setSpacing(8)
mainLayout.addWidget(self.actionTable)
mainLayout.addLayout(button_layout)
self.setLayout(mainLayout)
self.setWindowTitle(self.tr("Edit Shortcuts"))
def accept(self):
row = 0
for action in self.actions:
if not action.text().isEmpty():
action.setText(self.actionTable.item(row, 0).text())
action.setShortcut(QKeySequence(self.actionTable.item(row, 1).text()))
row += 1
QDialog.accept(self)
def loadSettings(self, settings, actions):
settings.beginGroup("Actions")
for action in actions:
shortcutText = settings.value(action.text()).toString()
if not shortcutText.isEmpty():
action.setShortcut(QKeySequence(shortcutText))
settings.endGroup()
loadSettings = classmethod(loadSettings)
def saveSettings(self, settings, actions):
settings.beginGroup("Actions")
for action in actions:
shortcutText = action.shortcut().toString()
settings.setValue(action.text(), QVariant(shortcutText))
settings.endGroup()
saveSettings = classmethod(saveSettings)
def validateAction(self, row, column):
if column != 1:
return
item = self.actionTable.item(row, column)
shortcutText = QKeySequence(item.text()).toString()
thisRow = self.actionTable.row(item)
if not shortcutText.isEmpty():
for row in range(self.actionTable.rowCount()):
if row == thisRow:
continue
other = self.actionTable.item(row, 1)
if other.text() == shortcutText:
other.setText(item.oldShortcutText)
break
item.setText(shortcutText)
item.oldShortcutText = shortcutText
self.actionTable.resizeColumnToContents(1)
|