📄 msgedit.cpp
字号:
int oldHeight = field->height(); if(contentsHeight < 30) contentsHeight = 30; field->resize(field->width(), contentsHeight); emit pageHeightUpdated(height() + (field->height() - oldHeight)); }}void EditorPage::fontChange(const QFont &){ //keep the labels bold... QFont fnt = font(); fnt.setBold(true); QFontMetrics fm(fnt); srcTextLbl->setFont(fnt); srcTextLbl->resize(fm.width(srcTextLbl->text()), srcTextLbl->height()); transLbl->setFont(fnt); transLbl->resize(fm.width(transLbl->text()), transLbl->height()); update();}/* MessageEditor class impl. Handle layout of dock windows and the editor page.*/MessageEditor::MessageEditor(MetaTranslator *t, QMainWindow *parent) : QScrollArea(parent), tor(t){ cutAvail = true; copyAvail = true; doGuesses = true; canPaste = false; topDockWnd = new QDockWidget(parent); topDockWnd->setAllowedAreas(Qt::AllDockWidgetAreas); topDockWnd->setFeatures(QDockWidget::AllDockWidgetFeatures); topDockWnd->setWindowTitle(tr("Source text")); srcTextView = new QTreeView(topDockWnd); srcMdl = new MessageModel(topDockWnd); srcTextView->setModel(srcMdl); srcTextView->setAlternatingRowColors(true); srcTextView->setSelectionBehavior(QAbstractItemView::SelectRows); srcTextView->setSelectionMode(QAbstractItemView::SingleSelection); srcTextView->setRootIsDecorated(false); srcTextView->setUniformRowHeights(true); QPalette pal = srcTextView->palette(); pal.setColor(QPalette::AlternateBase, TREEVIEW_ODD_COLOR); srcTextView->setPalette(pal); QFontMetrics fm(font()); srcTextView->header()->setResizeMode(1, QHeaderView::Stretch); srcTextView->header()->resizeSection(0, fm.width(MessageModel::tr("Done")) + 20); srcTextView->header()->resizeSection(2, 300); srcTextView->header()->setClickable(true); topDockWnd->setWidget(srcTextView); parent->addDockWidget(Qt::TopDockWidgetArea, topDockWnd); bottomDockWnd = new QDockWidget(parent); bottomDockWnd->setAllowedAreas(Qt::AllDockWidgetAreas); bottomDockWnd->setFeatures(QDockWidget::AllDockWidgetFeatures); bottomDockWnd->setWindowTitle(tr("Phrases")); QWidget *w = new QWidget(bottomDockWnd); w->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum)); QVBoxLayout *vl = new QVBoxLayout(w); vl->setSpacing(6); phraseLbl = new QLabel( tr("Phrases and guesses:"), w ); phraseTv = new QTreeView(w); phraseTv->setObjectName("phrase list view"); phrMdl = new PhraseModel(w); phraseTv->setModel(phrMdl); phraseTv->setAlternatingRowColors(true); phraseTv->setSelectionBehavior(QAbstractItemView::SelectRows); phraseTv->setSelectionMode(QAbstractItemView::SingleSelection); phraseTv->setRootIsDecorated(false); pal = phraseTv->palette(); pal.setColor(QPalette::AlternateBase, TREEVIEW_ODD_COLOR); phraseTv->setPalette(pal); phraseTv->header()->setResizeMode(QHeaderView::Stretch); phraseTv->header()->setClickable(true); vl->addWidget(phraseLbl); vl->addWidget(phraseTv); for (int i = 0; i < 9; ++i) { (void) new GuessShortcut(i, this, SLOT(guessActivated(int))); } bottomDockWnd->setWidget(w); parent->addDockWidget(Qt::BottomDockWidgetArea, bottomDockWnd); setObjectName("scroll area"); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setFrameStyle(QFrame::NoFrame); editorPage = new EditorPage(this, "editor page"); connect(editorPage, SIGNAL(pageHeightUpdated(int)), SLOT(updatePageHeight(int))); sw = new ShadowWidget(editorPage, this); sw->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding)); sw->setMinimumSize(QSize(100, 150)); setWidget(sw); defFormat = editorPage->srcText->currentCharFormat(); editorPage->transText->installEventFilter(this); // Signals connect(editorPage->pageCurl, SIGNAL(nextPage()), SIGNAL(nextUnfinished())); connect(editorPage->pageCurl, SIGNAL(prevPage()), SIGNAL(prevUnfinished())); connect(editorPage->transText->document(), SIGNAL(contentsChanged()), this, SLOT(emitTranslationChanged())); connect(editorPage->transText->document(), SIGNAL(contentsChanged()), this, SLOT(updateButtons())); connect(editorPage->transText->document(), SIGNAL(undoAvailable(bool)), this, SIGNAL(undoAvailable(bool))); connect(editorPage->transText->document(), SIGNAL(redoAvailable(bool)), this, SIGNAL(redoAvailable(bool))); connect(editorPage, SIGNAL(selectionChanged()), this, SLOT(updateCutAndCopy())); connect(qApp->clipboard(), SIGNAL(dataChanged()), this, SLOT(updateCanPaste())); connect(phraseTv, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(insertPhraseInTranslation(QModelIndex))); phraseTv->installEventFilter(this); connect(srcTextView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), parent, SLOT(showNewCurrent(QModelIndex,QModelIndex))); // 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.") ); editorPage->transText->setWhatsThis(tr("This is where you can enter or modify" " the translation of some source text.") ); showNothing();}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 (o == editorPage->transText && 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::sourceTextView() const{ return srcTextView;}QTreeView *MessageEditor::phraseView() const{ return phraseTv;}void MessageEditor::showNothing(){ editorPage->srcText->clear(); setEditionEnabled(false); sourceText.clear(); editorPage->cmtText->clear(); setTranslation(QString(), false); editorPage->handleSourceChanges(); editorPage->handleCommentChanges(); editorPage->handleTranslationChanges(); editorPage->updateCommentField();}void MessageEditor::showMessage(const QString &text, const QString &comment, const QString &fullContext, const QString &translation, 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(); setTranslation(translation, false); phrMdl->removePhrases(); foreach(Phrase p, phrases) { phrMdl->addPhrase(p); } if (doGuesses && !sourceText.isEmpty()) { CandidateList cl = similarTextHeuristicCandidates(tor, 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->handleTranslationChanges(); editorPage->updateCommentField();}void MessageEditor::setTranslation(const QString &translation, bool emitt){ // 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) editorPage->transText->document()->blockSignals(true); if (translation.isNull()) editorPage->transText->clear(); else editorPage->transText->setPlainText(translation); if (!emitt) { editorPage->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){ editorPage->transLbl->setEnabled(enabled); editorPage->transText->setReadOnly(!enabled); phraseLbl->setEnabled(enabled); phraseTv->setEnabled(enabled); updateCanPaste();}void MessageEditor::undo(){ editorPage->transText->document()->undo();}void MessageEditor::redo(){ editorPage->transText->document()->redo();}void MessageEditor::cut(){ if (editorPage->transText->textCursor().hasSelection()) editorPage->transText->cut();}void MessageEditor::copy(){ if (editorPage->srcText->textCursor().hasSelection()) { editorPage->srcText->copySelection(); } else if (editorPage->transText->textCursor().hasSelection()) { editorPage->transText->copy(); }}void MessageEditor::paste(){ editorPage->transText->paste();}void MessageEditor::selectAll(){ editorPage->transText->selectAll();}void MessageEditor::emitTranslationChanged(){ emit translationChanged(editorPage->transText->toPlainText());}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->transText->isReadOnly()) { editorPage->transText->textCursor().insertText(phrMdl->phrase(index).target()); emit translationChanged(editorPage->transText->toPlainText()); }}void MessageEditor::insertPhraseInTranslationAndLeave(const QModelIndex &index){ if (!editorPage->transText->isReadOnly()) { editorPage->transText->textCursor().insertText(phrMdl->phrase(index).target()); emit translationChanged(editorPage->transText->toPlainText()); editorPage->transText->setFocus(); }}void MessageEditor::updateButtons(){ bool overwrite = (!editorPage->transText->isReadOnly() && (editorPage->transText->toPlainText().trimmed().isEmpty() || mayOverwriteTranslation)); mayOverwriteTranslation = false; emit updateActions(overwrite);}void MessageEditor::beginFromSource(){ mayOverwriteTranslation = true; setTranslation(sourceText, true); setEditorFocus();}void MessageEditor::setEditorFocus(){ if (!editorPage->hasFocus()) editorPage->setFocus();}void MessageEditor::updateCutAndCopy(){ bool newCopyState = false; bool newCutState = false; if (editorPage->srcText->textCursor().hasSelection()) { newCopyState = true; } else if (editorPage->transText->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->transText->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 + -