87758c0fd6bfa183cb32d54c91ddabd95230ad71
[platform/upstream/ibus-libpinyin.git] / src / PYPhraseEditor.h
1 /* vim:set et ts=4 sts=4:
2  *
3  * ibus-pinyin - The Chinese PinYin engine for IBus
4  *
5  * Copyright (c) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
6  *
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)
10  * any later version.
11  *
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.
16  *
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.
20  */
21 #ifndef __PY_PHRASE_EDITOR_H_
22 #define __PY_PHRASE_EDITOR_H_
23
24 #include <boost/shared_ptr.hpp>
25 #include "PYString.h"
26 #include "PYPhraseArray.h"
27 #include "PYPinyinArray.h"
28
29 #define FILL_GRAN (12)
30
31 namespace PY {
32
33 class Query;
34 class Database;
35 class PinyinProperties;
36 class Config;
37
38 class PhraseEditor {
39 public:
40     PhraseEditor (PinyinProperties & props, Config & config);
41     ~PhraseEditor (void);
42
43     const String & selectedString (void) const  { return m_selected_string; }
44     const PinyinArray & pinyin (void) const     { return m_pinyin; }
45     const PhraseArray & candidates (void) const { return m_candidates; }
46     guint cursor (void) const                   { return m_cursor; }
47
48     guint cursorInChar (void) const
49     {
50         return m_cursor == 0 ? 0 : m_pinyin[m_cursor - 1].begin + m_pinyin[m_cursor - 1].len;
51     }
52
53     gboolean pinyinExistsAfterCursor (void) const
54     {
55         return m_pinyin.size () > m_cursor;
56     }
57
58     const Phrase & candidate (guint i) const
59     {
60         return m_candidates[i];
61     }
62
63     gboolean fillCandidates (void);
64
65     const PhraseArray & candidate0 (void) const
66     {
67         return m_candidate_0_phrases;
68     }
69
70     gboolean candidateIsUserPhease (guint i) const
71     {
72         const Phrase & phrase = m_candidates[i];
73         return phrase.len > 1 && phrase.user_freq > 0 && phrase.freq == 0;
74     }
75
76     gboolean unselectCandidates (void)
77     {
78         if (m_cursor == 0) {
79             return FALSE;
80         }
81         else {
82             m_selected_phrases.clear ();
83             m_selected_string.truncate (0);
84             m_cursor = 0;
85             updateCandidates ();
86             return TRUE;
87         }
88     }
89
90     void reset (void)
91     {
92         m_candidates.clear ();
93         m_selected_phrases.clear ();
94         m_selected_string.truncate (0);
95         m_candidate_0_phrases.clear ();
96         m_pinyin.clear ();
97         m_cursor = 0;
98         m_query.reset ();
99     }
100
101     gboolean update (const PinyinArray &pinyin);
102     gboolean selectCandidate (guint i);
103     gboolean resetCandidate (guint i);
104     void commit (void);
105
106     gboolean empty (void) const
107     {
108         return m_selected_string.empty () && m_candidate_0_phrases.empty ();
109     }
110
111     operator gboolean (void) const
112     {
113         return !empty ();
114     }
115
116 private:
117     void updateCandidates (void);
118     void updateTheFirstCandidate (void);
119
120 private:
121     PhraseArray m_candidates;           // candidates phrase array
122     PhraseArray m_selected_phrases;     // selected phrases, before cursor
123     String      m_selected_string;      // selected phrases, in string format
124     PhraseArray m_candidate_0_phrases;  // the first candidate in phrase array format
125     PinyinArray m_pinyin;
126     guint m_cursor;
127     PinyinProperties & m_props;
128     boost::shared_ptr<Query> m_query;
129     Config    & m_config;
130 };
131
132 };
133
134 #endif