That worked like a charm. I’ll share what I got.
So instead of doing a normal clicked.connect I created a Connect class.
Now when I Right-Click on a button I get a menu of choice of editor to open to that line number.
Super Awesome…at least I think so
Before:
self.button.clicked.connect(self.my_command)
After:
Connect(self.buttonObject, self.my_command)
import inspect
import subprocess
from distutils.spawn import find_executable
class RightClickMenu(QtGui.QMenu):
def __init__(self, *args, **kwargs):
super(RightClickMenu, self).__init__(*args)
# Prepare the parent widget for using the right-click menu
self.parentWidget().setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.parentWidget().customContextMenuRequested.connect(self.showMenu)
def showMenu(self, *args):
self.exec_(QtGui.QCursor.pos())
class Connect(object):
def __init__(self, obj, command, *args, **kwargs):
super(Connect, self).__init__(*args)
self.command = command
self.obj = obj
# Setup the signal
self.obj.clicked.connect(self.command)
menu = RightClickMenu(self.obj)
menu.addAction('Edit cmd (notepad++)', lambda *args: self.editCommand("notepad"))
menu.addAction('Edit cmd (sublime)', lambda *args: self.editCommand("sublime"))
menu.addAction('Print cmd', self.printCommand)
self.fileLocation = inspect.getsourcefile(self.command)
self.lineNumber = inspect.getsourcelines(self.command)[1]
self.sourceCode = inspect.getsourcelines(self.command)[0]
def editCommand(self, editor):
""" Open the file using one of the text editors """
sublime = find_executable("C:\\Program Files\\Sublime Text 2\\sublime_text.exe")
notepad = find_executable("C:\\Program Files (x86)\\Notepad++\
otepad++.exe")
if editor == "notepad":
if notepad:
subprocess.Popen([notepad, self.fileLocation, "-n{}".format(self.lineNumber)])
if editor == "sublime":
if sublime:
subprocess.Popen([sublime, self.fileLocation + ":{}".format(self.lineNumber)])
def printCommand(self):
""" output function """
def chomp(s):
if s.endswith('
'):
return s[:-1]
else:
return s
for code in self.sourceCode:
code = chomp(code)
print code