Skip to content

Commit 6b8358d

Browse files
author
Jim Hamill
committed
Various updates
1 parent 49b2027 commit 6b8358d

14 files changed

+303
-43
lines changed

BrowseWidget.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include <QMessageBox>
1212

13+
#include "ExportSelectionDialog.h"
14+
1315
BrowseWidget::BrowseWidget(QWidget *parent) :
1416
QWidget(parent),
1517
m_model(NULL),
@@ -57,10 +59,11 @@ bool BrowseWidget::setTable(QString table)
5759
ui->filterEdit->setEnabled(true);
5860
ui->filterButton->setEnabled(true);
5961

60-
// Enable Add/Remove/Clear
62+
// Enable Add/Remove/Clear/SaveAs
6163
ui->addButton->setEnabled(true);
6264
ui->removeButton->setEnabled(true);
6365
ui->clearButton->setEnabled(true);
66+
ui->saveAsButton->setEnabled(true);
6467

6568
return true;
6669
}
@@ -147,3 +150,15 @@ void BrowseWidget::currentChanged(QModelIndex previous, QModelIndex current)
147150
m_model->setEditStrategy(QSqlTableModel::OnFieldChange);
148151
}
149152
}
153+
154+
void BrowseWidget::on_saveAsButton_clicked()
155+
{
156+
// Create export selection dialog
157+
ExportSelectionDialog exportSelectionDialog;
158+
159+
// Initialise the dialog
160+
exportSelectionDialog.init(ui->tableView);
161+
162+
// Execute the dialog
163+
exportSelectionDialog.exec();
164+
}

BrowseWidget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ private slots:
3434

3535
void commitChanges();
3636

37+
void on_saveAsButton_clicked();
38+
3739
private:
3840
Ui::BrowseWidget *ui;
3941

BrowseWidget.ui

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<string>Form</string>
1515
</property>
1616
<layout class="QGridLayout" name="gridLayout">
17-
<item row="0" column="0" colspan="4">
17+
<item row="0" column="0" colspan="6">
1818
<layout class="QHBoxLayout" name="horizontalLayout">
1919
<item>
2020
<widget class="QLabel" name="label">
@@ -65,29 +65,14 @@
6565
</item>
6666
</layout>
6767
</item>
68-
<item row="1" column="0" colspan="4">
68+
<item row="1" column="0" colspan="6">
6969
<widget class="QTableView" name="tableView">
7070
<property name="enabled">
7171
<bool>false</bool>
7272
</property>
7373
</widget>
7474
</item>
75-
<item row="2" column="0">
76-
<widget class="QPushButton" name="clearButton">
77-
<property name="enabled">
78-
<bool>false</bool>
79-
</property>
80-
<property name="text">
81-
<string>Clear</string>
82-
</property>
83-
<property name="icon">
84-
<iconset theme="edit-clear">
85-
<normaloff/>
86-
</iconset>
87-
</property>
88-
</widget>
89-
</item>
90-
<item row="2" column="1">
75+
<item row="2" column="3">
9176
<spacer name="horizontalSpacer">
9277
<property name="orientation">
9378
<enum>Qt::Horizontal</enum>
@@ -100,7 +85,7 @@
10085
</property>
10186
</spacer>
10287
</item>
103-
<item row="2" column="2">
88+
<item row="2" column="4">
10489
<widget class="QPushButton" name="addButton">
10590
<property name="enabled">
10691
<bool>false</bool>
@@ -115,7 +100,7 @@
115100
</property>
116101
</widget>
117102
</item>
118-
<item row="2" column="3">
103+
<item row="2" column="5">
119104
<widget class="QPushButton" name="removeButton">
120105
<property name="enabled">
121106
<bool>false</bool>
@@ -130,6 +115,34 @@
130115
</property>
131116
</widget>
132117
</item>
118+
<item row="2" column="1">
119+
<widget class="QPushButton" name="saveAsButton">
120+
<property name="enabled">
121+
<bool>false</bool>
122+
</property>
123+
<property name="text">
124+
<string>Export Table</string>
125+
</property>
126+
<property name="icon">
127+
<iconset theme="filesave"/>
128+
</property>
129+
</widget>
130+
</item>
131+
<item row="2" column="2">
132+
<widget class="QPushButton" name="clearButton">
133+
<property name="enabled">
134+
<bool>false</bool>
135+
</property>
136+
<property name="text">
137+
<string>Clear</string>
138+
</property>
139+
<property name="icon">
140+
<iconset theme="edit-clear">
141+
<normaloff/>
142+
</iconset>
143+
</property>
144+
</widget>
145+
</item>
133146
</layout>
134147
</widget>
135148
<resources/>

DatabaseConnectionWidget.cpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,34 +47,22 @@ QSqlError DatabaseConnectionWidget::lastError()
4747
return m_database.lastError();
4848
}
4949

50-
bool DatabaseConnectionWidget::createSshTunnel(QString hostname, int remotePort, int localPort)
50+
bool DatabaseConnectionWidget::createSshTunnel(QString hostname, int remotePort, int forwardedPort, int sshPort)
5151
{
5252
// Compile Arguments (of the form: -fNg -L 16111:127.0.0.1:3306 [email protected]
5353
QStringList arguments;
54-
arguments << "-fNg" << "-L" << QString(QString::number(localPort) + ":" + "127.0.0.1" + ":" + QString::number(remotePort)) << QString(hostname);
54+
arguments << "-Ng" << "-L" << QString(QString::number(forwardedPort) + ":" + "127.0.0.1" + ":" + QString::number(remotePort)) << QString(hostname) << "-p" << QString::number(sshPort);
5555

5656
// Execute SSH Command
57-
//m_sshTunnel.start("ssh", arguments);
58-
QProcess::startDetached("ssh", arguments);
57+
m_sshTunnel.start("ssh", arguments);
5958

60-
for (int i = 0; i < 500000; i++) {
61-
qDebug() << i;
62-
}
63-
64-
/*// Wait until finished
59+
// Wait until finished
6560
if (!m_sshTunnel.waitForStarted(5000)) {
6661
return false;
6762
}
6863

69-
// If SSH returned an error
70-
if (m_sshTunnel.exitCode()) {
71-
qDebug() << m_sshTunnel.exitCode() << m_sshTunnel.readAll();
72-
return false;
73-
}
74-
7564
// Print useful information
76-
qDebug() << m_sshTunnel.exitCode() << m_sshTunnel.readAllStandardError();
77-
qDebug() << QString("SSH tunnel binded to 127.0.0.1:" + QString::number(localPort));*/
65+
qDebug() << QString("SSH tunnel binded to 127.0.0.1:" + QString::number(forwardedPort));
7866

7967
return true;
8068
}

DatabaseConnectionWidget.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class DatabaseConnectionWidget : public QWidget
2424

2525
QString name();
2626

27-
bool createSshTunnel(QString hostname, int remotePort, int localPort);
27+
bool createSshTunnel(QString hostname, int remotePort, int forwardedPort, int sshPort = 22);
2828

2929
bool connectToDatabase(QString name, QString driver, QString host, QString database, QString username, QString password, int port = 0);
3030

ExportSelectionDialog.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "ExportSelectionDialog.h"
2+
#include "ui_ExportSelectionDialog.h"
3+
4+
#include <QFile>
5+
#include <QFileDialog>
6+
#include <QTextStream>
7+
8+
ExportSelectionDialog::ExportSelectionDialog(QWidget *parent) :
9+
QDialog(parent),
10+
ui(new Ui::ExportSelectionDialog)
11+
{
12+
ui->setupUi(this);
13+
14+
ui->formatCombo->addItem("CSV");
15+
}
16+
17+
ExportSelectionDialog::~ExportSelectionDialog()
18+
{
19+
delete ui;
20+
}
21+
22+
void ExportSelectionDialog::init(QTableView *tableView)
23+
{
24+
m_tableView = tableView;
25+
}
26+
27+
void ExportSelectionDialog::on_buttonBox_accepted()
28+
{
29+
// Show dialog and get filename
30+
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), QDir::homePath(), tr("CSV Files (*.csv)"));
31+
32+
// If the user chose a file to save to
33+
if (fileName.length()) {
34+
35+
// Create file handle
36+
QFile file(fileName);
37+
38+
// If we could successfully open the file
39+
if (file.open(QFile::WriteOnly | QFile::Truncate))
40+
{
41+
QTextStream data(&file);
42+
QStringList strList;
43+
44+
// Cycle through each row
45+
for( int row = 0; row < m_tableView->model()->rowCount(); row++ )
46+
{
47+
strList.clear();
48+
49+
// Cycle through each column
50+
for( int column = 0; column < m_tableView->model()->columnCount(); column++ )
51+
{
52+
QModelIndex index = m_tableView->model()->index(row, column, QModelIndex());
53+
strList << "\" " + m_tableView->model()->data(index).toString() + "\" ";
54+
}
55+
data << strList.join( "," )+"\n";
56+
}
57+
58+
// Close file handle
59+
file.close();
60+
}
61+
}
62+
}

ExportSelectionDialog.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef EXPORTSELECTIONDIALOG_H
2+
#define EXPORTSELECTIONDIALOG_H
3+
4+
#include <QDialog>
5+
#include <QTableView>
6+
7+
namespace Ui {
8+
class ExportSelectionDialog;
9+
}
10+
11+
class ExportSelectionDialog : public QDialog
12+
{
13+
Q_OBJECT
14+
15+
public:
16+
explicit ExportSelectionDialog(QWidget *parent = 0);
17+
~ExportSelectionDialog();
18+
19+
void init(QTableView *tableView);
20+
21+
private slots:
22+
void on_buttonBox_accepted();
23+
24+
private:
25+
Ui::ExportSelectionDialog *ui;
26+
27+
QTableView *m_tableView;
28+
};
29+
30+
#endif // EXPORTSELECTIONDIALOG_H

ExportSelectionDialog.ui

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>ExportSelectionDialog</class>
4+
<widget class="QDialog" name="ExportSelectionDialog">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>262</width>
10+
<height>109</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>Dialog</string>
15+
</property>
16+
<layout class="QGridLayout" name="gridLayout_2">
17+
<item row="0" column="0">
18+
<widget class="QGroupBox" name="groupBox">
19+
<property name="title">
20+
<string>Export to</string>
21+
</property>
22+
<layout class="QGridLayout" name="gridLayout">
23+
<item row="0" column="0">
24+
<widget class="QLabel" name="label">
25+
<property name="text">
26+
<string>Export Format:</string>
27+
</property>
28+
</widget>
29+
</item>
30+
<item row="0" column="1">
31+
<widget class="QComboBox" name="formatCombo">
32+
<property name="currentText">
33+
<string/>
34+
</property>
35+
</widget>
36+
</item>
37+
</layout>
38+
</widget>
39+
</item>
40+
<item row="1" column="0">
41+
<widget class="QDialogButtonBox" name="buttonBox">
42+
<property name="orientation">
43+
<enum>Qt::Horizontal</enum>
44+
</property>
45+
<property name="standardButtons">
46+
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Save</set>
47+
</property>
48+
</widget>
49+
</item>
50+
</layout>
51+
</widget>
52+
<resources/>
53+
<connections>
54+
<connection>
55+
<sender>buttonBox</sender>
56+
<signal>accepted()</signal>
57+
<receiver>ExportSelectionDialog</receiver>
58+
<slot>accept()</slot>
59+
<hints>
60+
<hint type="sourcelabel">
61+
<x>248</x>
62+
<y>254</y>
63+
</hint>
64+
<hint type="destinationlabel">
65+
<x>157</x>
66+
<y>274</y>
67+
</hint>
68+
</hints>
69+
</connection>
70+
<connection>
71+
<sender>buttonBox</sender>
72+
<signal>rejected()</signal>
73+
<receiver>ExportSelectionDialog</receiver>
74+
<slot>reject()</slot>
75+
<hints>
76+
<hint type="sourcelabel">
77+
<x>316</x>
78+
<y>260</y>
79+
</hint>
80+
<hint type="destinationlabel">
81+
<x>286</x>
82+
<y>274</y>
83+
</hint>
84+
</hints>
85+
</connection>
86+
</connections>
87+
</ui>

0 commit comments

Comments
 (0)