LibreOffice Module cui (master)  1
SignatureLineDialog.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 <SignatureLineDialog.hxx>
11 
13 #include <comphelper/xmltools.hxx>
14 #include <tools/stream.hxx>
15 #include <unotools/streamwrap.hxx>
16 #include <utility>
17 #include <vcl/weld.hxx>
19 
20 #include <com/sun/star/beans/XPropertySet.hpp>
21 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
22 #include <com/sun/star/drawing/XShape.hpp>
23 #include <com/sun/star/graphic/GraphicProvider.hpp>
24 #include <com/sun/star/graphic/XGraphic.hpp>
25 #include <com/sun/star/graphic/XGraphicProvider.hpp>
26 #include <com/sun/star/io/XInputStream.hpp>
27 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <com/sun/star/sheet/XSpreadsheet.hpp>
30 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
31 #include <com/sun/star/sheet/XSpreadsheetView.hpp>
32 #include <com/sun/star/text/TextContentAnchorType.hpp>
33 #include <com/sun/star/text/XTextContent.hpp>
34 #include <com/sun/star/text/XTextDocument.hpp>
35 #include <com/sun/star/text/XTextViewCursor.hpp>
36 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
37 
38 using namespace css;
39 using namespace css::uno;
40 using namespace css::beans;
41 using namespace css::container;
42 using namespace css::frame;
43 using namespace css::io;
44 using namespace css::lang;
45 using namespace css::frame;
46 using namespace css::sheet;
47 using namespace css::text;
48 using namespace css::drawing;
49 using namespace css::graphic;
50 
51 SignatureLineDialog::SignatureLineDialog(weld::Widget* pParent, Reference<XModel> xModel,
52  bool bEditExisting)
53  : SignatureLineDialogBase(pParent, std::move(xModel), "cui/ui/signatureline.ui",
54  "SignatureLineDialog")
55  , m_xEditName(m_xBuilder->weld_entry("edit_name"))
56  , m_xEditTitle(m_xBuilder->weld_entry("edit_title"))
57  , m_xEditEmail(m_xBuilder->weld_entry("edit_email"))
58  , m_xEditInstructions(m_xBuilder->weld_text_view("edit_instructions"))
59  , m_xCheckboxCanAddComments(m_xBuilder->weld_check_button("checkbox_can_add_comments"))
60  , m_xCheckboxShowSignDate(m_xBuilder->weld_check_button("checkbox_show_sign_date"))
61 {
62  m_xEditInstructions->set_size_request(m_xEditInstructions->get_approximate_digit_width() * 48,
63  m_xEditInstructions->get_text_height() * 5);
64 
65  // No signature line selected - start with empty dialog and set some default values
66  if (!bEditExisting)
67  {
68  m_xCheckboxCanAddComments->set_active(true);
69  m_xCheckboxShowSignDate->set_active(true);
70  return;
71  }
72 
73  Reference<container::XIndexAccess> xIndexAccess(m_xModel->getCurrentSelection(),
74  UNO_QUERY_THROW);
75  Reference<XPropertySet> xProps(xIndexAccess->getByIndex(0), UNO_QUERY_THROW);
76 
77  // Read properties from selected signature line
78  xProps->getPropertyValue("SignatureLineId") >>= m_aSignatureLineId;
79  OUString aSuggestedSignerName;
80  xProps->getPropertyValue("SignatureLineSuggestedSignerName") >>= aSuggestedSignerName;
81  m_xEditName->set_text(aSuggestedSignerName);
82  OUString aSuggestedSignerTitle;
83  xProps->getPropertyValue("SignatureLineSuggestedSignerTitle") >>= aSuggestedSignerTitle;
84  m_xEditTitle->set_text(aSuggestedSignerTitle);
85  OUString aSuggestedSignerEmail;
86  xProps->getPropertyValue("SignatureLineSuggestedSignerEmail") >>= aSuggestedSignerEmail;
87  m_xEditEmail->set_text(aSuggestedSignerEmail);
88  OUString aSigningInstructions;
89  xProps->getPropertyValue("SignatureLineSigningInstructions") >>= aSigningInstructions;
90  m_xEditInstructions->set_text(aSigningInstructions);
91  bool bCanAddComments = false;
92  xProps->getPropertyValue("SignatureLineCanAddComment") >>= bCanAddComments;
93  m_xCheckboxCanAddComments->set_active(bCanAddComments);
94  bool bShowSignDate = false;
95  xProps->getPropertyValue("SignatureLineShowSignDate") >>= bShowSignDate;
96  m_xCheckboxShowSignDate->set_active(bShowSignDate);
97 
98  // Mark this as existing shape
100 }
101 
103 {
104  if (m_aSignatureLineId.isEmpty())
106  = OStringToOUString(comphelper::xml::generateGUIDString(), RTL_TEXTENCODING_ASCII_US);
107  OUString aSignerName(m_xEditName->get_text());
108  OUString aSignerTitle(m_xEditTitle->get_text());
109  OUString aSignerEmail(m_xEditEmail->get_text());
110  OUString aSigningInstructions(m_xEditInstructions->get_text());
111  bool bCanAddComments(m_xCheckboxCanAddComments->get_active());
112  bool bShowSignDate(m_xCheckboxShowSignDate->get_active());
113 
114  // Read svg and replace placeholder texts
115  OUString aSvgImage(svx::SignatureLineHelper::getSignatureImage());
116  aSvgImage = aSvgImage.replaceAll("[SIGNER_NAME]", getCDataString(aSignerName));
117  aSvgImage = aSvgImage.replaceAll("[SIGNER_TITLE]", getCDataString(aSignerTitle));
118 
119  // These are only filled if the signature line is signed.
120  aSvgImage = aSvgImage.replaceAll("[SIGNATURE]", "");
121  aSvgImage = aSvgImage.replaceAll("[SIGNED_BY]", "");
122  aSvgImage = aSvgImage.replaceAll("[INVALID_SIGNATURE]", "");
123  aSvgImage = aSvgImage.replaceAll("[DATE]", "");
124 
125  // Insert/Update graphic
126  Reference<XGraphic> xGraphic = svx::SignatureLineHelper::importSVG(aSvgImage);
127 
128  bool bIsExistingSignatureLine = m_xExistingShapeProperties.is();
129  Reference<XPropertySet> xShapeProps;
130  if (bIsExistingSignatureLine)
131  xShapeProps = m_xExistingShapeProperties;
132  else
133  xShapeProps.set(Reference<lang::XMultiServiceFactory>(m_xModel, UNO_QUERY_THROW)
134  ->createInstance("com.sun.star.drawing.GraphicObjectShape"),
135  UNO_QUERY);
136 
137  xShapeProps->setPropertyValue("Graphic", Any(xGraphic));
138  xShapeProps->setPropertyValue("SignatureLineUnsignedImage", Any(xGraphic));
139 
140  // Set signature line properties
141  xShapeProps->setPropertyValue("IsSignatureLine", Any(true));
142  xShapeProps->setPropertyValue("SignatureLineId", Any(m_aSignatureLineId));
143  if (!aSignerName.isEmpty())
144  xShapeProps->setPropertyValue("SignatureLineSuggestedSignerName", Any(aSignerName));
145  if (!aSignerTitle.isEmpty())
146  xShapeProps->setPropertyValue("SignatureLineSuggestedSignerTitle", Any(aSignerTitle));
147  if (!aSignerEmail.isEmpty())
148  xShapeProps->setPropertyValue("SignatureLineSuggestedSignerEmail", Any(aSignerEmail));
149  if (!aSigningInstructions.isEmpty())
150  xShapeProps->setPropertyValue("SignatureLineSigningInstructions",
151  Any(aSigningInstructions));
152  xShapeProps->setPropertyValue("SignatureLineShowSignDate", Any(bShowSignDate));
153  xShapeProps->setPropertyValue("SignatureLineCanAddComment", Any(bCanAddComments));
154 
155  if (bIsExistingSignatureLine)
156  return;
157 
158  // Default size
159  Reference<XShape> xShape(xShapeProps, UNO_QUERY);
160  awt::Size aShapeSize;
161  aShapeSize.Height = 3000;
162  aShapeSize.Width = 6000;
163  xShape->setSize(aShapeSize);
164 
165  // Default anchoring
166  xShapeProps->setPropertyValue("AnchorType", Any(TextContentAnchorType_AT_PARAGRAPH));
167 
168  // Writer
169  const Reference<XTextDocument> xTextDocument(m_xModel, UNO_QUERY);
170  if (xTextDocument.is())
171  {
172  Reference<XTextContent> xTextContent(xShape, UNO_QUERY_THROW);
173  Reference<XTextViewCursorSupplier> xViewCursorSupplier(m_xModel->getCurrentController(),
174  UNO_QUERY_THROW);
175  Reference<XTextViewCursor> xCursor = xViewCursorSupplier->getViewCursor();
176  // use cursor's XText - it might be in table cell, frame, ...
177  Reference<XText> const xText(xCursor->getText());
178  assert(xText.is());
179  xText->insertTextContent(xCursor, xTextContent, true);
180  return;
181  }
182 
183  // Calc
184  const Reference<XSpreadsheetDocument> xSpreadsheetDocument(m_xModel, UNO_QUERY);
185  if (!xSpreadsheetDocument.is())
186  return;
187 
188  Reference<XPropertySet> xSheetCell(m_xModel->getCurrentSelection(), UNO_QUERY_THROW);
189  awt::Point aCellPosition;
190  xSheetCell->getPropertyValue("Position") >>= aCellPosition;
191  xShape->setPosition(aCellPosition);
192 
193  Reference<XSpreadsheetView> xView(m_xModel->getCurrentController(), UNO_QUERY_THROW);
194  Reference<XSpreadsheet> xSheet(xView->getActiveSheet(), UNO_SET_THROW);
195  Reference<XDrawPageSupplier> xDrawPageSupplier(xSheet, UNO_QUERY_THROW);
196  Reference<XDrawPage> xDrawPage(xDrawPageSupplier->getDrawPage(), UNO_SET_THROW);
197  Reference<XShapes> xShapes(xDrawPage, UNO_QUERY_THROW);
198 
199  xShapes->add(xShape);
200 }
201 
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
std::unique_ptr< weld::Entry > m_xEditEmail
OUString getSignatureImage(const OUString &rType=OUString())
std::unique_ptr< weld::CheckButton > m_xCheckboxShowSignDate
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
std::unique_ptr< weld::CheckButton > m_xCheckboxCanAddComments
css::uno::Reference< css::frame::XModel > m_xModel
uno::Reference< graphic::XGraphic > importSVG(const OUString &rSVG)
std::unique_ptr< weld::Entry > m_xEditName
HRESULT createInstance(REFIID iid, Ifc **ppIfc)
OString generateGUIDString()
static OUString getCDataString(const OUString &rString)
std::unique_ptr< weld::TextView > m_xEditInstructions
SignatureLineDialog(weld::Widget *pParent, css::uno::Reference< css::frame::XModel > xModel, bool bEditExisting)
std::unique_ptr< weld::Entry > m_xEditTitle
css::uno::Reference< css::beans::XPropertySet > m_xExistingShapeProperties
Reference< XGraphic > xGraphic
virtual void Apply() override