⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainwindow.cpp

📁 Ocr source code. provides an Ocr engine that works in C++ language.
💻 CPP
字号:
/* mainwindow.cpp : code for the main window + custom widgets * Author: Maxie D. Schmidt (created 5/21/2006)               */#include "mainwindow.h"/////// drawing_area:main_drawing_area::main_drawing_area(int size_x, int size_y) :                                                 drawing_area(size_x, size_y) {                                                     signals.signal_main_refresh().connect(sigc::mem_fun(*this,                                     &main_drawing_area::refresh));}void main_drawing_area::refresh() {     if((pf != NULL) && (pf->is_valid)) {                    draw_image();          draw_parse_boundary();     }     if((pf != NULL) && (pf->parsed))          draw_parse_list(pf->plist);}bool main_drawing_area::on_expose_event(GdkEventExpose *event) {     refresh();     return true;}void main_drawing_area::draw_image() {     Glib::RefPtr<Gdk::GC> gc = get_style()->get_black_gc();     Glib::RefPtr<Gdk::Window> win = get_window();     int height = pf->height, width = pf->width;     win->draw_rgb_image(gc, 0, 0, width, height, Gdk::RGB_DITHER_MAX,                         pf->buf, 3 * width);}/////// image_display:image_display::image_display() : da(DA_DEFAULT_X_SIZE, DA_DEFAULT_Y_SIZE) {     set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);     set_size_request(SWIN_DEFAULT_X_SIZE, SWIN_DEFAULT_Y_SIZE);     signals.signal_new_scan().connect(sigc::mem_fun(*this,                                        &image_display::on_new_scan));     add(da);     show_all_children();     }void image_display::on_new_scan() {     if((pf != NULL) && (pf->is_valid)) {               da.set_size(pf->width, pf->height);          da.refresh();     }}/////// text_list:text_list::text_list() {     set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);     add(tree_view);     // create tree model:     ref_tree_model = Gtk::ListStore::create(m_columns);     tree_view.set_model(ref_tree_model);     tree_view.append_column("Line", m_columns.col_num);     tree_view.append_column("Text", m_columns.col_text);     // signal handler:     signals.signal_new_line().connect(sigc::mem_fun(*this,                                        &text_list::on_new_line));     show_all_children();}void text_list::on_new_line(translated_line line) {     append_row(line.line_num, line.text);}void text_list::append_row(int line_num, Glib::ustring text) {     row = *(ref_tree_model->append());     row[m_columns.col_num] = line_num;     row[m_columns.col_text] = text;}void text_list::clear_list() {     ref_tree_model->clear();}/////// main_window:main_window::main_window() {     set_title(CLASSNOTES_VER_STR);     set_border_width(10);     set_default_size(MAIN_WIN_DEFAULT_X_SIZE, MAIN_WIN_DEFAULT_Y_SIZE);     // menus:     {     Gtk::Menu::MenuList &menu_list = menu_file.items();     menu_list.push_back(Gtk::Menu_Helpers::MenuElem("_Open PNG ...",               sigc::mem_fun(*this, &main_window::on_menu_file_open_png)));     menu_list.push_back(Gtk::Menu_Helpers::MenuElem("_Save As ...",               sigc::mem_fun(*this, &main_window::on_menu_file_saveas)));     menu_list.push_back(Gtk::Menu_Helpers::MenuElem("E_xit",               sigc::mem_fun(*this, &main_window::on_menu_file_exit)));     }     {     Gtk::Menu::MenuList &menu_list = menu_parse.items();     menu_list.push_back(Gtk::Menu_Helpers::MenuElem("_Parse",               sigc::mem_fun(*this, &main_window::on_menu_parse_parse)));     menu_list.push_back(Gtk::Menu_Helpers::MenuElem("Parse _Stats",               sigc::mem_fun(*this, &main_window::on_menu_parse_parse_stats)));     menu_list.push_back(Gtk::Menu_Helpers::MenuElem("C_lear All",              sigc::mem_fun(*this, &main_window::on_menu_parse_clear_all)));     menu_list.push_back(Gtk::Menu_Helpers::MenuElem("New Scan Set",               sigc::mem_fun(*this, &main_window::on_menu_parse_newscanset)));     menu_list.push_back(Gtk::Menu_Helpers::MenuElem("Train With This Scan",              sigc::mem_fun(*this, &main_window::on_menu_parse_train_with_scan)));     menu_list.push_back(Gtk::Menu_Helpers::MenuElem("Enter _Training",               sigc::mem_fun(*this, &main_window::on_menu_parse_training)));     }         {     Gtk::Menu::MenuList &menu_list = menu_profile.items();     menu_list.push_back(Gtk::Menu_Helpers::MenuElem("Profi_le Stats",            sigc::mem_fun(*this,&main_window::on_menu_profile_profile_stats)));     menu_list.push_back(Gtk::Menu_Helpers::MenuElem("_Write Profile",            sigc::mem_fun(*this, &main_window::on_menu_profile_write_profile)));     menu_list.push_back(Gtk::Menu_Helpers::MenuElem("Clear P_rofile",            sigc::mem_fun(*this, &main_window::on_menu_profile_clear_profile)));     menu_list.push_back(Gtk::Menu_Helpers::MenuElem("Load _Profile",           sigc::mem_fun(*this, &main_window::on_menu_profile_load_profile)));     }          {     Gtk::Menu::MenuList &menu_list = menu_misc.items();     menu_list.push_back(Gtk::Menu_Helpers::MenuElem("Conf_iguration",               sigc::mem_fun(*this, &main_window::on_menu_misc_general_config)));     menu_list.push_back(Gtk::Menu_Helpers::MenuElem("Save Configuratio_n",              sigc::mem_fun(*this, &main_window::on_menu_misc_save_config)));     menu_list.push_back(Gtk::Menu_Helpers::MenuElem("View _Logs",              sigc::mem_fun(*this, &main_window::on_menu_misc_view_logs)));     }     menu_bar.items().push_back(Gtk::Menu_Helpers::MenuElem("_File",menu_file));     menu_bar.items().push_back(Gtk::Menu_Helpers::MenuElem("_Parse",                                menu_parse));     menu_bar.items().push_back(Gtk::Menu_Helpers::MenuElem("Prof_ile_",                                menu_profile));     menu_bar.items().push_back(Gtk::Menu_Helpers::MenuElem("_Misc",menu_misc));     // setup divider parts:     main_box.pack_start(menu_bar, Gtk::PACK_SHRINK);     main_box.pack_start(img_display);     add(divider);     divider.add1(main_box);     divider.add2(translated_text);     show_all_children();}void main_window::on_menu_file_open_png() {     Gtk::FileChooserDialog dialog("Choose a png scan",                                    Gtk::FILE_CHOOSER_ACTION_OPEN);     dialog.set_current_folder(cfg_scan_dir);     dialog.set_transient_for(*this);     dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);     dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);          int result = dialog.run();     if(result == Gtk::RESPONSE_OK) {          cfg_scan_dir = dialog.get_current_folder();          write_config_file(CONFIG_FILE_PATH);                    string filename = dialog.get_filename();          png_file *pfile = new png_file();          if(init_png_file(filename, pfile)) {               png_file *temp = pf;               pf = pfile;               delete temp;               signals.do_new_scan();          }          else {               delete pfile;               popup_message(*this, "An error occurred while loading the file. Check the logs for more detailed information.");          }     }}void main_window::on_menu_file_saveas() {     Gtk::FileChooserDialog dialog("Select a file for saving",                                    Gtk::FILE_CHOOSER_ACTION_SAVE);     dialog.set_transient_for(*this);     dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);     dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK);     int run_result = dialog.run();     if(run_result == Gtk::RESPONSE_OK) {                    if(!save_text(dialog.get_filename(), false))               popup_message(*this, "Unable to save the file.");          else               text_saved = true;     }}void main_window::on_menu_file_exit() {     if(!text_saved) {                    Gtk::MessageDialog dialog(*this, "Save text before exiting?", false,                                     Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO);          int result = dialog.run();          if(result == Gtk::RESPONSE_YES)               on_menu_file_saveas();     } // if          hide();}void main_window::on_menu_parse_parse_stats() {     if(pf->parsed) {                    parse_stats_dialog dialog;          dialog.run();     }     else {          popup_message(*this, "Parse an image before viewing parse stats.");     }}void main_window::on_menu_parse_parse() {     if(prof == NULL) {                    popup_message(*this, "Need a valid profile in order to parse.");          return;     }          if(pf == NULL)          return;     else if(pf->plist == NULL)          pf->plist = new parse_list();     if(!fill_parse_list(trcfg_line_height_int, pf, pf->plist)) {         pf->plist->clear();         popup_message(*this, "in on_menu_misc_parse: Error filling plist!");         return;     }          // translate characters:     parsed_line *cur_pl = pf->plist->pl_begin;     parsed_char *cur_pc;     grid_t char_grid;     vector<char_probability> prs;     translated_line tline;     tline.line_num = pf->scan_starting_line_num;          while(cur_pl != NULL) {          tline.line_num++;          tline.text = "";          cur_pc = cur_pl->pc_begin;          while(cur_pc != NULL) {               // see if have a space to handle:               if(cur_pc != cur_pl->pc_begin) {                    int char_spacing = cur_pc->self_node.self_box.xy.x -                                  (cur_pc->prev->self_node.self_box.xy.x +                                   cur_pc->prev->self_node.self_box.horiz_len);                    if((char_spacing >= trcfg_space_int.lower) &&                       (char_spacing <= trcfg_space_int.upper))                         tline.text = tline.text + ' ';               }                              char_grid = grid_char(cur_pc->self_node.self_box);               prs = identify_character(char_grid);                if(prs.size() != 0) {                                        cur_pc->self_node.character = prs[0].character;                    tline.text = tline.text + prs[0].character;                    cur_pc->self_node.char_matches.clear();                    for(int i = 0; i < parse_dialog_num_matches; i++)                         cur_pc->self_node.char_matches.push_back(prs[i]);               }               cur_pc = cur_pc->next;          }          cur_pl = cur_pl->next;          text_lines.push_back(tline);          signals.do_new_line(tline);     }          pf->parsed = true;     text_saved = false;     signals.do_main_refresh();}void main_window::on_menu_parse_clear_all() {     pf->plist->clear();     pf->parsed = false;     text_saved = true;     translated_text.clear_list();     text_lines.clear();     signals.do_main_refresh();     }void main_window::on_menu_parse_newscanset() {     new_scan_set = true;     delete prof;     prof = NULL;     prompt_for_profile_name = true;}void main_window::on_menu_parse_train_with_scan() {     if(!pf->parsed)          on_menu_parse_training();     else {          string parsed_text = "";          for(int i = 0; i < text_lines.size(); i++)               parsed_text = parsed_text + text_lines[i].text + "\n";                    // when the training close button is clicked the lines will          // get filled in with the updated text:          translated_text.clear_list();          text_lines.clear();          init_training(true);          training_window t_win(parsed_text);          t_win.run();          signals.do_main_refresh();          }}void main_window::on_menu_parse_training() {     if(!(pf->is_valid))          popup_message(*this, "Select an image before entering training.");     else {          init_training(false);          training_window t_win;          t_win.run();     }}void main_window::on_menu_profile_profile_stats() {     if(prof != NULL) {                    profile_stats_dialog dialog(prof);          dialog.run();     }     else           popup_message(*this, "Need to load a profile before viewing stats.");}void main_window::on_menu_profile_write_profile() {     if(prof != NULL) {          profile_name_dialog pn_dialog;          pn_dialog.run();          write_profile_to_file(new_profile_name, prof);          cfg_profile = new_profile_name;          write_config_file(CONFIG_FILE_PATH);     }     else          popup_message(*this, "Cannot write NULL profile.");}void main_window::on_menu_profile_clear_profile() {     delete prof;     prof = NULL;     prompt_for_profile_name = true;}void main_window::on_menu_profile_load_profile() {     // don't clobber the existing profile:     if(prof != NULL)          write_profile_to_file(cfg_profile, prof);               // get the new profile's name:     Gtk::FileChooserDialog dialog("Choose a profile",                                    Gtk::FILE_CHOOSER_ACTION_OPEN);     dialog.set_current_folder(cfg_profile_dir);     dialog.set_transient_for(*this);     dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);     dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);          int result = dialog.run();     if(result == Gtk::RESPONSE_OK) {          string profile = dialog.get_filename();          bool reload_old = !load_profile_from_file(profile);          if(reload_old) {               delete prof;               if(!load_profile_from_file(cfg_profile)) {                    delete prof;                    prof = NULL;               }          }          else {                              cfg_profile = profile;               write_config_file(CONFIG_FILE_PATH);          }     } // if Gtk::RESPONSE_OK}void main_window::on_menu_misc_general_config() {     cfg_dialog config_window(NORMAL);     config_window.run();}void main_window::on_menu_misc_save_config() {     write_config_file(CONFIG_FILE_PATH);}void main_window::on_menu_misc_view_logs() {     log_file_dialog dialog;     dialog.run();}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -