aided the possibility to select features from the attribute table and draw the selected features in another color

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@238 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
mhugent 2003-08-09 14:00:00 +00:00
parent 2e3989566f
commit 75601810e7
9 changed files with 197 additions and 25 deletions

View File

@ -247,6 +247,7 @@ void QgisApp::addLayer()
// create the layer
QgsShapeFileLayer *lyr = new QgsShapeFileLayer(*it, base);
QObject::connect(lyr,SIGNAL(repaintRequested()),mapCanvas,SLOT(refresh()));
// give it a random color
if (lyr->isValid()) {

View File

@ -19,21 +19,73 @@
#include <qcursor.h>
#include <qfont.h>
#include "qgsattributetable.h"
#include <iostream>
QgsAttributeTable::QgsAttributeTable(QWidget * parent, const char *name):QTable(parent, name)
QgsAttributeTable::QgsAttributeTable(QWidget * parent, const char *name):QTable(parent, name), lockKeyPressed(false)
{
QFont f( font() );
f.setFamily( "Helvetica" );
f.setPointSize(11);
setFont(f);
setSelectionMode(QTable::MultiRow);
QObject::connect(this,SIGNAL(selectionChanged()),this,SLOT(handleChangedSelections()));
setFocus();
}
QgsAttributeTable::~QgsAttributeTable()
{
}
void QgsAttributeTable::columnClicked(int col)
{
QApplication::setOverrideCursor(Qt::waitCursor);
sortColumn(col, true, true);
clearSelection(true);
emit selectionRemoved();
emit repaintRequested();
QApplication::restoreOverrideCursor();
}
void QgsAttributeTable::keyPressEvent(QKeyEvent* ev)
{
if(ev->key()==Qt::Key_Control||ev->key()==Qt::Key_Shift)
{
lockKeyPressed=true;
}
}
void QgsAttributeTable::keyReleaseEvent(QKeyEvent* ev)
{
if(ev->key()==Qt::Key_Control||ev->key()==Qt::Key_Shift)
{
lockKeyPressed=false;
}
}
void QgsAttributeTable::handleChangedSelections()
{
QTableSelection cselection;
if(lockKeyPressed==false)
{
//clear the list and evaluate the last selection
emit selectionRemoved();
}
//if there is no current selection, there is nothing to do
if(currentSelection()==-1)
{
emit repaintRequested();
return;
}
cselection=selection(currentSelection());
for(int index=cselection.topRow();index<=cselection.bottomRow();index++)
{
emit selected(text(index,0).toInt());
}
emit repaintRequested();
}

View File

@ -27,10 +27,28 @@
class QgsAttributeTable:public QTable
{
Q_OBJECT public:
QgsAttributeTable(QWidget * parent = 0, const char *name = 0);
~QgsAttributeTable();
public slots:void columnClicked(int col);
Q_OBJECT
public:
QgsAttributeTable(QWidget * parent = 0, const char *name = 0);
~QgsAttributeTable();
public slots:
void columnClicked(int col);
protected slots:
void handleChangedSelections();
protected:
/**Flag telling if the ctrl-button or the shift-button is pressed*/
bool lockKeyPressed;
void keyPressEvent(QKeyEvent* ev);
void keyReleaseEvent(QKeyEvent* ev);
signals:
/**Is emitted when a row was selected*/
void selected(int);
/**Is emitted when all rows have been deselected*/
void selectionRemoved();
/**Is emmited when a set of related selection and deselection signals have been emitted*/
void repaintRequested();
};
#endif

View File

@ -18,7 +18,7 @@
#include "qgsattributetabledisplay.h"
QgsAttributeTableDisplay::QgsAttributeTableDisplay(){
QgsAttributeTableDisplay::QgsAttributeTableDisplay() : QgsAttributeTableBase() {
}
QgsAttributeTableDisplay::~QgsAttributeTableDisplay(){
}

View File

@ -32,6 +32,9 @@ class QgsAttributeTableDisplay:public QgsAttributeTableBase
~QgsAttributeTableDisplay();
QgsAttributeTable *table();
void setTitle(QString title);
signals:
/**Is emitted before the widget deletes itself*/
void deleted();
};
#endif

View File

@ -139,6 +139,12 @@ QgsMapLayer *QgsMapCanvas::layerByName(QString name)
}
void QgsMapCanvas::refresh()
{
dirty=true;
render2();
}
void QgsMapCanvas::render2()
{
QString msg = frozen ? "frozen" : "thawed";

View File

@ -80,6 +80,8 @@ public:
void setDirty(bool _dirty);
friend class QgsLegend;
public slots:
/**Sets dirty=true and calls render2()*/
void refresh();
void render2();
//! This slot is connected to the visibility change of one or more layers
void layerStateChange();

View File

@ -29,7 +29,6 @@
#include "qgsshapefilelayer.h"
#include "qgsidentifyresults.h"
#include "qgsattributetable.h"
#include "qgsattributetabledisplay.h"
#include <ogrsf_frmts.h>
#include <ogr_geometry.h>
@ -38,7 +37,7 @@ QgsShapeFileLayer::QgsShapeFileLayer(QString vectorLayerPath, QString baseName)
{
// test ogr access to a shapefile
ogrDataSource = OGRSFDriverRegistrar::Open((const char *) dataSource);
if (ogrDataSource != NULL) {
//std::cout << "Adding " << dataSource << std::endl;
@ -67,11 +66,32 @@ QgsShapeFileLayer::QgsShapeFileLayer(QString vectorLayerPath, QString baseName)
valid = false;
}
//create a boolean vector and set every entry to false
if(valid)
{
selected=new QValueVector<bool>(ogrLayer->GetFeatureCount(),false);
}
else
{
selected=0;
}
tabledisplay=0;
//draw the selected features in yellow
selectionColor.setRgb(255,255,0);
}
QgsShapeFileLayer::~QgsShapeFileLayer()
{
//delete ogrDataSource;
if(selected)
{
delete selected;
}
if(tabledisplay)
{
tabledisplay->close();
}
}
/** No descriptions */
@ -111,6 +131,7 @@ void QgsShapeFileLayer::draw(QPainter * p, QgsRect * viewExtent, QgsCoordinateTr
OGRErr result = ((OGRPolygon *) filter)->importFromWkt(&wktText);
if (result == OGRERR_NONE) {
ogrLayer->SetSpatialFilter(filter);
int featureCount = 0;
while (OGRFeature * fet = ogrLayer->GetNextFeature()) {
@ -119,6 +140,20 @@ void QgsShapeFileLayer::draw(QPainter * p, QgsRect * viewExtent, QgsCoordinateTr
}
//std::cout << "reading feautures\n";
//fet->DumpReadable(stdout);
//if feature is selected, change the color of the painter
if((*selected)[fet->GetFID()]==true)
{
p->setPen(selectionColor);
brush->setColor(selectionColor);
}
else
{
p->setPen(sym->color());
brush->setColor(sym->fillColor());
}
OGRGeometry *geom = fet->GetGeometryRef();
if(!geom){
std::cout << "geom pointer is null" << std::endl;
@ -166,7 +201,6 @@ QString val;
//std::cout << "marker draw complete\n";
break;
case WKBLineString:
// get number of points in the line
ptr = feature + 5;
nPoints = (int *) ptr;
@ -182,7 +216,6 @@ QString val;
p->moveTo(pt.xToInt(), pt.yToInt());
else
p->lineTo(pt.xToInt(), pt.yToInt());
}
break;
case WKBMultiLineString:
@ -347,33 +380,44 @@ void QgsShapeFileLayer::identify(QgsRect * r)
}
void QgsShapeFileLayer::table()
{
if(tabledisplay)
{
tabledisplay->raise();
}
else
{
// display the attribute table
QApplication::setOverrideCursor(Qt::waitCursor);
ogrLayer->SetSpatialFilter(0);
OGRFeature *fet = ogrLayer->GetNextFeature();
int numFields = fet->GetFieldCount();
QgsAttributeTableDisplay *at = new QgsAttributeTableDisplay();
at->table()->setNumRows(ogrLayer->GetFeatureCount(true));
at->table()->setNumCols(numFields);
tabledisplay = new QgsAttributeTableDisplay();
QObject:connect(tabledisplay,SIGNAL(deleted()),this,SLOT(invalidateTableDisplay()));
tabledisplay->table()->setNumRows(ogrLayer->GetFeatureCount(true));
tabledisplay->table()->setNumCols(numFields+1);//+1 for the id-column
int row = 0;
// set up the column headers
QHeader *colHeader = at->table()->horizontalHeader();
for (int h = 0; h < numFields; h++) {
OGRFieldDefn *fldDef = fet->GetFieldDefnRef(h);
QHeader *colHeader = tabledisplay->table()->horizontalHeader();
colHeader->setLabel(0,"id");//label for the id-column
for (int h = 1; h < numFields+1; h++) {
OGRFieldDefn *fldDef = fet->GetFieldDefnRef(h-1);
QString fld = fldDef->GetNameRef();
colHeader->setLabel(h, fld);
}
while (fet) {
for (int i = 0; i < numFields; i++) {
//id-field
tabledisplay->table()->setText(row,0,QString::number(fet->GetFID()));
for (int i = 1; i < numFields+1; i++) {
// get the field values
QString val;
//if(fldType == 16604 ) // geometry
val = "(geometry column)";
// else
val = fet->GetFieldAsString(i);
at->table()->setText(row, i, val);
val = fet->GetFieldAsString(i-1);
tabledisplay->table()->setText(row, i, val);
}
row++;
@ -381,14 +425,40 @@ void QgsShapeFileLayer::table()
fet = ogrLayer->GetNextFeature();
}
// reset the pointer to start of features so
// reset the pointer to start of fetabledisplayures so
// subsequent reads will not fail
ogrLayer->ResetReading();
at->table()->setSorting(true);
tabledisplay->table()->setSorting(true);
at->setTitle("Attribute table - " + name());
tabledisplay->setTitle("Tabledisplaytribute table - " + name());
QApplication::restoreOverrideCursor();
at->show();
QObject::connect(tabledisplay->table(),SIGNAL(selected(int)),this,SLOT(select(int)));
QObject::connect(tabledisplay->table(),SIGNAL(selectionRemoved()),this,SLOT(removeSelection()));
QObject::connect(tabledisplay->table(),SIGNAL(repaintRequested()),this,SLOT(triggerRepaint()));
tabledisplay->show();
}
}
void QgsShapeFileLayer::select(int number)
{
(*selected)[number]=true;
}
void QgsShapeFileLayer::removeSelection()
{
for(int i=0;i<(int)selected->size();i++)
{
(*selected)[i]=false;
}
}
void QgsShapeFileLayer::triggerRepaint()
{
emit repaintRequested();
}
void QgsShapeFileLayer::invalidateTableDisplay()
{
tabledisplay=0;
}

View File

@ -24,6 +24,8 @@ class OGRLayer;
class OGRDataSource;
#include "qgsmaplayer.h"
#include "qvaluevector.h"
#include "qgsattributetabledisplay.h"
/*! \class QgsShapeFileLayer
* \brief Shapefile layer
@ -48,6 +50,21 @@ class QgsShapeFileLayer:public QgsMapLayer
Polygon
};
public slots:
/**Sets the 'tabledisplay' to 0 again*/
void invalidateTableDisplay();
void select(int number);
void removeSelection();
void triggerRepaint();
protected:
/**Pointer to the table display object if there is one, else a pointer to 0*/
QgsAttributeTableDisplay* tabledisplay;
/**Vector holding the information which features are activated*/
QValueVector<bool>* selected;
/**Color to draw and fill the selected features*/
QColor selectionColor;
private: // Private attributes
//! Draws the layer using coordinate transformation
void draw(QPainter * p, QgsRect * viewExtent, QgsCoordinateTransform * cXf);
@ -75,6 +92,9 @@ class QgsShapeFileLayer:public QgsMapLayer
void registerFormats();
int endian();
signals:
void repaintRequested();
};
#endif