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

📄 msgedit.cpp

📁 qt-x11-opensource-src-4.1.4.tar.gz源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the Qt Linguist of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************//*  TRANSLATOR MsgEdit  This is the right panel of the main window.*/#include "msgedit.h"#include "trwindow.h"#include "simtexth.h"#include "messagemodel.h"#include "phrasemodel.h"#include <QMenu>#include <QAction>#include <QApplication>#include <QClipboard>#include <QLabel>#include <QLayout>#include <QTextEdit>#include <QPalette>#include <QString>#include <QPainter>#include <QHeaderView>#include <QDockWidget>#include <QFont>#include <QTreeView>#include <QScrollArea>#include <QTextDocumentFragment>#include <QTextCursor>#include <QAbstractTextDocumentLayout>static const int MaxCandidates = 5;const char MessageEditor::backTab[] = "\a\b\f\n\r\t";const char * const MessageEditor::friendlyBackTab[] = {        QT_TRANSLATE_NOOP("MessageEditor", "bell"),        QT_TRANSLATE_NOOP("MessageEditor", "backspace"),        QT_TRANSLATE_NOOP("MessageEditor", "new page"),        QT_TRANSLATE_NOOP("MessageEditor", "new line"),        QT_TRANSLATE_NOOP("MessageEditor", "carriage return"),        QT_TRANSLATE_NOOP("MessageEditor", "tab")    };void MessageEditor::visualizeBackTabs(const QString &text, QTextEdit *te){    te->clear();    QTextCursor tc(te->textCursor());    QTextCharFormat blueFormat = defFormat;    blueFormat.setForeground(QBrush(Qt::blue));    blueFormat.setFontItalic(true);    blueFormat.setProperty(QTextFormat::UserProperty, -1);    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, defFormat);                plainText.clear();            }            const char *p = strchr(backTab, ch);            // store the character in the user format property            // in the first '(' in the phrase            blueFormat.setProperty(QTextFormat::UserProperty, ch);            tc.insertText(QString("("), blueFormat);            blueFormat.setProperty(QTextFormat::UserProperty, -1);            if (p == 0)            {                tc.insertText(QString::number(ch, 16) + ")", blueFormat);            }            else            {                tc.insertText(MessageEditor::tr(friendlyBackTab[p - backTab]) + ")",                    blueFormat);                if (backTab[p - backTab] == '\n')                    tc.insertBlock();            }        }        // if a space is by itself, at the end, or beside other spaces        else if (ch == ' ')        {            if (i == 0 || i == text.length() - 1 || text[i - 1].isSpace() ||                text[i + 1].isSpace())            {                blueFormat.setProperty(QTextFormat::UserProperty, ch);                tc.insertText(QString("("), blueFormat);                blueFormat.setProperty(QTextFormat::UserProperty, -1);                tc.insertText(MessageEditor::tr("sp)"), blueFormat);            }            else            {                plainText += ' ';            }        }        else        {            plainText += QString(ch);        }    }    tc.insertText(plainText, defFormat);}SourceTextEdit::SourceTextEdit(QWidget *parent) : QTextEdit(parent){    srcmenu = 0;    actCopy = new QAction(tr("&Copy"), this);    actCopy->setShortcut(QKeySequence(tr("Ctrl+C")));    actSelect = new QAction(tr("Select &All"), this);    actSelect->setShortcut(QKeySequence(tr("Ctrl+A")));    connect(actCopy, SIGNAL(triggered()), this, SLOT(copySelection()));    connect(actSelect, SIGNAL(triggered()), this, SLOT(selectAll()));}void SourceTextEdit::copySelection(){    QTextDocumentFragment tdf = textCursor().selection();    QTextDocument td;    QTextCursor tc(&td);    tc.insertFragment(tdf);    int ch;    tc.movePosition(QTextCursor::Start);    while(!tc.atEnd())    {        tc.movePosition(QTextCursor::NextCharacter);        ch = tc.charFormat().intProperty(QTextFormat::UserProperty);        if (ch != 0) // if wrong format        {            // delete char            tc.deletePreviousChar();            if (ch != -1) // insert backtab                tc.insertText(QString(ch));        }    }    QApplication::clipboard()->setText(td.toPlainText());}void SourceTextEdit::contextMenuEvent(QContextMenuEvent *e){    if (!srcmenu)    {        srcmenu = new QMenu(this);        srcmenu->addAction(actCopy);        srcmenu->addAction(actSelect);    }    actCopy->setEnabled(textCursor().hasSelection());    actSelect->setEnabled(!document()->isEmpty());    srcmenu->popup(e->globalPos());}/*   ShadowWidget class impl.   Used to create a shadow like effect for a widget*/ShadowWidget::ShadowWidget(QWidget *parent)    : QWidget(parent), sWidth(10), wMargin(3), childWgt(0){}ShadowWidget::ShadowWidget(QWidget *child, QWidget *parent)    : QWidget(parent), sWidth(10), wMargin(3), childWgt(0){    setWidget(child);}void ShadowWidget::setWidget(QWidget *child){    childWgt = child;    if (childWgt && childWgt->parent() != this) {        childWgt->setParent(this);        childWgt->move(0,0);        childWgt->show();    }}void ShadowWidget::resizeEvent(QResizeEvent *){    if(childWgt) {        childWgt->move(wMargin, wMargin);        childWgt->resize(width() - sWidth - wMargin, height() - sWidth -            wMargin);    }}void ShadowWidget::paintEvent(QPaintEvent *e){    QPainter p;    int w = width() - sWidth;    int h = height() - sWidth;    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::Background,                p.color(QPalette::Active, QPalette::Base));    p.setColor(QPalette::Inactive, QPalette::Background,                p.color(QPalette::Inactive, QPalette::Base));    p.setColor(QPalette::Disabled, QPalette::Background,                p.color(QPalette::Disabled, QPalette::Base));    parent->setPalette(p);    srcTextLbl = new QLabel(tr("Source text"), this);    transLbl   = new QLabel(tr("Translation"), this);    QFont fnt = font();    fnt.setBold(true);    srcTextLbl->setFont(fnt);    transLbl->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()));    transText = new QTextEdit(this);    transText->setObjectName("translation editor");    transText->setFrameStyle(QFrame::NoFrame);    transText->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding,                                             QSizePolicy::MinimumExpanding));    transText->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);    transText->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);    transText->setAutoFormatting(QTextEdit::AutoNone);    transText->setLineWrapMode(QTextEdit::WidgetWidth);    p = transText->palette();    p.setColor(QPalette::Disabled, QPalette::Base, p.color(QPalette::Active, QPalette::Base));    transText->setPalette(p);    connect(transText->document(), SIGNAL(contentsChanged()),             SLOT(handleTranslationChanges()));    connect(transText, SIGNAL(selectionChanged()),             SLOT(translationSelectionChanged()));    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();}/*   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());    cmtText->move(margin, srcText->y() + srcText->height() + space);    cmtText->resize(w - margin*2, cmtText->height());    if (cmtText->isHidden())        transLbl->move(margin, srcText->y() + srcText->height() + space);    else        transLbl->move(margin, cmtText->y() + cmtText->height() + space);    transLbl->resize( w - margin*2, transLbl->height() );    transText->move(margin, transLbl->y() + transLbl->height() + space);    transText->resize(w - margin*2, transText->height());    // Calculate the total height for the editor page - emit a signal    // if the actual page size is larger/smaller    int totHeight = margin + srcTextLbl->height() +                    srcText->height() + space +                    transLbl->height() + space +                    transText->height() + space +                    frameWidth()*lineWidth()*2 + space * 3;    if (!cmtText->isHidden())        totHeight += cmtText->height() + space;     if (height() != totHeight)         emit pageHeightUpdated(totHeight);}void EditorPage::resizeEvent(QResizeEvent *){    handleTranslationChanges();    handleSourceChanges();    handleCommentChanges();    layoutWidgets();}void EditorPage::handleTranslationChanges(){    calculateFieldHeight(transText);    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(){    bool oldBlockState = transText->blockSignals(true);    QTextCursor c = transText->textCursor();    c.clearSelection();    transText->setTextCursor(c);    transText->blockSignals(oldBlockState);    emit selectionChanged();}void EditorPage::translationSelectionChanged(){    bool oldBlockState = srcText->blockSignals(true);    QTextCursor c = srcText->textCursor();    c.clearSelection();    srcText->setTextCursor(c);    srcText->blockSignals(oldBlockState);    emit selectionChanged();}/*   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()) {

⌨️ 快捷键说明

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