#include "stdafx.h" #include "PoroMediaDialogShare.h" ////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////// // // // // The following methods are all utilities for other methods within this class // // // ////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////// //simply concats the input_dir and input_file, and places it in the combined field on the GUI void PoroMediaDialogShare::UpdateDirFileField() { char dir_name_str[512]; strcpy(dir_name_str, input_directory_name); strcat(dir_name_str, input_file_name); field_directory_and_file_input_cntrl.SetWindowText(dir_name_str); } //this checks if an input_num falls within the limits before placing it in a start/end field int PoroMediaDialogShare::LimitFieldValueSE(CEdit* field_SE_slice_cntrl, int input_num, int plane) { int largest; switch (plane){ case X_PLANE: if (input_num > max_X_size) input_num = max_X_size; else if (input_num < 1 ) input_num = 1; break; case Y_PLANE: if (input_num > max_Y_size) input_num = max_Y_size; else if (input_num < 1 ) input_num = 1; break; case S_PLANE: //if input is too large or to small, limit it automatically largest = (int)pow(10.0f, max_dig_cnt) - 1; if ( MAX_NUM_LIMIT < largest ) largest = MAX_NUM_LIMIT; if (input_num > largest ) input_num = largest; else if (input_num < 0) input_num = 0; default: break; } SetIntValField(field_SE_slice_cntrl, input_num); return input_num; } //slightly different for width field, since it can't exceed difference between end and start slices int PoroMediaDialogShare::LimitFieldValueWidth(CEdit* field_width_slices_cntrl, int num_slices, int start_indx, int plane) { int largest; //if input is too large or to small, limit it automatically switch (plane){ case X_PLANE: if ( (start_indx + num_slices - 1) > max_X_size) num_slices = max_X_size - start_indx; else if (num_slices < 1 ) num_slices = 1; break; case Y_PLANE: if ( (start_indx + num_slices - 1) > max_Y_size) num_slices = max_Y_size - start_indx; else if (num_slices < 1 ) num_slices = 1; break; case S_PLANE: largest = (int)pow(10.0f, max_dig_cnt) - 1; if ( MAX_NUM_LIMIT < largest ) largest = MAX_NUM_LIMIT; if ( (start_indx + num_slices - 1) > largest ) num_slices = largest - start_indx; else if (num_slices < 1) num_slices = 1; default: break; } SetIntValField(field_width_slices_cntrl, num_slices); return num_slices; } //for getting a integer value from a CEdit field, simple but used a lot int PoroMediaDialogShare::GetIntValFromField(CEdit* field_cntrl) { CString input_txt; field_cntrl->GetWindowText(input_txt); if (input_txt.GetLength() > 0) return (atoi(input_txt)); else return -1; } long PoroMediaDialogShare::GetLongValFromField(CEdit* field_cntrl) { CString input_txt; field_cntrl->GetWindowText(input_txt); if (input_txt.GetLength() > 0) return ( atol(input_txt) ); else return -1; } double PoroMediaDialogShare::GetFloatValFromField(CEdit* field_cntrl) { CString input_txt; field_cntrl->GetWindowText(input_txt); if (input_txt.GetLength() > 0) return (atof(input_txt)); else return -1; } //for setting a 32 bit integer value in a slice CEdit field void PoroMediaDialogShare::SetIntValField(CEdit* field_cntrl, int val) { //max slice num is SLICE_DIG_LIMIT + '\0' char val_str[12+1]; sprintf(val_str, "%d", val); field_cntrl->SetWindowText(val_str); } void PoroMediaDialogShare::SetLongValField(CEdit* field_cntrl, long val) { //max slice num is SLICE_DIG_LIMIT + '\0' char val_str[25+1]; //20 should be enough, but just in case sprintf(val_str, "%ld", val); field_cntrl->SetWindowText(val_str); } void PoroMediaDialogShare::SetFloatValField(CEdit* field_cntrl, double val) { //TODO: improve floating point displaying!! //max slice num is SLICE_DIG_LIMIT + '\0' char val_str[50+2]; //if (val < 1000) // sprintf(val_str, "%f", val); //else // sprintf(val_str, "%.2e", val); //sprintf(val_str, "%.4G", val); //Use the shorter of %f or %E (Scientific notation (mantise/exponent) using E character) sprintf(val_str, "%.5f", val); //Use the shorter of %f or %E (Scientific notation (mantise/exponent) using E character) unsigned short int len = (unsigned short)strlen(val_str); for (unsigned short int i = len-1; i > 0; i--) { if ( val_str[i] > '0' ) { if (i < len-1) val_str[i+1] = '\0'; break; } else if (val_str[i] == '.') { if (i < len-2) val_str[i+2] = '\0'; break; } } field_cntrl->SetWindowText(val_str); } //inits left bracket for file type inputs void PoroMediaDialogShare::InitLeftBracket(int input_num) { if (!( (input_file_ext == INPUT_IS_RAW_STACK) || (input_file_ext == INPUT_IS_BIN_STACK) || (input_file_ext == INPUT_IS_CBIN_STACK)) ){ // if this happens, might also need to change the input file '[' location int delta_indx = left_bracket_indx; int input_dig_cnt; // find the number of digits in the new number input_dig_cnt = FindNumberOfDigits(input_num); // find the new offset for left bracket if (cur_dig_cnt < input_dig_cnt){ delta_indx -= (input_dig_cnt - cur_dig_cnt); } else if (cur_dig_cnt > input_dig_cnt) { // insert '0's up front if new input is smaller than original for (int i = input_dig_cnt; i < cur_dig_cnt; i++) input_file_name.SetAt(delta_indx + i - input_dig_cnt + 1, '0'); delta_indx += (cur_dig_cnt - input_dig_cnt); } // update digit count variable cur_dig_cnt = input_dig_cnt; // remove old '[', and insert new one input_file_name.Delete(left_bracket_indx, 1); left_bracket_indx = delta_indx; input_file_name.Insert(left_bracket_indx, '['); // delete the old first slice number in input_file_name input_file_name.Delete(left_bracket_indx + 1, cur_dig_cnt); // update the numbers in input_file_name string char num_str[MAX_DIG_LIMIT+1]; sprintf(num_str, "%d", input_num); for (int i = 0; num_str[i] != '\0'; i++){ input_file_name.Insert(left_bracket_indx + 1 + i, num_str[i]); } // update the GUI directory+file field UpdateDirFileField(); } } // only called if a valid updated value is placed in the field_end_slice_cntrl void PoroMediaDialogShare::UpdateLeftBracket() { if ( (input_file_ext != INPUT_IS_BIN_STACK) && (input_file_ext != INPUT_IS_RAW_STACK) && (input_file_ext != INPUT_IS_CBIN_STACK)){ int new_dig_cnt; CString input_txt; field_end_slice_cntrl.GetWindowText(input_txt); new_dig_cnt = input_txt.GetLength(); //check that it does not exceed input file digits if ( new_dig_cnt <= max_dig_cnt ){ if (new_dig_cnt > cur_dig_cnt){ //check if it has more digits than the first slice int dig_diff; dig_diff = new_dig_cnt - cur_dig_cnt; if (dig_diff){ for (dig_diff; dig_diff > 0; dig_diff--){ //remove '[' from old spot, and move to left once input_file_name.Delete(left_bracket_indx, 1); left_bracket_indx--; input_file_name.Insert(left_bracket_indx, '['); } cur_dig_cnt = new_dig_cnt; } } if (new_dig_cnt < cur_dig_cnt){ //if the new one has shrunk back, move '[' back to the right int dig_diff; dig_diff = cur_dig_cnt - new_dig_cnt; if (dig_diff){ for (dig_diff; dig_diff > 0; dig_diff--){ //remove '[' from old spot, and move to right once input_file_name.Delete(left_bracket_indx, 1); left_bracket_indx++; input_file_name.Insert(left_bracket_indx, '['); } cur_dig_cnt = new_dig_cnt; } } //fill in input field UpdateDirFileField(); } } }