00001 #include <QtGui>
00002 #include "catchmentpolygon.h"
00003 #include "../../pihmRasterLIBS/lsm.h"
00004 #include "../../pihmRasterLIBS/catPoly.h"
00005
00006 #include "../../pihmLIBS/helpDialog/helpdialog.h"
00007
00008 #include <fstream>
00009 using namespace std;
00010
00011 CatchmentPolygonDlg::CatchmentPolygonDlg(QWidget *parent)
00012 {
00013 setupUi(this);
00014 connect(inputBrowseButton, SIGNAL(clicked()), this, SLOT(inputBrowse()));
00015 connect(outputBrowseButton, SIGNAL(clicked()), this, SLOT(outputBrowse()));
00016 connect(runButton, SIGNAL(clicked()), this, SLOT(run()));
00017 connect(helpButton, SIGNAL(clicked()), this, SLOT(help()));
00018 connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
00019 }
00020
00021 void CatchmentPolygonDlg::inputBrowse()
00022 {
00023 QString str = QFileDialog::getOpenFileName(this, "Choose File", "~/","CatchmentGrid File(*.adf *.asc)");
00024 inputFileLineEdit->setText(str);
00025 }
00026
00027
00028 void CatchmentPolygonDlg::outputBrowse()
00029 {
00030 QString temp = QFileDialog::getSaveFileName(this, "Choose File", "~/","Catchment Polygon File(*.shp)");
00031 QString tmp = temp;
00032 if(!(tmp.toLower()).endsWith(".shp")){
00033 tmp.append(".shp");
00034 temp = tmp;
00035 }
00036
00037 outputFileLineEdit->setText(temp);
00038 }
00039
00040
00041 void CatchmentPolygonDlg::run()
00042 {
00043 QString logFileName("c:\log.html");
00044 ofstream log;
00045 log.open(logFileName.ascii());
00046 log<<"<html><body><font size=3 color=black><p> Verifying Files...</p></font></body></html>";
00047 log.close();
00048 messageLog->setSource(logFileName);
00049 messageLog->setFocus();
00050 messageLog->setModified(TRUE);
00051
00052
00053 QString inputFileName((inputFileLineEdit->text()));
00054 QString outputShpFileName((outputFileLineEdit->text()));
00055 QString outputDbfFileName;
00056 outputDbfFileName = outputShpFileName;
00057 outputDbfFileName.truncate(outputDbfFileName.length()-3);
00058 outputDbfFileName.append("dbf");
00059
00060 ifstream inFile; inFile.open((inputFileLineEdit->text()).ascii());
00061 ofstream outFile; outFile.open((outputFileLineEdit->text()).ascii());
00062 int runFlag = 1;
00063
00064 log.open(logFileName.ascii(), ios::app);
00065 if(inputFileName.length()==0){
00066 log<<"<p><font size=3 color=red> Error! Please input Catchment Grid Input File</p>";
00067 runFlag = 0;
00068 }
00069 else{
00070 log<<"<p>Checking... "<<inputFileName.ascii()<<"... ";
00071 if(inFile == NULL){
00072 log<<"<font size=3 color=red> Error!</p>";
00073 qWarning("\n%s doesn't exist!", (inputFileLineEdit->text()).ascii());
00074 runFlag = 0;
00075 }
00076 else
00077 log<<"Done!</p>";
00078 }
00079 log.close();
00080 messageLog->reload();
00081 QApplication::processEvents();
00082
00083 log.open(logFileName.ascii(), ios::app);
00084 if(outputShpFileName.length()==0){
00085 log<<"<p><font size=3 color=red> Error! Please input Catchment Polygon Output File</p>";
00086 runFlag = 0;
00087 }
00088 else{
00089 log<<"<p>Checking... "<<outputShpFileName.ascii()<<"... ";
00090 if(outFile == NULL){
00091 log<<"<font size=3 color=red> Error!</p>";
00092 qWarning("\nCan not open output file name");
00093 runFlag = 0;
00094 }
00095 else
00096 log<<"Done!</p>";
00097 }
00098 log.close();
00099 messageLog->reload();
00100 QApplication::processEvents();
00101
00102
00103 if(runFlag == 1){
00104
00105 log.open(logFileName.ascii(), ios::app);
00106 log<<"<p>Running...";
00107 log.close();
00108 messageLog->reload();
00109 QApplication::processEvents();
00110
00111 int err = catchmentPoly((char *)inputFileName.ascii(), "dummy", (char *)outputShpFileName.ascii(), (char *)outputDbfFileName.ascii());
00112
00113 log.open(logFileName.ascii(), ios::app);
00114 log<<" Done!</p>";
00115 log.close();
00116 messageLog->reload();
00117 QApplication::processEvents();
00118
00119 if(showSG_DFrame->isChecked() == 1){
00120
00121 applicationPointer->addLayer(QStringList(outputShpFileName), NULL);
00122 }
00123 }
00124 }
00125
00126 void CatchmentPolygonDlg::help()
00127 {
00128 helpDialog* hlpDlg = new helpDialog(this, "Catchment Polygon", 1, "helpFiles/catchmentpolygon.html", "Help :: Catchment Polygon");
00129 hlpDlg->show();
00130
00131 }
00132
00133 void CatchmentPolygonDlg::setApplicationPointer(QgisApp* appPtr){
00134 applicationPointer = appPtr;
00135 }