Changeset View
Changeset View
Standalone View
Standalone View
src/kjotswidget.cpp
Show First 20 Lines • Show All 1066 Lines • ▼ Show 20 Line(s) | 1061 | if (!filename.isEmpty()) { | |||
---|---|---|---|---|---|
1067 | } | 1067 | } | ||
1068 | exportFile.write(renderSelectionToXml().toUtf8()); | 1068 | exportFile.write(renderSelectionToXml().toUtf8()); | ||
1069 | 1069 | | |||
1070 | exportFile.close(); | 1070 | exportFile.close(); | ||
1071 | } | 1071 | } | ||
1072 | m_loader->setTheme(currentTheme); | 1072 | m_loader->setTheme(currentTheme); | ||
1073 | } | 1073 | } | ||
1074 | 1074 | | |||
1075 | std::unique_ptr<QPrinter> KJotsWidget::setupPrinter(QPrinter::PrinterMode mode) | ||||
dvratil: If your intention is for the caller to own the QPrinter object, `std::unique_ptr` is a better… | |||||
1076 | { | ||||
1077 | auto printer = std::make_unique<QPrinter>(mode); | ||||
You want to make this a unique_ptr already so the QPrinter gets destroyed in case an exception is thrown in one of the lines above (which it won't, Qt doesn't throw, but you know - good practices :)), or an early return is implemented in this method later on. It's OK to bump required C++ to 14 or even 17 so you can use make_unique auto printer = std::make_unique<QPrinter>(mode); dvratil: You want to make this a `unique_ptr` already so the `QPrinter` gets destroyed in case an… | |||||
1078 | printer->setDocName(QStringLiteral("KJots_Print")); | ||||
1079 | printer->setCreator(QStringLiteral("KJots")); | ||||
1080 | if (!activeEditor()->textCursor().selection().isEmpty()) { | ||||
1081 | printer->setPrintRange(QPrinter::Selection); | ||||
1082 | } | ||||
1083 | return printer; | ||||
1084 | } | ||||
1085 | | ||||
1075 | void KJotsWidget::printPreviewSelection() | 1086 | void KJotsWidget::printPreviewSelection() | ||
1076 | { | 1087 | { | ||
1077 | QPrinter printer(QPrinter::HighResolution); | 1088 | auto printer = setupPrinter(QPrinter::ScreenResolution); | ||
1078 | printer.setDocName(QLatin1String("KJots_Print")); | 1089 | QPrintPreviewDialog previewdlg(printer.get(), this); | ||
1079 | printer.setFullPage(false); | 1090 | connect(&previewdlg, &QPrintPreviewDialog::paintRequested, this, &KJotsWidget::print); | ||
1080 | printer.setCreator(QLatin1String("KJots")); | | |||
1081 | QPrintPreviewDialog previewdlg(&printer, nullptr); | | |||
1082 | print(printer); | | |||
1083 | previewdlg.exec(); | 1091 | previewdlg.exec(); | ||
1084 | } | 1092 | } | ||
1085 | 1093 | | |||
1086 | void KJotsWidget::printSelection() | 1094 | void KJotsWidget::printSelection() | ||
1087 | { | 1095 | { | ||
1088 | 1096 | auto printer = setupPrinter(QPrinter::HighResolution); | |||
1089 | QPrinter printer(QPrinter::HighResolution); | 1097 | QPrintDialog printDialog(printer.get(), this); | ||
1090 | printer.setDocName(QLatin1String("KJots_Print")); | 1098 | if (printDialog.exec() == QDialog::Accepted) { | ||
1091 | printer.setFullPage(false); | 1099 | print(printer.get()); | ||
1092 | printer.setCreator(QLatin1String("KJots")); | | |||
1093 | //Not supported in Qt? | | |||
1094 | //printer->setPageSelection(QPrinter::ApplicationSide); | | |||
1095 | | ||||
1096 | //KPrinter::pageList() only works with ApplicationSide. ApplicationSide | | |||
1097 | //requires min/max pages. How am I supposed to tell how many pages there | | |||
1098 | //are before I setup the printer? | | |||
1099 | | ||||
1100 | QPointer<QPrintDialog> printDialog = new QPrintDialog(&printer, this); | | |||
1101 | | ||||
1102 | QAbstractPrintDialog::PrintDialogOptions options = printDialog->enabledOptions(); | | |||
1103 | options &= ~QAbstractPrintDialog::PrintPageRange; | | |||
1104 | if (activeEditor()->textCursor().hasSelection()) { | | |||
1105 | options |= QAbstractPrintDialog::PrintSelection; | | |||
1106 | } | 1100 | } | ||
1107 | printDialog->setEnabledOptions(options); | | |||
1108 | | ||||
1109 | printDialog->setWindowTitle(i18n("Send To Printer")); | | |||
1110 | if (printDialog->exec() == QDialog::Accepted) { | | |||
1111 | print(printer); | | |||
1112 | } | | |||
1113 | delete printDialog; | | |||
1114 | } | 1101 | } | ||
1115 | 1102 | | |||
1116 | void KJotsWidget::print(QPrinter &printer) | 1103 | void KJotsWidget::print(QPrinter *printer) | ||
1117 | { | 1104 | { | ||
1118 | QTextDocument printDocument; | 1105 | QTextDocument printDocument; | ||
1119 | if (printer.printRange() == QPrinter::Selection) { | 1106 | if (printer->printRange() == QPrinter::Selection) { | ||
1120 | printDocument.setHtml(activeEditor()->textCursor().selection().toHtml()); | 1107 | printDocument.setHtml(activeEditor()->textCursor().selection().toHtml()); | ||
1121 | } else { | 1108 | } else { | ||
1122 | //QTextCursor printCursor ( &printDocument ); | | |||
1123 | QString currentTheme = m_loader->themeName(); | 1109 | QString currentTheme = m_loader->themeName(); | ||
1124 | m_loader->setTheme(QLatin1String("default")); | 1110 | m_loader->setTheme(QLatin1String("default")); | ||
1125 | printDocument.setHtml(renderSelectionToHtml()); | 1111 | printDocument.setHtml(renderSelectionToHtml()); | ||
1126 | m_loader->setTheme(currentTheme); | 1112 | m_loader->setTheme(currentTheme); | ||
1127 | } | 1113 | } | ||
1128 | 1114 | printDocument.print(printer); | |||
1129 | QPainter p(&printer); | | |||
1130 | | ||||
1131 | // Check that there is a valid device to print to. | | |||
1132 | if (p.isActive()) { | | |||
1133 | QTextDocument *doc = &printDocument; | | |||
1134 | | ||||
1135 | QRectF body = QRectF(QPointF(0, 0), doc->pageSize()); | | |||
1136 | QPointF pageNumberPos; | | |||
1137 | | ||||
1138 | QAbstractTextDocumentLayout *layout = doc->documentLayout(); | | |||
1139 | layout->setPaintDevice(p.device()); | | |||
1140 | | ||||
1141 | const int dpiy = p.device()->logicalDpiY(); | | |||
1142 | | ||||
1143 | const int margin = (int)((2 / 2.54) * dpiy); // 2 cm margins | | |||
1144 | QTextFrameFormat fmt = doc->rootFrame()->frameFormat(); | | |||
1145 | fmt.setMargin(margin); | | |||
1146 | doc->rootFrame()->setFrameFormat(fmt); | | |||
1147 | | ||||
1148 | body = QRectF(0, 0, p.device()->width(), p.device()->height()); | | |||
1149 | pageNumberPos = QPointF(body.width() - margin, | | |||
1150 | body.height() - margin | | |||
1151 | + QFontMetrics(doc->defaultFont(), p.device()).ascent() | | |||
1152 | + 5 * p.device()->logicalDpiY() / 72); | | |||
1153 | | ||||
1154 | doc->setPageSize(body.size()); | | |||
1155 | | ||||
1156 | int docCopies = printer.numCopies(); | | |||
1157 | for (int copy = 0; copy < docCopies; ++copy) { | | |||
1158 | | ||||
1159 | int lastPage = layout->pageCount(); | | |||
1160 | for (int page = 1; page <= lastPage ; ++page) { | | |||
1161 | p.save(); | | |||
1162 | p.translate(body.left(), body.top() - (page - 1) * body.height()); | | |||
1163 | QRectF view(0, (page - 1) * body.height(), body.width(), body.height()); | | |||
1164 | | ||||
1165 | QAbstractTextDocumentLayout *layout = doc->documentLayout(); | | |||
1166 | QAbstractTextDocumentLayout::PaintContext ctx; | | |||
1167 | | ||||
1168 | p.setClipRect(view); | | |||
1169 | ctx.clip = view; | | |||
1170 | | ||||
1171 | // don't use the system palette text as default text color, on HP/UX | | |||
1172 | // for example that's white, and white text on white paper doesn't | | |||
1173 | // look that nice | | |||
1174 | ctx.palette.setColor(QPalette::Text, Qt::black); | | |||
1175 | | ||||
1176 | layout->draw(&p, ctx); | | |||
1177 | | ||||
1178 | if (!pageNumberPos.isNull()) { | | |||
1179 | p.setClipping(false); | | |||
1180 | p.setFont(QFont(doc->defaultFont())); | | |||
1181 | const QString pageString = QString::number(page); | | |||
1182 | | ||||
1183 | p.drawText(qRound(pageNumberPos.x() - p.fontMetrics().horizontalAdvance(pageString)), | | |||
1184 | qRound(pageNumberPos.y() + view.top()), | | |||
1185 | pageString); | | |||
1186 | } | | |||
1187 | | ||||
1188 | p.restore(); | | |||
1189 | | ||||
1190 | if ((page + 1) <= lastPage) { | | |||
1191 | printer.newPage(); | | |||
1192 | } | | |||
1193 | } | | |||
1194 | } | | |||
1195 | } | | |||
1196 | | ||||
1197 | } | 1115 | } | ||
1198 | 1116 | | |||
1199 | void KJotsWidget::selectNext(int role, int step) | 1117 | void KJotsWidget::selectNext(int role, int step) | ||
1200 | { | 1118 | { | ||
1201 | QModelIndexList list = treeview->selectionModel()->selectedRows(); | 1119 | QModelIndexList list = treeview->selectionModel()->selectedRows(); | ||
1202 | Q_ASSERT(list.size() == 1); | 1120 | Q_ASSERT(list.size() == 1); | ||
1203 | 1121 | | |||
1204 | QModelIndex idx = list.at(0); | 1122 | QModelIndex idx = list.at(0); | ||
▲ Show 20 Lines • Show All 626 Lines • Show Last 20 Lines |
If your intention is for the caller to own the QPrinter object, std::unique_ptr is a better choice.