Changes for --snapshot

git-svn-id: http://svn.osgeo.org/qgis/trunk@968 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
timlinux 2004-02-29 16:10:58 +00:00
parent cffcd1a49c
commit 24bb981c5a
5 changed files with 202 additions and 166 deletions

View File

@ -75,7 +75,7 @@ int main(int argc, char *argv[])
// special behaviours. Any remaining non command arguments will be kept aside to
// be passed as a list of layers and / or a project that should be loaded.
//
// This behaviour is used to load the app, snapshot the map,
// save the image to disk and then exit
QString mySnapshotFileName="";
@ -145,12 +145,12 @@ int main(int argc, char *argv[])
case 'h':
case '?':
usage( argv[0] );
return 2; // XXX need standard exit codes
usage( argv[0] );
return 2; // XXX need standard exit codes
break;
default:
std::cerr << argv[0] << ": getopt returned character code " << optionChar << "\n";
std::cerr << argv[0] << ": getopt returned character code " << optionChar << "\n";
return 1; // XXX need standard exit codes
}
}
@ -172,7 +172,7 @@ int main(int argc, char *argv[])
myFileList->append(argv[optind++]);
}
}
/////////////////////////////////////////////////////////////////////
// Now we have the handlers for the different behaviours...
////////////////////////////////////////////////////////////////////
@ -180,7 +180,7 @@ int main(int argc, char *argv[])
/////////////////////////////////////////////////////////////////////
// Initialise the application and the translation stuff
/////////////////////////////////////////////////////////////////////
QApplication a(argc, argv);
// a.setFont(QFont("helvetica", 11));
@ -198,7 +198,7 @@ int main(int argc, char *argv[])
{
tor.load(QString("qgis_") + QTextCodec::locale(), ".");
}
//tor.load("qgis_go", "." );
a.installTranslator(&tor);
/* uncomment the following line, if you want a Windows 95 look */
@ -207,25 +207,25 @@ int main(int argc, char *argv[])
QgisApp *qgis = new QgisApp();
a.setMainWidget(qgis);
/////////////////////////////////////////////////////////////////////
// Load a project file if one was specified
/////////////////////////////////////////////////////////////////////
if(myProjectFileName!="")
{
qgis->addProject(myProjectFileName);
}
if(myProjectFileName!="")
{
qgis->addProject(myProjectFileName);
}
/////////////////////////////////////////////////////////////////////
// autoload any filenames that were passed in on the command line
/////////////////////////////////////////////////////////////////////
#ifdef QGISDEBUG
std::cout << "Number of files in myFileList: " << myFileList->count() << std::endl;
std::cout << "Number of files in myFileList: " << myFileList->count() << std::endl;
#endif
for ( QStringList::Iterator myIterator = myFileList->begin(); myIterator != myFileList->end(); ++myIterator )
{
#ifdef QGISDEBUG
std::cout << "Trying to load file : " << *myIterator << std::endl;
#endif
@ -245,22 +245,22 @@ int main(int argc, char *argv[])
}
}
}
/////////////////////////////////////////////////////////////////////
// Take a snapshot of the map view then exit if snapshot mode requested
/////////////////////////////////////////////////////////////////////
if(myProjectFileName!="")
{
qgis->saveMapAsImage(mySnapshotFileName);
return 1;
}
if(mySnapshotFileName!="")
{
QPixmap * myQPixmap = new QPixmap(800,600);
qgis->saveMapAsImage(mySnapshotFileName,myQPixmap);
return 1;
}
/////////////////////////////////////////////////////////////////////
// Continue on to interactive gui...
/////////////////////////////////////////////////////////////////////
qgis->show();
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
return a.exec();

View File

@ -1419,7 +1419,7 @@ void QgisApp::saveMapAsImage()
}
//overloaded version of the above function
void QgisApp::saveMapAsImage(QString theImageFileNameQString)
void QgisApp::saveMapAsImage(QString theImageFileNameQString, QPixmap * theQPixmap)
{
if ( theImageFileNameQString=="")
{
@ -1429,7 +1429,7 @@ void QgisApp::saveMapAsImage(QString theImageFileNameQString)
else
{
//save the mapview to the selected file
mapCanvas->saveAsImage(theImageFileNameQString);
mapCanvas->saveAsImage(theImageFileNameQString,theQPixmap);
}
}
bool QgisApp::addProject(QString projectFile)

View File

@ -111,7 +111,7 @@ public:
bool addProject(QString projectFile);
//!Overloaded version of the private function with same name that takes the imagename as a parameter
void saveMapAsImage(QString);
void saveMapAsImage(QString, QPixmap *);
private:
//! Add a vector layer to the map

View File

@ -41,6 +41,7 @@
#include "qgsmaplayerinterface.h"
#include "qgsvectorlayer.h"
QgsMapCanvas::QgsMapCanvas(QWidget * parent, const char *name):QWidget(parent, name)
{
mapWindow = new QRect();
@ -81,6 +82,10 @@ bool QgsMapCanvas::isDirty()
return dirty;
}
bool QgsMapCanvas::isDrawing()
{
return drawing;;
}
void QgsMapCanvas::addLayer(QgsMapLayerInterface * lyr)
{
// add a maplayer interface to a layer type defined in a plugin
@ -189,7 +194,11 @@ void QgsMapCanvas::refresh()
render2();
}
void QgsMapCanvas::render2()
// The painter device parameter is optional - if ommitted it will default
// to the pmCanvas (ie the gui map display). The idea is that you can pass
// an alternative device such as one that will be used for printing or
// saving a map view as an image file.
void QgsMapCanvas::render2(QPaintDevice * theQPaintDevice)
{
QString msg = frozen ? "frozen" : "thawed";
#ifdef QGISDEBUG
@ -202,8 +211,16 @@ void QgsMapCanvas::render2()
// std::cout << "IN RENDER 2" << std::endl;
drawing = true;
QPainter *paint = new QPainter();
pmCanvas->fill(bgColor);
paint->begin(pmCanvas);
//default to pmCanvas if no paintdevice is supplied
if (theQPaintDevice==NULL)
{
pmCanvas->fill(bgColor);
paint->begin(pmCanvas);
}
else
{
paint->begin(theQPaintDevice);
}
// calculate the translation and scaling parameters
double muppX, muppY;
muppY = currentExtent.height() / height();
@ -272,6 +289,7 @@ void QgsMapCanvas::render2()
}
}
//Render is deprecated! Use Render2
void QgsMapCanvas::render()
{
/* QPainter *paint = new QPainter();
@ -313,9 +331,20 @@ void QgsMapCanvas::render()
paint->end();
*/
}
void QgsMapCanvas::saveAsImage(QString theFileName)
void QgsMapCanvas::saveAsImage(QString theFileName, QPixmap * theQPixmap)
{
pmCanvas->save(theFileName,"PNG");
//
//check if the optional QPaintDevice was supplied
//
if (theQPixmap != NULL)
{
render2(theQPixmap);
theQPixmap->save(theFileName,"PNG");
}
else //use the map view
{
pmCanvas->save(theFileName,"PNG");
}
}
void QgsMapCanvas::paintEvent(QPaintEvent * ev)
{

View File

@ -26,6 +26,7 @@
#include <qevent.h>
#include "qgsrect.h"
#include "qgspoint.h"
#include "qpaintdevice.h"
class QRect;
class QgsCoordinateTransform;
class QgsMapLayer;
@ -43,151 +44,157 @@ class QgsPoint;
class QgsMapCanvas:public QWidget
{
Q_OBJECT public:
//! Constructor
QgsMapCanvas(QWidget * parent = 0, const char *name = 0);
//! Destructor
~QgsMapCanvas();
//! Set the legend control to be used with this canvas
void setLegend(QgsLegend *legend);
//! Get a pointer to the legend control used with this canvas
QgsLegend * getLegend();
/*! Adds a layer to the map canvas.
* @param lyr Pointer to a layer derived from QgsMapLayer
*/
void addLayer(QgsMapLayer * lyr);
/*! \brief Add a layer from a map layer interface defined in a plugin.
* This is not currently implemented
*/
void addLayer(QgsMapLayerInterface * lyr);
//! Draw the map using the symbology set for each layer
void render();
//! Clear the map canvas
void clear();
//! Returns the mupp (map units per pixel) for the canvas
double mupp();
//! Returns the exent for all layers on the map canvased
QgsRect extent();
//! Set the extent of the map canvas
void setExtent(QgsRect);
//! Zoom to the full extent of all layers
void zoomFullExtent();
//! Zoom to the previous extent (view)
void zoomPreviousExtent();
/**Zooms to the extend of the selected features*/
void zoomToSelected();
//! Constructor
QgsMapCanvas(QWidget * parent = 0, const char *name = 0);
//! Destructor
~QgsMapCanvas();
//! Set the legend control to be used with this canvas
void setLegend(QgsLegend *legend);
//! Get a pointer to the legend control used with this canvas
QgsLegend * getLegend();
/*! Adds a layer to the map canvas.
* @param lyr Pointer to a layer derived from QgsMapLayer
*/
void addLayer(QgsMapLayer * lyr);
/*! \brief Add a layer from a map layer interface defined in a plugin.
* This is not currently implemented
*/
void addLayer(QgsMapLayerInterface * lyr);
//! Draw the map using the symbology set for each layer
void render();
//! Clear the map canvas
void clear();
//! Returns the mupp (map units per pixel) for the canvas
double mupp();
//! Returns the exent for all layers on the map canvased
QgsRect extent();
//! Set the extent of the map canvas
void setExtent(QgsRect);
//! Zoom to the full extent of all layers
void zoomFullExtent();
//! Zoom to the previous extent (view)
void zoomPreviousExtent();
/**Zooms to the extend of the selected features*/
void zoomToSelected();
/** \brief Sets the map tool currently being used on the canvas */
void setMapTool(int tool);
void setMapTool(int tool);
/** Write property of QColor bgColor. */
virtual void setbgColor(const QColor & _newVal);
virtual void setbgColor(const QColor & _newVal);
/** Updates the full extent to include the mbr of the rectangle r */
void updateFullExtent(QgsRect r);
//! return the map layer at postion index in the layer stack
QgsMapLayer *getZpos(int index);
//! return the layer by name
QgsMapLayer *layerByName(QString n);
//! return number of layers on the map
int layerCount();
/*! Freeze/thaw the map canvas. This is used to prevent the canvas from
* responding to events while layers are being added/removed etc.
* @param frz Boolean specifying if the canvas should be frozen (true) or
* thawed (false). Default is true.
*/
void freeze(bool frz = true);
//! remove the layer defined by key
void remove(QString key);
//! remove all layers from the map
void removeAll();
//! Flag the canvas as dirty and needed a refresh
void setDirty(bool _dirty);
//! Return the state of the canvas (dirty or not)
bool isDirty();
//! Declare the legend class as a friend of the map canvas
friend class QgsLegend;
public slots:
/**Sets dirty=true and calls render2()*/
void refresh();
void render2();
//! Save the convtents of the map canvas to disk as an image
void saveAsImage(QString theFileName);
//! This slot is connected to the visibility change of one or more layers
void layerStateChange();
//! sets z order based on order of layers in the legend
void setZOrderFromLegend(QgsLegend *lv);
signals:
void xyCoordinates(QgsPoint & p);
private:
//! Overridden mouse move event
void mouseMoveEvent(QMouseEvent * e);
//! Overridden mouse press event
void mousePressEvent(QMouseEvent * e);
//! Overridden mouse release event
void mouseReleaseEvent(QMouseEvent * e);
//! Overridden resize event
void resizeEvent(QResizeEvent * e);
//! Overridden paint event
void paintEvent(QPaintEvent * pe);
//! Gets the value used to calculated the identify search radius
double calculateSearchRadiusValue();
//! map containing the layers by name
std::map < QString, QgsMapLayer * >layers;
//! list containing the names of layers in zorder
std::list < QString > zOrder;
//! Full extent of the map canvas
QgsRect fullExtent;
//! Current extent
QgsRect currentExtent;
//! Previous view extent
QgsRect previousExtent;
//! Map window rectangle
QRect *mapWindow;
//! Pointer to the map legend
QgsLegend *mapLegend;
//! Pointer to the coordinate transform object used to transform coordinates
// from real world to device coordinates
QgsCoordinateTransform *coordXForm;
void updateFullExtent(QgsRect r);
//! return the map layer at postion index in the layer stack
QgsMapLayer *getZpos(int index);
//! return the layer by name
QgsMapLayer *layerByName(QString n);
//! return number of layers on the map
int layerCount();
/*! Freeze/thaw the map canvas. This is used to prevent the canvas from
* responding to events while layers are being added/removed etc.
* @param frz Boolean specifying if the canvas should be frozen (true) or
* thawed (false). Default is true.
*/
void freeze(bool frz = true);
//! remove the layer defined by key
void remove(QString key);
//! remove all layers from the map
void removeAll();
//! Flag the canvas as dirty and needed a refresh
void setDirty(bool _dirty);
//! Return the state of the canvas (dirty or not)
bool isDirty();
//! Declare the legend class as a friend of the map canvas
friend class QgsLegend;
public slots:
/**Sets dirty=true and calls render2()*/
void refresh();
//! The painter device parameter is optional - if ommitted it will default
// to the pmCanvas (ie the gui map display). The idea is that you can pass
// an alternative device such as one that will be used for printing or
// saving a map view as an image file.
void render2(QPaintDevice * theQPaintDevice=0);
//! Save the convtents of the map canvas to disk as an image
void saveAsImage(QString theFileName,QPixmap * QPixmap=0 );
//! This slot is connected to the visibility change of one or more layers
void layerStateChange();
//! sets z order based on order of layers in the legend
void setZOrderFromLegend(QgsLegend *lv);
signals:
void xyCoordinates(QgsPoint & p);
private:
//! Overridden mouse move event
void mouseMoveEvent(QMouseEvent * e);
//! Overridden mouse press event
void mousePressEvent(QMouseEvent * e);
//! Overridden mouse release event
void mouseReleaseEvent(QMouseEvent * e);
//! Overridden resize event
void resizeEvent(QResizeEvent * e);
//! Overridden paint event
void paintEvent(QPaintEvent * pe);
//! Gets the value used to calculated the identify search radius
double calculateSearchRadiusValue();
//! map containing the layers by name
std::map < QString, QgsMapLayer * >layers;
//! list containing the names of layers in zorder
std::list < QString > zOrder;
//! Full extent of the map canvas
QgsRect fullExtent;
//! Current extent
QgsRect currentExtent;
//! Previous view extent
QgsRect previousExtent;
//! Map window rectangle
QRect *mapWindow;
//! Pointer to the map legend
QgsLegend *mapLegend;
//! Pointer to the coordinate transform object used to transform coordinates
// from real world to device coordinates
QgsCoordinateTransform *coordXForm;
/**
* \brief Currently selected map tool.
* @see QGis::MapTools enum for valid values
*/
int mapTool;
* \brief Currently selected map tool.
* @see QGis::MapTools enum for valid values
*/
int mapTool;
//!Flag to indicate status of mouse button
bool mouseButtonDown;
//! Map units per pixel
double m_mupp;
bool mouseButtonDown;
//! Map units per pixel
double m_mupp;
//! Rubber band box for dynamic zoom
QRect zoomBox;
QRect zoomBox;
//! Beginning point of a rubber band box
QPoint boxStartPoint;
//! Pixmap used for restoring the canvas.
QPixmap *pmCanvas;
QPoint boxStartPoint;
//! Pixmap used for restoring the canvas.
QPixmap *pmCanvas;
//! Background color for the map canvas
QColor bgColor;
QColor bgColor;
//! Flag to indicate a map canvas drag operation is taking place
bool dragging;
//! Vector containing the inital color for a layer
std::vector < QColor > initialColor;
//! Increments the z order index
void incrementZpos();
//! Updates the z order for layers on the map
void updateZpos();
//! Flag indicating a map refresh is in progress
bool drawing;
//! Flag indicating if the map canvas is frozen.
bool frozen;
/*! \brief Flag to track the state of the Map canvas.
*
* The canvas is
* flagged as dirty by any operation that changes the state of
* the layers or the view extent. If the canvas is not dirty, paint
* events are handled by bit-blitting the stored canvas bitmap to
* the canvas. This improves performance by not reading the data source
* when no real change has occurred
*/
bool dirty;
//! Value use to calculate the search radius when identifying features
// TODO - Do we need this?
double radiusValue;
bool dragging;
//! Vector containing the inital color for a layer
std::vector < QColor > initialColor;
//! Increments the z order index
void incrementZpos();
//! Updates the z order for layers on the map
void updateZpos();
//! Flag indicating a map refresh is in progress
bool drawing;
//!Accessor for the above flag
bool isDrawing();
//! Flag indicating if the map canvas is frozen.
bool frozen;
/*! \brief Flag to track the state of the Map canvas.
*
* The canvas is
* flagged as dirty by any operation that changes the state of
* the layers or the view extent. If the canvas is not dirty, paint
* events are handled by bit-blitting the stored canvas bitmap to
* the canvas. This improves performance by not reading the data source
* when no real change has occurred
*/
bool dirty;
//! Value use to calculate the search radius when identifying features
// TODO - Do we need this?
double radiusValue;
};
#endif