예제 #1
0
    def __init__(self, parent, databaseConnection, item, action):
        QtGui.QDialog.__init__(self, parent)
        self.connection = databaseConnection
        self.item = item
        self.ui = Ui_ItemEditor()
        self.ui.setupUi(self)
        self.action = action
        if action == 'new': self.setWindowTitle('Adding new item')
        elif action == 'edit':
            if 'name' in self.item: self.setWindowTitle('Editing - ' + self.item['name'])
            else: self.setWindowTitle('Item editor - editing item')
        elif action == 'duplicate':
            if 'name' in self.item: self.setWindowTitle('Duplicating - ' + self.item['name'])
            else: self.setWindowTitle('Duplicating item')
        self.loadData(self.item)
        self.lastValidation = self.validate('all')
        
        #  /=====================================\
        #  |            SLOTS/SIGNALS            |
        self.ui.btnProcessTags.clicked.connect(self.itemTags_process)
        self.ui.buttonBox.button(QtGui.QDialogButtonBox.Save).clicked.connect(self.save)
        self.ui.buttonBox.button(QtGui.QDialogButtonBox.Cancel).clicked.connect(self.cancel)

        self.ui.itemName.textEdited.connect(self.validate_name)
        self.ui.itemCategory.currentIndexChanged.connect(self.validate_category)
        self.ui.itemContainer.currentIndexChanged.connect(self.validate_container)
        self.ui.itemAmount.valueChanged.connect(self.validate_amount)
        self.ui.itemAmountUnit.currentIndexChanged.connect(self.validate_amount)
        self.ui.itemFlags.itemChanged.connect(self.validate_flags)
        self.ui.itemDescription.textChanged.connect(self.validate_description)
        self.ui.itemTags.textChanged.connect(self.validate_tags)
        self.ui.itemBrand.textChanged.connect(self.validate_brand)
        self.ui.itemModel.textChanged.connect(self.validate_model)
        self.ui.itemVersion.textChanged.connect(self.validate_version)
        self.ui.itemSerialNumber.textChanged.connect(self.validate_serialNumber)
        self.ui.itemBuyDate.dateChanged.connect(self.validate_buyDate)
        self.ui.itemBuyPlace.textChanged.connect(self.validate_buyPlace)
        self.ui.itemWarranty.valueChanged.connect(self.validate_warranty)
        self.ui.itemPrice.valueChanged.connect(self.validate_price)
        self.ui.itemColors.itemChanged.connect(self.validate_colors)
        self.ui.itemMaterials.itemChanged.connect(self.validate_materials)
        self.ui.itemWeight.valueChanged.connect(self.validate_weight)
        self.ui.itemWidth.valueChanged.connect(self.validate_width)
        self.ui.itemHeight.valueChanged.connect(self.validate_height)
        self.ui.itemDepth.valueChanged.connect(self.validate_depth)
        self.ui.itemRadius1.valueChanged.connect(self.validate_radius1)
        self.ui.itemRadius2.valueChanged.connect(self.validate_radius2)
        self.ui.containerLabel.textEdited.connect(self.validate_containerLabel)
        self.ui.containerCategories.itemChanged.connect(self.validate_containerCategories)
예제 #2
0
class ItemEditor(QtGui.QDialog):
    def __init__(self, parent, databaseConnection, item, action):
        QtGui.QDialog.__init__(self, parent)
        self.connection = databaseConnection
        self.item = item
        self.ui = Ui_ItemEditor()
        self.ui.setupUi(self)
        self.action = action
        if action == 'new': self.setWindowTitle('Adding new item')
        elif action == 'edit':
            if 'name' in self.item: self.setWindowTitle('Editing - ' + self.item['name'])
            else: self.setWindowTitle('Item editor - editing item')
        elif action == 'duplicate':
            if 'name' in self.item: self.setWindowTitle('Duplicating - ' + self.item['name'])
            else: self.setWindowTitle('Duplicating item')
        self.loadData(self.item)
        self.lastValidation = self.validate('all')
        
        #  /=====================================\
        #  |            SLOTS/SIGNALS            |
        self.ui.btnProcessTags.clicked.connect(self.itemTags_process)
        self.ui.buttonBox.button(QtGui.QDialogButtonBox.Save).clicked.connect(self.save)
        self.ui.buttonBox.button(QtGui.QDialogButtonBox.Cancel).clicked.connect(self.cancel)

        self.ui.itemName.textEdited.connect(self.validate_name)
        self.ui.itemCategory.currentIndexChanged.connect(self.validate_category)
        self.ui.itemContainer.currentIndexChanged.connect(self.validate_container)
        self.ui.itemAmount.valueChanged.connect(self.validate_amount)
        self.ui.itemAmountUnit.currentIndexChanged.connect(self.validate_amount)
        self.ui.itemFlags.itemChanged.connect(self.validate_flags)
        self.ui.itemDescription.textChanged.connect(self.validate_description)
        self.ui.itemTags.textChanged.connect(self.validate_tags)
        self.ui.itemBrand.textChanged.connect(self.validate_brand)
        self.ui.itemModel.textChanged.connect(self.validate_model)
        self.ui.itemVersion.textChanged.connect(self.validate_version)
        self.ui.itemSerialNumber.textChanged.connect(self.validate_serialNumber)
        self.ui.itemBuyDate.dateChanged.connect(self.validate_buyDate)
        self.ui.itemBuyPlace.textChanged.connect(self.validate_buyPlace)
        self.ui.itemWarranty.valueChanged.connect(self.validate_warranty)
        self.ui.itemPrice.valueChanged.connect(self.validate_price)
        self.ui.itemColors.itemChanged.connect(self.validate_colors)
        self.ui.itemMaterials.itemChanged.connect(self.validate_materials)
        self.ui.itemWeight.valueChanged.connect(self.validate_weight)
        self.ui.itemWidth.valueChanged.connect(self.validate_width)
        self.ui.itemHeight.valueChanged.connect(self.validate_height)
        self.ui.itemDepth.valueChanged.connect(self.validate_depth)
        self.ui.itemRadius1.valueChanged.connect(self.validate_radius1)
        self.ui.itemRadius2.valueChanged.connect(self.validate_radius2)
        self.ui.containerLabel.textEdited.connect(self.validate_containerLabel)
        self.ui.containerCategories.itemChanged.connect(self.validate_containerCategories)
        #  |            SLOTS/SIGNALS            |
        #  \=====================================/



    ################################################################
    #### GETTERS/SETTERS ###########################################
    ################################################################

    def getName(self):
        return self.ui.itemName.text()
    def setName(self, name):
        self.ui.itemName.setText(name)
    def getCategory(self):
        return getSelectedText(self.ui.itemCategory)
    def setCategory(self, category):
        selectOption(self.ui.itemCategory, category)
    def getContainer(self):
        return getSelectedText(self.ui.itemContainer)
    def setContainer(self, container):
        selectOption(self.ui.itemContainer, container)
    def getAmount(self):
        return self.ui.itemAmount.value()
    def setAmount(self, amount):
        self.ui.itemAmount.setValue(amount)
    def getAmountUnit(self):
        return getSelectedText(self.ui.itemAmountUnit)
    def setAmountUnits(self, units):
        selectOption(self.ui.itemAmountUnit, units)
    def getFlags(self):
        return getCheckedText(self.ui.itemFlags)
    def setFlags(self, flags, checks = None):
        checkOptions(self.ui.itemFlags, flags, checks)
    def getDescription(self):
        return self.ui.itemDescription.toPlainText()
    def setDescription(self, description):
        self.ui.itemDescription.setPlainText(description)
    def getTags(self):
        text = self.ui.itemTags.toPlainText().toLower()
        tagsSet = set()
        lines = text.split('\n')
        for line in lines:
            tags = line.split(',')
            if type(tags) is QStringList:
                for tag in tags:
                    trimmed = tag.trimmed()
                    if len(trimmed) != 0:
                        tagsSet.add(trimmed)
        return sorted(tagsSet)
    def setTags(self, tags):
        print 'Got tags:', tags
        text = ''
        if len(tags) > 0:
            text += tags.pop(0)
        for tag in tags:
            text += '\n' + tag
        self.ui.itemTags.setPlainText(text)
    def getBrand(self):
        return self.ui.itemBrand.text()
    def setBrand(self, brand):
        self.ui.itemBrand.setText(brand)
    def getModel(self):
        return self.ui.itemModel.text()
    def setModel(self, model):
        self.ui.itemModel.setText(model)
    def getVersion(self):
        return self.ui.itemVersion.text()
    def setVersion(self, version):
        self.ui.itemVersion.setText(version)
    def getSerialNumber(self):
        return self.ui.itemSerialNumber.text()
    def setSerialNumber(self, serialNumber):
        self.ui.itemSerialNumber.setText(serialNumber)
    def getBuyDate(self):
        return self.ui.itemBuyDate.date()
    def setBuyDate(self, datetime):
        self.ui.itemBuyDate.setDate(datetime)
    def getBuyPlace(self):
        return self.ui.itemBuyPlace.text()
    def setBuyPlace(self, buyPlace):
        self.ui.itemBuyPlace.setText(buyPlace)
    def getWarranty(self):
        return self.ui.itemWarranty.value()
    def setWarranty(self, warranty):
        self.ui.itemWarranty.setValue(warranty)
    def getPrice(self):
        return self.ui.itemPrice.value()
    def setPrice(self, price):
        self.ui.itemPrice.setValue(price)
    def getColors(self):
        return getCheckedText(self.ui.itemColors)
    def setColors(self, colors, checks = None):
        checkOptions(self.ui.itemColors, colors, checks)
    def getMaterials(self):
        return getCheckedText(self.ui.itemMaterials)
    def setMaterials(self, materials, checks = None):
        checkOptions(self.ui.itemMaterials, materials, checks)
    def getWeight(self):
        return self.ui.itemWeight.value()
    def setWeight(self, weight):
        self.ui.itemWeight.setValue(weight)
    def getWidth(self):
        return self.ui.itemWidth.value()
    def setWidth(self, width):
        self.ui.itemWidth.setValue(width)
    def getHeight(self):
        return self.ui.itemHeight.value()
    def setHeight(self, height):
        self.ui.itemHeight.setValue(height)
    def getDepth(self):
        return self.ui.itemDepth.value()
    def setDepth(self, depth):
        self.ui.itemDepth.setValue(depth)
    def getRadius1(self):
        return self.ui.itemRadius1.value()
    def setRadius1(self, radius1):
        self.ui.itemRadius1.setValue(radius1)
    def getRadius2(self):
        return self.ui.itemRadius2.value()
    def setRadius2(self, radius2):
        self.ui.itemRadius2.setValue(radius2)
    def getContainerLabel(self):
        return self.ui.containerLabel.text()
    def setContainerLabel(self, label):
        self.ui.containerLabel.setText(label)
    def getContainerCategories(self):
        return getCheckedText(self.ui.containerCategories)
    def setContainerCategories(self, categories, checks = None):
        checkOptions(self.ui.containerCategories, categories, checks)

    ################################################################
    #### LOADING ###################################################
    ################################################################

    def loadContainers(self, currentFrom = 'item'):
        category = unicode(getSelectedText(self.ui.itemCategory))
        if category == 'None': category = None
        toSelect = 'None'
        toSelect = 'None'
        if currentFrom == 'item':
            if self.item is not None and 'container' in self.item:
                toSelect = self.item['container']
        elif currentFrom == 'selection':
            toSelect = getSelectedText(self.ui.itemContainer)
        setupCombobox(self.ui.itemContainer, self.connection.getContainers(category = category))
        self.setContainer(toSelect)



    def loadData(self, item):
        ## ID
        if item is not None and '_id' in item:
            self.ui.labelID.setText(str(item['_id']))
        else: self.ui.labelID.setText('---')
        ## Created
        if item is not None and 'created' in item:
            self.ui.labelCreated.setText(str(item['created']))
        else: self.ui.labelCreated.setText('---')
        ## Modified
        if item is not None and 'modified' in item:
            self.ui.labelModified.setText(str(item['modified']))
        else: self.ui.labelModified.setText('---')
        ## Name
        if item is not None and 'name' in item:
            self.setName(item['name'])
        else: self.setName('')
        ## Categories
        if item is not None and 'category' in item:
            setupCombobox(self.ui.itemCategory, self.connection.getCategories())
            self.setCategory(item['category'])
        else: setupCombobox(self.ui.itemCategory, self.connection.getCategories())
        ## Container
        self.loadContainers()
        ## Amount
        if item is not None and 'amount' in item:
            self.setAmount(item['amount'])
        else: self.setAmount(1.0)
        ## Amount units
        if item is not None and 'amountUnit' in item:
            setupCombobox(self.ui.itemAmountUnit, self.connection.getUnits())
            self.setAmountUnits(item['amountUnit'])
        else: setupCombobox(self.ui.itemAmountUnit, self.connection.getUnits())
        ## Flags
        if item is not None and 'flags' in item:
            checks = []
            for flag in self.connection.getFlags():
                checks.append(flag in item['flags'])
            setupChecklist(self.ui.itemFlags, self.connection.getFlags(), checks = checks)
        else: setupChecklist(self.ui.itemFlags, self.connection.getFlags())
        ## Description
        if item is not None and 'description' in item:
            self.setDescription(item['description'])
        else: self.setDescription('')
        ## Tags
        if item is not None and 'tags' in item:
            self.setTags(item['tags'])
        ## Brand
        if item is not None and 'brand' in item:
            self.setBrand(item['brand'])
        else: self.setBrand('')
        ## Model
        if item is not None and 'model' in item:
            self.setModel(item['model'])
        else: self.setModel('')
        ## Version
        if item is not None and 'version' in item:
            self.setVersion(item['version'])
        else: self.setVersion('')
        ## Serial number
        if item is not None and 'serialNumber' in item:
            self.setSerialNumber(item['serialNumber'])
        else: self.setSerialNumber('')
        ## Buy date
        if item is not None and 'buyDate' in item:
            self.setBuyDate(item['buyDate'])
        else: self.setBuyDate(QDate(1800,1,1))
        ## Buy place
        if item is not None and 'buyPlace' in item:
            self.setBuyPlace(item['buyPlace'])
        else: self.setBuyPlace('')
        ## Warranty
        if item is not None and 'warranty' in item:
            self.ui.itemWarranty.setValue(item['warranty'])
        else: self.ui.itemWarranty.setValue(0)
        ## Price
        if item is not None and 'price' in item:
            self.ui.itemPrice.setValue(item['price'])
        else: self.ui.itemPrice.setValue(0.0)
        ## Colors
        if item is not None and 'colors' in item:
            checks = []
            for color in self.connection.getColors():
                checks.append(color in item['colors'])
            setupChecklist(self.ui.itemColors, self.connection.getColors(), checks = checks)
        else: setupChecklist(self.ui.itemColors, self.connection.getColors())
        ## Materials
        if item is not None and 'materials' in item:
            checks = []
            for material in self.connection.getMaterials():
                checks.append(material in item['materials'])
            setupChecklist(self.ui.itemMaterials, self.connection.getMaterials(), checks = checks)
        else: setupChecklist(self.ui.itemMaterials, self.connection.getMaterials())
        ## Weight
        if item is not None and 'weight' in item:
            self.ui.itemWeight.setValue(item['weight'])
        else: self.ui.itemWeight.setValue(0.0)
        ## Width
        if item is not None and 'width' in item:
            self.ui.itemWidth.setValue(item['width'])
        else: self.ui.itemWidth.setValue(0.0)
        ## Height
        if item is not None and 'height' in item:
            self.ui.itemHeight.setValue(item['height'])
        else: self.ui.itemHeight.setValue(0.0)
        ## Depth
        if item is not None and 'depth' in item:
            self.ui.itemDepth.setValue(item['depth'])
        else: self.ui.itemDepth.setValue(0.0)
        ## Radius 1
        if item is not None and 'radius1' in item:
            self.ui.itemRadius1.setValue(item['radius1'])
        else: self.ui.itemRadius1.setValue(0.0)
        ## Radius 2
        if item is not None and 'radius2' in item:
            self.ui.itemRadius2.setValue(item['radius2'])
        else: self.ui.itemRadius2.setValue(0.0)
        ## Container label
        if item is not None and 'containerLabel' in item:
            self.setContainerLabel(item['containerLabel'])
        else: self.setContainerLabel('')
        ## Categories for container
        if item is not None and 'containerCategories' in item:
            checks = []
            for category in self.connection.getCategories(none = False):
                checks.append(category in item['containerCategories'])
            setupChecklist(self.ui.containerCategories, self.connection.getCategories(none = False), checks = checks)
        else: setupChecklist(self.ui.containerCategories, self.connection.getCategories(none = False))




    ################################################################
    #### PROCESSING ################################################
    ################################################################

    def itemTags_process(self):
        self.setTags(self.getTags())


    def validate(self, field):
        failed = []
        if field == 'all' or field == 'name':
            if len(self.getName()) < 5:
                colorizeLabel(self.ui.labelName, 'error')
                failed.append('name')
            else: colorizeLabel(self.ui.labelName, 'pass')
        if field == 'all' or field == 'category':
            if self.getCategory() in self.connection.getCategories() and self.getCategory() != 'None':
                colorizeLabel(self.ui.labelCategory, 'pass')
            else:
                colorizeLabel(self.ui.labelCategory, 'error')
                failed.append('category')
        if field == 'all' or field == 'container':
            if self.getContainer() in self.connection.getContainers() and self.getContainer() != 'None':
                colorizeLabel(self.ui.labelContainer, 'pass')
            elif self.getContainer() == 'None': colorizeLabel(self.ui.labelContainer, 'optional')
            else:
                colorizeLabel(self.ui.labelContainer, 'error')
                failed.append('container')
        if field == 'all' or field == 'amount':
            if self.getAmount() > 0.0 and self.getAmountUnit() in self.connection.getUnits():
                colorizeLabel(self.ui.labelAmount, 'pass')
            else:
                colorizeLabel(self.ui.labelAmount, 'error')
                failed.append('amount')
        if field == 'all' or field == 'flags':
            if len(self.getContainerLabel()) > 0 and 'container' not in self.getFlags():
                colorizeLabel(self.ui.labelFlags, 'error')
                failed.append('flags')
            else: colorizeLabel(self.ui.labelFlags, 'pass')
        if field == 'all' or field == 'description':
            if len(self.getDescription()) <= 0:
                colorizeLabel(self.ui.labelDescription, 'disabled')
            else: colorizeLabel(self.ui.labelDescription, 'pass')
        if field == 'all' or field == 'tags':
            if len(self.getTags()) > 0:
                colorizeLabel(self.ui.labelTags, 'pass')
            else: colorizeLabel(self.ui.labelTags, 'disabled')
        if field == 'all' or field == 'brand':
            if len(self.getBrand()) > 0:
                colorizeLabel(self.ui.labelBrand, 'pass')
            else: colorizeLabel(self.ui.labelBrand, 'disabled')
        if field == 'all' or field == 'model':
            if len(self.getModel()) > 0:
                colorizeLabel(self.ui.labelModel, 'pass')
            else: colorizeLabel(self.ui.labelModel, 'disabled')
        if field == 'all' or field == 'version':
            if len(self.getVersion()) > 0:
                colorizeLabel(self.ui.labelVersion, 'pass')
            else: colorizeLabel(self.ui.labelVersion, 'disabled')
        if field == 'all' or field == 'serialNumber':
            if len(self.getSerialNumber()) > 0:
                colorizeLabel(self.ui.labelSerialNumber, 'pass')
            else: colorizeLabel(self.ui.labelSerialNumber, 'disabled')
        if field == 'all' or field == 'buyDate':
            if self.getBuyDate() == QDate(1800,1,1):
                colorizeLabel(self.ui.labelBuyDate, 'disabled')
            else: colorizeLabel(self.ui.labelBuyDate, 'pass')
        if field == 'all' or field == 'buyPlace':
            if len(self.getBuyPlace()) > 0:
                colorizeLabel(self.ui.labelBuyPlace, 'pass')
            else: colorizeLabel(self.ui.labelBuyPlace, 'disabled')
        if field == 'all' or field == 'warranty':
            if self.getWarranty() < 0:
                colorizeLabel(self.ui.labelWarranty, 'error')
                failed.append('warranty')
            elif self.getWarranty() == 0:
                colorizeLabel(self.ui.labelWarranty, 'disabled')
            else: colorizeLabel(self.ui.labelWarranty, 'pass')
        if field == 'all' or field == 'price':
            if self.getPrice() < 0.0:
                colorizeLabel(self.ui.labelPrice, 'error')
                failed.append('price')
            elif self.getPrice() == 0.0:
                colorizeLabel(self.ui.labelPrice, 'disabled')
            else: colorizeLabel(self.ui.labelPrice, 'pass')
        if field == 'all' or field == 'colors':
            if len(self.getColors()) > 0:
                colorizeLabel(self.ui.labelColors, 'pass')
            else: colorizeLabel(self.ui.labelColors, 'disabled')
        if field == 'all' or field == 'materials':
            if len(self.getMaterials()) > 0:
                colorizeLabel(self.ui.labelMaterials, 'pass')
            else: colorizeLabel(self.ui.labelMaterials, 'disabled')
        if field == 'all' or field == 'weight':
            if self.getWeight() < 0.0:
                colorizeLabel(self.ui.labelWeight, 'error')
                failed.append('weight')
            elif self.getWeight() == 0.0:
                colorizeLabel(self.ui.labelWeight, 'disabled')
            else: colorizeLabel(self.ui.labelWeight, 'pass')
        if field == 'all' or field == 'width':
            if self.getWidth() < 0.0:
                colorizeLabel(self.ui.labelWidth, 'error')
                failed.append('width')
            elif self.getWidth() == 0.0:
                colorizeLabel(self.ui.labelWidth, 'disabled')
            else: colorizeLabel(self.ui.labelWidth, 'pass')
        if field == 'all' or field == 'height':
            if self.getHeight() < 0.0:
                colorizeLabel(self.ui.labelHeight, 'error')
                failed.append('height')
            elif self.getHeight() == 0.0:
                colorizeLabel(self.ui.labelHeight, 'disabled')
            else: colorizeLabel(self.ui.labelHeight, 'pass')
        if field == 'all' or field == 'depth':
            if self.getDepth() < 0.0:
                colorizeLabel(self.ui.labelDepth, 'error')
                failed.append('depth')
            elif self.getDepth() == 0.0:
                colorizeLabel(self.ui.labelDepth, 'disabled')
            else: colorizeLabel(self.ui.labelDepth, 'pass')
        if field == 'all' or field == 'radius1':
            if self.getRadius1() < 0.0:
                colorizeLabel(self.ui.labelRadius1, 'error')
                failed.append('radius1')
            elif self.getRadius1() == 0.0:
                colorizeLabel(self.ui.labelRadius1, 'disabled')
            else: colorizeLabel(self.ui.labelRadius1, 'pass')
        if field == 'all' or field == 'radius2':
            if self.getRadius2() < 0.0:
                colorizeLabel(self.ui.labelRadius2, 'error')
                failed.append('radius2')
            elif self.getRadius2() == 0.0:
                colorizeLabel(self.ui.labelRadius2, 'disabled')
            else: colorizeLabel(self.ui.labelRadius2, 'pass')
        if field == 'all' or field == 'containerLabel':
            if len(self.getContainerLabel()) == 0:
                colorizeLabel(self.ui.labelContainerLabel, 'disabled')
            elif len(self.getContainerLabel()) < 3:
                colorizeLabel(self.ui.labelContainerLabel, 'error')
                failed.append('name')
            elif len(self.getContainerLabel()) >= 3:
                sameLabel = self.connection.getContainer(unicode(self.getContainerLabel()))
                if sameLabel is None:
                    colorizeLabel(self.ui.labelContainerLabel, 'pass')
                elif '_id' in self.item:
                    if self.item['_id'] == sameLabel['_id']:
                        colorizeLabel(self.ui.labelContainerLabel, 'pass')
                    else:
                        colorizeLabel(self.ui.labelContainerLabel, 'error')
                        failed.append('containerLabel')
                else:
                    colorizeLabel(self.ui.labelContainerLabel, 'error')
                    failed.append('containerLabel')
        if field == 'all' or field == 'containerCategories':
            if len(self.getContainerLabel()) > 0:
                if len(self.getContainerCategories()) == 0:
                    colorizeLabel(self.ui.labelContainerCategories, 'error')
                    failed.append('containerCategories')
                else:
                    colorizeLabel(self.ui.labelContainerCategories, 'pass')
            elif len(self.getContainerCategories()) == 0:
                colorizeLabel(self.ui.labelContainerCategories, 'disabled')
            else:
                colorizeLabel(self.ui.labelContainerCategories, 'pass')

        return (len(failed)==0, failed)
    

    def validate_name(self):
        self.validate('name')
    def validate_category(self):
        if self.validate('category'):
            self.loadContainers(currentFrom = 'selection')
    def validate_container(self):
        self.validate('container')
    def validate_amount(self):
        self.validate('amount')
    def validate_flags(self):
        self.validate('flags')
    def validate_description(self):
        self.validate('description')
    def validate_tags(self):
        self.validate('tags')
    def validate_brand(self):
        self.validate('brand')
    def validate_model(self):
        self.validate('model')
    def validate_version(self):
        self.validate('version')
    def validate_serialNumber(self):
        self.validate('serialNumber')
    def validate_buyDate(self):
        self.validate('buyDate')
    def validate_buyPlace(self):
        self.validate('buyPlace')
    def validate_warranty(self):
        self.validate('warranty')
    def validate_price(self):
        self.validate('price')
    def validate_colors(self):
        self.validate('colors')
    def validate_materials(self):
        self.validate('materials')
    def validate_weight(self):
        self.validate('weight')
    def validate_width(self):
        self.validate('width')
    def validate_height(self):
        self.validate('height')
    def validate_depth(self):
        self.validate('depth')
    def validate_radius1(self):
        self.validate('radius1')
    def validate_radius2(self):
        self.validate('radius2')
    def validate_containerLabel(self):
        self.validate('containerLabel')
        self.validate('containerCategories')
        self.validate('flags')
    def validate_containerCategories(self):
        self.validate('containerCategories')



    ################################################################
    #### FINISH ####################################################
    ################################################################

    def updateItem(self):
        _id = None
        if '_id' in self.item:
            _id = self.item['_id']
        created = None
        if 'created' in self.item:
            created = self.item['created']

        self.item.clear()
        if _id is not None:
            self.item['_id'] = _id
        if created is None:
            self.item['created'] = datetime.datetime.now()
        else: self.item['created'] = created
        self.item['modified'] = datetime.datetime.now()

        self.item['programVersion'] = settings.programVersion

        name = self.getName()
        if name is not None and name != '': self.item['name'] = name

        category = getSelectedText(self.ui.itemCategory)
        if category is not None and category != '': self.item['category'] = category

        container = getSelectedText(self.ui.itemContainer)
        if container is not None and container != '': self.item['container'] = container

        amount = self.getAmount()
        if amount is not None and amount != '': self.item['amount'] = amount

        amountUnit = self.getAmountUnit()
        if amountUnit is not None and amountUnit != '': self.item['amountUnit'] = amountUnit

        flags = self.getFlags()
        if flags is not None and len(flags) > 0: self.item['flags'] = flags
        
        description = self.getDescription()
        if description is not None and description != '': self.item['description'] = description
        
        tags = self.getTags()
        if tags is not None and len(tags) > 0: self.item['tags'] = tags

        brand = self.getBrand()
        if brand is not None and brand != '': self.item['brand'] = brand
        model = self.getModel()
        if model is not None and model != '': self.item['model'] = model
        version = self.getVersion()
        if version is not None and version != '': self.item['version'] = version
        serialNumber = self.getSerialNumber()
        if serialNumber is not None and serialNumber != '': self.item['serialNumber'] = serialNumber

        buyDate = self.getBuyDate()
        if buyDate is not None and buyDate != QDate(1800,1,1): self.item['buyDate'] = buyDate
        buyPlace = self.getBuyPlace()
        if buyPlace is not None and buyPlace != '': self.item['buyPlace'] = buyPlace
        warranty = self.ui.itemWarranty.value()
        if warranty is not None and warranty > 0: self.item['warranty'] = warranty
        price = self.ui.itemPrice.value()
        if price is not None and price > 0.0: self.item['price'] = price
        
        colors = getCheckedText(self.ui.itemColors)
        if colors is not None and len(colors) > 0: self.item['colors'] = colors
        materials = getCheckedText(self.ui.itemMaterials)
        if materials is not None and len(materials) > 0: self.item['materials'] = materials
        
        weight = self.ui.itemWeight.value()
        if weight is not None and weight > 0.0: self.item['weight'] = weight
        width = self.ui.itemWidth.value()
        if width is not None and width > 0.0: self.item['width'] = width
        height = self.ui.itemHeight.value()
        if height is not None and height > 0.0: self.item['height'] = height
        depth = self.ui.itemDepth.value()
        if depth is not None and depth > 0.0: self.item['depth'] = depth
        radius1 = self.ui.itemRadius1.value()
        if radius1 is not None and radius1 > 0.0: self.item['radius1'] = radius1
        radius2 = self.ui.itemRadius2.value()
        if radius2 is not None and radius2 > 0.0: self.item['radius2'] = radius2
        
        containerLabel = self.getContainerLabel()
        if containerLabel is not None and containerLabel != '': self.item['containerLabel'] = containerLabel
        containerCategories = getCheckedText(self.ui.containerCategories)
        if containerCategories is not None and len(containerCategories) > 0: self.item['containerCategories'] = containerCategories



    def save(self):
        update = True
        validation = self.validate('all')
        if validation[0]:
            if 'containerLabel' in self.item and self.action == 'edit':
                if self.getContainerLabel() != self.item['containerLabel']:
                    msg = QMessageBox()
                    msg.setWindowTitle("Container label rename")
                    msg.setIcon(QMessageBox.Warning)
                    msg.setText("You are trying to rename container label. Currently renaming is not supported. Do you want to proceed?")
                    msg.setInformativeText("All items in this container will be broken, because of invalid label.")
                    msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
                    if msg.exec_() == 65536: # 16384 = Yes; 65536 = No
                        update = False
            
        else:
            msg = QMessageBox()
            msg.setWindowTitle("Validation error")
            msg.setIcon(QMessageBox.Warning)
            msg.setText("Cannot save this item. It didn't pass validation!")
            failed = validation[1].pop(0)
            for fail in validation[1]:
                failed += ',\n' + fail
            msg.setDetailedText(failed)
            msg.setStandardButtons(QMessageBox.Ok)
            msg.exec_()
            update = False

        if update:
            self.updateItem()
            self.accept()

    def cancel(self):
        self.reject()





##########################################################################
##########################################################################
##########################################################################
###### APPLICATION START #################################################
##########################################################################
##########################################################################
##########################################################################

#if __name__ == "__main__":
#    app = QtGui.QApplication(sys.argv)
#    appWindow = MainWindow()
#    appWindow.show()
#    sys.exit(app.exec_())