msgedit.cpp

来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 1,444 行 · 第 1/3 页

CPP
1,444
字号
    QWidget::paintEvent(e);}/*   EditorPage class impl.   A frame that contains the source text, translated text and any   source code comments and hints.*/EditorPage::EditorPage(MessageEditor *parent, const char *name)    : QFrame(parent){    setObjectName(QLatin1String(name));    setLineWidth(1);    setFrameStyle(QFrame::Box | QFrame::Plain);    // Use white explicitly as the background color for the editor page.    QPalette p = palette();    p.setColor(QPalette::Active, QPalette::Base, QColor(Qt::white));    p.setColor(QPalette::Inactive, QPalette::Base, QColor(Qt::white));    p.setColor(QPalette::Disabled, QPalette::Base, QColor(Qt::white));    p.setColor(QPalette::Active, QPalette::Window,                p.color(QPalette::Active, QPalette::Base));    p.setColor(QPalette::Inactive, QPalette::Window,                p.color(QPalette::Inactive, QPalette::Base));    p.setColor(QPalette::Disabled, QPalette::Window,                p.color(QPalette::Disabled, QPalette::Base));    parent->setPalette(p);    srcTextLbl = new QLabel(tr("Source text"), this);    QFont fnt = font();    fnt.setBold(true);    srcTextLbl->setFont(fnt);    srcText = new SourceTextEdit(this);    srcText->setFrameStyle(QFrame::NoFrame);    srcText->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding,        QSizePolicy::Minimum));    srcText->setAutoFormatting(QTextEdit::AutoNone);    srcText->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);    srcText->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);    p = srcText->palette();    p.setColor(QPalette::Disabled, QPalette::Base, p.color(QPalette::Active, QPalette::Base));    srcText->setPalette( p );	srcText->setReadOnly(true);    srcText->setWhatsThis(tr("This area shows the source text.") );    connect(srcText->document(), SIGNAL(contentsChanged()), SLOT(handleSourceChanges()));    connect(srcText, SIGNAL(selectionChanged()),             SLOT(sourceSelectionChanged()));    altTextLbl = new QLabel(this);    altTextLbl->setText(tr("Alternative source text"));    altTextLbl->setFont(fnt);    altText = new SourceTextEdit(this);    altText->setFrameStyle(QFrame::NoFrame);    altText->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding,        QSizePolicy::Minimum));    altText->setAutoFormatting(QTextEdit::AutoNone);    altText->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);    altText->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);    altText->setWhatsThis(tr("This area shows text from an auxillary translation.") );    p = altText->palette();    p.setColor(QPalette::Disabled, QPalette::Base, p.color(QPalette::Active, QPalette::Base));    altText->setPalette( p );    altText->setReadOnly(true);    connect(altText->document(), SIGNAL(contentsChanged()), SLOT(handleAltSourceChanges()));    connect(altText, SIGNAL(selectionChanged()),             SLOT(sourceSelectionChanged()));    cmtText = new QTextEdit(this);    cmtText->setObjectName(QLatin1String("comment/context view"));    cmtText->setFrameStyle( QFrame::NoFrame );    cmtText->setSizePolicy( QSizePolicy( QSizePolicy::MinimumExpanding,                                         QSizePolicy::Minimum ) );    cmtText->setAutoFormatting(QTextEdit::AutoNone);    cmtText->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);    cmtText->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);    p = cmtText->palette();    p.setColor(QPalette::Active, QPalette::Base, QColor(236,245,255));    p.setColor(QPalette::Inactive, QPalette::Base, QColor(236,245,255));    cmtText->setPalette(p);	cmtText->setReadOnly(true);    connect(cmtText->document(), SIGNAL(contentsChanged()), SLOT(handleCommentChanges()));    cmtText->setWhatsThis(tr("This area shows a comment that"                        " may guide you, and the context in which the text"                        " occurs.") );    m_pluralEditMode = false;    addPluralForm(m_invariantForm);    pageCurl = new PageCurl(this);    // Focus    //setFocusPolicy(Qt::StrongFocus);    //parent->setFocusProxy(transText);    //transLbl->setFocusProxy(transText);    //srcTextLbl->setFocusProxy(transText);    //srcText->setFocusProxy(transText);    //cmtText->setFocusProxy(transText);    //setFocusProxy(transText);    updateCommentField();    updateAltSourceField();    layoutWidgets();}void EditorPage::showNothing(){    srcText->clear();    cmtText->clear();    altText->clear();    handleChanges();}void EditorPage::handleChanges(){    handleSourceChanges();    handleAltSourceChanges();    handleCommentChanges();    adjustTranslationFieldHeights();    updateCommentField();    updateAltSourceField();}void EditorPage::addPluralForm(const QString &label){    TransEditor *te = new TransEditor(this);    te->setLabel(label);    if (m_transTexts.count()) {        te->setVisible(false);    }    te->label()->adjustSize();        te->editor()->setObjectName(QLatin1String("translation editor"));    te->editor()->setFrameStyle(QFrame::NoFrame);    te->editor()->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding,                                             QSizePolicy::MinimumExpanding));    te->editor()->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);    te->editor()->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);    te->editor()->setAutoFormatting(QTextEdit::AutoNone);    te->editor()->setLineWrapMode(QTextEdit::WidgetWidth);    QPalette p = te->editor()->palette();    p.setColor(QPalette::Disabled, QPalette::Base, p.color(QPalette::Active, QPalette::Base));    te->editor()->setPalette(p);    connect(te, SIGNAL(heightUpdated(int)), this, SLOT(updateHeight(int)));    connect(te->editor(), SIGNAL(selectionChanged()),             SLOT(translationSelectionChanged()));    connect(te, SIGNAL(gotFocusIn(void)), this, SIGNAL(currentTranslationEditorChanged(void)));    m_transTexts << te;}int EditorPage::currentTranslationEditor(){    for (int i = 0; i < m_transTexts.count(); ++i) {        QTextEdit *te = m_transTexts[i]->editor();        if (te->hasFocus()) return i;    }    return -1;  //no focus}void EditorPage::updateHeight(int /*h*/){    layoutWidgets();}/*! internal    Returns all translations for an item.    The number of translations is dependent on if we have a plural form or not.    If we don't have a plural form, then this should only contain one item.    Otherwise it will contain the number of numerus forms for the particular language.*/QStringList EditorPage::translations() const{    QStringList translations;    for (int i = 0; i < m_transTexts.count() && m_transTexts.at(i)->isVisible(); ++i) {        translations << m_transTexts[i]->translation();    }    return translations;}/*   Don't show the comment field if there are no comments.*/void EditorPage::updateCommentField(){    if (cmtText->toPlainText().isEmpty())        cmtText->hide();    else        cmtText->show();    layoutWidgets();}/*   Don't show the auxiliary translation field if there is no translation.*/void EditorPage::updateAltSourceField(){    if (altText->toPlainText().isEmpty()) {        altTextLbl->hide();        altText->hide();    } else {        altText->show();        altTextLbl->show();    }    layoutWidgets();}/*   Handle the widget layout manually*/void EditorPage::layoutWidgets() {    int margin = 6;    int space  = 2;    int w = width();    pageCurl->move(width() - pageCurl->width(), 0);    QFontMetrics fm(srcTextLbl->font());    srcTextLbl->move(margin, margin);    srcTextLbl->resize(fm.width(srcTextLbl->text()), srcTextLbl->height());    srcText->move(margin, srcTextLbl->y() + srcTextLbl->height() + space);    srcText->resize(w - margin*2, srcText->height());    int ypos = srcText->y() + srcText->height() + space;    if (altText->isVisible()) {        QFontMetrics altfm(altTextLbl->font());        altTextLbl->move(margin, ypos);        altTextLbl->resize(altfm.width(altTextLbl->text()), altTextLbl->height());        altText->move(margin, altTextLbl->y() + altTextLbl->height() + space);        altText->resize(w - margin*2, altText->height());        ypos = altText->y() + altText->height() + space;    }    if (cmtText->isVisible()) {        cmtText->move(margin, ypos);        cmtText->resize(w - margin*2, cmtText->height());        ypos+=cmtText->height() + space;    }    for (int i = 0; i < m_transTexts.count(); ++i) {        TransEditor *te = m_transTexts[i];        te->resize(w - margin * 2, te->height());        te->move(margin, ypos);        if (te->isVisible()) {            ypos += te->height() + space;        }    }    int totHeight = ypos + margin - srcTextLbl->y();    if (height() != totHeight)        emit pageHeightUpdated(totHeight);}void EditorPage::resizeEvent(QResizeEvent *){    adjustTranslationFieldHeights();    handleSourceChanges();    handleAltSourceChanges();    handleCommentChanges();    layoutWidgets();}void EditorPage::adjustTranslationFieldHeights(){    for (int i = 0; i < m_transTexts.count(); ++i) {        m_transTexts[i]->calculateFieldHeight();    }    if (srcText->textCursor().hasSelection())        translationSelectionChanged();}void EditorPage::handleSourceChanges(){    calculateFieldHeight(srcText);}void EditorPage::handleAltSourceChanges(){    calculateFieldHeight(altText);}void EditorPage::handleCommentChanges(){    calculateFieldHeight(cmtText);}// makes sure only one of the textedits has a selectionvoid EditorPage::sourceSelectionChanged(){    for (int i = 0; i < m_transTexts.count(); ++i) {        QTextEdit *te = m_transTexts[i]->editor();        bool oldBlockState = te->blockSignals(true);        QTextCursor c = te->textCursor();        c.clearSelection();        te->setTextCursor(c);        te->blockSignals(oldBlockState);    }    emit selectionChanged();}void EditorPage::translationSelectionChanged(){    bool oldBlockState = srcText->blockSignals(true);    QTextCursor c = srcText->textCursor();    c.clearSelection();    srcText->setTextCursor(c);    srcText->blockSignals(oldBlockState);    // clear the selection for all except the sender    QTextEdit *te = qobject_cast<QTextEdit*>(sender());    for (int i = 0; i < m_transTexts.count(); ++i) {        QTextEdit *t = m_transTexts[i]->editor();        if (t != te) {            oldBlockState = t->blockSignals(true);            QTextCursor c = t->textCursor();            c.clearSelection();            t->setTextCursor(c);            t->blockSignals(oldBlockState);        }    }    emit selectionChanged();}int EditorPage::activeTranslationNumerus() const{    for (int i = 0; i < m_transTexts.count(); ++i) {        if (m_transTexts[i]->editor()->hasFocus()) {            return i;        }    }    //### hmmm.....    if (m_transTexts.count()) return 0;    return -1;}void EditorPage::setNumerusForms(const QString &invariantForm, const QStringList &numerusForms){    m_invariantForm = invariantForm;    m_numerusForms = numerusForms;    if (!m_pluralEditMode) {        m_transTexts[0]->setLabel(invariantForm);    } else {        m_transTexts[0]->setLabel(tr("Translation (%1)").arg(m_numerusForms[0]));    }    int i;    for (i = 1; i < m_numerusForms.count(); ++i) {        QString label = tr("Translation (%1)").arg(m_numerusForms[i]);        if (i >= m_transTexts.count()) {            addPluralForm(label);        } else {            m_transTexts[i]->setLabel(label);        }        m_transTexts[i]->setVisible(m_pluralEditMode);    }    for (int j = m_transTexts.count() - i; j > 0; --j) {        TransEditor *te = m_transTexts.takeLast();        delete te;        ++i;    }    layoutWidgets();}QTextEdit *EditorPage::activeTransText() const{    int numerus = activeTranslationNumerus();    if (numerus != -1) {        return m_transTexts[numerus]->editor();    }    return 0;}/*   Check if the translation text field is big enough to show all text   that has been entered. If it isn't, resize it.*/void EditorPage::calculateFieldHeight(QTextEdit *field){    int contentsHeight = qRound(field->document()->documentLayout()->documentSize().height());    if (contentsHeight != field->height()) {        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());    for (int i = 0; i < m_transTexts.count(); ++i) {        QLabel *transLbl = m_transTexts[i]->label();        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(MessageModel *model, MessageModel *altTranslatorModel, QMainWindow *parent)    : QScrollArea(parent->centralWidget()), m_contextModel(model), m_altTranslatorModel(altTranslatorModel){    altTranslator = m_altTranslatorModel->translator();    cutAvail = false;    copyAvail = false;    doGuesses = true;    bottomDockWnd = new QDockWidget(parent);    bottomDockWnd->setObjectName(QLatin1String("PhrasesDockwidget"));    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(QLatin1String("phrase list view"));    phrMdl = new PhraseModel(w);    phraseTv->setModel(phrMdl);    phraseTv->setAlternatingRowColors(true);    phraseTv->setSelectionBehavior(QAbstractItemView::SelectRows);    phraseTv->setSelectionMode(QAbstractItemView::SingleSelection);    phraseTv->setRootIsDecorated(false);    phraseTv->setItemsExpandable(false);    QPalette pal = phraseTv->palette();    phraseTv->setPalette(pal);    phraseTv->header()->setResizeMode(QHeaderView::Stretch);    phraseTv->header()->setClickable(true);    vl->addWidget(phraseLbl);    vl->addWidget(phraseTv);    int i;    for (i = 0; i < 9; ++i) {        (void) new GuessShortcut(i, this, SLOT(guessActivated(int)));    }    bottomDockWnd->setWidget(w);    parent->addDockWidget(Qt::BottomDockWidgetArea, bottomDockWnd);    setObjectName(QLatin1String("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));

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?