indented code

git-svn-id: http://svn.osgeo.org/qgis/trunk@787 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
gsherman 2004-02-11 05:43:51 +00:00
parent adc62f73ea
commit b96dcd68b5

View File

@ -58,9 +58,9 @@
// typedef for the QgsDataProvider class factory // typedef for the QgsDataProvider class factory
typedef QgsDataProvider *create_it(const char *uri); typedef QgsDataProvider *create_it(const char *uri);
QgsVectorLayer::QgsVectorLayer(QString vectorLayerPath, QString baseName, QString providerKey) QgsVectorLayer::QgsVectorLayer(QString vectorLayerPath, QString baseName, QString providerKey):QgsMapLayer(VECTOR, baseName, vectorLayerPath), providerKey(providerKey),
:QgsMapLayer(VECTOR, baseName, vectorLayerPath), providerKey(providerKey), tabledisplay(0), m_renderer(0), m_propertiesDialog(0),
tabledisplay(0), m_renderer(0), m_propertiesDialog(0), m_rendererDialog(0) m_rendererDialog(0)
{ {
#ifdef DEBUG #ifdef DEBUG
std::cerr << "VECTORLAYERPATH: " << vectorLayerPath << std::endl; std::cerr << "VECTORLAYERPATH: " << vectorLayerPath << std::endl;
@ -74,10 +74,12 @@ const char *cOgrLib = (const char *)ogrlib;
#ifdef TESTPROVIDERLIB #ifdef TESTPROVIDERLIB
// test code to help debug provider loading problems // test code to help debug provider loading problems
void *handle = dlopen(cOgrLib, RTLD_LAZY); void *handle = dlopen(cOgrLib, RTLD_LAZY);
if (!handle) { if (!handle)
{
std::cout << "Error in dlopen: " << dlerror() << std::endl; std::cout << "Error in dlopen: " << dlerror() << std::endl;
} else { } else
{
std::cout << "dlopen suceeded" << std::endl; std::cout << "dlopen suceeded" << std::endl;
dlclose(handle); dlclose(handle);
} }
@ -89,24 +91,28 @@ const char *cOgrLib = (const char *)ogrlib;
std::cout << "Library name is " << myLib->library() << std::endl; std::cout << "Library name is " << myLib->library() << std::endl;
#endif #endif
bool loaded = myLib->load(); bool loaded = myLib->load();
if (loaded) { if (loaded)
{
#ifdef DEBUG #ifdef DEBUG
std::cout << "Loaded data provider library" << std::endl; std::cout << "Loaded data provider library" << std::endl;
std::cout << "Attempting to resolve the classFactory function" << std::endl; std::cout << "Attempting to resolve the classFactory function" << std::endl;
#endif #endif
create_it *cf = (create_it *) myLib->resolve("classFactory"); create_it *cf = (create_it *) myLib->resolve("classFactory");
valid = false; // assume the layer is invalid until we determine otherwise valid = false; // assume the layer is invalid until we determine otherwise
if (cf) { if (cf)
{
#ifdef DEBUG #ifdef DEBUG
std::cout << "Getting pointer to a dataProvider object from the library\n"; std::cout << "Getting pointer to a dataProvider object from the library\n";
#endif #endif
dataProvider = cf(vectorLayerPath); dataProvider = cf(vectorLayerPath);
if (dataProvider) { if (dataProvider)
{
#ifdef DEBUG #ifdef DEBUG
std::cout << "Instantiated the data provider plugin\n"; std::cout << "Instantiated the data provider plugin\n";
#endif #endif
if(dataProvider->isValid()){ if (dataProvider->isValid())
{
valid = true; valid = true;
// get the extent // get the extent
QgsRect *mbr = dataProvider->extent(); QgsRect *mbr = dataProvider->extent();
@ -127,7 +133,8 @@ const char *cOgrLib = (const char *)ogrlib;
setDisplayField(); setDisplayField();
QString layerTitle = baseName; QString layerTitle = baseName;
if(providerKey == "postgres"){ if (providerKey == "postgres")
{
// adjust the display name for postgres layers // adjust the display name for postgres layers
layerTitle = layerTitle.mid(layerTitle.find(".") + 1); layerTitle = layerTitle.mid(layerTitle.find(".") + 1);
layerTitle = layerTitle.left(layerTitle.find("(")); layerTitle = layerTitle.left(layerTitle.find("("));
@ -136,14 +143,16 @@ const char *cOgrLib = (const char *)ogrlib;
layerTitle = layerTitle.left(1).upper() + layerTitle.mid(1); layerTitle = layerTitle.left(1).upper() + layerTitle.mid(1);
setLayerName(layerTitle); setLayerName(layerTitle);
} }
} else { } else
{
#ifdef DEBUG #ifdef DEBUG
std::cout << "Unable to instantiate the data provider plugin\n"; std::cout << "Unable to instantiate the data provider plugin\n";
#endif #endif
valid = false; valid = false;
} }
} }
} else { } else
{
valid = false; valid = false;
#ifdef DEBUG #ifdef DEBUG
std::cout << "Failed to load " << "../providers/libproviders.so" << "\n"; std::cout << "Failed to load " << "../providers/libproviders.so" << "\n";
@ -161,7 +170,8 @@ const char *cOgrLib = (const char *)ogrlib;
QgsVectorLayer::~QgsVectorLayer() QgsVectorLayer::~QgsVectorLayer()
{ {
if (tabledisplay) { if (tabledisplay)
{
tabledisplay->close(); tabledisplay->close();
delete tabledisplay; delete tabledisplay;
} }
@ -183,10 +193,12 @@ QString QgsVectorLayer::providerType()
{ {
return providerKey; return providerKey;
} }
/** /**
* sets the preferred display field based on some fuzzy logic * sets the preferred display field based on some fuzzy logic
*/ */
void QgsVectorLayer::setDisplayField(){ void QgsVectorLayer::setDisplayField()
{
// Determine the field index for the feature column of the identify // Determine the field index for the feature column of the identify
// dialog. We look for fields containing "name" first and second for // dialog. We look for fields containing "name" first and second for
// fields containing "id". If neither are found, the first field // fields containing "id". If neither are found, the first field
@ -196,37 +208,46 @@ void QgsVectorLayer::setDisplayField(){
std::vector < QgsField > fields = dataProvider->fields(); std::vector < QgsField > fields = dataProvider->fields();
int j = 0; int j = 0;
for(int j=0; j< fields.size(); j++){ for (int j = 0; j < fields.size(); j++)
{
QString fldName = fields[j].getName(); QString fldName = fields[j].getName();
#ifdef DEBUG #ifdef DEBUG
std::cout << "Checking field " << fldName << std::endl; std::cout << "Checking field " << fldName << std::endl;
#endif #endif
if (fldName.find("name", false) > -1) { if (fldName.find("name", false) > -1)
{
idxName = fldName; idxName = fldName;
break; break;
} }
if(fldName.find("descrip", false) > -1){ if (fldName.find("descrip", false) > -1)
{
idxName = fldName; idxName = fldName;
break; break;
} }
if (fldName.find("id", false)> -1) { if (fldName.find("id", false) > -1)
{
idxId = fldName; idxId = fldName;
break; break;
} }
} }
if (idxName.length() > 0) { if (idxName.length() > 0)
{
fieldIndex = idxName; fieldIndex = idxName;
} else { } else
if (idxId.length() > 0) { {
if (idxId.length() > 0)
{
fieldIndex = idxId; fieldIndex = idxId;
}else{ } else
{
fieldIndex = fields[0].getName(); fieldIndex = fields[0].getName();
} }
} }
} }
/* /*
* Draw the layer * Draw the layer
*/ */
@ -245,7 +266,8 @@ void QgsVectorLayer::draw_old(QPainter * p, QgsRect * viewExtent, QgsCoordinateT
void QgsVectorLayer::draw(QPainter * p, QgsRect * viewExtent, QgsCoordinateTransform * cXf) void QgsVectorLayer::draw(QPainter * p, QgsRect * viewExtent, QgsCoordinateTransform * cXf)
{ {
if (/*1 == 1*/m_renderer) { if ( /*1 == 1 */ m_renderer)
{
// painter is active (begin has been called // painter is active (begin has been called
/* Steps to draw the layer /* Steps to draw the layer
1. get the features in the view extent by SQL query 1. get the features in the view extent by SQL query
@ -292,13 +314,16 @@ void QgsVectorLayer::draw(QPainter * p, QgsRect * viewExtent, QgsCoordinateTrans
QgsPoint pt; QgsPoint pt;
QPointArray *pa; QPointArray *pa;
int wkbType; int wkbType;
while ((fet = dataProvider->getNextFeature(attributesneeded))) {//true is necessary for graduated symbol while ((fet = dataProvider->getNextFeature(attributesneeded)))
{ //true is necessary for graduated symbol
if (fet == 0) { if (fet == 0)
{
#ifdef DEBUG #ifdef DEBUG
std::cout << "get next feature returned null\n"; std::cout << "get next feature returned null\n";
#endif #endif
} else { } else
{
//if feature is selected, change the color of the painter //if feature is selected, change the color of the painter
//TODO fix this selection code to work with the provider //TODO fix this selection code to work with the provider
//if ((*selected)[(fet->featureId())] == true) //if ((*selected)[(fet->featureId())] == true)
@ -328,7 +353,8 @@ void QgsVectorLayer::draw(QPainter * p, QgsRect * viewExtent, QgsCoordinateTrans
// std::cout << "Feature type: " << wkbType << std::endl; // std::cout << "Feature type: " << wkbType << std::endl;
// read each feature based on its type // read each feature based on its type
switch (wkbType) { switch (wkbType)
{
case WKBPoint: case WKBPoint:
p->setBrush(*brush); p->setBrush(*brush);
@ -352,7 +378,8 @@ void QgsVectorLayer::draw(QPainter * p, QgsRect * viewExtent, QgsCoordinateTrans
ptr = feature + 5; ptr = feature + 5;
nPoints = (int *) ptr; nPoints = (int *) ptr;
ptr = feature + 1 + 2 * sizeof(int); ptr = feature + 1 + 2 * sizeof(int);
for (idx = 0; idx < *nPoints; idx++) { for (idx = 0; idx < *nPoints; idx++)
{
x = (double *) ptr; x = (double *) ptr;
ptr += sizeof(double); ptr += sizeof(double);
y = (double *) ptr; y = (double *) ptr;
@ -370,13 +397,15 @@ void QgsVectorLayer::draw(QPainter * p, QgsRect * viewExtent, QgsCoordinateTrans
numLineStrings = (int) (feature[5]); numLineStrings = (int) (feature[5]);
ptr = feature + 9; ptr = feature + 9;
for (jdx = 0; jdx < numLineStrings; jdx++) { for (jdx = 0; jdx < numLineStrings; jdx++)
{
// each of these is a wbklinestring so must handle as such // each of these is a wbklinestring so must handle as such
lsb = *ptr; lsb = *ptr;
ptr += 5; // skip type since we know its 2 ptr += 5; // skip type since we know its 2
nPoints = (int *) ptr; nPoints = (int *) ptr;
ptr += sizeof(int); ptr += sizeof(int);
for (idx = 0; idx < *nPoints; idx++) { for (idx = 0; idx < *nPoints; idx++)
{
x = (double *) ptr; x = (double *) ptr;
ptr += sizeof(double); ptr += sizeof(double);
y = (double *) ptr; y = (double *) ptr;
@ -398,13 +427,15 @@ void QgsVectorLayer::draw(QPainter * p, QgsRect * viewExtent, QgsCoordinateTrans
numRings = (int *) (feature + 1 + sizeof(int)); numRings = (int *) (feature + 1 + sizeof(int));
//std::cout << "Number of rings: " << *numRings << std::endl; //std::cout << "Number of rings: " << *numRings << std::endl;
ptr = feature + 1 + 2 * sizeof(int); ptr = feature + 1 + 2 * sizeof(int);
for (idx = 0; idx < *numRings; idx++) { for (idx = 0; idx < *numRings; idx++)
{
// get number of points in the ring // get number of points in the ring
nPoints = (int *) ptr; nPoints = (int *) ptr;
//std::cout << "Number of points: " << *nPoints << std::endl; //std::cout << "Number of points: " << *nPoints << std::endl;
ptr += 4; ptr += 4;
pa = new QPointArray(*nPoints); pa = new QPointArray(*nPoints);
for (jdx = 0; jdx < *nPoints; jdx++) { for (jdx = 0; jdx < *nPoints; jdx++)
{
// add points to a point array for drawing the polygon // add points to a point array for drawing the polygon
// std::cout << "Adding points to array\n"; // std::cout << "Adding points to array\n";
x = (double *) ptr; x = (double *) ptr;
@ -428,18 +459,21 @@ void QgsVectorLayer::draw(QPainter * p, QgsRect * viewExtent, QgsCoordinateTrans
// get the number of polygons // get the number of polygons
ptr = feature + 5; ptr = feature + 5;
numPolygons = (int *) ptr; numPolygons = (int *) ptr;
for (kdx = 0; kdx < *numPolygons; kdx++) { for (kdx = 0; kdx < *numPolygons; kdx++)
{
//skip the endian and feature type info and //skip the endian and feature type info and
// get number of rings in the polygon // get number of rings in the polygon
ptr = feature + 14; ptr = feature + 14;
numRings = (int *) ptr; numRings = (int *) ptr;
ptr += 4; ptr += 4;
for (idx = 0; idx < *numRings; idx++) { for (idx = 0; idx < *numRings; idx++)
{
// get number of points in the ring // get number of points in the ring
nPoints = (int *) ptr; nPoints = (int *) ptr;
ptr += 4; ptr += 4;
pa = new QPointArray(*nPoints); pa = new QPointArray(*nPoints);
for (jdx = 0; jdx < *nPoints; jdx++) { for (jdx = 0; jdx < *nPoints; jdx++)
{
// add points to a point array for drawing the polygon // add points to a point array for drawing the polygon
x = (double *) ptr; x = (double *) ptr;
ptr += sizeof(double); ptr += sizeof(double);
@ -466,8 +500,7 @@ void QgsVectorLayer::draw(QPainter * p, QgsRect * viewExtent, QgsCoordinateTrans
break; break;
} }
delete[]feature; delete[]feature;
} } else
else
{ {
//pass the feature to the renderer //pass the feature to the renderer
m_renderer->renderFeature(p, fet, cXf); m_renderer->renderFeature(p, fet, cXf);
@ -480,8 +513,7 @@ void QgsVectorLayer::draw(QPainter * p, QgsRect * viewExtent, QgsCoordinateTrans
} }
} }
qApp->processEvents(); qApp->processEvents();
} } else
else
{ {
#ifdef DEBUG #ifdef DEBUG
qWarning("Warning, QgsRenderer is null in QgsVectorLayer::draw()"); qWarning("Warning, QgsRenderer is null in QgsVectorLayer::draw()");
@ -514,20 +546,24 @@ void QgsVectorLayer::identify(QgsRect * r)
unsigned char *feature; unsigned char *feature;
// display features falling within the search radius // display features falling within the search radius
QgsIdentifyResults *ir = 0; QgsIdentifyResults *ir = 0;
while ((fet = dataProvider->getNextFeature(true))) { while ((fet = dataProvider->getNextFeature(true)))
{
featureCount++; featureCount++;
if(featureCount == 1){ if (featureCount == 1)
{
ir = new QgsIdentifyResults(); ir = new QgsIdentifyResults();
} }
QListViewItem *featureNode = ir->addNode("foo"); QListViewItem *featureNode = ir->addNode("foo");
featureNode->setText(0, fieldIndex); featureNode->setText(0, fieldIndex);
std::vector < QgsFeatureAttribute > attr = fet->attributeMap(); std::vector < QgsFeatureAttribute > attr = fet->attributeMap();
for(int i=0; i < attr.size(); i++){ for (int i = 0; i < attr.size(); i++)
{
#ifdef DEBUG #ifdef DEBUG
std::cout << attr[i].fieldName() << " == " << fieldIndex << std::endl; std::cout << attr[i].fieldName() << " == " << fieldIndex << std::endl;
#endif #endif
if(attr[i].fieldName().lower() == fieldIndex){ if (attr[i].fieldName().lower() == fieldIndex)
{
featureNode->setText(1, attr[i].fieldValue()); featureNode->setText(1, attr[i].fieldValue());
} }
ir->addAttribute(featureNode, attr[i].fieldName(), attr[i].fieldValue()); ir->addAttribute(featureNode, attr[i].fieldName(), attr[i].fieldValue());
@ -538,21 +574,26 @@ void QgsVectorLayer::identify(QgsRect * r)
#ifdef DEBUG #ifdef DEBUG
std::cout << "Feature count on identify: " << featureCount << std::endl; std::cout << "Feature count on identify: " << featureCount << std::endl;
#endif #endif
if (ir) { if (ir)
{
ir->setTitle(name()); ir->setTitle(name());
ir->show(); ir->show();
} }
QApplication::restoreOverrideCursor(); QApplication::restoreOverrideCursor();
if (featureCount == 0) { if (featureCount == 0)
QMessageBox::information(0, tr("No features found"), tr("No features were found in the active layer at the point you clicked")); {
QMessageBox::information(0, tr("No features found"),
tr("No features were found in the active layer at the point you clicked"));
} }
} }
void QgsVectorLayer::table() void QgsVectorLayer::table()
{ {
if (tabledisplay) { if (tabledisplay)
{
tabledisplay->raise(); tabledisplay->raise();
} else { } else
{
// display the attribute table // display the attribute table
QApplication::setOverrideCursor(Qt::waitCursor); QApplication::setOverrideCursor(Qt::waitCursor);
dataProvider->reset(); dataProvider->reset();
@ -574,13 +615,15 @@ void QgsVectorLayer::table()
colHeader->setLabel(h, fields[h - 1].getName()); colHeader->setLabel(h, fields[h - 1].getName());
} }
QgsFeature *fet; QgsFeature *fet;
while ((fet = dataProvider->getNextFeature(true))) { while ((fet = dataProvider->getNextFeature(true)))
{
//id-field //id-field
tabledisplay->table()->setText(row, 0, QString::number(fet->featureId())); tabledisplay->table()->setText(row, 0, QString::number(fet->featureId()));
tabledisplay->table()->insertFeatureId(fet->featureId(), row); //insert the id into the search tree of qgsattributetable tabledisplay->table()->insertFeatureId(fet->featureId(), row); //insert the id into the search tree of qgsattributetable
std::vector < QgsFeatureAttribute > attr = fet->attributeMap(); std::vector < QgsFeatureAttribute > attr = fet->attributeMap();
for(int i=0; i < attr.size(); i++){ for (int i = 0; i < attr.size(); i++)
{
// get the field values // get the field values
tabledisplay->table()->setText(row, i + 1, attr[i].fieldValue()); tabledisplay->table()->setText(row, i + 1, attr[i].fieldValue());
} }
@ -599,7 +642,8 @@ void QgsVectorLayer::table()
tabledisplay->table()->clearSelection(); //deselect the first row tabledisplay->table()->clearSelection(); //deselect the first row
QObject::disconnect(tabledisplay->table(), SIGNAL(selectionChanged()), tabledisplay->table(), SLOT(handleChangedSelections())); QObject::disconnect(tabledisplay->table(), SIGNAL(selectionChanged()), tabledisplay->table(),
SLOT(handleChangedSelections()));
for (std::map < int, bool >::iterator it = selected.begin(); it != selected.end(); ++it) for (std::map < int, bool >::iterator it = selected.begin(); it != selected.end(); ++it)
{ {
@ -630,7 +674,8 @@ void QgsVectorLayer::select(QgsRect * rect, bool lock)
QApplication::setOverrideCursor(Qt::waitCursor); QApplication::setOverrideCursor(Qt::waitCursor);
if (tabledisplay) if (tabledisplay)
{ {
QObject::disconnect(tabledisplay->table(), SIGNAL(selectionChanged()), tabledisplay->table(), SLOT(handleChangedSelections())); QObject::disconnect(tabledisplay->table(), SIGNAL(selectionChanged()), tabledisplay->table(),
SLOT(handleChangedSelections()));
QObject::disconnect(tabledisplay->table(), SIGNAL(selected(int)), this, SLOT(select(int))); //disconnecting because of performance reason QObject::disconnect(tabledisplay->table(), SIGNAL(selected(int)), this, SLOT(select(int))); //disconnecting because of performance reason
} }
@ -691,8 +736,7 @@ void QgsVectorLayer::showLayerProperties()
{ {
m_propertiesDialog->raise(); m_propertiesDialog->raise();
m_propertiesDialog->show(); m_propertiesDialog->show();
} } else
else
{ {
m_propertiesDialog = new QgsDlgVectorLayerProperties(this); m_propertiesDialog = new QgsDlgVectorLayerProperties(this);
m_propertiesDialog->show(); m_propertiesDialog->show();
@ -754,8 +798,7 @@ QGis::VectorType QgsVectorLayer::vectorType()
case QGis::WKBMultiPolygon: case QGis::WKBMultiPolygon:
return QGis::Polygon; return QGis::Polygon;
} }
} } else
else
{ {
#ifdef DEBUG #ifdef DEBUG
qWarning("warning, pointer to dataProvider is null in QgsVectorLayer::vectorType()"); qWarning("warning, pointer to dataProvider is null in QgsVectorLayer::vectorType()");
@ -767,7 +810,9 @@ QgsDlgVectorLayerProperties* QgsVectorLayer::propertiesDialog()
{ {
return m_propertiesDialog; return m_propertiesDialog;
} }
void QgsVectorLayer::initContextMenu(QgisApp *app){
void QgsVectorLayer::initContextMenu(QgisApp * app)
{
popMenu = new QPopupMenu(); popMenu = new QPopupMenu();
popMenu->insertItem(tr("&Zoom to extent of selected layer"), app, SLOT(zoomToLayerExtent())); popMenu->insertItem(tr("&Zoom to extent of selected layer"), app, SLOT(zoomToLayerExtent()));
popMenu->insertItem(tr("&Open attribute table"), app, SLOT(attributeTable())); popMenu->insertItem(tr("&Open attribute table"), app, SLOT(attributeTable()));
@ -776,8 +821,10 @@ void QgsVectorLayer::initContextMenu(QgisApp *app){
popMenu->insertSeparator(); popMenu->insertSeparator();
popMenu->insertItem(tr("&Remove"), app, SLOT(removeLayer())); popMenu->insertItem(tr("&Remove"), app, SLOT(removeLayer()));
} }
// //
QPopupMenu *QgsVectorLayer::contextMenu(){ QPopupMenu *QgsVectorLayer::contextMenu()
{
return popMenu; return popMenu;
} }