summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMuhammet Kara <muhammet.kara@collabora.com>2019-05-07 22:03:25 +0300
committerMuhammet Kara <muhammet.kara@collabora.com>2019-05-08 13:25:29 +0200
commit141e33bc1d56f7b7af5037988eeb5ca36864a511 (patch)
tree5ea183262170cfbf296557be736b6390e1fff1c4
parentd3581eb79e204419a6906004c199512c1fa48a2c (diff)
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.kara@collabora.com>
-rw-r--r--sfx2/inc/SfxRedactionHelper.hxx26
-rw-r--r--sfx2/source/doc/SfxRedactionHelper.cxx152
-rw-r--r--sfx2/source/doc/objserv.cxx9
3 files changed, 182 insertions, 5 deletions
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 b63340c4913f..8f0b2ca5d1c0 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>
@@ -66,6 +73,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);
@@ -76,6 +89,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,
@@ -115,7 +144,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);
@@ -134,11 +164,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);
@@ -188,4 +220,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 eec137331a54..2063f40aa090 100644
--- a/sfx2/source/doc/objserv.cxx
+++ b/sfx2/source/doc/objserv.cxx
@@ -556,6 +556,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;
@@ -569,7 +576,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();