22#include <rtl/strbuf.hxx>
25#pragma GCC diagnostic push
26#pragma GCC diagnostic ignored "-Wshadow"
29#include <BarcodeFormat.h>
31#include <MultiFormatWriter.h>
34#pragma GCC diagnostic pop
38#include <BitMatrixIO.h>
41#if __has_include(<Utf.h>)
44#include <TextUtfEncoding.h>
49#include <com/sun/star/beans/XPropertySet.hpp>
50#include <com/sun/star/drawing/XDrawPageSupplier.hpp>
51#include <com/sun/star/drawing/XShape.hpp>
52#include <com/sun/star/graphic/GraphicProvider.hpp>
53#include <com/sun/star/graphic/XGraphic.hpp>
54#include <com/sun/star/drawing/BarCode.hpp>
55#include <com/sun/star/drawing/BarCodeErrorCorrection.hpp>
56#include <com/sun/star/graphic/XGraphicProvider.hpp>
57#include <com/sun/star/io/XInputStream.hpp>
58#include <com/sun/star/lang/XMultiServiceFactory.hpp>
59#include <com/sun/star/lang/XServiceInfo.hpp>
60#include <com/sun/star/sheet/XSpreadsheet.hpp>
61#include <com/sun/star/sheet/XSpreadsheetView.hpp>
62#include <com/sun/star/text/TextContentAnchorType.hpp>
63#include <com/sun/star/text/XTextContent.hpp>
64#include <com/sun/star/text/XTextViewCursor.hpp>
65#include <com/sun/star/text/XTextViewCursorSupplier.hpp>
66#include <com/sun/star/drawing/XDrawView.hpp>
67#include <com/sun/star/drawing/XDrawPage.hpp>
70using namespace css::uno;
71using namespace css::beans;
72using namespace css::container;
73using namespace css::frame;
74using namespace css::io;
75using namespace css::lang;
76using namespace css::sheet;
77using namespace css::text;
78using namespace css::drawing;
79using namespace css::graphic;
86OString ConvertToSVGFormat(
const ZXing::BitMatrix& bitmatrix)
89 const int width = bitmatrix.width();
90 const int height = bitmatrix.height();
91 sb.append(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
92 "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 "
93 + OString::number(width) +
" " + OString::number(height)
94 +
"\" stroke=\"none\">\n"
96 for (
int i = 0;
i < height; ++
i)
98 for (
int j = 0; j < width; ++j)
100 if (bitmatrix.get(j, i))
102 sb.append(
"M" + OString::number(j) +
"," + OString::number(i) +
"h1v1h-1z");
106 sb.append(
"\"/>\n</svg>");
107 return sb.toString();
111std::string GetBarCodeType(
int type)
122OString GenerateQRCode(std::u16string_view aQRText,
tools::Long aQRECC,
int aQRBorder,
int aQRType)
129 case css::drawing::BarCodeErrorCorrection::LOW:
134 case css::drawing::BarCodeErrorCorrection::MEDIUM:
139 case css::drawing::BarCodeErrorCorrection::QUARTILE:
144 case css::drawing::BarCodeErrorCorrection::HIGH:
152 std::string QRText(o);
153 ZXing::BarcodeFormat format = ZXing::BarcodeFormatFromString(GetBarCodeType(aQRType));
154 auto writer = ZXing::MultiFormatWriter(format).setMargin(aQRBorder).setEccLevel(bqrEcc);
155 writer.setEncoding(ZXing::CharacterSet::UTF8);
156#if __has_include(<Utf.h>)
157 ZXing::BitMatrix bitmatrix = writer.encode(ZXing::FromUtf8(QRText), 0, 0);
159 ZXing::BitMatrix bitmatrix = writer.encode(ZXing::TextUtfEncoding::FromUtf8(QRText), 0, 0);
162 return OString(ZXing::ToSVG(bitmatrix));
164 return ConvertToSVGFormat(bitmatrix);
173 : GenericDialogController(pParent,
"cui/ui/qrcodegen.ui",
"QrCodeGenDialog")
175 , m_xEdittext(m_xBuilder->weld_text_view(
"edit_text"))
176 , m_xECC{ m_xBuilder->weld_radio_button(
"button_low"),
177 m_xBuilder->weld_radio_button(
"button_medium"),
178 m_xBuilder->weld_radio_button(
"button_quartile"),
179 m_xBuilder->weld_radio_button(
"button_high") }
180 , m_xSpinBorder(m_xBuilder->weld_spin_button(
"edit_margin"))
181 , m_xComboType(m_xBuilder->weld_combo_box(
"choose_type"))
191 Reference<XIndexAccess> xSelections(
m_xModel->getCurrentSelection(), UNO_QUERY);
192 if (xSelections.is())
194 Reference<XTextRange> xSelection(xSelections->getByIndex(0), UNO_QUERY);
201 Reference<container::XIndexAccess> xIndexAccess(
m_xModel->getCurrentSelection(),
203 Reference<XPropertySet> xProps(xIndexAccess->getByIndex(0), UNO_QUERY_THROW);
206 css::drawing::BarCode aBarCode;
207 xProps->getPropertyValue(
"BarCodeProperties") >>= aBarCode;
228 nRet = GenericDialogController::run();
236 catch (
const std::exception&)
239 mpParent, VclMessageType::Warning, VclButtonsType::Ok,
240 CuiResId(RID_CUISTR_QRCODEDATALONG)));
256 css::drawing::BarCode aBarCode;
260 bool bLowECCActive(
m_xECC[0]->get_active());
261 bool bMediumECCActive(
m_xECC[1]->get_active());
262 bool bQuartileECCActive(
m_xECC[2]->get_active());
266 aBarCode.ErrorCorrection = css::drawing::BarCodeErrorCorrection::LOW;
268 else if (bMediumECCActive)
270 aBarCode.ErrorCorrection = css::drawing::BarCodeErrorCorrection::MEDIUM;
272 else if (bQuartileECCActive)
274 aBarCode.ErrorCorrection = css::drawing::BarCodeErrorCorrection::QUARTILE;
278 aBarCode.ErrorCorrection = css::drawing::BarCodeErrorCorrection::HIGH;
284 OString aSvgImage = GenerateQRCode(aBarCode.Payload, aBarCode.ErrorCorrection, aBarCode.Border,
292 Reference<XGraphicProvider> xProvider = css::graphic::GraphicProvider::create(xContext);
296 Reference<XGraphic> xGraphic(xProvider->queryGraphic(aMediaProperties));
299 Reference<XPropertySet> xShapeProps;
300 if (bIsExistingQRCode)
303 xShapeProps.set(Reference<lang::XMultiServiceFactory>(
m_xModel, UNO_QUERY_THROW)
307 xShapeProps->setPropertyValue(
"Graphic",
Any(xGraphic));
310 xShapeProps->setPropertyValue(
"BarCodeProperties",
Any(aBarCode));
312 if (bIsExistingQRCode)
316 Reference<XShape> xShape(xShapeProps, UNO_QUERY);
317 awt::Size aShapeSize;
318 aShapeSize.Height = 4000;
319 aShapeSize.Width = 4000;
320 xShape->setSize(aShapeSize);
323 xShapeProps->setPropertyValue(
"AnchorType",
Any(TextContentAnchorType_AT_PARAGRAPH));
325 const Reference<XServiceInfo> xServiceInfo(
m_xModel, UNO_QUERY_THROW);
328 if (xServiceInfo->supportsService(
"com.sun.star.text.TextDocument"))
330 Reference<XTextContent> xTextContent(xShape, UNO_QUERY_THROW);
331 Reference<XTextViewCursorSupplier> xViewCursorSupplier(
m_xModel->getCurrentController(),
333 Reference<XTextViewCursor> xCursor = xViewCursorSupplier->getViewCursor();
335 Reference<XText>
const xText(xCursor->getText());
337 xText->insertTextContent(xCursor, xTextContent,
true);
342 else if (xServiceInfo->supportsService(
"com.sun.star.sheet.SpreadsheetDocument"))
344 Reference<XPropertySet> xSheetCell(
m_xModel->getCurrentSelection(), UNO_QUERY_THROW);
345 awt::Point aCellPosition;
346 xSheetCell->getPropertyValue(
"Position") >>= aCellPosition;
347 xShape->setPosition(aCellPosition);
349 Reference<XSpreadsheetView> xView(
m_xModel->getCurrentController(), UNO_QUERY_THROW);
350 Reference<XSpreadsheet> xSheet(xView->getActiveSheet(), UNO_SET_THROW);
351 Reference<XDrawPageSupplier> xDrawPageSupplier(xSheet, UNO_QUERY_THROW);
352 Reference<XDrawPage> xDrawPage(xDrawPageSupplier->getDrawPage(), UNO_SET_THROW);
353 Reference<XShapes> xShapes(xDrawPage, UNO_QUERY_THROW);
355 xShapes->add(xShape);
360 else if (xServiceInfo->supportsService(
"com.sun.star.presentation.PresentationDocument")
361 || xServiceInfo->supportsService(
"com.sun.star.drawing.DrawingDocument"))
363 Reference<XDrawView> xView(
m_xModel->getCurrentController(), UNO_QUERY_THROW);
364 Reference<XDrawPage> xPage(xView->getCurrentPage(), UNO_SET_THROW);
365 Reference<XShapes> xShapes(xPage, UNO_QUERY_THROW);
367 xShapes->add(xShape);
374 throw uno::RuntimeException(
"Not implemented");
381 switch (ErrorCorrection)
383 case css::drawing::BarCodeErrorCorrection::LOW:
385 m_xECC[0]->set_active(
true);
388 case css::drawing::BarCodeErrorCorrection::MEDIUM:
390 m_xECC[1]->set_active(
true);
393 case css::drawing::BarCodeErrorCorrection::QUARTILE:
395 m_xECC[2]->set_active(
true);
398 case css::drawing::BarCodeErrorCorrection::HIGH:
400 m_xECC[3]->set_active(
true);
HRESULT createInstance(REFIID iid, Ifc **ppIfc)
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, const ILibreOfficeKitNotifier *pNotifier=nullptr)
virtual short run() override
QrCodeGenDialog(weld::Widget *pParent, css::uno::Reference< css::frame::XModel > xModel, bool bEditExisting)
std::unique_ptr< weld::SpinButton > m_xSpinBorder
css::uno::Reference< css::beans::XPropertySet > m_xExistingShapeProperties
std::unique_ptr< weld::RadioButton > m_xECC[4]
css::uno::Reference< css::frame::XModel > m_xModel
std::unique_ptr< weld::ComboBox > m_xComboType
std::unique_ptr< weld::TextView > m_xEdittext
void GetErrorCorrection(tools::Long)
SvStream & WriteOString(std::string_view rStr)
OUString CuiResId(TranslateId aKey)
Reference< frame::XModel > m_xModel
RegionData_Impl * mpParent
if(aStr !=aBuf) UpdateName_Impl(m_xFollowLb.get()
Reference< XComponentContext > getProcessComponentContext()
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
Reference< XModel > xModel