LibreOffice Module sc (master) 1
scuiimoptdlg.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#undef SC_DLLIMPLEMENTATION
21
22#include <scuiimoptdlg.hxx>
23#include <scresid.hxx>
24#include <strings.hrc>
25#include <strings.hxx>
26#include <officecfg/Office/Calc.hxx>
27#include <osl/thread.h>
28#include <rtl/tencinfo.h>
29#include <imoptdlg.hxx>
30#include <svx/txencbox.hxx>
31#include <o3tl/string_view.hxx>
32#include <utility>
33
34// ScDelimiterTable
35
37{
38public:
39 explicit ScDelimiterTable( OUString aDelTab )
40 : theDelTab (std::move( aDelTab )),
41 nDelIdx ( 0 )
42 {}
43
44 sal_uInt16 GetCode( std::u16string_view rDelimiter ) const;
45 OUString GetDelimiter( sal_Unicode nCode ) const;
46
47 OUString FirstDel() { nDelIdx = 0; return theDelTab.getToken( 0, cSep, nDelIdx ); }
48 OUString NextDel() { return theDelTab.getToken( 1, cSep, nDelIdx ); }
49
50private:
51 const OUString theDelTab;
52 static constexpr sal_Unicode cSep {'\t'};
53 sal_Int32 nDelIdx;
54};
55
56sal_uInt16 ScDelimiterTable::GetCode( std::u16string_view rDel ) const
57{
58 if (!theDelTab.isEmpty())
59 {
60 sal_Int32 nIdx {0};
61
62 // Check even tokens: start from 0 and then skip 1 token at each iteration
63 if (rDel != o3tl::getToken(theDelTab, 0, cSep, nIdx ))
64 while (nIdx>0 && rDel != o3tl::getToken(theDelTab, 1, cSep, nIdx ));
65
66 if (nIdx>0)
67 return static_cast<sal_Unicode>(o3tl::toInt32(o3tl::getToken(theDelTab, 0, cSep, nIdx )));
68 }
69
70 return 0;
71}
72
74{
75 if (!theDelTab.isEmpty())
76 {
77 sal_Int32 nIdx {0};
78 // Check odd tokens: start from 1 and then skip 1 token at each iteration
79 do
80 {
81 sal_Int32 nPrevIdx {nIdx};
82 if (nCode == static_cast<sal_Unicode>(o3tl::toInt32(o3tl::getToken(theDelTab, 1, cSep, nIdx ))))
83 return theDelTab.getToken( 0, cSep, nPrevIdx );
84 }
85 while (nIdx>0);
86 }
87
88 return OUString();
89}
90
91void ScImportOptionsDlg::FillFromTextEncodingTable(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags)
92{
94 m_xLbCharset->FillFromTextEncodingTable(bExcludeImportSubsets, nExcludeInfoFlags);
95 else
96 m_xTvCharset->FillFromTextEncodingTable(bExcludeImportSubsets, nExcludeInfoFlags);
97}
98
99void ScImportOptionsDlg::FillFromDbTextEncodingMap(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags)
100{
102 m_xLbCharset->FillFromDbTextEncodingMap(bExcludeImportSubsets, nExcludeInfoFlags);
103 else
104 m_xTvCharset->FillFromDbTextEncodingMap(bExcludeImportSubsets, nExcludeInfoFlags);
105}
106
107// ScImportOptionsDlg
109 const ScImportOptions* pOptions,
110 const OUString* pStrTitle,
111 bool bMultiByte, bool bOnlyDbtoolsEncodings,
112 bool bImport)
113 : GenericDialogController(pParent, "modules/scalc/ui/imoptdialog.ui", "ImOptDialog")
114 , m_bIsAsciiImport(bAscii)
115 , m_xFieldFrame(m_xBuilder->weld_frame("fieldframe"))
116 , m_xFtCharset(m_xBuilder->weld_label("charsetft"))
117 , m_xEncGrid(m_xBuilder->weld_widget("grid2"))
118 , m_xFtFieldSep(m_xBuilder->weld_label("fieldft"))
119 , m_xEdFieldSep(m_xBuilder->weld_combo_box("field"))
120 , m_xFtTextSep(m_xBuilder->weld_label("textft"))
121 , m_xEdTextSep(m_xBuilder->weld_combo_box("text"))
122 , m_xCbShown(m_xBuilder->weld_check_button("asshown"))
123 , m_xCbFormulas(m_xBuilder->weld_check_button("formulas"))
124 , m_xCbQuoteAll(m_xBuilder->weld_check_button("quoteall"))
125 , m_xCbFixed(m_xBuilder->weld_check_button("fixedwidth"))
126 , m_xLbCharset(new SvxTextEncodingBox(m_xBuilder->weld_combo_box("charsetdropdown")))
127 , m_xTvCharset(new SvxTextEncodingTreeView(m_xBuilder->weld_tree_view("charsetlist")))
128{
129 if (bAscii)
130 {
131 m_xDialog->set_help_id(m_xDialog->get_help_id() + "?config=NonTextImport");
132 m_xLbCharset->show();
133 m_xTvCharset->hide();
134 }
135 else
136 {
137 m_xTvCharset->set_size_request(-1, m_xTvCharset->get_height_rows(6));
138 m_xEncGrid->set_vexpand(true);
139 m_xLbCharset->hide();
140 m_xTvCharset->show();
141 }
142
143 OUString sFieldSep(SCSTR_FIELDSEP);
144 sFieldSep = sFieldSep.replaceFirst( "%TAB", ScResId(SCSTR_FIELDSEP_TAB) );
145 sFieldSep = sFieldSep.replaceFirst( "%SPACE", ScResId(SCSTR_FIELDSEP_SPACE) );
146
147 // not possible in the Ctor initializer (MSC cannot do that):
148 pFieldSepTab.reset( new ScDelimiterTable(sFieldSep) );
150
151 OUString aStr = pFieldSepTab->FirstDel();
152
153 while (!aStr.isEmpty())
154 {
155 m_xEdFieldSep->append_text(aStr);
156 aStr = pFieldSepTab->NextDel();
157 }
158
159 aStr = pTextSepTab->FirstDel();
160
161 while (!aStr.isEmpty())
162 {
163 m_xEdTextSep->append_text(aStr);
164 aStr = pTextSepTab->NextDel();
165 }
166
167 m_xEdFieldSep->set_active(0);
168 m_xEdTextSep->set_active(0);
169
170 if ( bOnlyDbtoolsEncodings )
171 {
172 // Even dBase export allows multibyte now
173 if ( bMultiByte )
174 FillFromDbTextEncodingMap( bImport );
175 else
176 FillFromDbTextEncodingMap( bImport, RTL_TEXTENCODING_INFO_MULTIBYTE );
177 }
178 else if ( !bAscii )
179 {
180 if ( bMultiByte )
181 FillFromTextEncodingTable( bImport, RTL_TEXTENCODING_INFO_UNICODE );
182 else
183 FillFromTextEncodingTable( bImport, RTL_TEXTENCODING_INFO_UNICODE |
184 RTL_TEXTENCODING_INFO_MULTIBYTE );
185 }
186 else
187 {
188 if ( pOptions )
189 {
190 sal_Unicode nCode = pOptions->nFieldSepCode;
191 aStr = pFieldSepTab->GetDelimiter( nCode );
192
193 if ( aStr.isEmpty() )
194 m_xEdFieldSep->set_entry_text(OUString(nCode));
195 else
196 m_xEdFieldSep->set_entry_text(aStr);
197
198 nCode = pOptions->nTextSepCode;
199 aStr = pTextSepTab->GetDelimiter( nCode );
200
201 if ( aStr.isEmpty() )
202 m_xEdTextSep->set_entry_text(OUString(nCode));
203 else
204 m_xEdTextSep->set_entry_text(aStr);
205 }
206 // all encodings allowed, even Unicode
207 FillFromTextEncodingTable( bImport );
208 }
209
210 if( bAscii )
211 {
212 sal_Int32 nCharSet = officecfg::Office::Calc::Dialogs::CSVExport::CharSet::get();
213 OUString strFieldSeparator = officecfg::Office::Calc::Dialogs::CSVExport::FieldSeparator::get();
214 OUString strTextSeparator = officecfg::Office::Calc::Dialogs::CSVExport::TextSeparator::get();
215 bool bSaveTrueCellContent = officecfg::Office::Calc::Dialogs::CSVExport::SaveTrueCellContent::get();
216 bool bSaveCellFormulas = officecfg::Office::Calc::Dialogs::CSVExport::SaveCellFormulas::get();
217 bool bQuoteAllTextCells = officecfg::Office::Calc::Dialogs::CSVExport::QuoteAllTextCells::get();
218 bool bFixedWidth = officecfg::Office::Calc::Dialogs::CSVExport::FixedWidth::get();
219
220 m_xCbFixed->show();
221 m_xCbFixed->connect_toggled(LINK(this, ScImportOptionsDlg, FixedWidthHdl));
222 m_xCbFixed->set_active( bFixedWidth );
223 FixedWidthHdl(*m_xCbFixed);
224 m_xCbShown->show();
225 m_xCbShown->set_active( bSaveTrueCellContent );
226 m_xCbQuoteAll->show();
227 m_xCbQuoteAll->set_active( bQuoteAllTextCells );
228 m_xCbFormulas->show();
229 // default option for "save formulas" no longer taken from view shell but from persisted dialog settings
230 m_xCbFormulas->set_active( bSaveCellFormulas );
231 // if no charset, text separator or field separator exist, keep the values from dialog initialization
232 if (strFieldSeparator.getLength() > 0)
233 m_xEdFieldSep->set_entry_text(strFieldSeparator);
234 if (strTextSeparator.getLength() > 0)
235 m_xEdTextSep->set_entry_text(strTextSeparator);
236 if (nCharSet < 0 || nCharSet == RTL_TEXTENCODING_DONTKNOW )
237 m_xLbCharset->SelectTextEncoding(pOptions ? pOptions->eCharSet : osl_getThreadTextEncoding());
238 else
239 m_xLbCharset->SelectTextEncoding(nCharSet);
240 }
241 else
242 {
243 m_xFieldFrame->set_label(m_xFtCharset->get_label());
244 m_xFtFieldSep->hide();
245 m_xFtTextSep->hide();
246 m_xFtCharset->hide();
247 m_xEdFieldSep->hide();
248 m_xEdTextSep->hide();
249 m_xCbFixed->hide();
250 m_xCbShown->hide();
251 m_xCbQuoteAll->hide();
252 m_xCbFormulas->hide();
253 m_xTvCharset->grab_focus();
254 m_xTvCharset->connect_row_activated(LINK(this, ScImportOptionsDlg, DoubleClickHdl));
255 m_xTvCharset->SelectTextEncoding(pOptions ? pOptions->eCharSet : osl_getThreadTextEncoding());
256 }
257
258 // optional title:
259 if (pStrTitle)
260 m_xDialog->set_title(*pStrTitle);
261}
262
264{
265}
266
268{
269 auto nEncoding = m_bIsAsciiImport ? m_xLbCharset->GetSelectTextEncoding() : m_xTvCharset->GetSelectTextEncoding();
270 rOptions.SetTextEncoding(nEncoding);
271
272 if (m_xCbFixed->get_visible())
273 {
276 rOptions.bFixedWidth = m_xCbFixed->get_active();
277 rOptions.bSaveAsShown = m_xCbShown->get_active();
278 rOptions.bQuoteAllText = m_xCbQuoteAll->get_active();
279 rOptions.bSaveFormulas = m_xCbFormulas->get_active();
280 }
281}
282
284{
285 ScDelimiterTable* pTab;
286 OUString aStr( rEd.get_active_text() );
287 sal_uInt16 nCode;
288
289 if (&rEd == m_xEdTextSep.get())
290 pTab = pTextSepTab.get();
291 else
292 pTab = pFieldSepTab.get();
293
294 if ( aStr.isEmpty() )
295 {
296 nCode = 0; // no separator
297 }
298 else
299 {
300 nCode = pTab->GetCode( aStr );
301
302 if ( nCode == 0 )
303 nCode = static_cast<sal_uInt16>(aStr[0]);
304 }
305
306 return nCode;
307}
308
310{
311 bool bEnable = !m_xCbFixed->get_active();
312 m_xFtFieldSep->set_sensitive( bEnable );
313 m_xEdFieldSep->set_sensitive( bEnable );
314 m_xFtTextSep->set_sensitive( bEnable );
315 m_xEdTextSep->set_sensitive( bEnable );
316 m_xCbShown->set_sensitive( bEnable );
317 m_xCbQuoteAll->set_sensitive( bEnable );
318}
319
321{
322 m_xDialog->response(RET_OK);
323 return true;
324}
325
327{
328 std::shared_ptr < comphelper::ConfigurationChanges > batch(comphelper::ConfigurationChanges::create());
329 auto nEncoding = m_bIsAsciiImport ? m_xLbCharset->GetSelectTextEncoding() : m_xTvCharset->GetSelectTextEncoding();
330 officecfg::Office::Calc::Dialogs::CSVExport::CharSet::set(nEncoding, batch);
331 officecfg::Office::Calc::Dialogs::CSVExport::FieldSeparator::set(m_xEdFieldSep->get_active_text(), batch);
332 officecfg::Office::Calc::Dialogs::CSVExport::TextSeparator::set(m_xEdTextSep->get_active_text(), batch);
333 officecfg::Office::Calc::Dialogs::CSVExport::FixedWidth::set(m_xCbFixed->get_active(), batch);
334 officecfg::Office::Calc::Dialogs::CSVExport::SaveCellFormulas::set(m_xCbFormulas->get_active(), batch);
335 officecfg::Office::Calc::Dialogs::CSVExport::SaveTrueCellContent::set(m_xCbShown->get_active(), batch);
336 officecfg::Office::Calc::Dialogs::CSVExport::QuoteAllTextCells::set(m_xCbQuoteAll->get_active(), batch);
337 batch->commit();
338}
339
340/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XExecutableDialog > m_xDialog
ScDelimiterTable(OUString aDelTab)
sal_uInt16 GetCode(std::u16string_view rDelimiter) const
const OUString theDelTab
OUString NextDel()
static constexpr sal_Unicode cSep
OUString GetDelimiter(sal_Unicode nCode) const
OUString FirstDel()
std::unique_ptr< weld::CheckButton > m_xCbFixed
std::unique_ptr< weld::Label > m_xFtFieldSep
std::unique_ptr< weld::ComboBox > m_xEdFieldSep
std::unique_ptr< weld::CheckButton > m_xCbQuoteAll
ScImportOptionsDlg(weld::Window *pParent, bool bAscii, const ScImportOptions *pOptions, const OUString *pStrTitle, bool bMultiByte, bool bOnlyDbtoolsEncodings, bool bImport)
std::unique_ptr< weld::Label > m_xFtTextSep
std::unique_ptr< weld::Widget > m_xEncGrid
std::unique_ptr< weld::CheckButton > m_xCbShown
void FillFromDbTextEncodingMap(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags=0)
std::unique_ptr< weld::ComboBox > m_xEdTextSep
std::unique_ptr< ScDelimiterTable > pTextSepTab
std::unique_ptr< SvxTextEncodingBox > m_xLbCharset
std::unique_ptr< SvxTextEncodingTreeView > m_xTvCharset
std::unique_ptr< weld::Label > m_xFtCharset
void GetImportOptions(ScImportOptions &rOptions) const
std::unique_ptr< ScDelimiterTable > pFieldSepTab
virtual ~ScImportOptionsDlg() override
std::unique_ptr< weld::CheckButton > m_xCbFormulas
sal_uInt16 GetCodeFromCombo(const weld::ComboBox &rEd) const
void SaveImportOptions() const
std::unique_ptr< weld::Frame > m_xFieldFrame
void FillFromTextEncodingTable(bool bExcludeImportSubsets, sal_uInt32 nExcludeInfoFlags=0)
bool bSaveFormulas
Definition: imoptdlg.hxx:52
void SetTextEncoding(rtl_TextEncoding nEnc)
Definition: imoptdlg.cxx:133
sal_Unicode nTextSepCode
Definition: imoptdlg.hxx:45
bool bQuoteAllText
Definition: imoptdlg.hxx:50
sal_Unicode nFieldSepCode
Definition: imoptdlg.hxx:44
rtl_TextEncoding eCharSet
Definition: imoptdlg.hxx:47
static std::shared_ptr< ConfigurationChanges > create()
virtual OUString get_active_text() const=0
std::shared_ptr< weld::Dialog > m_xDialog
aStr
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
OUString ScResId(TranslateId aId)
Definition: scdll.cxx:90
IMPL_LINK_NOARG(ScImportOptionsDlg, FixedWidthHdl, weld::Toggleable &, void)
constexpr OUStringLiteral SCSTR_FIELDSEP
Definition: strings.hxx:14
constexpr OUStringLiteral SCSTR_TEXTSEP
Definition: strings.hxx:15
sal_uInt16 sal_Unicode
RET_OK