OSDN Git Service

4fb00a6bec796ebd7ca7dd3ac032cda4aee938fe
[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 #include "tmsocket.h"
7
8 #include "html.h"
9 #include "language.h"
10
11 #include <QWidget>
12 #include <QPlainTextEdit>
13 #include <QTextCursor>
14
15 #include <QList>
16 #include <QIcon>
17 #include <QString>
18 #include <QColor>
19
20 #include <QJsonDocument>
21 #include <QJsonObject>
22 #include <QJsonArray>
23
24 #include <QMutex>
25
26 QT_BEGIN_NAMESPACE
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 SourcePanel;
46 class TargetPanel;
47
48 class EditorWidget : public QWidget
49 {
50         Q_OBJECT
51 public:
52         EditorWidget(Settings *settings, Service *service, QWidget *parent = 0);
53
54         void attach(SocketConnection *socket);
55         void detach(SocketConnection *connection);
56
57         void set_http_port(quint16 http_port);
58
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_segment(TextSegment::pointer segment);
68         void save_sentence(TextSentence::pointer text_sentence);
69         void remove_sentence(TextSentence::pointer text_sentence);
70
71         void do_panel_entered(SourcePanel *panel);
72         void do_panel_leaved(SourcePanel *panel);
73
74 signals:
75         //void editModeChanged(bool mode_);
76         void linkModeChanged(bool mode_);
77         void sourceLanguageChanged();
78         void targetLanguageChanged();
79
80 public slots:
81         void onLanguageLoaded(int code, QString name, QIcon icon);
82         void onEditModeTriggered(bool);
83         void onLinkModeTriggered(bool checked);
84         void onSourceLanguageTriggered(bool);
85         void onTargetLanguageTriggered(bool);
86         void onBrowserTriggered(bool);
87
88 private:
89         Service *m_service;
90         Settings *m_settings;
91         quint16 m_http_port;
92         SocketConnection *m_socket;
93
94         QToolBar *m_toolbar;
95         QAction *m_edit_mode;
96         QAction *m_link;
97         QAction *m_slang;
98         QAction *m_tlang;
99
100         Editor *m_edit;
101 };
102
103 class EditorPanel : public TextPanel
104 {
105         Q_OBJECT
106 public:
107         enum : int { Word = QTextFormat::UserProperty };
108
109 public:
110         explicit EditorPanel(QWidget *parent);
111
112         Editor* parent_editor();
113         void set_parent_editor(Editor *editor);
114
115         virtual Text::pointer sentence() = 0;
116         virtual void ensure_sentence();
117
118         bool is_empty() const;
119
120         QTextCursor select_cursor(Text::pointer word);
121         Text::pointer select_word(QPoint const &pos);
122         void highlight(Text::pointer word, QColor color);
123         void highlight(WordLink::storage_type *link, QColor color);
124         void clear_highlight();
125
126         QColor color(int index) const;
127
128 signals:
129         void panelEntered();
130         void panelLeaved();
131
132 private:
133         Editor *m_parent_editor;
134 };
135
136 class SourcePanel : public EditorPanel
137 {
138         Q_OBJECT
139 public:
140         explicit SourcePanel(QWidget *parent);
141
142         int index() const;
143         void set_index(int index);
144
145         Text::pointer sentence();
146         Text::pointer source_sentence();
147         Text::pointer target_sentence();
148         void set_target_sentence(Text::pointer sentence);
149
150         TextSentence::pointer text_sentence();
151         void set_text_sentence(TextSentence::pointer text_sentence);
152
153         TargetPanel* target_panel();
154         void set_target_panel(TargetPanel *target);
155
156         void commit_link();
157         WordLinker* linker();
158
159         void ensure_highlight();
160
161 protected:
162         bool canInsertFromMimeData(QMimeData const *source) const;
163         void insertFromMimeData(QMimeData const *source);
164
165         void inputMethodEvent(QInputMethodEvent *ev);
166         void keyPressEvent(QKeyEvent *ev);
167
168         void do_click(QPoint const &pos);
169         void do_click_in_link_mode(QPoint const &pos);
170
171 private:
172         int m_index; /*!< Editor上での位置を示す索引 */
173         TargetPanel *m_target_panel;
174         TextSentence::pointer m_text_sentence;
175 };
176
177 class TargetPanel: public EditorPanel
178 {
179         Q_OBJECT
180 public:
181         explicit TargetPanel(QWidget *parent);
182
183         SourcePanel* source_panel();
184         void set_source_panel(SourcePanel *source);
185
186         Text::pointer sentence();
187         void save_sentence();
188         void ensure_sentence();
189
190         bool is_text_saved() const;
191         void set_text_saved(bool saved);
192
193         bool is_text_dirty() const;
194         void set_text_dirty(bool dirty);
195
196 protected:
197         bool canInsertFromMimeData(QMimeData const *source) const;
198         void insertFromMimeData(QMimeData const *source);
199
200         void inputMethodEvent(QInputMethodEvent *ev);
201         void keyPressEvent(QKeyEvent *ev);
202
203         void do_click(QPoint const &pos);
204         void do_click_in_link_mode(QPoint const &pos);
205
206         void do_key_press_in_link_mode(QKeyEvent *ev);
207
208 private:
209         SourcePanel *m_source_panel;
210         bool m_text_dirty; /*!< 内容の変更を示すフラグ */
211         bool m_text_saved; /*!< データベース登録の必要性を示すフラグ */
212 };
213
214 class Editor : public TextWidget
215 {
216         Q_OBJECT
217 public:
218         Editor(Settings *settings, Service *service, QWidget *parent);
219
220         void clear();
221         void set_segment(TextSegment::pointer segment);
222
223         bool edit_mode() const;
224         void set_edit_mode(bool mode_);
225         bool link_mode() const;
226         void set_link_mode(bool mode_);
227
228         bool can_link_mode() const;
229
230         EditorWidget* parent_editor_widget();
231 private:
232         TargetPanel* current_target_panel();
233         TargetPanel const* current_target_panel() const;
234
235 protected slots:
236         void onFocusInChild(TextPanel *new_, TextPanel *old_);
237
238 protected:
239         SourcePanel* find_source_panel(TextPanel *panel);
240
241         void do_panel_enter(SourcePanel *panel);
242         void do_panel_leave(SourcePanel *panel);
243         void do_link_mode_enter(SourcePanel *panel);
244         void do_link_mode_leave(SourcePanel *panel);
245
246 private:
247         void divide_target_sentence(SourcePanel *source_panel);
248
249 private:
250         Settings *m_settings;
251         Service *m_service;
252
253         int m_segment_id;
254         bool m_edit_mode; /*!< 編集モードのときtrue */
255         bool m_link_mode; /*!< リンクモードのときtrue */
256
257         SourcePanel *m_current_source_panel;
258 };
259
260 } // namespace TM
261
262 #endif // TMWIDGET_H