OSDN Git Service

データ構造決定記念バックアップ♪
[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 QSettings;
28 class QToolBar;
29 class QAction;
30
31 class QTextEdit;
32 class QLineEdit;
33
34 class QKeyEvent;
35 QT_END_NAMESPACE
36
37 class Settings;
38 class HtmlDocument;
39
40 namespace TM
41 {
42
43 class Service;
44 //class SocketConnection;
45 class Editor;
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 *socket);
56
57         void set_http_port(quint16 http_port);
58
59         void set_edit_mode(bool mode);
60         bool edit_mode();
61         void set_link_mode(bool mode);
62         bool link_mode();
63         void set_link_mode_disabled(bool disable);
64
65         int source_language();
66         int target_language();
67
68         void set_segment(TextSegment::pointer segment);
69         void save_sentence(int segment_id, int index);
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
110         Editor* parent_editor();
111         void set_parent_editor(Editor *editor);
112
113         virtual Text::pointer sentence() = 0;
114         virtual void ensure_sentence();
115
116         bool is_empty() const;
117
118         QTextCursor select_cursor(Text::pointer word);
119         Text::pointer select_word(QPoint const &pos);
120         void highlight(Text::pointer word, QColor color);
121         void highlight(WordLink::storage_type *link, QColor color);
122         void clear_highlight();
123
124         QColor color(int index) const;
125
126 private:
127         Editor *m_parent_editor;
128 };
129
130 class SourcePanel : public EditorPanel
131 {
132         Q_OBJECT
133 public:
134         explicit SourcePanel(QWidget *parent);
135
136         int index() const;
137         void set_index(int index);
138
139         Text::pointer sentence();
140         Text::pointer source_sentence();
141         Text::pointer target_sentence();
142         void set_target_sentence(Text::pointer sentence);
143
144         TextSentence::pointer text_sentence();
145         void set_text_sentence(TextSentence::pointer text_sentence);
146
147         TargetPanel* target_panel();
148         void set_target_panel(TargetPanel *target);
149
150         void commit_link();
151         WordLinker* linker();
152
153         void ensure_highlight();
154
155 protected:
156         bool canInsertFromMimeData(QMimeData const *source) const;
157         void insertFromMimeData(QMimeData const *source);
158
159         void inputMethodEvent(QInputMethodEvent *ev);
160         void keyPressEvent(QKeyEvent *ev);
161
162         void do_click(QPoint const &pos);
163         void do_click_in_link_mode(QPoint const &pos);
164
165 private:
166         int m_index; /*!< Editor上での位置を示す索引 */
167         TargetPanel *m_target_panel;
168         TextSentence::pointer m_text_sentence;
169 };
170
171 class TargetPanel: public EditorPanel
172 {
173         Q_OBJECT
174 public:
175         explicit TargetPanel(QWidget *parent);
176
177         SourcePanel* source_panel();
178         void set_source_panel(SourcePanel *source);
179
180         Text::pointer sentence();
181         void save_sentence();
182         void ensure_sentence();
183
184         bool is_text_saved() const;
185         void set_text_saved(bool saved);
186
187         bool is_text_dirty() const;
188         void set_text_dirty(bool dirty);
189
190 protected:
191         bool canInsertFromMimeData(QMimeData const *source) const;
192         void insertFromMimeData(QMimeData const *source);
193
194         void inputMethodEvent(QInputMethodEvent *ev);
195         void keyPressEvent(QKeyEvent *ev);
196
197         void do_click(QPoint const &pos);
198         void do_click_in_link_mode(QPoint const &pos);
199
200         void do_key_press_in_link_mode(QKeyEvent *ev);
201
202 private:
203         SourcePanel *m_source_panel;
204         bool m_text_dirty; /*!< 内容の変更を示すフラグ */
205         bool m_text_saved; /*!< データベース登録の必要性を示すフラグ */
206 };
207
208 class Editor : public TextWidget
209 {
210         Q_OBJECT
211 public:
212         Editor(Settings *settings, Service *service, QWidget *parent);
213
214         void clear();
215         void set_segment(TextSegment::pointer segment);
216
217         bool edit_mode() const;
218         void set_edit_mode(bool mode_);
219         bool link_mode() const;
220         void set_link_mode(bool mode_);
221
222         bool can_link_mode() const;
223
224         EditorWidget* parent_editor_widget();
225 private:
226         TargetPanel* current_target_panel();
227         TargetPanel const* current_target_panel() const;
228
229 protected slots:
230         void onFocusInChild(TextPanel *new_, TextPanel *old_);
231
232 protected:
233         SourcePanel* find_source_panel(TextPanel *panel);
234
235         void do_panel_enter(SourcePanel *panel);
236         void do_panel_leave(SourcePanel *panel);
237         void do_link_mode_enter(SourcePanel *panel);
238         void do_link_mode_leave(SourcePanel *panel);
239
240 private:
241         void divide_target_sentence(SourcePanel *source_panel);
242
243 private:
244         Settings *m_settings;
245         Service *m_service;
246
247         int m_segment_id;
248         bool m_edit_mode; /*!< 編集モードのときtrue */
249         bool m_link_mode; /*!< リンクモードのときtrue */
250
251         SourcePanel *m_current_source_panel;
252 };
253
254 } // namespace TM
255
256 #endif // TMWIDGET_H