src/app/qgsclipboard.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002      qgsclipboard.cpp  -  QGIS internal clipboard for storage of features
00003      --------------------------------------------------------------------
00004     begin                : 20 May, 2005
00005     copyright            : (C) 2005 by Brendan Morley
00006     email                : morb at ozemail dot com dot au
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 /* $Id: qgsclipboard.cpp 9551 2008-10-27 17:35:28Z homann $ */
00019 
00020 #include <fstream>
00021 
00022 #include <QApplication>
00023 #include <QString>
00024 #include <QStringList>
00025 #include <QClipboard>
00026 
00027 #include "qgsclipboard.h"
00028 #include "qgsfeature.h"
00029 #include "qgsfield.h"
00030 #include "qgsgeometry.h"
00031 #include "qgslogger.h"
00032 #include "qgslogger.h"
00033 
00034 
00035 QgsClipboard::QgsClipboard()
00036     : mFeatureClipboard()
00037 {
00038 }
00039 
00040 QgsClipboard::~QgsClipboard()
00041 {
00042 }
00043 
00044 void QgsClipboard::replaceWithCopyOf( const QgsFieldMap& fields, QgsFeatureList& features )
00045 {
00046 
00047   // Replace the QGis clipboard.
00048   mFeatureClipboard = features;
00049   QgsDebugMsg( "replaced QGis clipboard." );
00050 
00051   // Replace the system clipboard.
00052 
00053   QStringList textLines;
00054   QStringList textFields;
00055 
00056   // first do the field names
00057   textFields += "wkt_geom";
00058   for ( QgsFieldMap::const_iterator fit = fields.begin(); fit != fields.end(); ++fit )
00059   {
00060     textFields += fit->name();
00061   }
00062   textLines += textFields.join( "\t" );
00063   textFields.clear();
00064 
00065 
00066   // then the field contents
00067   for ( QgsFeatureList::iterator it = features.begin(); it != features.end(); ++it )
00068   {
00069     QgsAttributeMap attributes = it->attributeMap();
00070 
00071 
00072     // TODO: Set up Paste Transformations to specify the order in which fields are added.
00073 
00074     if ( it->geometry() )
00075       textFields += it->geometry()->exportToWkt();
00076     else
00077       textFields += "NULL";
00078 
00079     // QgsDebugMsg("about to traverse fields.");
00080     //
00081     for ( QgsAttributeMap::iterator it2 = attributes.begin(); it2 != attributes.end(); ++it2 )
00082     {
00083       // QgsDebugMsg(QString("inspecting field '%1'.").arg(it2->toString()));
00084       textFields += it2->toString();
00085     }
00086 
00087     textLines += textFields.join( "\t" );
00088     textFields.clear();
00089   }
00090 
00091   QString textCopy = textLines.join( "\n" );
00092 
00093   QClipboard *cb = QApplication::clipboard();
00094 
00095   // Copy text into the clipboard
00096 
00097   // With qgis running under Linux, but with a Windows based X
00098   // server (Xwin32), ::Selection was necessary to get the data into
00099   // the Windows clipboard (which seems contrary to the Qt
00100   // docs). With a Linux X server, ::Clipboard was required.
00101   // The simple solution was to put the text into both clipboards.
00102 
00103   // The ::Selection setText() below one may need placing inside so
00104   // #ifdef so that it doesn't get compiled under Windows.
00105   cb->setText( textCopy, QClipboard::Selection );
00106   cb->setText( textCopy, QClipboard::Clipboard );
00107 
00108   QgsDebugMsg( QString( "replaced system clipboard with: %1." ).arg( textCopy ) );
00109 }
00110 
00111 QgsFeatureList QgsClipboard::copyOf()
00112 {
00113 
00114   QgsDebugMsg( "returning clipboard." );
00115 
00116   //TODO: Slurp from the system clipboard as well.
00117 
00118   return mFeatureClipboard;
00119 
00120 //  return mFeatureClipboard;
00121 
00122 }
00123 
00124 void QgsClipboard::clear()
00125 {
00126   mFeatureClipboard.clear();
00127 
00128   QgsDebugMsg( "cleared clipboard." );
00129 }
00130 
00131 void QgsClipboard::insert( QgsFeature& feature )
00132 {
00133   mFeatureClipboard.push_back( feature );
00134 
00135   QgsDebugMsg( "inserted " + feature.geometry()->exportToWkt() );
00136 }
00137 
00138 bool QgsClipboard::empty()
00139 {
00140   return mFeatureClipboard.empty();
00141 }

Generated on Tue Oct 28 16:51:26 2008 for Quantum GIS API Documentation by  doxygen 1.5.1