LibreOffice Module cui (master)  1
QrCodeGenDialog.cxx
Go to the documentation of this file.
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.
4  *
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  */
9 
10 #include <QrCodeGenDialog.hxx>
11 
13 #include <tools/stream.hxx>
14 #include <dialmgr.hxx>
15 #include <strings.hrc>
16 #include <unotools/streamwrap.hxx>
17 #include <utility>
18 #include <vcl/svapp.hxx>
19 
20 #if ENABLE_QRCODEGEN
21 #if defined(SYSTEM_QRCODEGEN)
22 #include <qrcodegen/QrCode.hpp>
23 #else
24 #include <QrCode.hpp>
25 #endif
26 #endif
27 
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
30 #include <com/sun/star/drawing/XShape.hpp>
31 #include <com/sun/star/graphic/GraphicProvider.hpp>
32 #include <com/sun/star/graphic/XGraphic.hpp>
33 #include <com/sun/star/drawing/QRCode.hpp>
34 #include <com/sun/star/drawing/QRCodeErrorCorrection.hpp>
35 #include <com/sun/star/graphic/XGraphicProvider.hpp>
36 #include <com/sun/star/io/XInputStream.hpp>
37 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38 #include <com/sun/star/lang/XServiceInfo.hpp>
39 #include <com/sun/star/sheet/XSpreadsheet.hpp>
40 #include <com/sun/star/sheet/XSpreadsheetView.hpp>
41 #include <com/sun/star/text/TextContentAnchorType.hpp>
42 #include <com/sun/star/text/XTextContent.hpp>
43 #include <com/sun/star/text/XTextViewCursor.hpp>
44 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
45 #include <com/sun/star/drawing/XDrawView.hpp>
46 #include <com/sun/star/drawing/XDrawPage.hpp>
47 
48 using namespace css;
49 using namespace css::uno;
50 using namespace css::beans;
51 using namespace css::container;
52 using namespace css::frame;
53 using namespace css::io;
54 using namespace css::lang;
55 using namespace css::frame;
56 using namespace css::sheet;
57 using namespace css::text;
58 using namespace css::drawing;
59 using namespace css::graphic;
60 #if ENABLE_QRCODEGEN
61 using namespace qrcodegen;
62 #endif
63 
64 QrCodeGenDialog::QrCodeGenDialog(weld::Widget* pParent, Reference<XModel> xModel,
65  bool bEditExisting)
66  : GenericDialogController(pParent, "cui/ui/qrcodegen.ui", "QrCodeGenDialog")
67  , m_xModel(std::move(xModel))
68  , m_xEdittext(m_xBuilder->weld_entry("edit_text"))
69  , m_xECC{ m_xBuilder->weld_radio_button("button_low"),
70  m_xBuilder->weld_radio_button("button_medium"),
71  m_xBuilder->weld_radio_button("button_quartile"),
72  m_xBuilder->weld_radio_button("button_high") }
73  , m_xSpinBorder(m_xBuilder->weld_spin_button("edit_border"))
74 #if ENABLE_QRCODEGEN
75  , mpParent(pParent)
76 #endif
77 {
78  if (!bEditExisting)
79  {
80  // TODO: This only works in Writer doc. Should also work in shapes
81  Reference<XIndexAccess> xSelections(m_xModel->getCurrentSelection(), UNO_QUERY);
82  if (xSelections.is())
83  {
84  Reference<XTextRange> xSelection(xSelections->getByIndex(0), UNO_QUERY);
85  if (xSelection.is())
86  m_xEdittext->set_text(xSelection->getString());
87  }
88  return;
89  }
90 
91  Reference<container::XIndexAccess> xIndexAccess(m_xModel->getCurrentSelection(),
92  UNO_QUERY_THROW);
93  Reference<XPropertySet> xProps(xIndexAccess->getByIndex(0), UNO_QUERY_THROW);
94 
95  // Read properties from selected QR Code
96  css::drawing::QRCode aQRCode;
97  xProps->getPropertyValue("QRCodeProperties") >>= aQRCode;
98 
99  m_xEdittext->set_text(aQRCode.Payload);
100 
101  //Get Error Correction Constant from selected QR Code
102  GetErrorCorrection(aQRCode.ErrorCorrection);
103 
104  m_xSpinBorder->set_value(aQRCode.Border);
105 
106  // Mark this as existing shape
107  m_xExistingShapeProperties = xProps;
108 }
109 
111 {
112 #if ENABLE_QRCODEGEN
113  short nRet;
114  while (true)
115  {
116  nRet = GenericDialogController::run();
117  if (nRet == RET_OK)
118  {
119  try
120  {
121  Apply();
122  break;
123  }
124  catch (const qrcodegen::data_too_long&)
125  {
126  std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(
127  mpParent, VclMessageType::Warning, VclButtonsType::Ok,
128  CuiResId(RID_SVXSTR_QRCODEDATALONG)));
129  xBox->run();
130  }
131  }
132  else
133  break;
134  }
135  return nRet;
136 #else
137  return RET_CANCEL;
138 #endif
139 }
140 
142 {
143 #if ENABLE_QRCODEGEN
144  css::drawing::QRCode aQRCode;
145  aQRCode.Payload = m_xEdittext->get_text();
146 
147  bool bLowECCActive(m_xECC[0]->get_active());
148  bool bMediumECCActive(m_xECC[1]->get_active());
149  bool bQuartileECCActive(m_xECC[2]->get_active());
150 
151  if (bLowECCActive)
152  {
153  aQRCode.ErrorCorrection = css::drawing::QRCodeErrorCorrection::LOW;
154  }
155  else if (bMediumECCActive)
156  {
157  aQRCode.ErrorCorrection = css::drawing::QRCodeErrorCorrection::MEDIUM;
158  }
159  else if (bQuartileECCActive)
160  {
161  aQRCode.ErrorCorrection = css::drawing::QRCodeErrorCorrection::QUARTILE;
162  }
163  else
164  {
165  aQRCode.ErrorCorrection = css::drawing::QRCodeErrorCorrection::HIGH;
166  }
167 
168  aQRCode.Border = m_xSpinBorder->get_value();
169 
170  // Read svg and replace placeholder texts
171  OUString aSvgImage = GenerateQRCode(aQRCode.Payload, aQRCode.ErrorCorrection, aQRCode.Border);
172 
173  // Insert/Update graphic
174  SvMemoryStream aSvgStream(4096, 4096);
175  aSvgStream.WriteOString(OUStringToOString(aSvgImage, RTL_TEXTENCODING_UTF8));
176  Reference<XInputStream> xInputStream(new utl::OSeekableInputStreamWrapper(aSvgStream));
178  Reference<XGraphicProvider> xProvider = css::graphic::GraphicProvider::create(xContext);
179 
180  Sequence<PropertyValue> aMediaProperties(1);
181  aMediaProperties[0].Name = "InputStream";
182  aMediaProperties[0].Value <<= xInputStream;
183  Reference<XGraphic> xGraphic(xProvider->queryGraphic(aMediaProperties));
184 
185  bool bIsExistingQRCode = m_xExistingShapeProperties.is();
186  Reference<XPropertySet> xShapeProps;
187  if (bIsExistingQRCode)
188  xShapeProps = m_xExistingShapeProperties;
189  else
190  xShapeProps.set(Reference<lang::XMultiServiceFactory>(m_xModel, UNO_QUERY_THROW)
191  ->createInstance("com.sun.star.drawing.GraphicObjectShape"),
192  UNO_QUERY);
193 
194  xShapeProps->setPropertyValue("Graphic", Any(xGraphic));
195 
196  // Set QRCode properties
197  xShapeProps->setPropertyValue("QRCodeProperties", Any(aQRCode));
198 
199  if (bIsExistingQRCode)
200  return;
201 
202  // Default size
203  Reference<XShape> xShape(xShapeProps, UNO_QUERY);
204  awt::Size aShapeSize;
205  aShapeSize.Height = 4000;
206  aShapeSize.Width = 4000;
207  xShape->setSize(aShapeSize);
208 
209  // Default anchoring
210  xShapeProps->setPropertyValue("AnchorType", Any(TextContentAnchorType_AT_PARAGRAPH));
211 
212  const Reference<XServiceInfo> xServiceInfo(m_xModel, UNO_QUERY_THROW);
213 
214  // Writer
215  if (xServiceInfo->supportsService("com.sun.star.text.TextDocument"))
216  {
217  Reference<XTextContent> xTextContent(xShape, UNO_QUERY_THROW);
218  Reference<XTextViewCursorSupplier> xViewCursorSupplier(m_xModel->getCurrentController(),
219  UNO_QUERY_THROW);
220  Reference<XTextViewCursor> xCursor = xViewCursorSupplier->getViewCursor();
221  // use cursor's XText - it might be in table cell, frame, ...
222  Reference<XText> const xText(xCursor->getText());
223  assert(xText.is());
224  xText->insertTextContent(xCursor, xTextContent, true);
225  return;
226  }
227 
228  // Calc
229  else if (xServiceInfo->supportsService("com.sun.star.sheet.SpreadsheetDocument"))
230  {
231  Reference<XPropertySet> xSheetCell(m_xModel->getCurrentSelection(), UNO_QUERY_THROW);
232  awt::Point aCellPosition;
233  xSheetCell->getPropertyValue("Position") >>= aCellPosition;
234  xShape->setPosition(aCellPosition);
235 
236  Reference<XSpreadsheetView> xView(m_xModel->getCurrentController(), UNO_QUERY_THROW);
237  Reference<XSpreadsheet> xSheet(xView->getActiveSheet(), UNO_SET_THROW);
238  Reference<XDrawPageSupplier> xDrawPageSupplier(xSheet, UNO_QUERY_THROW);
239  Reference<XDrawPage> xDrawPage(xDrawPageSupplier->getDrawPage(), UNO_SET_THROW);
240  Reference<XShapes> xShapes(xDrawPage, UNO_QUERY_THROW);
241 
242  xShapes->add(xShape);
243  return;
244  }
245 
246  //Impress and Draw
247  else if (xServiceInfo->supportsService("com.sun.star.presentation.PresentationDocument")
248  || xServiceInfo->supportsService("com.sun.star.drawing.DrawingDocument"))
249  {
250  Reference<XDrawView> xView(m_xModel->getCurrentController(), UNO_QUERY_THROW);
251  Reference<XDrawPage> xPage(xView->getCurrentPage(), UNO_SET_THROW);
252  Reference<XShapes> xShapes(xPage, UNO_QUERY_THROW);
253 
254  xShapes->add(xShape);
255  return;
256  }
257 
258  else
259  {
260  //Not implemented for math,base and other apps.
261  throw uno::RuntimeException("Not implemented");
262  }
263 #endif
264 }
265 
266 OUString QrCodeGenDialog::GenerateQRCode(OUString aQRText, tools::Long aQRECC, int aQRBorder)
267 {
268 #if ENABLE_QRCODEGEN
269  //Select ECC:: value from aQrECC
270  qrcodegen::QrCode::Ecc bqrEcc = qrcodegen::QrCode::Ecc::LOW;
271 
272  switch (aQRECC)
273  {
274  case 1:
275  {
276  bqrEcc = qrcodegen::QrCode::Ecc::LOW;
277  break;
278  }
279  case 2:
280  {
281  bqrEcc = qrcodegen::QrCode::Ecc::MEDIUM;
282  break;
283  }
284  case 3:
285  {
286  bqrEcc = qrcodegen::QrCode::Ecc::QUARTILE;
287  break;
288  }
289  case 4:
290  {
291  bqrEcc = qrcodegen::QrCode::Ecc::HIGH;
292  break;
293  }
294  }
295 
296  //OuString to char* qrtext
297  OString o = OUStringToOString(aQRText, RTL_TEXTENCODING_UTF8);
298  const char* qrtext = o.pData->buffer;
299 
300  // From QR Code library
301  qrcodegen::QrCode qr0 = qrcodegen::QrCode::encodeText(qrtext, bqrEcc);
302  std::string svg = qr0.toSvgString(aQRBorder);
303  //cstring to OUString
304  return OUString::createFromAscii(svg.c_str());
305 #else
306  (void)aQRText;
307  (void)aQRECC;
308  (void)aQRBorder;
309  return OUString();
310 #endif
311 }
312 
314 {
315  switch (ErrorCorrection)
316  {
317  case css::drawing::QRCodeErrorCorrection::LOW:
318  {
319  m_xECC[0]->set_active(true);
320  break;
321  }
322  case css::drawing::QRCodeErrorCorrection::MEDIUM:
323  {
324  m_xECC[1]->set_active(true);
325  break;
326  }
327  case css::drawing::QRCodeErrorCorrection::QUARTILE:
328  {
329  m_xECC[2]->set_active(true);
330  break;
331  }
332  case css::drawing::QRCodeErrorCorrection::HIGH:
333  {
334  m_xECC[3]->set_active(true);
335  break;
336  }
337  }
338 }
339 
340 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
Reference< frame::XModel > m_xModel
std::unique_ptr< weld::Builder > m_xBuilder
long Long
std::unique_ptr< weld::RadioButton > m_xECC[4]
SvStream & WriteOString(const OString &rStr)
RET_CANCEL
QrCodeGenDialog(weld::Widget *pParent, css::uno::Reference< css::frame::XModel > xModel, bool bEditExisting)
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
void GetErrorCorrection(tools::Long)
std::unique_ptr< weld::Entry > m_xEdittext
css::uno::Reference< css::beans::XPropertySet > m_xExistingShapeProperties
std::unique_ptr< weld::SpinButton > m_xSpinBorder
css::uno::Reference< css::frame::XModel > m_xModel
HRESULT createInstance(REFIID iid, Ifc **ppIfc)
virtual short run() override
OUString CuiResId(const char *pKey)
Definition: cuiresmgr.cxx:23
RegionData_Impl * mpParent
RET_OK
Reference< XComponentContext > getProcessComponentContext()
static OUString GenerateQRCode(OUString aQrText, tools::Long aQrECC, int aQrBorder)
Reference< XGraphic > xGraphic
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, bool bMobile=false)
exports com.sun.star. svg
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo