00001 #include <QtGui>
00002 #include "mergefeaturesdialog.h"
00003 #include "../../pihmLIBS/helpDialog/helpdialog.h"
00004 #include "../../pihmLIBS/mergeFeatures.h"
00005 #include "../../pihmLIBS/shapefil.h"
00006
00007 #include <fstream>
00008 using namespace std;
00009
00010 mergeFeaturesDialogDlg::mergeFeaturesDialogDlg(QWidget *parent)
00011 {
00012 setupUi(this);
00013 connect(addFileButton, SIGNAL(clicked()), this, SLOT(addBrowse()));
00014 connect(removeFileButton, SIGNAL(clicked()), this, SLOT(removeBrowse()));
00015 connect(removeAllButton, SIGNAL(clicked()), this, SLOT(removeAllBrowse()));
00016 connect(outputMergedFileButton, SIGNAL(clicked()), this, SLOT(outputBrowse()));
00017 connect(okButton, SIGNAL(clicked()), this, SLOT(run()));
00018 connect(helpButton, SIGNAL(clicked()), this, SLOT(help()));
00019 connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
00020 }
00021
00022 void mergeFeaturesDialogDlg::addBrowse()
00023 {
00024 QStringList temp = QFileDialog::getOpenFileNames(this, "Choose File", "~/","Shape Files(*.shp *.SHP)");
00025 QString str = "";
00026
00027
00028 unsigned int i;
00029
00030 int rows = inputOutputTable->numRows();
00031
00032 for(i=0; i< temp.count(); i++)
00033 {
00034
00035 str = temp[i];
00036
00037
00038
00039 inputOutputTable->insertRows(rows + i);
00040 Q3TableItem *tmpItem = new Q3TableItem(inputOutputTable, Q3TableItem::Always, str );
00041 inputOutputTable->setItem(rows+i,0,tmpItem);
00042
00043
00044 }
00045 }
00046
00047 void mergeFeaturesDialogDlg::removeBrowse()
00048 {
00049 int tmp = inputOutputTable->numRows();
00050
00051 Q3MemArray <int> rowArray(tmp);
00052 int j=0;
00053 for(int i=0; i<tmp;i++){
00054 if(inputOutputTable->isRowSelected(i))
00055 {
00056 rowArray[j]=i;
00057 qWarning("\n %d",rowArray[j]);
00058 j++;
00059 }
00060 }
00061 rowArray.truncate(j);
00062 inputOutputTable->removeRows(rowArray);
00063 }
00064
00065 void mergeFeaturesDialogDlg::removeAllBrowse()
00066 {
00067 int tmp = inputOutputTable->numRows();
00068 for(int i=0; i<= tmp;i++){
00069 inputOutputTable->removeRow(0);
00070 }
00071 }
00072
00073 void mergeFeaturesDialogDlg::outputBrowse()
00074 {
00075 QString temp = QFileDialog::getSaveFileName(this, "Choose File", "~/","Shape File(*.shp *.SHP)");
00076 QString tmp = temp;
00077 if(!(tmp.toLower()).endsWith(".shp")){
00078 tmp.append(".shp");
00079 temp = tmp;
00080 }
00081 outputMergedFileLineEdit->setText(temp);
00082 }
00083
00084 void mergeFeaturesDialogDlg::run()
00085 {
00086
00087 QString logFileName("c:\log.html");
00088 ofstream log;
00089 log.open(logFileName.ascii());
00090 log<<"<html><body><font size=3 color=black><p> Verifying Files...</p></font></body></html>";
00091 log.close();
00092 messageLog->setSource(logFileName);
00093 messageLog->setFocus();
00094 messageLog->setModified(TRUE);
00095
00096
00097
00098 int rows = inputOutputTable->numRows();
00099 qWarning("Number of rows = %d", rows);
00100 const char **shpFileNamesChar = new const char*[rows];
00101 const char **dbfFileNamesChar = new const char*[rows];
00102 QString *shpFileNames = new QString[rows];
00103 QString *dbfFileNames = new QString[rows];
00104 int fileCount = 0;
00105
00106 QString shpFileName, dbfFileName, shpFileNameMerge, dbfFileNameMerge;
00107
00108 int runFlag = 1;
00109 if(inputOutputTable->numRows() < 2){
00110 log.open(logFileName.ascii(), ios::app);
00111 log<<"<p><font size=3 color=red> First Please input Files</p>";
00112 log.close();
00113 messageLog->reload();
00114 QApplication::processEvents();
00115 }
00116 else{
00117 for(int i=0; i<rows; i++){
00118 shpFileName = inputOutputTable->text(i, 0);
00119 dbfFileName = shpFileName;
00120 dbfFileName.truncate(dbfFileName.length()-3);
00121 dbfFileName.append("dbf");
00122
00123 ifstream file;
00124 file.open(shpFileName.ascii(), ios::in);
00125 log.open(logFileName.ascii(), ios::app);
00126
00127 qWarning("\n %s", shpFileName.ascii());
00128
00129 log<<"<p>Checking... "<<shpFileName.ascii()<<"... ";
00130 if(file == NULL){
00131 log<<"<font size=3 color=red> Error!</p>";
00132 runFlag = 0;
00133 }
00134 else{
00135 log<<"Done!</p>";
00136 shpFileNamesChar[fileCount] = new char[200];
00137 dbfFileNamesChar[fileCount] = new char[200];
00138 shpFileNamesChar[fileCount] = shpFileName.ascii();
00139 dbfFileNamesChar[fileCount] = dbfFileName.ascii();
00140
00141 shpFileNames[fileCount] = shpFileName;
00142 dbfFileNames[fileCount] = dbfFileName;
00143 fileCount++;
00144 }
00145 log.close();
00146 messageLog->reload();
00147 QApplication::processEvents();
00148 }
00149 }
00150
00151 ofstream out;
00152 out.open((outputMergedFileLineEdit->text()).ascii());
00153
00154 log.open(logFileName.ascii(), ios::app);
00155 if((outputMergedFileLineEdit->text()).length()==0){
00156 runFlag = 0;
00157 log<<"<p><font size=3 color=red> Error! Please input Merge Output File</p>";
00158 qWarning("\nPlease enter output file name");
00159 }
00160 else{
00161 log<<"<p>Checking... "<<(outputMergedFileLineEdit->text()).ascii()<<"... ";
00162 if(out == NULL){
00163 log<<"<font size=3 color=red> Error!</p>";
00164
00165 runFlag = 0;
00166 }
00167 else
00168 log<<"Done!</p>";
00169 }
00170 log.close();
00171 messageLog->reload();
00172 QApplication::processEvents();
00173
00174
00175 if(runFlag == 1){
00176 shpFileNameMerge = outputMergedFileLineEdit->text();
00177 dbfFileNameMerge = shpFileNameMerge;
00178 dbfFileNameMerge.truncate(dbfFileNameMerge.length()-3);
00179 dbfFileNameMerge.append("dbf");
00180
00181 log.open(logFileName.ascii(), ios::app);
00182 log<<"<p>Running...";
00183 log.close();
00184 messageLog->reload();
00185 QApplication::processEvents();
00186
00187
00188
00189 mergeFeatures(shpFileNamesChar, dbfFileNamesChar, fileCount, shpFileNameMerge.ascii(), dbfFileNameMerge.ascii());
00190
00191 log.open(logFileName.ascii(), ios::app);
00192 log<<" Done!</p>";
00193 log.close();
00194 messageLog->reload();
00195 QApplication::processEvents();
00196 }
00197 }
00198
00199 void mergeFeaturesDialogDlg::help()
00200 {
00201 helpDialog* hlpDlg = new helpDialog(this, "Vecto Merge", 1, "helpFiles/mergeFeaturesDialog.html", "Help :: Vector Merge");
00202 hlpDlg->show();
00203 }