Last active
July 5, 2017 14:17
-
-
Save JokerMartini/c4e22160dd2bd6dcfe6e to your computer and use it in GitHub Desktop.
Pyside Pythong: Demonstrates a popup dialog used to edit names and returns true/false along with value.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from PySide import QtGui, QtCore | |
from PySide.QtGui import QDialog, QVBoxLayout, QDialogButtonBox, QDateTimeEdit, QApplication | |
from PySide.QtCore import Qt, QDateTime | |
class RenameDialog(QDialog): | |
def __init__(self, parent = None): | |
super(RenameDialog, self).__init__(parent) | |
# formatting | |
self.setWindowTitle("Rename") | |
# validator | |
regexp = QtCore.QRegExp('[A-Za-z0-9_]+') | |
validator = QtGui.QRegExpValidator(regexp) | |
# widgets | |
self.nameInput = QtGui.QLineEdit() | |
self.nameInput.setValidator(validator) | |
# OK and Cancel buttons | |
self.buttons = QDialogButtonBox( QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self) | |
# signals | |
self.nameInput.textChanged.connect(self.check_input) | |
self.nameInput.textChanged.emit(self.nameInput.text()) | |
# layout | |
self.mainLayout = QtGui.QVBoxLayout(self) | |
self.mainLayout.addWidget(self.nameInput) | |
self.mainLayout.addWidget(self.buttons) | |
self.buttons.accepted.connect(self.accept) | |
self.buttons.rejected.connect(self.reject) | |
# self.buttons.setEnabled(False) | |
self.nameInput.setFocus() | |
def check_input(self, *args, **kwargs): | |
sender = self.sender() | |
validator = sender.validator() | |
state = validator.validate(sender.text(), 0)[0] | |
if state == QtGui.QValidator.Acceptable: | |
color = '#c4df9b' # green | |
elif state == QtGui.QValidator.Intermediate: | |
color = '#fff79a' # yellow | |
else: | |
color = '#f6989d' # red | |
sender.setStyleSheet('QLineEdit { background-color: %s }' % color) | |
# get current data from the dialog | |
def getNameInput(self): | |
return self.nameInput.text() | |
# static method to create the dialog and return (date, time, accepted) | |
@staticmethod | |
def run(parent = None): | |
dialog = RenameDialog(parent) | |
result = dialog.exec_() | |
newName = dialog.getNameInput() | |
return (newName, result == QDialog.Accepted) | |
app = QApplication([]) | |
name, ok = RenameDialog.run() | |
print("{} {}".format(name, ok)) | |
app.exec_() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from PySide import QtGui, QtCore | |
class RenameDialog(QtGui.QDialog): | |
def __init__(self, parent = None): | |
super(RenameDialog, self).__init__(parent) | |
# formatting | |
self.setWindowTitle("Rename") | |
# validator | |
regexp = QtCore.QRegExp('[A-Za-z0-9_]+') | |
validator = QtGui.QRegExpValidator(regexp) | |
# widgets | |
self.nameInput = QtGui.QLineEdit() | |
self.nameInput.setValidator(validator) | |
# OK and Cancel buttons | |
self.buttons = QtGui.QDialogButtonBox( QtGui.QDialogButtonBox.Ok, QtCore.Qt.Horizontal, self) | |
# signals | |
self.nameInput.textChanged.connect(self.check_input) | |
self.nameInput.textChanged.emit(self.nameInput.text()) | |
# layout | |
self.mainLayout = QtGui.QVBoxLayout(self) | |
self.mainLayout.addWidget(self.nameInput) | |
self.mainLayout.addWidget(self.buttons) | |
self.buttons.accepted.connect(self.accept) | |
self.buttons.rejected.connect(self.reject) | |
self.nameInput.setFocus() | |
def check_input(self, *args, **kwargs): | |
sender = self.sender() | |
validator = sender.validator() | |
state = validator.validate(sender.text(), 0)[0] | |
if state == QtGui.QValidator.Acceptable: | |
color = '#c4df9b' # green | |
self.buttons.setEnabled(True) | |
elif state == QtGui.QValidator.Intermediate: | |
color = '#fff79a' # yellow | |
self.buttons.setEnabled(False) | |
else: | |
color = '#f6989d' # red | |
self.buttons.setEnabled(False) | |
sender.setStyleSheet('QLineEdit { background-color: %s }' % color) | |
# get current data from the dialog | |
def getNameInput(self): | |
return self.nameInput.text() | |
# get current data from the dialog | |
def setNameInput(self, txt=None): | |
if txt != None: | |
self.nameInput.setText(txt) | |
self.nameInput.selectAll() | |
# static method to create the dialog and return (date, time, accepted) | |
@staticmethod | |
def run(parent = None, txt = None): | |
dialog = RenameDialog(parent) | |
dialog.setNameInput(txt) | |
result = dialog.exec_() | |
name = dialog.getNameInput() | |
return (result == QtGui.QDialog.Accepted, name ) | |
app = QtGui.QApplication([]) | |
ok, name = RenameDialog.run(None, "candy") | |
print("{} {}".format(ok, name)) | |
app.exec_() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# to do clean up redundant validation checks if possible | |
# Imports | |
# ------------------------------------------------------------------------------ | |
import json | |
import os | |
import sys | |
import re | |
import variables as var | |
from PySide import QtGui, QtCore | |
# Rename Widget | |
# ------------------------------------------------------------------------------ | |
class RenameDialog( QtGui.QDialog): | |
def __init__(self, parent = None): | |
super(RenameDialog, self).__init__(parent) | |
# list of names which currently exist | |
self.item_list = {} | |
# formatting | |
self.setWindowTitle("Rename") | |
# validator | |
regexp = QtCore.QRegExp('[A-Za-z0-9_]+') | |
validator = QtGui.QRegExpValidator(regexp) | |
# widgets | |
self.nameInput = QtGui.QLineEdit() | |
self.nameInput.setValidator(validator) | |
# OK and Cancel buttons | |
self.buttons = QtGui.QDialogButtonBox( QtGui.QDialogButtonBox.Ok, QtCore.Qt.Horizontal, self) | |
# signals | |
self.nameInput.textChanged.connect(self.check_input) | |
self.nameInput.textChanged.emit(self.nameInput.text()) | |
# layout | |
self.mainLayout = QtGui.QVBoxLayout(self) | |
self.mainLayout.addWidget(self.nameInput) | |
self.mainLayout.addWidget(self.buttons) | |
self.buttons.accepted.connect(self.accept) | |
self.buttons.rejected.connect(self.reject) | |
self.nameInput.setFocus() | |
def does_name_exist(self, nameslist, name): | |
for n in nameslist: | |
if (n.lower() == name.lower()): | |
return True | |
return False | |
def check_input(self, *args, **kwargs): | |
sender = self.sender() | |
validator = sender.validator() | |
state = validator.validate(sender.text(), 0)[0] | |
# make sure name is unique | |
if (self.does_name_exist(self.item_list, sender.text())): | |
state = QtGui.QValidator.Invalid | |
if state == QtGui.QValidator.Acceptable: | |
color = '#c4df9b' # green | |
self.buttons.setEnabled(True) | |
elif state == QtGui.QValidator.Intermediate: | |
color = '#fff79a' # yellow | |
self.buttons.setEnabled(False) | |
else: | |
color = '#f6989d' # red | |
self.buttons.setEnabled(False) | |
sender.setStyleSheet('QLineEdit { background-color: %s }' % color) | |
# get current data from the dialog | |
def getNameInput(self): | |
return self.nameInput.text() | |
# get current data from the dialog | |
def setNameInput(self, text="", item_list=[]): | |
self.nameInput.setText(text) | |
self.nameInput.selectAll() | |
# exclude name being changed from list to allow for case chaning | |
def setItemList(self, item_list={}, text=""): | |
self.item_list = item_list.copy() | |
self.item_list.pop(text, None) | |
# static method to create the dialog and return (date, time, accepted) | |
@staticmethod | |
def run(parent = None, text="", item_list={}): | |
dialog = RenameDialog(parent) | |
dialog.setNameInput(text) | |
dialog.setItemList(item_list, text) | |
result = dialog.exec_() | |
name = dialog.getNameInput() | |
return (result == QtGui.QDialog.Accepted, name ) | |
# Main Widget | |
# ------------------------------------------------------------------------------ | |
class FactionsWidget(QtGui.QWidget): | |
def __init__(self,): | |
super(FactionsWidget, self).__init__() | |
self.initUI() | |
def initUI(self): | |
# formatting | |
self.setGeometry(300, 300, 300, 300) | |
self.setWindowTitle("Factions") | |
# widgets | |
self.itemList = QtGui.QListWidget() | |
self.itemList.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) | |
self.itemList.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) | |
self.itemList.setSortingEnabled(True) | |
# signals | |
self.itemList.itemDoubleClicked.connect(self.doubleclicked_item) | |
# layout | |
self.mainLayout = QtGui.QGridLayout(self) | |
self.mainLayout.addWidget(self.itemList, 0, 0) | |
self.show() | |
self.refresh_list() | |
def doubleclicked_item(self): | |
# get selected faction | |
oldName = self.itemList.currentItem().text() | |
# open rename dialog | |
ok, newName = RenameDialog.run(None, oldName, var.ITEMLIST) | |
# rename faction if valid | |
if ok: | |
var.ITEMLIST = (self.rename_key(var.ITEMLIST, oldName, newName)) | |
self.refresh_list() | |
def refresh_list(self): | |
self.itemList.clear() | |
self.itemList.addItems(var.ITEMLIST.keys()) | |
def rename_key(self, iterable, oldkey, newKey): | |
if type(iterable) is dict: | |
for key in iterable.keys(): | |
if key == oldkey: | |
iterable[newKey] = iterable.pop(key) | |
return iterable | |
# Main | |
# ------------------------------------------------------------------------------ | |
if __name__ == "__main__": | |
app = QtGui.QApplication(sys.argv) | |
ex = FactionsWidget() | |
res = app.exec_() | |
sys.exit(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment