Sie sind auf Seite 1von 10

#include #include #include #include

"Graph.h" "GUI.h" "aggiesnap.h" "std_lib_facilities_3.h"

/* This is the code for the AggieSnap program by Kevin Bradshaw, Liliana Balbo, and Michael Boutwell. This is a picture index program that allows the user to upload pictures through a website, add tags, and search through them with ease. */ using namespace Graph_lib; Point tl(01,01); //Window Origin //============================================================================== // // Introductory Window that displays instructions on how to use the program. struct Splash : Graph_lib::Window { Splash(Point xy, int w, int h, const string& title); private: Image intro; Button splash_quit_button; static void cb_quit(Address, Address); void quit(); void draw_lines(); }; Splash::Splash(Point xy, int w, int h, const string& title) :Window(xy,w,h,title), // Instruction image intro(Point(25,35),"intro.jpg"), // Button that closes window to continue to the AggieSnap program splash_quit_button(Point(x_max()-175,0),175, 30, "Continue to AggieSnap" , cb_quit) { attach(intro); attach(splash_quit_button); } void Splash::cb_quit(Address, Address pw) { reference_to<Splash>(pw).quit(); } void Splash::quit() { hide(); } //============================================================================== // struct Project : Graph_lib::Window { Project(Point xy, int w, int h, const string& title); vector<string>names{"howdy.jpg"}; // Vector holds image n ames. vector<Picture>all_tags; // Vector holds

all tags. int file_number = 0; string display_file = "howdy.jpg"; bool can_open(const string& s) // From FLTK_Image: Checks if a file named s exists and can be opened fo r reading. { ifstream ff(s.c_str()); return ff; } private: // Simple decor Text Title; Rectangle border1; Rectangle border2; Image decor1; Image display; //Input declarations In_box enter_filename; In_box rename; In_box search_input; In_box enter_tags1; boxes for tags In_box enter_tags2; In_box enter_tags3; In_box enter_tags4; In_box enter_tags5; //Output declarations Out_box tag_display; Out_box show_errors; //Button declarations Button search_button; Button get_picture_button; Button next_button; Button previous_button; Button quit_button; //Button callback functions static void cb_search(Address, Address); void search(); static void cb_get_picture(Address, Address); void get_picture(); static void cb_tag_picture(Address, Address); void tag_picture(); static void cb_next(Address, Address); void next(); static void cb_previous(Address, Address); void previous(); static void cb_quit(Address, Address); void quit(); void draw_lines(); }; //============================================================================== // Five

// // Main Window constructor: Project::Project(Point xy, int w, int h, const string& title) :Window(xy,w,h,title), // Title and Image implementation Title(Point(05,65), "AggieSnap!"), border1(Point(0,y_max()-470),400,400), border2(Point(x_max()-300,y_max()-300),300,225), decor1(Point(x_max()-300,y_max()-300),"background_logo.jpg"), /* Only one image is displayed at a time. The string 'display_file' is late r changed by the vector of image names. The address of vector is changed by the in itialized int file_number = 0. */ display(Point(0,y_max()-470), display_file), // In_box implementation enter_filename(Point(x_max()-300,100), 200,30, "URL:"), rename(Point(x_max()-300,130), 200,30, "Rename:"), search_input(Point(x_max()-300,0), 150,30, " "), enter_tags1(Point(x_max()-200,220), enter_tags2(Point(x_max()-100,220), enter_tags3(Point(x_max()-300,250), enter_tags4(Point(x_max()-200,250), enter_tags5(Point(x_max()-100,250), 100,30, 100,30, 100,30, 100,30, 100,30, "Tag Picture: "), " "), " "), " "), " "),

// Out_box implementation tag_display(Point(50,y_max()-70),350,30, "Tags:"), show_errors(Point(x_max()-300,160), 300,30, "Status:"), // Button implementation search_button(Point(x_max()-150,0), 80, 30, "Search", cb_search), get_picture_button(Point(x_max()-100,100),100, 60, "Get Picture!", cb_ge t_picture), next_button(Point(x_max()-525,y_max()-40),125,20, "Next", cb_next), previous_button(Point(0,y_max()-40),125,20, "Previous", cb_previous), quit_button(Point(x_max()-70,0),70, 30, "Quit", cb_quit) { // ALL attachments are in this constructor. No new windows created other than Splash window. Title.set_font_size(70); Title.set_color(Color::red); attach(Title); attach(border1); decor1.resize(300,225); attach(decor1); border2.set_style(Line_style(Line_style::solid, 6)); attach(border2); display.resize(400,400); attach(display); attach(enter_filename); attach(rename); attach(search_input);

attach(enter_tags1); attach(enter_tags2); attach(enter_tags3); attach(enter_tags4); attach(enter_tags5); attach(tag_display); attach(show_errors); attach(search_button); attach(get_picture_button); attach(next_button); attach(previous_button); attach(quit_button); } //============================================================================== // // Search Button Implementation void Project::cb_search(Address, Address pw) { reference_to<Project>(pw).search(); } void Project::search() { /* Tags are searched from a text file that's created in the 'get_picture ' implementation. The filename and 5 tags are read into a Picture object a nd are put into a vector of Pictures. */ string filename; string t1; string t2; string t3; string t4; string t5; ifstream tags_index; vector<string>tags; tags.resize(5, ""); tags_index.open("Tags Index.txt", ios_base::in); if (!tags_index) error("can't open output file"); while (tags_index >> filename >> tags[0] >> tags[1] >> tags[2] >> tags[3 ] >> tags[4]) { Picture *new_pic = new Picture(filename, tags); all_tags.push_back(*new_pic); } tags_index.close(); // The input from the search box is compared to all tags pushed into the vector. string search = search_input.get_string(); cerr << "getting ready to search for " << search << endl; for (int i = 0; i<all_tags.size(); ++i) {

Picture pic = all_tags[i]; string pic_name = pic.get_filename(); vector<string> pic_tags(pic.get_tags()); for (int j = 0; j < 5; ++j){ if (search == pic_tags[j]){ tag_display.put(pic_name + ": " + pic_tags[0] + ", " + pic_tags[1] + ", " + pic_tags[2] + ", " + pic_tags[3] + ", " + pic_tags[4]); /* When an image is searched, it is also attache d to the current display. All other tags associated with that pic ture are then displayed. */ Picture_type image1(pic_name, ".jpg"); Picture_type image2(pic_name, ".gif"); Picture_type image3(pic_name, ".jpeg"); display_file = image1.full(); if (!can_open(display_file)) { display_file = image2.full(); } else if(!can_open(display_file)) { display_file = image3.full(); } else if(!can_open(display_file)) { tag_display.put("Can't Find Image."); } display.change_image(display_file); display.resize(400,400); redraw(); return; } } } tag_display.put("No matches found"); } //============================================================================== // // Get Picture Button Implementation void Project::cb_get_picture(Address, Address pw) { reference_to<Project>(pw).get_picture(); } void Project::get_picture() { /* Text file opens that saves only the image name and extension. To grab a picture from a URL, the in_box takes a string then compares if it's a .jpg, .gif, or a .jpeg. It saves the image into the same directory of the aggiesnap.cpp. */ string current; fstream index;

index.open("Picture Index.txt", ios_base::app | ios_base::out); if (!index) error("can't open output file"); string new_url = enter_filename.get_string(); string string string string string string string string string string string str1 = new_url.substr (0,7); str2 = new_url.substr (new_url.size()-3,3); str3 = new_url.substr (new_url.size()-4,4); initial = "http://"; final1 = "jpg"; final2 = "JPG"; final3 = "gif"; final4 = "GIF"; final5 = "jpeg"; final6 = "JPEG"; new_name = rename.get_string();

if(str1 == initial && (str2 == final1 || str2 == final2)){ string a = "wget " + new_url + " -O" + new_name + ".jpg"; system(a.c_str()); show_errors.put("Image Saved!"); // Shows status of the image string save_image_name = new_name + ".jpg"; // New n ame written index << new_name << ".jpg" << endl; // Name written in text file } else if(str1 == initial && (str2 == final3 || str2 == final4)){ string b = "wget " + new_url + " -O" + new_name + ".gif"; system(b.c_str()); show_errors.put("Image Saved!"); string save_image_name = new_name + ".gif"; index << new_name << ".gif" << endl; } else if(str1 == initial && (str3 == final5 || str3 == final6)){ string c = "wget " + new_url + " -O" + new_name + ".jpeg"; system(c.c_str()); show_errors.put("Image Saved!"); string save_image_name = new_name + ".jpeg"; index << new_name << ".jpeg" << endl; } else{ show_errors.put("Invalid URL"); } index.close(); //==========================// // Tagging the Picture fstream tags_index; tags_index.open("Tags Index.txt", ios_base::app | ios_base::out); if (!tags_index) error("can't open output file "); vector<string> tag_from_box; tag_from_box.resize(5, ""); string f1 = enter_filename.get_string(); the new file name tag_from_box[0] = enter_tags1.get_string(); tags tag_from_box[1] = enter_tags2.get_string(); tag_from_box[2] = enter_tags3.get_string(); tag_from_box[3] = enter_tags4.get_string(); tag_from_box[4] = enter_tags5.get_string(); // Takes // Five

Picture image1(rename.get_string(), tag_from_box); tags_index for (int i { tags_index } tags_index } //============================================================================== // // Next Button Implementation void Project::cb_next(Address, Address pw) { reference_to<Project>(pw).next(); } void Project::next() { string current; fstream index; index.open("Picture Index.txt", ios_base::in); if (!index) error("can't open output file"); while (getline(index, current)) names.push_back(current); index.close(); /* File Number initially at 0, is incremented to change the details of the image displayed. */ if(file_number < names.size() - 1) { ++file_number; } else file_number = 0; //==========================// /* Same code for tagging pictures but in this case, used for displaying the current tag corresponding to the current image displayed. */ string filename; string t1; string t2; string t3; string t4; string t5; ifstream tags_index; vector<string>tags; tags.resize(5, ""); tags_index.open("Tags Index.txt", ios_base::in); << image1.get_filename() << " "; = 0; i<5; ++i) << tag_from_box[i] << " "; << endl;

if (!tags_index) error("can't open output file"); while (tags_index >> filename >> tags[0] >> tags[1] >> tags[2] >> tags[3 ] >> tags[4]) { Picture *new_pic = new Picture(filename, tags); all_tags.push_back(*new_pic); } tags_index.close(); Picture pic = all_tags[file_number]; string pic_name = pic.get_filename(); vector<string> pic_tags(pic.get_tags()); tag_display.put(pic_name + ": " + pic_tags[0] + ", " + pic_tags[1] + ", " + pic_tags[2] + ", " + pic_tags[3] + ", " + pic_tags[4]); //==========================// // String for the image is modified, changing what image is then display ed. string display_file = names[file_number]; display.change_image(display_file); function in Graph.cpp display.resize(400,400); ied FLTK function in Graph.cpp redraw(); names.clear(); } // Modified FLTK // Modif

//============================================================================== // // Previous Button Implementation // Same code for previous button but instead, implemented backwards. void Project::cb_previous(Address, Address pw) { reference_to<Project>(pw).previous(); } void Project::previous() { string current; fstream index; index.open("Picture Index.txt", ios_base::in); if (!index) error("can't open output file"); while (getline(index, current)) names.push_back(current); index.close(); if(file_number <= names.size() - 1 && file_number != 0) { --file_number; } else if (file_number == 0) file_number = names.size()-1; //==========================// string filename;

string string string string string

t1; t2; t3; t4; t5;

ifstream tags_index; vector<string>tags; tags.resize(5, ""); tags_index.open("Tags Index.txt", ios_base::in); if (!tags_index) error("can't open output file"); while (tags_index >> filename >> tags[0] >> tags[1] >> tags[2] >> tags[3 ] >> tags[4]) { Picture *new_pic = new Picture(filename, tags); all_tags.push_back(*new_pic); } tags_index.close(); Picture pic = all_tags[file_number]; string pic_name = pic.get_filename(); vector<string> pic_tags(pic.get_tags()); tag_display.put(pic_name + ": " + pic_tags[0] + ", " + pic_tags[1] + ", " + pic_tags[2] + ", " + pic_tags[3] + ", " + pic_tags[4]); //==========================// string display_file = names[file_number]; display.change_image(display_file); display.resize(400,400); redraw(); names.clear(); } //============================================================================== // // Quit Button Implementation void Project::cb_quit(Address, Address pw) { reference_to<Project>(pw).quit(); } void Project::quit() { hide(); } //============================================================================== // // Main Implementation: Windows are created! int main(){ try { Project win(tl, 800, 600, "AggieSnap!"); Splash win2(Point(25,25), 600, 450, "Instructions"); return gui_main(); }

catch(exception& e){ cerr << "Error: " << e.what() << endl; return 1; } catch(...){ cerr << "Unknown Error." << endl; return 2; } }