Kaydet (Commit) 141e33bc authored tarafından Muhammet Kara's avatar Muhammet Kara

Respect page margins of the source doc during redaction

Change-Id: Ieaa50a2eba17145180ddd5d2bfc77add4801c43a
Reviewed-on: https://gerrit.libreoffice.org/71929
Tested-by: Jenkins
Reviewed-by: 's avatarMuhammet Kara <muhammet.kara@collabora.com>
üst d3581eb7
......@@ -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
......
......@@ -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: */
......@@ -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();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment