android: Update app-specific/MIME type icons
[LibreOffice.git] / cui / source / dialogs / QrCodeGenDialog.cxx
blob6982ca7f8e69548111535e9256b30b4f5dfc21d6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <QrCodeGenDialog.hxx>
12 #include <comphelper/processfactory.hxx>
13 #include <comphelper/propertyvalue.hxx>
14 #include <tools/stream.hxx>
15 #include <dialmgr.hxx>
16 #include <strings.hrc>
17 #include <unotools/streamwrap.hxx>
18 #include <utility>
19 #include <vcl/svapp.hxx>
21 #if ENABLE_ZXING
22 #include <rtl/strbuf.hxx>
24 #ifdef __GNUC__
25 #pragma GCC diagnostic push
26 #pragma GCC diagnostic ignored "-Wshadow"
27 #endif
29 #include <BarcodeFormat.h>
30 #include <BitMatrix.h>
31 #include <MultiFormatWriter.h>
32 #include <TextUtfEncoding.h>
34 #ifdef __GNUC__
35 #pragma GCC diagnostic pop
36 #endif
38 #endif // ENABLE_ZXING
40 #include <com/sun/star/beans/XPropertySet.hpp>
41 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
42 #include <com/sun/star/drawing/XShape.hpp>
43 #include <com/sun/star/graphic/GraphicProvider.hpp>
44 #include <com/sun/star/graphic/XGraphic.hpp>
45 #include <com/sun/star/drawing/BarCode.hpp>
46 #include <com/sun/star/drawing/BarCodeErrorCorrection.hpp>
47 #include <com/sun/star/graphic/XGraphicProvider.hpp>
48 #include <com/sun/star/io/XInputStream.hpp>
49 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
50 #include <com/sun/star/lang/XServiceInfo.hpp>
51 #include <com/sun/star/sheet/XSpreadsheet.hpp>
52 #include <com/sun/star/sheet/XSpreadsheetView.hpp>
53 #include <com/sun/star/text/TextContentAnchorType.hpp>
54 #include <com/sun/star/text/XTextContent.hpp>
55 #include <com/sun/star/text/XTextViewCursor.hpp>
56 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
57 #include <com/sun/star/drawing/XDrawView.hpp>
58 #include <com/sun/star/drawing/XDrawPage.hpp>
60 using namespace css;
61 using namespace css::uno;
62 using namespace css::beans;
63 using namespace css::container;
64 using namespace css::frame;
65 using namespace css::io;
66 using namespace css::lang;
67 using namespace css::sheet;
68 using namespace css::text;
69 using namespace css::drawing;
70 using namespace css::graphic;
72 namespace
74 #if ENABLE_ZXING
75 // Implementation adapted from the answer: https://stackoverflow.com/questions/10789059/create-qr-code-in-vector-image/60638350#60638350
76 OString ConvertToSVGFormat(const ZXing::BitMatrix& bitmatrix)
78 OStringBuffer sb;
79 const int width = bitmatrix.width();
80 const int height = bitmatrix.height();
81 sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
82 "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 "
83 + OString::number(width) + " " + OString::number(height)
84 + "\" stroke=\"none\">\n"
85 "<path d=\"");
86 for (int i = 0; i < height; ++i)
88 for (int j = 0; j < width; ++j)
90 if (bitmatrix.get(j, i))
92 sb.append("M" + OString::number(j) + "," + OString::number(i) + "h1v1h-1z");
96 sb.append("\"/>\n</svg>");
97 return sb.toString();
100 std::string GetBarCodeType(int type)
102 switch (type)
104 case 1:
105 return "Code128";
106 default:
107 return "QRCode";
111 OString GenerateQRCode(std::u16string_view aQRText, tools::Long aQRECC, int aQRBorder, int aQRType)
113 // Associated ZXing error correction levels (0-8) to our constants arbitrarily.
114 int bqrEcc = 1;
116 switch (aQRECC)
118 case css::drawing::BarCodeErrorCorrection::LOW:
120 bqrEcc = 1;
121 break;
123 case css::drawing::BarCodeErrorCorrection::MEDIUM:
125 bqrEcc = 3;
126 break;
128 case css::drawing::BarCodeErrorCorrection::QUARTILE:
130 bqrEcc = 5;
131 break;
133 case css::drawing::BarCodeErrorCorrection::HIGH:
135 bqrEcc = 7;
136 break;
140 OString o = OUStringToOString(aQRText, RTL_TEXTENCODING_UTF8);
141 std::string QRText(o);
142 ZXing::BarcodeFormat format = ZXing::BarcodeFormatFromString(GetBarCodeType(aQRType));
143 auto writer = ZXing::MultiFormatWriter(format).setMargin(aQRBorder).setEccLevel(bqrEcc);
144 writer.setEncoding(ZXing::CharacterSet::UTF8);
145 ZXing::BitMatrix bitmatrix = writer.encode(ZXing::TextUtfEncoding::FromUtf8(QRText), 0, 0);
146 return ConvertToSVGFormat(bitmatrix);
148 #endif
150 } // anonymous namespace
152 QrCodeGenDialog::QrCodeGenDialog(weld::Widget* pParent, Reference<XModel> xModel,
153 bool bEditExisting)
154 : GenericDialogController(pParent, "cui/ui/qrcodegen.ui", "QrCodeGenDialog")
155 , m_xModel(std::move(xModel))
156 , m_xEdittext(m_xBuilder->weld_text_view("edit_text"))
157 , m_xECC{ m_xBuilder->weld_radio_button("button_low"),
158 m_xBuilder->weld_radio_button("button_medium"),
159 m_xBuilder->weld_radio_button("button_quartile"),
160 m_xBuilder->weld_radio_button("button_high") }
161 , m_xSpinBorder(m_xBuilder->weld_spin_button("edit_margin"))
162 , m_xComboType(m_xBuilder->weld_combo_box("choose_type"))
163 #if ENABLE_ZXING
164 , mpParent(pParent)
165 #endif
167 m_xEdittext->set_size_request(m_xEdittext->get_approximate_digit_width() * 28,
168 m_xEdittext->get_height_rows(6));
169 if (!bEditExisting)
171 // TODO: This only works in Writer doc. Should also work in shapes
172 Reference<XIndexAccess> xSelections(m_xModel->getCurrentSelection(), UNO_QUERY);
173 if (xSelections.is())
175 Reference<XTextRange> xSelection(xSelections->getByIndex(0), UNO_QUERY);
176 if (xSelection.is())
177 m_xEdittext->set_text(xSelection->getString());
179 return;
182 Reference<container::XIndexAccess> xIndexAccess(m_xModel->getCurrentSelection(),
183 UNO_QUERY_THROW);
184 Reference<XPropertySet> xProps(xIndexAccess->getByIndex(0), UNO_QUERY_THROW);
186 // Read properties from selected QR Code
187 css::drawing::BarCode aBarCode;
188 xProps->getPropertyValue("BarCodeProperties") >>= aBarCode;
190 m_xEdittext->set_text(aBarCode.Payload);
192 //Get Error Correction Constant from selected QR Code
193 GetErrorCorrection(aBarCode.ErrorCorrection);
195 m_xSpinBorder->set_value(aBarCode.Border);
197 m_xComboType->set_active(aBarCode.Type);
199 // Mark this as existing shape
200 m_xExistingShapeProperties = xProps;
203 short QrCodeGenDialog::run()
205 #if ENABLE_ZXING
206 short nRet;
207 while (true)
209 nRet = GenericDialogController::run();
210 if (nRet == RET_OK)
214 Apply();
215 break;
217 catch (const std::exception&)
219 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(
220 mpParent, VclMessageType::Warning, VclButtonsType::Ok,
221 CuiResId(RID_CUISTR_QRCODEDATALONG)));
222 xBox->run();
225 else
226 break;
228 return nRet;
229 #else
230 return RET_CANCEL;
231 #endif
234 void QrCodeGenDialog::Apply()
236 #if ENABLE_ZXING
237 css::drawing::BarCode aBarCode;
238 aBarCode.Payload = m_xEdittext->get_text();
239 aBarCode.Type = m_xComboType->get_active();
241 bool bLowECCActive(m_xECC[0]->get_active());
242 bool bMediumECCActive(m_xECC[1]->get_active());
243 bool bQuartileECCActive(m_xECC[2]->get_active());
245 if (bLowECCActive)
247 aBarCode.ErrorCorrection = css::drawing::BarCodeErrorCorrection::LOW;
249 else if (bMediumECCActive)
251 aBarCode.ErrorCorrection = css::drawing::BarCodeErrorCorrection::MEDIUM;
253 else if (bQuartileECCActive)
255 aBarCode.ErrorCorrection = css::drawing::BarCodeErrorCorrection::QUARTILE;
257 else
259 aBarCode.ErrorCorrection = css::drawing::BarCodeErrorCorrection::HIGH;
262 aBarCode.Border = m_xSpinBorder->get_value();
264 // Read svg and replace placeholder texts
265 OString aSvgImage = GenerateQRCode(aBarCode.Payload, aBarCode.ErrorCorrection, aBarCode.Border,
266 aBarCode.Type);
268 // Insert/Update graphic
269 SvMemoryStream aSvgStream(4096, 4096);
270 aSvgStream.WriteOString(aSvgImage);
271 Reference<XInputStream> xInputStream(new utl::OSeekableInputStreamWrapper(aSvgStream));
272 Reference<XComponentContext> xContext(comphelper::getProcessComponentContext());
273 Reference<XGraphicProvider> xProvider = css::graphic::GraphicProvider::create(xContext);
275 Sequence<PropertyValue> aMediaProperties{ comphelper::makePropertyValue("InputStream",
276 xInputStream) };
277 Reference<XGraphic> xGraphic(xProvider->queryGraphic(aMediaProperties));
279 bool bIsExistingQRCode = m_xExistingShapeProperties.is();
280 Reference<XPropertySet> xShapeProps;
281 if (bIsExistingQRCode)
282 xShapeProps = m_xExistingShapeProperties;
283 else
284 xShapeProps.set(Reference<lang::XMultiServiceFactory>(m_xModel, UNO_QUERY_THROW)
285 ->createInstance("com.sun.star.drawing.GraphicObjectShape"),
286 UNO_QUERY);
288 xShapeProps->setPropertyValue("Graphic", Any(xGraphic));
290 // Set QRCode properties
291 xShapeProps->setPropertyValue("BarCodeProperties", Any(aBarCode));
293 if (bIsExistingQRCode)
294 return;
296 // Default size
297 Reference<XShape> xShape(xShapeProps, UNO_QUERY);
298 awt::Size aShapeSize;
299 aShapeSize.Height = 4000;
300 aShapeSize.Width = 4000;
301 xShape->setSize(aShapeSize);
303 // Default anchoring
304 xShapeProps->setPropertyValue("AnchorType", Any(TextContentAnchorType_AT_PARAGRAPH));
306 const Reference<XServiceInfo> xServiceInfo(m_xModel, UNO_QUERY_THROW);
308 // Writer
309 if (xServiceInfo->supportsService("com.sun.star.text.TextDocument"))
311 Reference<XTextContent> xTextContent(xShape, UNO_QUERY_THROW);
312 Reference<XTextViewCursorSupplier> xViewCursorSupplier(m_xModel->getCurrentController(),
313 UNO_QUERY_THROW);
314 Reference<XTextViewCursor> xCursor = xViewCursorSupplier->getViewCursor();
315 // use cursor's XText - it might be in table cell, frame, ...
316 Reference<XText> const xText(xCursor->getText());
317 assert(xText.is());
318 xText->insertTextContent(xCursor, xTextContent, true);
319 return;
322 // Calc
323 else if (xServiceInfo->supportsService("com.sun.star.sheet.SpreadsheetDocument"))
325 Reference<XPropertySet> xSheetCell(m_xModel->getCurrentSelection(), UNO_QUERY_THROW);
326 awt::Point aCellPosition;
327 xSheetCell->getPropertyValue("Position") >>= aCellPosition;
328 xShape->setPosition(aCellPosition);
330 Reference<XSpreadsheetView> xView(m_xModel->getCurrentController(), UNO_QUERY_THROW);
331 Reference<XSpreadsheet> xSheet(xView->getActiveSheet(), UNO_SET_THROW);
332 Reference<XDrawPageSupplier> xDrawPageSupplier(xSheet, UNO_QUERY_THROW);
333 Reference<XDrawPage> xDrawPage(xDrawPageSupplier->getDrawPage(), UNO_SET_THROW);
334 Reference<XShapes> xShapes(xDrawPage, UNO_QUERY_THROW);
336 xShapes->add(xShape);
337 return;
340 //Impress and Draw
341 else if (xServiceInfo->supportsService("com.sun.star.presentation.PresentationDocument")
342 || xServiceInfo->supportsService("com.sun.star.drawing.DrawingDocument"))
344 Reference<XDrawView> xView(m_xModel->getCurrentController(), UNO_QUERY_THROW);
345 Reference<XDrawPage> xPage(xView->getCurrentPage(), UNO_SET_THROW);
346 Reference<XShapes> xShapes(xPage, UNO_QUERY_THROW);
348 xShapes->add(xShape);
349 return;
352 else
354 //Not implemented for math,base and other apps.
355 throw uno::RuntimeException("Not implemented");
357 #endif
360 void QrCodeGenDialog::GetErrorCorrection(tools::Long ErrorCorrection)
362 switch (ErrorCorrection)
364 case css::drawing::BarCodeErrorCorrection::LOW:
366 m_xECC[0]->set_active(true);
367 break;
369 case css::drawing::BarCodeErrorCorrection::MEDIUM:
371 m_xECC[1]->set_active(true);
372 break;
374 case css::drawing::BarCodeErrorCorrection::QUARTILE:
376 m_xECC[2]->set_active(true);
377 break;
379 case css::drawing::BarCodeErrorCorrection::HIGH:
381 m_xECC[3]->set_active(true);
382 break;
387 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */