LibreOffice Module sw (master)  1
StylePresetsPanel.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 
11 #include <sal/config.h>
12 
13 #include "StylePresetsPanel.hxx"
14 
15 #include <swtypes.hxx>
16 #include <cmdid.h>
17 
18 #include <svl/intitem.hxx>
19 #include <svx/svxids.hrc>
20 #include <svx/dlgutil.hxx>
21 #include <svx/rulritem.hxx>
22 #include <vcl/virdev.hxx>
23 #include <vcl/svapp.hxx>
24 
26 #include <sfx2/dispatch.hxx>
27 #include <sfx2/bindings.hxx>
28 #include <sfx2/viewsh.hxx>
29 #include <sfx2/objsh.hxx>
31 
32 #include <com/sun/star/frame/DocumentTemplates.hpp>
33 #include <com/sun/star/lang/IllegalArgumentException.hpp>
34 
35 #include <sfx2/doctempl.hxx>
36 
37 #include <shellio.hxx>
38 #include <docsh.hxx>
39 
41 #include <sfx2/docfile.hxx>
42 
43 namespace sw { namespace sidebar {
44 
45 namespace {
46 
47 void renderPreview(sfx2::StyleManager* pStyleManager, OutputDevice& aOutputDevice,
48  OUString const & sName, sal_Int32 nHeight, tools::Rectangle const & aRect)
49 {
50  SfxStyleSheetBase* pStyleSheet = pStyleManager->Search(sName, SfxStyleFamily::Para);
51 
52  if (pStyleSheet)
53  {
54  std::unique_ptr<sfx2::StylePreviewRenderer> pStylePreviewRenderer
55  = pStyleManager->CreateStylePreviewRenderer(aOutputDevice, pStyleSheet, nHeight);
56  pStylePreviewRenderer->recalculate();
57  pStylePreviewRenderer->render(aRect, sfx2::StylePreviewRenderer::RenderAlign::TOP);
58  }
59 }
60 
61 BitmapEx GenerateStylePreview(SfxObjectShell& rSource, OUString const & aName)
62 {
63  sfx2::StyleManager* pStyleManager = rSource.GetStyleManager();
64 
66 
67  float fScalingFactor = pVirtualDev->GetDPIScaleFactor();
68 
69  sal_Int32 nMargin = 6 * fScalingFactor;
70 
71  sal_Int32 nPreviewWidth = 144 * fScalingFactor;
72 
73  sal_Int32 nNameHeight = 16 * fScalingFactor;
74  sal_Int32 nTitleHeight = 32 * fScalingFactor;
75  sal_Int32 nHeadingHeight = 24 * fScalingFactor;
76  sal_Int32 nTextBodyHeight = 16 * fScalingFactor;
77  sal_Int32 nBottomMargin = 2 * fScalingFactor;
78 
79  sal_Int32 nNameFontSize = 12 * fScalingFactor;
80 
81  sal_Int32 nPreviewHeight = nNameHeight + nTitleHeight + nHeadingHeight + nTextBodyHeight + nBottomMargin;
82 
83  Size aSize(nPreviewWidth, nPreviewHeight);
84 
85  pVirtualDev->SetOutputSizePixel(aSize);
86 
87  pVirtualDev->SetLineColor(COL_LIGHTGRAY);
88  pVirtualDev->SetFillColor();
89 
90  long y = 0;
91  {
92  pVirtualDev->SetFillColor(COL_LIGHTGRAY);
93  tools::Rectangle aNameRect(0, y, nPreviewWidth, nNameHeight);
94  pVirtualDev->DrawRect(aNameRect);
95 
96  vcl::Font aFont;
97  aFont.SetFontSize(Size(0, nNameFontSize));
98 
99  pVirtualDev->SetFont(aFont);
100 
101  Size aTextSize(pVirtualDev->GetTextWidth(aName), pVirtualDev->GetTextHeight());
102 
103  Point aPoint((aNameRect.GetWidth() / 2.0) - (aTextSize.Width() / 2.0),
104  y + (aNameRect.GetHeight() / 2.0) - (aTextSize.Height() / 2.0));
105 
106  pVirtualDev->DrawText(aPoint, aName);
107 
108  y += nNameHeight;
109  }
110 
111  {
112  tools::Rectangle aRenderRect(Point(nMargin, y), aSize);
113  renderPreview(pStyleManager, *pVirtualDev, "Title", nTitleHeight, aRenderRect);
114  y += nTitleHeight;
115  }
116 
117  {
118  tools::Rectangle aRenderRect(Point(nMargin, y), aSize);
119  renderPreview(pStyleManager, *pVirtualDev, "Heading 1", nHeadingHeight, aRenderRect);
120  y += nHeadingHeight;
121  }
122  {
123  tools::Rectangle aRenderRect(Point(nMargin, y), aSize);
124  renderPreview(pStyleManager, *pVirtualDev, "Text Body", nTextBodyHeight, aRenderRect);
125  }
126 
127  return pVirtualDev->GetBitmapEx(Point(), aSize);
128 }
129 
130 BitmapEx CreatePreview(OUString const & aUrl, OUString const & aName)
131 {
132  SfxMedium aMedium(aUrl, StreamMode::STD_READWRITE);
133  SfxObjectShell* pObjectShell = SfxObjectShell::Current();
134  SfxObjectShellLock xTemplDoc = SfxObjectShell::CreateObjectByFactoryName(pObjectShell->GetFactory().GetFactoryName(), SfxObjectCreateMode::ORGANIZER);
135  xTemplDoc->DoInitNew();
136  if (xTemplDoc->LoadFrom(aMedium))
137  {
138  return GenerateStylePreview(*xTemplDoc, aName);
139  }
140  return BitmapEx();
141 }
142 
143 }
144 
146  const css::uno::Reference<css::frame::XFrame>& rxFrame)
147 {
148  if (pParent == nullptr)
149  throw css::lang::IllegalArgumentException("no parent Window given to StylePresetsPanel::Create", nullptr, 0);
150  if (!rxFrame.is())
151  throw css::lang::IllegalArgumentException("no XFrame given to StylePresetsPanel::Create", nullptr, 1);
152 
153  return VclPtr<StylePresetsPanel>::Create(pParent, rxFrame);
154 }
155 
157  const css::uno::Reference<css::frame::XFrame>& rxFrame)
158  : PanelLayout(pParent, "StylePresetsPanel", "modules/swriter/ui/sidebarstylepresets.ui", rxFrame)
159 {
160  get(mpValueSet, "valueset");
161 
163 
164  mpValueSet->SetDoubleClickHdl(LINK(this, StylePresetsPanel, DoubleClickHdl));
165 
166  RefreshList();
167 }
168 
170 {
171  SfxDocumentTemplates aTemplates;
172  sal_uInt16 nCount = aTemplates.GetRegionCount();
173  for (sal_uInt16 i = 0; i < nCount; ++i)
174  {
175  OUString aRegionName(aTemplates.GetFullRegionName(i));
176  if (aRegionName == "Styles")
177  {
178  for (sal_uInt16 j = 0; j < aTemplates.GetCount(i); ++j)
179  {
180  OUString aName = aTemplates.GetName(i,j);
181  OUString aURL = aTemplates.GetPath(i,j);
182  BitmapEx aPreview = CreatePreview(aURL, aName);
183  mpValueSet->InsertItem(j, Image(aPreview), aName);
184  maTemplateEntries.push_back(std::make_unique<TemplateEntry>(aURL));
185  mpValueSet->SetItemData(j, maTemplateEntries.back().get());
186  }
187  }
188  }
189 }
190 
192 {
193  disposeOnce();
194 }
195 
197 {
199 
201 }
202 
204 {
205  sal_Int32 nItemId = mpValueSet->GetSelectedItemId();
206  TemplateEntry* pEntry = static_cast<TemplateEntry*>(mpValueSet->GetItemData(nItemId));
207 
208  SwDocShell* pDocSh = static_cast<SwDocShell*>(SfxObjectShell::Current());
209  if (pDocSh)
210  {
211  SwgReaderOption aOption;
212  aOption.SetTextFormats(true);
213  aOption.SetNumRules(true);
214  pDocSh->LoadStylesFromFile(pEntry->maURL, aOption, false);
215  }
216 }
217 
218 void StylePresetsPanel::NotifyItemUpdate(const sal_uInt16 /*nSId*/,
219  const SfxItemState /*eState*/,
220  const SfxPoolItem* /*pState*/,
221  const bool /*bIsEnabled*/)
222 {
223 }
224 
225 }} // end of namespace ::sw::sidebar
226 
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual SfxObjectFactory & GetFactory() const =0
void SetFontSize(const Size &)
OUString GetPath(sal_uInt16 nRegion, sal_uInt16 nIdx) const
const OUString & GetFactoryName() const
void disposeAndClear()
static SfxObjectShell * Current()
Dialog to specify the properties of date form field.
Definition: accfrmobj.cxx:40
virtual sfx2::StyleManager * GetStyleManager()
std::vector< std::unique_ptr< TemplateEntry > > maTemplateEntries
void SetTextFormats(const bool bNew)
Definition: shellio.hxx:126
sal_uInt16 GetRegionCount() const
static OutputDevice * GetDefaultDevice()
void SetDoubleClickHdl(const Link< ValueSet *, void > &rLink)
float y
SfxStyleSheetBase * Search(const OUString &rStyleName, SfxStyleFamily eFamily)
ErrCode LoadStylesFromFile(const OUString &rURL, SwgReaderOption const &rOpt, bool bUnoCall)
Definition: docsh2.cxx:1569
IMPL_LINK_NOARG(PageColumnControl, MoreButtonClickHdl_Impl, Button *, void)
static SfxObjectShell * CreateObjectByFactoryName(const OUString &rURL, SfxObjectCreateMode=SfxObjectCreateMode::STANDARD)
int i
virtual bool LoadFrom(SfxMedium &rMedium)
sal_uInt16 GetCount(sal_uInt16 nRegion) const
virtual void dispose() override
void InsertItem(sal_uInt16 nItemId, const Image &rImage)
virtual void NotifyItemUpdate(const sal_uInt16 nSId, const SfxItemState eState, const SfxPoolItem *pState, const bool bIsEnabled) override
virtual void dispose() override
SfxItemState
virtual std::unique_ptr< StylePreviewRenderer > CreateStylePreviewRenderer(OutputDevice &rOutputDev, SfxStyleSheetBase *pStyle, long nMaxHeight)=0
void SetColCount(sal_uInt16 nNewCols=1)
bool DoInitNew(SfxMedium *pMedium=nullptr)
static VclPtr< vcl::Window > Create(vcl::Window *pParent, const css::uno::Reference< css::frame::XFrame > &rxFrame)
static VclPtr< reference_type > Create(Arg &&...arg)
StylePresetsPanel(vcl::Window *pParent, const css::uno::Reference< css::frame::XFrame > &rxFrame)
OUString GetFullRegionName(sal_uInt16 nIdx) const
OUString GetName(sal_uInt16 nRegion, sal_uInt16 nIdx) const
long const nBottomMargin
void SetItemData(sal_uInt16 nItemId, void *pData)
void SetNumRules(const bool bNew)
Definition: shellio.hxx:129