def newLetter(self): self.textEdit.clear() cursor = self.textEdit.textCursor() cursor.movePosition(QTextCursor.Start) topFrame = cursor.currentFrame() topFrameFormat = topFrame.frameFormat() topFrameFormat.setPadding(16) topFrame.setFrameFormat(topFrameFormat) textFormat = QTextCharFormat() boldFormat = QTextCharFormat() boldFormat.setFontWeight(QFont.Bold) italicFormat = QTextCharFormat() italicFormat.setFontItalic(True) tableFormat = QTextTableFormat() tableFormat.setBorder(1) tableFormat.setCellPadding(16) tableFormat.setAlignment(Qt.AlignRight) ##The Firm Box cursor.insertTable(1, 1, tableFormat) cursor.insertText("The Firm", boldFormat) cursor.insertBlock() cursor.insertText("321 City Street", textFormat) cursor.insertBlock() cursor.insertText("Industry Park") cursor.insertBlock() cursor.insertText("Some Country") cursor.setPosition(topFrame.lastPosition()) ##The Firm Box 아래 편지부분 cursor.insertText(QDate.currentDate().toString("d MMMM yyyy"), textFormat) cursor.insertBlock() cursor.insertBlock() cursor.insertText("Dear ", textFormat) cursor.insertText("NAME", italicFormat) cursor.insertText(",", textFormat) for i in range(3): cursor.insertBlock() cursor.insertText("Yours sincerely,", textFormat) for i in range(3): cursor.insertBlock() cursor.insertText("The Boss", textFormat) cursor.insertBlock() cursor.insertText("ADDRESS", italicFormat)
def SaveTableImage(table): pixmap = table.grab() pixmap.save("widget.png") SaveTableImage(table) nrows = table.rowCount() ncols = table.columnCount() doc = QTextDocument() cursor = QTextCursor(doc) tableFormat = QTextTableFormat() tableFormat.setHeaderRowCount(1) tableFormat.setAlignment(Qt.AlignHCenter) tableFormat.setCellPadding(0) tableFormat.setCellSpacing(0) tableFormat.setBorder(1) tableFormat.setBorderBrush(QBrush(Qt.SolidPattern)) tableFormat.clearColumnWidthConstraints() textTable = cursor.insertTable(nrows + 1, ncols, tableFormat) tableHeaderFormat = QTextCharFormat() tableHeaderFormat.setBackground(QColor("#DADADA")) for i in range(ncols): cell = textTable.cellAt(0, i) cell.setFormat(tableHeaderFormat) cellCursor = cell.firstCursorPosition() cellCursor.insertText(table.horizontalHeaderItem(i).text()) for i in range(nrows): for j in range(ncols): item = table.item(i, j) t = "" if item is None else str(item.text()) # if item.text().iEmpty(): # table.setItem(i,j,QTableWidgetItem("0")) cell = textTable.cellAt(i + 1, j) cellCursor = cell.firstCursorPosition() cellCursor.insertText(t) cursor.movePosition(QTextCursor.End) printer = QPrinter(QPrinter.PrinterResolution) printer.setPaperSize(QPrinter.A4) printer.setOrientation(QPrinter.Landscape) printer.setOutputFileName("w8.pdf") doc.setDocumentMargin(0) doc.setTextWidth(5) doc.print(printer)
def newLetter(self): self.textEdit.clear() cursor = self.textEdit.textCursor() cursor.movePosition(QTextCursor.Start) topFrame = cursor.currentFrame() topFrameFormat = topFrame.frameFormat() topFrameFormat.setPadding(16) topFrame.setFrameFormat(topFrameFormat) textFormat = QTextCharFormat() boldFormat = QTextCharFormat() boldFormat.setFontWeight(QFont.Bold) italicFormat = QTextCharFormat() italicFormat.setFontItalic(True) tableFormat = QTextTableFormat() tableFormat.setBorder(1) tableFormat.setCellPadding(16) tableFormat.setAlignment(Qt.AlignRight) cursor.insertTable(1, 1, tableFormat) cursor.insertText("The Firm", boldFormat) cursor.insertBlock() cursor.insertText("321 City Street", textFormat) cursor.insertBlock() cursor.insertText("Industry Park") cursor.insertBlock() cursor.insertText("Some Country") cursor.setPosition(topFrame.lastPosition()) cursor.insertText(QDate.currentDate().toString("d MMMM yyyy"), textFormat) cursor.insertBlock() cursor.insertBlock() cursor.insertText("Dear ", textFormat) cursor.insertText("NAME", italicFormat) cursor.insertText(",", textFormat) for i in range(3): cursor.insertBlock() cursor.insertText("Yours sincerely,", textFormat) for i in range(3): cursor.insertBlock() cursor.insertText("The Boss", textFormat) cursor.insertBlock() cursor.insertText("ADDRESS", italicFormat)
def insert_table(self): self.param = mainDialog() table_format = QTextTableFormat() table_format.setWidth(QTextLength(QTextLength.PercentageLength,50)) table_format.setAlignment(Qt.AlignCenter) table_format.setCellPadding(5) table_format.setCellSpacing(0) ret = self.param.exec_() if ret == 1: if self.param.border_style == 'Dotted': table_format.setBorderStyle(QTextFrameFormat.BorderStyle_Dotted) elif self.param.border_style == 'Groove': table_format.setBorderStyle(QTextFrameFormat.BorderStyle_Groove) elif self.param.border_style == 'Dashed': table_format.setBorderStyle(QTextFrameFormat.BorderStyle_Dashed) elif self.param.border_style == 'DotDotDash': table_format.setBorderStyle(QTextFrameFormat.BorderStyle_DotDotDash) elif self.param.border_style == 'DotDash': table_format.setBorderStyle(QTextFrameFormat.BorderStyle_DotDash) elif self.param.border_style == 'Double': table_format.setBorderStyle(QTextFrameFormat.BorderStyle_Double) elif self.param.border_style == 'Inset': table_format.setBorderStyle(QTextFrameFormat.BorderStyle_Inset) elif self.param.border_style == 'None': table_format.setBorderStyle(QTextFrameFormat.BorderStyle_Inset) elif self.param.border_style == 'Outset': table_format.setBorderStyle(QTextFrameFormat.BorderStyle_Outset) elif self.param.border_style == 'Ridge': table_format.setBorderStyle(QTextFrameFormat.BorderStyle_Ridge) elif self.param.border_style == 'Solid': table_format.setBorderStyle(QTextFrameFormat.BorderStyle_Solid) if self.param.layout_Direction == 'Left To Right': table_format.setLayoutDirection(Qt.LeftToRight) elif self.param.layout_Direction == 'Right To Left': table_format.setLayoutDirection(Qt.RightToLeft) cursor = self.ui.textEdit.textCursor() cursor.insertTable(self.param.number_of_rows,self.param.number_of_columns,table_format) self.param.close()
def CreatePDF(self, out, timestamp, data, frame, rows, columns, fileName, VManager): ''' Create PDF QgsTask ''' QCoreApplication.processEvents() QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) font_normal = QFont("Helvetica", 10, QFont.Normal) font_bold = QFont("Helvetica", 12, QFont.Bold) printer = QPrinter(QPrinter.HighResolution) printer.setOrientation(QPrinter.Portrait) printer.setOutputFormat(QPrinter.PdfFormat) printer.setFullPage(True) printer.setPaperSize(QPrinter.A4) printer.setOutputFileName(out) printer.setPageMargins(15, 15, 15, 15, QPrinter.Point) printer.setColorMode(QPrinter.Color) document = QTextDocument() document.setDefaultFont(font_normal) cursor = QTextCursor(document) cursor.movePosition(QTextCursor.Start, QTextCursor.MoveAnchor) cursor.insertHtml(""" <p style='text-align: center;'> <img style='display: block; margin-left: auto; margin-right: auto;' src=\':/imgFMV/images/header_logo.png\' width='200' height='25' /> </p> <p style='text-align: center;'> <strong>Video : </strong>%s<strong> </p> <p style='text-align: center;'> <strong>TimeStamp : </strong>%s</p><br><br> """ % (fileName, timestamp)) tableFormat = QTextTableFormat() tableFormat.setHeaderRowCount(1) tableFormat.setBorderBrush(QBrush(Qt.black)) tableFormat.setAlignment(Qt.AlignHCenter) tableFormat.setCellPadding(2) tableFormat.setCellSpacing(2) centerFormat = QTextBlockFormat() centerFormat.setAlignment(Qt.AlignCenter) cursor.insertBlock(centerFormat) textTable = cursor.insertTable(rows + 1, columns, tableFormat) tableHeaderFormat = QTextCharFormat() tableHeaderFormat.setFont(font_bold) tableHeaderFormat.setBackground(QColor("#67b03a")) tableHeaderFormat.setForeground(Qt.white) alternate_background = QTextCharFormat() alternate_background.setBackground(QColor("#DDE9ED")) for column in range(columns): cell = textTable.cellAt(0, column) cell.setFormat(tableHeaderFormat) cellCursor = cell.firstCursorPosition() cellCursor.insertText(VManager.horizontalHeaderItem(column).text()) row = 0 for key in sorted(data.keys()): cell0 = textTable.cellAt(row + 1, 0) cell1 = textTable.cellAt(row + 1, 1) cell2 = textTable.cellAt(row + 1, 2) if (row + 1) % 2 == 0: cell0.setFormat(alternate_background) cell1.setFormat(alternate_background) cell2.setFormat(alternate_background) cellCursor0 = cell0.firstCursorPosition() cellCursor0.insertText(str(key)) cellCursor1 = cell1.firstCursorPosition() cellCursor1.insertText(str(data[key][0])) cellCursor2 = cell2.firstCursorPosition() cellCursor2.insertText(str(data[key][1])) row += 1 cursor.movePosition(QTextCursor.End, QTextCursor.MoveAnchor) cursor.insertHtml(""" <br><p style='text-align: center;'><strong>Current Frame</strong></p><br> """) centerFormat = QTextBlockFormat() centerFormat.setAlignment(Qt.AlignHCenter) cursor.insertBlock(centerFormat) cursor.insertImage(frame.scaledToWidth(500)) QCoreApplication.processEvents() document.print_(printer) QCoreApplication.processEvents()
def insertCalendar(self): self.editor.clear() cursor = self.editor.textCursor() cursor.beginEditBlock() date = QDate(self.selectedDate.year(), self.selectedDate.month(), 1) tableFormat = QTextTableFormat() tableFormat.setAlignment(Qt.AlignHCenter) tableFormat.setBackground(QColor("#e0e0e0")) tableFormat.setCellPadding(2) tableFormat.setCellSpacing(4) constraints = [ QTextLength(QTextLength.PercentageLength, 14), QTextLength(QTextLength.PercentageLength, 14), QTextLength(QTextLength.PercentageLength, 14), QTextLength(QTextLength.PercentageLength, 14), QTextLength(QTextLength.PercentageLength, 14), QTextLength(QTextLength.PercentageLength, 14), QTextLength(QTextLength.PercentageLength, 14), ] tableFormat.setColumnWidthConstraints(constraints) table = cursor.insertTable(1, 7, tableFormat) frame = cursor.currentFrame() frameFormat = frame.frameFormat() frameFormat.setBorder(1) frame.setFrameFormat(frameFormat) format = cursor.charFormat() format.setFontPointSize(self.fontSize) boldFormat = QTextCharFormat(format) boldFormat.setFontWeight(QFont.Bold) highlightedFormat = QTextCharFormat(boldFormat) highlightedFormat.setBackground(Qt.yellow) for weekDay in range(1, 8): cell = table.cellAt(0, weekDay - 1) cellCursor = cell.firstCursorPosition() cellCursor.insertText(QDate.longDayName(weekDay), boldFormat) table.insertRows(table.rows(), 1) while date.month() == self.selectedDate.month(): weekDay = date.dayOfWeek() cell = table.cellAt(table.rows() - 1, weekDay - 1) cellCursor = cell.firstCursorPosition() if date == QDate.currentDate(): cellCursor.insertText(str(date.day()), highlightedFormat) else: cellCursor.insertText(str(date.day()), format) date = date.addDays(1) if weekDay == 7 and date.month() == self.selectedDate.month(): table.insertRows(table.rows(), 1) cursor.endEditBlock() self.setWindowTitle( "Calendar for %s %d" % (QDate.longMonthName(self.selectedDate.month()), self.selectedDate.year()) )
def SaveAsPDF(self): """ Save Table as pdf """ fileName, _ = QFileDialog.getSaveFileName(self, "Save File", "", "PDF Files (*.pdf)") if fileName == "": return try: videoName = self.player.fileName timestamp = self.player.player.position() QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) rows = self.VManager.rowCount() columns = self.VManager.columnCount() printer = QPrinter(QPrinter.HighResolution) innerRect = printer.pageRect() sizeF = QSizeF(innerRect.size().width(), innerRect.size().height()) header = QTextDocument() header.setPageSize(sizeF) cursor_header = QTextCursor(header) format1 = QTextCharFormat() format1.setFontPointSize(16) cursor_header.insertHtml( "<p style='text-align: left;'><strong>Video</strong>: %s <strong> TimeStamp</strong>: %s </p>" % (videoName, timestamp)) cursor_header.insertHtml("<br><br><br> ") cursor_header.select(QTextCursor.Document) fragment_header = cursor_header.selection() document = QTextDocument() cursor = QTextCursor(document) tableFormat = QTextTableFormat() tableFormat.setHeaderRowCount(1) tableFormat.setBorderBrush(QBrush(Qt.black)) tableFormat.setAlignment(Qt.AlignHCenter) tableFormat.setCellPadding(5) tableFormat.setCellSpacing(5) cursor.movePosition(QTextCursor.Start, QTextCursor.MoveAnchor) cursor.insertFragment(fragment_header) textTable = cursor.insertTable(rows + 1, columns, tableFormat) tableHeaderFormat = QTextCharFormat() tableHeaderFormat.setBackground(QColor("#DADADA")) for column in range(columns): cell = textTable.cellAt(0, column) cell.setFormat(tableHeaderFormat) cellCursor = cell.firstCursorPosition() cellCursor.insertText( self.VManager.horizontalHeaderItem(column).data( Qt.DisplayRole)) for row in range(rows): for column in range(columns): item = self.VManager.item(row, column) if item is not None: cell = textTable.cellAt(row + 1, column) cellCursor = cell.firstCursorPosition() cellCursor.insertText( self.VManager.item(row, column).text()) cursor.movePosition(QTextCursor.End) printer.setOrientation(QPrinter.Portrait) printer.setPageMargins(30, 100, 10, 40, QPrinter.DevicePixel) printer.setFullPage(True) printer.setPageSize(QPrinter.A4) printer.setOutputFormat(QPrinter.PdfFormat) printer.setOutputFileName(fileName) document.print_(printer) QApplication.restoreOverrideCursor() qgsu.showUserAndLogMessage( QCoreApplication.translate("QgsFmvMetadata", "Succesfully creating PDF")) except Exception as e: QApplication.restoreOverrideCursor() qgsu.showUserAndLogMessage(QCoreApplication.translate( "QgsFmvMetadata", "Failed creating PDF"), level=QGis.Warning) return return
def interp(self): self.reset() curve = self.curveCombobox.currentText().strip() if self.view is None: return if curve not in self.view.pointObjs or len( self.view.pointObjs[curve]) < 2: return xs = [] ys = [] for item in self.view.pointObjs[curve]: xs.append(item.pos().x()) ys.append(item.pos().y()) xs, ys = self.view.pointToCoord(xs, ys) xmin = None xmax = None xstep = None if self.minXTextBox.text().strip(): xmin = str2num(self.minXTextBox.text().strip()) self.view.proj.fitx[0] = xmin if xmin is None: xmin = min(xs) if self.maxXTextBox.text().strip(): xmax = str2num(self.maxXTextBox.text().strip()) self.view.proj.fitx[1] = xmax if xmax is None: xmax = max(xs) if self.stepXTextBox.text().strip(): xstep = str2num(self.stepXTextBox.text().strip()) self.view.proj.fitx[2] = xstep if xstep is None: xstep = (xmax - xmin) / 15 xnew = np.arange(xmin, xmax + xstep / 2, xstep) degree = int(self.degreeComboBox.currentText()) self.view.proj.degree = degree self.view.proj.precision = self.presicion ynew = interp(xs, ys, xnew, degree) xpos, ypos = self.view.coordToPoint(xnew, ynew) for i in range(1, len(xpos)): line = QGraphicsLineItem(xpos[i - 1], ypos[i - 1], xpos[i], ypos[i]) line.setZValue(10) line.setPen(QPen(Qt.yellow, 2, Qt.SolidLine)) self.interpCurveObjs.append(line) self.view.scene.addItem(line) self.minXTextBox.setText(str(xmin)) self.maxXTextBox.setText(str(xmax)) self.stepXTextBox.setText(str(xstep)) text = "interpolation points:\n\n" self.outTextBox.setText(text) cursor = QTextCursor(self.outTextBox.textCursor()) cursor.movePosition(QTextCursor.End) # move the end of documents ttf = QTextTableFormat() # 创建表格对象格式 ttf.setCellPadding(2) # 单元格内文本和边框距离 ttf.setCellSpacing(0) # 单元格线宽 ttf.setAlignment(Qt.AlignLeft) # 表格对齐模式 ttf.setBorder(0.5) # ttf.setColumnWidthConstraints( # (QTextLength(QTextLength.PercentageLength, 50), QTextLength(QTextLength.PercentageLength, 50))) # 百分比定义列宽 ttf.setColumnWidthConstraints( (QTextLength(QTextLength.FixedLength, 75), QTextLength(QTextLength.FixedLength, 75))) # 像素定义列宽 table = cursor.insertTable(len(xnew) + 1, 2, ttf) table.cellAt(0, 0).firstCursorPosition().insertHtml("<b>x</b>") table.cellAt(0, 1).firstCursorPosition().insertHtml("<b>y</b>") for i in range(len(xnew)): table.cellAt(i + 1, 0).firstCursorPosition().insertText( str(round(xnew[i], self.presicion))) table.cellAt(i + 1, 1).firstCursorPosition().insertText( str(round(ynew[i], self.presicion))) self.view.sigModified.emit(True)
def createLetter(self, name, address, orderItems, sendOffers): editor = QTextEdit() tabIndex = self.letters.addTab(editor, name) self.letters.setCurrentIndex(tabIndex) cursor = editor.textCursor() cursor.movePosition(QTextCursor.Start) topFrame = cursor.currentFrame() topFrameFormat = topFrame.frameFormat() topFrameFormat.setPadding(16) topFrame.setFrameFormat(topFrameFormat) textFormat = QTextCharFormat() boldFormat = QTextCharFormat() boldFormat.setFontWeight(QFont.Bold) referenceFrameFormat = QTextFrameFormat() referenceFrameFormat.setBorder(1) referenceFrameFormat.setPadding(8) referenceFrameFormat.setPosition(QTextFrameFormat.FloatRight) referenceFrameFormat.setWidth(QTextLength(QTextLength.PercentageLength, 40)) cursor.insertFrame(referenceFrameFormat) cursor.insertText("A company", boldFormat) cursor.insertBlock() cursor.insertText("321 City Street") cursor.insertBlock() cursor.insertText("Industry Park") cursor.insertBlock() cursor.insertText("Another country") cursor.setPosition(topFrame.lastPosition()) cursor.insertText(name, textFormat) for line in address.split("\n"): cursor.insertBlock() cursor.insertText(line) cursor.insertBlock() cursor.insertBlock() date = QDate.currentDate() cursor.insertText("Date: %s" % date.toString('d MMMM yyyy'), textFormat) cursor.insertBlock() bodyFrameFormat = QTextFrameFormat() bodyFrameFormat.setWidth(QTextLength(QTextLength.PercentageLength, 100)) cursor.insertFrame(bodyFrameFormat) cursor.insertText("I would like to place an order for the following " "items:", textFormat) cursor.insertBlock() cursor.insertBlock() orderTableFormat = QTextTableFormat() orderTableFormat.setAlignment(Qt.AlignHCenter) orderTable = cursor.insertTable(1, 2, orderTableFormat) orderFrameFormat = cursor.currentFrame().frameFormat() orderFrameFormat.setBorder(1) cursor.currentFrame().setFrameFormat(orderFrameFormat) cursor = orderTable.cellAt(0, 0).firstCursorPosition() cursor.insertText("Product", boldFormat) cursor = orderTable.cellAt(0, 1).firstCursorPosition() cursor.insertText("Quantity", boldFormat) for text, quantity in orderItems: row = orderTable.rows() orderTable.insertRows(row, 1) cursor = orderTable.cellAt(row, 0).firstCursorPosition() cursor.insertText(text, textFormat) cursor = orderTable.cellAt(row, 1).firstCursorPosition() cursor.insertText(str(quantity), textFormat) cursor.setPosition(topFrame.lastPosition()) cursor.insertBlock() cursor.insertText("Please update my records to take account of the " "following privacy information:") cursor.insertBlock() offersTable = cursor.insertTable(2, 2) cursor = offersTable.cellAt(0, 1).firstCursorPosition() cursor.insertText("I want to receive more information about your " "company's products and special offers.", textFormat) cursor = offersTable.cellAt(1, 1).firstCursorPosition() cursor.insertText("I do not want to receive any promotional " "information from your company.", textFormat) if sendOffers: cursor = offersTable.cellAt(0, 0).firstCursorPosition() else: cursor = offersTable.cellAt(1, 0).firstCursorPosition() cursor.insertText('X', boldFormat) cursor.setPosition(topFrame.lastPosition()) cursor.insertBlock() cursor.insertText("Sincerely,", textFormat) cursor.insertBlock() cursor.insertBlock() cursor.insertBlock() cursor.insertText(name) self.printAction.setEnabled(True)
def insertCalendar(self): self.editor.clear() cursor = self.editor.textCursor() cursor.beginEditBlock() date = QDate(self.selectedDate.year(), self.selectedDate.month(), 1) tableFormat = QTextTableFormat() tableFormat.setAlignment(Qt.AlignHCenter) tableFormat.setBackground(QColor('#e0e0e0')) tableFormat.setCellPadding(2) tableFormat.setCellSpacing(4) constraints = [QTextLength(QTextLength.PercentageLength, 14), QTextLength(QTextLength.PercentageLength, 14), QTextLength(QTextLength.PercentageLength, 14), QTextLength(QTextLength.PercentageLength, 14), QTextLength(QTextLength.PercentageLength, 14), QTextLength(QTextLength.PercentageLength, 14), QTextLength(QTextLength.PercentageLength, 14)] tableFormat.setColumnWidthConstraints(constraints) table = cursor.insertTable(1, 7, tableFormat) frame = cursor.currentFrame() frameFormat = frame.frameFormat() frameFormat.setBorder(1) frame.setFrameFormat(frameFormat) format = cursor.charFormat() format.setFontPointSize(self.fontSize) boldFormat = QTextCharFormat(format) boldFormat.setFontWeight(QFont.Bold) highlightedFormat = QTextCharFormat(boldFormat) highlightedFormat.setBackground(Qt.yellow) for weekDay in range(1, 8): cell = table.cellAt(0, weekDay-1) cellCursor = cell.firstCursorPosition() cellCursor.insertText(QDate.longDayName(weekDay), boldFormat) table.insertRows(table.rows(), 1) while date.month() == self.selectedDate.month(): weekDay = date.dayOfWeek() cell = table.cellAt(table.rows()-1, weekDay-1) cellCursor = cell.firstCursorPosition() if date == QDate.currentDate(): cellCursor.insertText(str(date.day()), highlightedFormat) else: cellCursor.insertText(str(date.day()), format) date = date.addDays(1) if weekDay == 7 and date.month() == self.selectedDate.month(): table.insertRows(table.rows(), 1) cursor.endEditBlock() self.setWindowTitle("Calendar for %s %d" % (QDate.longMonthName(self.selectedDate.month()), self.selectedDate.year()))
def createTabSolver(self, problem): editor = QTextEdit() tabIndex = self.solvers.addTab(editor, problem.name) self.solvers.setCurrentIndex(tabIndex) cursor = editor.textCursor() cursor.movePosition(QTextCursor.Start) topFrame = cursor.currentFrame() topFrameFormat = topFrame.frameFormat() topFrameFormat.setPadding(16) topFrame.setFrameFormat(topFrameFormat) textFormat = QTextCharFormat() boldFormat = QTextCharFormat() boldFormat.setFontWeight(QFont.Bold) referenceFrameFormat = QTextFrameFormat() referenceFrameFormat.setBorder(1) referenceFrameFormat.setPadding(8) referenceFrameFormat.setPosition(QTextFrameFormat.FloatLeft) referenceFrameFormat.setWidth(QTextLength(QTextLength.PercentageLength, 100)) cursor.insertFrame(referenceFrameFormat) cursor.insertText("Title Problem: ", boldFormat) cursor.insertText(problem.name+"\n", textFormat) cursor.insertText("Criterion: ", boldFormat) if problem.sense == 1: cursor.insertText("Minimize\n",textFormat) else: cursor.insertText("Maximize\n",textFormat) WasNone, dummyVar = problem.fixObjective() cursor.insertText("Status: ", boldFormat) cursor.insertText(str(LpStatus[problem.status])+"\n", textFormat) cursor.insertText("Value Function Objetive: ", boldFormat) cursor.insertText(str(value(problem.objective))+"\n", textFormat) cursor.insertBlock() cursor.insertText("Objective\n", boldFormat) cursor.insertText(str(problem.objective)+"\n", textFormat) cursor.insertBlock() cursor.insertText("Subject To\n", boldFormat) ks = list(problem.constraints.keys()) ks.sort() for k in ks: constraint = problem.constraints[k] print constraint if not list(constraint.keys()): #empty constraint add the dummyVar dummyVar = problem.get_dummyVar() constraint += dummyVar #set this dummyvar to zero so infeasible problems are not made #feasible cursor.insertText((dummyVar == 0.0).asCplexLpConstraint("_dummy") , textFormat) cursor.insertBlock() cursor.insertText(str(k)+" : ", boldFormat) cursor.insertText(str(constraint), textFormat) cursor.insertBlock() vs = problem.variables() cursor.insertBlock() # Bounds on non-"positive" variables # Note: XPRESS and CPLEX do not interpret integer variables without # explicit bounds mip=1 if mip: vg=[v for v in vs if not (v.isPositive() and v.cat==LpContinuous) \ and not v.isBinary()] else: vg = [v for v in vs if not v.isPositive()] if vg: cursor.insertText("Bounds\n", boldFormat) for v in vg: cursor.insertText("%s, " % v.asCplexLpVariable(), textFormat) # Integer non-binary variables if mip: vg = [v for v in vs if v.cat == LpInteger and not v.isBinary()] if vg: cursor.insertText("Generals\n", boldFormat) for v in vg: cursor.insertText("%s, " % v.name, textFormat) # Binary variables vg = [v for v in vs if v.isBinary()] if vg: cursor.insertText("Binaries\n",boldFormat) for v in vg: cursor.insertText("%s, " % v.name, textFormat) cursor.setPosition(topFrame.lastPosition()) bodyFrameFormat = QTextFrameFormat() bodyFrameFormat.setWidth(QTextLength(QTextLength.PercentageLength, 100)) cursor.insertBlock() cursor.insertFrame(bodyFrameFormat) cursor.insertBlock() orderTableFormat = QTextTableFormat() orderTableFormat.setAlignment(Qt.AlignHCenter) orderTable = cursor.insertTable(1, 3, orderTableFormat) orderFrameFormat = cursor.currentFrame().frameFormat() orderFrameFormat.setBorder(1) cursor.currentFrame().setFrameFormat(orderFrameFormat) cursor = orderTable.cellAt(0, 0).firstCursorPosition() cursor.insertText("Variable", boldFormat) cursor = orderTable.cellAt(0, 1).firstCursorPosition() cursor.insertText("Value", boldFormat) cursor = orderTable.cellAt(0, 2).firstCursorPosition() cursor.insertText("Reduced Cost", boldFormat) for v in problem.variables(): row = orderTable.rows() orderTable.insertRows(row, 1) #Name variable cursor = orderTable.cellAt(row, 0).firstCursorPosition() cursor.insertText(v.name, textFormat) #Value variable cursor = orderTable.cellAt(row, 1).firstCursorPosition() cursor.insertText(str(v.varValue), textFormat) #Cost Reduced variable cursor = orderTable.cellAt(row, 2).firstCursorPosition() cursor.insertText(str(v.dj), textFormat) cursor.setPosition(topFrame.lastPosition()) cursor.insertBlock() orderTableFormat = QTextTableFormat() orderTableFormat.setAlignment(Qt.AlignHCenter) orderTable = cursor.insertTable(1, 3, orderTableFormat) orderFrameFormat = cursor.currentFrame().frameFormat() orderFrameFormat.setBorder(1) cursor.currentFrame().setFrameFormat(orderFrameFormat) cursor = orderTable.cellAt(0, 0).firstCursorPosition() cursor.insertText("Constraint", boldFormat) cursor = orderTable.cellAt(0, 1).firstCursorPosition() cursor.insertText("Slack", boldFormat) cursor = orderTable.cellAt(0, 2).firstCursorPosition() cursor.insertText("Shadow Price", boldFormat) for m in range(problem.numConstraints()): row = orderTable.rows() orderTable.insertRows(row, 1) #Name Constraint cursor = orderTable.cellAt(row, 0).firstCursorPosition() cursor.insertText("C"+ str(m+1), textFormat) #Slack Constraint cursor = orderTable.cellAt(row, 1).firstCursorPosition() cursor.insertText(str(problem.constraints.get("_C"+str(m+1)).slack) , textFormat) cursor = orderTable.cellAt(row, 2).firstCursorPosition() cursor.insertText(str(problem.constraints.get("_C"+str(m+1)).pi) , textFormat) self.printAction.setEnabled(True)
def createTabSolver(self, problem): editor = QTextEdit() tabIndex = self.solvers.addTab(editor, problem.name) self.solvers.setCurrentIndex(tabIndex) cursor = editor.textCursor() cursor.movePosition(QTextCursor.Start) topFrame = cursor.currentFrame() topFrameFormat = topFrame.frameFormat() topFrameFormat.setPadding(16) topFrame.setFrameFormat(topFrameFormat) textFormat = QTextCharFormat() boldFormat = QTextCharFormat() boldFormat.setFontWeight(QFont.Bold) referenceFrameFormat = QTextFrameFormat() referenceFrameFormat.setBorder(1) referenceFrameFormat.setPadding(8) referenceFrameFormat.setPosition(QTextFrameFormat.FloatLeft) referenceFrameFormat.setWidth( QTextLength(QTextLength.PercentageLength, 100)) cursor.insertFrame(referenceFrameFormat) cursor.insertText("Title Problem: ", boldFormat) cursor.insertText(problem.name + "\n", textFormat) cursor.insertText("Criterion: ", boldFormat) if problem.sense == 1: cursor.insertText("Minimize\n", textFormat) else: cursor.insertText("Maximize\n", textFormat) WasNone, dummyVar = problem.fixObjective() cursor.insertText("Status: ", boldFormat) cursor.insertText(str(LpStatus[problem.status]) + "\n", textFormat) cursor.insertText("Value Function Objetive: ", boldFormat) cursor.insertText(str(value(problem.objective)) + "\n", textFormat) cursor.insertBlock() cursor.insertText("Objective\n", boldFormat) cursor.insertText(str(problem.objective) + "\n", textFormat) cursor.insertBlock() cursor.insertText("Subject To\n", boldFormat) ks = list(problem.constraints.keys()) ks.sort() for k in ks: constraint = problem.constraints[k] print constraint if not list(constraint.keys()): #empty constraint add the dummyVar dummyVar = problem.get_dummyVar() constraint += dummyVar #set this dummyvar to zero so infeasible problems are not made #feasible cursor.insertText( (dummyVar == 0.0).asCplexLpConstraint("_dummy"), textFormat) cursor.insertBlock() cursor.insertText(str(k) + " : ", boldFormat) cursor.insertText(str(constraint), textFormat) cursor.insertBlock() vs = problem.variables() cursor.insertBlock() # Bounds on non-"positive" variables # Note: XPRESS and CPLEX do not interpret integer variables without # explicit bounds mip = 1 if mip: vg=[v for v in vs if not (v.isPositive() and v.cat==LpContinuous) \ and not v.isBinary()] else: vg = [v for v in vs if not v.isPositive()] if vg: cursor.insertText("Bounds\n", boldFormat) for v in vg: cursor.insertText("%s, " % v.asCplexLpVariable(), textFormat) # Integer non-binary variables if mip: vg = [v for v in vs if v.cat == LpInteger and not v.isBinary()] if vg: cursor.insertText("Generals\n", boldFormat) for v in vg: cursor.insertText("%s, " % v.name, textFormat) # Binary variables vg = [v for v in vs if v.isBinary()] if vg: cursor.insertText("Binaries\n", boldFormat) for v in vg: cursor.insertText("%s, " % v.name, textFormat) cursor.setPosition(topFrame.lastPosition()) bodyFrameFormat = QTextFrameFormat() bodyFrameFormat.setWidth(QTextLength(QTextLength.PercentageLength, 100)) cursor.insertBlock() cursor.insertFrame(bodyFrameFormat) cursor.insertBlock() orderTableFormat = QTextTableFormat() orderTableFormat.setAlignment(Qt.AlignHCenter) orderTable = cursor.insertTable(1, 3, orderTableFormat) orderFrameFormat = cursor.currentFrame().frameFormat() orderFrameFormat.setBorder(1) cursor.currentFrame().setFrameFormat(orderFrameFormat) cursor = orderTable.cellAt(0, 0).firstCursorPosition() cursor.insertText("Variable", boldFormat) cursor = orderTable.cellAt(0, 1).firstCursorPosition() cursor.insertText("Value", boldFormat) cursor = orderTable.cellAt(0, 2).firstCursorPosition() cursor.insertText("Reduced Cost", boldFormat) for v in problem.variables(): row = orderTable.rows() orderTable.insertRows(row, 1) #Name variable cursor = orderTable.cellAt(row, 0).firstCursorPosition() cursor.insertText(v.name, textFormat) #Value variable cursor = orderTable.cellAt(row, 1).firstCursorPosition() cursor.insertText(str(v.varValue), textFormat) #Cost Reduced variable cursor = orderTable.cellAt(row, 2).firstCursorPosition() cursor.insertText(str(v.dj), textFormat) cursor.setPosition(topFrame.lastPosition()) cursor.insertBlock() orderTableFormat = QTextTableFormat() orderTableFormat.setAlignment(Qt.AlignHCenter) orderTable = cursor.insertTable(1, 3, orderTableFormat) orderFrameFormat = cursor.currentFrame().frameFormat() orderFrameFormat.setBorder(1) cursor.currentFrame().setFrameFormat(orderFrameFormat) cursor = orderTable.cellAt(0, 0).firstCursorPosition() cursor.insertText("Constraint", boldFormat) cursor = orderTable.cellAt(0, 1).firstCursorPosition() cursor.insertText("Slack", boldFormat) cursor = orderTable.cellAt(0, 2).firstCursorPosition() cursor.insertText("Shadow Price", boldFormat) for m in range(problem.numConstraints()): row = orderTable.rows() orderTable.insertRows(row, 1) #Name Constraint cursor = orderTable.cellAt(row, 0).firstCursorPosition() cursor.insertText("C" + str(m + 1), textFormat) #Slack Constraint cursor = orderTable.cellAt(row, 1).firstCursorPosition() cursor.insertText( str(problem.constraints.get("_C" + str(m + 1)).slack), textFormat) cursor = orderTable.cellAt(row, 2).firstCursorPosition() cursor.insertText( str(problem.constraints.get("_C" + str(m + 1)).pi), textFormat) self.printAction.setEnabled(True)
def listing(self): #print("listing") if not self.db.open: self.db=pymysql.connect("#IP ADDRESS FOR DB","#USERID","#PASSWORD","#DB_NAME") # Replace your DB details here self.cursor = self.db.cursor() print("Database Connected") checkset=0 sql='SELECT BOOK_ID,AUTHOR,TITLE,PUBLICATION,EDITION,CLASSIFYING_SUBJECT,AVAILABILITY,ROW_NO,COLUMN_NO,PARTITION_NO FROM LIBOT WHERE ' if len(bookidsinpreference): checkset=1 sql+='BOOK_ID IN (' + ','.join(('"{0}"'.format(str(x)) for x in bookidsinpreference)) + ')' if len(authorsinpreference): if checkset==1: sql+=' OR ' else: checkset=1 sql+='AUTHOR IN (' + ','.join(('"{0}"'.format(str(x)) for x in authorsinpreference)) + ')' if len(titlesinpreference): if checkset==1: sql+=' OR ' else: checkset=1 sql+='TITLE IN (' + ','.join(('"{0}"'.format(str(x)) for x in titlesinpreference)) + ')' if len(publicationsinpreference): if checkset==1: sql+=' OR ' else: checkset=1 sql+='PUBLICATION IN (' + ','.join(('"{0}"'.format(str(x)) for x in publicationsinpreference)) + ')' if len(subjectsinpreference): if checkset==1: sql+=' OR ' else: checkset=1 sql+='CLASSIFYING_SUBJECT IN (' + ','.join(('"{0}"'.format(str(x)) for x in subjectsinpreference)) + ')' #print(sql) try: if len(bookidsinpreference) or len(authorsinpreference) or len(titlesinpreference) or len(publicationsinpreference) or len(subjectsinpreference): if not self.db.open: self.db=pymysql.connect("#IP ADDRESS FOR DB","#USERID","#PASSWORD","#DB_NAME") # Replace your DB details here self.cursor = self.db.cursor() print("Database Connected") self.cursor.execute(sql) answersql = self.cursor.fetchall() if len(answersql): entries=['BOOK ID','AUTHOR','TITLE','PUBLICATION','EDITION','SUBJECT','AVAILABILITY','ROW NO','COLUMN NO','PARTITION NO'] textBlockFormat = QTextTableFormat() textBlockFormat.setAlignment(Qt.AlignRight) cursor = QTextCursor() self.chatinterface.moveCursor(QtGui.QTextCursor.End) cursor = self.chatinterface.textCursor() cursor.insertTable(len(answersql)+1,10,textBlockFormat) for val in entries: cursor.insertText(str(val)) cursor.movePosition(QTextCursor.NextCell) for val in answersql: for value in val: cursor.insertText(str(value)) cursor.movePosition(QTextCursor.NextCell) else: self.chatinterface.moveCursor(QtGui.QTextCursor.End) self.chatinterface.append(str(" Your search is empty! ")) cursor = QTextCursor() cursor = self.chatinterface.textCursor() textBlockFormat = cursor.blockFormat() textBlockFormat.setAlignment(Qt.AlignRight) cursor.mergeBlockFormat(textBlockFormat) self.chatinterface.setTextCursor(cursor) self.chatinterface.append(" ") else: self.chatinterface.moveCursor(QtGui.QTextCursor.End) self.chatinterface.append(str(" Your search is empty! ")) cursor = QTextCursor() cursor = self.chatinterface.textCursor() textBlockFormat = cursor.blockFormat() textBlockFormat.setAlignment(Qt.AlignRight) cursor.mergeBlockFormat(textBlockFormat) self.chatinterface.setTextCursor(cursor) self.chatinterface.append(" ") except: self.workerthread = workerThread() self.workerthread.signal.connect(self.crashingmsg) self.workerthread.start()
def insertCalendar(self): self.editor.clear() cursor = self.editor.textCursor() cursor.beginEditBlock() #self.editor.setStyleSheet("QTextTableFormat {border-color:white}") date = QDate(self.selectedDate.year(), self.selectedDate.month(), 1) tableFormat = QTextTableFormat() self.setStyleSheet( "selection-color: yellow;" "selection-background-color: black;" "border-width: 1px;border-style: solid;border-color: white;") tableFormat.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) tableFormat.setBackground(QColor('#ffffff')) tableFormat.setCellPadding(1) tableFormat.setCellSpacing(0) constraints = [ QTextLength(QTextLength.PercentageLength, 7), QTextLength(QTextLength.PercentageLength, 7), QTextLength(QTextLength.PercentageLength, 7), QTextLength(QTextLength.PercentageLength, 7), QTextLength(QTextLength.PercentageLength, 7), QTextLength(QTextLength.PercentageLength, 7), QTextLength(QTextLength.PercentageLength, 7) ] tableFormat.setColumnWidthConstraints(constraints) table = cursor.insertTable(1, 7, tableFormat) # frame = cursor.currentFrame() # frameFormat = frame.frameFormat() # frameFormat.setBorder(2) # frame.setFrameFormat(frameFormat) format = cursor.charFormat() format.setFontPointSize(self.fontSize) boldFormat = QTextCharFormat(format) boldFormat.setFontWeight(QFont.Bold) dayFormat = QTextCharFormat(format) #self.editor.setStyleSheet("color:red;") dayFormat.setForeground(QColor('#f44336')) highlightedFormat = QTextCharFormat(boldFormat) highlightedFormat.setBackground(Qt.yellow) highlightedFormat.setFontUnderline(True) highlightedFormat.setUnderlineColor(QColor('#f44336')) # highlightedFormat.setColor(Qt.white) for weekDay in range(1, 8): cell = table.cellAt(0, weekDay - 1) cellCursor = cell.firstCursorPosition() cellCursor.insertText(day[weekDay - 1], dayFormat) table.insertRows(table.rows(), 1) while date.month() == self.selectedDate.month(): weekDay = date.dayOfWeek() cell = table.cellAt(table.rows() - 1, weekDay - 1) cellCursor = cell.firstCursorPosition() if date == QDate.currentDate(): cellCursor.insertText(str(date.day()), highlightedFormat) else: cellCursor.insertText(str(date.day()), format) date = date.addDays(1) if weekDay == 7 and date.month() == self.selectedDate.month(): table.insertRows(table.rows(), 1) cursor.endEditBlock() self.setWindowTitle("Calendar for %s %d" % (QDate.longMonthName( self.selectedDate.month()), self.selectedDate.year()))