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

📄 mainframe.cpp

📁 用QT设计的手持设备PDF阅读器。实现了阅读
💻 CPP
字号:
#include "mainframe.h"Mainframe::Mainframe(QWidget * parent, Qt::WindowFlags f)	: QMainWindow(parent, f){	setupUi(this);		scrollArea = new QScrollArea(centralwidget);	scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);	scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);	scrollArea->setWidget(pdfviewer);	//scrollArea->setWidgetResizable(true); 	gridLayout = new QGridLayout(centralwidget);	gridLayout->addWidget(scrollArea, 0, 0, 1, 1);		createWidgets();}void Mainframe::createWidgets(){	lineEdit = new QLineEdit(centralwidget);    lineEdit->setObjectName(QString::fromUtf8("lineEdit"));	lineEdit->setFixedWidth(30);	lineEdit->setText(QString::number(0));	//lineEdit->show();	label = new QLabel(centralwidget);    label->setObjectName(QString::fromUtf8("label"));	label->show();	toolBar_2->insertWidget(actionFirst,lineEdit);	toolBar_2->insertWidget(actionFirst,label);		    sceneScaleCombo = new QComboBox;    QStringList scales;    scales << tr("50%") << tr("75%") << tr("100%") << tr("125%") << tr("150%");    sceneScaleCombo->addItems(scales);    sceneScaleCombo->setCurrentIndex(2);    connect(sceneScaleCombo, SIGNAL(currentIndexChanged(const QString &)),            this, SLOT(sceneScaleChanged(const QString &)));	toolBar_3->insertWidget(actionRight,sceneScaleCombo);	connect(actionOpen, SIGNAL(triggered()), this, SLOT(open()));	connect(actionSave, SIGNAL(triggered()), this, SLOT(save()));	connect(actionPrint, SIGNAL(triggered()), this, SLOT(print()));	connect(actionUp, SIGNAL(triggered()), this, SLOT(turnup()));	connect(actionDown, SIGNAL(triggered()), this, SLOT(turndown()));	connect(actionFirst, SIGNAL(triggered()), this, SLOT(firstpage()));	connect(actionLast, SIGNAL(triggered()), this, SLOT(lastpage()));	connect(actionBig, SIGNAL(triggered()), this, SLOT(zoomIn()));	connect(actionSmall, SIGNAL(triggered()), this, SLOT(zoomOut()));	connect(actionAbout, SIGNAL(triggered()), this, SLOT(about()));}bool Mainframe::open(){	QString filename = QFileDialog::getOpenFileName(this,                                     tr("Open File"), QDir::currentPath());	// TODO	if(filename.isEmpty()){		qDebug()<<"No file"<<filename;		return false;	}	else {		pdfviewer->OpenDocument(filename);		pagenum = 0;		allnum = pdfviewer->allPageNum();		label->setText("/"+QString::number(allnum));		scaleFactor = 1.0;	}		return true;}void Mainframe::save(){	QAction *action = qobject_cast<QAction *>(sender());	QByteArray fileFormat = action->data().toByteArray();	saveFile(fileFormat);}bool Mainframe::saveFile(const QByteArray &fileFormat){	QString initialPath = QDir::currentPath() + "/untitled." + fileFormat;	QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"),                                initialPath,                                tr("%1 Files (*.%2);;All Files (*)")                                .arg(QString(fileFormat.toUpper()))                                .arg(QString(fileFormat)));	if (fileName.isEmpty()){		return false;	} else {		return saveImage(fileName, fileFormat);	}} bool Mainframe::saveImage(const QString &fileName, const char *fileFormat){    QImage visibleImage = image;    resizeImage(&visibleImage, size());    if (visibleImage.save(fileName, fileFormat)) {        //modified = false;        return true;    } else {        return false;    }}void Mainframe::resizeImage(QImage *image, const QSize &newSize){    if (image->size() == newSize)        return;    QImage newImage(newSize, QImage::Format_RGB32);    newImage.fill(qRgb(255, 255, 255));    QPainter painter(&newImage);    painter.drawImage(QPoint(0, 0), *image);    *image = newImage;}void Mainframe::about(){	QMessageBox::about(this, tr("About 	PDF"),            tr("<p>The <b>Scribble</b> This software is for PDF view and editor "               "base widget for an application, and how to reimplement some of "               "QWidget's event handlers to receive the events generated for "               "the application's widgets:</p><p> We reimplement the mouse event "               "handlers to facilitate drawing, the paint event handler to "               "update the application and the resize event handler to optimize "               "the application's appearance. In addition we reimplement the "               "close event handler to intercept the close events before "               "terminating the application.</p><p> The example also demonstrates "               "how to use QPainter to draw an image in real time, as well as "               "to repaint widgets.</p>"));}void Mainframe::print(){    Q_ASSERT(pdfviewer->pixmap());    QPrintDialog dialog(&printer, this);    if (dialog.exec()) {        QPainter painter(&printer);        QRect rect = painter.viewport();        QSize size = pdfviewer->pixmap()->size();        size.scale(rect.size(), Qt::KeepAspectRatio);        painter.setViewport(rect.x(), rect.y(), size.width(), size.height());        painter.setWindow(pdfviewer->pixmap()->rect());        painter.drawPixmap(0, 0, *pdfviewer->pixmap());    }}void Mainframe::sceneScaleChanged(const QString &scale){    double newScale = scale.left(scale.indexOf(tr("%"))).toDouble() / 100.0;    QMatrix oldMatrix = view->matrix();    view->resetMatrix();    view->translate(oldMatrix.dx(), oldMatrix.dy());    view->scale(newScale, newScale);}void Mainframe::turnup(){	if(pagenum>0)		pagenum--;	pdfviewer->showpage(pagenum);	lineEdit->setText(QString::number(pagenum));}void Mainframe::turndown(){	if(pagenum<200)		pagenum++;	pdfviewer->showpage(pagenum);	lineEdit->setText(QString::number(pagenum));	//lineEdit->show();}void Mainframe::firstpage(){	pdfviewer->showpage(0);	lineEdit->setText(QString::number(0));}void Mainframe::lastpage(){	pdfviewer->showpage(allnum = pdfviewer->allPageNum()-1);	lineEdit->setText(QString::number(allnum));}void Mainframe::zoomIn(){	QPainter painter(&(pdfviewer->pixmap1));	QMatrix matrix;	matrix.scale(2,2);	painter.setWorldMatrix(matrix, true);    //scaleImage(1.25);}void Mainframe::zoomOut(){	//QPainter painter(pdfviewer);	//painter.scale(0.5,0.5);    scaleImage(0.8);}void Mainframe::scaleImage(double factor){    Q_ASSERT(pdfviewer->pixmap());    scaleFactor *= factor;    pdfviewer->resize(scaleFactor * pdfviewer->pixmap()->size());    adjustScrollBar(scrollArea->horizontalScrollBar(), factor);    adjustScrollBar(scrollArea->verticalScrollBar(), factor);    actionBig->setEnabled(scaleFactor < 3.0);    actionSmall->setEnabled(scaleFactor > 0.333);	pdfviewer->update();}void Mainframe::adjustScrollBar(QScrollBar *scrollBar, double factor){    scrollBar->setValue(int(factor * scrollBar->value()                            + ((factor - 1) * scrollBar->pageStep()/2)));}

⌨️ 快捷键说明

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