📄 book.cpp
字号:
/****************************************************************************** $Id: qt/qwerty.cpp 3.3.2 edited May 27 2003 $**** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.**** This file is part of an example program for Qt. This example** program may be used, distributed and modified without limitation.*******************************************************************************/
#include "data.h"#include "book.h"
#include "gotopagedialog.h"
#include "bookmarkdialog.h"#include <qapplication.h>#include <qfile.h>#include <qtextstream.h>#include <qmessagebox.h>#include <qtextcodec.h>
#include <qstatusbar.h>
#include <qstring.h>
#include <qmessagebox.h>
#include <qlistbox.h>
#include <qtextstream.h>
#include <qsizepolicy.h>
#include <qfileinfo.h>
#include "filedialog.h"
#include <qtopia/resource.h>
#include <qtopia/qpeapplication.h>
int CertainLines = 13; //固定行数
SubQMultiLineEdit::SubQMultiLineEdit( QWidget * parent , const char * name): QMultiLineEdit( parent, name)
{
}
SubQMultiLineEdit::~SubQMultiLineEdit()
{
}
/*-----------------------屏蔽鼠标事件---------------------*/
void SubQMultiLineEdit::ForPage()
{
pageDown(FALSE);
}
void SubQMultiLineEdit::Bakpage()
{
pageUp(FALSE);
}
void SubQMultiLineEdit::Hidescrollbar()
{
verticalScrollBar()->hide();
}
void SubQMultiLineEdit::mousePressEvent( QMouseEvent *k)
{
}
void SubQMultiLineEdit::mouseReleaseEvent ( QMouseEvent *k )
{
}
void SubQMultiLineEdit::mouseDoubleClickEvent ( QMouseEvent *k )
{
}
void SubQMultiLineEdit::mouseMoveEvent ( QMouseEvent *k )
{
}
void SubQMultiLineEdit::wheelEvent ( QWheelEvent * w )
{
}
/*------------------构造函数------------------*/ReadBook::ReadBook( QWidget * parent , const char * name, WFlags f ): QMainWindow( parent, name, f){ //setIcon(QPixmap::fromMimeSource("read.png"));
//setSizePolicy( QSizePolicy::Expanding ); //, QSizePolicy::Expanding
setMinimumSize( QSize(0,0) );
setMinimumSize(320,240);
setMaximumSize(320,240);
Initial(); createActions();
createToolbars();
createLabel();
createHeader();
createMulEdits();
//createfiledialog();}
ReadBook::~ReadBook()
{
delete filetools;
delete e;
delete num;
}
/*---------------创建action-------------*/void ReadBook::createActions(){
QString path = QPEApplication::qpeDir();
qDebug( path );
QString openfile = path + "pics/ebook/fileopen.png";
QString fallback = path + "pics/ebook/back.png";
QString goahead = path + "pics/ebook/forward.png";
QString gotoicon = path + "pics/ebook/find.png";
QString bookicon = path + "pics/ebook/bookmark.png";
QString closeicon = path + "pics/ebook/close.png";
//qDebug( openfile );
qDebug( fallback );
openfileAct = new QAction(tr("Open"), loadIcon( openfile ),
QString::null, 0, this, "Open" ); connect(openfileAct, SIGNAL(activated()), this, SLOT(load())); fallbackAct = new QAction(tr("PageUp"), loadIcon( fallback ),
QString::null, 0, this, "Back" );
connect(fallbackAct, SIGNAL(activated()), this, SLOT( backpage() ));
goaheadAct= new QAction(tr("PageDown"), loadIcon( goahead ),
QString::null, 0, this, "Forward" );
connect(goaheadAct, SIGNAL(activated()), this, SLOT( forwardpage()) );
gotopageAct = new QAction(tr("GotoPage"), loadIcon( gotoicon ),
QString::null, 0, this, "Find" );
connect(gotopageAct, SIGNAL(activated()), this, SLOT(gotopage())); bookmarkAct = new QAction(tr("BookMark Manage"), loadIcon( bookicon ),
QString::null, 0, this, "Bookmark" );
connect(bookmarkAct, SIGNAL(activated()), this, SLOT(bookmark()));
closeAct = new QAction(tr("Close"), loadIcon( closeicon ),
QString::null, 0, this, "Close" );
connect(closeAct, SIGNAL(activated()), this, SLOT(close()));}
/*-------------创建工具条-------------*/void ReadBook::createToolbars(){ filetools = new QPEToolBar(this);
//filetools->setHorizontalStretchable( TRUE ); openfileAct->addTo(filetools); fallbackAct->addTo(filetools); goaheadAct->addTo(filetools); gotopageAct->addTo(filetools); bookmarkAct->addTo(filetools);
filetools->addSeparator();
closeAct->addTo(filetools);
setToolBarsMovable( false );
/*setTabOrder( openfileAct, fallbackAct );
setTabOrder( fallbackAct, goaheadAct );
setTabOrder( goaheadAct, gotopageAct );
setTabOrder( gotopageAct, bookmarkAct );
setTabOrder( bookmarkAct, closeAct );*/}
/*--------创建标题栏hao----------------*/
void ReadBook::createLabel()
{
pageinfoLabel = new QLabel(this);
pageinfoLabel->setGeometry( 200, 5, 120, 20);
pageinfoLabel->setText( "<font color=red>Hello World!<!/font>" );
pageinfoLabel->setAlignment(AlignHCenter);
connect(this, SIGNAL(pagesignal()), this, SLOT(updatepageIndicators()));
}
/*-----------创建标题行------------*/
void ReadBook::createHeader()
{
int i = 0;
//QColor color( 124, 104, 238, QColor::Rgb);
num = new QHeader(CertainLines, this, 0 );
num->setGeometry(0, 24, 30, 188);
num->setOrientation( Vertical );
while(i <= CertainLines -2)
{
num->resizeSection(i, 14);
i++;
}
num->resizeSection( CertainLines-1, 16);
num->setResizeEnabled(FALSE, -1); //固定象素;
num->setMovingEnabled(FALSE);
QFont helv("helvetica",11); helv.setPixelSize(11);
num->setFont( helv );
for (i = 0; i < CertainLines; i++ )
num->setLabel(i, " ", -1);
}
/*-------------创建mulidedt---------------*/
void ReadBook::createMulEdits()
{
QFont unifont("wenquanyi",12,5);
unifont.setPixelSize(12); //wenquanyi
//QColor color( 255, 240, 245, QColor::Rgb);
e = new SubQMultiLineEdit( this , "Editor" );
e->setReadOnly(TRUE);
e->setGeometry(28, 22, 290, 188);
//e->setBackgroundColor( color);
e->setFocusPolicy( NoFocus);
e->setWordWrap( QMultiLineEdit::WidgetWidth ); //FixedPixelWidth
//e->setWrapColumnOrWidth( 260 );
e->setWrapPolicy( QMultiLineEdit::Anywhere ); //Anywhere
e->setFont( unifont );
}
/*----------------加载图象-----------------*/
QIconSet ReadBook::loadIcon( const QString & fileName )
{
return QIconSet( QPixmap( fileName ) ); //Resource::loadIconSet( fileName );
}
/*---------------------打开文件对话框----------------*/
void ReadBook::load()
{
Filedialog file( this );
if ( file.exec()) {
PathName = file.OpenFileName();
if ( PathName.right(2) == ".." )
QMessageBox::about(this,tr("Warning"),
tr("<h3><i>Not Right File</i><font color=red>BaBy!<!/font></h3>", 0));
else {
if ( !PathName.isEmpty() )
{
Initial();
load( PathName );
}
}
}
else
return;
}
/*-------------------装载文件并计算文本的总行数和页数---------------------*/
void ReadBook::load( const QString& fileName )
{
QFile f( fileName );
if ( !f.open( IO_ReadOnly ) )
return;
QTextStream t(&f);
QTextCodec *codec = QTextCodec::codecForName("GBK"); //返回文本解码对象
t.setCodec( codec ); //设置中文解码器
e->setText( t.read() );
f.close();
setCaption(QFileInfo( PathName ).fileName());
e->setCursorPosition(0, 0, FALSE); //guiwei
bid = 1; //标志位
Totallines = e->numLines();
if ( Totallines%CertainLines == 0 )
Totalpage = Totallines/CertainLines;
else
Totalpage = Totallines/CertainLines + 1;
CalculateLineNumber(1);
firststatuspage();
printf("%5d\n", Totallines);
}
/*--------------初始化变量-------------*/
void ReadBook::Initial()
{
PageNumber = 1;
Totalpage = 1;
Totallines = 0;
bid = 0;
}
/*--------------添加行号--------------*/
void ReadBook::CalculateLineNumber(int y)
{
int lines = 0, i;
QString Numberstring;
int x = Totallines%CertainLines;
if( y == 1) {
for(i = 0; i < CertainLines; i++) {
lines = (i + 1 + CertainLines*(PageNumber-1))%10000;
Numberstring = QString::number(lines);
num->setLabel(i, Numberstring, -1);
}
}
else if( y == 2) {
for( i = 0; i < CertainLines; i++) {
lines = (i + 1 + x + CertainLines*(PageNumber-1))%10000;
Numberstring = QString::number(lines);
// Numberstring = "<font color=blue>" + Numberstring + "<!/font>";
num->setLabel(i, Numberstring, -1);
}
}
}
/*--------向后翻页-----------*/
void ReadBook::forwardpage()
{
if ( PageNumber < (Totalpage - 1) ) {
e->ForPage();
LineNumber(1);
CalculateLineNumber(1);
}
else if ( PageNumber == Totalpage -1 ) {
if ( Totallines%CertainLines == 0) {
e->setCursorPosition( Totallines-1 , 0, FALSE);
e->setCursorPosition( Totallines-CertainLines , 0, FALSE);
LineNumber(1);
CalculateLineNumber(1);
}
else {
e->setCursorPosition( Totallines-1 , 0, FALSE);
CalculateLineNumber(2);
LineNumber(1);
//e->setCursorPosition( Totallines-Totallines%CertainLines-1, 0, FALSE);
}
}
}
/*--------向前翻页----------*/
void ReadBook::backpage()
{
if ( PageNumber < Totalpage ) {
e->Bakpage();
}
else if ( PageNumber == Totalpage ) {
if ( Totallines%CertainLines == 0 )
e->setCursorPosition( Totallines - 2*CertainLines, 0, FALSE);
else
e->setCursorPosition( Totallines-Totallines%CertainLines - CertainLines, 0, FALSE);
}
if( bid == 1 ) {
LineNumber(2);
CalculateLineNumber(1);
}
else
return;
}
/*-------------查询-------------*/
void ReadBook::gotopage()
{
Gotopagedialog dialog(this);
if(dialog.exec()) {
QString str = dialog.lineEdit->text();
if ( !str.isEmpty()) {
int c = str.mid(0).toInt();
findpage(c);
}
else
return;
}
}
/*-----------------计算行、页的增减---------------*/
void ReadBook::LineNumber(int s)
{
if (s == 1) {
PageNumber++;
}
else if(s == 2 && PageNumber > 1) {
PageNumber--;
}
emit pagesignal();
}
/*---------------更新状态栏的页号--------------*/
void ReadBook::updatepageIndicators()
{
QString latterpage;
QString formpage;
latterpage = QString::number( PageNumber );
formpage = QString::number( Totalpage );
latterpage.append(" / ");
latterpage.append( formpage );
pageinfoLabel->setText( latterpage );
}
void ReadBook::firststatuspage()
{
QString firstStatus = QString( "1 / ");
firstStatus.append( QString::number( Totalpage ));
pageinfoLabel->setText( firstStatus );
}
void ReadBook::findpage( int b)
{
int j = 0;
if( b < Totalpage && Totalpage > 1)
{
if (b > PageNumber && PageNumber < Totalpage) {
j = b - PageNumber;
while( j >= 1) {
e->ForPage();
j--;
}
PageNumber = b;
CalculateLineNumber(1);
updatepageIndicators();
}
else if ( b < PageNumber && PageNumber < Totalpage) {
j = PageNumber - b;
while( j >= 1) {
e->Bakpage();
j--;
}
PageNumber = b;
CalculateLineNumber(1);
updatepageIndicators();
}
else if ( PageNumber == Totalpage ) {
if ( Totallines%CertainLines == 0 )
e->setCursorPosition( Totallines - 2*CertainLines, 0, FALSE);
else
e->setCursorPosition( Totallines-Totallines%CertainLines - CertainLines, 0, FALSE);
j = PageNumber - b -1;
while( j >= 1) {
e->Bakpage();
j--;
}
PageNumber = b;
CalculateLineNumber(1);
updatepageIndicators();
}
}
else if ( b == Totalpage && Totalpage > 1)
{
j = b - PageNumber;
PageNumber = Totalpage -1;
if ( Totallines%CertainLines == 0) {
e->setCursorPosition( Totallines-1, FALSE); //不可缺少
e->setCursorPosition( Totallines - CertainLines, 0, FALSE);
LineNumber(1); //升至总页数
CalculateLineNumber(1);
}
else {
e->setCursorPosition( Totallines-1, 0, FALSE);
//e->setCursorPosition( Totallines-Totallines%CertainLines, 0, FALSE);
CalculateLineNumber(2);
LineNumber(1);
}
updatepageIndicators();
}
else if ( b > Totalpage)
{
if( Totallines > 0 )
QMessageBox::about(this,tr("Warning"), tr("<h3><i>Not Correct PageNumber</i><font color=red>Huny!<!/font></h3>", 0));
else
QMessageBox::about(this,tr("Warning"), tr("<h3><i>Not Open File Yet</i><font color=red>BaBy!<!/font></h3>", 0));
}
else
return;
}
void ReadBook::initbookdialog()
{
bookdialog = new BookMarkDialog( this );
QFile file( "SavedFiles.txt");
if ( !file.open( IO_ReadOnly ) )
return;
QTextStream bufstream(&file);//读文件内容到listbox中
QString funame;
QString namenum;
QString name;
int i = 0;
while( bufstream.atEnd() == 0) {
funame = bufstream.readLine();
namenum = QFileInfo( funame ).fileName();
i = namenum.find(":");
name = namenum.left(i);
bookdialog->listBox->insertItem(tr( name ), 0);
}
file.close();
bookdialog->listBox->setCurrentItem(0);
}
void ReadBook::bookmark()
{
initbookdialog();
if ( bookdialog->exec()) {
QString markfilename = bookdialog->opencertainfile();
int m = PageNumber;
Initial();
load( markfilename );
findpage( m );
}
delete bookdialog;
}
//#cpp
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -