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/image.hxx>
23 #include <vcl/virdev.hxx>
24 #include <vcl/svapp.hxx>
25 
27 #include <sfx2/dispatch.hxx>
28 #include <sfx2/bindings.hxx>
29 #include <sfx2/viewsh.hxx>
30 #include <sfx2/objsh.hxx>
32 
33 #include <com/sun/star/frame/DocumentTemplates.hpp>
34 #include <com/sun/star/lang/IllegalArgumentException.hpp>
35 
36 #include <sfx2/doctempl.hxx>
37 
38 #include <shellio.hxx>
39 #include <docsh.hxx>
40 
42 #include <sfx2/docfile.hxx>
43 
44 namespace sw { namespace sidebar {
45 
46 namespace {
47 
48 void renderPreview(sfx2::StyleManager* pStyleManager, OutputDevice& aOutputDevice,
49  OUString const & sName, sal_Int32 nHeight, tools::Rectangle const & aRect)
50 {
51  SfxStyleSheetBase* pStyleSheet = pStyleManager->Search(sName, SfxStyleFamily::Para);
52 
53  if (pStyleSheet)
54  {
55  std::unique_ptr<sfx2::StylePreviewRenderer> pStylePreviewRenderer
56  = pStyleManager->CreateStylePreviewRenderer(aOutputDevice, pStyleSheet, nHeight);
57  pStylePreviewRenderer->recalculate();
58  pStylePreviewRenderer->render(aRect, sfx2::StylePreviewRenderer::RenderAlign::TOP);
59  }
60 }
61 
62 BitmapEx GenerateStylePreview(SfxObjectShell& rSource, OUString const & aName)
63 {
64  sfx2::StyleManager* pStyleManager = rSource.GetStyleManager();
65 
67 
68  float fScalingFactor = pVirtualDev->GetDPIScaleFactor();
69 
70  sal_Int32 nMargin = 6 * fScalingFactor;
71 
72  sal_Int32 nPreviewWidth = 144 * fScalingFactor;
73 
74  sal_Int32 nNameHeight = 16 * fScalingFactor;
75  sal_Int32 nTitleHeight = 32 * fScalingFactor;
76  sal_Int32 nHeadingHeight = 24 * fScalingFactor;
77  sal_Int32 nTextBodyHeight = 16 * fScalingFactor;
78  sal_Int32 nBottomMargin = 2 * fScalingFactor;
79 
80  sal_Int32 nNameFontSize = 12 * fScalingFactor;
81 
82  sal_Int32 nPreviewHeight = nNameHeight + nTitleHeight + nHeadingHeight + nTextBodyHeight + nBottomMargin;
83 
84  Size aSize(nPreviewWidth, nPreviewHeight);
85 
86  pVirtualDev->SetOutputSizePixel(aSize);
87 
88  pVirtualDev->SetLineColor(COL_LIGHTGRAY);
89  pVirtualDev->SetFillColor();
90 
91  long y = 0;
92  {
93  pVirtualDev->SetFillColor(COL_LIGHTGRAY);
94  tools::Rectangle aNameRect(0, y, nPreviewWidth, nNameHeight);
95  pVirtualDev->DrawRect(aNameRect);
96 
97  vcl::Font aFont;
98  aFont.SetFontSize(Size(0, nNameFontSize));
99 
100  pVirtualDev->SetFont(aFont);
101 
102  Size aTextSize(pVirtualDev->GetTextWidth(aName), pVirtualDev->GetTextHeight());
103 
104  Point aPoint((aNameRect.GetWidth() / 2.0) - (aTextSize.Width() / 2.0),
105  y + (aNameRect.GetHeight() / 2.0) - (aTextSize.Height() / 2.0));
106 
107  pVirtualDev->DrawText(aPoint, aName);
108 
109  y += nNameHeight;
110  }
111 
112  {
113  tools::Rectangle aRenderRect(Point(nMargin, y), aSize);
114  renderPreview(pStyleManager, *pVirtualDev, "Title", nTitleHeight, aRenderRect);
115  y += nTitleHeight;
116  }
117 
118  {
119  tools::Rectangle aRenderRect(Point(nMargin, y), aSize);
120  renderPreview(pStyleManager, *pVirtualDev, "Heading 1", nHeadingHeight, aRenderRect);
121  y += nHeadingHeight;
122  }
123  {
124  tools::Rectangle aRenderRect(Point(nMargin, y), aSize);
125  renderPreview(pStyleManager, *pVirtualDev, "Text Body", nTextBodyHeight, aRenderRect);
126  }
127 
128  return pVirtualDev->GetBitmapEx(Point(), aSize);
129 }
130 
131 BitmapEx CreatePreview(OUString const & aUrl, OUString const & aName)
132 {
133  SfxMedium aMedium(aUrl, StreamMode::STD_READWRITE);
134  SfxObjectShell* pObjectShell = SfxObjectShell::Current();
135  SfxObjectShellLock xTemplDoc = SfxObjectShell::CreateObjectByFactoryName(pObjectShell->GetFactory().GetFactoryName(), SfxObjectCreateMode::ORGANIZER);
136  xTemplDoc->DoInitNew();
137  if (xTemplDoc->LoadFrom(aMedium))
138  {
139  return GenerateStylePreview(*xTemplDoc, aName);
140  }
141  return BitmapEx();
142 }
143 
144 }
145 
147  const css::uno::Reference<css::frame::XFrame>& rxFrame)
148 {
149  if (pParent == nullptr)
150  throw css::lang::IllegalArgumentException("no parent Window given to StylePresetsPanel::Create", nullptr, 0);
151  if (!rxFrame.is())
152  throw css::lang::IllegalArgumentException("no XFrame given to StylePresetsPanel::Create", nullptr, 1);
153 
154  return VclPtr<StylePresetsPanel>::Create(pParent, rxFrame);
155 }
156 
158  const css::uno::Reference<css::frame::XFrame>& rxFrame)
159  : PanelLayout(pParent, "StylePresetsPanel", "modules/swriter/ui/sidebarstylepresets.ui", rxFrame)
160 {
161  get(mpValueSet, "valueset");
162 
164 
165  mpValueSet->SetDoubleClickHdl(LINK(this, StylePresetsPanel, DoubleClickHdl));
166 
167  RefreshList();
168 }
169 
171 {
172  SfxDocumentTemplates aTemplates;
173  sal_uInt16 nCount = aTemplates.GetRegionCount();
174  for (sal_uInt16 i = 0; i < nCount; ++i)
175  {
176  OUString aRegionName(aTemplates.GetFullRegionName(i));
177  if (aRegionName == "Styles")
178  {
179  for (sal_uInt16 j = 0; j < aTemplates.GetCount(i); ++j)
180  {
181  OUString aName = aTemplates.GetName(i,j);
182  OUString aURL = aTemplates.GetPath(i,j);
183  BitmapEx aPreview = CreatePreview(aURL, aName);
184  mpValueSet->InsertItem(j, Image(aPreview), aName);
185  maTemplateEntries.push_back(std::make_unique<TemplateEntry>(aURL));
186  mpValueSet->SetItemData(j, maTemplateEntries.back().get());
187  }
188  }
189  }
190 }
191 
193 {
194  disposeOnce();
195 }
196 
198 {
200 
202 }
203 
205 {
206  sal_Int32 nItemId = mpValueSet->GetSelectedItemId();
207  TemplateEntry* pEntry = static_cast<TemplateEntry*>(mpValueSet->GetItemData(nItemId));
208 
209  SwDocShell* pDocSh = static_cast<SwDocShell*>(SfxObjectShell::Current());
210  if (pDocSh)
211  {
212  SwgReaderOption aOption;
213  aOption.SetTextFormats(true);
214  aOption.SetNumRules(true);
215  pDocSh->LoadStylesFromFile(pEntry->maURL, aOption, false);
216  }
217 }
218 
219 void StylePresetsPanel::NotifyItemUpdate(const sal_uInt16 /*nSId*/,
220  const SfxItemState /*eState*/,
221  const SfxPoolItem* /*pState*/)
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.
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) 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