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