]> Creatis software - creaWT.git/blobdiff - wt/bbtk_wt_PKG/src/bbwtFileUploader.cxx
2498 BBTK FeatureNewNormal wt-version PackageWt
[creaWT.git] / wt / bbtk_wt_PKG / src / bbwtFileUploader.cxx
diff --git a/wt/bbtk_wt_PKG/src/bbwtFileUploader.cxx b/wt/bbtk_wt_PKG/src/bbwtFileUploader.cxx
new file mode 100644 (file)
index 0000000..96847c4
--- /dev/null
@@ -0,0 +1,214 @@
+//===== 
+// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
+//===== 
+#include "bbwtFileUploader.h"
+#include "bbwtPackage.h"
+//#include <Wt/WFileUpload>
+namespace bbwt
+{
+
+       class FileUploaderWidget : public Wt::WContainerWidget
+       {
+               public:
+             FileUploaderWidget(FileUploader* box,
+                                 Wt::WContainerWidget *parent,
+                                 Wt::WString label,
+                                 std::string pPath,
+                                 std::string fileName,
+                                 bool pNameIn);
+             ~FileUploaderWidget();
+             void fileUploaded();
+             void fileTooLarge();
+             void mUpload();
+           
+
+           private:
+             FileUploader* mBox;
+             std::string mPath;
+             std::string mName;
+             Wt::WPushButton *mwtPushButton;
+        Wt::WFileUpload *upload;
+        bool nameIn;
+        Wt::WLineEdit *name;
+
+       };
+
+  FileUploaderWidget::FileUploaderWidget(FileUploader* box,
+                                 Wt::WContainerWidget *parent,
+                                 Wt::WString label,
+                                 std::string pPath,
+                                 std::string fileName,
+                                 bool pNameIn)
+                      :
+                      Wt::WContainerWidget(parent),
+                      mBox(box),
+                      mPath(pPath),
+                      mName(fileName),
+                      nameIn(pNameIn)
+  {
+
+
+         Wt::WContainerWidget* w = this;
+         Wt::WVBoxLayout *vLay = new Wt::WVBoxLayout(); 
+         Wt::WVBoxLayout *vLay1 = new Wt::WVBoxLayout(); 
+         Wt::WHBoxLayout *hLay1 = new Wt::WHBoxLayout(); 
+         Wt::WContainerWidget* aux1 = new Wt::WContainerWidget();
+         Wt::WHBoxLayout *hLay2 = new Wt::WHBoxLayout(); 
+         Wt::WContainerWidget* aux2 = new Wt::WContainerWidget();
+         Wt::WContainerWidget* aux3 = new Wt::WContainerWidget();
+         upload = new Wt::WFileUpload(0);
+         upload->setFileTextSize(40);
+         // Provide a button
+         Wt::WPushButton *uploadButton = new Wt::WPushButton("Upload", 0);
+         // Upload when the button is clicked.
+         uploadButton->clicked().connect(upload, &Wt::WFileUpload::upload);
+         //uploadButton->clicked().connect(uploadButton, &Wt::WPushButton::disable);
+         // Upload automatically when the user entered a file.
+         //upload->changed().connect(upload, &Wt::WFileUpload::upload);
+         //upload->changed().connect(uploadButton, &Wt::WPushButton::disable);
+         // React to a succesfull upload.
+         upload->uploaded().connect(this, &FileUploaderWidget::fileUploaded);
+         // React to a fileupload problem.
+         //upload->fileTooLarge().connect(this, &FileUploaderWidget::fileTooLarge);
+    
+    vLay->addWidget(new Wt::WText("Upload your file"));
+    hLay1->addWidget(upload,1);
+    hLay1->addWidget(uploadButton,1);
+    aux1->setLayout(hLay1);
+    
+    hLay2->addWidget(new Wt::WText("File Name"),1);
+    
+    
+    
+    name = new Wt::WLineEdit();
+    name->setText(bbtk::std2wt(mName));
+    name->setReadOnly(!nameIn);
+    
+    hLay2->addWidget(name,1);
+    
+    aux2->setLayout(hLay2);
+    
+    
+    vLay1->addWidget(aux1,1);
+    vLay1->addWidget(aux2,1);
+    
+    aux3->setLayout(vLay1);
+    vLay->addWidget(aux3);
+    
+    
+    w->setMaximumSize(350,150);
+    w->setLayout(vLay);
+    
+
+  }
+
+  FileUploaderWidget::~FileUploaderWidget()
+  {
+
+  }
+  
+  void   FileUploaderWidget::fileUploaded()
+  {
+    std::cout<<"FILEUPLOAD----UPLOADING-----"<<std::endl;
+    std::string mFilename = upload->spoolFileName(); 
+    std::vector<Wt::Http::UploadedFile> mFileContents = upload->uploadedFiles();
+    upload->stealSpooledFile();
+    std::string mContents;
+    mContents=mFileContents.data()->spoolFileName();
+    std::ifstream  src(mContents.c_str(), std::ios::binary);
+    std::string ppa;
+    if(bbtk::wt2std(name->text()) == "")
+    {
+      ppa=mPath + bbtk::wt2std(upload->clientFileName());
+    }
+    else
+    {
+      ppa = mPath + bbtk::wt2std(name->text());
+
+    }
+    std::ofstream  dst(ppa.c_str(),   std::ios::binary);
+    dst << src.rdbuf();    
+    std::cout<<"FILEUPLOAD---------"<<ppa<<std::endl;
+    mBox->updatePath(ppa);
+  }
+ /* void fileTooLarge()
+  {
+    std::cout<<"ERROR ---- bbwtFileUploader : File size too large"<<std::endl;
+  }
+  */
+BBTK_ADD_BLACK_BOX_TO_PACKAGE(wt,FileUploader)
+BBTK_BLACK_BOX_IMPLEMENTATION(FileUploader,bbtk::WtBlackBox);
+//===== 
+// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
+//===== 
+void FileUploader::Process()
+{
+/*
+   std::string msg;
+    if (bbGetInputTitle()!="")
+      {
+       msg = bbGetInputTitle()+": " + bbGetInputIn();
+      }
+    else
+      {
+       msg = bbGetInputIn();
+      }
+   ((wxStaticText*)bbGetOutputWidget())->SetLabel( bbtk::std2wx( msg ) ); 
+  */
+}
+//===== 
+
+void FileUploader::updatePath(std::string sPath)
+{
+  std::cout<<"DEBUG : ----- FileUpliader.cxx "<<std::endl<<"\t\t Nuevo Path : "<<sPath<<std::endl;
+  bbSetOutputOutPath(sPath);
+}
+
+// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
+//===== 
+void FileUploader::CreateWidget(Wt::WContainerWidget* parent)
+{
+
+  FileUploaderWidget *w = new FileUploaderWidget( this,
+                                parent,
+                                bbtk::std2wt(bbGetInputTitle()),
+                                bbGetInputPath(),
+                                bbGetInputNameFile(),
+                                bbGetInputInName());
+
+
+   bbSetOutputWidget(w);
+  
+}
+//===== 
+// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
+//===== 
+void FileUploader::bbUserSetDefaultValues()
+{
+  bbSetInputTitle("");
+  //bbSetInputPath("");
+    //Testing by Johann Felipe Gonzalez Avila, Delete this line and replace it with the path of resources given in the bbtkWtBlackBox.cxx file in the inicialization of the app CreateWindow.
+  bbSetInputPath("/home/gonzalez/Documents/pruebaResources/");
+  bbSetInputInName(true);
+  bbSetInputNameFile("");
+  bbSetOutputOutPath("");
+
+}
+//===== 
+// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
+//===== 
+void FileUploader::bbUserInitializeProcessing()
+{
+
+}
+//===== 
+// Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
+//===== 
+void FileUploader::bbUserFinalizeProcessing()
+{
+
+}
+}
+// EO namespace bbwt
+
+