def setData(self, index, value, role): """ :param index: :param value: :type value: QVariant :param role: :return: """ if role != Qt.EditRole: return True if not index.isValid(): return True model = self.record(index.row(), index.internalPointer()) if not model: return True field = self.field(index.column()) fieldType = self.fieldType(index.column(), index.internalPointer()) if fieldType == 'boolean': model.setValue(field, bool(value)) elif fieldType in ('float', 'float_time'): model.setValue(field, value.value()) elif fieldType == 'integer': model.setValue(field, value.toInt()[0]) elif fieldType == 'selection': value = str(value.toString()) modelField = self.fields[self.field(index.column())] for x in modelField['selection']: if x[1] == value: model.setValue(field, x[0]) elif fieldType in ('char', 'text'): model.setValue(field, str(value)) elif fieldType == 'date': model.setValue(field, Calendar.dateToStorage(value.toDate())) elif fieldType == 'datetime' and value: model.setValue(field, Calendar.dateTimeToStorage(value.value())) elif fieldType == 'time' and value: model.setValue(field, Calendar.timeToStorage(value.toTime())) elif fieldType == 'many2many': m = model.value(field) m.clear() ids = [x.toInt()[0] for x in value.toList()] m.load(ids) elif fieldType == 'many2one': value = value.toList() if value: value = [int(value[0].toInt()[0]), str(value[1].toString())] model.setValue(field, value) else: print("Unable to store value of type: ", fieldType) return True
def updateCalendarData(self): if not self._model: return if self._modelDateColumn >= self._model.columnCount(): return if self._modelTitleColumn >= self._model.columnCount(): return for x in list(self._days.values()): x.clear() # # Filter the records, so only those in the current* month/week/day # are processed (just the ones that will be shown). # # (*) We use a hack, to get events that span from the previous # month, so we really are processing one month more than the # days shown :( # startField = self._model.field(self._modelDateColumn) durationField = self._model.field(self._modelDurationColumn) if startField == durationField: startDate = Calendar.dateToStorage(self.startDate()) else: # TODO: Based on web client hack to get spanded events. # A better solution should be used someday... startDate = Calendar.dateToStorage(self.startDate().addMonths(-1)) endDate = Calendar.dateToStorage(self.endDate()) oldFilter = self._model.group.filter() or [] newFilter = [] # Ignore any (start_field, ..., ...) item from the filter. for x in oldFilter: if x[0] != startField: newFilter.append(x) # Add the start field restrictions to the new filter newFilter.extend([ (startField, '>=', startDate), (startField, '<=', endDate), ]) self._model.group.setFilter(newFilter) # Get the data for x in range(self._model.rowCount()): idx = self._model.index(x, self._modelDateColumn) date = Calendar.dateToText(self.dateTimeFromIndex(idx).date()) if date in self._days: idx = self._model.index(x, self._modelTitleColumn) self._days[date].addModelIndex(idx) # Restore the old filter self._model.group.setFilter(oldFilter)
def scan(model, id, ids, context): Directories = 1 Files = 0 Cancel = 2 dialog = QMessageBox() dialog.setWindowTitle(_('Import Documents')) dialog.setText(_('How do you want to import documents?')) dialog.addButton(_('Select Files'), 3) dialog.addButton(_('Selected Directory'), 2) dialog.addButton(_('Cancel Import'), 1) result = dialog.exec_() if result == Cancel: return if result == Directories: directory = str(QFileDialog.getExistingDirectory()) if not directory: return fileNames = QDir(directory).entryList() fileNames = [os.path.join(directory, str(x)) for x in fileNames] else: fileNames = QFileDialog.getOpenFileNames() fileNames = [str(x) for x in fileNames] for fileName in fileNames: try: # As fileName may not be a file, simply try the next file. f = open(fileName, 'rb') except: continue try: data = base64.encodestring(f.read()) except: continue finally: f.close() id = Rpc.session.execute( '/object', 'execute', 'nan.document', 'create', { 'name': Calendar.dateTimeToText(QDateTime.currentDateTime()), 'datas': data, 'filename': os.path.basename(fileName), }, context)
def updateCalendarView(self): self.clear() if not self.daysCount(): return daysPerRow = 7 offset = self._startDate.dayOfWeek() - 1 rows = math.ceil( ( offset + self.daysCount() ) / 7.0 ) if rows == 1: offset = 0 dayWidth = self._size.width() / min( daysPerRow, self.daysCount() ) dayHeight = self._size.height() / rows date = self._startDate for x in range( self._startDate.daysTo( self._endDate ) + 1 ): item = GraphicsDayItem( self ) item.setDate( date ) item.setPos( (x+offset) % 7 * dayWidth, dayHeight * ( (x+offset) // 7 ) ) item.setSize( QSize( dayWidth, dayHeight ) ) self._days[ str(Calendar.dateToText( date )) ] = item date = date.addDays( 1 ) self.updateCalendarData()
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()
def correctValue(self, value, fieldName, relatedFieldName): text = value if relatedFieldName: fieldType = self.relatedFields[relatedFieldName].get('type') else: fieldType = self.fields[fieldName].get('type') if fieldType == 'date': value = Calendar.textToDate(value) text = Calendar.dateToText(value) value = Calendar.dateToStorage(value) elif fieldType == 'datetime': value = Calendar.textToDateTime(value) text = Calendar.dateTimeToText(value) value = Calendar.dateTimeToStorage(value) elif fieldType == 'float_time': value = Calendar.textToFloatTime(value) text = Calendar.floatTimeToText(value) value = Calendar.floatTimeToStorage(value) elif fieldType == 'time': value = Calendar.textToTime(value) text = Calendar.timeToText(value) value = Calendar.timeToStorage(value) elif fieldType == 'float': value = Numeric.textToFloat(value) or 0.0 text = Numeric.floatToText(value) elif fieldType == 'integer': value = Numeric.textToInteger(value) or 0.0 text = Numeric.integerToText(value) elif fieldType == 'selection': if relatedFieldName: options = self.relatedFields[relatedFieldName]['selection'] else: options = self.fields[fieldName]['selection'] text = [] keys = [] for selection in options: # If text matches exactly one of the options of the selection field # do not 'accept' other options. Otherwise, all options that contain # the text will be 'accepted'. if value.lower().strip() == selection[1].lower().strip(): keys = [selection[0]] text = [selection[1]] break if value.lower() in selection[1].lower(): keys.append(selection[0]) text.append(selection[1]) value = keys text = ', '.join(text) elif fieldType == 'boolean': options = [ (True, _('True')), (False, _('False')), ] text = '' for selection in options: if value.lower() in selection[1].lower(): value = selection[0] text = selection[1] break if not text: value = options[1][0] text = options[1][1] return (value, text)
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(unicode(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: