📄 msgedit.cpp
字号:
/************************************************************************ Copyright (C) 2000-2002 Trolltech AS. All rights reserved.**** This file is part of Qt Linguist.**** This file may be distributed and/or modified under the terms of the** GNU General Public License version 2 as published by the Free Software** Foundation and appearing in the file LICENSE.GPL included in the** packaging of this file.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.**** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************//* TRANSLATOR MsgEdit This is the right panel of the main window.*/#include "msgedit.h"#include "trwindow.h"#include "phraselv.h"#include "simtexth.h"#include <qapplication.h>#include <qcheckbox.h>#include <qclipboard.h>#include <qlabel.h>#include <qlayout.h>#include <qmultilineedit.h>#include <qpalette.h>#include <qpushbutton.h>#include <qstring.h>#include <qtextview.h>#include <qwhatsthis.h>#include <qvbox.h>#include <qmainwindow.h>#include <qheader.h>#include <qregexp.h>#include <qdockarea.h>#include <qdockwindow.h>#include <qscrollview.h>#include <qfont.h>#include <qaccel.h>#include <private/qrichtext_p.h>static const int MaxCandidates = 5;class MED : public QMultiLineEdit{public: MED( QWidget *parent, const char *name = 0 ) : QMultiLineEdit( parent, name ) {} void del() { QMultiLineEdit::del(); } int cursorX() const { return textCursor()->x(); } int cursorY() const { return textCursor()->paragraph()->rect().y() + textCursor()->y(); }};QString richMeta( const QString& text ){ return QString( "<small><font color=blue>(" ) + text + QString( ")</font></small>" );}QString richText( const QString& text ){ const char backTab[] = "\a\b\f\n\r\t"; const char * const 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" ) }; QString rich; int lastSpace = -2; for ( int i = 0; i < (int) text.length(); i++ ) { int ch = text[i].unicode(); if ( ch < 0x20 ) { const char *p = strchr( backTab, ch ); if ( p == 0 ) rich += richMeta( QString::number(ch, 16) ); else rich += richMeta( MessageEditor::tr(friendlyBackTab[p - backTab]) ); } else if ( ch == '<' ) { rich += QString( "<" ); } else if ( ch == '>' ) { rich += QString( ">" ); } else if ( ch == '&' ) { rich += QString( "&" ); } else if ( ch == ' ' ) { if ( lastSpace == i - 1 ) rich += QChar( 0x00a0 ); else rich += ' '; lastSpace = i; } else { rich += QChar( ch ); } if ( ch == '\n' ) rich += QString( "<br>" ); } return rich;}/* ShadowWidget class impl. Used to create a shadow like effect for a widget*/ShadowWidget::ShadowWidget( QWidget * parent, const char * name ) : QWidget( parent, name ), sWidth( 10 ), wMargin( 3 ), childWgt( 0 ){}ShadowWidget::ShadowWidget( QWidget * child, QWidget * parent, const char * name ) : QWidget( parent, name ), sWidth( 10 ), wMargin( 3 ), childWgt( 0 ){ setWidget( child );}void ShadowWidget::setWidget( QWidget * child ){ childWgt = child; if ( childWgt && childWgt->parent() != this ) { childWgt->reparent( this, QPoint( 0, 0 ), TRUE ); }}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( colorGroup().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.eraseRect( w, 0, sWidth, 45 ); // Cheap hack for the page curl.. 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( QWidget * parent, const char * name ) : QFrame( parent, 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, QColorGroup::Base, QColor( white ) ); p.setColor( QPalette::Inactive, QColorGroup::Base, QColor( white ) ); p.setColor( QPalette::Active, QColorGroup::Background, p.active().color( QColorGroup::Base ) ); p.setColor( QPalette::Inactive, QColorGroup::Background, p.inactive().color( QColorGroup::Base ) ); setPalette( p ); srcTextLbl = new QLabel( tr("Source text"), this, "source text label" ); p = srcTextLbl->palette(); p.setColor( QPalette::Active, QColorGroup::Background, palette().active().base() ); p.setColor( QPalette::Inactive, QColorGroup::Background, palette().inactive().base() ); srcTextLbl->setPalette( p ); transLbl = new QLabel( tr("Translation"), this, "translation label" ); p = transLbl->palette(); p.setColor( QPalette::Active, QColorGroup::Background, palette().active().base() ); p.setColor( QPalette::Inactive, QColorGroup::Background, palette().inactive().base() ); transLbl->setPalette( p ); QFont fnt = font(); fnt.setBold( TRUE ); srcTextLbl->setFont( fnt ); transLbl->setFont( fnt ); srcText = new QTextView( this, "source text view" ); srcText->setFrameStyle( QFrame::NoFrame ); srcText->setSizePolicy( QSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum ) ); srcText->setResizePolicy( QScrollView::AutoOne ); srcText->setHScrollBarMode( QScrollView::AlwaysOff ); srcText->setVScrollBarMode( QScrollView::AlwaysOff ); p = srcText->palette(); p.setColor( QPalette::Disabled, QColorGroup::Base, p.active().base() ); srcText->setPalette( p ); connect( srcText, SIGNAL(textChanged()), SLOT(handleSourceChanges()) ); cmtText = new QTextView( this, "comment/context view" ); cmtText->setFrameStyle( QFrame::NoFrame ); cmtText->setSizePolicy( QSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Minimum ) ); cmtText->setResizePolicy( QScrollView::AutoOne ); cmtText->setHScrollBarMode( QScrollView::AlwaysOff ); cmtText->setVScrollBarMode( QScrollView::AlwaysOff ); p = cmtText->palette(); p.setColor( QPalette::Active, QColorGroup::Base, QColor( 236,245,255 ) ); p.setColor( QPalette::Inactive, QColorGroup::Base, QColor( 236,245,255 ) ); cmtText->setPalette( p ); connect( cmtText, SIGNAL(textChanged()), SLOT(handleCommentChanges()) ); translationMed = new MED( this, "translation editor" ); translationMed->setFrameStyle( QFrame::NoFrame ); translationMed->setSizePolicy( QSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding ) ); translationMed->setHScrollBarMode( QScrollView::AlwaysOff ); translationMed->setVScrollBarMode( QScrollView::AlwaysOff ); translationMed->setResizePolicy( QScrollView::AutoOne ); translationMed->setWrapPolicy( QTextView::AtWhiteSpace ); translationMed->setWordWrap( QTextView::WidgetWidth ); translationMed->setTextFormat( QTextView::PlainText ); p = translationMed->palette(); p.setColor( QPalette::Disabled, QColorGroup::Base, p.active().base() ); translationMed->setPalette( p ); connect( translationMed, SIGNAL(textChanged()), SLOT(handleTranslationChanges()) ); pageCurl = new PageCurl( this, "page curl" ); // Focus setFocusPolicy( StrongFocus ); transLbl->setFocusProxy( translationMed ); srcTextLbl->setFocusProxy( translationMed ); cmtText->setFocusProxy( translationMed ); srcText->setFocusProxy( translationMed ); setFocusProxy( translationMed ); updateCommentField();}/* Don't show the comment field if there are no comments.*/void EditorPage::updateCommentField(){ if( cmtText->text().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() ); translationMed->move( margin, transLbl->y() + transLbl->height() + space ); translationMed->resize( w - margin*2, translationMed->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 + translationMed->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( (QTextView *) translationMed );}void EditorPage::handleSourceChanges(){ calculateFieldHeight( srcText );}void EditorPage::handleCommentChanges(){ calculateFieldHeight( cmtText );}/* 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( QTextView * field ){ field->sync(); // make sure the text formatting is done! int contentsHeight = field->contentsHeight(); 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 & ){ 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, QWidget * parent, const char * name ) : QWidget( parent, name ), tor( t ){ doGuesses = TRUE; v = new QVBoxLayout( this ); topDock = new QDockArea( Qt::Horizontal, QDockArea::Normal, this, "top dock area" ); topDock->setMinimumHeight( 10 ); topDock->setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum) ); topDockWnd = new QDockWindow( QDockWindow::InDock, topDock, "top dock window" ); QMainWindow *mw = (QMainWindow*)topLevelWidget(); if ( mw ) { mw->setDockEnabled( topDockWnd, Qt::DockTop, TRUE ); mw->setDockEnabled( topDockWnd, Qt::DockLeft, TRUE ); mw->setDockEnabled( topDockWnd, Qt::DockRight, TRUE ); mw->setDockEnabled( topDockWnd, Qt::DockBottom, TRUE ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -