Coverage Report

Created: 2026-02-14 09:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/svx/source/unodialogs/textconversiondlgs/chinese_dictionarydialog.cxx
Line
Count
Source
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
21
#include "chinese_dictionarydialog.hxx"
22
#include <com/sun/star/i18n/TextConversionOption.hpp>
23
#include <com/sun/star/linguistic2/ConversionDictionaryType.hpp>
24
#include <com/sun/star/linguistic2/ConversionPropertyType.hpp>
25
#include <com/sun/star/linguistic2/ConversionDictionaryList.hpp>
26
#include <com/sun/star/linguistic2/XConversionPropertyType.hpp>
27
#include <com/sun/star/util/XFlushable.hpp>
28
#include <com/sun/star/lang/Locale.hpp>
29
#include <comphelper/processfactory.hxx>
30
#include <o3tl/safeint.hxx>
31
#include <unotools/lingucfg.hxx>
32
#include <unotools/linguprops.hxx>
33
#include <vcl/vclenum.hxx>
34
#include <vcl/weld/Builder.hxx>
35
#include <utility>
36
#include <osl/diagnose.h>
37
38
namespace textconversiondlgs
39
{
40
41
using namespace ::com::sun::star;
42
using namespace ::com::sun::star::uno;
43
44
DictionaryList::DictionaryList(std::unique_ptr<weld::TreeView> xControl)
45
0
    : m_xControl(std::move(xControl))
46
0
    , m_xIter(m_xControl->make_iterator())
47
0
    , m_pED_Term(nullptr)
48
0
    , m_pED_Mapping(nullptr)
49
0
    , m_pLB_Property(nullptr)
50
0
{
51
0
    m_xControl->make_sorted();
52
0
}
53
54
OUString DictionaryList::getPropertyTypeName( sal_Int16 nConversionPropertyType ) const
55
0
{
56
0
    if (!m_pLB_Property || !m_pLB_Property->get_count())
57
0
        return OUString();
58
59
0
    sal_uInt16 nPos = static_cast<sal_uInt16>( nConversionPropertyType )-1;
60
0
    if (nPos < m_pLB_Property->get_count())
61
0
        return m_pLB_Property->get_text(nPos);
62
0
    return m_pLB_Property->get_text(0);
63
0
}
64
65
void DictionaryList::save()
66
0
{
67
0
    if (!m_xDictionary.is())
68
0
        return;
69
70
0
    Reference< linguistic2::XConversionPropertyType > xPropertyType( m_xDictionary, uno::UNO_QUERY );
71
72
0
    sal_Int32 nN;
73
0
    DictionaryEntry* pE;
74
75
0
    for( nN = m_aToBeDeleted.size(); nN--; )
76
0
    {
77
0
        pE = m_aToBeDeleted[nN];
78
0
        m_xDictionary->removeEntry( pE->m_aTerm, pE->m_aMapping );
79
0
    }
80
0
    int nRowCount = m_xControl->n_children();
81
0
    for( nN = nRowCount; nN--; )
82
0
    {
83
0
        pE = getEntryOnPos( nN );
84
0
        if(pE->m_bNewEntry)
85
0
        {
86
0
            try
87
0
            {
88
0
                m_xDictionary->addEntry( pE->m_aTerm, pE->m_aMapping );
89
0
                xPropertyType->setPropertyType( pE->m_aTerm, pE->m_aMapping, pE->m_nConversionPropertyType );
90
0
            }
91
0
            catch( uno::Exception& )
92
0
            {
93
94
0
            }
95
0
        }
96
0
    }
97
0
    Reference< util::XFlushable > xFlush( m_xDictionary, uno::UNO_QUERY );
98
0
    if( xFlush.is() )
99
0
        xFlush->flush();
100
0
}
101
102
void DictionaryList::deleteAll()
103
0
{
104
0
    sal_Int32 nN;
105
0
    int nRowCount = m_xControl->n_children();
106
0
    for( nN = nRowCount; nN--; )
107
0
        deleteEntryOnPos( nN  );
108
0
    for( nN = m_aToBeDeleted.size(); nN--; )
109
0
    {
110
0
        DictionaryEntry* pE = m_aToBeDeleted[nN];
111
0
        delete pE;
112
0
    }
113
0
    m_aToBeDeleted.clear();
114
0
}
115
116
void DictionaryList::refillFromDictionary( sal_Int32 nTextConversionOptions )
117
0
{
118
0
    deleteAll();
119
120
0
    if(!m_xDictionary.is())
121
0
        return;
122
123
0
    const Sequence< OUString > aLeftList(  m_xDictionary->getConversionEntries( linguistic2::ConversionDirection_FROM_LEFT ) );
124
125
0
    Reference< linguistic2::XConversionPropertyType > xPropertyType( m_xDictionary, uno::UNO_QUERY );
126
127
0
    OUString aRight;
128
0
    sal_Int16 nConversionPropertyType;
129
130
0
    for(const OUString& aLeft : aLeftList)
131
0
    {
132
0
        Sequence< OUString > aRightList( m_xDictionary->getConversions(
133
0
            aLeft, 0, aLeft.getLength()
134
0
            , linguistic2::ConversionDirection_FROM_LEFT, nTextConversionOptions ) );
135
136
0
        if(aRightList.getLength()!=1)
137
0
        {
138
0
            OSL_FAIL("The Chinese Translation Dictionary should have exactly one Mapping for each term.");
139
0
            continue;
140
0
        }
141
142
0
        aRight = aRightList[0];
143
0
        nConversionPropertyType = linguistic2::ConversionPropertyType::OTHER;
144
0
        if(xPropertyType.is())
145
0
            nConversionPropertyType = xPropertyType->getPropertyType(aLeft, aRight);
146
147
0
        DictionaryEntry* pEntry = new DictionaryEntry( aLeft, aRight, nConversionPropertyType );
148
149
0
        m_xControl->append(m_xIter.get());
150
0
        m_xControl->set_text(*m_xIter, pEntry->m_aTerm, 0);
151
0
        m_xControl->set_text(*m_xIter, pEntry->m_aMapping, 1);
152
0
        m_xControl->set_text(*m_xIter, getPropertyTypeName(pEntry->m_nConversionPropertyType), 2);
153
0
        m_xControl->set_id(*m_xIter, weld::toId(pEntry));
154
0
    }
155
0
}
156
157
DictionaryEntry* DictionaryList::getFirstSelectedEntry() const
158
0
{
159
0
    DictionaryEntry* pRet=nullptr;
160
0
    int nN = m_xControl->get_selected_index();
161
0
    if (nN != -1)
162
0
        pRet = getEntryOnPos( nN );
163
0
    return pRet;
164
0
}
165
166
DictionaryEntry* DictionaryList::getEntryOnPos(sal_Int32 nPos) const
167
0
{
168
0
    OUString sLBEntry = m_xControl->get_id(nPos);
169
0
    return weld::fromId<DictionaryEntry*>(sLBEntry);
170
0
}
171
172
DictionaryEntry* DictionaryList::getTermEntry( std::u16string_view rTerm ) const
173
0
{
174
0
    int nRowCount = m_xControl->n_children();
175
0
    for( sal_Int32 nN = nRowCount; nN--; )
176
0
    {
177
0
        DictionaryEntry* pE = getEntryOnPos( nN );
178
0
        if( pE && rTerm == pE->m_aTerm )
179
0
            return pE;
180
0
    }
181
0
    return nullptr;
182
0
}
183
184
bool DictionaryList::hasTerm( std::u16string_view rTerm ) const
185
0
{
186
0
    return getTermEntry(rTerm) !=nullptr ;
187
0
}
188
189
void DictionaryList::addEntry(const OUString& rTerm, const OUString& rMapping,
190
                              sal_Int16 nConversionPropertyType, int nPos)
191
0
{
192
0
    if( hasTerm( rTerm ) )
193
0
        return;
194
195
0
    DictionaryEntry* pEntry = new DictionaryEntry( rTerm, rMapping, nConversionPropertyType, true );
196
0
    m_xControl->insert(nPos, m_xIter.get());
197
0
    m_xControl->set_text(*m_xIter, pEntry->m_aTerm, 0);
198
0
    m_xControl->set_text(*m_xIter, pEntry->m_aMapping, 1);
199
0
    m_xControl->set_text(*m_xIter, getPropertyTypeName(pEntry->m_nConversionPropertyType), 2);
200
0
    m_xControl->set_id(*m_xIter, weld::toId(pEntry));
201
0
    m_xControl->select(*m_xIter);
202
0
}
203
204
void DictionaryList::deleteEntryOnPos( sal_Int32 nPos  )
205
0
{
206
0
    DictionaryEntry* pEntry = getEntryOnPos( nPos );
207
0
    m_xControl->remove(nPos);
208
0
    if (pEntry)
209
0
    {
210
0
        if( pEntry->m_bNewEntry )
211
0
            delete pEntry;
212
0
        else
213
0
            m_aToBeDeleted.push_back( pEntry );
214
0
    }
215
0
}
216
217
int DictionaryList::deleteEntries( std::u16string_view rTerm )
218
0
{
219
0
    int nPos = -1;
220
0
    int nRowCount = m_xControl->n_children();
221
0
    for (sal_Int32 nN = nRowCount; nN--;)
222
0
    {
223
0
        DictionaryEntry* pCurEntry = getEntryOnPos( nN );
224
0
        if( rTerm == pCurEntry->m_aTerm )
225
0
        {
226
0
            nPos = nN;
227
0
            m_xControl->remove(nN);
228
0
            if( pCurEntry->m_bNewEntry )
229
0
                delete pCurEntry;
230
0
            else
231
0
                m_aToBeDeleted.push_back( pCurEntry );
232
0
        }
233
0
    }
234
0
    return nPos;
235
0
}
236
237
DictionaryEntry::DictionaryEntry( OUString aTerm, OUString aMapping
238
                    , sal_Int16 nConversionPropertyType
239
                    , bool bNewEntry )
240
0
        : m_aTerm(std::move( aTerm ))
241
0
        , m_aMapping(std::move( aMapping ))
242
0
        , m_nConversionPropertyType( nConversionPropertyType )
243
0
        , m_bNewEntry( bNewEntry )
244
0
{
245
0
    if( m_nConversionPropertyType == 0 )
246
0
        m_nConversionPropertyType = 1;
247
0
}
248
249
DictionaryEntry::~DictionaryEntry()
250
0
{
251
0
}
252
253
IMPL_LINK_NOARG(ChineseDictionaryDialog, SizeAllocHdl, const Size&, void)
254
0
{
255
0
    DictionaryList* pControl = m_xCT_DictionaryToTraditional->get_visible() ?
256
0
                                m_xCT_DictionaryToTraditional.get() :
257
0
                                m_xCT_DictionaryToSimplified.get();
258
0
    std::vector<int> aWidths;
259
0
    int x1, x2, y, width, height;
260
0
    if (!m_xED_Mapping->get_extents_relative_to(pControl->get_widget(), x1, y, width, height))
261
0
        return;
262
0
    aWidths.push_back(x1);
263
0
    if (!m_xLB_Property->get_extents_relative_to(pControl->get_widget(), x2, y, width, height))
264
0
        return;
265
0
    aWidths.push_back(x2 - x1);
266
0
    m_xCT_DictionaryToTraditional->get_widget().set_column_fixed_widths(aWidths);
267
0
    m_xCT_DictionaryToSimplified->get_widget().set_column_fixed_widths(aWidths);
268
0
}
269
270
void DictionaryList::init(const Reference< linguistic2::XConversionDictionary>& xDictionary,
271
    weld::Entry *pED_Term, weld::Entry *pED_Mapping, weld::ComboBox *pLB_Property)
272
0
{
273
0
    if (m_xDictionary.is())
274
0
        return;
275
276
0
    m_xDictionary = xDictionary;
277
278
0
    m_pED_Term = pED_Term;
279
0
    m_pED_Mapping = pED_Mapping;
280
0
    m_pLB_Property = pLB_Property;
281
282
0
    m_xControl->set_sort_column(0);
283
0
    m_xControl->set_sort_indicator(TRISTATE_TRUE, 0);
284
285
0
    std::vector<int> aWidths
286
0
    {
287
0
        o3tl::narrowing<int>(m_pED_Term->get_preferred_size().Width()),
288
0
        o3tl::narrowing<int>(m_pED_Mapping->get_preferred_size().Width())
289
0
    };
290
0
    m_xControl->set_column_fixed_widths(aWidths);
291
0
}
292
293
void ChineseDictionaryDialog::initDictionaryControl(DictionaryList *pList,
294
    const Reference< linguistic2::XConversionDictionary>& xDictionary)
295
0
{
296
    //set widgets to track the width of for columns
297
0
    pList->init(xDictionary,
298
0
        m_xED_Term.get(), m_xED_Mapping.get(), m_xLB_Property.get());
299
0
}
300
301
ChineseDictionaryDialog::ChineseDictionaryDialog(weld::Window* pParent)
302
0
    : GenericDialogController(pParent, u"svx/ui/chinesedictionary.ui"_ustr, u"ChineseDictionaryDialog"_ustr)
303
0
    , m_nTextConversionOptions(i18n::TextConversionOption::NONE)
304
0
    , m_xRB_To_Simplified(m_xBuilder->weld_radio_button(u"tradtosimple"_ustr))
305
0
    , m_xRB_To_Traditional(m_xBuilder->weld_radio_button(u"simpletotrad"_ustr))
306
0
    , m_xCB_Reverse(m_xBuilder->weld_check_button(u"reverse"_ustr))
307
0
    , m_xED_Term(m_xBuilder->weld_entry(u"term"_ustr))
308
0
    , m_xED_Mapping(m_xBuilder->weld_entry(u"mapping"_ustr))
309
0
    , m_xLB_Property(m_xBuilder->weld_combo_box(u"property"_ustr))
310
0
    , m_xCT_DictionaryToSimplified(new DictionaryList(m_xBuilder->weld_tree_view(u"tradtosimpleview"_ustr)))
311
0
    , m_xCT_DictionaryToTraditional(new DictionaryList(m_xBuilder->weld_tree_view(u"simpletotradview"_ustr)))
312
0
    , m_xPB_Add(m_xBuilder->weld_button(u"add"_ustr))
313
0
    , m_xPB_Modify(m_xBuilder->weld_button(u"modify"_ustr))
314
0
    , m_xPB_Delete(m_xBuilder->weld_button(u"delete"_ustr))
315
0
{
316
0
    m_xCT_DictionaryToSimplified->set_size_request(-1, m_xCT_DictionaryToSimplified->get_height_rows(8));
317
0
    m_xCT_DictionaryToTraditional->set_size_request(-1, m_xCT_DictionaryToTraditional->get_height_rows(8));
318
319
0
    SvtLinguConfig  aLngCfg;
320
0
    bool bValue;
321
0
    Any aAny( aLngCfg.GetProperty( UPN_IS_REVERSE_MAPPING ) );
322
0
    if( aAny >>= bValue )
323
0
        m_xCB_Reverse->set_active( bValue );
324
325
0
    m_xLB_Property->set_active(0);
326
327
0
    Reference< linguistic2::XConversionDictionary > xDictionary_To_Simplified;
328
0
    Reference< linguistic2::XConversionDictionary > xDictionary_To_Traditional;
329
    //get dictionaries
330
0
    {
331
0
        css::uno::Reference<css::uno::XComponentContext> xContext = ::comphelper::getProcessComponentContext();
332
0
        if (xContext.is())
333
0
        {
334
0
            Reference< linguistic2::XConversionDictionaryList > xDictionaryList = linguistic2::ConversionDictionaryList::create(xContext);
335
0
            Reference< container::XNameContainer > xContainer( xDictionaryList->getDictionaryContainer() );
336
0
            if(xContainer.is())
337
0
            {
338
0
                try
339
0
                {
340
0
                    OUString aNameTo_Simplified(u"ChineseT2S"_ustr);
341
0
                    OUString aNameTo_Traditional(u"ChineseS2T"_ustr);
342
0
                    lang::Locale aLocale;
343
0
                    aLocale.Language = "zh";
344
345
0
                    if( xContainer->hasByName( aNameTo_Simplified ) )
346
0
                        xDictionary_To_Simplified.set(
347
0
                                xContainer->getByName( aNameTo_Simplified ), UNO_QUERY );
348
0
                    else
349
0
                    {
350
0
                        aLocale.Country = "TW";
351
0
                        xDictionary_To_Simplified =
352
0
                                xDictionaryList->addNewDictionary( aNameTo_Simplified
353
0
                                    , aLocale, linguistic2::ConversionDictionaryType::SCHINESE_TCHINESE
354
0
                                );
355
0
                    }
356
0
                    if (xDictionary_To_Simplified.is())
357
0
                        xDictionary_To_Simplified->setActive( true );
358
359
360
0
                    if( xContainer->hasByName( aNameTo_Traditional ) )
361
0
                        xDictionary_To_Traditional.set(
362
0
                                xContainer->getByName( aNameTo_Traditional ), UNO_QUERY );
363
0
                    else
364
0
                    {
365
0
                        aLocale.Country = "CN";
366
0
                        xDictionary_To_Traditional =
367
0
                                xDictionaryList->addNewDictionary( aNameTo_Traditional
368
0
                                    ,aLocale, linguistic2::ConversionDictionaryType::SCHINESE_TCHINESE);
369
0
                    }
370
0
                    if (xDictionary_To_Traditional.is())
371
0
                        xDictionary_To_Traditional->setActive( true );
372
373
0
                }
374
0
                catch(const uno::Exception& )
375
0
                {
376
0
                }
377
0
            }
378
0
        }
379
0
    }
380
381
    //init dictionary controls
382
0
    initDictionaryControl(m_xCT_DictionaryToSimplified.get(), xDictionary_To_Simplified);
383
0
    initDictionaryControl(m_xCT_DictionaryToTraditional.get(), xDictionary_To_Traditional);
384
385
    //set hdl
386
0
    m_xCT_DictionaryToSimplified->connect_column_clicked(LINK(this, ChineseDictionaryDialog, ToSimplifiedHeaderBarClick));
387
0
    m_xCT_DictionaryToTraditional->connect_column_clicked(LINK(this, ChineseDictionaryDialog, ToTraditionalHeaderBarClick));
388
389
0
    updateAfterDirectionChange();
390
391
0
    m_xED_Term->connect_changed( LINK( this, ChineseDictionaryDialog, EditFieldsHdl ) );
392
0
    m_xED_Mapping->connect_changed( LINK( this, ChineseDictionaryDialog, EditFieldsHdl ) );
393
0
    m_xLB_Property->connect_changed( LINK( this, ChineseDictionaryDialog, EditFieldsListBoxHdl ) );
394
395
0
    m_xRB_To_Simplified->connect_toggled( LINK( this, ChineseDictionaryDialog, DirectionHdl ) );
396
397
0
    m_xCT_DictionaryToSimplified->connect_changed( LINK( this, ChineseDictionaryDialog, MappingSelectHdl ));
398
0
    m_xCT_DictionaryToTraditional->connect_changed( LINK( this, ChineseDictionaryDialog, MappingSelectHdl ));
399
400
0
    m_xPB_Add->connect_clicked( LINK( this, ChineseDictionaryDialog, AddHdl ) );
401
0
    m_xPB_Modify->connect_clicked( LINK( this, ChineseDictionaryDialog, ModifyHdl ) );
402
0
    m_xPB_Delete->connect_clicked( LINK( this, ChineseDictionaryDialog, DeleteHdl ) );
403
404
0
    m_xED_Mapping->connect_size_allocate(LINK(this, ChineseDictionaryDialog, SizeAllocHdl));
405
0
    m_xLB_Property->connect_size_allocate(LINK(this, ChineseDictionaryDialog, SizeAllocHdl));
406
0
}
407
408
ChineseDictionaryDialog::~ChineseDictionaryDialog()
409
0
{
410
0
}
411
412
void ChineseDictionaryDialog::setDirectionAndTextConversionOptions( bool bDirectionToSimplified, sal_Int32 nTextConversionOptions /*i18n::TextConversionOption*/ )
413
0
{
414
0
    if( bDirectionToSimplified == m_xRB_To_Simplified->get_active()
415
0
        && nTextConversionOptions == m_nTextConversionOptions )
416
0
        return;
417
418
0
    m_nTextConversionOptions = nTextConversionOptions;
419
420
0
    if (bDirectionToSimplified)
421
0
        m_xRB_To_Simplified->set_active(true);
422
0
    else
423
0
        m_xRB_To_Traditional->set_active(true);
424
0
    updateAfterDirectionChange();
425
0
}
426
427
IMPL_LINK_NOARG(ChineseDictionaryDialog, DirectionHdl, weld::Toggleable&, void)
428
0
{
429
0
    updateAfterDirectionChange();
430
0
}
431
432
void ChineseDictionaryDialog::updateAfterDirectionChange()
433
0
{
434
0
    Reference< linguistic2::XConversionDictionary > xDictionary;
435
436
0
    if (m_xRB_To_Simplified->get_active())
437
0
    {
438
0
        m_xCT_DictionaryToTraditional->hide();
439
0
        m_xCT_DictionaryToSimplified->show();
440
0
        xDictionary = m_xCT_DictionaryToSimplified->m_xDictionary;
441
0
    }
442
0
    else
443
0
    {
444
0
        m_xCT_DictionaryToSimplified->hide();
445
0
        m_xCT_DictionaryToTraditional->show();
446
0
        xDictionary = m_xCT_DictionaryToTraditional->m_xDictionary;
447
0
    }
448
449
0
    updateButtons();
450
0
}
451
452
IMPL_LINK_NOARG(ChineseDictionaryDialog, EditFieldsListBoxHdl, weld::ComboBox&, void)
453
0
{
454
0
    updateButtons();
455
0
}
456
457
IMPL_LINK_NOARG(ChineseDictionaryDialog, EditFieldsHdl, weld::Entry&, void)
458
0
{
459
0
    updateButtons();
460
0
}
461
462
IMPL_LINK_NOARG(ChineseDictionaryDialog, MappingSelectHdl, weld::TreeView&, void)
463
0
{
464
0
    DictionaryEntry* pE = getActiveDictionary().getFirstSelectedEntry();
465
0
    if (pE)
466
0
    {
467
0
        m_xED_Term->set_text( pE->m_aTerm );
468
0
        m_xED_Mapping->set_text( pE->m_aMapping );
469
0
        sal_Int16 nPos = pE->m_nConversionPropertyType-1;
470
0
        if (nPos<0 || nPos>=m_xLB_Property->get_count())
471
0
            nPos=0;
472
0
        if (m_xLB_Property->get_count())
473
0
            m_xLB_Property->set_active(nPos);
474
0
    }
475
476
0
    updateButtons();
477
0
}
478
479
bool ChineseDictionaryDialog::isEditFieldsHaveContent() const
480
0
{
481
0
    return !m_xED_Term->get_text().isEmpty() && !m_xED_Mapping->get_text().isEmpty();
482
0
}
483
484
bool ChineseDictionaryDialog::isEditFieldsContentEqualsSelectedListContent() const
485
0
{
486
0
    DictionaryEntry* pE = getActiveDictionary().getFirstSelectedEntry();
487
0
    if( pE )
488
0
    {
489
0
        if (pE->m_aTerm != m_xED_Term->get_text())
490
0
            return false;
491
0
        if (pE->m_aMapping != m_xED_Mapping->get_text())
492
0
            return false;
493
0
        if (pE->m_nConversionPropertyType != m_xLB_Property->get_active() + 1)
494
0
            return false;
495
0
        return true;
496
0
    }
497
0
    return false;
498
0
}
499
500
const DictionaryList& ChineseDictionaryDialog::getActiveDictionary() const
501
0
{
502
0
    if( m_xRB_To_Traditional->get_active() )
503
0
        return *m_xCT_DictionaryToTraditional;
504
0
    return *m_xCT_DictionaryToSimplified;
505
0
}
506
507
DictionaryList& ChineseDictionaryDialog::getActiveDictionary()
508
0
{
509
0
    if( m_xRB_To_Traditional->get_active() )
510
0
        return *m_xCT_DictionaryToTraditional;
511
0
    return *m_xCT_DictionaryToSimplified;
512
0
}
513
514
const DictionaryList& ChineseDictionaryDialog::getReverseDictionary() const
515
0
{
516
0
    if( m_xRB_To_Traditional->get_active() )
517
0
        return *m_xCT_DictionaryToSimplified;
518
0
    return *m_xCT_DictionaryToTraditional;
519
0
}
520
521
DictionaryList& ChineseDictionaryDialog::getReverseDictionary()
522
0
{
523
0
    if( m_xRB_To_Traditional->get_active() )
524
0
        return *m_xCT_DictionaryToSimplified;
525
0
    return *m_xCT_DictionaryToTraditional;
526
0
}
527
528
void ChineseDictionaryDialog::updateButtons()
529
0
{
530
0
    bool bAdd = isEditFieldsHaveContent() && !getActiveDictionary().hasTerm(m_xED_Term->get_text());
531
0
    m_xPB_Add->set_sensitive( bAdd );
532
533
0
    m_xPB_Delete->set_sensitive(!bAdd && getActiveDictionary().get_selected_index() != -1);
534
535
0
    bool bModify = false;
536
0
    {
537
0
        DictionaryEntry* pFirstSelectedEntry = getActiveDictionary().getFirstSelectedEntry();
538
0
        bModify = !bAdd && pFirstSelectedEntry && pFirstSelectedEntry->m_aTerm == m_xED_Term->get_text();
539
0
        if( bModify && isEditFieldsContentEqualsSelectedListContent() )
540
0
            bModify = false;
541
0
    }
542
0
    m_xPB_Modify->set_sensitive( bModify );
543
0
}
544
545
IMPL_LINK_NOARG(ChineseDictionaryDialog, AddHdl, weld::Button&, void)
546
0
{
547
0
    if( !isEditFieldsHaveContent() )
548
0
        return;
549
550
0
    sal_Int16 nConversionPropertyType = m_xLB_Property->get_active() + 1;
551
552
0
    getActiveDictionary().addEntry( m_xED_Term->get_text(), m_xED_Mapping->get_text(), nConversionPropertyType );
553
554
0
    if( m_xCB_Reverse->get_active() )
555
0
    {
556
0
        getReverseDictionary().deleteEntries( m_xED_Mapping->get_text() );
557
0
        getReverseDictionary().addEntry( m_xED_Mapping->get_text(), m_xED_Term->get_text(), nConversionPropertyType );
558
0
    }
559
560
0
    updateButtons();
561
0
}
562
563
IMPL_LINK_NOARG(ChineseDictionaryDialog, ModifyHdl, weld::Button&, void)
564
0
{
565
0
    OUString aTerm( m_xED_Term->get_text() );
566
0
    OUString aMapping( m_xED_Mapping->get_text() );
567
0
    sal_Int16 nConversionPropertyType = m_xLB_Property->get_active() + 1;
568
569
0
    DictionaryList& rActive  = getActiveDictionary();
570
0
    DictionaryList& rReverse = getReverseDictionary();
571
572
0
    DictionaryEntry* pE = rActive.getFirstSelectedEntry();
573
0
    if( pE && pE->m_aTerm != aTerm )
574
0
        return;
575
576
0
    if( pE )
577
0
    {
578
0
        if( pE->m_aMapping != aMapping || pE->m_nConversionPropertyType != nConversionPropertyType )
579
0
        {
580
0
            if( m_xCB_Reverse->get_active() )
581
0
            {
582
0
                rReverse.deleteEntries( pE->m_aMapping );
583
0
                int nPos = rReverse.deleteEntries( aMapping );
584
0
                rReverse.addEntry( aMapping, aTerm, nConversionPropertyType, nPos );
585
0
            }
586
587
0
            int nPos = rActive.deleteEntries( aTerm );
588
0
            rActive.addEntry( aTerm, aMapping, nConversionPropertyType, nPos );
589
0
        }
590
0
    }
591
592
0
    updateButtons();
593
0
}
594
595
IMPL_LINK_NOARG(ChineseDictionaryDialog, DeleteHdl, weld::Button&, void)
596
0
{
597
0
    DictionaryList& rActive  = getActiveDictionary();
598
0
    DictionaryList& rReverse = getReverseDictionary();
599
600
0
    int nEntry = rActive.get_selected_index();
601
0
    if (nEntry != -1)
602
0
    {
603
0
        DictionaryEntry* pEntry = rActive.getEntryOnPos(nEntry);
604
0
        if (pEntry)
605
0
        {
606
0
            OUString aMapping = pEntry->m_aMapping;
607
0
            rActive.deleteEntryOnPos(nEntry);
608
0
            if (m_xCB_Reverse->get_active())
609
0
                rReverse.deleteEntries(aMapping);
610
0
        }
611
0
    }
612
613
0
    updateButtons();
614
0
}
615
616
short ChineseDictionaryDialog::run()
617
0
{
618
0
    sal_Int32 nTextConversionOptions = m_nTextConversionOptions;
619
0
    if(m_nTextConversionOptions & i18n::TextConversionOption::USE_CHARACTER_VARIANTS )
620
0
        nTextConversionOptions = nTextConversionOptions^i18n::TextConversionOption::USE_CHARACTER_VARIANTS ;
621
622
0
    m_xCT_DictionaryToSimplified->refillFromDictionary( nTextConversionOptions );
623
0
    m_xCT_DictionaryToTraditional->refillFromDictionary( m_nTextConversionOptions );
624
625
0
    short nRet = GenericDialogController::run();
626
627
0
    if( nRet == RET_OK )
628
0
    {
629
        //save settings to configuration
630
0
        SvtLinguConfig  aLngCfg;
631
0
        aLngCfg.SetProperty( UPN_IS_REVERSE_MAPPING, uno::Any(m_xCB_Reverse->get_active()) );
632
633
0
        m_xCT_DictionaryToSimplified->save();
634
0
        m_xCT_DictionaryToTraditional->save();
635
0
    }
636
637
0
    m_xCT_DictionaryToSimplified->deleteAll();
638
0
    m_xCT_DictionaryToTraditional->deleteAll();
639
640
0
    return nRet;
641
0
}
642
643
void ChineseDictionaryDialog::HeaderBarClick(DictionaryList& rList, int nColumn)
644
0
{
645
0
    bool bSortAtoZ = rList.get_sort_order();
646
647
    //set new arrow positions in headerbar
648
0
    if (nColumn == rList.get_sort_column())
649
0
    {
650
0
        bSortAtoZ = !bSortAtoZ;
651
0
        rList.set_sort_order(bSortAtoZ);
652
0
    }
653
0
    else
654
0
    {
655
0
        rList.set_sort_indicator(TRISTATE_INDET, rList.get_sort_column());
656
0
        rList.set_sort_column(nColumn);
657
0
    }
658
659
    //sort lists
660
0
    rList.set_sort_indicator(bSortAtoZ ? TRISTATE_TRUE : TRISTATE_FALSE, nColumn);
661
0
}
662
663
IMPL_LINK(ChineseDictionaryDialog, ToSimplifiedHeaderBarClick, int, nColumn, void)
664
0
{
665
0
    HeaderBarClick(*m_xCT_DictionaryToSimplified, nColumn);
666
0
}
667
668
IMPL_LINK(ChineseDictionaryDialog, ToTraditionalHeaderBarClick, int, nColumn, void)
669
0
{
670
0
    HeaderBarClick(*m_xCT_DictionaryToTraditional, nColumn);
671
0
}
672
673
} //end namespace
674
675
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */