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

I have created a form and added an attribute as a Text widget in the Roam config manager.

Is it possible to restrict the input to be an integer between 0 and 999?
I want the field to stay "empty" (or "blanc") as long as the user enter a letter and only to accept integers (but not more that 3 digits). If a 4th. digit is entered, the field should continue only showing the 3 first allowed digits.

Please see http://gis.stackexchange.com/questions/131184/restrict-text-widget-to-only-integer-between-0-and-999-in-intramaps-roam for more details (e.g. code that works in QGIS).

Yes, I think so if you're refering to the top of the corresponding form.py:

from roam.api import FeatureForm, RoamEvents, utils
from PyQt4.QtGui import QIntValidator
class Form(FeatureForm):
    def __init__(self, *args, **kwargs):
    def uisetup(self):
        self.boundwidgets['flatenr'].valuechanged.connect(self.handler1)
    def handler1(self,value):
        # validator = QIntValidator(0, 999, self)
        # self.boundwidgets['flatenr'].setValidator(validator)
        self.boundwidgets['flatenr'].setValidator(QtGui.QIntValidator(1, 999, self))

In Roam I get this error message: AttributeError: 'TextWidget' object has no attribute 'setValidator'.
Any tip?

An update: I never managed to use QIntValidator nor QRegExpValidator directly on self.boundwidgets['flatenr'] with setValidator as e.g. self.boundwidgets['flatenr'].setValidator(validator).
My "solution" (workaround): I create a new QLineEdit called flatenr and display it directly above the original field (created in the Config Manager) and then I save the value from this QLineEdit as the attribute value for the self.boundwidgets['flatenr']. In addition I added "save last value".
With "my own" QLineEdit QIntValidator and QRegExpValidator works. However, I would prefer to be able to use these directly on self.boundwidgets['...']. Is it possible? (It's time consuming placing the QLineEdit directly on the self.boundwidgets['...']....)

My workaround-code:

from roam.api import FeatureForm, RoamEvents, utils
from PyQt4.QtGui import QRegExpValidator, QLineEdit, QDesktopWidget
from PyQt4.QtCore import QRegExp, QSettings
class Form(FeatureForm):
    def __init__(self, *args, **kwargs):
        super(Form, self).__init__(*args, **kwargs)
    def uisetup(self):
        # Getting the screen size
        screen = QDesktopWidget().availableGeometry()  # returns the usable rectangle of the screen
        wSize = screen.width()                         # returns the usable screen witdh
        # Creating a new lineedit-widget performing regexp to ensure 4 digits flatenr (between 1000 and 9999)
        global flatenr
        flatenr = QLineEdit(self)
        # Display the QLineEdit above self.boundwidgets['flatenr'], depends on PC or tablet (with 150 % text size) screen size
        flatenr.setGeometry(169.5,19,wSize-537,42)      #vertical 26 on PC, sets the widt to available screen width
        # Make the QLineEdit frame transparent (the frame of the text widget self.boundwidgets['flatenr'] will be visible)
        flatenr.setStyleSheet("QLineEdit{border: 1px transparent}")
        # Change the frame color if the linedit is selcted
        flatenr.setStyleSheet("QLineEdit{selection-border: 2px solid #5CADFF; selection-border-radius: 2px}")
        # Only allow 4 digits flatenr not starting with a zero
        rx = QRegExp('^[1-9]\d{3}$')
        validator = QRegExpValidator(rx)
        flatenr.setValidator(validator)
        flatenr.textChanged.connect(self.handler1)
    def handler1(self,value):
        # Save the value from QLineEdit as the attribute flatenr in the shapefile
        self.boundwidgets['flatenr'].setvalue(flatenr.text())
        # Save value from QLineEdit (just like "action: save last value" in the config manager)
        settings = QSettings("setting.set")
        text = flatenr.text()
        settings.setValue("text", text)
    def load(self, feature, layers, values):
        # Restore value of QLineEdit (show the last saved value)
        settings = QSettings("setting.set")
        text = settings.value("text", "")
        flatenr.setText(str(text))
        # Set the value of the attribute flatenr = the value in the QLineEdit
        self.boundwidgets['flatenr'].setvalue(flatenr.text())
          

Why can't you attach it to the widget already there?

On Wed, 1 Jul 2015 6:26 pm 9ls1 notifications@github.com wrote:

An update: I never managed to use QIntValidator nor QRegExpValidator
directly on self.boundwidgets['flatenr'] with setValidator as e.g.
self.boundwidgets['flatenr'].setValidator(validator).
My "solution" (workaround): I create a new QLineEdit called flatenr and
display it directly above the original field (created in the Config
Manager) and then I save the value from this QLineEdit as the attribute
value for the self.boundwidgets['flatenr']. In addition I added "save last
value".
With "my own" QLineEdit QIntValidator and QRegExpValidator works. However,
I would prefer to be able to use these directly on
self.boundwidgets['...']. Is it possible? (It's time consuming placing
the QLineEdit directly on the self.boundwidgets['...']....)

My workaround-code:

from roam.api import FeatureForm, RoamEvents, utils
from PyQt4.QtGui import QRegExpValidator, QLineEdit, QDesktopWidget
from PyQt4.QtCore import QRegExp, QSettings

class Form(FeatureForm):
def init(self, _args, *_kwargs):
super(Form, self).init(_args, *_kwargs)

def uisetup(self):
    # Getting the screen size
    screen = QDesktopWidget().availableGeometry()  # returns the usable rectangle of the screen
    wSize = screen.width()                         # returns the usable screen witdh
    # Creating a new lineedit-widget performing regexp to ensure 4 digits flatenr (between 1000 and 9999)
    global flatenr
    flatenr = QLineEdit(self)
    # Display the QLineEdit above self.boundwidgets['flatenr'], depends on PC or tablet (with 150 % text size) screen size
    flatenr.setGeometry(169.5,19,wSize-537,42)      #vertical 26 on PC, sets the widt to available screen width
    # Make the QLineEdit frame transparent (the frame of the text widget self.boundwidgets['flatenr'] will be visible)
    flatenr.setStyleSheet("QLineEdit{border: 1px transparent}")
    # Change the frame color if the linedit is selcted
    flatenr.setStyleSheet("QLineEdit{selection-border: 2px solid #5CADFF; selection-border-radius: 2px}")
    # Only allow 4 digits flatenr not starting with a zero
    rx = QRegExp('^[1-9]\d{3}$')
    validator = QRegExpValidator(rx)
    flatenr.setValidator(validator)
    flatenr.textChanged.connect(self.handler1)
def handler1(self,value):
# Save the value from QLineEdit as the attribute flatenr in the shapefile
self.boundwidgets['flatenr'].setvalue(flatenr.text())
# Save value from QLineEdit (just like "action: save last value" in the config manager)
settings = QSettings("setting.set")
text = flatenr.text()
settings.setValue("text", text)
def load(self, feature, layers, values):
# Restore value of QLineEdit (show the last saved value)
settings = QSettings("setting.set")
text = settings.value("text", "")
flatenr.setText(str(text))
# Set the value of the attribute flatenr = the value in the QLineEdit
self.boundwidgets['flatenr'].setvalue(flatenr.text())
Reply to this email directly or view it on GitHub
#239 (comment).

rx = QRegExp('^[1-9]\d{3}$') validator = QRegExpValidator(rx) self.boundwidgets['flatenr'].setValidator(validator)

without any luck.
In Roam I get this error message: AttributeError: 'TextWidget' object has no attribute 'setValidator'.
How to write the code correctly?

self.boundwidgets['flatenr'].widget.setValidator

On Wed, 1 Jul 2015 6:48 pm 9ls1 notifications@github.com wrote:

Hm, I tried different variants of

def handler1(self,value):
rx = QRegExp('^[1-9]\d{3}$')
validator = QRegExpValidator(rx)
self.boundwidgets['flatenr'].setValidator(validator)

without any luck.

In Roam I get this error message: AttributeError: 'TextWidget' object has
no attribute 'setValidator'.

How to write the code correctly?

Reply to this email directly or view it on GitHub
#239 (comment).

Oh dear. The "additional" widget in self.boundwidgets['flatenr'].widget.setValidator did the trick! Thanks!
Now the code

rx = QRegExp('^[1-9]\d{3}$')
validator = QRegExpValidator(rx)
self.boundwidgets['flatenr'].widget.setValidator(validator)

works. :)
(It's a pitty I didn't figure out the need of the "widget" myself a couple of weeks ago - would have saved me 2 days of trying and failing.)

Thanks for your great effort so far with IntraMaps Roam. It's appreciated very much indeed.
We really enjoy using ver. 2.4.2. Looking forward to the next version with a new (dear I say improved?) widget API. We really hope you'll include the possibility to "dynamic" required fields
through the API or through python-code, ref. #283.

**An update:** I never managed to use QIntValidator nor QRegExpValidator directly on self.boundwidgets['flatenr'] with setValidator as e.g. self.boundwidgets['flatenr'].setValidator(validator).