From 6d8bc516acf7a5995736423e838c987d08e69c09 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 17 May 2011 14:56:28 +0000 Subject: Ok, you can now open files, save files, save files from proxies and clients, and add new root items to files. Later I'll add some actual editing capabilities, should be really easy. --- src/gatscon/clientwidget.cpp | 16 +++++++++++ src/gatscon/clientwidget.h | 5 +++- src/gatscon/filewidget.cpp | 33 ++++++++++++++++++++++ src/gatscon/filewidget.h | 9 +++++- src/gatscon/filewidget.ui | 59 ++++++++++++++++++++++++++++++++++++-- src/gatscon/iobase.cpp | 1 + src/gatscon/iobase.h | 10 +++++++ src/gatscon/mainwnd.cpp | 59 ++++++++++++++++++++++++++++++++++++++ src/gatscon/mainwnd.h | 6 ++++ src/gatscon/mainwnd.ui | 67 ++++++++++++++++++++++++++++++++++++++++++++ src/gatscon/proxywidget.cpp | 16 +++++++++++ src/gatscon/proxywidget.h | 5 +++- src/gatscon/treetogats.cpp | 52 ++++++++++++++++++++++++++++++++++ src/gatscon/treetogats.h | 13 +++++++++ 14 files changed, 346 insertions(+), 5 deletions(-) create mode 100644 src/gatscon/iobase.cpp create mode 100644 src/gatscon/iobase.h create mode 100644 src/gatscon/treetogats.cpp create mode 100644 src/gatscon/treetogats.h (limited to 'src/gatscon') diff --git a/src/gatscon/clientwidget.cpp b/src/gatscon/clientwidget.cpp index 0ea04a3..c9cb4c1 100644 --- a/src/gatscon/clientwidget.cpp +++ b/src/gatscon/clientwidget.cpp @@ -2,11 +2,14 @@ #include "clientthread.h" #include "gatstotree.h" +#include "treetogats.h" #include +#include #include #include +#include using namespace Bu; @@ -27,6 +30,19 @@ ClientWidget::~ClientWidget() { } +void ClientWidget::saveTo( const QString &sFile ) +{ + File fOut( sFile.toAscii().constData(), File::WriteNew ); + Gats::GatsStream gsOut( fOut ); + QTreeWidgetItem *pRoot = twHistory->invisibleRootItem(); + for( int j = 0; j < pRoot->childCount(); j++ ) + { + Gats::Object *pObj = treeToGats( pRoot->child( j ) ); + gsOut.writeObject( pObj ); + delete pObj; + } +} + void ClientWidget::send() { try diff --git a/src/gatscon/clientwidget.h b/src/gatscon/clientwidget.h index 9ce2798..5ff18fb 100644 --- a/src/gatscon/clientwidget.h +++ b/src/gatscon/clientwidget.h @@ -2,19 +2,22 @@ #define CLIENT_WIDGET_H #include "ui_clientwidget.h" +#include "iobase.h" namespace Gats { class Object; }; -class ClientWidget : public QWidget, protected Ui::ClientWidget +class ClientWidget : public QWidget, protected Ui::ClientWidget, public IoBase { Q_OBJECT; public: ClientWidget( QWidget *pParent, const QByteArray &baHost, int iPort ); virtual ~ClientWidget(); + virtual void saveTo( const QString &sFile ); + public slots: void send(); void recv( Gats::Object *pObj ); diff --git a/src/gatscon/filewidget.cpp b/src/gatscon/filewidget.cpp index b2dd09e..70f7b03 100644 --- a/src/gatscon/filewidget.cpp +++ b/src/gatscon/filewidget.cpp @@ -1,11 +1,14 @@ #include "filewidget.h" #include "gatstotree.h" +#include "treetogats.h" #include #include #include +#include + using namespace Bu; FileWidget::FileWidget( QWidget *pParent ) : @@ -37,3 +40,33 @@ FileWidget::~FileWidget() { } +void FileWidget::saveTo( const QString &sFile ) +{ + File fOut( sFile.toAscii().constData(), File::WriteNew ); + Gats::GatsStream gsOut( fOut ); + QTreeWidgetItem *pRoot = twGats->invisibleRootItem(); + for( int j = 0; j < pRoot->childCount(); j++ ) + { + Gats::Object *pObj = treeToGats( pRoot->child( j ) ); + gsOut.writeObject( pObj ); + delete pObj; + } +} + +void FileWidget::addRootItem() +{ + QString sText = QInputDialog::getText( this, "GatsCon - Add Root Item", + "Gats:"); + Gats::Object *pObj = Gats::Object::strToGats( sText.toAscii().constData() ); + QTreeWidgetItem *pNew = new QTreeWidgetItem( + twGats->invisibleRootItem() + ); + pNew->setText( 0, "" ); + gatsToTree( pNew, pObj ); + delete pObj; +} + +void FileWidget::delRootItem() +{ +} + diff --git a/src/gatscon/filewidget.h b/src/gatscon/filewidget.h index ac46685..9993bfe 100644 --- a/src/gatscon/filewidget.h +++ b/src/gatscon/filewidget.h @@ -2,13 +2,14 @@ #define FILE_WIDGET_H #include "ui_filewidget.h" +#include "iobase.h" namespace Gats { class Object; }; -class FileWidget : public QWidget, protected Ui::FileWidget +class FileWidget : public QWidget, protected Ui::FileWidget, public IoBase { Q_OBJECT; public: @@ -16,6 +17,12 @@ public: FileWidget( QWidget *pParent, QString sFile ); virtual ~FileWidget(); + virtual void saveTo( const QString &sFile ); + +public slots: + void addRootItem(); + void delRootItem(); + private: }; diff --git a/src/gatscon/filewidget.ui b/src/gatscon/filewidget.ui index 6557035..d0e6ec3 100644 --- a/src/gatscon/filewidget.ui +++ b/src/gatscon/filewidget.ui @@ -6,7 +6,7 @@ 0 0 - 400 + 316 300 @@ -33,8 +33,63 @@ + + + + + + Add Root Item + + + + + + + Del Root Item + + + + + - + + + pushButton_2 + clicked() + FileWidget + addRootItem() + + + 60 + 283 + + + 10 + 343 + + + + + pushButton + clicked() + FileWidget + delRootItem() + + + 252 + 283 + + + 258 + 324 + + + + + + addRootItem() + delRootItem() + diff --git a/src/gatscon/iobase.cpp b/src/gatscon/iobase.cpp new file mode 100644 index 0000000..309444c --- /dev/null +++ b/src/gatscon/iobase.cpp @@ -0,0 +1 @@ +#include "iobase.h" diff --git a/src/gatscon/iobase.h b/src/gatscon/iobase.h new file mode 100644 index 0000000..5bd3843 --- /dev/null +++ b/src/gatscon/iobase.h @@ -0,0 +1,10 @@ +#ifndef IO_BASE_H +#define IO_BASE_H + +class IoBase +{ +public: + virtual void saveTo( const class QString &sFile )=0; +}; + +#endif diff --git a/src/gatscon/mainwnd.cpp b/src/gatscon/mainwnd.cpp index 628988e..bddaf08 100644 --- a/src/gatscon/mainwnd.cpp +++ b/src/gatscon/mainwnd.cpp @@ -7,11 +7,15 @@ #include "connectdlg.h" #include "setupproxydlg.h" +#include #include MainWnd::MainWnd() { setupUi( this ); + + pMode = new QLabel( "Idle", this ); + statusBar()->addPermanentWidget( pMode ); } MainWnd::~MainWnd() @@ -23,11 +27,16 @@ void MainWnd::connect() ConnectDlg dlg( this ); if( dlg.exec() == QDialog::Accepted ) { + sCurFile.clear(); setCentralWidget( new ClientWidget( this, dlg.getHostname(), dlg.getPort() ) ); + pMode->setText( + QString("Client Mode: %1:%2").arg( QString(dlg.getHostname()) ). + arg( dlg.getPort() ) + ); } } @@ -37,11 +46,17 @@ void MainWnd::proxy() if( dlg.exec() == QDialog::Accepted ) { + sCurFile.clear(); setCentralWidget( new ProxyWidget( this, dlg.getPortIn(), dlg.getHostOut(), dlg.getPortOut() ) ); + pMode->setText( + QString("Proxy Mode: :%1 -> %2:%3").arg( dlg.getPortIn() ). + arg( QString(dlg.getHostOut()) ). + arg( dlg.getPortOut() ) + ); } } @@ -53,8 +68,52 @@ void MainWnd::open() if( sFile.isEmpty() ) return; + sCurFile = sFile; setCentralWidget( new FileWidget( this, sFile ) ); + pMode->setText( QString("File mode: %1").arg( sCurFile ) ); +} + +void MainWnd::newFile() +{ + sCurFile.clear(); + setCentralWidget( + new FileWidget( this ) + ); + pMode->setText( QString("File mode: ") ); +} + +void MainWnd::save() +{ + if( sCurFile.isEmpty() ) + { + saveAs(); + } + else + { + IoBase *pIo = dynamic_cast(centralWidget()); + if( !pIo ) + return; + + pIo->saveTo( sCurFile ); + } +} + +void MainWnd::saveAs() +{ + IoBase *pIo = dynamic_cast(centralWidget()); + if( !pIo ) + return; + + QString sFile = QFileDialog::getSaveFileName( + this, "GatsCon - save gats file" + ); + if( sFile.isEmpty() ) + return; + + pIo->saveTo( sFile ); + + sCurFile = sFile; } diff --git a/src/gatscon/mainwnd.h b/src/gatscon/mainwnd.h index 8f638d8..d1ae080 100644 --- a/src/gatscon/mainwnd.h +++ b/src/gatscon/mainwnd.h @@ -14,7 +14,13 @@ public slots: void connect(); void proxy(); void open(); + void newFile(); + void save(); + void saveAs(); +private: + QString sCurFile; + class QLabel *pMode; }; #endif diff --git a/src/gatscon/mainwnd.ui b/src/gatscon/mainwnd.ui index dbf5e11..397860e 100644 --- a/src/gatscon/mainwnd.ui +++ b/src/gatscon/mainwnd.ui @@ -131,10 +131,77 @@ + + actionE_xit + triggered() + MainWnd + close() + + + -1 + -1 + + + 215 + 159 + + + + + action_New_Gats_File + triggered() + MainWnd + newFile() + + + -1 + -1 + + + 215 + 159 + + + + + action_Save + triggered() + MainWnd + save() + + + -1 + -1 + + + 215 + 159 + + + + + action_Save_As + triggered() + MainWnd + saveAs() + + + -1 + -1 + + + 215 + 159 + + + connect() proxy() open() + save() + saveAs() + newFile() diff --git a/src/gatscon/proxywidget.cpp b/src/gatscon/proxywidget.cpp index 28bd1fc..3792e31 100644 --- a/src/gatscon/proxywidget.cpp +++ b/src/gatscon/proxywidget.cpp @@ -2,11 +2,14 @@ #include "proxythread.h" #include "gatstotree.h" +#include "treetogats.h" #include +#include #include #include +#include using namespace Bu; @@ -33,6 +36,19 @@ ProxyWidget::~ProxyWidget() { } +void ProxyWidget::saveTo( const QString &sFile ) +{ + File fOut( sFile.toAscii().constData(), File::WriteNew ); + Gats::GatsStream gsOut( fOut ); + QTreeWidgetItem *pRoot = twHistory->invisibleRootItem(); + for( int j = 0; j < pRoot->childCount(); j++ ) + { + Gats::Object *pObj = treeToGats( pRoot->child( j ) ); + gsOut.writeObject( pObj ); + delete pObj; + } +} + void ProxyWidget::sendToClient() { try diff --git a/src/gatscon/proxywidget.h b/src/gatscon/proxywidget.h index 0ef1fd4..d6ebf4d 100644 --- a/src/gatscon/proxywidget.h +++ b/src/gatscon/proxywidget.h @@ -2,13 +2,14 @@ #define PROXY_WIDGET_H #include "ui_proxywidget.h" +#include "iobase.h" namespace Gats { class Object; }; -class ProxyWidget : public QWidget, protected Ui::ProxyWidget +class ProxyWidget : public QWidget, protected Ui::ProxyWidget, public IoBase { Q_OBJECT; public: @@ -16,6 +17,8 @@ public: int iPortOut ); virtual ~ProxyWidget(); + virtual void saveTo( const QString &sFile ); + public slots: void sendToClient(); void sendToServer(); diff --git a/src/gatscon/treetogats.cpp b/src/gatscon/treetogats.cpp new file mode 100644 index 0000000..a1571d1 --- /dev/null +++ b/src/gatscon/treetogats.cpp @@ -0,0 +1,52 @@ +#include "treetogats.h" + +#include + +#include + +Gats::Object *treeToGats( QTreeWidgetItem *pRoot ) +{ + QString sType = pRoot->text( 1 ); + QByteArray baDat = pRoot->text( 2 ).toAscii(); + if( sType == "int" ) + { + return new Gats::Integer( strtoll( baDat.constData(), NULL, 10 ) ); + } + else if( sType == "str" ) + { + return new Gats::String( baDat.constData(), baDat.size() ); + } + else if( sType == "float" ) + { + return new Gats::Float( strtod( baDat.constData(), NULL ) ); + } + else if( sType == "bool" ) + { + return new Gats::Boolean( baDat == "true" ); + } + else if( sType == "list" ) + { + Gats::List *pRet = new Gats::List(); + for( int j = 0; j < pRoot->childCount(); j++ ) + { + pRet->append( treeToGats( pRoot->child( j ) ) ); + } + return pRet; + } + else if( sType == "dict" ) + { + Gats::Dictionary *pRet = new Gats::Dictionary(); + for( int j = 0; j < pRoot->childCount(); j++ ) + { + QTreeWidgetItem *pChild = pRoot->child( j ); + pRet->insert( + pChild->text( 0 ).toAscii().constData(), + treeToGats( pChild ) + ); + } + return pRet; + } + + throw Bu::ExceptionBase("Unhandled type found."); +} + diff --git a/src/gatscon/treetogats.h b/src/gatscon/treetogats.h new file mode 100644 index 0000000..931623d --- /dev/null +++ b/src/gatscon/treetogats.h @@ -0,0 +1,13 @@ +#ifndef TREE_TO_GATS_H +#define TREE_TO_GATS_H + +class QTreeWidgetItem; + +namespace Gats +{ + class Object; +}; + +Gats::Object *treeToGats( QTreeWidgetItem *pRoot ); + +#endif -- cgit v1.2.3