Exemple #1
0
    def __init__(self, arch, fields, state, name, datas, parent=None):
        QDialog.__init__(self, parent)
        self.setModal(True)
        buttons = []
        self.datas = datas
        self.buttonsLayout = QHBoxLayout()
        self.buttonsLayout.addStretch()
        for x in state:
            but = QPushButton(Common.normalizeLabel(x[1]))
            # We store the value to return into objectName property
            but.setObjectName(x[0])
            # The third element is the gtk-icon
            if len(x) >= 3:
                but.setIcon(Icons.kdeIcon(x[2]))
            # The forth element is True if the button is the default one
            if len(x) >= 4 and x[3]:
                but.setDefault(True)
            self.buttonsLayout.addWidget(but)
            but.clicked.connect(self.slotPush)

        val = {}
        for f in fields:
            if 'value' in fields[f]:
                val[f] = fields[f]['value']

        self.group = RecordGroup('wizard.' + name)
        # Do not allow record loading as most probably 'wizard.'+name model
        # won't exist in the server
        self.group.setDomainForEmptyGroup()
        self.screen = Screen(self)
        self.screen.setRecordGroup(self.group)
        self.screen.new(default=False)
        # We don't want the toolbar to be shown at the wizard
        self.screen.setToolbarVisible(False)
        self.screen.addView(arch, fields, display=True)
        # Set default values
        self.screen.currentRecord().set(val)
        # Set already stored values
        self.screen.currentRecord().set(self.datas)
        self.screen.display()

        # Set minimum and maximum dialog size
        size = self.screen.sizeHint()
        self.setMinimumSize(size.width() + 100, min(600, size.height() + 25))
        size = QApplication.desktop().availableGeometry(self).size()
        size -= QSize(50, 50)
        self.setMaximumSize(size)

        self.layout = QVBoxLayout(self)
        self.layout.addWidget(self.screen)
        self.layout.addLayout(self.buttonsLayout)
        self.setWindowTitle(self.screen.currentView().title)
Exemple #2
0
    def __init__(self, parent, view, attributes):
        AbstractFieldWidget.__init__(self, parent, view, attributes)

        self.button = QPushButton(self)
        layout = QHBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.addWidget(self.button)

        self.button.setText(
            Common.normalizeLabel(attributes.get('string', 'unknown')))
        if 'icon' in attributes:
            self.button.setIcon(Icons.kdeIcon(attributes['icon']))

        self.connect(self.button, SIGNAL('clicked()'), self.click)
    def getImage(self):
        value = self.record.value(self.name)

        if (isinstance(value, tuple) or isinstance(value, list)) and len(value) == 2:
            type, data = value
        else:
            type, data = None, value

        if not data:
            return False

        if type == 'stock':
            stock, size = data
            return self.pixmapToData(Icons.kdePixmap(stock))
        else:
            try:
                return base64.decodestring(data)
            except binascii.Error:
                return False
Exemple #4
0
    def parse(self, root_node, fields, notebook=None, container=None):
        attrs = Common.nodeAttributes(root_node)
        onWriteFunction = attrs.get('on_write', '')

        if container == None:
            parent = self.view
            if notebook:
                parent = notebook
            # We want FormContainer parent to be the notebook for the case
            # when it's a QTabWidget. This way FormContainer can enable/disable
            # the tab when necessary.
            container = FormContainer(parent, int(attrs.get('col', 4)), fields)

        if not self.view.title:
            self.view.title = attrs.get('string', 'Unknown')

        for node in root_node.childNodes:
            if not node.nodeType == node.ELEMENT_NODE:
                continue
            attrs = Common.nodeAttributes(node)
            if node.localName == 'image':
                icon = QLabel(container)
                icon.setPixmap(Icons.kdePixmap(attrs['name']))
                container.addWidget(icon, attrs)

            elif node.localName == 'separator':
                caption = attrs.get('string', '')

                separator = QWidget(container)
                label = QLabel(separator)
                label.setText(caption)
                font = label.font()
                font.setBold(True)
                label.setFont(font)
                line = QFrame(separator)
                line.setFrameShadow(QFrame.Plain)
                if attrs.get('orientation') == 'vertical':
                    line.setFrameShape(QFrame.VLine)
                    separator.setSizePolicy(
                        QSizePolicy.Fixed, QSizePolicy.Expanding)
                    layout = QHBoxLayout(separator)
                else:
                    line.setFrameShape(QFrame.HLine)
                    separator.setSizePolicy(
                        QSizePolicy.Expanding, QSizePolicy.Fixed)
                    layout = QVBoxLayout(separator)
                layout.setAlignment(Qt.AlignTop)
                layout.setContentsMargins(0, 0, 0, 0)
                layout.setSpacing(0)
                layout.addWidget(label)
                layout.addWidget(line)

                self.view.addStateWidget(
                    separator, attrs.get('attrs'), attrs.get('states'))
                container.addWidget(separator, attrs)

            elif node.localName == 'label':
                text = attrs.get('string', '')
                if not text:
                    for node in node.childNodes:
                        if node.nodeType == node.TEXT_NODE:
                            text += node.data
                        else:
                            text += node.toxml()
                label = QLabel(text, container)
                label.setWordWrap(True)
                if 'width' in attrs:
                    label.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
                    label.setMinimumWidth(int(attrs['width']))
                    label.setMaximumWidth(int(attrs['width']))
                else:
                    label.setSizePolicy(
                        QSizePolicy.Preferred, QSizePolicy.Fixed)
                container.addWidget(label, attrs)

            elif node.localName == 'newline':
                container.newRow()

            elif node.localName == 'button':
                if not self.isWidgetVisible(attrs):
                    continue
                button = FieldWidgetFactory.create(
                    'button', container, self.view, attrs)
                name = attrs.get('name')
                if not name:
                    name = 'unnamed_%d' % self.unnamed
                    self.unnamed += 1
                self.view.widgets[name] = button
                self.view.addStateWidget(
                    button, attrs.get('attrs'), attrs.get('states'))
                container.addWidget(button, attrs)

            elif node.localName == 'notebook':
                if not self.isWidgetVisible(attrs):
                    continue
                tab = FormTabWidget(container)
                if attrs and 'tabpos' in attrs:
                    pos = {
                        'up': QTabWidget.North,
                        'down': QTabWidget.South,
                        'left': QTabWidget.West,
                        'right': QTabWidget.East
                    }[attrs['tabpos']]
                else:
                    pos = {
                        'left': QTabWidget.West,
                        'top': QTabWidget.North,
                        'right': QTabWidget.East,
                        'bottom': QTabWidget.South
                    }[Settings.value('koo.tabs_position')]

                tab.setTabPosition(pos)

                attrs['colspan'] = attrs.get('colspan', 3)

                container.addWidget(tab, attrs)

                # We pass a container because othewise a new container would
                # be created by parse() and we don't want that because the tab
                # itself doesn't have a container: it's each of it's pages
                # that will have a container.
                _, onWriteFunction = self.parse(node, fields, tab, container)

            elif node.localName == 'page':
                if not self.isWidgetVisible(attrs):
                    continue
                widget, onWriteFunction = self.parse(node, fields, notebook)
                # Mark the container as the main widget in a Tab. This way
                # we can enable/disable the whole tab easily.
                widget.isTab = True
                self.view.addStateWidget(
                    widget, attrs.get('attrs'), attrs.get('states'))

                notebook.addTab(widget, Common.normalizeLabel(
                    attrs.get('string', '')))

            elif node.localName == 'hpaned':
                widget = QSplitter(Qt.Horizontal, container)

                container.addWidget(widget, attrs)
                self.parse(node, fields, widget, container)

            elif node.localName == 'vpaned':
                widget = QSplitter(Qt.Vertical, container)

                container.addWidget(widget, attrs)
                self.parse(node, fields, widget, container)

            elif node.localName == 'child1':
                widget, onWriteFunction = self.parse(node, fields)
                notebook.addWidget(widget)

            elif node.localName == 'child2':
                widget, onWriteFunction = self.parse(node, fields)
                notebook.addWidget(widget)

            elif node.localName == 'action':
                name = str(attrs['name'])
                widget = FieldWidgetFactory.create(
                    'action', container, self.view, attrs)
                attrs['colspan'] = attrs.get('colspan', 3)
                self.view.widgets[name] = widget
                container.addWidget(widget, attrs)

            elif node.localName == 'field':
                if not self.isWidgetVisible(attrs):
                    continue
                name = attrs['name']
                del attrs['name']
                type = attrs.get('widget', fields[name]['type'])
                fields[name].update(attrs)
                fields[name]['model'] = self.viewModel

                # Create the appropiate widget for the given field type
                widget = FieldWidgetFactory.create(
                    type, container, self.view, fields[name])
                if not widget:
                    continue

                fields[name]['name'] = name
                if self.filter:
                    widget.node = node
                    self.widgetList.append(widget)

                label = None
                if not int(attrs.get('nolabel', 0)):
                    label = fields[name]['string'] + ' :'

                self.view.widgets[name] = widget
                #attrs['colspan'] = int(attrs.get('colspan', widgets_type[ type ][1]))

                if not 'help' in attrs:
                    attrs['help'] = fields[name].get('help', False)

                container.addWidget(widget, attrs, label)

            elif node.localName == 'group':
                if not self.isWidgetVisible(attrs):
                    continue
                widget, onWriteFunction = self.parse(node, fields, notebook)
                if 'string' in attrs:
                    group = QGroupBox(notebook)
                    group.setTitle(attrs['string'])
                    layout = QHBoxLayout(group)
                    layout.setAlignment(Qt.AlignTop)
                    layout.setContentsMargins(0, 0, 0, 0)
                    layout.setSpacing(0)
                    layout.addWidget(widget)
                    widget = group

                self.view.addStateWidget(
                    widget, attrs.get('attrs'), attrs.get('states'))
                container.addWidget(widget, attrs)

        return container, onWriteFunction
Exemple #5
0
 def data(self, index, role=Qt.DisplayRole):
     if not self.group:
         return QVariant()
     if role in (Qt.DisplayRole, Qt.EditRole) or (self._showToolTips and
                                                  role == Qt.ToolTipRole):
         value = self.value(index.row(), index.column(),
                            index.internalPointer())
         fieldType = self.fieldType(index.column(), index.internalPointer())
         if fieldType in ['one2many', 'many2many']:
             return QVariant('(%d)' % value.count())
         elif fieldType == 'selection':
             field = self.fields[self.field(index.column())]
             for x in field['selection']:
                 if x[0] == value:
                     return QVariant(str(x[1]))
             return QVariant()
         elif fieldType == 'date' and value:
             return QVariant(
                 Calendar.dateToText(Calendar.storageToDate(value)))
         elif fieldType == 'datetime' and value:
             return QVariant(
                 Calendar.dateTimeToText(Calendar.storageToDateTime(value)))
         elif fieldType == 'float':
             # If we use the default conversion big numbers are shown
             # in scientific notation. Also we have to respect the number
             # of decimal digits given by the server.
             field = self.fields[self.field(index.column())]
             if role == Qt.EditRole:
                 thousands = False
             else:
                 thousands = True
             return QVariant(
                 Numeric.floatToText(value, field.get('digits', None),
                                     thousands))
         elif fieldType == 'integer':
             return QVariant(Numeric.integerToText(value))
         elif fieldType == 'float_time':
             return QVariant(Calendar.floatTimeToText(value))
         elif fieldType == 'binary':
             if value:
                 return QVariant(_('%d bytes') % len(value))
             else:
                 return QVariant()
         elif fieldType == 'boolean':
             return QVariant(bool(value))
         elif fieldType == 'button':
             if role == Qt.ToolTipRole:
                 fieldName = self.field(index.column())
                 return QVariant(self.buttons[fieldName].get('string', ''))
             return QVariant()
         else:
             if value == False or value == None:
                 return QVariant()
             else:
                 # If the text has several lines put them all in a single one
                 return QVariant(str(value).replace('\n', ' '))
     elif role == Qt.DecorationRole:
         fieldType = self.fieldType(index.column(), index.internalPointer())
         if fieldType == 'button':
             fieldName = self.field(index.column())
             return QVariant(
                 Icons.kdeIcon(self.buttons[fieldName].get('icon')))
         if self.field(index.column()) == self.iconField:
             # Not all models necessarily have the icon so check that first
             model = self.record(index.row(), index.internalPointer())
             if model and self.icon in model.values:
                 return QVariant(Icons.kdeIcon(model.value(self.icon)))
             else:
                 return QVariant()
         else:
             return QVariant()
     elif role == Qt.BackgroundRole:
         if not self.showBackgroundColor:
             return QVariant()
         field = self.fields[self.field(index.column())]
         model = self.record(index.row(), index.internalPointer())
         # We need to ensure we're not being asked about a non existent row.
         # This happens in some special cases (an editable tree in a one2many field,
         # such as the case of fiscal year inside sequences).
         # Note that trying to avoid processing this function if index.row() > self.rowCount()-1
         # works to avoid this but has problems with some tree structures (such as the menu).
         # So we need to make the check here.
         if not model:
             return QVariant()
         # Priorize readonly to required as if it's readonly the
         # user doesn't mind if it's required as she won't be able
         # to change it anyway.
         if not model.isFieldValid(self.field(index.column())):
             color = '#FF6969'
         elif 'readonly' in field and field['readonly']:
             color = 'lightgrey'
         elif 'required' in field and field['required']:
             color = '#ddddff'
         else:
             color = 'white'
         return QVariant(QBrush(QColor(color)))
     elif role == Qt.ForegroundRole:
         if not self.colors:
             return QVariant()
         model = self.record(index.row(), index.internalPointer())
         # We need to ensure we're not being asked about a non existent row.
         # This happens in some special cases (an editable tree in a one2many field,
         # such as the case of fiscal year inside sequences).
         # Note that trying to avoid processing this function if index.row() > self.rowCount()-1
         # works to avoid this but has problems with some tree structures (such as the menu).
         # So we need to make the check here.
         if not model:
             return QVariant()
         palette = QPalette()
         color = palette.color(QPalette.WindowText)
         for (c, expression) in self.colors:
             if model.evaluateExpression(expression, checkLoad=False):
                 color = c
                 break
         return QVariant(QBrush(QColor(color)))
     elif role == Qt.TextAlignmentRole:
         fieldType = self.fieldType(index.column(), index.internalPointer())
         if fieldType in [
                 'integer', 'float', 'float_time', 'time', 'date',
                 'datetime'
         ]:
             return QVariant(Qt.AlignRight | Qt.AlignVCenter)
         else:
             return QVariant(Qt.AlignLeft | Qt.AlignVCenter)
     elif role == KooModel.IdRole:
         model = self.record(index.row(), index.internalPointer())
         return QVariant(model.id)
     elif role == KooModel.ValueRole:
         value = self.value(index.row(), index.column(),
                            index.internalPointer())
         fieldType = self.fieldType(index.column(), index.internalPointer())
         if fieldType in ['one2many', 'many2many']:
             # By now, return the same as DisplayRole for these
             return QVariant('(%d)' % value.count())
         elif fieldType == 'selection':
             # By now, return the same as DisplayRole for these
             field = self.fields[self.field(index.column())]
             for x in field['selection']:
                 if x[0] == value:
                     return QVariant(str(x[1]))
             return QVariant()
         elif fieldType == 'date' and value:
             return QVariant(Calendar.storageToDate(value))
         elif fieldType == 'datetime' and value:
             return QVariant(Calendar.storageToDateTime(value))
         elif fieldType == 'float':
             # If we use the default conversion big numbers are shown
             # in scientific notation. Also we have to respect the number
             # of decimal digits given by the server.
             field = self.fields[self.field(index.column())]
             return QVariant(
                 Numeric.floatToText(value, field.get('digits', None)))
         elif fieldType == 'float_time':
             return QVariant(value)
         elif fieldType == 'binary':
             if value:
                 return QVariant(QByteArray.fromBase64(value))
             else:
                 return QVariant()
         elif fieldType == 'boolean':
             return QVariant(bool(value))
         else:
             if value == False or value == None:
                 return QVariant()
             else:
                 return QVariant(str(value))
     else:
         return QVariant()