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_xBtnOk(m_xBuilder->weld_button("ok"))
127 , m_xLbCharset(new SvxTextEncodingBox(m_xBuilder->weld_combo_box("charsetdropdown")))
128 , m_xTvCharset(new SvxTextEncodingTreeView(m_xBuilder->weld_tree_view("charsetlist")))
129{
130 if (bAscii)
131 {
132 m_xDialog->set_help_id(m_xDialog->get_help_id() + "?config=NonTextImport");
133 m_xLbCharset->show();
134 m_xTvCharset->hide();
135 }
136 else
137 {
138 m_xTvCharset->set_size_request(-1, m_xTvCharset->get_height_rows(6));
139 m_xEncGrid->set_vexpand(true);
140 m_xLbCharset->hide();
141 m_xTvCharset->show();
142 }
143
144 OUString sFieldSep(SCSTR_FIELDSEP);
145 sFieldSep = sFieldSep.replaceFirst( "%TAB", ScResId(SCSTR_FIELDSEP_TAB) );
146 sFieldSep = sFieldSep.replaceFirst( "%SPACE", ScResId(SCSTR_FIELDSEP_SPACE) );
147
148 // not possible in the Ctor initializer (MSC cannot do that):
149 pFieldSepTab.reset( new ScDelimiterTable(sFieldSep) );
151
152 OUString aStr = pFieldSepTab->FirstDel();
153
154 while (!aStr.isEmpty())
155 {
156 m_xEdFieldSep->append_text(aStr);
157 aStr = pFieldSepTab->NextDel();
158 }
159
160 aStr = pTextSepTab->FirstDel();
161
162 while (!aStr.isEmpty())
163 {
164 m_xEdTextSep->append_text(aStr);
165 aStr = pTextSepTab->NextDel();
166 }
167
168 m_xEdFieldSep->set_active(0);
169 m_xEdTextSep->set_active(0);
170
171 if ( bOnlyDbtoolsEncodings )
172 {
173 // Even dBase export allows multibyte now
174 if ( bMultiByte )
175 FillFromDbTextEncodingMap( bImport );
176 else
177 FillFromDbTextEncodingMap( bImport, RTL_TEXTENCODING_INFO_MULTIBYTE );
178 }
179 else if ( !bAscii )
180 {
181 if ( bMultiByte )
182 FillFromTextEncodingTable( bImport, RTL_TEXTENCODING_INFO_UNICODE );
183 else
184 FillFromTextEncodingTable( bImport, RTL_TEXTENCODING_INFO_UNICODE |
185 RTL_TEXTENCODING_INFO_MULTIBYTE );
186 }
187 else
188 {
189 if ( pOptions )
190 {
191 sal_Unicode nCode = pOptions->nFieldSepCode;
192 aStr = pFieldSepTab->GetDelimiter( nCode );
193
194 if ( aStr.isEmpty() )
195 m_xEdFieldSep->set_entry_text(OUString(nCode));
196 else
197 m_xEdFieldSep->set_entry_text(aStr);
198
199 nCode = pOptions->nTextSepCode;
200 aStr = pTextSepTab->GetDelimiter( nCode );
201
202 if ( aStr.isEmpty() )
203 m_xEdTextSep->set_entry_text(OUString(nCode));
204 else
205 m_xEdTextSep->set_entry_text(aStr);
206 }
207 // all encodings allowed, even Unicode
208 FillFromTextEncodingTable( bImport );
209 }
210
211 if( bAscii )
212 {
213 sal_Int32 nCharSet = officecfg::Office::Calc::Dialogs::CSVExport::CharSet::get();
214 OUString strFieldSeparator = officecfg::Office::Calc::Dialogs::CSVExport::FieldSeparator::get();
215 OUString strTextSeparator = officecfg::Office::Calc::Dialogs::CSVExport::TextSeparator::get();
216 bool bSaveTrueCellContent = officecfg::Office::Calc::Dialogs::CSVExport::SaveTrueCellContent::get();
217 bool bSaveCellFormulas = officecfg::Office::Calc::Dialogs::CSVExport::SaveCellFormulas::get();
218 bool bQuoteAllTextCells = officecfg::Office::Calc::Dialogs::CSVExport::QuoteAllTextCells::get();
219 bool bFixedWidth = officecfg::Office::Calc::Dialogs::CSVExport::FixedWidth::get();
220
221 m_xCbFixed->show();
222 m_xCbFixed->connect_toggled(LINK(this, ScImportOptionsDlg, FixedWidthHdl));
223 m_xCbFixed->set_active( bFixedWidth );
224 FixedWidthHdl(*m_xCbFixed);
225 m_xCbShown->show();
226 m_xCbShown->set_active( bSaveTrueCellContent );
227 m_xCbQuoteAll->show();
228 m_xCbQuoteAll->set_active( bQuoteAllTextCells );
229 m_xCbFormulas->show();
230 // default option for "save formulas" no longer taken from view shell but from persisted dialog settings
231 m_xCbFormulas->set_active( bSaveCellFormulas );
232 // if no charset, text separator or field separator exist, keep the values from dialog initialization
233 if (strFieldSeparator.getLength() > 0)
234 m_xEdFieldSep->set_entry_text(strFieldSeparator);
235 if (strTextSeparator.getLength() > 0)
236 m_xEdTextSep->set_entry_text(strTextSeparator);
237 if (nCharSet < 0 || nCharSet == RTL_TEXTENCODING_DONTKNOW )
238 m_xLbCharset->SelectTextEncoding(pOptions ? pOptions->eCharSet : osl_getThreadTextEncoding());
239 else
240 m_xLbCharset->SelectTextEncoding(nCharSet);
241 }
242 else
243 {
244 m_xFieldFrame->set_label(m_xFtCharset->get_label());
245 m_xFtFieldSep->hide();
246 m_xFtTextSep->hide();
247 m_xFtCharset->hide();
248 m_xEdFieldSep->hide();
249 m_xEdTextSep->hide();
250 m_xCbFixed->hide();
251 m_xCbShown->hide();
252 m_xCbQuoteAll->hide();
253 m_xCbFormulas->hide();
254 m_xTvCharset->grab_focus();
255 m_xTvCharset->connect_row_activated(LINK(this, ScImportOptionsDlg, DoubleClickHdl));
256 m_xTvCharset->SelectTextEncoding(pOptions ? pOptions->eCharSet : osl_getThreadTextEncoding());
257 }
258
259 // optional title:
260 if (pStrTitle)
261 m_xDialog->set_title(*pStrTitle);
262}
263
265{
266}
267
269{
270 auto nEncoding = m_bIsAsciiImport ? m_xLbCharset->GetSelectTextEncoding() : m_xTvCharset->GetSelectTextEncoding();
271 rOptions.SetTextEncoding(nEncoding);
272
273 if (m_xCbFixed->get_visible())
274 {
277 rOptions.bFixedWidth = m_xCbFixed->get_active();
278 rOptions.bSaveAsShown = m_xCbShown->get_active();
279 rOptions.bQuoteAllText = m_xCbQuoteAll->get_active();
280 rOptions.bSaveFormulas = m_xCbFormulas->get_active();
281 }
282}
283
285{
286 ScDelimiterTable* pTab;
287 OUString aStr( rEd.get_active_text() );
288 sal_uInt16 nCode;
289
290 if (&rEd == m_xEdTextSep.get())
291 pTab = pTextSepTab.get();
292 else
293 pTab = pFieldSepTab.get();
294
295 if ( aStr.isEmpty() )
296 {
297 nCode = 0; // no separator
298 }
299 else
300 {
301 nCode = pTab->GetCode( aStr );
302
303 if ( nCode == 0 )
304 nCode = static_cast<sal_uInt16>(aStr[0]);
305 }
306
307 return nCode;
308}
309
311{
312 bool bEnable = !m_xCbFixed->get_active();
313 m_xFtFieldSep->set_sensitive( bEnable );
314 m_xEdFieldSep->set_sensitive( bEnable );
315 m_xFtTextSep->set_sensitive( bEnable );
316 m_xEdTextSep->set_sensitive( bEnable );
317 m_xCbShown->set_sensitive( bEnable );
318 m_xCbQuoteAll->set_sensitive( bEnable );
319}
320
322{
323 m_xDialog->response(RET_OK);
324 return true;
325}
326
328{
329 std::shared_ptr < comphelper::ConfigurationChanges > batch(comphelper::ConfigurationChanges::create());
330 auto nEncoding = m_bIsAsciiImport ? m_xLbCharset->GetSelectTextEncoding() : m_xTvCharset->GetSelectTextEncoding();
331 officecfg::Office::Calc::Dialogs::CSVExport::CharSet::set(nEncoding, batch);
332 officecfg::Office::Calc::Dialogs::CSVExport::FieldSeparator::set(m_xEdFieldSep->get_active_text(), batch);
333 officecfg::Office::Calc::Dialogs::CSVExport::TextSeparator::set(m_xEdTextSep->get_active_text(), batch);
334 officecfg::Office::Calc::Dialogs::CSVExport::FixedWidth::set(m_xCbFixed->get_active(), batch);
335 officecfg::Office::Calc::Dialogs::CSVExport::SaveCellFormulas::set(m_xCbFormulas->get_active(), batch);
336 officecfg::Office::Calc::Dialogs::CSVExport::SaveTrueCellContent::set(m_xCbShown->get_active(), batch);
337 officecfg::Office::Calc::Dialogs::CSVExport::QuoteAllTextCells::set(m_xCbQuoteAll->get_active(), batch);
338 batch->commit();
339}
340
341/* 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:128
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_uInt16 nCode
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