NIREP
|
00001 #include "WidgetList.h" 00002 00003 #include "NIREPDefinitions.h" 00004 #include <algorithm> 00005 00006 #define WIDGETLIST 1 00007 00008 00009 WidgetList::WidgetList(VariableList* vList, DisplayAttributes *da) 00010 { 00011 error = false; 00012 vars = NULL; 00013 displayAttributes = NULL; 00014 this->SetVarList(vList); 00015 this->SetDisplayAttributes(da); 00016 } 00017 00018 WidgetList::WidgetList(const std::string& fileName, VariableList* vList, DisplayAttributes *da) 00019 { 00020 vars = NULL; 00021 displayAttributes = NULL; 00022 this->SetDisplayAttributes(da); 00023 this->SetVarList(vList); 00024 error = this->Read(fileName); 00025 } 00026 00029 WidgetList::~WidgetList() 00030 { 00031 //This class does not own vars or displayAttributes 00032 //so therefore we do not destroy them but set them 00033 //to NULL. 00034 vars = NULL; 00035 displayAttributes = NULL; 00036 } 00037 00038 00039 void WidgetList::SetDisplayAttributes(DisplayAttributes* da) 00040 { 00041 if(da == NULL) 00042 return; 00043 displayAttributes = da; 00044 } 00045 00048 void WidgetList::SetVarList(VariableList* list) 00049 { 00050 if(list == NULL) 00051 return; 00052 this->vars = list; 00053 } 00054 //read in text description and populate the widget list 00055 bool WidgetList::Read(const std::string& fileName) 00056 { 00057 this->widget.clear(); 00058 if(displayAttributes->AnyError() ) 00059 return true; 00060 if(displayAttributes != NULL) { 00061 this->widget.resize(displayAttributes->GetColDimension()*displayAttributes->GetRowDimension()); 00062 } 00063 00064 std::ifstream file (fileName.c_str()); 00065 int list = 0; 00066 if (file.is_open()) 00067 { 00068 std::string line; 00069 while (! file.eof() ) 00070 { 00071 getline (file,line); 00072 00073 // Convert line to all lower case for comparison 00074 std::transform(line.begin(), line.end(), line.begin(), ::tolower); 00075 if(line.size() > 0) 00076 { 00077 //Run through an if statment. If we have not hit 00078 //the begin WidgetList then we are going to be 00079 //looking for a begin widgetList or columnSize 00080 //or rowSize. If we have hit the beginWidgetList 00081 //then either look for the End WidgetList or 00082 //insert the items into the vector at the locations 00083 //specified in the file. In the file, there should 00084 //be a line like: 00085 // W1,1 = view(_A1, Transverse) 00086 // This example will place a view widget at 00087 //row 1, column 1. 00088 if(list == 0) 00089 { 00090 if(!line.compare("begin widgetlist")) 00091 { 00092 list = WIDGETLIST; 00093 } 00094 } 00095 else if(list == WIDGETLIST) { 00096 if(!line.compare("end widgetlist")) { 00097 list =0; 00098 } 00099 else if(line.compare("") != 0) { 00100 00101 // Find position of = in widget command 00102 size_t index1 = line.find_first_of("="); 00103 00104 // Load function string with RHS of widget command 00105 std::string function(line, index1+1,line.length()); 00106 wxString wxFunction(function.c_str(), wxConvUTF8); 00107 wxFunction.Trim(); 00108 wxFunction.Trim(false); 00109 function = wxFunction.ToAscii(); 00110 00111 // Set loc to the single index of widget command 00112 int loc = this->GetLocation(line); 00113 if(loc >= 0) { 00114 this->widget[loc] = function; 00115 std::string widget_type = this->GetWidgetType(loc); 00116 bool isCommand = false; 00117 00118 // Loop through list of widget commands to determine if widget_type 00119 // is a valid widget command. 00120 WidgetCmdList::iterator iter; 00121 for(iter = WidgetCmd.begin(); iter != WidgetCmd.end(); iter++){ 00122 if(!iter->second.compare(widget_type )) { 00123 isCommand = true; 00124 } 00125 } 00126 00127 if(!isCommand) { 00128 wxString temp(wxT("Error: Unknown widget command: ")); 00129 temp.Append(wxString::Format (wxT("%s for %s"), widget_type.c_str(), line.c_str())); 00130 wxLogError(temp); 00131 return true; 00132 } 00133 } 00134 } 00135 } 00136 00137 } 00138 } 00139 00140 file.close(); 00141 return false; 00142 00143 } 00144 00145 wxString temp(wxT("Error: Could not open file: ")); 00146 temp.Append(wxString::Format (wxT("%s"), fileName.c_str())); 00147 wxLogError(temp); 00148 return true; 00149 } 00150 00151 void WidgetList::Write(const std::string& fileName) 00152 { 00153 } 00154 00155 00156 //Need to use this one. 00157 std::string WidgetList::GetWidgetType(int row, int col) 00158 { 00159 std::string temp = widget.at((row)*this->displayAttributes->GetColDimension()+col); 00160 size_t tempThree = temp.find_first_of("("); 00161 std::string type(temp, 0,tempThree); 00162 return type; 00163 } 00164 00165 //Need to use this one. 00166 std::string WidgetList::GetWidgetType(int location) 00167 { 00168 std::string temp = widget.at(location); 00169 size_t tempThree = temp.find_first_of("("); 00170 std::string type(temp, 0,tempThree); 00171 return type; 00172 } 00173 00174 00175 void WidgetList::SetWidgetType(std::string widgetName, std::string type) 00176 { 00177 // widget[widgetName] = type; 00178 } 00179 00180 std::vector<std::string> WidgetList::GetWidgetParameters (int row, int col) 00181 { 00182 std::vector<std::string> Parameters; 00183 std::string temp = widget.at((row)*this->displayAttributes->GetColDimension()+col); 00184 size_t tempTwo = temp.find_first_of("("); 00185 size_t tempThree = temp.find_last_of(")"); 00186 size_t tempFour = temp.find_first_of(","); 00187 if(tempThree < tempFour) 00188 { 00189 std::string parameter(temp, (tempTwo+1), (tempThree-tempTwo-1)); 00190 Parameters.push_back(parameter); 00191 } 00192 else 00193 { 00194 std::string parameter(temp, tempTwo+1, tempFour-tempTwo-1); 00195 Parameters.push_back(parameter); 00196 while(tempFour != std::string::npos) 00197 { 00198 size_t tempFive = tempFour; 00199 tempFour= temp.find(",",tempFour+1); 00200 if(tempFour == std::string::npos) 00201 { 00202 std::string parameterThree(temp, tempFive+1, tempThree-tempFive-1); 00203 Parameters.push_back(parameterThree); 00204 } 00205 else 00206 { 00207 std::string parameterTwo(temp, tempFive+1, tempFour-tempFive-1); 00208 Parameters.push_back(parameterTwo); 00209 } 00210 } 00211 } 00212 for(unsigned int i=0; i < Parameters.size(); i++) { 00213 Parameters[i] = this->vars->UpdateString(Parameters[i]); 00214 } 00215 return Parameters; 00216 } 00217 00218 std::string WidgetList::GetTitle(int row, int col) 00219 { 00220 std::vector<std::string> params = this->GetWidgetParameters(row, col); 00221 std::string title = params.at(params.size()-1).c_str(); 00222 return this->vars->UpdateString(title); 00223 } 00224 00225 // Note that this function expects widgets to be in the form 00226 // w1,1 where the w can be upper case or lower case 00227 int WidgetList::GetLocation(std::string line) 00228 { 00229 // Convert line to lower case for comparison 00230 // Do it in place by providing the same start iterator 00231 // for both the input and output strings 00232 std::transform(line.begin(), line.end(), line.begin(), ::tolower); 00233 00234 //Get the column that the widget is supposed to be in 00235 size_t index1 = line.find_first_of("w"); 00236 size_t index2 = line.find_first_of(","); 00237 size_t num = index2 - index1 - 1; 00238 00239 std::string id(line, index1+1,num); 00240 wxString wxID(id.c_str(), wxConvUTF8); 00241 wxID.Trim(); 00242 wxID.Trim(false); 00243 00244 //Get the row that the widget is supposed to be in 00245 size_t index3 = line.find_first_of("="); 00246 num = index3 - index2 - 1; 00247 00248 /*if( foundTwo == std::string::npos ) { 00249 wxString temp = "Error: Missing equal sign in the line: "; 00250 temp.Append(wxString::Format ("%s", _(line.c_str()))); 00251 ::wxLogError(temp); 00252 return true; 00253 }*/ 00254 std::string idTwo(line, index2+1,num); 00255 wxString wxIDTwo(idTwo.c_str(), wxConvUTF8); 00256 wxIDTwo.Trim(); 00257 wxIDTwo.Trim(false); 00258 idTwo = wxIDTwo.ToAscii(); 00259 00260 long int col; 00261 wxIDTwo.ToLong(&col); 00262 long int row; 00263 //wxString wxID; 00264 wxID.ToLong(&row); 00265 return ((int)row-1)*this->displayAttributes->GetColDimension()+(int)col-1; 00266 }