tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / sd / source / ui / dlg / sdpreslt.cxx
blob0e129f4206d17916c391406353b899c75fd627c2
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 <svl/itemset.hxx>
21 #include <svl/eitem.hxx>
22 #include <svl/stritem.hxx>
23 #include <sfx2/new.hxx>
24 #include <svtools/valueset.hxx>
25 #include <tools/debug.hxx>
26 #include <vcl/image.hxx>
28 #include <strings.hrc>
30 #include <bitmaps.hlst>
31 #include <sdpreslt.hxx>
32 #include <sdattr.hrc>
33 #include <sdresid.hxx>
34 #include <drawdoc.hxx>
35 #include <sdpage.hxx>
36 #include <DrawDocShell.hxx>
37 #include <memory>
39 SdPresLayoutDlg::SdPresLayoutDlg(::sd::DrawDocShell* pDocShell,
40 weld::Window* pWindow, const SfxItemSet& rInAttrs)
41 : GenericDialogController(pWindow, u"modules/simpress/ui/slidedesigndialog.ui"_ustr, u"SlideDesignDialog"_ustr)
42 , mpDocSh(pDocShell)
43 , mrOutAttrs(rInAttrs)
44 , maStrNone(SdResId(STR_NULL))
45 , m_xCbxMasterPage(m_xBuilder->weld_check_button(u"masterpage"_ustr))
46 , m_xCbxCheckMasters(m_xBuilder->weld_check_button(u"checkmasters"_ustr))
47 , m_xBtnLoad(m_xBuilder->weld_button(u"load"_ustr))
48 , m_xVS(new ValueSet(m_xBuilder->weld_scrolled_window(u"selectwin"_ustr, true)))
49 , m_xVSWin(new weld::CustomWeld(*m_xBuilder, u"select"_ustr, *m_xVS))
50 , m_xLabel(m_xBuilder->weld_label(u"label1"_ustr))
52 if (mpDocSh->GetDoc()->GetDocumentType() == DocumentType::Draw)
54 m_xDialog->set_title(SdResId(STR_AVAILABLE_MASTERPAGE));
55 m_xLabel->set_label(SdResId(STR_SELECT_PAGE));
57 else
59 m_xDialog->set_title(SdResId(STR_AVAILABLE_MASTERSLIDE));
60 m_xLabel->set_label(SdResId(STR_SELECT_SLIDE));
62 m_xVSWin->set_size_request(m_xBtnLoad->get_approximate_digit_width() * 60,
63 m_xBtnLoad->get_text_height() * 20);
65 m_xVS->SetDoubleClickHdl(LINK(this, SdPresLayoutDlg, ClickLayoutHdl));
66 m_xBtnLoad->connect_clicked(LINK(this, SdPresLayoutDlg, ClickLoadHdl));
68 Reset();
71 SdPresLayoutDlg::~SdPresLayoutDlg()
75 /**
76 * Initialize
78 void SdPresLayoutDlg::Reset()
80 tools::Long nName;
82 // replace master page
83 if( const SfxBoolItem* pPoolItem = mrOutAttrs.GetItemIfSet( ATTR_PRESLAYOUT_MASTER_PAGE, false ) )
85 bool bMasterPage = pPoolItem->GetValue();
86 m_xCbxMasterPage->set_sensitive( !bMasterPage );
87 m_xCbxMasterPage->set_active( bMasterPage );
90 // remove not used master pages
91 m_xCbxCheckMasters->set_active(false);
93 if( const SfxStringItem* pPoolItem = mrOutAttrs.GetItemIfSet(ATTR_PRESLAYOUT_NAME) )
94 maName = pPoolItem->GetValue();
95 else
96 maName.clear();
98 FillValueSet();
100 mnLayoutCount = maLayoutNames.size();
101 for( nName = 0; nName < mnLayoutCount; nName++ )
103 if (maLayoutNames[nName] == maName)
104 break;
106 DBG_ASSERT(nName < mnLayoutCount, "Layout not found");
108 m_xVS->SelectItem(static_cast<sal_uInt16>(nName) + 1); // Indices of the ValueSets start at 1
113 * Fills the provided Item-Set with dialog box attributes
115 void SdPresLayoutDlg::GetAttr(SfxItemSet& rOutAttrs)
117 short nId = m_xVS->GetSelectedItemId();
118 bool bLoad = nId > mnLayoutCount;
119 rOutAttrs.Put( SfxBoolItem( ATTR_PRESLAYOUT_LOAD, bLoad ) );
121 OUString aLayoutName;
123 if( bLoad )
125 aLayoutName = maName + "#" + maLayoutNames[ nId - 1 ];
127 else if (nId)
129 aLayoutName = maLayoutNames[ nId - 1 ];
130 if( aLayoutName == maStrNone )
131 aLayoutName.clear(); // that way we encode "- nothing -" (see below)
134 rOutAttrs.Put( SfxStringItem( ATTR_PRESLAYOUT_NAME, aLayoutName ) );
135 rOutAttrs.Put( SfxBoolItem( ATTR_PRESLAYOUT_MASTER_PAGE, m_xCbxMasterPage->get_active() ) );
136 rOutAttrs.Put( SfxBoolItem( ATTR_PRESLAYOUT_CHECK_MASTERS, m_xCbxCheckMasters->get_active() ) );
140 * Fills ValueSet with bitmaps
142 void SdPresLayoutDlg::FillValueSet()
144 m_xVS->SetStyle(m_xVS->GetStyle() | WB_ITEMBORDER | WB_DOUBLEBORDER
145 | WB_VSCROLL | WB_NAMEFIELD);
147 m_xVS->SetColCount(2);
148 m_xVS->SetLineCount(2);
149 m_xVS->SetExtraSpacing(2);
151 SdDrawDocument* pDoc = mpDocSh->GetDoc();
153 sal_uInt16 nCount = pDoc->GetMasterPageCount();
155 for (sal_uInt16 nLayout = 0; nLayout < nCount; nLayout++)
157 SdPage* pMaster = static_cast<SdPage*>(pDoc->GetMasterPage(nLayout));
158 if (pMaster->GetPageKind() == PageKind::Standard)
160 OUString aLayoutName(pMaster->GetLayoutName());
161 aLayoutName = aLayoutName.copy(0, aLayoutName.indexOf(SD_LT_SEPARATOR));
162 maLayoutNames.push_back(aLayoutName);
164 Image aBitmap(mpDocSh->GetPagePreviewBitmap(pMaster));
165 m_xVS->InsertItem(static_cast<sal_uInt16>(maLayoutNames.size()), aBitmap, aLayoutName);
169 m_xVS->Show();
173 * DoubleClick handler
175 IMPL_LINK_NOARG(SdPresLayoutDlg, ClickLayoutHdl, ValueSet*, void)
177 m_xDialog->response(RET_OK);
181 * Click handler for load button
183 IMPL_LINK_NOARG(SdPresLayoutDlg, ClickLoadHdl, weld::Button&, void)
185 SfxNewFileDialog aDlg(m_xDialog.get(), SfxNewFileDialogMode::Preview);
186 if (mpDocSh->GetDoc()->GetDocumentType() == DocumentType::Draw)
187 aDlg.set_title(SdResId(STR_LOAD_DRAWING_LAYOUT));
188 else
189 aDlg.set_title(SdResId(STR_LOAD_PRESENTATION_LAYOUT));
190 sal_uInt16 nResult = aDlg.run();
192 bool bCancel = false;
194 switch (nResult)
196 case RET_OK:
198 if (aDlg.IsTemplate())
200 maName = aDlg.GetTemplateFileName();
202 else
204 // that way we encode "- nothing -"
205 maName.clear();
208 break;
210 default:
211 bCancel = true;
214 if( bCancel )
215 return;
217 // check if template already exists
218 OUString aCompareStr(maName);
219 if (aCompareStr.isEmpty())
220 aCompareStr = maStrNone;
222 auto it = std::find(maLayoutNames.begin(), maLayoutNames.end(), aCompareStr);
223 if (it != maLayoutNames.end())
225 sal_uInt16 aPos = static_cast<sal_uInt16>(std::distance(maLayoutNames.begin(), it));
226 // select template
227 m_xVS->SelectItem( aPos + 1 );
229 else
231 // load document in order to determine preview bitmap (if template is selected)
232 if (!maName.isEmpty())
234 // determine document in order to call OpenBookmarkDoc
235 SdDrawDocument* pDoc = mpDocSh->GetDoc();
236 SdDrawDocument* pTemplDoc = pDoc->OpenBookmarkDoc( maName );
238 if (pTemplDoc)
240 ::sd::DrawDocShell* pTemplDocSh= pTemplDoc->GetDocSh();
242 sal_uInt16 nCount = pTemplDoc->GetMasterPageCount();
244 for (sal_uInt16 nLayout = 0; nLayout < nCount; nLayout++)
246 SdPage* pMaster = static_cast<SdPage*>( pTemplDoc->GetMasterPage(nLayout) );
247 if (pMaster->GetPageKind() == PageKind::Standard)
249 OUString aLayoutName(pMaster->GetLayoutName());
250 aLayoutName = aLayoutName.copy(0, aLayoutName.indexOf(SD_LT_SEPARATOR));
251 maLayoutNames.push_back(aLayoutName);
253 Image aBitmap(pTemplDocSh->GetPagePreviewBitmap(pMaster));
254 m_xVS->InsertItem(static_cast<sal_uInt16>(maLayoutNames.size()), aBitmap, aLayoutName);
258 else
260 bCancel = true;
263 pDoc->CloseBookmarkDoc();
265 else
267 // empty layout
268 maLayoutNames.push_back(maStrNone);
269 m_xVS->InsertItem( static_cast<sal_uInt16>(maLayoutNames.size()),
270 Image(BMP_SLIDE_NONE), maStrNone );
273 if (!bCancel)
275 // select template
276 m_xVS->SelectItem( static_cast<sal_uInt16>(maLayoutNames.size()) );
281 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */