LibreOffice Module sfx2 (master) 1
templatedlglocalview.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
11
12#include <comphelper/string.hxx>
13#include <sfx2/inputdlg.hxx>
14#include <templateviewitem.hxx>
15#include <sfx2/sfxresid.hxx>
17#include <sfx2/strings.hrc>
18#include <vcl/commandevent.hxx>
19#include <vcl/svapp.hxx>
20#include <vcl/event.hxx>
21#include <sfx2/doctempl.hxx>
22#include <helpids.h>
23#include <bitmaps.hlst>
24
25TemplateDlgLocalView::TemplateDlgLocalView(std::unique_ptr<weld::ScrolledWindow> xWindow,
26 std::unique_ptr<weld::Menu> xMenu,
27 std::unique_ptr<weld::TreeView> xTreeView)
28 : TemplateLocalView(std::move(xWindow), std::move(xMenu))
29 , ListView(std::move(xTreeView))
31{
32 mxTreeView->connect_row_activated(LINK(this, TemplateDlgLocalView, RowActivatedHdl));
33 mxTreeView->connect_column_clicked(LINK(this, ListView, ColumnClickedHdl));
34 mxTreeView->connect_changed(LINK(this, TemplateDlgLocalView, ListViewChangedHdl));
35 mxTreeView->connect_popup_menu(LINK(this, TemplateDlgLocalView, PopupMenuHdl));
36 mxTreeView->connect_key_press(LINK(this, TemplateDlgLocalView, KeyPressHdl));
37}
38
40{
41 mnCurRegionId = 0;
42
43 insertItems(maAllTemplates, false, true);
44
45 maOpenRegionHdl.Call(nullptr);
46}
47
49{
50 mnCurRegionId = pItem->mnRegionId + 1;
51
52 insertItems(pItem->maTemplates, true, false);
53
54 maOpenRegionHdl.Call(nullptr);
55}
56
57void TemplateDlgLocalView::showRegion(std::u16string_view rName)
58{
59 for (auto const& pRegion : maRegions)
60 {
61 if (pRegion->maTitle == rName)
62 {
63 showRegion(pRegion.get());
64 break;
65 }
66 }
67}
68
70{
71 mpDocTemplates->Update();
72 OUString sCurRegionName = getRegionItemName(mnCurRegionId);
73 Populate();
74 mnCurRegionId = getRegionId(sCurRegionName);
75
76 // Check if we are currently browsing a region or root folder
77 if (mnCurRegionId)
78 {
79 sal_uInt16 nRegionId = mnCurRegionId - 1; //Is offset by 1
80
81 for (auto const& pRegion : maRegions)
82 {
83 if (pRegion->mnRegionId == nRegionId)
84 {
85 showRegion(pRegion.get());
86 break;
87 }
88 }
89 }
90 else
92
93 //No items should be selected by default
96}
97
98void TemplateDlgLocalView::createContextMenu(const bool bIsDefault, const bool bIsBuiltIn,
99 const bool bIsSingleSel, const OUString& rDefaultImg)
100{
101 mxContextMenu->clear();
102 mxContextMenu->append("open", SfxResId(STR_OPEN), BMP_MENU_OPEN);
103 mxContextMenu->append("edit", SfxResId(STR_EDIT_TEMPLATE), BMP_MENU_EDIT);
104
105 if (!bIsDefault)
106 mxContextMenu->append("default", SfxResId(STR_DEFAULT_TEMPLATE), rDefaultImg);
107 else
108 mxContextMenu->append("default", SfxResId(STR_RESET_DEFAULT), rDefaultImg);
109
110 mxContextMenu->append_separator("separator1");
111 mxContextMenu->append("rename", SfxResId(STR_SFX_RENAME), BMP_MENU_RENAME);
112 mxContextMenu->append("delete", SfxResId(STR_DELETE_TEMPLATE), BMP_MENU_DELETE);
113 mxContextMenu->append_separator("separator2");
114 mxContextMenu->append("move", SfxResId(STR_MOVE), BMP_MENU_MOVE);
115 mxContextMenu->append("export", SfxResId(STR_EXPORT), BMP_MENU_EXPORT);
116
117 // Set the help IDs of all context menu entries
118 mxContextMenu->set_item_help_id("open", HID_TEMPLATEDLG_OPEN);
119 mxContextMenu->set_item_help_id("edit", HID_TEMPLATEDLG_EDIT);
120 mxContextMenu->set_item_help_id("default", HID_TEMPLATEDLG_DEFAULT);
121 mxContextMenu->set_item_help_id("rename", HID_TEMPLATEDLG_RENAME);
122 mxContextMenu->set_item_help_id("delete", HID_TEMPLATEDLG_DELETE);
123 mxContextMenu->set_item_help_id("move", HID_TEMPLATEDLG_MOVE);
124 mxContextMenu->set_item_help_id("export", HID_TEMPLATEDLG_EXPORT);
125
126 if (!bIsSingleSel)
127 {
128 mxContextMenu->set_sensitive("open", false);
129 mxContextMenu->set_sensitive("edit", false);
130 mxContextMenu->set_sensitive("default", false);
131 mxContextMenu->set_sensitive("rename", false);
132 }
133 if (bIsBuiltIn)
134 {
135 mxContextMenu->set_sensitive("rename", false);
136 mxContextMenu->set_sensitive("delete", false);
137 }
139 {
140 ContextMenuSelectHdl(mxContextMenu->popup_at_rect(
142 Invalidate();
143 }
145 ContextMenuSelectHdl(mxContextMenu->popup_at_rect(
147}
148
149void TemplateDlgLocalView::ContextMenuSelectHdl(std::u16string_view rIdent)
150{
151 if (rIdent == u"open")
153 else if (rIdent == u"edit")
155 else if (rIdent == u"rename")
156 {
157 InputDialog aTitleEditDlg(GetDrawingArea(), SfxResId(STR_RENAME_TEMPLATE));
158 aTitleEditDlg.set_title(SfxResId(STR_WINDOW_TITLE_RENAME_TEMPLATE));
159 OUString sOldTitle = maSelectedItem->getTitle();
160 aTitleEditDlg.SetEntryText(sOldTitle);
161 aTitleEditDlg.HideHelpBtn();
162
163 auto aCurRegionItems = getFilteredItems([&](const TemplateItemProperties& rItem) {
165 });
166 OUString sTooltip(SfxResId(STR_TOOLTIP_ERROR_RENAME_TEMPLATE));
167 sTooltip = sTooltip.replaceFirst("$2", getRegionName(maSelectedItem->mnRegionId));
168 aTitleEditDlg.setCheckEntry([&](OUString sNewTitle) {
169 if (sNewTitle.isEmpty() || sNewTitle == sOldTitle)
170 return true;
171 for (const auto& rItem : aCurRegionItems)
172 {
173 if (rItem.aName == sNewTitle)
174 {
175 aTitleEditDlg.SetTooltip(sTooltip.replaceFirst("$1", sNewTitle));
176 return false;
177 }
178 }
179 return true;
180 });
181 if (!aTitleEditDlg.run())
182 return;
183 OUString sNewTitle = comphelper::string::strip(aTitleEditDlg.GetEntryText(), ' ');
184
185 if (!sNewTitle.isEmpty() && sNewTitle != sOldTitle)
186 {
187 maSelectedItem->setTitle(sNewTitle);
189 }
190 }
191 else if (rIdent == u"delete")
192 {
193 std::unique_ptr<weld::MessageDialog> xQueryDlg(Application::CreateMessageDialog(
194 GetDrawingArea(), VclMessageType::Question, VclButtonsType::YesNo,
195 SfxResId(STR_QMSG_SEL_TEMPLATE_DELETE)));
196 if (xQueryDlg->run() != RET_YES)
197 return;
198
200 reload();
201 }
202 else if (rIdent == u"default")
203 {
206 }
207 else if (rIdent == u"move")
208 {
210 }
211 else if (rIdent == u"export")
212 {
214 }
215}
216
218{
220 for (const ThumbnailViewItem* rItem : mFilteredItemList)
221 {
222 const TemplateViewItem* pViewItem = static_cast<const TemplateViewItem*>(rItem);
223 if (!pViewItem)
224 return;
225 bool isDefault = pViewItem->IsDefaultTemplate();
226 OUString sId = OUString::number(pViewItem->mnId);
227 ListView::AppendItem(sId, rItem->maTitle, getRegionName(pViewItem->mnRegionId),
228 pViewItem->getPath(), isDefault);
229 }
231}
232
233void TemplateDlgLocalView::insertItems(const std::vector<TemplateItemProperties>& rTemplates,
234 bool isRegionSelected = true,
235 bool bShowCategoryInTooltip = false)
236{
237 TemplateLocalView::insertItems(rTemplates, isRegionSelected, bShowCategoryInTooltip);
239}
240
242
244{
246 {
249 }
250 else
251 {
254 }
255 syncCursor();
256}
258{
261}
262
264{
266}
267
269{
271 {
273 int nIndex = -1;
274
275 for (auto it = mFilteredItemList.cbegin(); it != mFilteredItemList.cend(); ++it)
276 {
277 if ((*it)->mbSelected)
278 {
279 nIndex = ListView::get_index((*it)->mnId);
280 if (nIndex >= 0)
281 {
284 break;
285 }
286 }
287 }
289 }
290 else
291 {
293 std::vector<int> aSelRows = ListView::get_selected_rows();
294 if (aSelRows.empty())
295 return;
296 sal_uInt16 nCursorId = ListView::get_cursor_nId();
297 ThumbnailView::SelectItem(nCursorId);
298 MakeItemVisible(nCursorId);
299
300 for (auto it = mFilteredItemList.begin(); it != mFilteredItemList.end(); ++it)
301 {
302 if ((*it)->mnId == nCursorId)
303 {
304 mpStartSelRange = it;
305 break;
306 }
307 }
308
309 size_t nPos = GetItemPos(nCursorId);
311 const TemplateViewItem* pViewItem = dynamic_cast<const TemplateViewItem*>(pItem);
312 if (pViewItem)
313 maSelectedItem = dynamic_cast<TemplateViewItem*>(pItem);
314 }
315}
316
318{
321 {
323 }
324
325 sal_uInt16 nCursorId = ListView::get_cursor_nId();
326 size_t nPos = GetItemPos(nCursorId);
328 const TemplateViewItem* pViewItem = dynamic_cast<const TemplateViewItem*>(pItem);
329 if (pViewItem)
330 maSelectedItem = dynamic_cast<TemplateViewItem*>(pItem);
331 return;
332}
333
335{
336 maOpenTemplateHdl.Call(maSelectedItem);
337 return true;
338}
339
340IMPL_LINK(TemplateDlgLocalView, PopupMenuHdl, const CommandEvent&, rCEvt, bool)
341{
342 if (rCEvt.GetCommand() != CommandEventId::ContextMenu)
343 return false;
344
345 if (rCEvt.IsMouseEvent())
346 {
347 if (ListView::get_selected_rows().empty())
348 return true;
349 Point aPosition(rCEvt.GetMousePosPixel());
350 maPosition = aPosition;
351 updateSelection();
352 if (maSelectedItem)
353 maCreateContextMenuHdl.Call(maSelectedItem);
354 return true;
355 }
356 else
357 {
358 if (ListView::get_selected_rows().empty())
359 return true;
360 maPosition = Point(0, 0);
361 updateSelection();
362 if (maSelectedItem)
363 maCreateContextMenuHdl.Call(maSelectedItem);
364 return true;
365 }
366}
367
369{
370 updateSelection();
371}
372
374{
375 vcl::KeyCode aKeyCode = rKEvt.GetKeyCode();
376
377 if (aKeyCode == (KEY_MOD1 | KEY_A))
378 {
380 {
381 if (!pItem->isSelected())
382 {
383 pItem->setSelection(true);
384 maItemStateHdl.Call(pItem);
385 }
386 }
387
388 if (IsReallyVisible() && IsUpdateMode())
389 Invalidate();
390 return true;
391 }
392 else if (aKeyCode == KEY_DELETE && !mFilteredItemList.empty())
393 {
394 std::unique_ptr<weld::MessageDialog> xQueryDlg(Application::CreateMessageDialog(
395 GetDrawingArea(), VclMessageType::Question, VclButtonsType::YesNo,
396 SfxResId(STR_QMSG_SEL_TEMPLATE_DELETE)));
397 if (xQueryDlg->run() != RET_YES)
398 return true;
399
401 reload();
402 }
403
404 return ThumbnailView::KeyInput(rKEvt);
405}
406
407IMPL_LINK(TemplateDlgLocalView, KeyPressHdl, const KeyEvent&, rKEvt, bool)
408{
409 vcl::KeyCode aKeyCode = rKEvt.GetKeyCode();
410
411 if (aKeyCode == KEY_DELETE && !mFilteredItemList.empty()
412 && !ListView::get_selected_rows().empty())
413 {
414 std::unique_ptr<weld::MessageDialog> xQueryDlg(Application::CreateMessageDialog(
415 mxTreeView.get(), VclMessageType::Question, VclButtonsType::YesNo,
416 SfxResId(STR_QMSG_SEL_TEMPLATE_DELETE)));
417 if (xQueryDlg->run() != RET_YES)
418 return true;
419
420 maDeleteTemplateHdl.Call(maSelectedItem);
421 reload();
422 }
423 return false;
424}
425/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
geometry::RealPoint2D maPosition
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, const ILibreOfficeKitNotifier *pNotifier=nullptr)
OUString GetEntryText() const
Definition: inputdlg.cxx:24
void setCheckEntry(std::function< bool(OUString)> aFunc)
Definition: inputdlg.cxx:54
void SetEntryText(const OUString &rStr)
Definition: inputdlg.cxx:26
void HideHelpBtn()
Definition: inputdlg.cxx:22
const vcl::KeyCode & GetKeyCode() const
void rename(const OUString &rId, const OUString &rTitle)
Definition: listview.cxx:272
void select(int pos)
Definition: listview.hxx:81
sal_uInt16 get_cursor_nId() const
Definition: listview.hxx:79
void ShowListView()
Definition: listview.hxx:50
bool IsListViewVisible() const
Definition: listview.hxx:71
int get_index(sal_uInt16 nId) const
Definition: listview.hxx:83
void AppendItem(const OUString &rId, const OUString &rTitle, const OUString &rSubtitle, const OUString &rPath, bool bDefault)
Definition: listview.cxx:69
void unselect_all()
Definition: listview.hxx:54
std::unique_ptr< weld::TreeView > mxTreeView
Definition: listview.hxx:90
void clearListView()
Definition: listview.cxx:283
void sort()
Definition: listview.cxx:254
void refreshDefaultColumn()
Definition: listview.cxx:256
std::vector< int > get_selected_rows() const
Definition: listview.hxx:69
sal_uInt16 get_nId(int pos) const
Definition: listview.cxx:300
void HideListView()
Definition: listview.hxx:52
void set_cursor(int pos)
Definition: listview.hxx:75
std::vector< TemplateItemProperties > maTemplates
void showRegion(TemplateContainerItem const *pItem)
virtual void Hide() override
void insertItems(const std::vector< TemplateItemProperties > &rTemplates, bool isRegionSelected, bool bShowCategoryInTooltip)
virtual void Show() override
virtual void showAllTemplates() override
void ContextMenuSelectHdl(std::u16string_view rIdent)
void MakeItemVisible(sal_uInt16 nId)
TemplateDlgLocalView(std::unique_ptr< weld::ScrolledWindow > xWindow, std::unique_ptr< weld::Menu > xMenu, std::unique_ptr< weld::TreeView > xTreeView)
void setTemplateViewMode(TemplateViewMode eMode)
virtual bool KeyInput(const KeyEvent &rKEvt) override
void createContextMenu(const bool bIsDefault, const bool bIsBuiltIn, const bool bIsSingleSel, const OUString &rDefaultImg)
std::vector< TemplateItemProperties > maAllTemplates
Link< ThumbnailViewItem *, void > maEditTemplateHdl
OUString getRegionName(const sal_uInt16 nRegionId) const
Link< ThumbnailViewItem *, void > maDefaultTemplateHdl
sal_uInt16 getRegionId(size_t pos) const
OUString getRegionItemName(const sal_uInt16 nItemId) const
Link< void *, void > maOpenRegionHdl
Link< void *, void > maExportTemplateHdl
Link< void *, void > maDeleteTemplateHdl
std::unique_ptr< SfxDocumentTemplates > mpDocTemplates
std::vector< TemplateItemProperties > getFilteredItems(const std::function< bool(const TemplateItemProperties &) > &rFunc) const
void insertItems(const std::vector< TemplateItemProperties > &rTemplates, bool isRegionSelected=true, bool bShowCategoryInTooltip=false)
Link< ThumbnailViewItem *, void > maOpenTemplateHdl
std::vector< std::unique_ptr< TemplateContainerItem > > maRegions
TemplateViewItem * maSelectedItem
Link< void *, void > maMoveTemplateHdl
bool IsDefaultTemplate() const
const OUString & getPath() const
OUString const & getTitle() const
void setTitle(const OUString &rTitle)
size_t GetItemPos(sal_uInt16 nItemId) const
void deselectItems()
deselect all current selected items.
Link< const ThumbnailViewItem *, void > maItemStateHdl
size_t ImplGetItem(const Point &rPoint) const
virtual bool KeyInput(const KeyEvent &rKEvt) override
ThumbnailValueItemList::iterator mpStartSelRange
std::unique_ptr< weld::Menu > mxContextMenu
virtual void Show() override
void SelectItem(sal_uInt16 nItemId)
ThumbnailValueItemList mFilteredItemList
Cache to store the filtered items.
virtual void Hide() override
weld::DrawingArea * GetDrawingArea() const
virtual short run()
void set_title(const OUString &rTitle)
float u
constexpr OUStringLiteral HID_TEMPLATEDLG_EDIT
Definition: helpids.h:44
constexpr OUStringLiteral HID_TEMPLATEDLG_OPEN
Definition: helpids.h:43
constexpr OUStringLiteral HID_TEMPLATEDLG_RENAME
Definition: helpids.h:46
constexpr OUStringLiteral HID_TEMPLATEDLG_EXPORT
Definition: helpids.h:49
constexpr OUStringLiteral HID_TEMPLATEDLG_MOVE
Definition: helpids.h:48
constexpr OUStringLiteral HID_TEMPLATEDLG_DEFAULT
Definition: helpids.h:45
constexpr OUStringLiteral HID_TEMPLATEDLG_DELETE
Definition: helpids.h:47
sal_Int32 nIndex
Mode eMode
constexpr sal_uInt16 KEY_MOD1
constexpr sal_uInt16 KEY_A
constexpr sal_uInt16 KEY_DELETE
sal_uInt16 nPos
Definition: linksrc.cxx:118
TemplateViewMode
Definition: listview.hxx:15
@ eThumbnailView
Definition: listview.hxx:17
@ eListView
Definition: listview.hxx:16
OString strip(const OString &rIn, char c)
OUString SfxResId(TranslateId aId)
Definition: sfxresid.cxx:22
IMPL_LINK_NOARG(TemplateDlgLocalView, RowActivatedHdl, weld::TreeView &, bool)
IMPL_LINK(TemplateDlgLocalView, PopupMenuHdl, const CommandEvent &, rCEvt, bool)
OUString sId
RET_YES