Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "qgsdelimitedtextplugingui.h"
#include "qgsdelimitedtextsourceselect.h"

#include "qgisinterface.h"
#include "qgscontexthelp.h"
#include "qgslogger.h"

#include "qgsdelimitedtextprovider.h"

#include <QFile>
#include <QFileDialog>
#include <QFileInfo>
Expand All @@ -26,15 +28,19 @@
#include <QTextStream>
#include <QUrl>

QgsDelimitedTextPluginGui::QgsDelimitedTextPluginGui( QgisInterface * _qI, QWidget * parent, Qt::WFlags fl )
: QDialog( parent, fl ), qI( _qI )
QgsDelimitedTextSourceSelect::QgsDelimitedTextSourceSelect( QWidget * parent, Qt::WFlags fl, bool embedded ):
QDialog( parent, fl )
{
setupUi( this );

QSettings settings;
restoreGeometry( settings.value( "/Plugin-DelimitedText/geometry" ).toByteArray() );

pbnOK = buttonBox->button( QDialogButtonBox::Ok );
if ( embedded )
{
buttonBox->button( QDialogButtonBox::Cancel )->hide();
buttonBox->button( QDialogButtonBox::Ok )->hide();
}

updateFieldsAndEnable();

Expand Down Expand Up @@ -89,18 +95,18 @@ QgsDelimitedTextPluginGui::QgsDelimitedTextPluginGui( QgisInterface * _qI, QWidg
connect( rowCounter, SIGNAL( valueChanged( int ) ), this, SLOT( updateFieldsAndEnable() ) );
}

QgsDelimitedTextPluginGui::~QgsDelimitedTextPluginGui()
QgsDelimitedTextSourceSelect::~QgsDelimitedTextSourceSelect()
{
QSettings settings;
settings.setValue( "/Plugin-DelimitedText/geometry", saveGeometry() );
}

void QgsDelimitedTextPluginGui::on_btnBrowseForFile_clicked()
void QgsDelimitedTextSourceSelect::on_btnBrowseForFile_clicked()
{
getOpenFileName();
}

void QgsDelimitedTextPluginGui::on_buttonBox_accepted()
void QgsDelimitedTextSourceSelect::on_buttonBox_accepted()
{
if ( !txtLayerName->text().isEmpty() )
{
Expand Down Expand Up @@ -143,7 +149,7 @@ void QgsDelimitedTextPluginGui::on_buttonBox_accepted()
url.addQueryItem( "skipLines", QString( "%1" ).arg( skipLines ) );

// add the layer to the map
emit drawVectorLayer( QString::fromAscii( url.toEncoded() ), txtLayerName->text(), "delimitedtext" );
emit addVectorLayer( QString::fromAscii( url.toEncoded() ), txtLayerName->text(), "delimitedtext" );

// store the settings
QSettings settings;
Expand All @@ -170,12 +176,12 @@ void QgsDelimitedTextPluginGui::on_buttonBox_accepted()
}
}

void QgsDelimitedTextPluginGui::on_buttonBox_rejected()
void QgsDelimitedTextSourceSelect::on_buttonBox_rejected()
{
reject();
}

QString QgsDelimitedTextPluginGui::selectedChars()
QString QgsDelimitedTextSourceSelect::selectedChars()
{
QString chars = "";
if ( cbxDelimSpace->isChecked() )
Expand All @@ -191,36 +197,27 @@ QString QgsDelimitedTextPluginGui::selectedChars()
return chars;
}

QStringList QgsDelimitedTextPluginGui::splitLine( QString line )
QStringList QgsDelimitedTextSourceSelect::splitLine( QString line )
{
QStringList fieldList;
QString delimiter = txtDelimiter->text();

if ( delimiterPlain->isChecked() )
{
// convert \t to tabulator
delimiter = txtDelimiter->text();
delimiter.replace( "\\t", "\t" );
fieldList = line.split( delimiter );
return QgsDelimitedTextProvider::splitLine( line, "plain", delimiter );
}
else if ( delimiterSelection->isChecked() )

if ( delimiterSelection->isChecked() )
{
delimiter = "[";
delimiter += selectedChars();
delimiter += "]";
txtDelimiter->setText( delimiter );
fieldList = line.split( QRegExp( delimiter ) );
}
else
{
QRegExp del( delimiter );
fieldList = line.split( QRegExp( delimiter ) );
}

return fieldList;
return QgsDelimitedTextProvider::splitLine( line, "regexp", delimiter );
}

bool QgsDelimitedTextPluginGui::haveValidFileAndDelimiters()
bool QgsDelimitedTextSourceSelect::haveValidFileAndDelimiters()
{

bool valid = true;
Expand All @@ -245,7 +242,7 @@ bool QgsDelimitedTextPluginGui::haveValidFileAndDelimiters()
return valid;
}

void QgsDelimitedTextPluginGui::updateFieldLists()
void QgsDelimitedTextSourceSelect::updateFieldLists()
{
// Update the x and y field dropdown boxes
QgsDebugMsg( "Updating field lists" );
Expand Down Expand Up @@ -288,7 +285,7 @@ void QgsDelimitedTextPluginGui::updateFieldLists()
QString line;
do
{
line = readLine( stream ); // line of text excluding '\n'
line = QgsDelimitedTextProvider::readLine( &stream ); // line of text excluding '\n'
}
while ( !line.isEmpty() && skipLines-- > 0 );

Expand Down Expand Up @@ -403,7 +400,7 @@ void QgsDelimitedTextPluginGui::updateFieldLists()

// put a few more lines into the sample box
int counter = 0;
line = readLine( stream );
line = QgsDelimitedTextProvider::readLine( &stream );
while ( !line.isEmpty() && counter < 20 )
{
QStringList values = splitLine( line );
Expand All @@ -430,7 +427,7 @@ void QgsDelimitedTextPluginGui::updateFieldLists()
}

counter++;
line = readLine( stream );
line = QgsDelimitedTextProvider::readLine( &stream );
}
// close the file
file.close();
Expand All @@ -440,7 +437,7 @@ void QgsDelimitedTextPluginGui::updateFieldLists()
txtLayerName->setText( finfo.completeBaseName() );
}

void QgsDelimitedTextPluginGui::getOpenFileName()
void QgsDelimitedTextSourceSelect::getOpenFileName()
{
// Get a file to process, starting at the current directory
// Set inital dir to last used
Expand All @@ -457,13 +454,13 @@ void QgsDelimitedTextPluginGui::getOpenFileName()
txtFilePath->setText( s );
}

void QgsDelimitedTextPluginGui::updateFieldsAndEnable()
void QgsDelimitedTextSourceSelect::updateFieldsAndEnable()
{
updateFieldLists();
enableAccept();
}

void QgsDelimitedTextPluginGui::enableAccept()
void QgsDelimitedTextSourceSelect::enableAccept()
{
// If the geometry type field is enabled then there must be
// a valid file, and it must be
Expand All @@ -481,27 +478,5 @@ void QgsDelimitedTextPluginGui::enableAccept()
}
}

pbnOK->setEnabled( enabled );
}

QString QgsDelimitedTextPluginGui::readLine( QTextStream &stream )
{
QString buffer;

while ( !stream.atEnd() )
{
QChar c = stream.read( 1 ).at( 0 );

if ( c == '\r' || c == '\n' )
{
if ( buffer.isEmpty() )
{
// skip leading CR / LF
continue;
}
break;
}
buffer.append( c );
}
return buffer;
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( enabled );
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,28 @@
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#ifndef PLUGINGUI_H
#define PLUGINGUI_H
#ifndef QGSDELIMITEDTEXTSOURCESELECT_H
#define QGSDELIMITEDTEXTSOURCESELECT_H

#include "ui_qgsdelimitedtextsourceselectbase.h"

#include "ui_qgsdelimitedtextpluginguibase.h"
#include <QTextStream>
#include "qgscontexthelp.h"
#include "qgisgui.h"

class QgisInterface;

/**
* \class QgsDelimitedTextPluginGui
* \class QgsDelimitedTextSourceSelect
*/
class QgsDelimitedTextPluginGui : public QDialog, private Ui::QgsDelimitedTextPluginGuiBase
class QgsDelimitedTextSourceSelect : public QDialog, private Ui::QgsDelimitedTextSourceSelectBase
{
Q_OBJECT

public:
QgsDelimitedTextPluginGui( QgisInterface * _qI, QWidget* parent = 0, Qt::WFlags fl = 0 );
~QgsDelimitedTextPluginGui();
QgsDelimitedTextSourceSelect( QWidget * parent, Qt::WFlags fl = QgisGui::ModalDialogFlags, bool embedded = false );
~QgsDelimitedTextSourceSelect();

QString readLine( QTextStream & stream );
QStringList splitLine( QString line );

private:
Expand All @@ -39,9 +40,6 @@ class QgsDelimitedTextPluginGui : public QDialog, private Ui::QgsDelimitedTextPl
void getOpenFileName();
QString selectedChars();

QgisInterface * qI;
QAbstractButton *pbnOK;

private slots:
void on_buttonBox_accepted();
void on_buttonBox_rejected();
Expand All @@ -53,8 +51,7 @@ class QgsDelimitedTextPluginGui : public QDialog, private Ui::QgsDelimitedTextPl
void enableAccept();

signals:
void drawRasterLayer( QString );
void drawVectorLayer( QString, QString, QString );
void addVectorLayer( QString, QString, QString );
};

#endif
#endif // QGSDELIMITEDTEXTSOURCESELECT_H
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>QgsDelimitedTextPluginGuiBase</class>
<widget class="QDialog" name="QgsDelimitedTextPluginGuiBase">
<class>QgsDelimitedTextSourceSelectBase</class>
<widget class="QDialog" name="QgsDelimitedTextSourceSelectBase">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>492</width>
<height>528</height>
<height>543</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -42,7 +42,7 @@
</widget>
</item>
<item row="0" column="1">
<widget class="QgsFileDropEdit" name="txtFilePath">
<widget class="QLineEdit" name="txtFilePath">
<property name="toolTip">
<string>Full path to the delimited text file</string>
</property>
Expand Down Expand Up @@ -462,13 +462,6 @@
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>QgsFileDropEdit</class>
<extends>QLineEdit</extends>
<header>qgsfiledropedit.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>txtFilePath</tabstop>
<tabstop>btnBrowseForFile</tabstop>
Expand All @@ -491,9 +484,7 @@
<tabstop>tblSample</tabstop>
<tabstop>buttonBox</tabstop>
</tabstops>
<resources>
<include location="delimited_text.qrc"/>
</resources>
<resources/>
<connections>
<connection>
<sender>delimiterSelection</sender>
Expand Down