📄 qgsspit.cpp
字号:
/*************************************************************************** qgsspit.cpp - description ------------------- begin : Fri Dec 19 2003 copyright : (C) 2003 by Denis Antipov email :***************************************************************************//*************************************************************************** * * * 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. * * * ***************************************************************************//* $Id: qgsspit.cpp 8293 2008-03-28 17:36:20Z jef $ */#include <QMessageBox>#include <QComboBox>#include <QFileDialog>#include <QProgressDialog>#include <QRegExp>#include <QFile>#include <QSettings>#include <QPixmap>#include <QHeaderView>#include <QTextCodec>#include <QList>#include <QTableWidgetItem>#include <iostream> #include "qgsencodingfiledialog.h"#include "qgspgutil.h"#include "qgsspit.h"#include "qgsconnectiondialog.h"#include "qgsdatasourceuri.h"#include "qgsmessageviewer.h"#include "spiticon.xpm"#include "qgslogger.h"// Qt implementation of alignment() + changed the numeric types to be shown on the left as well/* Is this still needed? Numbers in Qt4 table seem to be left justified by default.int Q3TableItem::alignment() const{ bool num; bool ok1 = FALSE, ok2 = FALSE; ( void ) txt.toInt( &ok1 ); if ( !ok1 ) ( void ) txt.toDouble( &ok2 ); num = ok1 || ok2; return ( num ? Qt::AlignLeft : Qt::AlignLeft ) | Qt::AlignVCenter;}*/QgsSpit::QgsSpit( QWidget *parent, Qt::WFlags fl ) : QDialog( parent, fl ){ setupUi(this); QPixmap icon; icon = QPixmap( spitIcon ); setIcon( icon ); // Set up the table column headers tblShapefiles->setColumnCount(5); QStringList headerText; headerText << tr("File Name") << tr("Feature Class") << tr("Features") << tr("DB Relation Name") << tr("Schema"); tblShapefiles->setHorizontalHeaderLabels(headerText); tblShapefiles->verticalHeader()->hide(); tblShapefiles->horizontalHeader()->setStretchLastSection(true); populateConnectionList(); defSrid = -1; defGeom = "the_geom"; total_features = 0; chkUseDefaultSrid->setChecked( true ); chkUseDefaultGeom->setChecked( true ); useDefaultSrid(); useDefaultGeom(); txtPrimaryKeyName->setText("gid"); schema_list << "public"; gl_key = "/PostgreSQL/connections/"; getSchema(); // Install a delegate that provides the combo box widget for // changing the schema (but there can only be one delegate per // table, so it also provides edit widgets for the textual columns). // This needs to be done after the call to getSchema() so that // schema_list is populated. ShapefileTableDelegate* delegate = new ShapefileTableDelegate(tblShapefiles, schema_list); tblShapefiles->setItemDelegate(delegate); // Now that everything is in the table, adjust the column sizes tblShapefiles->resizeColumnsToContents();}QgsSpit::~QgsSpit(){}void QgsSpit::populateConnectionList(){ QSettings settings; QStringList keys = settings.subkeyList( "/PostgreSQL/connections" ); QStringList::Iterator it = keys.begin(); cmbConnections->clear(); while ( it != keys.end() ) { cmbConnections->insertItem( *it ); ++it; }}void QgsSpit::newConnection(){ QgsConnectionDialog * con = new QgsConnectionDialog( this, tr("New Connection") ); if ( con->exec() ) { populateConnectionList(); getSchema(); } delete con;}void QgsSpit::editConnection(){ QgsConnectionDialog * con = new QgsConnectionDialog( this, cmbConnections->currentText() ); if ( con->exec() ) { con->saveConnection(); getSchema(); } delete con;}void QgsSpit::removeConnection(){ QSettings settings; QString key = "/PostgreSQL/connections/" + cmbConnections->currentText(); QString msg = tr("Are you sure you want to remove the [") + cmbConnections->currentText() + tr("] connection and all associated settings?"); QMessageBox::StandardButton result = QMessageBox::information( this, tr("Confirm Delete"), msg, QMessageBox::Ok | QMessageBox::Cancel); if ( result == QMessageBox::Ok ) { settings.removeEntry( key + "/host" ); settings.removeEntry( key + "/database" ); settings.removeEntry( key + "/port" ); settings.removeEntry( key + "/username" ); settings.removeEntry( key + "/password" ); settings.removeEntry( key + "/save" ); cmbConnections->removeItem( cmbConnections->currentItem() ); getSchema(); }}void QgsSpit::addFile(){ QString error1 = ""; QString error2 = ""; bool exist; bool is_error = false; QSettings settings; QgsEncodingFileDialog dlg(this, tr("Add Shapefiles"), settings.readEntry( "/Plugin-Spit/last_directory" ), tr("Shapefiles (*.shp);;All files (*.*)"), settings.readEntry( "/Plugin-Spit/last_encoding" ) ); dlg.setMode(QFileDialog::ExistingFiles); if (dlg.exec() != QDialog::Accepted) return; QStringList files = dlg.selectedFiles(); if ( files.size() > 0 ) { // Save the directory for future use QFileInfo fi( files[ 0 ] ); settings.writeEntry( "/Plugin-Spit/last_directory", fi.dirPath( true ) ); settings.writeEntry( "/Plugin-Spit/last_encoding", dlg.encoding()); } // Process the files for ( QStringList::Iterator it = files.begin(); it != files.end(); ++it ) { exist = false; is_error = false; // Check to ensure that we don't insert the same file twice QList<QTableWidgetItem*> items = tblShapefiles->findItems(*it, Qt::MatchExactly); if (items.count() > 0) { exist = true; } if ( !exist ) { // check other files: file.dbf and file.shx QString name = *it; if ( !QFile::exists( name.left( name.length() - 3 ) + "dbf" ) ) { is_error = true; } else if ( !QFile::exists( name.left( name.length() - 3 ) + "shx" ) ) { is_error = true; } if ( !is_error ) { QgsShapeFile * file = new QgsShapeFile( name, dlg.encoding() ); if ( file->is_valid() ) { /* XXX getFeatureClass actually does a whole bunch * of things and is probably better named * something else */ QString featureClass = file->getFeatureClass(); fileList.push_back( file ); QTableWidgetItem *filenameItem = new QTableWidgetItem( name ); QTableWidgetItem *featureClassItem = new QTableWidgetItem( featureClass ); QTableWidgetItem *featureCountItem = new QTableWidgetItem( QString( "%1" ).arg( file->getFeatureCount() ) ); // Sanitize the relation name to make it pg friendly QString relName = file->getTable().replace(QRegExp("\\s"), "_"); QTableWidgetItem *dbRelationNameItem = new QTableWidgetItem( relName ); QTableWidgetItem *dbSchemaNameItem = new QTableWidgetItem( cmbSchema->currentText() ); // All items are editable except for these two filenameItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); featureCountItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); int row = tblShapefiles->rowCount(); tblShapefiles->insertRow( row ); tblShapefiles->setItem( row, ColFILENAME, filenameItem ); tblShapefiles->setItem( row, ColFEATURECLASS, featureClassItem ); tblShapefiles->setItem( row, ColFEATURECOUNT, featureCountItem ); tblShapefiles->setItem( row, ColDBRELATIONNAME, dbRelationNameItem ); tblShapefiles->setItem( row, ColDBSCHEMA, dbSchemaNameItem ); total_features += file->getFeatureCount(); } else { error1 += name + "\n"; is_error = true; delete file; } } else { error2 += name + "\n"; } } } tblShapefiles->resizeColumnsToContents(); if ( error1 != "" || error2 != "" ) { QString message = tr("The following Shapefile(s) could not be loaded:\n\n"); if ( error1 != "" ) { error1 += "----------------------------------------------------------------------------------------"; error1 += "\n" + tr("REASON: File cannot be opened") + "\n\n"; } if ( error2 != "" ) { error2 += "----------------------------------------------------------------------------------------"; error2 += "\n" + tr("REASON: One or both of the Shapefile files (*.dbf, *.shx) missing") + "\n\n"; } QgsMessageViewer * e = new QgsMessageViewer( this ); e->setMessageAsPlainText( message + error1 + error2 ); e->exec(); // deletes itself on close }}void QgsSpit::removeFile(){ std::vector <int> temp; for ( int n = 0; n < tblShapefiles->rowCount(); n++ ) if ( tblShapefiles->isItemSelected( tblShapefiles->item( n, 0 ) ) ) { for ( std::vector<QgsShapeFile *>::iterator vit = fileList.begin(); vit != fileList.end(); vit++ ) { if ( ( *vit ) ->getName() == tblShapefiles->item( n, 0 )->text() ) { total_features -= ( *vit ) ->getFeatureCount(); fileList.erase( vit ); temp.push_back( n ); break; } } } for ( int i = temp.size()-1; i >= 0; --i ) tblShapefiles->removeRow( temp[ i ] ); QList<QTableWidgetItem*> selected = tblShapefiles->selectedItems(); for (int i = 0; i < selected.count(); ++i) selected[i]->setSelected(false);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -