📄 trwindow.cpp
字号:
/******************************************************************************** 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 TrWindow This is the application's main window.*/#include "trwindow.h"#include "finddialog.h"#include "msgedit.h"#include "phrasebookbox.h"#include "printout.h"#include "about.h"#include "statistics.h"#include "contextmodel.h"#include "messagemodel.h"#include "phrasemodel.h"#include <QAction>#include <QApplication>#include <QBitmap>#include <QDockWidget>#include <QFile>#include <QFileDialog>#include <QFileInfo>#include <QHeaderView>#include <QLabel>#include <QLayout>#include <QMenuBar>#include <QMessageBox>#include <QMenu>#include <QRegExp>#include <QSettings>#include <QStatusBar>#include <QToolBar>#include <QWhatsThis>#include <QAssistantClient>#include <QDesktopWidget>#include <QPrintDialog>#include <QLibraryInfo>#define pagecurl_mask_width 53#define pagecurl_mask_height 51static const uchar pagecurl_mask_bits[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00, 0xf0, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00, 0xc0, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x80, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xfc, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xfc, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xf8, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xf0, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xf0, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xf0, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xf0, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xc0, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0xc0, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08 };typedef QList<MetaTranslatorMessage> TML;static const int ErrorMS = 600000; // for error messagesstatic const int MessageMS = 2500;QPixmap *TrWindow::pxOn = 0;QPixmap *TrWindow::pxOff = 0;QPixmap *TrWindow::pxObsolete = 0;QPixmap *TrWindow::pxDanger = 0;QPixmap *TrWindow::pxWarning = 0;QPixmap *TrWindow::pxEmpty = 0;enum Ending {End_None, End_FullStop, End_Interrobang, End_Colon, End_Ellipsis};static Ending ending(QString str){ str = str.simplified(); int ch = 0; if (!str.isEmpty()) ch = str.right(1)[0].unicode(); switch (ch) { case 0x002e: // full stop if (str.endsWith(QString("..."))) return End_Ellipsis; else return End_FullStop; case 0x0589: // armenian full stop case 0x06d4: // arabic full stop case 0x3002: // ideographic full stop return End_FullStop; case 0x0021: // exclamation mark case 0x003f: // question mark case 0x00a1: // inverted exclamation mark case 0x00bf: // inverted question mark case 0x01c3: // latin letter retroflex click case 0x037e: // greek question mark case 0x061f: // arabic question mark case 0x203c: // double exclamation mark case 0x203d: // interrobang case 0x2048: // question exclamation mark case 0x2049: // exclamation question mark case 0x2762: // heavy exclamation mark ornament return End_Interrobang; case 0x003a: // colon return End_Colon; case 0x2026: // horizontal ellipsis return End_Ellipsis; default: return End_None; }}const QPixmap TrWindow::pageCurl(){ QPixmap pixmap; pixmap.load(":/images/pagecurl.png" ); if ( !pixmap.isNull() ) { QBitmap pageCurlMask = QBitmap::fromData(QSize(pagecurl_mask_width, pagecurl_mask_height), pagecurl_mask_bits, QImage::Format_MonoLSB); pixmap.setMask(pageCurlMask); } return pixmap;}#ifdef Q_WS_MACconst QString rsrcString = ":/images/mac";#elseconst QString rsrcString = ":/images/win";#endifTrWindow::TrWindow() : QMainWindow(0, Qt::Window){ ac = 0;#ifndef Q_WS_MAC setWindowIcon(QPixmap(":/images/appicon.png" ));#endif // Create the application global listview symbols pxOn = new QPixmap(":/images/s_check_on.png"); pxOff = new QPixmap(":/images/s_check_off.png"); pxObsolete = new QPixmap(":/images/s_check_obsolete.png"); pxDanger = new QPixmap(":/images/s_check_danger.png"); pxWarning = new QPixmap(":/images/s_check_warning.png"); pxEmpty = new QPixmap(":/images/s_check_empty.png"); setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea); setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea); setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea); setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea); // Set up the Scope dock window dwScope = new QDockWidget(this); dwScope->setAllowedAreas(Qt::AllDockWidgetAreas); dwScope->setFeatures(QDockWidget::AllDockWidgetFeatures); dwScope->setWindowTitle(tr("Context")); tv = new QTreeView(dwScope); cmdl = new ContextModel(dwScope); tv->setModel(cmdl); tv->setAlternatingRowColors(true); QPalette tvp = tv->palette(); tvp.setColor(QPalette::AlternateBase, TREEVIEW_ODD_COLOR); tv->setPalette(tvp); tv->setSelectionBehavior(QAbstractItemView::SelectRows); tv->setSelectionMode(QAbstractItemView::SingleSelection); tv->setRootIsDecorated(false); dwScope->setWidget(tv); addDockWidget(Qt::LeftDockWidgetArea, dwScope); QFontMetrics fm(font()); tv->header()->setResizeMode(1, QHeaderView::Stretch); tv->header()->resizeSection(0, fm.width(ContextModel::tr("Done")) + 20); tv->header()->resizeSection(2, 55); tv->header()->setClickable(true); me = new MessageEditor(&tor, this); setCentralWidget(me); stv = me->sourceTextView(); mmdl = qobject_cast<MessageModel *>(stv->model()); ptv = me->phraseView(); pmdl = qobject_cast<PhraseModel *>(ptv->model()); setupMenuBar(); setupToolBars(); progress = new QLabel(statusBar()); statusBar()->addPermanentWidget(progress); modified = new QLabel(QString(" %1 ").arg(tr("MOD")), statusBar()); statusBar()->addPermanentWidget(modified); numFinished = 0; numNonobsolete = 0; numMessages = 0; updateProgress(); dirty = false; updateCaption(); finddlg = new FindDialog(this); findMatchCase = false; findWhere = 0; foundWhere = 0; foundOffset = 0; connect(tv->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(showNewScope(QModelIndex,QModelIndex))); connect(stv, SIGNAL(clicked(QModelIndex)), this, SLOT(toggleFinished(QModelIndex))); connect(me, SIGNAL(translationChanged(QString)), this, SLOT(updateTranslation(QString))); connect(me, SIGNAL(finished(bool)), this, SLOT(updateFinished(bool))); connect(me, SIGNAL(prevUnfinished()), this, SLOT(prevUnfinished())); connect(me, SIGNAL(nextUnfinished()), this, SLOT(nextUnfinished())); connect(me, SIGNAL(focusSourceList()), this, SLOT(focusSourceList())); connect(me, SIGNAL(focusPhraseList()), this, SLOT(focusPhraseList())); connect(finddlg, SIGNAL(findNext(QString,int,bool)), this, SLOT(findNext(QString,int,bool))); connect(tv->header(), SIGNAL(sectionClicked(int)), tv, SLOT(clearSelection())); connect(stv->header(), SIGNAL(sectionClicked(int)), stv, SLOT(clearSelection())); tv->setWhatsThis(tr("This panel lists the source contexts.")); stv->setWhatsThis(tr("This panel lists the source texts. " "Items that violate validation rules " "are marked with a warning.")); QSize as( qApp->desktop()->size() ); as -= QSize( 30, 30 ); resize( QSize( 1000, 800 ).boundedTo( as ) ); readConfig(); stats = 0; srcWords = 0; srcChars = 0; srcCharsSpc = 0; QWidget::setTabOrder(ptv, tv); QWidget::setTabOrder(tv, stv);}TrWindow::~TrWindow(){ writeConfig(); cmdl->clearContextList(); delete stats;}void TrWindow::openFile( const QString& name ){ if (name.isEmpty()) return; statusBar()->showMessage(tr("Loading...")); qApp->processEvents(); tor.clear(); if (!tor.load(name)) { statusBar()->clearMessage(); QMessageBox::warning(this, tr("Qt Linguist"), tr("Cannot open '%1'.").arg(name)); return; } tv->clearSelection(); mmdl->setContextItem(0); cmdl->clearContextList(); numFinished = 0; numNonobsolete = 0; numMessages = 0;// foundScope = 0; TML all = tor.messages(); QHash<QString, ContextItem*> contexts; srcWords = 0; srcChars = 0; srcCharsSpc = 0; foreach (MetaTranslatorMessage mtm, all) { qApp->processEvents(); ContextItem *c; if (contexts.contains(QString(mtm.context()))) { c = contexts.value( QString(mtm.context())); } else { c = new ContextItem(tor.toUnicode(mtm.context(), mtm.utf8())); cmdl->appendContextItem(c); contexts.insert(QString(mtm.context()), c); } if (QByteArray(mtm.sourceText()) == ContextComment) { c->appendToComment(tor.toUnicode(mtm.comment(), mtm.utf8())); } else { MessageItem *tmp = new MessageItem(mtm, tor.toUnicode(mtm.sourceText(), mtm.utf8()), tor.toUnicode(mtm.comment(), mtm.utf8()), c); c->appendMessageItem(tmp); updateDanger(tmp); if (mtm.type() != MetaTranslatorMessage::Obsolete) { numNonobsolete++; if (mtm.type() == MetaTranslatorMessage::Finished) numFinished++; doCharCounting(tmp->sourceText(), srcWords, srcChars, srcCharsSpc); } else { c->incrementObsoleteCount(); } numMessages++; } } cmdl->updateAll(); setEnabled(true); updateProgress(); filename = name; dirty = false; updateCaption(); me->showNothing(); doneAndNextAct->setEnabled(false); doneAndNextAlt->setEnabled(false); statusBar()->showMessage(tr("%1 source phrase(s) loaded.").arg(numMessages), MessageMS); foundWhere = 0; foundOffset = 0; if (cmdl->contextsInList() > 0) { findAct->setEnabled(true); findAgainAct->setEnabled(false); } addRecentlyOpenedFile(name, recentFiles); updateStatistics();}void TrWindow::open(){ if (maybeSave()) { QString newFilename = QFileDialog::getOpenFileName( this, QString(), filename, tr("Qt translation source (*.ts)\nAll files (*)")); openFile(newFilename); }}void TrWindow::save(){ if (filename.isEmpty()) return; if (tor.save(filename)) { dirty = false; updateCaption();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -