libime
tablecontext.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2017-2017 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  */
6 #ifndef _FCITX_LIBIME_TABLE_TABLECONTEXT_H_
7 #define _FCITX_LIBIME_TABLE_TABLECONTEXT_H_
8 
9 /// \file
10 /// \brief Class provide input method support for table-based ones, like wubi.
11 
12 // Workaround a boost missing include bug.
13 #include <boost/type_traits/add_const.hpp>
14 
15 #include <cstddef>
16 #include <cstdint>
17 #include <memory>
18 #include <string>
19 #include <string_view>
20 #include <tuple>
21 #include <vector>
22 #include <boost/iterator/iterator_categories.hpp>
23 #include <boost/range/any_range.hpp>
24 #include <fcitx-utils/macros.h>
25 #include <libime/core/inputbuffer.h>
26 #include <libime/table/libimetable_export.h>
27 #include <libime/table/tablebaseddictionary.h>
28 #include "libime/core/lattice.h"
29 
30 namespace libime {
31 
32 class TableContextPrivate;
33 class TableBasedDictionary;
34 class UserLanguageModel;
35 
36 /// \brief Input context for table input method.
37 class LIBIMETABLE_EXPORT TableContext : public InputBuffer {
38 public:
39  using CandidateRange = boost::any_range<const SentenceResult,
40  boost::random_access_traversal_tag>;
41 
43  virtual ~TableContext();
44 
45  void erase(size_t from, size_t to) override;
46 
47  void select(size_t idx);
48 
49  bool isValidInput(uint32_t c) const;
50 
51  CandidateRange candidates() const;
52 
53  std::string candidateHint(size_t idx, bool custom = false) const;
54 
55  static std::string code(const SentenceResult &sentence);
56  static PhraseFlag flag(const SentenceResult &sentence);
57  static bool isPinyin(const SentenceResult &sentence);
58  static bool isAuto(const SentenceResult &sentence);
59 
60  bool selected() const;
61  size_t selectedSize() const;
62  std::tuple<std::string, bool> selectedSegment(size_t idx) const;
63  std::string selectedCode(size_t idx) const;
64  size_t selectedSegmentLength(size_t idx) const;
65 
66  /// \brief A simple preedit implementation.
67  /// The value is derived from function selectedSegment and currentCode.
68  std::string preedit() const;
69 
70  /// \brief Current unselected code.
71  const std::string &currentCode() const;
72 
73  /// \brief The concatenation of all selectedSegment where bool == true.
74  std::string selectedSentence() const;
75  size_t selectedLength() const;
76 
77  /// \brief Save the current selected text.
78  void learn();
79 
80  /// \brief Save the last selected text.
81  void learnLast();
82 
83  /// \brief Learn auto word from string.
84  ///
85  /// Depending on the tableOptions, it will try to learn the word in history.
86  void learnAutoPhrase(std::string_view history);
87 
88  /// \brief Learn auto word from string
89  ///
90  /// Similar to its overload, but with hint of given code.
91  void learnAutoPhrase(std::string_view history,
92  const std::vector<std::string> &hints);
93 
94  const TableBasedDictionary &dict() const;
95  TableBasedDictionary &mutableDict();
96 
97  const UserLanguageModel &model() const;
98  UserLanguageModel &mutableModel();
99  void autoSelect();
100 
101  /// Set the auto select index, usually, this is the candidate cursor index.
102  ///
103  /// \since 1.0.12
104  void setAutoSelectIndex(size_t index);
105 
106 protected:
107  bool typeImpl(const char *s, size_t length) override;
108 
109 private:
110  void update();
111  bool typeOneChar(std::string_view chr);
112 
113  std::unique_ptr<TableContextPrivate> d_ptr;
114  FCITX_DECLARE_PRIVATE(TableContext);
115 };
116 } // namespace libime
117 
118 #endif // _FCITX_LIBIME_TABLE_TABLECONTEXT_H_
Input context for table input method.
Definition: tablecontext.h:37