OSDN Git Service

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