sfx2/inc/SfxRedactionHelper.hxx        |   26 +++++
 sfx2/source/doc/SfxRedactionHelper.cxx |  152 ++++++++++++++++++++++++++++++++-
 sfx2/source/doc/objserv.cxx            |    9 +
 3 files changed, 182 insertions(+), 5 deletions(-)

New commits:
commit 83e6a2cf6916aa8ee7dd3e4d58ccc0fc5b049712
Author:     Muhammet Kara <muhammet.k...@collabora.com>
AuthorDate: Tue May 7 22:03:25 2019 +0300
Commit:     Muhammet Kara <muhammet.k...@collabora.com>
CommitDate: Wed May 8 13:28:04 2019 +0200

    Respect page margins of the source doc during redaction
    
    Change-Id: Ieaa50a2eba17145180ddd5d2bfc77add4801c43a
    Reviewed-on: https://gerrit.libreoffice.org/71929
    Tested-by: Jenkins
    Reviewed-by: Muhammet Kara <muhammet.k...@collabora.com>
    (cherry picked from commit 141e33bc1d56f7b7af5037988eeb5ca36864a511)
    Reviewed-on: https://gerrit.libreoffice.org/71960
    Tested-by: Muhammet Kara <muhammet.k...@collabora.com>

diff --git a/sfx2/inc/SfxRedactionHelper.hxx b/sfx2/inc/SfxRedactionHelper.hxx
index 8b1bdd57e247..ac15bb790fe9 100644
--- a/sfx2/inc/SfxRedactionHelper.hxx
+++ b/sfx2/inc/SfxRedactionHelper.hxx
@@ -12,6 +12,8 @@
 
 #include <com/sun/star/uno/Reference.hxx>
 #include <com/sun/star/lang/XComponent.hpp>
+#include <com/sun/star/frame/XModel.hpp>
+#include <com/sun/star/beans/XPropertySet.hpp>
 
 #include <sal/types.h>
 #include <rtl/ustring.hxx>
@@ -29,6 +31,15 @@ class GDIMetaFile;
 class DocumentToGraphicRenderer;
 class SfxViewFrame;
 
+struct PageMargins
+{
+    // Page margins in mm100th
+    sal_Int32 nTop;
+    sal_Int32 nBottom;
+    sal_Int32 nLeft;
+    sal_Int32 nRight;
+};
+
 /*
  * Mostly a bunch of static methods to handle the redaction functionality at
  * different points of the process.
@@ -56,13 +67,26 @@ public:
      * */
     static void addPagesToDraw(uno::Reference<XComponent>& xComponent, const 
sal_Int32& nPages,
                                const std::vector<GDIMetaFile>& aMetaFiles,
-                               const std::vector<::Size>& aPageSizes);
+                               const std::vector<::Size>& aPageSizes,
+                               const PageMargins& aPageMargins);
     /*
      * Makes the Redaction toolbar visible to the user.
      * Meant to be called after converting a document to a Draw doc
      * for redaction purposes.
      * */
     static void showRedactionToolbar(SfxViewFrame* pViewFrame);
+
+    /*
+     * Used to get the page margins from the original/source Writer document. 
Then we apply these values to the
+     * pages inserted into Draw for redaction.
+     * */
+    static PageMargins 
getPageMarginsForWriter(css::uno::Reference<css::frame::XModel>& xModel);
+
+    /*
+     * Used to get the page margins from the original/source Calc document. 
Then we apply these values to the
+     * pages inserted into Draw for redaction.
+     * */
+    static PageMargins 
getPageMarginsForCalc(css::uno::Reference<css::frame::XModel>& xModel);
 };
 
 #endif // INCLUDED_CUI_SOURCE_INC_SFXREDACTIONHELPER_HXX
diff --git a/sfx2/source/doc/SfxRedactionHelper.cxx 
b/sfx2/source/doc/SfxRedactionHelper.cxx
index f31e6eb2d73b..3680323a7cf0 100644
--- a/sfx2/source/doc/SfxRedactionHelper.cxx
+++ b/sfx2/source/doc/SfxRedactionHelper.cxx
@@ -11,10 +11,17 @@
 
 #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
-#include <com/sun/star/beans/XPropertySet.hpp>
 #include <com/sun/star/graphic/XGraphic.hpp>
 #include <com/sun/star/frame/XLayoutManager.hpp>
 
+// For page margin related methods
+#include <com/sun/star/style/XStyle.hpp>
+#include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
+#include <com/sun/star/text/XPageCursor.hpp>
+#include <com/sun/star/text/XTextViewCursor.hpp>
+#include <com/sun/star/text/XTextViewCursorSupplier.hpp>
+#include <com/sun/star/sheet/XSpreadsheetView.hpp>
+
 #include <sfx2/request.hxx>
 #include <sfx2/sfxsids.hrc>
 #include <sfx2/viewfrm.hxx>
@@ -65,6 +72,12 @@ OUString SfxRedactionHelper::getStringParam(const 
SfxRequest& rReq, const sal_uI
 
 namespace
 {
+/*
+ * Roundtrip the gdimetafile to and from WMF
+ * to get rid of the position and size irregularities
+ * We better check the conversion method to see what it
+ * actually does to correct these issues, and do it ourselves.
+ * */
 void fixMetaFile(GDIMetaFile& tmpMtf)
 {
     SvMemoryStream aDestStrm(65535, 65535);
@@ -75,6 +88,22 @@ void fixMetaFile(GDIMetaFile& tmpMtf)
 
     ReadWindowMetafile(aDestStrm, tmpMtf);
 }
+
+/*
+ * Sets page margins for a Draw page. Negative values are considered erronous.
+ * */
+void setPageMargins(uno::Reference<beans::XPropertySet>& xPageProperySet,
+                    const PageMargins& aPageMargins)
+{
+    if (aPageMargins.nTop < 0 || aPageMargins.nBottom < 0 || 
aPageMargins.nLeft < 0
+        || aPageMargins.nRight < 0)
+        return;
+
+    xPageProperySet->setPropertyValue("BorderTop", 
css::uno::makeAny(aPageMargins.nTop));
+    xPageProperySet->setPropertyValue("BorderBottom", 
css::uno::makeAny(aPageMargins.nBottom));
+    xPageProperySet->setPropertyValue("BorderLeft", 
css::uno::makeAny(aPageMargins.nLeft));
+    xPageProperySet->setPropertyValue("BorderRight", 
css::uno::makeAny(aPageMargins.nRight));
+}
 }
 
 void SfxRedactionHelper::getPageMetaFilesFromDoc(std::vector<GDIMetaFile>& 
aMetaFiles,
@@ -114,7 +143,8 @@ void 
SfxRedactionHelper::getPageMetaFilesFromDoc(std::vector<GDIMetaFile>& aMeta
 void SfxRedactionHelper::addPagesToDraw(uno::Reference<XComponent>& xComponent,
                                         const sal_Int32& nPages,
                                         const std::vector<GDIMetaFile>& 
aMetaFiles,
-                                        const std::vector<::Size>& aPageSizes)
+                                        const std::vector<::Size>& aPageSizes,
+                                        const PageMargins& aPageMargins)
 {
     // Access the draw pages
     uno::Reference<drawing::XDrawPagesSupplier> xDrawPagesSupplier(xComponent, 
uno::UNO_QUERY);
@@ -133,11 +163,13 @@ void 
SfxRedactionHelper::addPagesToDraw(uno::Reference<XComponent>& xComponent,
         uno::Reference<graphic::XGraphic> xGraph = aGraphic.GetXGraphic();
         uno::Reference<drawing::XDrawPage> xPage = 
xDrawPages->insertNewByIndex(nPage);
 
-        // Set page size
+        // Set page size & margins
         uno::Reference<beans::XPropertySet> xPageProperySet(xPage, 
uno::UNO_QUERY);
         xPageProperySet->setPropertyValue("Height", 
css::uno::makeAny(nPageHeight));
         xPageProperySet->setPropertyValue("Width", 
css::uno::makeAny(nPageWidth));
 
+        setPageMargins(xPageProperySet, aPageMargins);
+
         // Create and insert the shape
         uno::Reference<drawing::XShape> xShape(
             
xFactory->createInstance("com.sun.star.drawing.GraphicObjectShape"), 
uno::UNO_QUERY);
@@ -187,4 +219,118 @@ void 
SfxRedactionHelper::showRedactionToolbar(SfxViewFrame* pViewFrame)
     }
 }
 
+PageMargins
+SfxRedactionHelper::getPageMarginsForWriter(css::uno::Reference<css::frame::XModel>&
 xModel)
+{
+    PageMargins aPageMargins = { -1, -1, -1, -1 };
+
+    Reference<text::XTextViewCursorSupplier> 
xTextViewCursorSupplier(xModel->getCurrentController(),
+                                                                     
UNO_QUERY);
+    if (!xTextViewCursorSupplier.is())
+    {
+        SAL_WARN("sfx.doc", "Ref to xTextViewCursorSupplier is null in 
setPageMargins().");
+        return aPageMargins;
+    }
+
+    Reference<text::XPageCursor> 
xCursor(xTextViewCursorSupplier->getViewCursor(), UNO_QUERY);
+
+    uno::Reference<beans::XPropertySet> xPageProperySet(xCursor, UNO_QUERY);
+    OUString sPageStyleName;
+    Any aValue = xPageProperySet->getPropertyValue("PageStyleName");
+    aValue >>= sPageStyleName;
+
+    Reference<css::style::XStyleFamiliesSupplier> 
xStyleFamiliesSupplier(xModel, UNO_QUERY);
+    if (!xStyleFamiliesSupplier.is())
+    {
+        SAL_WARN("sfx.doc", "Ref to xStyleFamiliesSupplier is null in 
setPageMargins().");
+        return aPageMargins;
+    }
+    uno::Reference<container::XNameAccess> xStyleFamilies(
+        xStyleFamiliesSupplier->getStyleFamilies(), UNO_QUERY);
+
+    if (!xStyleFamilies.is())
+        return aPageMargins;
+
+    uno::Reference<container::XNameAccess> 
xPageStyles(xStyleFamilies->getByName("PageStyles"),
+                                                       UNO_QUERY);
+
+    if (!xPageStyles.is())
+        return aPageMargins;
+
+    uno::Reference<css::style::XStyle> 
xPageStyle(xPageStyles->getByName(sPageStyleName),
+                                                  UNO_QUERY);
+
+    if (!xPageStyle.is())
+        return aPageMargins;
+
+    uno::Reference<beans::XPropertySet> xPageProperties(xPageStyle, 
uno::UNO_QUERY);
+
+    if (!xPageProperties.is())
+        return aPageMargins;
+
+    xPageProperties->getPropertyValue("LeftMargin") >>= aPageMargins.nLeft;
+    xPageProperties->getPropertyValue("RightMargin") >>= aPageMargins.nRight;
+    xPageProperties->getPropertyValue("TopMargin") >>= aPageMargins.nTop;
+    xPageProperties->getPropertyValue("BottomMargin") >>= aPageMargins.nBottom;
+
+    return aPageMargins;
+}
+
+PageMargins
+SfxRedactionHelper::getPageMarginsForCalc(css::uno::Reference<css::frame::XModel>&
 xModel)
+{
+    PageMargins aPageMargins = { -1, -1, -1, -1 };
+    OUString sPageStyleName("Default");
+
+    css::uno::Reference<css::sheet::XSpreadsheetView> xSpreadsheetView(
+        xModel->getCurrentController(), UNO_QUERY);
+
+    if (!xSpreadsheetView.is())
+    {
+        SAL_WARN("sfx.doc", "Ref to xSpreadsheetView is null in 
getPageMarginsForCalc().");
+        return aPageMargins;
+    }
+
+    uno::Reference<beans::XPropertySet> 
xSheetProperties(xSpreadsheetView->getActiveSheet(),
+                                                         UNO_QUERY);
+
+    xSheetProperties->getPropertyValue("PageStyle") >>= sPageStyleName;
+
+    Reference<css::style::XStyleFamiliesSupplier> 
xStyleFamiliesSupplier(xModel, UNO_QUERY);
+    if (!xStyleFamiliesSupplier.is())
+    {
+        SAL_WARN("sfx.doc", "Ref to xStyleFamiliesSupplier is null in 
getPageMarginsForCalc().");
+        return aPageMargins;
+    }
+    uno::Reference<container::XNameAccess> xStyleFamilies(
+        xStyleFamiliesSupplier->getStyleFamilies(), UNO_QUERY);
+
+    if (!xStyleFamilies.is())
+        return aPageMargins;
+
+    uno::Reference<container::XNameAccess> 
xPageStyles(xStyleFamilies->getByName("PageStyles"),
+                                                       UNO_QUERY);
+
+    if (!xPageStyles.is())
+        return aPageMargins;
+
+    uno::Reference<css::style::XStyle> 
xPageStyle(xPageStyles->getByName(sPageStyleName),
+                                                  UNO_QUERY);
+
+    if (!xPageStyle.is())
+        return aPageMargins;
+
+    uno::Reference<beans::XPropertySet> xPageProperties(xPageStyle, 
uno::UNO_QUERY);
+
+    if (!xPageProperties.is())
+        return aPageMargins;
+
+    xPageProperties->getPropertyValue("LeftMargin") >>= aPageMargins.nLeft;
+    xPageProperties->getPropertyValue("RightMargin") >>= aPageMargins.nRight;
+    xPageProperties->getPropertyValue("TopMargin") >>= aPageMargins.nTop;
+    xPageProperties->getPropertyValue("BottomMargin") >>= aPageMargins.nBottom;
+
+    return aPageMargins;
+}
+
 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s 
cinkeys+=0=break: */
diff --git a/sfx2/source/doc/objserv.cxx b/sfx2/source/doc/objserv.cxx
index 1523f43a1724..a21a5c755397 100644
--- a/sfx2/source/doc/objserv.cxx
+++ b/sfx2/source/doc/objserv.cxx
@@ -541,6 +541,13 @@ void SfxObjectShell::ExecFile_Impl(SfxRequest &rReq)
 
             DocumentToGraphicRenderer aRenderer(xSourceDoc, false);
 
+            // Get the page margins of the original doc
+            PageMargins aPageMargins = {-1, -1, -1, -1};
+            if (aRenderer.isWriter())
+                aPageMargins = 
SfxRedactionHelper::getPageMarginsForWriter(xModel);
+            else if (aRenderer.isCalc())
+                aPageMargins = 
SfxRedactionHelper::getPageMarginsForCalc(xModel);
+
             sal_Int32 nPages = aRenderer.getPageCount();
             std::vector< GDIMetaFile > aMetaFiles;
             std::vector< ::Size > aPageSizes;
@@ -554,7 +561,7 @@ void SfxObjectShell::ExecFile_Impl(SfxRequest &rReq)
             uno::Reference<lang::XComponent> xComponent = 
xComponentLoader->loadComponentFromURL("private:factory/sdraw", "_default", 0, 
{});
 
             // Add the doc pages to the new draw document
-            SfxRedactionHelper::addPagesToDraw(xComponent, nPages, aMetaFiles, 
aPageSizes);
+            SfxRedactionHelper::addPagesToDraw(xComponent, nPages, aMetaFiles, 
aPageSizes, aPageMargins);
 
             // Show the Redaction toolbar
             SfxViewFrame* pViewFrame = SfxViewFrame::Current();
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to