1 #include "tmeditorwidget.h"
12 #include <QDesktopServices>
14 #include <QVBoxLayout>
17 #include <QMutexLocker>
20 #include <QTextDocument>
21 #include <QTextCursor>
23 #include <QTextBlockFormat>
35 TM::EditorWidget::EditorWidget(Settings *settings, Service *service, QWidget *parent)
37 , m_mutex(QMutex::Recursive)
39 , m_settings(settings)
43 QVBoxLayout *vlayout = new QVBoxLayout(this);
44 vlayout->setSpacing(4);
45 vlayout->setContentsMargins(0, 0, 0, 0);
47 m_toolbar = new QToolBar("Editor", this);
49 m_edit_mode = new QAction(QIcon(":/edit.png"), "edit", this);
50 m_edit_mode->setCheckable(true);
51 m_toolbar->addAction(m_edit_mode);
52 connect(m_edit_mode, SIGNAL(triggered(bool)), this, SLOT(onEditModeTriggered(bool)));
54 m_link = new QAction(QIcon(":/link.png"), "link", this);
55 m_link->setCheckable(true);
56 m_link->setDisabled(true);
57 m_toolbar->addAction(m_link);
58 connect(m_link, SIGNAL(triggered(bool)), this, SLOT(onLinkModeTriggered(bool)));
60 m_slang = new QAction("&Source", this);
61 m_slang->setDisabled(true);
62 m_slang->setMenu(new QMenu(this));
63 m_tlang = new QAction("&Target", this);
64 m_tlang->setDisabled(true);
65 m_tlang->setMenu(new QMenu(this));
66 m_toolbar->addAction(m_slang);
67 m_toolbar->addAction(m_tlang);
69 QAction *browser = new QAction(QIcon(":/browser.png"), "browser", this);
70 m_toolbar->addAction(browser);
71 connect(browser, SIGNAL(triggered(bool)), this, SLOT(onBrowserTriggered(bool)));
73 QAction *setting = new QAction(QIcon(":/setting.png"), "setting", this);
74 m_toolbar->addAction(setting);
76 m_edit = new Editor(settings, service, this);
77 vlayout->addWidget(m_toolbar);
78 vlayout->addWidget(m_edit);
80 connect(m_service, SIGNAL(languageLoaded(int, QString, QIcon)), this, SLOT(onLanguageLoaded(int,QString,QIcon)));
84 * \brief ブラウザ上でドキュメントがフォーカスを取得したとき、
85 * SocketConnectionから呼び出されます。
87 * SocketConnectionは別スレッドに在ります。
89 void TM::EditorWidget::attach(SocketConnection *socket)
91 if(m_socket == socket) return;
92 if(m_socket) detach(m_socket);
94 QMutexLocker lock(&m_mutex);
96 connect(this, SIGNAL(editModeChanged(bool)), m_socket, SLOT(changeEditMode(bool)));
97 set_link_mode_disabled(true);
101 * \brief ブラウザ上でドキュメントがフォーカスを取得することで、
102 * 現在のSocketConnectionがフォーカスを失った時に呼び出されます。
104 * このメンバはattach()から呼び出されますが、その呼び出し元は別スレッドに在ります。
106 void TM::EditorWidget::detach(SocketConnection *)
108 QMutexLocker lock(&m_mutex);
109 disconnect(this, SIGNAL(editModeChanged(bool)), m_socket, SLOT(changeEditMode(bool)));
115 void TM::EditorWidget::set_http_port(quint16 http_port) { m_http_port = http_port; }
118 * \brief 編集モードを変更します。
120 void TM::EditorWidget::set_edit_mode(bool mode)
122 QMutexLocker lock(&m_mutex);
123 bool old_mode = m_edit_mode->isChecked();
124 if(old_mode == mode) return;
126 m_edit_mode->setChecked(mode);
130 * \brief 編集モードの場合trueを返します。
132 bool TM::EditorWidget::edit_mode()
134 QMutexLocker lock(&m_mutex);
135 return m_edit_mode->isChecked();
139 * \brief リンクモードを設定します。
141 void TM::EditorWidget::set_link_mode(bool mode)
143 QMutexLocker lock(&m_mutex);
144 bool old_mode = m_link->isChecked();
145 if(old_mode == mode) return;
147 m_link->setChecked(mode);
151 * \brief リンクモードの場合、trueを返します。
153 bool TM::EditorWidget::link_mode()
155 QMutexLocker lock(&m_mutex);
156 return m_link->isChecked();
160 * \brief リンクボタンをグレーアウトします。
164 void TM::EditorWidget::set_link_mode_disabled(bool disable)
166 QMutexLocker lock(&m_mutex);
167 m_link->setDisabled(disable);
171 * \brief 原文の言語コードを返します。
173 int TM::EditorWidget::source_language()
175 QMutexLocker lock(&m_mutex);
176 return m_slang->data().toInt();
180 * \brief 訳文の言語コードを返します。
182 int TM::EditorWidget::target_language()
184 QMutexLocker lock(&m_mutex);
185 return m_tlang->data().toInt();
188 void TM::EditorWidget::set_string(QString source_, QString target_)
190 QMutexLocker lock(&m_mutex);
191 int code = m_slang->data().toInt();
193 Text::pointer result = Text::create();
194 Text::pointer sentences = m_service->divide_into_sentences(code, source_);
195 for(Text::pointer s = sentences->begin(); s; s = s->next())
197 Text::pointer words = m_service->divide_into_words(code, s);
198 result->append(words);
200 m_edit->set_sentences(result);
204 * \brief 言語プラグインが読み込まれるたびに呼び出されます。
207 * \param icon 言語を表すアイコン。
209 void TM::EditorWidget::onLanguageLoaded(int code, QString name, QIcon icon)
211 QString dslanguage = m_settings->value("Widget/defaultSourceLanguage", "English").toString();
212 QString tslanguage = m_settings->value("Widget/defaultSourceLanguage", "Japanese").toString();
213 if(dslanguage == name)
215 m_slang->setIcon(icon);
216 m_slang->setData(code);
218 if(tslanguage == name)
220 m_tlang->setIcon(icon);
221 m_tlang->setData(code);
223 QAction *saction = m_slang->menu()->addAction(icon, name);
224 saction->setData(code);
225 connect(saction, SIGNAL(triggered(bool)), this, SLOT(onSourceLanguageTriggered(bool)));
226 QAction *taction = m_tlang->menu()->addAction(icon, name);
227 taction->setData(code);
228 connect(taction, SIGNAL(triggered(bool)), this, SLOT(onTargetLanguageTriggered(bool)));
232 * \brief 編集アイコンがクリックされたとき呼び出されます。
234 void TM::EditorWidget::onEditModeTriggered(bool)
236 QAction *edit_mode = qobject_cast<QAction*>(sender());
237 bool checked = edit_mode->isChecked();
238 m_link->setDisabled(true);
239 m_slang->setDisabled(!checked);
240 m_tlang->setDisabled(!checked);
241 emit editModeChanged(checked);
243 m_edit->set_edit_mode(checked);
247 * \brief リンクアイコンがクリックされたとき呼び出されます。
249 void TM::EditorWidget::onLinkModeTriggered(bool checked)
251 //QAction *link_mode = qobject_cast<QAction*>(sender());
252 m_edit->set_link_mode(checked);
256 * \brief アクションから原文言語が変更されたとき呼び出されます。
258 void TM::EditorWidget::onSourceLanguageTriggered(bool)
260 QAction *saction = qobject_cast<QAction*>(sender());
262 m_slang->setIcon(saction->icon());
263 m_slang->setData(saction->data());
267 * \brief アクションから訳文言語が変更されたとき呼び出されます。
269 void TM::EditorWidget::onTargetLanguageTriggered(bool)
271 QAction *taction = qobject_cast<QAction*>(sender());
273 m_tlang->setIcon(taction->icon());
274 m_tlang->setData(taction->data());
278 * \brief ブラウザ・アイコンがクリックされたとき呼び出されます。
280 void TM::EditorWidget::onBrowserTriggered(bool)
282 QString url("http://localhost:");
283 url += QString::number(m_http_port) + "/";
285 QDesktopServices::openUrl(QUrl(url));
288 // EditorPanel ----------------------------------------------------------------
290 TM::EditorPanel::EditorPanel(QWidget *parent)
297 * \brief 親となるエディタを設定します。
299 void TM::EditorPanel::set_editor(Editor *editor)
306 * \brief 保持している文を返します。
308 Text::pointer TM::EditorPanel::sentence() { return m_sentence; }
313 void TM::EditorPanel::set_sentence(Text::pointer sentence)
316 m_sentence = sentence;
318 QTextCursor c = textCursor();
319 for(Text::pointer p = sentence->begin(); p; p = p->next()) // p=word
322 QVariant v = QVariant::fromValue(Text::weak_pointer(p));
323 cf.setProperty(Word, v);
324 c.insertText(p->string(), cf);
329 * \brief 内容が空の場合、trueを返します。
331 bool TM::EditorPanel::is_empty() const { return document()->isEmpty(); }
334 * \brief 引数として与えられた単語に該当する範囲を選択するカーソルを返します。
336 QTextCursor TM::EditorPanel::select_cursor(Text::pointer word)
338 RangeData *rd = static_cast<RangeData*>(word->data().get());
340 int slength = word->string().length();
341 QTextCursor c = textCursor();
342 c.movePosition(QTextCursor::Start);
343 c.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, rd->begin());
344 c.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, slength);
349 * \brief 引数として与えられた位置にある単語を返します。
351 Text::pointer TM::EditorPanel::select_word(QPoint const &pos)
353 QTextCursor c = cursorForPosition(pos);
355 QVariant v = c.charFormat().property(Word);
356 if(!v.isValid()) return Text::pointer();
357 Text::weak_pointer wp = v.value<Text::weak_pointer>();
358 assert(!wp.expired());
359 Text::pointer p = wp.lock();
364 * \brief wordの範囲に背景色colorを設定します。
366 * 透過色は、Qt::transparentです。
368 void TM::EditorPanel::highlight(Text::pointer word, QColor color)
370 QTextCursor c = select_cursor(word);
371 QTextCharFormat cf = c.charFormat();
372 cf.setBackground(color);
373 c.mergeCharFormat(cf);
376 void TM::EditorPanel::highlight(WordLink::storage_type *link, QColor color)
378 for(Text::pointer p : *link) highlight(p, color);
381 void TM::EditorPanel::clear_highlight()
383 if(m_sentence) set_sentence(m_sentence);
387 * \brief 引数として与えられた整数値から色を作成します。
389 * 色分け用に使えるQColorを返します。
391 QColor TM::EditorPanel::color(int index) const
394 result.setHsv(index * 199, 128, 128, 128);
398 // SourcePanel ----------------------------------------------------------------
400 TM::SourcePanel::SourcePanel(QWidget *parent)
401 : EditorPanel(parent)
402 , m_target_panel(nullptr)
404 setUndoRedoEnabled(false);
405 setContextMenuPolicy(Qt::NoContextMenu);
408 TM::TargetPanel* TM::SourcePanel::target_panel() { return m_target_panel; }
410 void TM::SourcePanel::set_target_panel(TargetPanel *target)
412 m_target_panel = target;
415 void TM::SourcePanel::commit_link()
419 m_target_panel->clear_highlight();
422 TM::WordLinker* TM::SourcePanel::linker()
428 * \brief 保持するリンクを色分け表示します。
430 void TM::SourcePanel::ensure_highlight()
433 m_target_panel->clear_highlight();
435 WordLink::pointer wl = m_linker.current();
438 highlight(wl->sources(), Qt::cyan);
439 target_panel()->highlight(wl->targets(), Qt::cyan);
442 for(WordLink::pointer wl : m_linker)
444 int index = m_linker.index_of(wl);
445 QColor c = color(index);
446 highlight(wl->sources(), c);
447 target_panel()->highlight(wl->targets(), c);
451 bool TM::SourcePanel::canInsertFromMimeData(const QMimeData *) const
453 return false; // ドロップ禁止カーソルにする
456 void TM::SourcePanel::insertFromMimeData(const QMimeData *) { } // ドロップされても何もしない
458 void TM::SourcePanel::inputMethodEvent(QInputMethodEvent *ev)
460 ev->setCommitString(""); // 入力禁止
461 QPlainTextEdit::inputMethodEvent(ev);
464 void TM::SourcePanel::keyPressEvent(QKeyEvent *ev)
471 if(m_editor->link_mode()) m_editor->set_link_mode(false);
474 if(!ev->modifiers().testFlag(Qt::ControlModifier)) break;
479 TextPanel::keyPressEvent(ev);
484 void TM::SourcePanel::do_click(QPoint const &pos)
486 if(m_editor->link_mode()) do_click_in_link_mode(pos);
489 void TM::SourcePanel::do_click_in_link_mode(QPoint const &pos)
491 Text::pointer w = select_word(pos);
494 m_linker.toggle(WordLink::Source, w);
496 setTextCursor(cursorForPosition(pos));
499 void TM::SourcePanel::do_focusin()
501 //if(m_target_panel) m_target_panel->show();
504 void TM::SourcePanel::do_focusout()
508 // TargetPanel ----------------------------------------------------------------
510 TM::TargetPanel::TargetPanel(QWidget *parent)
511 : EditorPanel(parent)
512 , m_source_panel(nullptr)
513 , m_text_dirty(false)
516 //setContextMenuPolicy(Qt::NoContextMenu);
519 TM::SourcePanel* TM::TargetPanel::source_panel() { return m_source_panel; }
521 void TM::TargetPanel::set_source_panel(SourcePanel *source)
523 m_source_panel = source;
526 void TM::TargetPanel::set_sentence(Text::pointer sentence)
528 EditorPanel::set_sentence(sentence);
529 set_text_dirty(false);
530 set_text_saved(false);
534 * \brief データベースへの登録が必要な場合、falseを返します。
536 * パネル内の文字列が編集された場合、trueを返します。
537 * 原文パネルがフォーカスを失うと、エディタがこのメンバを呼び出し、
538 * データベース登録の必要性を判定します。
539 * その後、エディタはデータベース登録を行い、フラグをクリアするので、
540 * 次に文字列が編集されるまでtrueを返し続けます。
542 bool TM::TargetPanel::is_text_saved() const { return m_text_saved; }
544 void TM::TargetPanel::set_text_saved(bool saved)
546 m_text_saved = saved;
550 * \brief パネル内の文字列が編集された場合、trueを返します。
552 * エディタがリンクモードに入るとき、このメンバを呼び出し、
553 * 単語へ分割する必要があるか判定します。
554 * エディタはパネルの文字列を単語に分割した後、set_sentence()によって
555 * パネルに文を設定するため、次に文字列を編集するまでfalseを返すようになります。
557 bool TM::TargetPanel::is_text_dirty() const { return m_text_dirty; }
560 * \brief パネルの文字列が編集されたかを示すフラグを設定します。
562 void TM::TargetPanel::set_text_dirty(bool dirty)
564 m_text_dirty = dirty;
565 if(dirty) m_editor->parent_editor_widget()->set_link_mode_disabled(false);
568 bool TM::TargetPanel::canInsertFromMimeData(QMimeData const *source) const
570 if(source->hasText()) return true;
574 void TM::TargetPanel::insertFromMimeData(QMimeData const *source)
576 TextPanel::insertFromMimeData(source);
577 set_text_dirty(true);
578 set_text_saved(false);
581 void TM::TargetPanel::inputMethodEvent(QInputMethodEvent *ev)
583 if(m_editor->link_mode()) ev->setCommitString("");
584 if(!ev->commitString().isEmpty())
586 set_text_dirty(true);
587 set_text_saved(false);
589 QPlainTextEdit::inputMethodEvent(ev);
592 void TM::TargetPanel::keyPressEvent(QKeyEvent *ev)
594 if(m_editor->link_mode()) do_key_press_in_link_mode(ev);
597 TextPanel::keyPressEvent(ev);
598 if(!ev->text().isEmpty())
600 set_text_dirty(true);
601 set_text_saved(false);
606 void TM::TargetPanel::do_click(QPoint const &pos)
608 if(m_editor->link_mode()) do_click_in_link_mode(pos);
611 void TM::TargetPanel::do_click_in_link_mode(QPoint const &pos)
613 Text::pointer w = select_word(pos);
616 WordLinker *wl = m_source_panel->linker();
617 wl->toggle(WordLink::Target, w);
618 m_source_panel->ensure_highlight();
619 setTextCursor(cursorForPosition(pos));
622 void TM::TargetPanel::do_focusin()
627 void TM::TargetPanel::do_focusout()
632 void TM::TargetPanel::do_key_press_in_link_mode(QKeyEvent *ev)
639 m_editor->set_link_mode(false);
645 TextPanel::keyPressEvent(ev);
650 // Editor ---------------------------------------------------------------------
652 TM::Editor::Editor(Settings *settings, Service *service, QWidget *parent)
654 , m_settings(settings)
658 , m_current_source_panel(nullptr)
660 TextArea *ta = text_area();
661 connect(ta, SIGNAL(focusInChild(TextPanel*,TextPanel*)), this, SLOT(onFocusInChild(TextPanel*,TextPanel*)));
665 * \brief エディタが保持しているリソースを適切に解放します。
667 void TM::Editor::clear()
669 if(!m_sentences) return; // 文を保持していない場合、何もしない。
671 m_sentences.reset(); // 保持している文のメモリーを開放する。
673 if(m_current_source_panel) do_panel_leave(m_current_source_panel);
674 m_current_source_panel = nullptr;
676 TextArea *ta = text_area();
679 set_link_mode(false);
680 parent_editor_widget()->set_link_mode_disabled(true);
684 * \brief エディタに文を設定します。
688 void TM::Editor::set_sentences(Text::pointer sentences)
691 m_sentences = sentences;
692 TextArea *ta = text_area();
693 for(Text::pointer s = sentences->begin(); s; s = s->next()) // s: sentence
695 SourcePanel *sp = ta->append<SourcePanel>();
696 TargetPanel *tp = ta->append<TargetPanel>();
697 sp->set_target_panel(tp);
698 sp->set_editor(this);
699 tp->set_source_panel(sp);
700 tp->set_editor(this);
706 bool TM::Editor::edit_mode() const { return m_edit_mode; }
708 void TM::Editor::set_edit_mode(bool mode_) { m_edit_mode = mode_; }
710 bool TM::Editor::link_mode() const { return m_link_mode; }
712 void TM::Editor::set_link_mode(bool mode_)
714 if(mode_ == m_link_mode) return; // モードが変化していない場合、何もしない。
715 assert(!mode_ || can_link_mode()); // リンクモードに入るには、条件がある。
718 parent_editor_widget()->set_link_mode(mode_);
720 if(mode_) do_link_mode_enter(m_current_source_panel);
721 else if(m_current_source_panel) do_link_mode_leave(m_current_source_panel);
724 bool TM::Editor::can_link_mode() const
726 if(!m_current_source_panel) return false;
727 if(!edit_mode()) return false;
728 if(current_target_panel()->is_empty()) return false;
733 TM::EditorWidget* TM::Editor::parent_editor_widget()
735 EditorWidget* result = qobject_cast<EditorWidget*>(parentWidget());
740 TM::TargetPanel* TM::Editor::current_target_panel()
742 TargetPanel *result = nullptr;
743 if(m_current_source_panel) result = m_current_source_panel->target_panel();
747 TM::TargetPanel const* TM::Editor::current_target_panel() const
749 return const_cast<Editor*>(this)->current_target_panel();
752 void TM::Editor::onFocusInChild(TextPanel *new_, TextPanel *)
754 SourcePanel *old_panel = m_current_source_panel;
755 SourcePanel *new_panel = find_source_panel(new_);
758 if(old_panel && old_panel != new_panel) do_panel_leave(old_panel);
759 m_current_source_panel = new_panel;
760 if(old_panel != new_panel) do_panel_enter(new_panel);
763 TM::SourcePanel* TM::Editor::find_source_panel(TextPanel *panel)
765 SourcePanel *sp = qobject_cast<SourcePanel*>(panel);
767 TargetPanel *tp = qobject_cast<TargetPanel*>(panel);
768 if(tp) return tp->source_panel();
773 * \brief 文を表示するパネルにフォーカスが入るとき呼び出されます。
775 void TM::Editor::do_panel_enter(SourcePanel *panel)
777 TargetPanel *tp = panel->target_panel();
780 if(can_link_mode()) parent_editor_widget()->set_link_mode_disabled(false);
784 * \brief 文を表示するパネルからフォーカスが出るとき呼び出されます。
786 void TM::Editor::do_panel_leave(SourcePanel *panel)
788 TargetPanel *tp = panel->target_panel();
792 set_link_mode(false);
793 parent_editor_widget()->set_link_mode_disabled(true);
795 if(!tp->is_text_saved())
797 //qDebug() << m_service->si
798 tp->set_text_saved(true);
803 * \brief リンクモードに入るとき呼び出されます。
805 void TM::Editor::do_link_mode_enter(SourcePanel *panel)
808 TargetPanel *tp = panel->target_panel();
810 int tcode = parent_editor_widget()->target_language();
812 if(tp->is_text_dirty())
814 panel->linker()->clear();
815 QString string = tp->toPlainText();
816 Text::pointer sentences = m_service->divide_into_sentences(tcode, string);
817 if(sentences->size())
819 Text::pointer words = m_service->divide_into_words(tcode, sentences->begin());
820 tp->set_sentence(words); // ココで、text_dirtyがfalseになる。
824 panel->ensure_highlight();
828 * \brief リンクモードから出るとき呼び出されます。
830 void TM::Editor::do_link_mode_leave(SourcePanel *panel)
832 qDebug() << m_current_source_panel->linker()->to_json_array();
834 panel->commit_link();
835 //panel->clear_highlight();