QGIS/plugins/delimited_text/plugingui.cpp
gsherman 9e6fe39e1d Initial checkin of delmited text plugin files
git-svn-id: http://svn.osgeo.org/qgis/trunk@1005 c8812cc2-4d05-0410-92ff-de0c093fc19c
2004-03-10 23:16:52 +00:00

112 lines
3.6 KiB
C++

/***************************************************************************
* Copyright (C) 2004 by Gary Sherman *
* sherman at mrcc.com *
* *
* GUI for loading a delimited text file as a layer in QGIS *
* This plugin works in conjuction with the delimited text data *
* provider plugin *
* *
* 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. *
***************************************************************************/
#include <iostream>
#include <qfiledialog.h>
#include <qlineedit.h>
#include <qfile.h>
#include <qcombobox.h>
#include <qpushbutton.h>
#include "plugingui.h"
PluginGui::PluginGui() : PluginGuiBase()
{
}
PluginGui::PluginGui( QWidget* parent , const char* name , bool modal , WFlags fl )
: PluginGuiBase( parent, name, modal, fl )
{
}
PluginGui::~PluginGui()
{
}
void PluginGui::pbnOK_clicked()
{
//Build the delimited text URI from the user provided information
QString uri = QString("%1?delimiter=%2&xField=%3&yField=%4")
.arg(txtFilePath->text())
.arg(txtDelimiter->text())
.arg(cmbXField->currentText())
.arg(cmbYField->currentText());
std::cerr << "Adding layer using " << uri << std::endl;
// add the layer to the map
emit drawVectorLayer(uri,QString("layername"),"delimitedtext");
}
void PluginGui::updateFieldLists()
{
// Update the x and y field dropdown boxes
#ifdef QGISDEBUG
std::cerr << "Updating field lists" << std::endl;
#endif
// open the file
if(QFile::exists(txtFilePath->text())){
QFile *file = new QFile(txtFilePath->text());
if ( file->open( IO_ReadOnly ) ) {
QTextStream stream( file );
QString line;
line = stream.readLine(); // line of text excluding '\n'
#ifdef QGISDEBUG
std::cerr << "Attempting to split the input line: " << line <<
" using delimiter " << txtDelimiter->text() << std::endl;
#endif
QStringList fieldList = QStringList::split(txtDelimiter->text(), line);
#ifdef QGISDEBUG
std::cerr << "Split line into " << fieldList.size() << " parts" << std::endl;
#endif
//
// We don't know anything about a text based field other
// than its name. All fields are assumed to be text
int fieldPos = 0;
for ( QStringList::Iterator it = fieldList.begin(); it != fieldList.end(); ++it ) {
// add item to both drop-downs (X field and Y field)
cmbXField->insertItem(*it);
cmbYField->insertItem(*it);
}
// close the file
file->close();
}
}
}
void PluginGui::getOpenFileName()
{
// Get a file to process, starting at the current directory
QString s = QFileDialog::getOpenFileName(
"./",
"Text files (*.txt)",
0,
"open file dialog",
"Choose a delimited text file to open" );
// set path
txtFilePath->setText(s);
// update the field drop-down boxes by parsing and splitting
// the header row
updateFieldLists();
}
void PluginGui::enableBrowseButton(const QString &delimiter)
{
// If a delimiter has entered, enable the browse button,
// otherwise disable it.
btnBrowseForFile->setEnabled(delimiter.length() > 0);
}