2012-10-05 23:28:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
ParameterNumber.py
|
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-12-08 23:21:08 +01:00
|
|
|
from PyQt4 import QtGui
|
2012-10-05 23:28:47 +02:00
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
from PyQt4.QtGui import *
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.parameters.Parameter import Parameter
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
class ParameterNumber(Parameter):
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
def __init__(self, name='', description='', minValue=None, maxValue=None,
|
|
|
|
default=0.0):
|
2012-09-15 18:25:25 +03:00
|
|
|
Parameter.__init__(self, name, description)
|
|
|
|
try:
|
|
|
|
self.default = int(str(default))
|
|
|
|
self.isInteger = True
|
|
|
|
except:
|
|
|
|
self.default = default
|
|
|
|
self.isInteger = False
|
2013-01-12 23:36:00 +01:00
|
|
|
self.min = minValue
|
2012-09-15 18:25:25 +03:00
|
|
|
self.max = maxValue
|
|
|
|
self.value = None
|
|
|
|
|
|
|
|
def setValue(self, n):
|
|
|
|
if n is None:
|
|
|
|
self.value = self.default
|
|
|
|
return True
|
|
|
|
try:
|
2013-10-01 20:52:22 +03:00
|
|
|
if float(n) - int(float(n)) == 0:
|
2012-09-15 18:25:25 +03:00
|
|
|
value = int(float(n))
|
|
|
|
else:
|
2013-01-12 23:36:00 +01:00
|
|
|
value = float(n)
|
2012-12-08 23:21:08 +01:00
|
|
|
if self.min is not None:
|
2013-01-12 23:36:00 +01:00
|
|
|
if value < self.min:
|
2012-09-15 18:25:25 +03:00
|
|
|
return False
|
2012-12-08 23:21:08 +01:00
|
|
|
if self.max is not None:
|
2012-09-15 18:25:25 +03:00
|
|
|
if value > self.max:
|
|
|
|
return False
|
|
|
|
self.value = value
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def serialize(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
return self.__module__.split('.')[-1] + '|' + self.name + '|' \
|
|
|
|
+ self.description + '|' + str(self.min) + '|' + str(self.max) \
|
|
|
|
+ '|' + str(self.default)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def deserialize(self, s):
|
2013-10-01 20:52:22 +03:00
|
|
|
tokens = s.split('|')
|
|
|
|
for i in range(3, 5):
|
2012-09-15 18:25:25 +03:00
|
|
|
if tokens[i] == str(None):
|
|
|
|
tokens[i] = None
|
|
|
|
else:
|
|
|
|
tokens[i] = float(tokens[i])
|
|
|
|
try:
|
2013-04-12 17:08:43 +02:00
|
|
|
val = int(tokens[5])
|
2012-09-15 18:25:25 +03:00
|
|
|
except:
|
2013-04-12 17:08:43 +02:00
|
|
|
val = float(tokens[5])
|
|
|
|
return ParameterNumber(tokens[1], tokens[2], tokens[3], tokens[4], val)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def getAsScriptCode(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
return '##' + self.name + '=number ' + str(self.default)
|