Skip to content

Commit

Permalink
Merge pull request #448 from davidlatwe/Fix#289
Browse files Browse the repository at this point in the history
Fix#289: Validate subset name for database
  • Loading branch information
davidlatwe authored Sep 17, 2019
2 parents abdb1ff + 4645704 commit 2bdcb79
Showing 1 changed file with 99 additions and 9 deletions.
108 changes: 99 additions & 9 deletions avalon/tools/creator/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import inspect
import re

from ...vendor.Qt import QtWidgets, QtCore, QtGui
from ...vendor import qtawesome
Expand All @@ -20,6 +21,97 @@
Separator = "---separator---"


class SubsetNameValidator(QtGui.QRegExpValidator):

invalid = QtCore.Signal(set)
pattern = "^[a-zA-Z0-9_.]*$"

def __init__(self):
reg = QtCore.QRegExp(self.pattern)
super(SubsetNameValidator, self).__init__(reg)

def validate(self, input, pos):
results = super(SubsetNameValidator, self).validate(input, pos)
if results[0] == self.Invalid:
self.invalid.emit(self.invalid_chars(input))
return results

def invalid_chars(self, input):
invalid = set()
re_valid = re.compile(self.pattern)
for char in input:
if char == " ":
invalid.add("' '")
continue
if not re_valid.match(char):
invalid.add(char)
return invalid


class SubsetNameLineEdit(QtWidgets.QLineEdit):

report = QtCore.Signal(str)
colors = {
"empty": (QtGui.QColor("#78879b"), ""),
"exists": (QtGui.QColor("#4E76BB"), "border-color: #4E76BB;"),
"new": (QtGui.QColor("#7AAB8F"), "border-color: #7AAB8F;"),
}

def __init__(self, *args, **kwargs):
super(SubsetNameLineEdit, self).__init__(*args, **kwargs)

validator = SubsetNameValidator()
self.setValidator(validator)
self.setToolTip("Only alphanumeric characters (A-Z a-z 0-9), "
"'_' and '.' are allowed.")

self._status_color = None

anim = QtCore.QPropertyAnimation()
anim.setTargetObject(self)
anim.setPropertyName("status_color")
anim.setEasingCurve(QtCore.QEasingCurve.InCubic)
anim.setDuration(300)
anim.setStartValue(QtGui.QColor("#C84747")) # `Invalid` status color
self.animation = anim

validator.invalid.connect(self.on_invalid)

def on_invalid(self, invalid):
message = "Invalid character: %s" % ", ".join(invalid)
self.report.emit(message)
self.animation.stop()
self.animation.start()

def as_empty(self):
self._set_border("empty")
self.report.emit("Empty subset name ..")

def as_exists(self):
self._set_border("exists")
self.report.emit("Existing subset, appending next version.")

def as_new(self):
self._set_border("new")
self.report.emit("New subset, creating first version.")

def _set_border(self, status):
qcolor, style = self.colors[status]
self.animation.setEndValue(qcolor)
self.setStyleSheet(style)

def _get_status_color(self):
return self._status_color

def _set_status_color(self, color):
self._status_color = color
self.setStyleSheet("border-color: %s;" % color.name())

status_color = QtCore.Property(QtGui.QColor,
_get_status_color,
_set_status_color)


class Window(QtWidgets.QDialog):

stateChanged = QtCore.Signal(bool)
Expand All @@ -45,7 +137,7 @@ def __init__(self, parent=None):

listing = QtWidgets.QListWidget()
asset = QtWidgets.QLineEdit()
name = QtWidgets.QLineEdit()
name = SubsetNameLineEdit()
result = QtWidgets.QLineEdit()
result.setStyleSheet("color: gray;")
result.setEnabled(False)
Expand Down Expand Up @@ -127,6 +219,7 @@ def __init__(self, parent=None):
create_btn.clicked.connect(self.on_create)
name.returnPressed.connect(self.on_create)
name.textChanged.connect(self.on_data_changed)
name.report.connect(self.echo)
asset.textChanged.connect(self.on_data_changed)
listing.currentItemChanged.connect(self.on_selection_changed)
listing.currentItemChanged.connect(header.set_item)
Expand Down Expand Up @@ -236,17 +329,14 @@ def _on_data_changed(self):

# Indicate subset existence
if not subset_name:
subset.setStyleSheet("")
message = "Empty subset name .."
subset.as_empty()
elif subset_name in existed_subsets:
subset.setStyleSheet("border-color: #4E76BB;")
message = "Existing subset, appending next version."
else: # New subset
subset.setStyleSheet("border-color: #7AAB8F;")
message = "New subset, creating first version."
subset.as_exists()
else:
subset.as_new()

item.setData(ExistsRole, True)
self.echo(message)

else:
self._build_menu([])
item.setData(ExistsRole, False)
Expand Down

0 comments on commit 2bdcb79

Please sign in to comment.