def load(self): exception = None fh = None try: if self.filename.isEmpty(): raise IOError, "no filename specified for loading" fh = QFile(self.filename) if not fh.open(QIODevice.ReadOnly): raise IOError, unicode(fh.errorString()) stream = QDataStream(fh) magic = stream.readInt32() if magic != MAGIC_NUMBER: raise IOError, "unrecognized file type" fileVersion = stream.readInt16() if fileVersion != FILE_VERSION: raise IOError, "unrecognized file type version" self.ships = [] while not stream.atEnd(): name = QString() owner = QString() country = QString() description = QString() stream >> name >> owner >> country >> description teu = stream.readInt32() self.ships.append(Ship(name, owner, country, teu, description)) self.owners.add(unicode(owner)) self.countries.add(unicode(country)) self.dirty = False except IOError, e: exception = e
def load(self): exception = None fh = None try: if self.filename.isEmpty(): raise IOError, "no filename specified for loading" fh = QFile(self.filename) if not fh.open(QIODevice.ReadOnly): raise IOError, unicode(fh.errorString()) stream = QDataStream(fh) magic = stream.readInt32() if magic != MAGIC_NUMBER: raise IOError, "unrecognized file type" fileVersion = stream.readInt16() if fileVersion != FILE_VERSION: raise IOError, "unrecognized file type version" self.ships = [] while not stream.atEnd(): name = QString() owner = QString() country = QString() description = QString() stream >> name >> owner >> country >> description teu = stream.readInt32() self.ships.append(Ship(name, owner, country, teu, description)) self.owners.add(unicode(owner)) self.countries.add(unicode(country)) self.dirty = False except IOError, err: exception = err
def test_drag_information_are_correct(self): rw = RecipesWidget() self.assertIn('application/vnd.re-eat.recipe', rw.mimeTypes()) items = [rw.item(i) for i in (0, 1)] ids = [item.data(Qt.UserRole) for item in items] data = rw.mimeData(items) stream = QDataStream(data.data('application/vnd.re-eat.recipe')) result = [] while not stream.atEnd(): result.append(stream.readInt()) self.assertListEqual(result, ids)
def dropMimeData(self, index, data, action): if action == Qt.IgnoreAction: return True if data.hasFormat('application/vnd.re-eat.recipe'): encodedData = data.data('application/vnd.re-eat.recipe') stream = QDataStream(encodedData, QIODevice.ReadOnly) while not stream.atEnd(): id = stream.readInt() self.recipeAdded.emit(id, self.date, self.index) return True elif data.hasFormat('application/vnd.re-eat.meal_recipe'): encodedData = data.data('application/vnd.re-eat.meal_recipe') stream = QDataStream(encodedData, QIODevice.ReadOnly) while not stream.atEnd(): id = stream.readInt() date = stream.readQVariant() index = stream.readInt() self.recipeMoved.emit(id, date, index, self.date, self.index) return True return False
def loadQDataStream(self): error = None fh = None try: fh = QFile(self.__fname) if not fh.open(QIODevice.ReadOnly): raise IOError(str(fh.errorString())) stream = QDataStream(fh) magic = stream.readInt32() if magic != MovieContainer.MAGIC_NUMBER: raise IOError("unrecognized file type") version = stream.readInt32() if version < MovieContainer.OLD_FILE_VERSION: raise IOError("old and unreadable file format") elif version > MovieContainer.FILE_VERSION: raise IOError("new and unreadable file format") old = False if version == MovieContainer.OLD_FILE_VERSION: old = True stream.setVersion(QDataStream.Qt_4_2) self.clear(False) while not stream.atEnd(): title = QString() acquired = QDate() location = QString() notes = QString() stream >> title year = stream.readInt16() minutes = stream.readInt16() if old: stream >> acquired >> notes else: stream >> acquired >> location >> notes self.add(Movie(title, year, minutes, acquired, location, notes)) except EnvironmentError as e: error = "Failed to load: {0}".format(e) finally: if fh is not None: fh.close() if error is not None: return False, error self.__dirty = False return True, "Loaded {0} movie records from {1}".format( len(self.__movies), QFileInfo(self.__fname).fileName())
def dropMimeData(self, data, action, row, column, parent): # 如果放入动作是Qt.IgnoreAction,那么返回True if (action == Qt.IgnoreAction): return True # 如果数据的格式不是指定的格式,那么返回False if (not data.hasFormat("application/vnd.text.list")): return False # 因为这里是列表,只用一列,所以列大于0时返回False if (column > 0): return False # 设置开始插入的行 if (row != -1): beginRow = row elif parent.isValid(): beginRow = parent.row() else: beginRow = self.rowCount(QModelIndex()) # 将数据从QMimeData中读取出来,然后插入到模型中 encodedData = data.data("application/vnd.text.list") stream = QDataStream(encodedData, QIODevice.ReadOnly) newItems = QStringList() rows = 0 while (not stream.atEnd()): text = QString() stream >> text newItems.append(text) rows += 1 self.insertRows(beginRow, rows, QModelIndex()) for text in newItems: idx = self.index(beginRow, 0, QModelIndex()) self.setData(idx, QVariant(text)) beginRow += 1 return True
def decode_data(self, bytearray): """Handle drag/drop data.""" data = [] item = {} ds = QDataStream(bytearray) while not ds.atEnd(): row = ds.readInt32() column = ds.readInt32() map_items = ds.readInt32() for i in range(map_items): key = ds.readInt32() value = QVariant() ds >> value item[Qt.ItemDataRole(key)] = value data.append(item) return data
def load(self): exception = None fh = None try: if self.filename.isEmpty(): raise IOError("no filename specified for loading") fh = QFile(self.filename) if not fh.open(QIODevice.ReadOnly): raise IOError(str(fh.errorString())) stream = QDataStream(fh) magic = stream.readInt32() if magic != MAGIC_NUMBER: raise IOError("unrecognized file type") fileVersion = stream.readInt16() if fileVersion != FILE_VERSION: raise IOError("unrecognized file type version") self.ships = {} while not stream.atEnd(): name = QString() owner = QString() country = QString() description = QString() stream >> name >> owner >> country >> description teu = stream.readInt32() ship = Ship(name, owner, country, teu, description) self.ships[id(ship)] = ship self.owners.add(str(owner)) self.countries.add(str(country)) self.dirty = False except IOError as e: exception = e finally: if fh is not None: fh.close() if exception is not None: raise exception
def dropMimeData(self, mime_data, action, row, col_unused, parent_unused, filling_empty_model=False): if action == Qt.IgnoreAction: return True if not mime_data.hasFormat("application/vnd.text.list"): return False if not filling_empty_model and row == self.rowCount(): # It's forbidden to insert before the first commit (last row of the # model). return False if row != -1: begin_row = row else: return False encoded_data = mime_data.data("application/vnd.text.list") stream = QDataStream(encoded_data, QIODevice.ReadOnly) new_items = [] rows = 0 while not stream.atEnd(): text = stream.readQString() item_branch, item_row_s = str(text).split(' ') new_items.append([int(item_row_s),]) rows += 1 # Now new_items contains 1 element lists with the row of the inserted # commit. We will complete these lists with the actual Commit object. # item_branch contains the name of the branch. for (branch, model) in self._all_models_dict.items(): if branch.name == item_branch: item_model = model # We're going to store the data to be inserted in a dictionnary before # inserting the rows. This is to avoid problems when copying rows from # a model to somewhere above in the same model. The insertion of rows # causes a shift of all the rows, including the ones to be copied from. data_to_be_inserted = {} insert_row = begin_row for item in new_items: item_row = item[0] for column, field in enumerate(self.get_columns()): item_index = self.createIndex(item_row, column) data = item_model.data(item_index, Qt.EditRole) data_to_be_inserted[(insert_row, column)] = data insert_row += 1 self.start_history_event() parents_col = self.get_columns().index("parents") children_col = self.get_columns().index("children") _row = begin_row for item in new_items: replaced_row = _row replaced_index = self.createIndex(replaced_row, 0) while self.is_deleted(replaced_index): replaced_row += 1 replaced_index = self.createIndex(replaced_row, 0) replaced_commit = self.git_model.get_commits()[replaced_index.row()] new_parents = [replaced_commit,] new_children = list(self.git_model.c_data(replaced_commit, "children")) self.insertRows(_row, 1, QModelIndex()) for column, field in enumerate(self.get_columns()): index = self.createIndex(_row, column) self.setData(index, data_to_be_inserted[(_row, column)]) self.setData(self.createIndex(_row, children_col), new_children) self.setData(self.createIndex(_row, parents_col), [replaced_commit,]) this_commit = self.git_model.get_commits()[_row] # Updating parent's children and children parent's for child in new_children: row_of_child = self.git_model.row_of(child) _parents = list(self.git_model.c_data(child, "parents")) parents_position = _parents.index(replaced_commit) # Removing only the replaced commit from the parents _parents.pop(parents_position) _parents.insert(parents_position, this_commit) self.setData(self.createIndex(row_of_child, parents_col), _parents) for parent in new_parents: row_of_parent = self.git_model.row_of(parent) self.setData(self.createIndex(row_of_parent, children_col), [this_commit,]) _row += 1 self.reset() return True