def _do(self, new, old): if new.toString() == '': new = QVariant() elif new.toString() == '""': new = QVariant(QString('')) self.model.rows[self.row][self.column] = new if self.column == 0: # commented or uncommented index1 = self.model.index(self.row, 0) index2 = self.model.index(self.row, self.model.columnCount()-1) elif self.column==1: # we have just changed the object name, other objects might # reference it index1 = self.model.index(0, 0) index2 = self.model.index( self.model.rowCount()-1, self.model.columnCount()-1) else: # just changed this cell index1 = self.model.index(self.row, self.column) index2 = index1 self.model.emit( SIGNAL('dataChanged(const QModelIndex &, const QModelIndex &)'), index1, index2)
def _do(self, new, old): if new.toString() == '': new = QVariant() elif new.toString() == '""': new = QVariant(QString('')) self.model.rows[self.row][self.column] = new if self.column == 0: # commented or uncommented index1 = self.model.index(self.row, 0) index2 = self.model.index(self.row, self.model.columnCount() - 1) elif self.column == 1: # we have just changed the object name, other objects might # reference it index1 = self.model.index(0, 0) index2 = self.model.index(self.model.rowCount() - 1, self.model.columnCount() - 1) else: # just changed this cell index1 = self.model.index(self.row, self.column) index2 = index1 self.model.emit( SIGNAL('dataChanged(const QModelIndex &, const QModelIndex &)'), index1, index2)
def setData(self, index, value, role): ''' PyQt API Method -- See the PyQt documentation for a description ''' if not index.isValid(): return False item = index.internalPointer() node = item.node is_checkbox_node = node.tag == 'selectable' or node.get( 'type') == 'boolean' # only allow editing in second column if index.column() != 1: return False # user clicking on a checkbox if role == Qt.CheckStateRole and is_checkbox_node: # ask the users if they want to make inherited nodes local first if node.get('inherited'): title = 'Editing inherited node' msg = ("'%s' is inherited from a parent project. \n\n" "Do you want to make this node part of this project " "so that you can edit it?" % node.get('name') or node.tag) b = (QMessageBox.Yes, QMessageBox.No) ans = QMessageBox.question(None, title, msg, *b) if ans == QMessageBox.Yes: self.make_item_local(item) else: return False del title, msg, b, ans # Clean up namespace if value.toInt()[0] == Qt.Checked: value = QVariant('True') else: value = QVariant('False') # convert the value to a string and set the nodes text value value = value.toString() changed_value = node.text != value if changed_value: node.text = str(value) # avoid QString's in the xml self.dirty = True s = SIGNAL("dataChanged(const QModelIndex &, const QModelIndex &)") self.emit(s, index, index) return True
def loadResource(self, type, name): ret = QVariant() name.setFragment(QString()) if type == QTextDocument.HtmlResource: loop = QEventLoop() self.connect(self.http, SIGNAL("done(bool)"), loop, SLOT("quit()")) self.http.get(name.toString()) loop.exec_(QEventLoop.AllEvents|QEventLoop.WaitForMoreEvents) data = QVariant(QString(self.html)) if data.toString().trimmed().isEmpty(): fileName = QFileInfo( name.toLocalFile()).fileName() data = QTextBrowser.loadResource(self, type, QUrl(fileName)) else: fileName = QFileInfo( name.toLocalFile()).fileName() data = QTextBrowser.loadResource(self, type, QUrl(fileName)) return data
def setData(self, index, value, role): ''' PyQt API Method -- See the PyQt documentation for a description ''' if not index.isValid(): return False item = index.internalPointer() node = item.node is_checkbox_node = node.tag == 'selectable' or node.get('type') == 'boolean' # only allow editing in second column if index.column() != 1: return False # user clicking on a checkbox if role == Qt.CheckStateRole and is_checkbox_node: # ask the users if they want to make inherited nodes local first if node.get('inherited'): title = 'Editing inherited node' msg = ("'%s' is inherited from a parent project. \n\n" "Do you want to make this node part of this project " "so that you can edit it?" % node.get('name') or node.tag) b = (QMessageBox.Yes, QMessageBox.No) ans = QMessageBox.question(None, title, msg, *b) if ans == QMessageBox.Yes: self.make_item_local(item) else: return False del title, msg, b, ans # Clean up namespace if value.toInt()[0] == Qt.Checked: value = QVariant('True') else: value = QVariant('False') # convert the value to a string and set the nodes text value value = value.toString() changed_value = node.text != value if changed_value: node.text = str(value) # avoid QString's in the xml self.dirty = True s = SIGNAL("dataChanged(const QModelIndex &, const QModelIndex &)") self.emit(s, index, index) return True
def _updateRecentFileActions(self): files = list(self.settings.value("recentFileList").toStringList()) fileNames = {} numRecentFiles = min(len(files), self.MaxRecentFiles) for action in self.recentFileActions: action.setVisible(False) for i in range(numRecentFiles): fileName = QFileInfo(files[i]).fileName() if fileName in fileNames.keys(): fileNames[fileName] += 1 else: fileNames[fileName] = 1 for i in range(numRecentFiles): fileName = QFileInfo(files[i]).fileName() filePath = QVariant(files[i]) action = self.recentFileActions[i] action.setText(fileNames[fileName] == 1 and fileName or filePath.toString()) action.setData(filePath) action.setVisible(True) self.emit(SIGNAL("recentFileActionsAvailable(bool)"), numRecentFiles > 0)
def writeAttributeRow(self, rowNr, attributes): colNr = 0 for cell in attributes: # QGIS2.0 does not have QVariants anymore, only for <2.0: if QGis.QGIS_VERSION_INT < 10900: cell = QVariant(cell) if cell.canConvert(QVariant.Int) and cell.toInt()[1]: cell = cell.toInt()[0] elif cell.canConvert(QVariant.Double) and cell.toDouble()[1]: cell = cell.toDouble()[0] else: cell = unicode(cell.toString()) # handle NULL values by writing 'NULL' if not cell: cell = '' else: cell = unicode(cell) try: cell = float(cell) except: pass self.ws.write(rowNr, colNr, cell) colNr = colNr + 1
def addObstacleToModel(self, obstacle, checkResult=None): standardItemList = [] # obstacle.positionDegree = QgisHelper.Meter2Degree(obstacle.Position.x(), obstacle.Position.y()) standardItem = QStandardItem(str(obstacle.featureId)) standardItem.setData(obstacle.featureId) standardItemList.append(standardItem) standardItem = QStandardItem(str(obstacle.layerId)) standardItem.setData(obstacle.layerId) standardItemList.append(standardItem) standardItem = QStandardItem(str(obstacle.name)) standardItem.setData(obstacle.name) standardItemList.append(standardItem) standardItem = QStandardItem(str(obstacle.Position.x())) standardItem.setData(obstacle.Position.x()) standardItemList.append(standardItem) standardItem = QStandardItem(str(obstacle.Position.y())) standardItem.setData(obstacle.Position.y()) standardItemList.append(standardItem) value = QVariant(QgisHelper.strDegree(obstacle.positionDegree.y())) standardItem = QStandardItem(value.toString()) standardItem.setData(obstacle.positionDegree.y()) standardItemList.append(standardItem) strV = QgisHelper.strDegree(obstacle.positionDegree.y()) value = QVariant(QgisHelper.strDegree(obstacle.positionDegree.x())) standardItem = QStandardItem(value.toString()) standardItem.setData(obstacle.positionDegree.x()) standardItemList.append(standardItem) standardItem = QStandardItem(str(obstacle.Position.z())) standardItem.setData(obstacle.Position.z()) standardItemList.append(standardItem) standardItem = QStandardItem( str(Unit.ConvertMeterToFeet(obstacle.Position.z()))) standardItem.setData(Unit.ConvertMeterToFeet(obstacle.Position.z())) standardItemList.append(standardItem) standardItem = QStandardItem(str(obstacle.trees)) standardItem.setData(obstacle.trees) standardItemList.append(standardItem) standardItem = QStandardItem( str(Unit.ConvertMeterToFeet(obstacle.trees))) standardItem.setData(Unit.ConvertMeterToFeet(obstacle.trees)) standardItemList.append(standardItem) # for i in range(len(standardItemList), self.source.columnCount()): # standardItemList.append(QStandardItem("None")) self.source.appendRow(standardItemList) standardItem = QStandardItem(str(obstacle.mocMultiplier)) standardItem.setData(obstacle.mocMultiplier) self.source.setItem(self.source.rowCount() - 1, self.IndexMocMultiplier, standardItem)
def data(self, index, role): col = index.column() row = index.row() value = self.rows[row][col] # default view if role == Qt.DisplayRole: # comment row if col == 1: if len(value.toString()) > 0: return QVariant("#..") else: return QVariant() # if the cell is defaulted, display the default value elif self._isDefault(value, col): value = self._defaults[col] # if we've got a combo box lookup the appropriate value for the enum if col in self._cValues: # lookup the display in the list of _cItems for i, v in enumerate(self._cValues[col]): if v.toString() == value.toString(): return QVariant(self._cItems[col].toStringList()[i]) # display commented out rows as X elif col == 0: if value.toBool(): return QVariant(QString('X')) else: return QVariant(QString('')) # empty string rows should be "" elif not value.isNull() and self._types[col] == str and \ str(value.toString()) == '' and col != 1: value = QVariant(QString('""')) return value # text editor elif role == Qt.EditRole: # if the cell is defaulted, display the default value if self._isDefault(value, col): value = self._defaults[col] # if we've got a combo box lookup the appropriate value for the enum if col in self._cValues: # lookup the display in the list of _cItems for i, v in enumerate(self._cValues[col]): if v.toString() == value.toString(): return QVariant(self._cItems[col].toStringList()[i]) # empty string rows should be "" elif not value.isNull() and self._types[col] == str and \ str(value.toString()) == '' and col != 1: value = QVariant(QString('""')) return value elif role == Qt.ToolTipRole: # tooltip error = self._isInvalid(value, row, col) text = str(self._tooltips[col].toString()) if error: text = '***Error: %s\n%s'%(error, text) if col in self._idents: lines = ['\nPossible Values: '] for name in self._nameList(filt = self._types[col], upto = row): if len(lines[-1]) > 80: lines.append('') lines[-1] += str(name) + ', ' text += '\n'.join(lines).rstrip(' ,') if col == 1 and len(value.toString()) > 0: text += ":\n\n" + value.toString() return QVariant(text) elif role == Qt.ForegroundRole: # cell foreground if self._isCommented(row): # comment return QVariant(QColor(120,140,180)) if self._isDefault(value, col): # is default arg (always valid) return QVariant(QColor(160,160,160)) elif self._isInvalid(value, row, col): # invalid return QVariant(QColor(255,0,0)) else: # valid return QVariant(QColor(0,0,0)) elif role == Qt.BackgroundRole: # cell background if self._isCommented(row): # commented return QVariant(QColor(160,180,220)) elif self._isInvalid(value, row, col): # invalid return QVariant(QColor(255,200,200)) elif col in self._defaults: # has default return QVariant(QColor(255,255,240)) elif col in self._optional: #is optional return QVariant(QColor(180,180,180)) else: # valid return QVariant(QColor(250,250,250)) elif role == Qt.UserRole: # combo box asking for list of items if col in self._idents: return QVariant( self._nameList(filt = self._types[col], upto = row)) elif col in self._cItems: return self._cItems[col] else: return QVariant() else: return QVariant()
class Cell(QObject): def __init__(self): super(Cell, self).__init__() self.value = QVariant() self.cacheIsDirty=False self.cachedValue = QVariant() self.setDirty() def clone(self): return deepcopy(self) def setData(self,role,value): QTableWidgetItem.setData(role,value) if(role == Qt.EditRole): self.setDirty() def data(self,role): if role == Qt.DisplayRole : if self.value.isValid(): return self.value.toString() else: return "####" elif (role == Qt.TextAlignmentRole): if (self.value.type() == QVariant.String): return int(Qt.AlignLeft | Qt.AlignVCenter) else: return int(Qt.AlignRight | Qt.AlignVCenter) else: return QTableWidgetItem.data(role) # def setFormula(self, &formula): def setFormula(self, formula): self.setData(Qt.EditRole, formula) def formula(self) : return self.data(Qt.EditRole).toString() def setDirty(self): self.cacheIsDirty = True def value(self) : if (self.cacheIsDirty): self.cacheIsDirty = False formulaStr = self.formula() if (formulaStr.startsWith('\'')): self.cachedValue = formulaStr.mid(1) elif (formulaStr.startsWith('=')): self.cachedValue = self.Invalid expr = QString(formulaStr.mid(1)) expr.replace(" ", "") expr.append(QChar.Null) pos = int(0) cachedValue = self.evalExpression(expr, pos) if (expr[pos] != QChar.Null): cachedValue = self.Invalid else: pass ok = bool(False) # double d = formulaStr.toDouble(&ok) # if (ok) # cachedValue = d # else # cachedValue = formulaStr # return self.cachedValue # def evalExpression(const QString &str, int &pos) const def evalExpression(self,str, pos): result = QVariant(self.evalTerm(str, pos)) # while (str[pos] != QChar.Null): # op = QChar(str[pos]) # if (op != '+' && op != '-'): # return result # pos = pos + 1 # # term = QVariant(self.evalTerm(str, pos)) # if (result.type() == QVariant.Double # && term.type() == QVariant.Double) # if (op == '+') # result = result.toDouble() + term.toDouble() # else # result = result.toDouble() - term.toDouble() # # else # result = Invalid return QVariant(result) # QVariant Cell.evalTerm(const QString &str, int &pos) const def evalTerm(self,str, pos) : pass # # QVariant result = evalFactor(str, pos) # while (str[pos] != QChar.Null) # QChar op = str[pos] # if (op != '*' && op != '/') # return result # ++pos # # QVariant factor = evalFactor(str, pos) # if (result.type() == QVariant.Double # && factor.type() == QVariant.Double) # if (op == '*') # result = result.toDouble() * factor.toDouble() # else # if (factor.toDouble() == 0.0) # result = Invalid # else # result = result.toDouble() / factor.toDouble() # # # else # result = Invalid # # return result return QVariant(0) # QVariant Cell.evalFactor(const QString &str, int &pos) const def evalFactor(self,str, pos) : # # QVariant result # bool negative = false # # if (str[pos] == '-') # negative = true # ++pos # # # if (str[pos] == '(') # ++pos # result = evalExpression(str, pos) # if (str[pos] != ')') # result = Invalid # ++pos # else # QRegExp regExp("[A-Za-z][1-9][0-9]0,2") # QString token # # while (str[pos].isLetterOrNumber() || str[pos] == '.') # token += str[pos] # ++pos # # # if (regExp.exactMatch(token)) # int column = token[0].toUpper().unicode() - 'A' # int row = token.mid(1).toInt() - 1 # # Cell *c = static_cast<Cell *>( # tableWidget()->item(row, column)) # if (c) # result = c->value() # else # result = 0.0 # # else # bool ok # result = token.toDouble(&ok) # if (!ok) # result = Invalid # # # # if (negative) # if (result.type() == QVariant.Double) # result = -result.toDouble() # else # result = Invalid # # # return result return QVariant(0)
class Cell(QObject): def __init__(self): super(Cell, self).__init__() self.value = QVariant() self.cacheIsDirty = False self.cachedValue = QVariant() self.setDirty() def clone(self): return deepcopy(self) def setData(self, role, value): QTableWidgetItem.setData(role, value) if (role == Qt.EditRole): self.setDirty() def data(self, role): if role == Qt.DisplayRole: if self.value.isValid(): return self.value.toString() else: return "####" elif (role == Qt.TextAlignmentRole): if (self.value.type() == QVariant.String): return int(Qt.AlignLeft | Qt.AlignVCenter) else: return int(Qt.AlignRight | Qt.AlignVCenter) else: return QTableWidgetItem.data(role) # def setFormula(self, &formula): def setFormula(self, formula): self.setData(Qt.EditRole, formula) def formula(self): return self.data(Qt.EditRole).toString() def setDirty(self): self.cacheIsDirty = True def value(self): if (self.cacheIsDirty): self.cacheIsDirty = False formulaStr = self.formula() if (formulaStr.startsWith('\'')): self.cachedValue = formulaStr.mid(1) elif (formulaStr.startsWith('=')): self.cachedValue = self.Invalid expr = QString(formulaStr.mid(1)) expr.replace(" ", "") expr.append(QChar.Null) pos = int(0) cachedValue = self.evalExpression(expr, pos) if (expr[pos] != QChar.Null): cachedValue = self.Invalid else: pass ok = bool(False) # double d = formulaStr.toDouble(&ok) # if (ok) # cachedValue = d # else # cachedValue = formulaStr # return self.cachedValue # def evalExpression(const QString &str, int &pos) const def evalExpression(self, str, pos): result = QVariant(self.evalTerm(str, pos)) # while (str[pos] != QChar.Null): # op = QChar(str[pos]) # if (op != '+' && op != '-'): # return result # pos = pos + 1 # # term = QVariant(self.evalTerm(str, pos)) # if (result.type() == QVariant.Double # && term.type() == QVariant.Double) # if (op == '+') # result = result.toDouble() + term.toDouble() # else # result = result.toDouble() - term.toDouble() # # else # result = Invalid return QVariant(result) # QVariant Cell.evalTerm(const QString &str, int &pos) const def evalTerm(self, str, pos): pass # # QVariant result = evalFactor(str, pos) # while (str[pos] != QChar.Null) # QChar op = str[pos] # if (op != '*' && op != '/') # return result # ++pos # # QVariant factor = evalFactor(str, pos) # if (result.type() == QVariant.Double # && factor.type() == QVariant.Double) # if (op == '*') # result = result.toDouble() * factor.toDouble() # else # if (factor.toDouble() == 0.0) # result = Invalid # else # result = result.toDouble() / factor.toDouble() # # # else # result = Invalid # # return result return QVariant(0) # QVariant Cell.evalFactor(const QString &str, int &pos) const def evalFactor(self, str, pos): # # QVariant result # bool negative = false # # if (str[pos] == '-') # negative = true # ++pos # # # if (str[pos] == '(') # ++pos # result = evalExpression(str, pos) # if (str[pos] != ')') # result = Invalid # ++pos # else # QRegExp regExp("[A-Za-z][1-9][0-9]0,2") # QString token # # while (str[pos].isLetterOrNumber() || str[pos] == '.') # token += str[pos] # ++pos # # # if (regExp.exactMatch(token)) # int column = token[0].toUpper().unicode() - 'A' # int row = token.mid(1).toInt() - 1 # # Cell *c = static_cast<Cell *>( # tableWidget()->item(row, column)) # if (c) # result = c->value() # else # result = 0.0 # # else # bool ok # result = token.toDouble(&ok) # if (!ok) # result = Invalid # # # # if (negative) # if (result.type() == QVariant.Double) # result = -result.toDouble() # else # result = Invalid # # # return result return QVariant(0)