Get the style color and number just once
[LibreOffice.git] / sd / source / ui / sidebar / MasterPageContainerProviders.cxx
blobfecd93f36f7c1065f350f7db72be52cd01734c9e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "MasterPageContainerProviders.hxx"
22 #include <DrawDocShell.hxx>
23 #include <drawdoc.hxx>
24 #include <sdpage.hxx>
25 #include <PreviewRenderer.hxx>
26 #include <svl/eitem.hxx>
27 #include <sfx2/app.hxx>
28 #include <sfx2/sfxsids.hrc>
29 #include <sfx2/thumbnailview.hxx>
30 #include <utility>
31 #include <vcl/image.hxx>
32 #include <comphelper/diagnose_ex.hxx>
33 #include <sal/log.hxx>
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::uno;
38 namespace sd::sidebar {
40 //===== PagePreviewProvider ===================================================
42 PagePreviewProvider::PagePreviewProvider()
46 Image PagePreviewProvider::operator () (
47 int nWidth,
48 SdPage* pPage,
49 ::sd::PreviewRenderer& rRenderer)
51 Image aPreview;
53 if (pPage != nullptr)
55 // Use the given renderer to create a preview of the given page
56 // object.
57 aPreview = rRenderer.RenderPage(
58 pPage,
59 nWidth);
62 return aPreview;
65 int PagePreviewProvider::GetCostIndex()
67 return 5;
70 bool PagePreviewProvider::NeedsPageObject()
72 return true;
75 //===== TemplatePreviewProvider ===============================================
77 TemplatePreviewProvider::TemplatePreviewProvider (OUString sURL)
78 : msURL(std::move(sURL))
82 Image TemplatePreviewProvider::operator() (
83 int,
84 SdPage*,
85 ::sd::PreviewRenderer&)
87 return Image(ThumbnailView::readThumbnail(msURL));
90 int TemplatePreviewProvider::GetCostIndex()
92 return 10;
95 bool TemplatePreviewProvider::NeedsPageObject()
97 return false;
100 //===== TemplatePageObjectProvider =============================================
102 TemplatePageObjectProvider::TemplatePageObjectProvider (OUString sURL)
103 : msURL(std::move(sURL))
107 SdPage* TemplatePageObjectProvider::operator() (SdDrawDocument*)
109 SdPage* pPage = nullptr;
111 mxDocumentShell = nullptr;
114 // Load the template document and return its first page.
115 ::sd::DrawDocShell* pDocumentShell = LoadDocument (msURL);
116 if (pDocumentShell != nullptr)
118 SdDrawDocument* pDocument = pDocumentShell->GetDoc();
119 if (pDocument != nullptr)
121 pPage = pDocument->GetMasterSdPage(0, PageKind::Standard);
122 // In order to make the newly loaded master page deletable
123 // when copied into documents it is marked as no "precious".
124 // When it is modified then it is marked as "precious".
125 if (pPage != nullptr)
126 pPage->SetPrecious(false);
130 catch (const uno::RuntimeException&)
132 DBG_UNHANDLED_EXCEPTION("sd");
133 pPage = nullptr;
136 return pPage;
139 ::sd::DrawDocShell* TemplatePageObjectProvider::LoadDocument (const OUString& sFileName)
141 SfxApplication* pSfxApp = SfxGetpApp();
142 std::unique_ptr<SfxItemSet> pSet(new SfxAllItemSet (pSfxApp->GetPool()));
143 pSet->Put (SfxBoolItem (SID_TEMPLATE, true));
144 pSet->Put (SfxBoolItem (SID_PREVIEW, true));
145 if (pSfxApp->LoadTemplate (mxDocumentShell, sFileName, std::move(pSet)))
147 mxDocumentShell = nullptr;
149 SfxObjectShell* pShell = mxDocumentShell;
150 return dynamic_cast< ::sd::DrawDocShell *>( pShell );
153 int TemplatePageObjectProvider::GetCostIndex()
155 return 20;
158 //===== DefaultPageObjectProvider ==============================================
160 DefaultPageObjectProvider::DefaultPageObjectProvider()
164 SdPage* DefaultPageObjectProvider::operator () (SdDrawDocument* pContainerDocument)
166 SdPage* pLocalMasterPage = nullptr;
167 if (pContainerDocument != nullptr)
169 SdPage* pLocalSlide = pContainerDocument->GetSdPage(0, PageKind::Standard);
170 if (pLocalSlide!=nullptr && pLocalSlide->TRG_HasMasterPage())
171 pLocalMasterPage = dynamic_cast<SdPage*>(&pLocalSlide->TRG_GetMasterPage());
174 if (pLocalMasterPage == nullptr)
176 SAL_WARN( "sd", "can not create master page for slide");
179 return pLocalMasterPage;
182 int DefaultPageObjectProvider::GetCostIndex()
184 return 15;
187 //===== ExistingPageProvider ==================================================
189 ExistingPageProvider::ExistingPageProvider (SdPage* pPage)
190 : mpPage(pPage)
194 SdPage* ExistingPageProvider::operator() (SdDrawDocument*)
196 return mpPage;
199 int ExistingPageProvider::GetCostIndex()
201 return 0;
204 } // end of namespace sd::sidebar
206 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */