def dropMimeData(self, data, action, row, column, parent): if data.hasFormat('application/target.tableitem.creepy'): encodedData = data.data('application/target.tableitem.creepy') stream = QDataStream(encodedData, QIODevice.ReadOnly) columnsList = [] qVariant = QVariant() while not stream.atEnd(): stream >> qVariant columnsList.append(qVariant.toPyObject()) draggedRows = [ columnsList[x:x + 5] for x in range(0, len(columnsList), 5) ] droppedRows = [] for row in draggedRows: # Ensure we are not putting duplicates in the target list existed = False for target in self.targets: if row[2] == target['targetUsername'] and row[0] == target[ 'pluginName']: existed = True if not existed: droppedRows.append({ 'targetUsername': row[2], 'targetFullname': row[3], 'targetPicture': row[1], 'targetUserid': row[4], 'pluginName': row[0] }) self.insertRows(droppedRows, len(droppedRows), parent) return True
def __getstate__(self): result = dict(self.__dict__) result['url'] = result['url'].toEncoded() data = QByteArray() ds = QDataStream(data, QIODevice.WriteOnly) ds.writeQVariant(self.icon) result['icon'] = data.data() return result
def __setstate__(self, state): for key, val in state.items(): if key == 'url': self.__dict__[key] = QUrl.fromEncoded(val) elif key == 'icon': ds = QDataStream(QByteArray(val)) self.__dict__[key] = ds.readQVariant() else: self.__dict__[key] = val
def __getstate__(self): data = QByteArray() stream = QDataStream(data, QIODevice.WriteOnly) stream.writeQVariant(self.windows) stream.writeInt(self._s_restoreDataVersion) stream.writeQVariant(self.crashedSession) stream.writeQVariant(self.closedWindows) return data
def _restoreState(self, state): ''' @param: state QByteArray ''' stream = QDataStream(state) if stream.atEnd(): return version = -1 historyIndex = -1 databases = False localStorage = False cache = False cookies = False version = stream.readInt() if version != self._s_stateDataVersoin: return historyIndex = stream.readInt() databases = stream.readBool() localStorage = stream.readBool() cache = stream.readBool() cookies = stream.readBool() if historyIndex != -1: self._ui.history.setChecked(True) self._ui.historyLength.setEnabled(True) self._ui.historyLength.setCurrentIndex(historyIndex) self._ui.databases.setChecked(databases) self._ui.localStorage.setChecked(localStorage) self._ui.cache.setChecked(cache) self._ui.cookies.setChecked(cookies)
def saveState(self): restoreData = RestoreData() for window in self._windows: restoreData.windows.append(BrowserWindow.SavedWindow(window)) if self._restoreManager and self._restoreManager.isValid(): stream = QDataStream(restoreData.crashedSession, QIODevice.WriteOnly) stream.writeQVariant(self._restoreManager.restoreData()) restoreData.closedWindows = self._closedWindowsManager.saveState() data = QByteArray() stream = QDataStream(data, QIODevice.WriteOnly) stream.writeInt(const.sessionVersion) stream.writeBytes(pickle.dumps(restoreData)) return data
def return_data_to_clients(self, client_id, data): try: for client_socket in self.clients: return_data_string = 'Client {} sent: {}'.format(client_id, data) data_byte_array =QByteArray() stream =QDataStream(data_byte_array, QIODevice.WriteOnly) stream.setVersion(QDataStream.Qt_5_9) stream.writeUInt32(0) stream.writeQString(return_data_string) client_socket.write(data_byte_array) except Exception as e: print(e)
def historyData(self): ''' @return QByteArray ''' if self.isRestored(): historyArray = QByteArray() stream = QDataStream(historyArray, QIODevice.WriteOnly) history = self._webView.history() stream << history return historyArray else: return self._savedTab.history
def p_restoreTabByUrl(self, url, history, zoomLevel): self._webView.load(url) # Restoring history of internal pages crashes QtWebEngine 5.8 blacklistedSchemes = ['view-source', 'chrome'] if (url.scheme() not in blacklistedSchemes): stream = QDataStream(history) stream >> self._webView.history() self._webView.setZoomLevel(zoomLevel) self._webView.setFocus()
def mimeData(self, indices): mimeData = QMimeData() encodedData = QByteArray() stream = QDataStream(encodedData, QIODevice.WriteOnly) for index in indices: if index.column() == 1: d = QVariant(self.data(index, Qt.DecorationRole)) else: d = QVariant(self.data(index, Qt.DisplayRole).toString()) stream << d mimeData.setData('application/target.tableitem.creepy', encodedData) return mimeData
def write_data(self): try: data_byte_array = QByteArray() stream = QDataStream(data_byte_array, QIODevice.WriteOnly) stream.setVersion(QDataStream.Qt_5_9) stream.writeUInt32(0) if self.line_edit: print('inside write_data') stream.writeQString(self.line_edit.text()) self.socket.write(data_byte_array) data_byte_array = None if self.line_edit: self.line_edit.setText('') except Exception as e: print(e)
def read_data(self): try: for client_id, client_socket in enumerate(self.clients): if client_socket.bytesAvailable() > 0: stream = QDataStream(client_socket) stream.setVersion(QDataStream.Qt_5_9) stream.readUInt32() client_data= stream.readQString() self.return_data_to_clients(client_id, client_data) except Exception as e: print(e)
def restoreState(self, state): ''' @param: state QByteArray ''' stream = QDataStream(state) version = stream.readInt() if version < 1: return self._closedWindows.clear() windowCount = stream.readInt() for idx in range(windowCount): window = self.Window() window.windowState = stream.readQVariant() if not window.isValid(): continue window.icon = window.windowState.tabs[0].icon window.title = window.windowState.tabs[0].title self._closedWindows.append(window)
def createFromFile(cls, fpath): ''' @param: fpath QString @return: data RestoreData ''' data = RestoreData() if not QFile.exists(fpath): return data recoveryFile = QFile(fpath) if not recoveryFile.open(QIODevice.ReadOnly): return data stream = QDataStream(recoveryFile) version = stream.readInt() if version == const.sessionVersion: byteData = stream.readBytes() data = pickle.loads(byteData) else: print('WARNING: Unsupported session file version', version, 'path', fpath) return data
def saveState(self): ''' @return: QByteArray ''' data = QByteArray() stream = QDataStream(data, QIODevice.WriteOnly) stream.writeInt(self._s_closedWindowsVersion) # Only save last 3 windows windowCount = min(max(0, len(self._closedWindows)), 3) stream.writeInt(windowCount) for window in self._closedWindows[:windowCount]: stream.writeQVariant(window.windowState) return data
def __setstate__(self, state): stream = QDataStream(state) self.windows = stream.readQVariant() assert (type(self.windows) == list) version = stream.readInt() if version >= 1: self.crashedSession = stream.readQVariant() if version >= 2: self.closedWindows = stream.readQVariant()
def read_data(self): try: stream =QDataStream(self.socket) stream.setVersion(QDataStream.Qt_5_9) while True: if self.socket.bytesAvailable() <= 0: break stream.readUInt32() text_from_server = stream.readQString() if self.text_widget: print('display_text') print(text_from_server) self.text_widget.append(text_from_server) except Exception as e: print(e)
def clearRestoreData(self): crashedSession = QByteArray(self._data.crashedSession) self._data.clear() stream = QDataStream(crashedSession, QIODevice.ReadOnly) self._data = stream.readQVariant()
result = dict(self.__dict__) result['test1'] = result['test1'].toEncoded() return result def __setstate__(self, state): print('__setstate__') for key, val in state.items(): if key == 'test1': self.test1 = QUrl.fromEncoded(val) else: setattr(self, key, val) if argv[1] == 'dump': data = QByteArray() ds = QDataStream(data, QIODevice.WriteOnly) ds.writeInt(1) obj = ObjType() ds.writeQVariant([obj, obj]) print(data) with open('test.dat', 'wb') as fp: fp.write(data) elif argv[1] == 'load': with open('test.dat', 'rb') as fp: data = fp.read() dsOut = QDataStream(QByteArray(data)) ver = dsOut.readInt() lst = dsOut.readQVariant() print(lst[0].test1) print(lst) else:
def _saveState(self): ''' @return: QByteArray ''' # history - web database - local storage - cache - icons data = QByteArray() stream = QDataStream(data, QIODevice.WriteOnly) stream.writeInt(self._s_stateDataVersoin) if not self._ui.history.isChecked(): stream.writeInt(-1) else: stream.writeInt(self._ui.historyLength.currentIndex()) stream.writeBool(self._ui.databases.isChecked()) stream.writeBool(self._ui.localStorage.isChecked()) stream.writeBool(self._ui.cache.isChecked()) stream.writeBool(self._ui.cookies.isChecked()) return data