Added text file preview to the plugin and changed the logic so the delimiter doesn't have to be entered first.

git-svn-id: http://svn.osgeo.org/qgis/trunk@2342 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
gsherman 2004-11-26 22:00:44 +00:00
parent ae7a3a4bf3
commit f77150b2ad
3 changed files with 128 additions and 69 deletions

View File

@ -14,12 +14,14 @@
#include <iostream>
#include <qfiledialog.h>
#include <qlineedit.h>
#include <qtextedit.h>
#include <qfile.h>
#include <qcombobox.h>
#include <qpushbutton.h>
#include <qsettings.h>
#include <qfileinfo.h>
#include <qregexp.h>
#include <qmessagebox.h>
#include "qgsdelimitedtextplugingui.h"
#include "../../src/qgisiface.h"
@ -43,22 +45,29 @@ QgsDelimitedTextPluginGui::~QgsDelimitedTextPluginGui()
void QgsDelimitedTextPluginGui::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,txtLayerName->text(),"delimitedtext");
// store the settings
if(txtLayerName->text().length() > 0)
{
//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,txtLayerName->text(),"delimitedtext");
// store the settings
QSettings settings;
QString key = "/Qgis/delimited_text_plugin";
settings.writeEntry(key + "/delimiter", txtDelimiter->text());
QFileInfo fi(txtFilePath->text());
settings.writeEntry(key + "/text_path", fi.dirPath());
QSettings settings;
QString key = "/Qgis/delimited_text_plugin";
settings.writeEntry(key + "/delimiter", txtDelimiter->text());
QFileInfo fi(txtFilePath->text());
settings.writeEntry(key + "/text_path", fi.dirPath());
}
else
{
QMessageBox::warning(this, tr("No layer name"), tr("Please enter a layer name before adding the layer to the map"));
}
}
void QgsDelimitedTextPluginGui::updateFieldLists()
@ -78,28 +87,48 @@ void QgsDelimitedTextPluginGui::updateFieldLists()
QTextStream stream( file );
QString line;
line = stream.readLine(); // line of text excluding '\n'
if(txtDelimiter->text().length() > 0)
{
#ifdef QGISDEBUG
std::cerr << "Attempting to split the input line: " << line <<
" using delimiter " << txtDelimiter->text() << std::endl;
std::cerr << "Attempting to split the input line: " << line <<
" using delimiter " << txtDelimiter->text() << std::endl;
#endif
QStringList fieldList = QStringList::split(QRegExp(txtDelimiter->text()), line);
QStringList fieldList = QStringList::split(QRegExp(txtDelimiter->text()), line);
#ifdef QGISDEBUG
std::cerr << "Split line into " << fieldList.size() << " parts" << std::endl;
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)
if((*it).length() > 0)
{
cmbXField->insertItem(*it);
cmbYField->insertItem(*it);
}
}
//
// 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)
if((*it).length() > 0)
{
cmbXField->insertItem(*it);
cmbYField->insertItem(*it);
}
}
// enable the buttons
enableButtons();
}
else
{
QMessageBox::warning(this, tr("No delimiter"),tr("Please specify a delimiter prior to parsing the file"));
}
// clear the sample text box
txtSample->clear();
// put the header row in the sample box
txtSample->insert(line + "\n");
// put a few more lines into the sample box
int counter = 0;
while((line=stream.readLine()) && (counter < 20))
{
txtSample->insert(line + "\n");
counter++;
}
// close the file
file->close();
}
@ -112,13 +141,13 @@ void QgsDelimitedTextPluginGui::getOpenFileName()
// Get a file to process, starting at the current directory
// Set inital dir to last used
QSettings settings;
QString s = QFileDialog::getOpenFileName(
settings.readEntry("/Qgis/delimited_text_plugin/text_path","./"),
settings.readEntry("/Qgis/delimited_text_plugin/text_path","./"),
"Text files (*.txt)",
0,
"open file dialog",
"Choose a delimited text file to open" );
tr("Choose a delimited text file to open") );
// set path
txtFilePath->setText(s);
@ -126,13 +155,12 @@ void QgsDelimitedTextPluginGui::getOpenFileName()
// the header row
updateFieldLists();
}
void QgsDelimitedTextPluginGui::enableBrowseButton(const QString &delimiter)
void QgsDelimitedTextPluginGui::enableButtons()
{
// If a delimiter has entered, enable the browse button,
// otherwise disable it.
btnBrowseForFile->setEnabled(delimiter.length() > 0);
pbnParse->setEnabled(txtDelimiter->text().length() > 0 && txtFilePath->text().length() > 0);
pbnOK->setEnabled(txtDelimiter->text().length() > 0 && txtFilePath->text().length() > 0);
}
void QgsDelimitedTextPluginGui::help()
{
qI->openURL("plugins/delimited_text/index.html",true);
qI->openURL("plugins/delimited_text/index.html",true);
}

View File

@ -30,7 +30,7 @@ class QgsDelimitedTextPluginGui : public QgsDelimitedTextPluginGuiBase
void pbnOK_clicked();
void updateFieldLists();
void getOpenFileName();
void enableBrowseButton(const QString &);
void enableButtons();
void help();
private:

View File

@ -8,8 +8,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>600</width>
<height>398</height>
<width>577</width>
<height>516</height>
</rect>
</property>
<property name="paletteBackgroundColor">
@ -37,7 +37,7 @@
<string>Cancel</string>
</property>
</widget>
<widget class="QLabel" row="0" column="0" rowspan="6" colspan="1">
<widget class="QLabel" row="0" column="0" rowspan="8" colspan="2">
<property name="name">
<cstring>pixmapLabel1</cstring>
</property>
@ -48,7 +48,7 @@
<bool>true</bool>
</property>
</widget>
<widget class="QLayoutWidget" row="5" column="3">
<widget class="QLayoutWidget" row="7" column="2">
<property name="name">
<cstring>layout5</cstring>
</property>
@ -143,7 +143,21 @@
</widget>
</hbox>
</widget>
<widget class="QLayoutWidget" row="4" column="3">
<widget class="Line" row="1" column="1" rowspan="7" colspan="1">
<property name="name">
<cstring>line1</cstring>
</property>
<property name="frameShape">
<enum>VLine</enum>
</property>
<property name="frameShadow">
<enum>Sunken</enum>
</property>
<property name="orientation">
<enum>Vertical</enum>
</property>
</widget>
<widget class="QLayoutWidget" row="5" column="2">
<property name="name">
<cstring>layout6</cstring>
</property>
@ -225,16 +239,47 @@
</widget>
</hbox>
</widget>
<widget class="QTextEdit" row="1" column="3">
<widget class="QGroupBox" row="6" column="2">
<property name="name">
<cstring>groupBox1</cstring>
</property>
<property name="title">
<string>Sample text</string>
</property>
<grid>
<property name="name">
<cstring>unnamed</cstring>
</property>
<widget class="QTextEdit" row="0" column="0">
<property name="name">
<cstring>txtSample</cstring>
</property>
<property name="font">
<font>
<family>Courier New</family>
</font>
</property>
<property name="textFormat">
<enum>PlainText</enum>
</property>
<property name="wordWrap">
<enum>NoWrap</enum>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</grid>
</widget>
<widget class="QTextEdit" row="2" column="2">
<property name="name">
<cstring>teInstructions</cstring>
</property>
<property name="text">
<string>&lt;html&gt;&lt;head&gt;&lt;meta name="qrichtext" content="1" /&gt;&lt;/head&gt;&lt;body style="font-size:12pt;font-family:Arial"&gt;
&lt;p style="margin-top:16px"&gt;&lt;span style="font-size:18pt;font-weight:600"&gt;Description&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Select the file containing delimited text with lat/lon coordinates that you would like to use as a point layer and this plugin will do the job for you! &lt;/p&gt;
&lt;p&gt;Use the layer name box to specify the legend name for the new layer. Use the delimeter box to specify what the delimeter is for your file (e.g. space, commar or tab). Note this box accepts regex entries so use \t for tab and so on. After choosing a delimeter, press the parse button to see what field names have been detected in your file.&lt;/p&gt;
&lt;p&gt;Use the X and Y fields to specify which columns represent latitude and longitude..&lt;/p&gt;
&lt;p style="margin-top:16px"&gt;&lt;span style="font-size:16pt;font-weight:600"&gt;Description&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Select a delimited text file containing x and y coordinates that you would like to use as a point layer and this plugin will do the job for you! &lt;/p&gt;
&lt;p&gt;Use the layer name box to specify the legend name for the new layer. Use the delimiter box to specify what delimeter is used in your file (e.g. space, commar or tab). Note this box accepts regular expressions so use \t for tab and so on. After choosing a delimiter, press the parse button an select the columns containing the x and y values for the layer.&lt;/p&gt;
&lt;/body&gt;&lt;/html&gt;
</string>
</property>
@ -245,27 +290,13 @@
<bool>true</bool>
</property>
</widget>
<widget class="Line" row="0" column="1" rowspan="6" colspan="1">
<property name="name">
<cstring>line1</cstring>
</property>
<property name="frameShape">
<enum>VLine</enum>
</property>
<property name="frameShadow">
<enum>Sunken</enum>
</property>
<property name="orientation">
<enum>Vertical</enum>
</property>
</widget>
<widget class="QLabel" row="0" column="2" rowspan="1" colspan="2">
<widget class="QLabel" row="0" column="2" rowspan="2" colspan="1">
<property name="name">
<cstring>txtHeading</cstring>
</property>
<property name="font">
<font>
<pointsize>24</pointsize>
<pointsize>20</pointsize>
<bold>1</bold>
</font>
</property>
@ -276,7 +307,7 @@
<set>AlignCenter</set>
</property>
</widget>
<widget class="QLayoutWidget" row="3" column="3">
<widget class="QLayoutWidget" row="3" column="2">
<property name="name">
<cstring>layout3</cstring>
</property>
@ -311,7 +342,7 @@
<cstring>btnBrowseForFile</cstring>
</property>
<property name="enabled">
<bool>false</bool>
<bool>true</bool>
</property>
<property name="maximumSize">
<size>
@ -331,7 +362,7 @@
</widget>
</hbox>
</widget>
<widget class="QLayoutWidget" row="2" column="3">
<widget class="QLayoutWidget" row="4" column="2">
<property name="name">
<cstring>layout5</cstring>
</property>