Logo Search packages:      
Sourcecode: veusz version File versions  Download package

field.py

#    Copyright (C) 2010 Jeremy S. Sanders
#    Email: Jeremy Sanders <jeremy@jeremysanders.net>
#
#    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.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
##############################################################################

# $Id: field.py 1433 2010-11-04 19:12:00Z jeremysanders $

"""Data entry fields for plugins."""

import veusz.qtall as qt4
import veusz.utils as utils
import veusz.setting as setting

00027 class Field(object):
    """A class to represent an input field on the dialog or command line."""
00029     def __init__(self, name, descr=None, default=None):
        """name: name of field
        descr: description to show to user
        default: default value."""
        self.name = name
        if descr:
            self.descr = descr
        else:
            self.descr = name
        self.default = default

00040     def makeControl(self, doc, currentwidget):
        """Create a set of controls for field."""
        return None

00044     def setControlVal(self, controls, val):
        """Update control's value to val."""
        pass

00048     def getControlResults(self, cntrls):
        """Get result from created contrls."""
        return None

00052 class FieldText(Field):
    """Text entry on the dialog."""

00055     def makeControl(self, doc, currentwidget):
        l = qt4.QLabel(self.descr)
        e = qt4.QLineEdit()
        if self.default:
            e.setText(self.default)
        return (l, e)

00062     def setControlVal(self, controls, val):
        controls[1].setText(val)

00065     def getControlResults(self, cntrls):
        return unicode( cntrls[1].text() )

00068 class FieldCombo(Field):
    """Drop-down combobox on dialog."""
00070     def __init__(self, name, descr=None, default=None, items=(),
                 editable=True):
        """name: name of field
        descr: description to show to user
        default: default value
        items: items in drop-down box
        editable: whether user can enter their own value."""
        Field.__init__(self, name, descr=descr, default=default)
        self.items = items
        self.editable = editable

00081     def makeControl(self, doc, currentwidget):
        l = qt4.QLabel(self.descr)
        c = qt4.QComboBox()
        c.addItems(self.items)
        c.setEditable(bool(self.editable))

        if self.default:
            self.setControlVal((l, c), self.default)

        return (l, c)

00092     def setControlVal(self, controls, val):
        """Update value to val."""
        if self.editable:
            controls[1].setEditText(val)
        else:
            controls[1].setCurrentIndex(controls[1].findText(val))

00099     def getControlResults(self, cntrls):
        return unicode( cntrls[1].currentText() )

00102 class _WidgetCombo(qt4.QComboBox):
    """Combo box for selecting widgets."""

00105     def __init__(self, doc, widgettypes, default):
        """doc: Veusz document
        widgettypes: set of allowed widgettypes or empty for all
        default: default path."""

        qt4.QComboBox.__init__(self)
        self.doc = doc
        self.widgettypes = widgettypes
        self.default = default
        self.updateWidgets()
        self.connect(doc, qt4.SIGNAL("sigModified"), self.updateWidgets)

00117     def _iterateWidgets(self, comboitems, paths, widget, level):
        """Walk widget tree recursively.

        Adds name onto a list of strings (comboitems)
        Adds path to widget onto list of paths (paths)
        """

        if not self.widgettypes or widget.typename in self.widgettypes:
            comboitems.append('  '*level + widget.name)
            paths.append(widget.path)
        for w in widget.children:
            self._iterateWidgets(comboitems, paths, w, level+1)

00130     def updateWidgets(self):
        """Update combo with new widgets."""

        self.paths = []    # veusz widget paths of items
        comboitems = []    # names of items (with tree spacing)
        self._iterateWidgets(comboitems, self.paths, self.doc.basewidget, 0)

        if self.count() == 0:
            # first time around add default to get it selected, yuck :-(
            try:
                idx = self.paths.index(self.default)
                self.addItem( comboitems[idx] )
            except ValueError:
                pass

        utils.populateCombo(self, comboitems)
    
00147     def getWidgetPath(self):
        """Get path of selected widget."""
        return self.paths[self.currentIndex()]

00151 class FieldWidget(Field):
    """Drop-down combobox for selecting widgets."""

00154     def __init__(self, name, descr=None, default='/', widgettypes=set()):
        """name: name of field
        descr: description to show to user
        default: default value - set to '' to get current widget."""

        Field.__init__(self, name, descr=descr, default=default)
        self.widgettypes = widgettypes

00162     def makeControl(self, doc, currentwidget):
        default = self.default
        if default == '':
            default = currentwidget

        l = qt4.QLabel(self.descr)
        c = _WidgetCombo(doc, self.widgettypes, default)
        return (l, c)

00171     def setControlVal(self, cntrls, val):
        controls[1].setCurrentIndex(c.findText(val))

00174     def getControlResults(self, cntrls):
        return cntrls[1].getWidgetPath()

00177 class _FieldSetting(Field):
    """Field using a setting internally to avoid code duplication.
    Designed to be subclassed."""

    def __init__(self, settingkls, name, descr=None, default='',
                 setnparams = {}):
        Field.__init__(self, name, descr=descr, default=default)
        self.default = default
        self.setn = settingkls(name, default, **setnparams)

00187     def makeControl(self, doc, currentwidget):
        """Use setting makeControl method to make control."""
        self.setn.parent = self # setting looks to parent for document
        self.setn.set(self.default)

        self.document = doc
        l = qt4.QLabel(self.descr)
        c = self.setn.makeControl(None)

        def updateval(cntrl, setn, val):
            setn.set(val)

        # if control changes setting, update setting
        c.connect(c, qt4.SIGNAL('settingChanged'), updateval)

        return (l, c)

00204     def setControlVal(self, cntrls, val):
        self.setn.set(val)

00207     def getDocument(self):
        """This is used by settings to get their document."""
        return self.document

00211     def getControlResults(self, cntrls):
        """Get result from setting."""
        return self.setn.get()

00215 class FieldBool(_FieldSetting):
    """A true/false value using a check box."""
00217     def __init__(self, name, descr=None, default=False):
        _FieldSetting.__init__(self, setting.Bool, name,
                               descr=descr, default=default)

00221 class FieldInt(_FieldSetting):
    """An integer number field."""

00224     def __init__(self, name, descr=None, default=0,
                 minval=-9999999, maxval=9999999):
        """name: name of field
        descr: description to show to user
        default: default value.
        minval and maxval: minimum and maximum integers
        """
        _FieldSetting.__init__(self, setting.Int,
                               name, descr=descr, default=default,
                               setnparams={'minval': minval, 'maxval': maxval})

00235 class FieldFloat(_FieldSetting):
    """A floating point number field."""

00238     def __init__(self, name, descr=None, default=None,
                 minval=-1e99, maxval=1e99):
        """name: name of field
        descr: description to show to user
        default: default value.
        minval and maxval: minimum and maximum values
        """

        _FieldSetting.__init__(self, setting.Float,
                               name, descr=descr, default=default,
                               setnparams={'minval': minval, 'maxval': maxval})

00250 class FieldColor(_FieldSetting):
    """Field for selecting a color - returns #rrggbb string."""
00252     def __init__(self, name, descr=None, default='black'):
        _FieldSetting.__init__(self, setting.Color, name,
                               descr=descr, default=default)

00256 class FieldFillStyle(_FieldSetting):
    """Field for selecting fill styles - returns a string."""
00258     def __init__(self, name, descr=None, default='solid'):
        _FieldSetting.__init__(self, setting.FillStyle, name,
                               descr=descr, default=default)

00262 class FieldLineStyle(_FieldSetting):
    """Field for selecting line styles - returns a string."""
00264     def __init__(self, name, descr=None, default='solid'):
        _FieldSetting.__init__(self, setting.LineStyle, name,
                               descr=descr, default=default)

00268 class FieldMarker(_FieldSetting):
    """Field for selecting a marker type.
    
    Returns a string
    """
00273     def __init__(self, name, descr=None, default='circle'):
        _FieldSetting.__init__(self, setting.Marker, name,
                               descr=descr, default=default)

00277 class FieldArrow(_FieldSetting):
    """Field for selecting an arrow type.
    
    Returns a string
    """
00282     def __init__(self, name, descr=None, default='none'):
        _FieldSetting.__init__(self, setting.Arrow, name,
                               descr=descr, default=default)

00286 class FieldErrorStyle(_FieldSetting):
    """Field for selecting an error bar style
    
    Returns a string
    """
00291     def __init__(self, name, descr=None, default='bar'):
        _FieldSetting.__init__(self, setting.ErrorStyle, name,
                               descr=descr, default=default)

00295 class FieldDistance(_FieldSetting):
    """Field for selecting a veusz-style distance, e.g. '1pt'.

    Returns a string
    """
00300     def __init__(self, name, descr=None, default='1pt'):
        _FieldSetting.__init__(self, setting.Distance, name,
                               descr=descr, default=default)

00304 class FieldFloatList(_FieldSetting):
    """Field for entering multiple numbers, separated by commas or spaces

    Returns a list/tuple of floats
    """
00309     def __init__(self, name, descr=None, default=()):
        _FieldSetting.__init__(self, setting.FloatList, name,
                               descr=descr, default=default)

00313 class FieldDataset(_FieldSetting):
    """Field for selecting a datset.
    Returns a string.

    Note that the validity of dataset names is not checked
    Note that a blank string may result
    """

00321     def __init__(self, name, descr=None, default='', dims=1,
                 datatype='numeric'):
        """name: name of field
        descr: description to show to user
        default: default value (ignored currently)
        dims: dimensions of dataset to show
        datatype: type of data: numeric or text
        """
        _FieldSetting.__init__(self, setting.Dataset,
                               name, descr=descr, default=default,
                               setnparams={'dimensions': dims,
                                           'datatype': datatype})

00334 class FieldTextMulti(_FieldSetting):
    """Field for entering multiple lines of text.
    Returns a tuple/list of strings.
    """
00338     def __init__(self, name, descr=None, default=('')):
        _FieldSetting.__init__(self, setting.Strings, name,
                               descr=descr, default=default)

00342 class FieldDatasetMulti(_FieldSetting):
    """Field for entering multiple datasets.
    Returns a tuple/list of strings.
    """
00346     def __init__(self, name, descr=None, default=(''), dims=1,
                 datatype='numeric'):
        """dims is number of dimensions of datasets to show in
        drop-down list.

        datatype is 'numeric' or 'text'
        """
        _FieldSetting.__init__(self, setting.Datasets, name,
                               descr=descr, default=default,
                               setnparams={'dimensions': dims,
                                           'datatype': datatype})

00358 class FieldLineMulti(_FieldSetting):
    """A field for holding a set of lines. Consists of tuples
    [('dotted', '1pt', 'color', <trans>, False), ...]

    These are style, width, color, and hide or
    style, widget, color, transparency, hide

    This is compatible with the contour widget line style
    """

    def __init__(self, name, descr=None,
                 default=(('solid', '1pt', 'black', False),) ):
        _FieldSetting.__init__(self, setting.LineSet, name,
                               descr=descr, default=default)

00373 class FieldFillMulti(_FieldSetting):
    """A field for holding a set of fills. Consists of tuples

    [('solid', 'color', <trans>, False), ...]
        
    These are color, fill style, and hide or
    color, fill style, transparency and hide

    This is compatible with the contour widget line style
    """

00384     def __init__(self, name, descr=None, default=()):
        _FieldSetting.__init__(self, setting.FillSet, name,
                               descr=descr, default=default)

00388 class FieldFontFamily(_FieldSetting):
    """A field for holding a font family.

    Returns a string.
    """

00394     def __init__(self, name, descr=None, default=None):
        """Default None selects the default font."""
        if default is None:
            default = setting.Text.defaultfamily
        _FieldSetting.__init__(self, setting.FontFamily, name,
                               descr=descr, default=default)

00401 class FieldFilename(_FieldSetting):
    """Select a filename with a browse button."""

00404     def __init__(self, name, descr=None, default=''):
        _FieldSetting.__init__(self, setting.Filename, name,
                               descr=descr, default=default)


Generated by  Doxygen 1.6.0   Back to index