LibreOffice Module extensions (master) 1
listselectiondlg.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 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include "listselectiondlg.hxx"
21
22#include "formstrings.hxx"
24#include <utility>
26
27namespace pcr
28{
29 using namespace ::com::sun::star::uno;
30 using namespace ::com::sun::star::beans;
31
32 ListSelectionDialog::ListSelectionDialog(weld::Window* pParent, const Reference< XPropertySet >& _rxListBox,
33 OUString _sPropertyName, const OUString& _rPropertyUIName)
34 : GenericDialogController(pParent, "modules/spropctrlr/ui/listselectdialog.ui", "ListSelectDialog")
35 , m_xListBox ( _rxListBox )
36 , m_sPropertyName(std::move( _sPropertyName ))
37 , m_xFrame(m_xBuilder->weld_frame("frame"))
38 , m_xEntries(m_xBuilder->weld_tree_view("treeview"))
39 {
40 OSL_PRECOND( m_xListBox.is(), "ListSelectionDialog::ListSelectionDialog: invalid list box!" );
41
42 m_xEntries->set_size_request(m_xEntries->get_approximate_digit_width() * 40, m_xEntries->get_height_rows(9));
43
44 m_xDialog->set_title(_rPropertyUIName);
45 m_xFrame->set_label(_rPropertyUIName);
46
47 initialize( );
48 }
49
51 {
52 }
53
55 {
56 short nResult = GenericDialogController::run();
57
58 if ( RET_OK == nResult )
60
61 return nResult;
62 }
63
64
66 {
67 if ( !m_xListBox.is() )
68 return;
69
70 try
71 {
72 // initialize the multi-selection flag
73 bool bMultiSelection = false;
74 OSL_VERIFY( m_xListBox->getPropertyValue( PROPERTY_MULTISELECTION ) >>= bMultiSelection );
75 m_xEntries->set_selection_mode(bMultiSelection ? SelectionMode::Single : SelectionMode::Multiple);
76
77 // fill the list box with all entries
78 Sequence< OUString > aListEntries;
79 OSL_VERIFY( m_xListBox->getPropertyValue( PROPERTY_STRINGITEMLIST ) >>= aListEntries );
80 fillEntryList( aListEntries );
81
82 // select entries according to the property
83 Sequence< sal_Int16 > aSelection;
84 OSL_VERIFY( m_xListBox->getPropertyValue( m_sPropertyName ) >>= aSelection );
85 selectEntries( aSelection );
86 }
87 catch( const Exception& )
88 {
89 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "ListSelectionDialog::initialize" );
90 }
91 }
92
94 {
95 if ( !m_xListBox.is() )
96 return;
97
98 std::vector< sal_Int16 > aSelection;
99 collectSelection( aSelection );
100
101 try
102 {
103 m_xListBox->setPropertyValue( m_sPropertyName, Any( comphelper::containerToSequence(aSelection) ) );
104 }
105 catch( const Exception& )
106 {
107 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "ListSelectionDialog::commitSelection" );
108 }
109 }
110
111 void ListSelectionDialog::fillEntryList( const Sequence< OUString >& _rListEntries )
112 {
113 m_xEntries->freeze();
114 m_xEntries->clear();
115 for (auto const & entry : _rListEntries)
116 m_xEntries->append_text(entry);
117 m_xEntries->thaw();
118 }
119
120 void ListSelectionDialog::collectSelection( std::vector< sal_Int16 >& /* [out] */ _rSelection )
121 {
122 auto aSelection = m_xEntries->get_selected_rows();
123 _rSelection.resize(aSelection.size());
124 for (auto row : aSelection)
125 _rSelection.push_back(row);
126 }
127
128 void ListSelectionDialog::selectEntries( const Sequence< sal_Int16 >& /* [in ] */ _rSelection )
129 {
130 m_xEntries->unselect_all();
131 for (auto const & selection : _rSelection)
132 m_xEntries->select(selection);
133 }
134
135
136} // namespace pcr
137
138
139/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::lang::XComponent > m_xFrame
css::uno::Reference< css::beans::XPropertySet > m_xListBox
virtual short run() override
std::unique_ptr< weld::Frame > m_xFrame
void collectSelection(std::vector< sal_Int16 > &_rSelection)
ListSelectionDialog(weld::Window *_pParent, const css::uno::Reference< css::beans::XPropertySet > &_rxListBox, OUString _sPropertyName, const OUString &_rPropertyUIName)
void selectEntries(const css::uno::Sequence< sal_Int16 > &_rSelection)
std::unique_ptr< weld::TreeView > m_xEntries
void fillEntryList(const css::uno::Sequence< OUString > &_rListEntries)
virtual ~ListSelectionDialog() override
std::shared_ptr< weld::Dialog > m_xDialog
#define TOOLS_WARN_EXCEPTION(area, stream)
constexpr OUStringLiteral PROPERTY_STRINGITEMLIST
Definition: formstrings.hxx:60
constexpr OUStringLiteral PROPERTY_MULTISELECTION
Definition: formstrings.hxx:76
@ Exception
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
a property handler for any virtual string properties
Definition: browserline.cxx:39
RET_OK