egami
C++ Image Processing program
/homes/esi30/DCS339/coursework/src/catalogue.cpp
Go to the documentation of this file.
00001 #include <algorithm>
00002 #include <memory>
00003 #include <string>
00004 
00005 #include "catalogue.hpp"
00006 #include "display_page.hpp"
00007 #include "display_unit.hpp"
00008 #include "processing_page.hpp"
00009 
00010 #include <gtkmm/enums.h>
00011 #include <gtkmm/messagedialog.h>
00012 
00013 Display_catalogue *Display_catalogue::instance;
00014 
00015 Display_catalogue::Display_catalogue(){
00016     instance=this;
00017 }
00018 
00019 void Display_catalogue::open_image(const std::string &filepath){
00020     std::string::const_iterator lastslash=std::find(filepath.rbegin(), filepath.rend(), '/').base();
00021     std::string::const_iterator lastdot=std::find(filepath.rbegin(), filepath.rend(), '.').base();
00022     std::string filename(lastslash, lastdot-1);
00023 
00024     std::shared_ptr<Display_page_header> dph(new Display_page_header(filename));
00025     std::shared_ptr<Image_unit> unit(new Image_unit(filepath));
00026     std::shared_ptr<Display_page> dp(new Display_page(unit));
00027     unit->set_page(dp.get());
00028     dph->assign(this, append(dph, dp));
00029     set_tab_reorderable(*dp);
00030 }
00031 
00032 void Display_catalogue::undo_current(){
00033     std::shared_ptr<Display_page> current=current_page();
00034     if(!current){
00035         Gtk::MessageDialog dialog("No image loaded", false, Gtk::MESSAGE_ERROR);
00036         dialog.run();
00037         return;
00038     }
00039     current->undo();
00040 }
00041 
00042 void Display_catalogue::redo_current(){
00043     std::shared_ptr<Display_page> current=current_page();
00044     if(!current){
00045         Gtk::MessageDialog dialog("No image loaded", false, Gtk::MESSAGE_ERROR);
00046         dialog.run();
00047         return;
00048     }
00049     current->redo();
00050 }
00051 
00052 void Display_catalogue::add_page(const std::string &title, Display_unit *unit){
00053     std::shared_ptr<Display_page_header> dph(new Display_page_header(title));
00054     Display_page *rdp;
00055 
00056     if(Image_unit *iu=dynamic_cast<Image_unit*>(unit))
00057         rdp=new Display_page((std::shared_ptr<Image_unit>(iu)));
00058     else if(Info_unit *iu=dynamic_cast<Info_unit*>(unit))
00059         rdp=new Display_page((std::shared_ptr<Info_unit>(iu)));
00060 
00061     dph->assign(instance, instance->append(dph, std::shared_ptr<Display_page>(rdp)));
00062     instance->set_tab_reorderable(*rdp);
00063 }
00064 
00065 std::shared_ptr<Display_page> Display_catalogue::current_page(){
00066     // Tricky, the return value of get_nth_page is not used in case the object is destroyed after the return of this function.
00067     // By using the shared_ptr of pages, even if the page is closed, the Display_page will survive until it's no more used.
00068     int cp=instance->get_current_page();
00069     if(cp==-1)
00070         return std::shared_ptr<Display_page>(nullptr);
00071     return instance->pages[dynamic_cast<Display_page_header*>(instance->get_tab_label(*instance->get_nth_page(cp)))->id].second;
00072 }
00073 
00074 std::vector<std::shared_ptr<Display_unit>> Display_catalogue::all_units(){
00075     std::vector<std::shared_ptr<Display_unit>> r;
00076     r.reserve(instance->pages.size());
00077     for(auto e: instance->pages)
00078         r.push_back(e.second.second->display_unit);
00079     return r;
00080 }
00081 
00082 bool Display_catalogue::save(){
00083     std::shared_ptr<Display_page> current=current_page();
00084     if(!current)
00085         return false;
00086     return current->save();
00087 }
00088 
00089 void Display_catalogue::save_as(const std::string &fp){
00090     std::shared_ptr<Display_page> current=current_page();
00091     if(!current){
00092         Gtk::MessageDialog dialog("No image loaded", false, Gtk::MESSAGE_ERROR);
00093         dialog.run();
00094         return;
00095     }
00096     current->save_as(fp);
00097 }
00098 
00099 Processing_catalogue::Processing_catalogue(){
00100     set_tab_pos(Gtk::POS_LEFT);
00101     create_processing_group<Processing_page>("Colours");
00102     create_processing_group<Processing_page>("Edge detection");
00103     create_processing_group<Processing_page>("Convolutions");
00104     create_processing_group<Processing_page>("Order statistic");
00105     create_processing_group<Processing_page>("Threshold");
00106     create_processing_group<Processing_page>("Reshape");
00107     create_processing_group<Processing_page>("Binary Op.");
00108     create_processing_group<Processing_page>("Tools");
00109 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends