tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / sd / source / ui / dlg / custsdlg.cxx
blob3cec4c1d738e9dcca1386aecad569a082a31ce07
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 <custsdlg.hxx>
22 #include <strings.hrc>
23 #include <sdresid.hxx>
25 #include <drawdoc.hxx>
26 #include <sdpage.hxx>
27 #include <cusshow.hxx>
28 #include <customshowlist.hxx>
29 #include <vcl/svapp.hxx>
30 #include <vcl/weld.hxx>
31 #include <unotools/charclass.hxx>
32 #include <tools/debug.hxx>
34 // SdCustomShowDlg
35 SdCustomShowDlg::SdCustomShowDlg(weld::Window* pWindow, SdDrawDocument& rDrawDoc)
36 : GenericDialogController(pWindow, u"modules/simpress/ui/customslideshows.ui"_ustr, u"CustomSlideShows"_ustr)
37 , rDoc(rDrawDoc)
38 , pCustomShowList(nullptr)
39 , m_xLbCustomShows(m_xBuilder->weld_tree_view(u"customshowlist"_ustr))
40 , m_xBtnNew(m_xBuilder->weld_button(u"new"_ustr))
41 , m_xBtnEdit(m_xBuilder->weld_button(u"edit"_ustr))
42 , m_xBtnRemove(m_xBuilder->weld_button(u"delete"_ustr))
43 , m_xBtnCopy(m_xBuilder->weld_button(u"copy"_ustr))
44 , m_xBtnStartShow(m_xBuilder->weld_button(u"startshow"_ustr))
46 m_xLbCustomShows->set_size_request(m_xLbCustomShows->get_approximate_digit_width() * 32,
47 m_xLbCustomShows->get_height_rows(8));
49 Link<weld::Button&,void> aLink( LINK( this, SdCustomShowDlg, ClickButtonHdl ) );
50 m_xBtnNew->connect_clicked( aLink );
51 m_xBtnEdit->connect_clicked( aLink );
52 m_xBtnRemove->connect_clicked( aLink );
53 m_xBtnCopy->connect_clicked( aLink );
54 m_xLbCustomShows->connect_selection_changed(LINK(this, SdCustomShowDlg, SelectListBoxHdl));
56 m_xBtnStartShow->connect_clicked( LINK( this, SdCustomShowDlg, StartShowHdl ) ); // for test
58 // get CustomShow list of docs
59 pCustomShowList = rDoc.GetCustomShowList();
60 if( pCustomShowList )
62 tools::Long nPosToSelect = pCustomShowList->GetCurPos();
63 // fill ListBox with CustomShows
64 for( SdCustomShow* pCustomShow = pCustomShowList->First();
65 pCustomShow != nullptr;
66 pCustomShow = pCustomShowList->Next() )
68 m_xLbCustomShows->append_text(pCustomShow->GetName());
70 m_xLbCustomShows->select(nPosToSelect);
71 pCustomShowList->Seek( nPosToSelect );
74 CheckState();
77 SdCustomShowDlg::~SdCustomShowDlg()
81 void SdCustomShowDlg::CheckState()
83 int nPos = m_xLbCustomShows->get_selected_index();
85 bool bEnable = nPos != -1;
86 m_xBtnEdit->set_sensitive( bEnable );
87 m_xBtnRemove->set_sensitive( bEnable );
88 m_xBtnCopy->set_sensitive( bEnable );
89 m_xBtnStartShow->set_sensitive(bEnable);
91 if (bEnable && pCustomShowList)
92 pCustomShowList->Seek( nPos );
95 IMPL_LINK( SdCustomShowDlg, ClickButtonHdl, weld::Button&, r, void )
97 SelectHdl(&r);
100 IMPL_LINK( SdCustomShowDlg, SelectListBoxHdl, weld::TreeView&, rListBox, void )
102 SelectHdl(&rListBox);
105 void SdCustomShowDlg::SelectHdl(void const *p)
107 // new CustomShow
108 if (p == m_xBtnNew.get())
110 std::unique_ptr<SdCustomShow> pCustomShow;
111 SdDefineCustomShowDlg aDlg(m_xDialog.get(), rDoc, pCustomShow);
112 if (aDlg.run() == RET_OK)
114 if( pCustomShow )
116 if( !pCustomShowList )
117 pCustomShowList = rDoc.GetCustomShowList( true );
119 SdCustomShow* pCustomShowTmp = pCustomShow.get();
120 pCustomShowList->push_back( std::move(pCustomShow) );
121 pCustomShowList->Last();
122 m_xLbCustomShows->append_text( pCustomShowTmp->GetName() );
123 m_xLbCustomShows->select_text( pCustomShowTmp->GetName() );
127 // edit CustomShow
128 else if( p == m_xBtnEdit.get() )
130 int nPos = m_xLbCustomShows->get_selected_index();
131 if (nPos != -1)
133 DBG_ASSERT( pCustomShowList, "pCustomShowList does not exist" );
134 std::unique_ptr<SdCustomShow>& pCustomShow = (*pCustomShowList)[ nPos ];
135 SdDefineCustomShowDlg aDlg(m_xDialog.get(), rDoc, pCustomShow);
137 if (aDlg.run() == RET_OK)
139 pCustomShowList->Seek(nPos);
140 m_xLbCustomShows->remove(nPos);
141 m_xLbCustomShows->insert_text(nPos, pCustomShow->GetName());
142 m_xLbCustomShows->select(nPos);
146 // delete CustomShow
147 else if( p == m_xBtnRemove.get() )
149 int nPos = m_xLbCustomShows->get_selected_index();
150 if (nPos != -1)
152 pCustomShowList->erase( pCustomShowList->begin() + nPos );
153 m_xLbCustomShows->remove(nPos);
154 m_xLbCustomShows->select(nPos == 0 ? nPos : nPos - 1);
157 // copy CustomShow
158 else if( p == m_xBtnCopy.get() )
160 int nPos = m_xLbCustomShows->get_selected_index();
161 if (nPos != -1)
163 std::unique_ptr<SdCustomShow> pShow(new SdCustomShow( *(*pCustomShowList)[nPos] ));
164 OUString aStr( pShow->GetName() );
165 OUString aStrCopy( SdResId( STR_COPY_CUSTOMSHOW ) );
167 sal_Int32 nStrPos = aStr.indexOf( aStrCopy );
168 sal_Int32 nNum = 1;
169 if( nStrPos < 0 )
171 aStr += " (" + aStrCopy + OUString::number( nNum ) + ")";
172 nStrPos = aStr.indexOf( aStrCopy );
174 nStrPos = nStrPos + aStrCopy.getLength();
175 // that we do not access into the nirvana (--> endless loop)
176 if( nStrPos >= aStr.getLength() )
178 aStr += " " + OUString::number( nNum );
181 // check name...
182 bool bDifferent = false;
183 //long nPosToSelect = pCustomShowList->GetCurPos();
184 while( !bDifferent )
186 bDifferent = true;
187 for( SdCustomShow* pCustomShow = pCustomShowList->First();
188 pCustomShow != nullptr && bDifferent;
189 pCustomShow = pCustomShowList->Next() )
191 if( aStr == pCustomShow->GetName() )
192 bDifferent = false;
194 if( !bDifferent )
196 // replace number by a number increased by 1
198 const std::optional<CharClass>& pCharClass = rDoc.GetCharClass();
199 while( pCharClass->isDigit( aStr, nStrPos ) )
200 aStr = aStr.replaceAt( nStrPos, 1, u"" );
201 aStr = aStr.subView( 0, nStrPos) + OUString::number( ++nNum ) + aStr.subView( nStrPos);
205 //pCustomShowList->Seek( nPosToSelect );
206 pShow->SetName( aStr );
208 auto pShowTmp = pShow.get();
209 pCustomShowList->push_back( std::move(pShow) );
210 pCustomShowList->Last();
211 m_xLbCustomShows->append_text(pShowTmp->GetName());
212 m_xLbCustomShows->select_text(pShowTmp->GetName());
215 else if( p == m_xLbCustomShows.get() )
217 int nPos = m_xLbCustomShows->get_selected_index();
218 if (nPos != -1)
219 pCustomShowList->Seek(nPos);
222 CheckState();
225 // StartShow-Hdl
226 IMPL_LINK_NOARG(SdCustomShowDlg, StartShowHdl, weld::Button&, void)
228 m_xDialog->response(RET_YES);
231 // CheckState
232 bool SdCustomShowDlg::IsCustomShow() const
234 if (!pCustomShowList->empty())
235 return true;
236 else
237 return false;
240 // SdDefineCustomShowDlg
241 SdDefineCustomShowDlg::SdDefineCustomShowDlg(weld::Window* pWindow, SdDrawDocument& rDrawDoc, std::unique_ptr<SdCustomShow>& rpCS)
242 : GenericDialogController(pWindow, u"modules/simpress/ui/definecustomslideshow.ui"_ustr, u"DefineCustomSlideShow"_ustr)
243 , rDoc(rDrawDoc)
244 , rpCustomShow(rpCS)
245 , bModified(false)
246 , m_xEdtName(m_xBuilder->weld_entry(u"customname"_ustr))
247 , m_xLbPages(m_xBuilder->weld_tree_view(u"pages"_ustr))
248 , m_xBtnAdd(m_xBuilder->weld_button(u"add"_ustr))
249 , m_xBtnRemove(m_xBuilder->weld_button(u"remove"_ustr))
250 , m_xLbCustomPages(m_xBuilder->weld_tree_view(u"custompages"_ustr))
251 , m_xDropTargetHelper(new weld::ReorderingDropTarget(*m_xLbCustomPages))
252 , m_xBtnOK(m_xBuilder->weld_button(u"ok"_ustr))
254 Link<weld::Button&,void> aLink = LINK( this, SdDefineCustomShowDlg, ClickButtonHdl );
255 m_xBtnAdd->connect_clicked( aLink );
256 m_xBtnRemove->connect_clicked( aLink );
257 m_xEdtName->connect_changed( LINK( this, SdDefineCustomShowDlg, ClickButtonEditHdl ) );
258 m_xLbPages->connect_selection_changed(LINK(this, SdDefineCustomShowDlg, ClickButtonHdl4)); // because of status
259 m_xLbCustomPages->connect_selection_changed(LINK(this, SdDefineCustomShowDlg, ClickButtonHdl3)); // because of status
261 m_xBtnOK->connect_clicked( LINK( this, SdDefineCustomShowDlg, OKHdl ) );
263 m_xLbPages->set_selection_mode(SelectionMode::Multiple);
265 // shape 'em a bit
266 m_xLbPages->set_size_request(m_xLbPages->get_approximate_digit_width() * 24, m_xLbPages->get_height_rows(10));
267 m_xLbCustomPages->set_size_request(m_xLbPages->get_approximate_digit_width() * 24, m_xLbCustomPages->get_height_rows(10));
269 // fill Listbox with page names of Docs
270 for( tools::Long nPage = 0;
271 nPage < rDoc.GetSdPageCount( PageKind::Standard );
272 nPage++ )
274 SdPage* pPage = rDoc.GetSdPage( static_cast<sal_uInt16>(nPage), PageKind::Standard );
275 m_xLbPages->append_text(pPage->GetName());
277 // aLbPages.SelectEntryPos( 0 );
279 if( rpCustomShow )
281 aOldName = rpCustomShow->GetName();
282 m_xEdtName->set_text( aOldName );
284 // fill ListBox with CustomShow pages
285 for( const auto& rpPage : rpCustomShow->PagesVector() )
287 m_xLbCustomPages->append(weld::toId(rpPage), rpPage->GetName(), u""_ustr);
290 else
292 rpCustomShow.reset(new SdCustomShow);
293 m_xEdtName->set_text( SdResId( STR_NEW_CUSTOMSHOW ) );
294 m_xEdtName->select_region(0, -1);
295 rpCustomShow->SetName( m_xEdtName->get_text() );
298 m_xBtnOK->set_sensitive( false );
299 CheckState();
302 SdDefineCustomShowDlg::~SdDefineCustomShowDlg()
306 // CheckState
307 void SdDefineCustomShowDlg::CheckState()
309 bool bPages = m_xLbPages->count_selected_rows() > 0;
310 bool bCSPages = m_xLbCustomPages->get_selected_index() != -1;
311 bool bCount = m_xLbCustomPages->n_children() > 0;
313 m_xBtnOK->set_sensitive( bCount );
314 m_xBtnAdd->set_sensitive( bPages );
315 m_xBtnRemove->set_sensitive( bCSPages );
318 IMPL_LINK( SdDefineCustomShowDlg, ClickButtonHdl, weld::Button&, rWidget, void )
320 ClickButtonHdl2(&rWidget);
323 IMPL_LINK( SdDefineCustomShowDlg, ClickButtonHdl3, weld::TreeView&, rWidget, void )
325 ClickButtonHdl2(&rWidget);
328 IMPL_LINK( SdDefineCustomShowDlg, ClickButtonHdl4, weld::TreeView&, rListBox, void )
330 ClickButtonHdl2(&rListBox);
333 IMPL_LINK( SdDefineCustomShowDlg, ClickButtonEditHdl, weld::Entry&, rEdit, void )
335 ClickButtonHdl2(&rEdit);
338 // ButtonHdl()
339 void SdDefineCustomShowDlg::ClickButtonHdl2(void const * p)
341 if( p == m_xBtnAdd.get() )
343 auto aRows = m_xLbPages->get_selected_rows();
344 if (!aRows.empty())
346 int nPosCP = m_xLbCustomPages->get_selected_index();
347 if (nPosCP != -1)
348 ++nPosCP;
350 for (auto i : aRows)
352 OUString aStr = m_xLbPages->get_text(i);
353 SdPage* pPage = rDoc.GetSdPage(i, PageKind::Standard);
354 OUString sId(weld::toId(pPage));
355 m_xLbCustomPages->insert(nPosCP, aStr, &sId, nullptr, nullptr);
356 m_xLbCustomPages->select(nPosCP != -1 ? nPosCP : m_xLbCustomPages->n_children() - 1);
358 if (nPosCP != -1)
359 ++nPosCP;
361 bModified = true;
364 else if (p == m_xBtnRemove.get())
366 int nPos = m_xLbCustomPages->get_selected_index();
367 if (nPos != -1)
369 m_xLbCustomPages->remove(nPos);
370 m_xLbCustomPages->select(nPos == 0 ? nPos : nPos - 1);
371 bModified = true;
374 else if( p == m_xEdtName.get() )
376 bModified = true;
379 CheckState();
383 * Checks the page pointer of the Show since entries can be moved and copied
384 * by TreeLB.
386 void SdDefineCustomShowDlg::CheckCustomShow()
388 bool bDifferent = false;
390 // compare count
391 size_t nCount = m_xLbCustomPages->n_children();
392 if (rpCustomShow->PagesVector().size() != nCount)
394 rpCustomShow->PagesVector().clear();
395 bDifferent = true;
398 // compare page pointer
399 if( !bDifferent )
401 size_t i = 0;
402 for (const auto& rpPage : rpCustomShow->PagesVector())
404 SdPage* pPage = weld::fromId<SdPage*>(m_xLbCustomPages->get_id(i));
405 if (rpPage != pPage)
407 rpCustomShow->PagesVector().clear();
408 bDifferent = true;
409 break;
412 ++i;
416 // set new page pointer
417 if( bDifferent )
419 for (size_t i = 0; i < nCount; ++i)
421 SdPage* pPage = weld::fromId<SdPage*>(m_xLbCustomPages->get_id(i));
422 rpCustomShow->PagesVector().push_back(pPage);
424 bModified = true;
427 // compare name and set name if necessary
428 OUString aStr( m_xEdtName->get_text() );
429 if( rpCustomShow->GetName() != aStr )
431 rpCustomShow->SetName( aStr );
432 bModified = true;
436 // OK-Hdl
437 IMPL_LINK_NOARG(SdDefineCustomShowDlg, OKHdl, weld::Button&, void)
439 // check name...
440 bool bDifferent = true;
441 SdCustomShowList* pCustomShowList = rDoc.GetCustomShowList();
442 if( pCustomShowList )
444 OUString aName( m_xEdtName->get_text() );
445 SdCustomShow* pCustomShow;
447 tools::Long nPosToSelect = pCustomShowList->GetCurPos();
448 for( pCustomShow = pCustomShowList->First();
449 pCustomShow != nullptr;
450 pCustomShow = pCustomShowList->Next() )
452 if( aName == pCustomShow->GetName() && aName != aOldName )
453 bDifferent = false;
455 pCustomShowList->Seek( nPosToSelect );
458 if( bDifferent )
460 CheckCustomShow();
462 m_xDialog->response(RET_OK);
464 else
466 std::unique_ptr<weld::MessageDialog> xWarn(Application::CreateMessageDialog(m_xDialog.get(),
467 VclMessageType::Warning, VclButtonsType::Ok,
468 SdResId(STR_WARN_NAME_DUPLICATE)));
469 xWarn->run();
470 m_xEdtName->grab_focus();
474 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */