Ejemplo n.º 1
0
    def addSaleCommission(self):
        title = QApplication.translate('DetailsTabWidget', "Commission")
        item = FormItem(self.settings, None, title, Type.Money | Type.Disabled)
        self.saleCommission = item.widget()

        self.items['saleprice'].widget().textChanged.connect(self.salePriceChanged)
        self.items['totalsaleprice'].widget().textChanged.connect(self.salePriceChanged)

        return item
Ejemplo n.º 2
0
    def addSaleCommission(self):
        title = QApplication.translate('DetailsTabWidget', "Commission")
        item = FormItem(self.settings, None, title, Type.Money | Type.Disabled)
        self.saleCommission = item.widget()

        self.items['saleprice'].widget().textChanged.connect(self.salePriceChanged)
        self.items['totalsaleprice'].widget().textChanged.connect(self.salePriceChanged)

        return item
Ejemplo n.º 3
0
    def addSaleCommission(self):
        item = FormItem(None, self.tr("Commission"), Type.Money)
        self.saleCommission = item.widget()

        validator = CommissionValidator(0, 9999999999, 2, self)
        validator.setNotation(QDoubleValidator.StandardNotation)
        self.saleCommission.setValidator(validator)

        self.items['saleprice'].widget().textChanged.connect(self.saleCommissionChanged)
        self.saleCommission.textChanged.connect(self.saleCommissionChanged)
        self.items['totalsaleprice'].widget().textChanged.connect(self.saleTotalPriceChanged)

        return item
Ejemplo n.º 4
0
    def addSaleCommission(self):
        item = FormItem(None, self.tr("Commission"), Type.Money)
        self.saleCommission = item.widget()

        validator = CommissionValidator(0, 9999999999, 2, self)
        validator.setNotation(QDoubleValidator.StandardNotation)
        self.saleCommission.setValidator(validator)

        self.items['saleprice'].widget().textChanged.connect(self.saleCommissionChanged)
        self.saleCommission.textChanged.connect(self.saleCommissionChanged)
        self.items['totalsaleprice'].widget().textChanged.connect(self.saleTotalPriceChanged)

        return item
Ejemplo n.º 5
0
    def addPayCommission(self):
        item = FormItem(self.settings, None, self.tr("Commission"), Type.Money)
        self.payCommission = item.widget()
        self.payCommission.setToolTip(self.tr("Available format 12.5 or 10%"))

        validator = CommissionValidator(0, 9999999999, 2, self)
        validator.setNotation(QDoubleValidator.StandardNotation)
        self.payCommission.setValidator(validator)

        self.items['payprice'].widget().textChanged.connect(self.payCommissionChanged)
        self.payCommission.textChanged.connect(self.payCommissionChanged)
        self.items['totalpayprice'].widget().textChanged.connect(self.payTotalPriceChanged)

        return item
Ejemplo n.º 6
0
    def addPayCommission(self):
        item = FormItem(self.settings, None, self.tr("Commission"), Type.Money)
        self.payCommission = item.widget()
        self.payCommission.setToolTip(self.tr("Available format 12.5 or 10%"))

        validator = CommissionValidator(0, 9999999999, 2, self)
        validator.setNotation(QDoubleValidator.StandardNotation)
        self.payCommission.setValidator(validator)

        self.items['payprice'].widget().textChanged.connect(self.payCommissionChanged)
        self.payCommission.textChanged.connect(self.payCommissionChanged)
        self.items['totalpayprice'].widget().textChanged.connect(self.payTotalPriceChanged)

        return item
Ejemplo n.º 7
0
class DetailsTabWidget(QTabWidget):
    Direction = QBoxLayout.LeftToRight
    Stretch = 'stretch item'

    def __init__(self, model, parent=None):
        super(DetailsTabWidget, self).__init__(parent)

        self.model = model

        self.createItems()
        self.createPages()

    def createPages(self):
        self.createCoinPage()
        self.createTrafficPage()
        self.createParametersPage()
        self.createDesignPage()
        self.createClassificationPage()

    def createCoinPage(self):
        main = self.mainDetailsLayout()
        state = self.stateLayout()
        title = QApplication.translate('DetailsTabWidget', "Coin")
        self.addTabPage(title, [main, self.Stretch, state])

    def createTrafficPage(self):
        self.oldTrafficIndex = 0
        parts = self._createTrafficParts(self.oldTrafficIndex)
        title = QApplication.translate('DetailsTabWidget', "Traffic")
        self.addTabPage(title, parts)

    def createParametersPage(self):
        parameters = self.parametersLayout()
        minting = self.mintingLayout()
        note = self.noteLayout()

        title = QApplication.translate('DetailsTabWidget', "Parameters")
        self.addTabPage(title, [parameters, self.Stretch, minting, note])

    def createDesignPage(self):
        obverse = self.obverseDesignLayout()
        reverse = self.reverseDesignLayout()
        edge = self.edgeDesignLayout()
        subject = self.subjectLayout()

        title = QApplication.translate('DetailsTabWidget', "Design")
        self.addTabPage(title, [obverse, reverse, self.Stretch, edge, subject])

    def createClassificationPage(self):
        catalogue = self.catalogueLayout()
        rarity = self.rarityLayout()
        price = self.priceLayout()
        variation = self.variationLayout()
        url = self.urlLayout()

        title = QApplication.translate('DetailsTabWidget', "Classification")
        self.addTabPage(title, [catalogue, rarity, price, self.Stretch,
                                variation, url])

    def _layoutToWidget(self, layout):
        widget = QWidget(self)
        widget.setLayout(layout)
        return widget

    def createTabPage(self, parts):
        # Remove all empty parts
        for part in parts:
            if isinstance(part, BaseFormGroupBox):
                if part.isEmpty():
                    parts.remove(part)

        if self.Direction == QBoxLayout.LeftToRight:
            newParts = []
            layout = QVBoxLayout()
            stretchNeeded = True
            count = 0
            for part in parts:
                if part == self.Stretch:
                    if count > 0:
                        newParts.append(layout)
                        if stretchNeeded:
                            layout.insertStretch(-1)
                        layout = QVBoxLayout()
                    stretchNeeded = True
                    count = 0
                else:
                    if isinstance(part, QWidget):
                        layout.addWidget(part)
                        if part.sizePolicy().verticalPolicy() == QSizePolicy.Preferred:
                            stretchNeeded = False
                    else:
                        layout.addLayout(part)
                    count = count + 1
            if count > 0:
                newParts.append(layout)
                if stretchNeeded:
                    layout.insertStretch(-1)
            parts = newParts
        else:
            for part in parts:
                if part == self.Stretch:
                    parts.remove(part)

        pageLayout = QBoxLayout(self.Direction, self)
        # Fill layout with it's parts
        stretchNeeded = True
        for part in parts:
            if isinstance(part, QWidget):
                pageLayout.addWidget(part)
                if part.sizePolicy().verticalPolicy() == QSizePolicy.Preferred:
                    stretchNeeded = False
            else:
                pageLayout.addLayout(part)
                if isinstance(part, ImageFormLayout):
                    stretchNeeded = False

        if self.Direction == QBoxLayout.TopToBottom and stretchNeeded:
            pageLayout.insertStretch(-1)

        return self._layoutToWidget(pageLayout)

    def addTabPage(self, title, parts):
        page = self.createTabPage(parts)
        index = self.addTab(page, title)
        # Disable if empty
        if len(parts) == 0:
            self.setTabEnabled(index, False)

    def addItem(self, field):
        # Skip image fields for not a form
        if field.type in Type.ImageTypes:
            return

        item = FormItem(field.name, field.title, field.type | Type.Disabled)
        if not field.enabled:
            item.setHidden()
        self.items[field.name] = item

    def createItems(self):
        self.items = {}

        fields = self.model.fields
        for field in fields:
            if field not in fields.systemFields:
                self.addItem(field)

    def fillItems(self, record):
        if not record.isEmpty():
            # Fields with commission dependent on status field and should be
            # filled after it and in right order
            ordered_item_keys = ['status', 'payprice', 'totalpayprice',
                                 'saleprice', 'totalsaleprice']
            for key in ordered_item_keys:
                if key in self.items:
                    item = self.items[key]
                    self._fillItem(record, item)

            for item in self.items.values():
                if item.field() in ordered_item_keys:
                    continue

                self._fillItem(record, item)

    def _fillItem(self, record, item):
        if not record.isNull(item.field()):
            value = record.value(item.field())
            item.setValue(value)
        else:
            item.widget().clear()

    def clear(self):
        for item in self.items.values():
            item.widget().clear()

    def mainDetailsLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Main details")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['title'])
        layout.addRow(self.items['country'])
        layout.addRow(self.items['period'])
        layout.addRow(self.items['value'], self.items['unit'])
        layout.addRow(self.items['year'])
        layout.addRow(self.items['mintmark'], self.items['mint'])
        layout.addRow(self.items['type'])
        layout.addRow(self.items['series'])
        layout.addRow(self.items['subjectshort'])

        return layout

    def stateLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "State")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['status'], self.items['grade'])
        self.items['status'].widget().currentIndexChanged.connect(self.indexChangedState)
        layout.addRow(self.items['storage'])
        layout.addRow(self.items['quantity'], self.items['barcode'])
        layout.addRow(self.items['defect'])
        layout.addRow(self.items['features'])

        return layout

    def payLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Buy")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['paydate'], self.items['payprice'])

        # Add auxiliary field
        item = self.addPayCommission()

        layout.addRow(self.items['totalpayprice'], item)
        layout.addRow(self.items['seller'])
        layout.addRow(self.items['payplace'])
        layout.addRow(self.items['payinfo'])

        return layout

    def saleLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Sale")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['saledate'], self.items['saleprice'])

        # Add auxiliary field
        item = self.addSaleCommission()
        layout.addRow(self.items['totalsaleprice'], item)

        layout.addRow(self.items['buyer'])
        layout.addRow(self.items['saleplace'])
        layout.addRow(self.items['saleinfo'])

        return layout

    def passLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Pass")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['saledate'], self.items['saleprice'])

        # Add auxiliary field
        item = self.addPayCommission()
        layout.addRow(self.items['totalpayprice'], item)
        self.items['saleprice'].widget().textChanged.connect(self.items['payprice'].widget().setText)

        # Add auxiliary field
        item = self.addSaleCommission()
        layout.addRow(self.items['totalsaleprice'], item)

        layout.addRow(self.items['seller'])
        layout.addRow(self.items['buyer'])
        layout.addRow(self.items['saleplace'])
        layout.addRow(self.items['saleinfo'])

        return layout

    def parametersLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Parameters")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['material'])
        layout.addRow(self.items['fineness'], self.items['weight'])
        layout.addRow(self.items['diameter'], self.items['thickness'])
        layout.addRow(self.items['shape'])
        layout.addRow(self.items['obvrev'])

        return layout

    def mintingLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Minting")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['issuedate'], self.items['mintage'])
        layout.addRow(self.items['dateemis'])

        item = self.items['quality']
        layout.addHalfRow(item)
        item.widget().setSizePolicy(QSizePolicy.Preferred,
                                    QSizePolicy.Fixed)

        return layout

    def noteLayout(self, parent=None):
        layout = BaseFormLayout(parent)

        layout.addRow(self.items['note'])

        return layout

    def obverseDesignLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Obverse")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['obversedesign'])
        layout.addRow(self.items['obversedesigner'])

        return layout

    def reverseDesignLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Reverse")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['reversedesign'])
        layout.addRow(self.items['reversedesigner'])

        return layout

    def edgeDesignLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Edge")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['edge'])
        layout.addRow(self.items['edgelabel'])

        return layout

    def subjectLayout(self, parent=None):
        layout = BaseFormLayout(parent)

        layout.addRow(self.items['subject'])

        return layout

    def rarityLayout(self, parent=None):
        layout = BaseFormLayout(parent)

        item = self.items['rarity']
        layout.addHalfRow(item)
        item.widget().setSizePolicy(QSizePolicy.Preferred,
                                    QSizePolicy.Fixed)

        return layout

    def catalogueLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Catalogue")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['catalognum1'], self.items['catalognum2'])
        layout.addRow(self.items['catalognum3'], self.items['catalognum4'])

        return layout

    def priceLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Price")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['price1'], self.items['price2'])
        layout.addRow(self.items['price3'], self.items['price4'])

        return layout

    def variationLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Variation")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['variety'])
        layout.addRow(self.items['obversevar'], self.items['reversevar'])
        layout.addHalfRow(self.items['edgevar'])

        return layout

    def urlLayout(self, parent=None):
        layout = BaseFormLayout(parent)

        layout.addRow(self.items['url'])

        return layout

    def _createTrafficParts(self, index=0):
        pageParts = []
        if index == 0:
            pass
        elif index == 1:
            pass_ = self.passLayout()
            pageParts.append(pass_)
        elif index == 2:
            pay = self.payLayout()
            pageParts.append(pay)
        elif index == 3:
            pay = self.payLayout()
            pageParts.append(pay)
        elif index == 4:
            pay = self.payLayout()
            sale = self.saleLayout()
            pageParts.extend([pay, self.Stretch, sale])
        elif index == 5:
            pay = self.payLayout()
            pageParts.append(pay)
        elif index == 6:
            pass

        self.oldTrafficIndex = index

        return pageParts

    def indexChangedState(self, index):
        pageIndex = self.currentIndex()

        self.removeTab(1)
        pageParts = self._createTrafficParts(index)
        page = self.createTabPage(pageParts)

        title = QApplication.translate('DetailsTabWidget', "Traffic")
        self.insertTab(1, page, title)
        if len(pageParts) == 0:
            self.setTabEnabled(1, False)
#            self.items['grade'].widget().setEnabled(False)
            if pageIndex == 1:
                self.setCurrentIndex(pageIndex - 1)
        else:
#            self.items['grade'].widget().setEnabled(True)
            self.setCurrentIndex(pageIndex)

    def addPayCommission(self):
        title = QApplication.translate('DetailsTabWidget', "Commission")
        self.payComission = FormItem(None, title, Type.Money | Type.Disabled)

        self.items['payprice'].widget().textChanged.connect(self.payPriceChanged)
        self.items['totalpayprice'].widget().textChanged.connect(self.payPriceChanged)

        return self.payComission

    def payPriceChanged(self, text):
        totalPriceValue = self.items['totalpayprice'].value()
        if totalPriceValue:
            price = textToFloat(self.items['payprice'].value())
            totalPrice = textToFloat(totalPriceValue)
            self.payComission.widget().setText(floatToText(totalPrice - price))
        else:
            self.payComission.widget().setText('')

    def addSaleCommission(self):
        title = QApplication.translate('DetailsTabWidget', "Commission")
        self.saleComission = FormItem(None, title, Type.Money | Type.Disabled)

        self.items['saleprice'].widget().textChanged.connect(self.salePriceChanged)
        self.items['totalsaleprice'].widget().textChanged.connect(self.salePriceChanged)

        return self.saleComission

    def salePriceChanged(self, text):
        totalPriceValue = self.items['totalsaleprice'].value()
        if totalPriceValue:
            price = textToFloat(self.items['saleprice'].value())
            totalPrice = textToFloat(totalPriceValue)
            self.saleComission.widget().setText(floatToText(price - totalPrice))
        else:
            self.saleComission.widget().setText('')
class DetailsTabWidget(QTabWidget):
    Direction = QBoxLayout.LeftToRight
    Stretch = 'stretch item'

    def __init__(self, model, parent=None):
        super(DetailsTabWidget, self).__init__(parent)

        self.model = model

        self.createItems()
        self.createPages()

    def createPages(self):
        self.createCoinPage()
        self.createTrafficPage()
        self.createParametersPage()

    def createCoinPage(self):
        main = self.mainDetailsLayout()
        state = self.stateLayout()
        title = QApplication.translate('DetailsTabWidget', "Coin")
        self.addTabPage(title, [main, self.Stretch, state])

    def createTrafficPage(self):
        pass1 = self.pass1Layout()
        pass2 = self.pass2Layout()
        title = QApplication.translate('DetailsTabWidget', "Traffic")
        self.addTabPage(title, [pass1, self.Stretch, pass2])

    def createParametersPage(self):
        parameters = self.parametersLayout()

        catalogue = self.catalogueLayout()
        rarity = self.rarityLayout()
        variation = self.variationLayout()

        title = QApplication.translate('DetailsTabWidget', "Parameters")
        self.addTabPage(title, [parameters, self.Stretch, catalogue, rarity,
                                variation])

    def _layoutToWidget(self, layout):
        widget = QWidget(self)
        widget.setLayout(layout)
        return widget

    def createTabPage(self, parts):
        # Remove all empty parts
        for part in parts:
            if isinstance(part, BaseFormGroupBox):
                if part.isEmpty():
                    parts.remove(part)

        if self.Direction == QBoxLayout.LeftToRight:
            newParts = []
            layout = QVBoxLayout()
            stretchNeeded = True
            count = 0
            for part in parts:
                if part == self.Stretch:
                    if count > 0:
                        newParts.append(layout)
                        if stretchNeeded:
                            layout.insertStretch(-1)
                        layout = QVBoxLayout()
                    stretchNeeded = True
                    count = 0
                else:
                    if isinstance(part, QWidget):
                        layout.addWidget(part)
                        if part.sizePolicy().verticalPolicy() == QSizePolicy.Preferred:
                            stretchNeeded = False
                    else:
                        layout.addLayout(part)
                    count = count + 1
            if count > 0:
                newParts.append(layout)
                if stretchNeeded:
                    layout.insertStretch(-1)
            parts = newParts
        else:
            for part in parts:
                if part == self.Stretch:
                    parts.remove(part)

        pageLayout = QBoxLayout(self.Direction, self)
        # Fill layout with it's parts
        stretchNeeded = True
        for part in parts:
            if isinstance(part, QWidget):
                pageLayout.addWidget(part)
                if part.sizePolicy().verticalPolicy() == QSizePolicy.Preferred:
                    stretchNeeded = False
            else:
                pageLayout.addLayout(part)
                if isinstance(part, ImageFormLayout):
                    stretchNeeded = False

        if self.Direction == QBoxLayout.TopToBottom and stretchNeeded:
            pageLayout.insertStretch(-1)

        return self._layoutToWidget(pageLayout)

    def addTabPage(self, title, parts):
        page = self.createTabPage(parts)
        index = self.addTab(page, title)
        # Disable if empty
        if len(parts) == 0:
            self.setTabEnabled(index, False)

    def addItem(self, field):
        # Skip image fields for not a form
        if field.type in Type.ImageTypes:
            return

        item = FormItem(field.name, field.title, field.type | Type.Disabled)
        if not field.enabled:
            item.setHidden()
        self.items[field.name] = item

    def createItems(self):
        self.items = {}

        fields = self.model.fields
        for field in fields:
            if field not in fields.systemFileds:
                self.addItem(field)

    def fillItems(self, record):
        if not record.isEmpty():
            # Fields with commission dependent on status field and should be
            # filled after it and in right order
            ordered_item_keys = ['status', 'price', 'totalpayprice',
                                 'totalsaleprice']
            for key in ordered_item_keys:
                if key in self.items:
                    item = self.items[key]
                    self._fillItem(record, item)

            for item in self.items.values():
                if item.field() in ordered_item_keys:
                    continue

                self._fillItem(record, item)

    def _fillItem(self, record, item):
        if not record.isNull(item.field()):
            value = record.value(item.field())
            item.setValue(value)
        else:
            item.widget().clear()

    def clear(self):
        for item in self.items.values():
            item.widget().clear()

    def mainDetailsLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Main details")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['title'])
        layout.addRow(self.items['country'])
        layout.addRow(self.items['period'])
        layout.addRow(self.items['denomination'])
        layout.addRow(self.items['year'], self.items['mintmark'])
        layout.addRow(self.items['category'])
        layout.addRow(self.items['subject'])

        return layout

    def stateLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "State")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['grade'])
        layout.addRow(self.items['quantity'])

        return layout

    def pass1Layout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Pass")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['price'], self.items['date'])

        layout.addRow(self.items['bids'], self.items['bidders'])
        layout.addRow(self.items['place'], self.items['site'])
        layout.addRow(self.items['auctionnum'], self.items['lotnum'])
        layout.addRow(self.items['url'])

        return layout

    def pass2Layout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Pass details")
        layout = BaseFormGroupBox(title, parent)

        # Add auxiliary field
        item = self.addPayCommission()
        layout.addRow(self.items['totalpayprice'], item)

        # Add auxiliary field
        item = self.addSaleCommission()
        layout.addRow(self.items['totalsaleprice'], item)

        layout.addRow(self.items['saller'], self.items['buyer'])

        layout.addRow(self.items['info'])

        return layout

    def parametersLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Parameters")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['material'])
        layout.addRow(self.items['fineness'], self.items['weight'])
        layout.addRow(self.items['diameter'])

        return layout

    def rarityLayout(self, parent=None):
        layout = BaseFormLayout(parent)

        item = self.items['rarity']
        layout.addWidget(item.label(), 1, 0)
        layout.addWidget(item.widget(), 1, 1)
        layout.addWidget(QWidget(), 1, 2)
        item.widget().setSizePolicy(QSizePolicy.Preferred,
                                    QSizePolicy.Fixed)

        return layout

    def catalogueLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Catalogue")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['catalognum'])

        return layout

    def variationLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Variation")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['variety'])

        return layout

    def addPayCommission(self):
        title = QApplication.translate('DetailsTabWidget', "Commission")
        self.payComission = FormItem(None, title, Type.Money)

        self.items['price'].widget().textChanged.connect(self.payPriceChanged)
        self.items['totalpayprice'].widget().textChanged.connect(self.payPriceChanged)

        return self.payComission

    def payPriceChanged(self, text):
        price = textToFloat(self.items['price'].value())
        totalPrice = textToFloat(self.items['totalpayprice'].value())
        self.payComission.widget().setText(floatToText(totalPrice - price))

    def addSaleCommission(self):
        title = QApplication.translate('DetailsTabWidget', "Commission")
        self.saleComission = FormItem(None, title, Type.Money)

        self.items['price'].widget().textChanged.connect(self.salePriceChanged)
        self.items['totalsaleprice'].widget().textChanged.connect(self.salePriceChanged)

        return self.saleComission

    def salePriceChanged(self, text):
        price = textToFloat(self.items['price'].value())
        totalPrice = textToFloat(self.items['totalsaleprice'].value())
        self.saleComission.widget().setText(floatToText(price - totalPrice))
Ejemplo n.º 9
0
class DetailsTabWidget(QTabWidget):
    Direction = QBoxLayout.LeftToRight
    Stretch = 'stretch item'

    def __init__(self, model, parent=None):
        super(DetailsTabWidget, self).__init__(parent)

        self.model = model

        self.createItems()
        self.createPages()

    def createPages(self):
        self.createCoinPage()
        self.createTrafficPage()
        self.createParametersPage()
        self.createDesignPage()
        self.createClassificationPage()

    def createCoinPage(self):
        main = self.mainDetailsLayout()
        state = self.stateLayout()
        title = QApplication.translate('DetailsTabWidget', "Coin")
        self.addTabPage(title, [main, self.Stretch, state])

    def createTrafficPage(self):
        self.oldTrafficIndex = 0
        parts = self._createTrafficParts(self.oldTrafficIndex)
        title = QApplication.translate('DetailsTabWidget', "Traffic")
        self.addTabPage(title, parts)

    def createParametersPage(self):
        parameters = self.parametersLayout()
        minting = self.mintingLayout()
        note = self.noteLayout()

        title = QApplication.translate('DetailsTabWidget', "Parameters")
        self.addTabPage(title, [parameters, self.Stretch, minting, note])

    def createDesignPage(self):
        obverse = self.obverseDesignLayout()
        reverse = self.reverseDesignLayout()
        edge = self.edgeDesignLayout()
        subject = self.subjectLayout()

        title = QApplication.translate('DetailsTabWidget', "Design")
        self.addTabPage(title, [obverse, reverse, self.Stretch, edge, subject])

    def createClassificationPage(self):
        catalogue = self.catalogueLayout()
        rarity = self.rarityLayout()
        price = self.priceLayout()
        variation = self.variationLayout()
        url = self.urlLayout()

        title = QApplication.translate('DetailsTabWidget', "Classification")
        self.addTabPage(title, [catalogue, rarity, price, self.Stretch,
                                variation, url])

    def _layoutToWidget(self, layout):
        widget = QWidget(self)
        widget.setLayout(layout)
        return widget

    def createTabPage(self, parts):
        # Remove all empty parts
        for part in parts:
            if isinstance(part, BaseFormGroupBox):
                if part.isEmpty():
                    parts.remove(part)

        if self.Direction == QBoxLayout.LeftToRight:
            newParts = []
            layout = QVBoxLayout()
            stretchNeeded = True
            count = 0
            for part in parts:
                if part == self.Stretch:
                    if count > 0:
                        newParts.append(layout)
                        if stretchNeeded:
                            layout.insertStretch(-1)
                        layout = QVBoxLayout()
                    stretchNeeded = True
                    count = 0
                else:
                    if isinstance(part, QWidget):
                        layout.addWidget(part)
                        if part.sizePolicy().verticalPolicy() == QSizePolicy.Preferred:
                            stretchNeeded = False
                    else:
                        layout.addLayout(part)
                    count = count + 1
            if count > 0:
                newParts.append(layout)
                if stretchNeeded:
                    layout.insertStretch(-1)
            parts = newParts
        else:
            for part in parts:
                if part == self.Stretch:
                    parts.remove(part)

        pageLayout = QBoxLayout(self.Direction, self)
        # Fill layout with it's parts
        stretchNeeded = True
        for part in parts:
            if isinstance(part, QWidget):
                pageLayout.addWidget(part)
                if part.sizePolicy().verticalPolicy() == QSizePolicy.Preferred:
                    stretchNeeded = False
            else:
                pageLayout.addLayout(part)
                if isinstance(part, ImageFormLayout):
                    stretchNeeded = False

        if self.Direction == QBoxLayout.TopToBottom and stretchNeeded:
            pageLayout.insertStretch(-1)

        return self._layoutToWidget(pageLayout)

    def addTabPage(self, title, parts):
        page = self.createTabPage(parts)
        index = self.addTab(page, title)
        # Disable if empty
        if len(parts) == 0:
            self.setTabEnabled(index, False)

    def addItem(self, field):
        # Skip image fields for not a form
        if field.type in Type.ImageTypes:
            return

        item = FormItem(field.name, field.title, field.type | Type.Disabled)
        if not field.enabled:
            item.setHidden()
        self.items[field.name] = item

    def createItems(self):
        self.items = {}

        fields = self.model.fields
        for field in fields:
            if field not in fields.systemFields:
                self.addItem(field)

    def fillItems(self, record):
        if not record.isEmpty():
            # Fields with commission dependent on status field and should be
            # filled after it and in right order
            ordered_item_keys = ['status', 'payprice', 'totalpayprice',
                                 'saleprice', 'totalsaleprice']
            for key in ordered_item_keys:
                if key in self.items:
                    item = self.items[key]
                    self._fillItem(record, item)

            for item in self.items.values():
                if item.field() in ordered_item_keys:
                    continue

                self._fillItem(record, item)

    def _fillItem(self, record, item):
        if not record.isNull(item.field()):
            value = record.value(item.field())
            item.setValue(value)
        else:
            item.widget().clear()

    def clear(self):
        for item in self.items.values():
            item.widget().clear()

    def mainDetailsLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Main details")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['title'])
        layout.addRow(self.items['country'])
        layout.addRow(self.items['period'])
        layout.addRow(self.items['value'], self.items['unit'])
        layout.addRow(self.items['year'])
        layout.addRow(self.items['mintmark'], self.items['mint'])
        layout.addRow(self.items['type'])
        layout.addRow(self.items['series'])
        layout.addRow(self.items['subjectshort'])

        return layout

    def stateLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "State")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['status'], self.items['grade'])
        self.items['status'].widget().currentIndexChanged.connect(self.indexChangedState)
        layout.addRow(self.items['storage'])
        layout.addRow(self.items['quantity'], self.items['barcode'])
        layout.addRow(self.items['defect'])
        layout.addRow(self.items['features'])

        return layout

    def payLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Buy")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['paydate'], self.items['payprice'])

        # Add auxiliary field
        item = self.addPayCommission()

        layout.addRow(self.items['totalpayprice'], item)
        layout.addRow(self.items['saller'])
        layout.addRow(self.items['payplace'])
        layout.addRow(self.items['payinfo'])

        return layout

    def saleLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Sale")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['saledate'], self.items['saleprice'])

        # Add auxiliary field
        item = self.addSaleCommission()
        layout.addRow(self.items['totalsaleprice'], item)

        layout.addRow(self.items['buyer'])
        layout.addRow(self.items['saleplace'])
        layout.addRow(self.items['saleinfo'])

        return layout

    def passLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Pass")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['saledate'], self.items['saleprice'])

        # Add auxiliary field
        item = self.addPayCommission()
        layout.addRow(self.items['totalpayprice'], item)
        self.items['saleprice'].widget().textChanged.connect(self.items['payprice'].widget().setText)

        # Add auxiliary field
        item = self.addSaleCommission()
        layout.addRow(self.items['totalsaleprice'], item)

        layout.addRow(self.items['saller'])
        layout.addRow(self.items['buyer'])
        layout.addRow(self.items['saleplace'])
        layout.addRow(self.items['saleinfo'])

        return layout

    def parametersLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Parameters")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['material'])
        layout.addRow(self.items['fineness'], self.items['weight'])
        layout.addRow(self.items['diameter'], self.items['thickness'])
        layout.addRow(self.items['shape'])
        layout.addRow(self.items['obvrev'])

        return layout

    def mintingLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Minting")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['issuedate'], self.items['mintage'])
        layout.addRow(self.items['dateemis'])

        item = self.items['quality']
        layout.addHalfRow(item)
        item.widget().setSizePolicy(QSizePolicy.Preferred,
                                    QSizePolicy.Fixed)

        return layout

    def noteLayout(self, parent=None):
        layout = BaseFormLayout(parent)

        layout.addRow(self.items['note'])

        return layout

    def obverseDesignLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Obverse")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['obversedesign'])
        layout.addRow(self.items['obversedesigner'])

        return layout

    def reverseDesignLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Reverse")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['reversedesign'])
        layout.addRow(self.items['reversedesigner'])

        return layout

    def edgeDesignLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Edge")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['edge'])
        layout.addRow(self.items['edgelabel'])

        return layout

    def subjectLayout(self, parent=None):
        layout = BaseFormLayout(parent)

        layout.addRow(self.items['subject'])

        return layout

    def rarityLayout(self, parent=None):
        layout = BaseFormLayout(parent)

        item = self.items['rarity']
        layout.addHalfRow(item)
        item.widget().setSizePolicy(QSizePolicy.Preferred,
                                    QSizePolicy.Fixed)

        return layout

    def catalogueLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Catalogue")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['catalognum1'], self.items['catalognum2'])
        layout.addRow(self.items['catalognum3'], self.items['catalognum4'])

        return layout

    def priceLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Price")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['price1'], self.items['price2'])
        layout.addRow(self.items['price3'], self.items['price4'])

        return layout

    def variationLayout(self, parent=None):
        title = QApplication.translate('DetailsTabWidget', "Variation")
        layout = BaseFormGroupBox(title, parent)

        layout.addRow(self.items['variety'])
        layout.addRow(self.items['obversevar'], self.items['reversevar'])
        layout.addHalfRow(self.items['edgevar'])

        return layout

    def urlLayout(self, parent=None):
        layout = BaseFormLayout(parent)

        layout.addRow(self.items['url'])

        return layout

    def _createTrafficParts(self, index=0):
        pageParts = []
        if index == 0:
            pass
        elif index == 1:
            pass_ = self.passLayout()
            pageParts.append(pass_)
        elif index == 2:
            pay = self.payLayout()
            pageParts.append(pay)
        elif index == 3:
            pay = self.payLayout()
            pageParts.append(pay)
        elif index == 4:
            pay = self.payLayout()
            sale = self.saleLayout()
            pageParts.extend([pay, self.Stretch, sale])
        elif index == 5:
            pay = self.payLayout()
            pageParts.append(pay)
        elif index == 6:
            pass

        self.oldTrafficIndex = index

        return pageParts

    def indexChangedState(self, index):
        pageIndex = self.currentIndex()

        self.removeTab(1)
        pageParts = self._createTrafficParts(index)
        page = self.createTabPage(pageParts)

        title = QApplication.translate('DetailsTabWidget', "Traffic")
        self.insertTab(1, page, title)
        if len(pageParts) == 0:
            self.setTabEnabled(1, False)
#            self.items['grade'].widget().setEnabled(False)
            if pageIndex == 1:
                self.setCurrentIndex(pageIndex - 1)
        else:
#            self.items['grade'].widget().setEnabled(True)
            self.setCurrentIndex(pageIndex)

    def addPayCommission(self):
        title = QApplication.translate('DetailsTabWidget', "Commission")
        self.payComission = FormItem(None, title, Type.Money | Type.Disabled)

        self.items['payprice'].widget().textChanged.connect(self.payPriceChanged)
        self.items['totalpayprice'].widget().textChanged.connect(self.payPriceChanged)

        return self.payComission

    def payPriceChanged(self, text):
        totalPriceValue = self.items['totalpayprice'].value()
        if totalPriceValue:
            price = textToFloat(self.items['payprice'].value())
            totalPrice = textToFloat(totalPriceValue)
            self.payComission.widget().setText(floatToText(totalPrice - price))
        else:
            self.payComission.widget().setText('')

    def addSaleCommission(self):
        title = QApplication.translate('DetailsTabWidget', "Commission")
        self.saleComission = FormItem(None, title, Type.Money | Type.Disabled)

        self.items['saleprice'].widget().textChanged.connect(self.salePriceChanged)
        self.items['totalsaleprice'].widget().textChanged.connect(self.salePriceChanged)

        return self.saleComission

    def salePriceChanged(self, text):
        totalPriceValue = self.items['totalsaleprice'].value()
        if totalPriceValue:
            price = textToFloat(self.items['saleprice'].value())
            totalPrice = textToFloat(totalPriceValue)
            self.saleComission.widget().setText(floatToText(price - totalPrice))
        else:
            self.saleComission.widget().setText('')