📄 qgscomposer.cpp
字号:
/*************************************************************************** qgscomposer.cpp - description ------------------- begin : January 2005 copyright : (C) 2005 by Radim Blazek email : blazek@itc.it ***************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/#include "qgscomposer.h"#include "qgisapp.h"#include "qgsapplication.h"#include "qgscomposerview.h"#include "qgscomposition.h"#include "qgsexception.h"#include "qgsproject.h"#include "qgsmessageviewer.h"#include "qgscontexthelp.h"#include <QDesktopWidget>#include <QFileDialog>#include <QFileInfo>#include <QMatrix>#include <QMessageBox>#include <QPainter>#include <QPrinter>#include <QPrintDialog>#include <QSettings>#include <QIcon>#include <QPixmap>#if QT_VERSION < 0x040300#include <Q3Picture>#else#include <QSvgGenerator>#endif#include <QToolBar>#include <QImageWriter>#include <QCheckBox>#include <QSizeGrip>#include <iostream>QgsComposer::QgsComposer( QgisApp *qgis): QMainWindow(){ setupUi(this); setupTheme(); setWindowTitle(tr("QGIS - print composer")); // Template save and load is not yet implemented, so disable those actions mActionOpenTemplate->setEnabled(false); mActionSaveTemplateAs->setEnabled(false); mQgis = qgis; mFirstTime = true;#ifdef QGISDEBUG std::cout << "QgsComposer::QgsComposer" << std::endl;#endif mView = new QgsComposerView ( this, mViewFrame); mPrinter = 0; QGridLayout *l = new QGridLayout(mViewFrame, 1, 1 ); l->addWidget( mView, 0, 0 ); mCompositionOptionsLayout = new QGridLayout( mCompositionOptionsFrame, 1, 1 ); mItemOptionsLayout = new QGridLayout( mItemOptionsFrame, 1, 1 ); mCompositionNameComboBox->insertItem( tr("Map 1") ); mComposition = new QgsComposition( this, 1 ); mComposition->setActive ( true ); // Create size grip (needed by Mac OS X for QMainWindow if QStatusBar is not visible) mSizeGrip = new QSizeGrip(this); mSizeGrip->resize(mSizeGrip->sizeHint()); mSizeGrip->move(rect().bottomRight() - mSizeGrip->rect().bottomRight()); if ( ! connect( mQgis, SIGNAL( projectRead() ), this, SLOT( projectRead()) ) ) { qDebug( "unable to connect to projectRead" ); } if ( ! connect( mQgis, SIGNAL( newProject() ), this, SLOT(newProject()) ) ) { qDebug( "unable to connect to newProject" ); } if ( ! connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(saveWindowState()) ) ) { qDebug( "unable to connect to aboutToQuit" ); } restoreWindowState(); selectItem(); // Set selection tool}QgsComposer::~QgsComposer(){}void QgsComposer::setupTheme(){ //calculate the active theme path QString myThemePath= QgsApplication::themePath(); //now set all the icons mActionOpenTemplate->setIconSet(QIcon(QPixmap(myThemePath + "/mActionFileOpen.png"))); mActionSaveTemplateAs->setIconSet(QIcon(QPixmap(myThemePath + "/mActionFileSaveAs.png"))); mActionExportAsImage->setIconSet(QIcon(QPixmap(myThemePath + "/mActionExportMapServer.png"))); mActionExportAsSVG->setIconSet(QIcon(QPixmap(myThemePath + "/mActionSaveAsSVG.png"))); mActionPrint->setIconSet(QIcon(QPixmap(myThemePath + "/mActionFilePrint.png"))); mActionZoomAll->setIconSet(QIcon(QPixmap(myThemePath + "/mActionZoomFullExtent.png"))); mActionZoomIn->setIconSet(QIcon(QPixmap(myThemePath + "/mActionZoomIn.png"))); mActionZoomOut->setIconSet(QIcon(QPixmap(myThemePath + "/mActionZoomOut.png"))); mActionRefreshView->setIconSet(QIcon(QPixmap(myThemePath + "/mActionDraw.png"))); mActionAddImage->setIconSet(QIcon(QPixmap(myThemePath + "/mActionSaveMapAsImage.png"))); mActionAddNewMap->setIconSet(QIcon(QPixmap(myThemePath + "/mActionAddRasterLayer.png"))); mActionAddNewLabel->setIconSet(QIcon(QPixmap(myThemePath + "/mActionLabel.png"))); mActionAddNewVectLegend->setIconSet(QIcon(QPixmap(myThemePath + "/mActionAddLegend.png"))); mActionAddNewScalebar->setIconSet(QIcon(QPixmap(myThemePath + "/mActionScaleBar.png"))); mActionSelectMoveItem->setIconSet(QIcon(QPixmap(myThemePath + "/mActionPan.png")));}void QgsComposer::open ( void ){ if ( mFirstTime ) { mComposition->createDefault(); mFirstTime = false; show(); zoomFull(); // zoomFull() does not work properly until we have called show() } else{ show(); //make sure the window is displayed - with a saved project, it's possible to not have already called show() //is that a bug? raise(); //bring the composer window to the front }}void QgsComposer::removeWidgetChildren ( QWidget *w ){#ifdef QGISDEBUG std::cout << "QgsComposer::removeWidgetChildren" << std::endl;#endif const QObjectList ol = mItemOptionsFrame->children(); if ( !ol.isEmpty() ) { QListIterator<QObject*> olit( ol ); QObject *ob; while( olit.hasNext() ) { ob = olit.next(); if( ob->isWidgetType() ) { QWidget *ow = (QWidget *) ob; // The following line is legacy Qt3, is not supported in Qt4 // and can cause a SIGABRT //w->removeChild ( ob ); // instead: ow->setParent(0); // TODO: Eventually mItemOptionsFrame should be made // a Qt4 QStackedWidget and all this removeWidgetChildren // shenanigans can alledgedly go away ow->hide(); } } }}void QgsComposer::showCompositionOptions ( QWidget *w ) {#ifdef QGISDEBUG std::cout << "QgsComposer::showCompositionOptions" << std::endl;#endif removeWidgetChildren ( mCompositionOptionsFrame ); if ( w ) { w->reparent ( mCompositionOptionsFrame, QPoint(0,0), TRUE ); mCompositionOptionsLayout->addWidget( w, 0, 0 ); }}void QgsComposer::showItemOptions ( QWidget *w ){#ifdef QGISDEBUG std::cout << "QgsComposer::showItemOptions" << std::endl;#endif removeWidgetChildren ( mItemOptionsFrame ); // NOTE: It is better to leave there the tab with item options if w is NULL if ( w ) { w->reparent ( mItemOptionsFrame, QPoint(0,0), TRUE ); mItemOptionsLayout->addWidget( w, 0, 0 ); mOptionsTabWidget->setCurrentPage (1); }}QgsMapCanvas *QgsComposer::mapCanvas(void){ return mQgis->getMapCanvas();}QgsComposerView *QgsComposer::view(void){ return mView;}QgsComposition *QgsComposer::composition(void){ return mComposition;}void QgsComposer::zoomFull(void){//can we just use QGraphicsView::fitInView with the "paper" rect? QMatrix m; // scale double xscale = 1.0 * (mView->width()-10) / mComposition->canvas()->width(); double yscale = 1.0 * (mView->height()-10) / mComposition->canvas()->height(); double scale = ( xscale < yscale ? xscale : yscale ); // translate double dx = ( mView->width() - scale * mComposition->canvas()->width() ) / 2; double dy = ( mView->height() - scale * mComposition->canvas()->height() ) / 2; m.translate ( dx, dy ); m.scale( scale, scale ); mView->setMatrix( m );// mView->repaintContents(); //needed?}void QgsComposer::on_mActionZoomAll_activated(void){ zoomFull();}/*QMatrix QgsComposer::updateMatrix(double scaleChange){ double scale = mView->matrix().m11() * scaleChange; // get new scale double dx = ( mView->width() - scale * mComposition->canvas()->width() ) / 2; double dy = ( mView->height() - scale * mComposition->canvas()->height() ) / 2; // don't translate if composition is bigger than view if (dx < 0) dx = 0; if (dy < 0) dy = 0; // create new world matrix: QMatrix m; m.translate ( dx, dy ); m.scale ( scale, scale ); return m;}*/void QgsComposer::on_mActionZoomIn_activated(void){ mView->scale(2, 2); mView->update();}void QgsComposer::on_mActionZoomOut_activated(void){ mView->scale(.5, .5); mView->update();}void QgsComposer::on_mActionRefreshView_activated(void){ mComposition->refresh(); mView->update();}void QgsComposer::on_mActionPrint_activated(void){ /* Uff!!! It is impossible to set a custom page size for QPrinter. * Only the sizes hardcoded in Qt library can be used. * 'Fortunately', it seems that everything is written to postscript output file, * regardless the pages size -> * * 1) outputToFile == false: If the output page size doesn't match the user defined size * (in QgsComposer) the output is rescaled and centered so that * it fit to the select output page size. * a warning is displayed, that the map was rescaled. * * 2) outputToFile == true: the output postscript file is written (page size is not * important but bigger is better because it lefts enough space * in BoundingBox definition), then the file is reopened, * and the BoundingBox is redefined. */ // NOTE: QT 3.2 has QPrinter::setOptionEnabled but only for three options if (!mPrinter) { mPrinter = new QPrinter(QPrinter::PrinterResolution); //mPrinter = new QPrinter ( QPrinter::HighResolution ); //mPrinter = new QPrinter ( QPrinter::ScreenResolution ); mPrinter->setFullPage(true);#ifndef Q_OS_MACX // For Qt/Mac 3, don't set outputToFile to true before calling setup // because it wiil suppress the Print dialog and output to file without // giving the user a chance to select a printer instead. // The Mac Print dialog provides an option to create a pdf which is // intended to be invisible to the application. If an eps is desired, // a custom Mac Print dialog is needed. // There is a bug in Qt<=4.2.2 (dialog is not correct) if output is set to file // => disable until they fix it //mPrinter->setOutputToFile (true ) ; //mPrinter->setOutputFileName ( QDir::convertSeparators ( QDir::home().path() + "/" + "qgis.eps") );#endif mPrinter->setColorMode(QPrinter::Color); mPrinter->setPageSize(QPrinter::A4); //would be nice set this based on the composition paper size } else { // Because of bug in Qt<=4.2.2 (dialog is not correct) we have to reset always // to printer otherwise print to file is checked but printer combobox is in dialog mPrinter->setOutputToFile(false); } //set the resolution and paper orientation each time we call up the dialog, not just the first time we run it mPrinter->setResolution(mComposition->resolution()); if (mComposition->paperOrientation() == QgsComposition::Portrait) { mPrinter->setOrientation(QPrinter::Portrait); } else { mPrinter->setOrientation(QPrinter::Landscape); } //if ( mPrinter->setup(this) ) { QPrintDialog printDialog(mPrinter, this); if (printDialog.exec() == QDialog::Accepted) { // TODO: mPrinter->setup() moves the composer under Qgisapp, get it to foreground somehow // raise() for now, is it something better? raise();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -