Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #534 by implementing name validating widget #543

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 5 additions & 82 deletions avalon/tools/creator/app.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import sys
import inspect
import re

from ...vendor.Qt import QtWidgets, QtCore, QtGui
from ...vendor import qtawesome
from ...vendor import six
from ... import api, io, style

from .. import lib
from .. import lib, widgets

module = sys.modules[__name__]
module.window = None
Expand All @@ -21,96 +20,20 @@
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 = self.colors["empty"][0]

anim = QtCore.QPropertyAnimation()
anim.setTargetObject(self)
anim.setPropertyName(b"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()
class SubsetNameLineEdit(widgets.NameValidEdit):

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

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

def as_new(self):
self._set_border("new")
self.indicate("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):

Expand Down
3 changes: 2 additions & 1 deletion avalon/tools/projectmanager/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from ...vendor.Qt import QtWidgets, QtCore

from . import lib
from .. import widgets


class TasksCreateDialog(QtWidgets.QDialog):
Expand Down Expand Up @@ -67,7 +68,7 @@ def __init__(self, parent=None):

# Label
label_label = QtWidgets.QLabel("Label:")
label = QtWidgets.QLineEdit()
label = widgets.NameValidEdit()
label.setPlaceholderText("<label>")

# Parent
Expand Down
101 changes: 101 additions & 0 deletions avalon/tools/widgets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import re

from . import lib

Expand Down Expand Up @@ -521,3 +522,103 @@ def on_changed(self, argument):

def parse(self):
return self._options.copy()


class NameValidator(QtGui.QRegExpValidator):

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

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

def validate(self, input, pos):
results = super(NameValidator, 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 str(input):
if char in [" ", "\n", "\t"]:
invalid.add(char)
continue
if not re_valid.match(char):
invalid.add(char)
return invalid, input


class NameValidEdit(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(NameValidEdit, self).__init__(*args, **kwargs)

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

self._status_color = self.colors["empty"][0]

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

validator.invalid.connect(self.on_invalid)

def on_invalid(self, invalid, input):
message = "Invalid character: %s" % ", ".join(repr(c) for c in invalid)
self.report.emit(message)
self.animation.stop()
self.animation.start()
# For improving UX in case like pasting invalid string, set cleaned
# text back to widget, instead of nothing happens by default.
text = self.text()
for c in invalid:
text = text.replace(c, "")
self.setText(text)

def indicate(self, status):
"""Indicate the status of current input via animation

`NameValidEdit` has three states:

"empty": Empty input
"exists": Input value already exists
"new": Input value is new

And each state has a corresponding border color.

Args:
status (str): Status name

"""
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)