1
/* vim:set et ts=4 sts=4:
3
* ibus-pinyin - The Chinese PinYin engine for IBus
5
* Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2, or (at your option)
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
#include "PhraseEditor.h"
24
#include "PinyinProperties.h"
25
#include "SimpTradConverter.h"
29
PhraseEditor::PhraseEditor (PinyinProperties & props, Config & config)
31
m_selected_phrases (8),
32
m_selected_string (32),
33
m_candidate_0_phrases (8),
41
PhraseEditor::~PhraseEditor (void)
46
PhraseEditor::update (const PinyinArray &pinyin)
48
/* the size of pinyin must not bigger than MAX_PHRASE_LEN */
49
g_assert (pinyin.size () <= MAX_PHRASE_LEN);
54
/* FIXME, should not remove all phrases1 */
55
m_selected_phrases.clear ();
56
m_selected_string.truncate (0);
62
PhraseEditor::resetCandidate (guint i)
64
Database::instance ().remove (m_candidates[i]);
71
PhraseEditor::commit (void)
73
Database::instance ().commit (m_selected_phrases);
78
PhraseEditor::selectCandidate (guint i)
80
if (G_UNLIKELY (i >= m_candidates.size ()))
83
if (G_LIKELY (i == 0)) {
84
m_selected_phrases.insert (m_selected_phrases.end (),
85
m_candidate_0_phrases.begin (),
86
m_candidate_0_phrases.end ());
87
if (G_LIKELY (m_props.modeSimp ()))
88
m_selected_string << m_candidates[0].phrase;
90
SimpTradConverter::simpToTrad (m_candidates[0].phrase, m_selected_string);
91
m_cursor = m_pinyin.size ();
94
m_selected_phrases.push_back (m_candidates[i]);
95
if (G_LIKELY (m_props.modeSimp ()))
96
m_selected_string << m_candidates[i].phrase;
98
SimpTradConverter::simpToTrad (m_candidates[i].phrase, m_selected_string);
99
m_cursor += m_candidates[i].len;
107
PhraseEditor::updateCandidates (void)
109
m_candidates.clear ();
111
updateTheFirstCandidate ();
113
if (G_UNLIKELY (m_pinyin.size () == 0))
116
if (G_LIKELY (m_candidate_0_phrases.size () > 1)) {
119
for (guint i = 0; i < m_candidate_0_phrases.size (); i++)
120
phrase += m_candidate_0_phrases[i];
121
m_candidates.push_back (phrase);
124
m_query.reset (new Query (m_pinyin,
126
m_pinyin.size () - m_cursor,
127
m_config.option ()));
132
PhraseEditor::updateTheFirstCandidate (void)
137
m_candidate_0_phrases.clear ();
139
if (G_UNLIKELY (m_pinyin.size () == 0))
143
end = m_pinyin.size ();
145
while (begin != end) {
147
Query query (m_pinyin,
151
ret = query.fill (m_candidate_0_phrases, 1);
153
begin += m_candidate_0_phrases.back ().len;
158
PhraseEditor::fillCandidates (void)
160
if (G_UNLIKELY (m_query.get () == NULL)) {
164
gint ret = m_query->fill (m_candidates, FILL_GRAN);
166
if (G_UNLIKELY (ret < FILL_GRAN)) {
167
/* got all candidates from query */
171
return ret > 0 ? TRUE : FALSE;