LibreOffice Module uui (master) 1
fltdlg.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 "fltdlg.hxx"
21
22#include <com/sun/star/util/XStringWidth.hpp>
24#include <tools/urlobj.hxx>
25
26#include <osl/file.hxx>
27
28namespace uui
29{
30/*-************************************************************************************************************
31 @short initialize filter dialog with start values
32 @descr We set some necessary information on these instance for later working and create internal structures.
33 After construction user should call "SetFilters()" and "SetURL()" to fill listbox with selectable filter
34 names and set file name of file, which should be used for selected filter.
35
36 @seealso method SetFilters()
37 @seealso method SetURL()
38
39 @param "pParentWindow" , parent window for dialog
40 @threadsafe no
41*/ /*-*************************************************************************************************************/
43 : GenericDialogController(pParentWindow, "uui/ui/filterselect.ui", "FilterSelectDialog")
44 , m_pFilterNames(nullptr)
45 , m_xFtURL(m_xBuilder->weld_label("url"))
46 , m_xLbFilters(m_xBuilder->weld_tree_view("filters"))
47{
48 m_xLbFilters->set_size_request(m_xLbFilters->get_approximate_digit_width() * 42,
49 m_xLbFilters->get_height_rows(15));
50}
51
53
54/*-************************************************************************************************************
55 @short set file name on dialog control
56 @descr We convert given URL (it must be a URL!) into valid file name and show it on our dialog.
57 @param "sURL", URL for showing
58 @threadsafe no
59*/ /*-*************************************************************************************************************/
60void FilterDialog::SetURL(const OUString& sURL)
61{
62 // convert it and use given pure string as fallback if conversion failed
63 m_xFtURL->set_label(impl_buildUIFileName(sURL));
64}
65
66/*-************************************************************************************************************
67 @short change list of filter names
68 @descr We save given pointer internal and use it to fill our listbox with given names.
69 Saved list pointer is used on method "AskForFilter()" too, to find user selected item
70 and return pointer into these list as result of operation.
71 So it's possible to call dialog again and again for different or same filter list
72 and ask user for his decision by best performance!
73
74 @attention Don't free memory of given list after this call till object will die ... or
75 you call "ChangeFilters( NULL )"! Then we forget it too.
76
77 @seealso method AskForFilter()
78
79 @param "pFilterNames", pointer to list of filter names, which should be used for later operations.
80 @onerror We clear list box and forget our currently set filter information completely!
81 @threadsafe no
82*/ /*-*************************************************************************************************************/
84{
85 m_pFilterNames = pFilterNames;
86 m_xLbFilters->clear();
87 if (m_pFilterNames != nullptr)
88 {
89 for (const auto& rItem : *m_pFilterNames)
90 {
91 m_xLbFilters->append_text(rItem.sUI);
92 }
93 }
94}
95
96/*-************************************************************************************************************
97 @short ask user for his decision
98 @descr We show the dialog and if user finish it with "OK" - we try to find selected item in internal saved
99 name list (which you must set in "ChangeFilters()"!). If we return sal_True as result, you can use out
100 parameter "pSelectedItem" as pointer into your FilterNameList to get selected item really ...
101 but if we return sal_False ... user has cancel the dialog ... you should not do that. pSelectedItem is not
102 set to any valid value then. We don't change them ...
103
104 @seealso method ChangeFilters()
105
106 @param "pSelectedItem", returns result of selection as pointer into set list of filter names
107 (valid for function return sal_True only!)
108 @return true => pSelectedItem parameter points into name list and represent use decision
109 false => use has cancelled dialog (pSelectedItem is not valid then!)
110
111 @onerror We return false ... but don't change pSelectedItem!
112 @threadsafe no
113*/ /*-*************************************************************************************************************/
115{
116 bool bSelected = false;
117
118 if (m_pFilterNames != nullptr)
119 {
120 if (m_xDialog->run() == RET_OK)
121 {
122 OUString sEntry = m_xLbFilters->get_selected_text();
123 if (!sEntry.isEmpty())
124 {
125 int nPos = m_xLbFilters->get_selected_index();
126 if (nPos < static_cast<int>(m_pFilterNames->size()))
127 {
128 pSelectedItem = m_pFilterNames->begin();
129 pSelectedItem += nPos;
130 bSelected = (pSelectedItem != m_pFilterNames->end());
131 }
132 }
133 }
134 }
135
136 return bSelected;
137}
138
139namespace
140{
141/*-************************************************************************************************************
142 @short helper class to calculate length of given string
143 @descr Instances of it can be used as callback for INetURLObject::getAbbreviated() method to build
144 short URLs to show it on GUI. We use in ctor set OutputDevice to call special VCL method ...
145
146 @seealso method OutputDevice::GetTextWidth()
147 @seealso method InetURLObject::getAbbreviated()
148 @threadsafe no
149*/ /*-*************************************************************************************************************/
150class StringCalculator : public ::cppu::WeakImplHelper<css::util::XStringWidth>
151{
152public:
153 explicit StringCalculator(weld::Widget* pDevice)
154 : m_pDevice(pDevice)
155 {
156 }
157
158 sal_Int32 SAL_CALL queryStringWidth(const OUString& sString) override
159 {
160 return static_cast<sal_Int32>(m_pDevice->get_pixel_size(sString).Width());
161 }
162
163private:
165};
166}
167
168/*-************************************************************************************************************
169 @short try to build short name of given URL to show it n GUI
170 @descr We detect type of given URL automatically and build this short name depend on this type ...
171 If we couldn't make it right we return full given string without any changes ...
172
173 @seealso method InetURLObject::getAbbreviated()
174
175 @param "sName", file name
176 @return A short file name ...
177
178 @onerror We return given name without any changes.
179 @threadsafe no
180*/ /*-*************************************************************************************************************/
181OUString FilterDialog::impl_buildUIFileName(const OUString& sName)
182{
183 OUString sShortName(sName);
184
185 if (osl::FileBase::getSystemPathFromFileURL(sName, sShortName) == osl::FileBase::E_None)
186
187 {
188 // it's a system file... build short name by using osl functionality
189 }
190 else
191 {
192 // otherwise it's really a URL... build short name by using INetURLObject
193 css::uno::Reference<css::util::XStringWidth> xStringCalculator(
194 new StringCalculator(m_xFtURL.get()));
195 if (xStringCalculator.is())
196 {
197 INetURLObject aBuilder(sName);
198 Size aSize = m_xLbFilters->get_preferred_size();
199 sShortName = aBuilder.getAbbreviated(xStringCalculator, aSize.Width(),
201 }
202 }
203
204 return sShortName;
205}
206
207} // namespace uui
208
209/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString getAbbreviated(css::uno::Reference< css::util::XStringWidth > const &rStringWidth, sal_Int32 nWidth, DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
constexpr tools::Long Width() const
void ChangeFilters(const FilterNameList *pFilterNames)
Definition: fltdlg.cxx:83
void SetURL(const OUString &sURL)
Definition: fltdlg.cxx:60
FilterDialog(weld::Window *pParentWindow)
Definition: fltdlg.cxx:42
OUString impl_buildUIFileName(const OUString &sURL)
Definition: fltdlg.cxx:181
std::unique_ptr< weld::Label > m_xFtURL
Definition: fltdlg.hxx:54
std::unique_ptr< weld::TreeView > m_xLbFilters
Definition: fltdlg.hxx:55
bool AskForFilter(FilterNameListPtr &pSelectedItem)
Definition: fltdlg.cxx:114
const FilterNameList * m_pFilterNames
Definition: fltdlg.hxx:53
virtual ~FilterDialog() override
Definition: fltdlg.cxx:52
std::shared_ptr< weld::Dialog > m_xDialog
virtual Size get_pixel_size(const OUString &rText) const=0
weld::Widget * m_pDevice
Definition: fltdlg.cxx:164
OUString sName
sal_uInt16 nPos
Definition: fltdlg.cxx:29
FilterNameList::const_iterator FilterNameListPtr
Definition: fltdlg.hxx:36
::std::vector< FilterNamePair > FilterNameList
Definition: fltdlg.hxx:35
RET_OK