📄 msgedit.cpp
字号:
connect(phraseTv, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(insertPhraseInTranslation(QModelIndex))); phraseTv->installEventFilter(this); // What's this this->setWhatsThis(tr("This whole panel allows you to view and edit " "the translation of some source text.") ); editorPage->srcText->setWhatsThis(tr("This area shows the source text.") ); editorPage->cmtText->setWhatsThis(tr("This area shows a comment that" " may guide you, and the context in which the text" " occurs.") ); showNothing();}void MessageEditor::checkUndoRedo(){ QTextEdit *te = editorPage->activeTransText(); undoAvailable(te->document()->isUndoAvailable()); redoAvailable(te->document()->isRedoAvailable());}bool MessageEditor::eventFilter(QObject *o, QEvent *e){ // handle copying from the source if ((e->type() == QEvent::KeyPress) || (e->type() == QEvent::ShortcutOverride)) { QKeyEvent *ke = static_cast<QKeyEvent *>(e); // handle return key in phrase list if (o == phraseTv && e->type() == QEvent::KeyPress && ke->modifiers() == Qt::NoModifier && ke->key() == Qt::Key_Return && phraseTv->currentIndex().isValid()) { insertPhraseInTranslationAndLeave(phraseTv->currentIndex()); return false; } if (ke->modifiers() & Qt::ControlModifier) { if ((ke->key() == Qt::Key_A) && editorPage->srcText->underMouse()) { editorPage->srcText->selectAll(); return true; } if ((ke->key() == Qt::Key_C) && editorPage->srcText->textCursor().hasSelection()) { editorPage->srcText->copySelection(); return true; } } } return QScrollArea::eventFilter(o, e);}void MessageEditor::updatePageHeight(int height){ sw->resize(sw->width(), height + sw->margin() + sw->shadowWidth());}void MessageEditor::resizeEvent(QResizeEvent *e){ sw->resize(viewport()->width(), sw->height()); QScrollArea::resizeEvent(e);}QTreeView *MessageEditor::phraseView() const{ return phraseTv;}void MessageEditor::showNothing(){ editorPage->srcText->clear(); setEditionEnabled(false); sourceText.clear(); editorPage->cmtText->clear(); setTranslation(QString(), 0, false); editorPage->handleSourceChanges(); editorPage->handleCommentChanges(); editorPage->adjustTranslationFieldHeights(); editorPage->updateCommentField();}static CandidateList similarTextHeuristicCandidates( MessageModel::iterator it, const char *text, int maxCandidates ){ QList<int> scores; CandidateList candidates; StringSimilarityMatcher stringmatcher(QString::fromLatin1(text)); for (MessageItem *m = 0; (m = it.current()); ++it) { MetaTranslatorMessage mtm = m->message(); if ( mtm.type() == MetaTranslatorMessage::Unfinished || mtm.translation().isEmpty() ) continue; QString s = m->sourceText(); int score = stringmatcher.getSimilarityScore(s); if ( (int) candidates.count() == maxCandidates && score > scores[maxCandidates - 1] ) candidates.removeAt( candidates.size()-1 ); if ( (int) candidates.count() < maxCandidates && score >= textSimilarityThreshold ) { Candidate cand( s, mtm.translation() ); int i; for ( i = 0; i < (int) candidates.size(); i++ ) { if ( score >= scores.at(i) ) { if ( score == scores.at(i) ) { if ( candidates.at(i) == cand ) goto continue_outer_loop; } else { break; } } } scores.insert( i, score ); candidates.insert( i, cand ); } continue_outer_loop: ; } return candidates;}void MessageEditor::showMessage(const QString &text, const QString &comment, const QString &fullContext, const QStringList &translations, MetaTranslatorMessage::Type type, const QList<Phrase> &phrases){ phraseTv->clearSelection(); bool obsolete = (type == MetaTranslatorMessage::Obsolete); setEditionEnabled(!obsolete); sourceText = text; visualizeBackTabs(text, editorPage->srcText); if (!fullContext.isEmpty() && !comment.isEmpty()) visualizeBackTabs(fullContext.simplified() + "\n" + comment.simplified(), editorPage->cmtText); else if (!fullContext.isEmpty() && comment.isEmpty()) visualizeBackTabs(fullContext.simplified(), editorPage->cmtText); else if (fullContext.isEmpty() && !comment.isEmpty()) visualizeBackTabs(comment.simplified(), editorPage->cmtText); else editorPage->cmtText->clear(); editorPage->m_pluralEditMode = translations.count() > 1; if (!editorPage->m_pluralEditMode) { editorPage->m_transTexts[0]->setLabel(editorPage->m_invariantForm); } else { editorPage->m_transTexts[0]->setLabel(tr("Translation (%1)").arg(editorPage->m_numerusForms.first())); } int i; for (i = 0; i < qMax(1, editorPage->m_numerusForms.count()); ++i) { bool shouldShow = i < translations.count(); if (shouldShow) { setTranslation(translations[i], i, false); } else { setTranslation(QString(), i, false); } if (i >= 1) { editorPage->m_transTexts[i]->setVisible(shouldShow); } } phrMdl->removePhrases(); foreach(Phrase p, phrases) { phrMdl->addPhrase(p); } if (doGuesses && !sourceText.isEmpty()) { CandidateList cl = similarTextHeuristicCandidates(m_contextModel->begin(), sourceText.toLatin1(), MaxCandidates); int n = 0; QList<Candidate>::Iterator it = cl.begin(); while (it != cl.end()) { QString def; if (n < 9) def = tr("Guess (%1)").arg(QString(QKeySequence(Qt::CTRL | (Qt::Key_0 + (n + 1))))); else def = tr("Guess"); phrMdl->addPhrase(Phrase((*it).source, (*it).target, def, n)); ++n; ++it; } } phrMdl->resort(); editorPage->handleSourceChanges(); editorPage->handleCommentChanges(); editorPage->adjustTranslationFieldHeights(); editorPage->updateCommentField(); setEditorFocus();}void MessageEditor::setNumerusForms(const QString &invariantForm, const QStringList &numerusForms){ // uninstall the emitTranslationChanged slots and remove event filters for (int i = 0; i < editorPage->m_transTexts.count(); ++i) { QTextEdit *transText = editorPage->m_transTexts[i]->editor(); disconnect( transText->document(), SIGNAL(contentsChanged()), this, SLOT(emitTranslationChanged()) ); transText->removeEventFilter(this); } editorPage->setNumerusForms(invariantForm, numerusForms); // reinstall event filters and set up the emitTranslationChanged slot for (int i = 0; i < editorPage->m_transTexts.count(); ++i) { QTextEdit *transText = editorPage->m_transTexts[i]->editor(); transText->installEventFilter(this); connect(transText->document(), SIGNAL(contentsChanged()), this, SLOT(emitTranslationChanged())); // What's this transText->setWhatsThis(tr("This is where you can enter or modify" " the translation of some source text.") ); }}static void visualizeImages(const QString &text, QTextEdit *te){ te->clear(); QTextCursor tc(te->textCursor()); QString plainText; for (int i = 0; i < (int) text.length(); ++i) { int ch = text[i].unicode(); if (ch < 0x20) { if (!plainText.isEmpty()) { tc.insertText(plainText); plainText.clear(); } const char *p = strchr(MessageEditor::backTab, ch); if (p) { if (backTabImages[p - MessageEditor::backTab]) { tc.insertImage(QLatin1String(backTabImages[p - MessageEditor::backTab])); } if (MessageEditor::backTab[p - MessageEditor::backTab] == '\n') { tc.insertBlock(); } } } else { plainText += QString(ch); } } tc.insertText(plainText);}void MessageEditor::setTranslation(const QString &translation, int numerus, bool emitt){ if (numerus >= editorPage->m_transTexts.count()) numerus = 0; QTextEdit *transText = editorPage->m_transTexts[numerus]->editor(); // Block signals so that a signal is not emitted when // for example a new source text item is selected and *not* // the actual translation. if (!emitt) transText->document()->blockSignals(true); if (translation.isNull()) transText->clear(); else visualizeImages(translation, transText); if (!emitt) { transText->document()->blockSignals(false); //don't undo the change emit undoAvailable(false); emit redoAvailable(false); updateButtons(); } emit cutAvailable(false); emit copyAvailable(false);}void MessageEditor::setEditionEnabled(bool enabled){ for (int i = 0; i < editorPage->m_transTexts.count(); ++i) { TransEditor* te = editorPage->m_transTexts[i]; te->label()->setEnabled(enabled); te->editor()->setReadOnly(!enabled); } phraseLbl->setEnabled(enabled); phraseTv->setEnabled(enabled); updateCanPaste();}void MessageEditor::undo(){ editorPage->activeTransText()->document()->undo();}void MessageEditor::redo(){ editorPage->activeTransText()->document()->redo();}void MessageEditor::cut(){ if (editorPage->activeTransText()->textCursor().hasSelection()) editorPage->activeTransText()->cut();}void MessageEditor::copy(){ if (editorPage->srcText->textCursor().hasSelection()) { editorPage->srcText->copySelection(); } else if (editorPage->activeTransText()->textCursor().hasSelection()) { editorPage->activeTransText()->copy(); }}void MessageEditor::paste(){ editorPage->activeTransText()->paste();}void MessageEditor::selectAll(){ // make sure we don't select the selection of a translator textedit, if we really want the // source text editor to be selected. if (!editorPage->srcText->underMouse()) editorPage->activeTransText()->selectAll();}void MessageEditor::emitTranslationChanged(){ emit translationChanged( editorPage->translations());}void MessageEditor::guessActivated(int key){ QModelIndex mi; Phrase p; for (int i=0; i<phrMdl->phraseList().count(); ++i) { mi = phrMdl->QAbstractTableModel::index(i, 0); p = phrMdl->phrase(mi); if (p.shortcut() == key) { insertPhraseInTranslation(mi); break; } }}void MessageEditor::insertPhraseInTranslation(const QModelIndex &index){ if (!editorPage->activeTransText()->isReadOnly()) { editorPage->activeTransText()->textCursor().insertText(phrMdl->phrase(index).target()); emitTranslationChanged(); }}void MessageEditor::insertPhraseInTranslationAndLeave(const QModelIndex &index){ if (!editorPage->activeTransText()->isReadOnly()) { editorPage->activeTransText()->textCursor().insertText(phrMdl->phrase(index).target()); emitTranslationChanged(); editorPage->activeTransText()->setFocus(); }}void MessageEditor::updateButtons(){ bool overwrite = (!editorPage->activeTransText()->isReadOnly() && (editorPage->activeTransText()->toPlainText().trimmed().isEmpty() || mayOverwriteTranslation)); mayOverwriteTranslation = false; emit updateActions(overwrite);}void MessageEditor::beginFromSource(){ mayOverwriteTranslation = true; for ( int i = 0; i < editorPage->m_transTexts.count(); ++i) { setTranslation(sourceText, i, true); } setEditorFocus();}void MessageEditor::setEditorFocus(){ if (!editorPage->hasFocus()) editorPage->activeTransText()->setFocus();}void MessageEditor::updateCutAndCopy(){ bool newCopyState = false; bool newCutState = false; if (editorPage->srcText->textCursor().hasSelection()) { newCopyState = true; } else if (editorPage->activeTransText()->textCursor().hasSelection()) { newCopyState = true; newCutState = true; } if (newCopyState != copyAvail) { copyAvail = newCopyState; emit copyAvailable(copyAvail); } if (newCutState != cutAvail) { cutAvail = newCutState; emit cutAvailable(cutAvail); }}void MessageEditor::updateCanPaste(){ bool oldCanPaste = canPaste; canPaste = (!editorPage->activeTransText()->isReadOnly() && !qApp->clipboard()->text().isNull()); if (canPaste != oldCanPaste) emit pasteAvailable(canPaste);}void MessageEditor::toggleGuessing(){ doGuesses = !doGuesses; if (!doGuesses) { phrMdl->removePhrases(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -