⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 msgedit.cpp

📁 QT 开发环境里面一个很重要的文件
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    if (!((w > 0) && (h > 0)))        return;    if (p.begin(this)) {        p.setPen(palette().color(QPalette::Shadow));        p.drawPoint(w + 5, 6);        p.drawLine(w + 3, 6, w + 5, 8);        p.drawLine(w + 1, 6, w + 5, 10);        int i;        for (i=7; i < h; i += 2)            p.drawLine( w, i, w + 5, i + 5);        for (i = w - i + h; i > 6; i -= 2)            p.drawLine( i, h, i + 5, h + 5);        for (; i > 0 ; i -= 2)            p.drawLine( 6, h + 6 - i, i + 5, h + 5);        p.end();    }    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(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);    connect(srcText->document(), SIGNAL(contentsChanged()), SLOT(handleSourceChanges()));    connect(srcText, SIGNAL(selectionChanged()),             SLOT(sourceSelectionChanged()));    cmtText = new QTextEdit(this);    cmtText->setObjectName("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()));    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();    layoutWidgets();}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("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();}/*   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 (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();    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::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, QMainWindow *parent)    : QScrollArea(parent->centralWidget()), m_contextModel(model){    cutAvail = false;    copyAvail = false;    doGuesses = true;    canPaste = false;    bottomDockWnd = new QDockWidget(parent);    bottomDockWnd->setObjectName("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("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();    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);    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("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();    // Signals    connect(editorPage->pageCurl, SIGNAL(nextPage()),        SIGNAL(nextUnfinished()));    connect(editorPage->pageCurl, SIGNAL(prevPage()),        SIGNAL(prevUnfinished()));    connect(this, SIGNAL(translationChanged(const QStringList &)),        this, SLOT(updateButtons()));    connect(this, SIGNAL(translationChanged(const QStringList &)),        this, SLOT(checkUndoRedo()));    connect(editorPage, SIGNAL(currentTranslationEditorChanged()),        this, SLOT(checkUndoRedo()));    connect(editorPage, SIGNAL(selectionChanged()),        this, SLOT(updateCutAndCopy()));    connect(qApp->clipboard(), SIGNAL(dataChanged()),        this, SLOT(updateCanPaste()));

⌨️ 快捷键说明

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