OSDN Git Service

ab4218b0c0fcee445392cc18549bcc9e05692931
[wordring-tm/wordring-tm.git] / proxy / tmeditorwidget.h
1 #ifndef TMWIDGET_H
2 #define TMWIDGET_H
3
4 #include "tmtext.h"
5 #include "textwidget.h"
6
7 #include "html.h"
8 #include "language.h"
9
10 #include <QWidget>
11 #include <QPlainTextEdit>
12 #include <QTextCursor>
13
14 #include <QList>
15 #include <QIcon>
16 #include <QString>
17 #include <QColor>
18
19 #include <QJsonDocument>
20 #include <QJsonObject>
21 #include <QJsonArray>
22
23 #include <QMutex>
24
25 QT_BEGIN_NAMESPACE
26 class QSettings;
27 class QToolBar;
28 class QAction;
29
30 class QTextEdit;
31 class QLineEdit;
32
33 class QKeyEvent;
34 QT_END_NAMESPACE
35
36 class Settings;
37 class HtmlDocument;
38
39 namespace TM
40 {
41
42 class Service;
43 class SocketConnection;
44 class Editor;
45 class TargetPanel;
46
47 class EditorWidget : public QWidget
48 {
49         Q_OBJECT
50 public:
51         EditorWidget(Settings *settings, Service *service, QWidget *parent = 0);
52
53         void attach(SocketConnection *socket);
54         void detach(SocketConnection *socket);
55
56         void set_http_port(quint16 http_port);
57
58         void set_edit_mode(bool mode);
59         bool edit_mode();
60         void set_link_mode(bool mode);
61         bool link_mode();
62         void set_link_mode_disabled(bool disable);
63
64         int source_language();
65         int target_language();
66
67         void set_string(QString source_, QString target_);
68         void append_sentence();
69         void save_sentence();
70
71 signals:
72         void editModeChanged(bool mode_);
73         void linkModeChanged(bool mode_);
74
75         void contentsEdited();
76
77 public slots:
78         void onLanguageLoaded(int code, QString name, QIcon icon);
79         void onEditModeTriggered(bool);
80         void onLinkModeTriggered(bool checked);
81         void onSourceLanguageTriggered(bool);
82         void onTargetLanguageTriggered(bool);
83         void onBrowserTriggered(bool);
84
85 private:
86         QMutex m_mutex;
87         Service *m_service;
88         Settings *m_settings;
89         quint16 m_http_port;
90         SocketConnection *m_socket;
91
92         QToolBar *m_toolbar;
93         QAction *m_edit_mode;
94         QAction *m_link;
95         QAction *m_slang;
96         QAction *m_tlang;
97
98         Editor *m_edit;
99 };
100
101 class EditorPanel : public TextPanel
102 {
103         Q_OBJECT
104 public:
105         enum : int { Word = QTextFormat::UserProperty };
106
107 public:
108         explicit EditorPanel(QWidget *parent);
109         void set_editor(Editor *editor);
110
111         Text::pointer sentence();
112         virtual void set_sentence(Text::pointer sentence);
113
114         bool is_empty() const;
115
116         QTextCursor select_cursor(Text::pointer word);
117         Text::pointer select_word(QPoint const &pos);
118         void highlight(Text::pointer word, QColor color);
119         void highlight(WordLink::storage_type *link, QColor color);
120         void clear_highlight();
121
122         QColor color(int index) const;
123
124 protected:
125         Editor *m_editor;
126         Text::pointer m_sentence;
127 };
128
129 class SourcePanel : public EditorPanel
130 {
131         Q_OBJECT
132 public:
133         explicit SourcePanel(QWidget *parent);
134
135         TargetPanel* target_panel();
136         void set_target_panel(TargetPanel *target);
137
138         void commit_link();
139         WordLinker* linker();
140
141         void ensure_highlight();
142
143 protected:
144         bool canInsertFromMimeData(QMimeData const *source) const;
145         void insertFromMimeData(QMimeData const *source);
146
147         void inputMethodEvent(QInputMethodEvent *ev);
148         void keyPressEvent(QKeyEvent *ev);
149
150         //void do_enter();
151         void do_click(QPoint const &pos);
152         void do_click_in_link_mode(QPoint const &pos);
153
154         void do_focusin();
155         void do_focusout();
156
157 private:
158         TargetPanel *m_target_panel;
159         WordLinker m_linker;
160 };
161
162 class TargetPanel: public EditorPanel
163 {
164         Q_OBJECT
165 public:
166         explicit TargetPanel(QWidget *parent);
167
168         SourcePanel* source_panel();
169         void set_source_panel(SourcePanel *source);
170
171         void set_sentence(Text::pointer sentence);
172
173         bool is_text_saved() const;
174         void set_text_saved(bool saved);
175
176         bool is_text_dirty() const;
177         void set_text_dirty(bool dirty);
178
179 protected:
180         bool canInsertFromMimeData(QMimeData const *source) const;
181         void insertFromMimeData(QMimeData const *source);
182
183         void inputMethodEvent(QInputMethodEvent *ev);
184         void keyPressEvent(QKeyEvent *ev);
185
186         //void do_enter();
187         void do_click(QPoint const &pos);
188         void do_click_in_link_mode(QPoint const &pos);
189
190         void do_focusin();
191         void do_focusout();
192
193         void do_key_press_in_link_mode(QKeyEvent *ev);
194
195 private:
196         SourcePanel *m_source_panel;
197         bool m_text_dirty; /*!< 内容の変更を示すフラグ */
198         bool m_text_saved; /*!< データベース登録の必要性を示すフラグ */
199 };
200
201 class Editor : public TextWidget
202 {
203         Q_OBJECT
204 public:
205         Editor(Settings *settings, Service *service, QWidget *parent);
206
207         void clear();
208         void set_sentences(Text::pointer sentences);
209
210         bool edit_mode() const;
211         void set_edit_mode(bool mode_);
212         bool link_mode() const;
213         void set_link_mode(bool mode_);
214
215         bool can_link_mode() const;
216
217         EditorWidget* parent_editor_widget();
218         TargetPanel* current_target_panel();
219         TargetPanel const* current_target_panel() const;
220
221         //bool is_panel_changed(TextPanel *new_) const;
222
223 protected slots:
224         void onFocusInChild(TextPanel *new_, TextPanel *old_);
225
226 protected:
227         SourcePanel* find_source_panel(TextPanel *panel);
228
229         void do_panel_enter(SourcePanel *panel);
230         void do_panel_leave(SourcePanel *panel);
231         void do_link_mode_enter(SourcePanel *panel);
232         void do_link_mode_leave(SourcePanel *panel);
233
234 private:
235         Settings *m_settings;
236         Service *m_service;
237
238         Text::pointer m_sentences;
239         bool m_edit_mode; /*!< 編集モードのときtrue */
240         bool m_link_mode; /*!< リンクモードのときtrue */
241
242         SourcePanel *m_current_source_panel;
243 };
244
245 } // namespace TM
246
247 #endif // TMWIDGET_H