📄 kognition.cpp
字号:
/****************************************************************************** * 光学字符识别程序 * 文件名:kognition.cpp * 功能 :kognitionapp类的实现 * modified by PRTsinghua@hotmail.com******************************************************************************/// QT 相关头文件#include <qdir.h>#include <qprinter.h>#include <qpainter.h>// KDE 相关头文件#include <kiconloader.h>#include <kmessagebox.h>#include <kfiledialog.h>#include <kmenubar.h>#include <kstatusbar.h>#include <klocale.h>#include <kconfig.h>#include <kstdaction.h>#include <klineeditdlg.h>#include <kstandarddirs.h>// 应用相关头文件#include "global.h"#include "kognition.h"#include "kognitiondoc.h"#define ID_STATUS_MSG 1KognitionApp::KognitionApp(QWidget* , const char* name) : KMainWindow(0, name), CharDBString( "abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789" ){ config=kapp->config(); CurrentDir=locate("appdata", "examples/"); // 调用inits,进行构造 initStatusBar(); initActions(); initDocument(); readOptions(); // disable所有响应 fileSave->setEnabled(false); fileSaveAs->setEnabled(false); filePrint->setEnabled(false); editCut->setEnabled(false); editCopy->setEnabled(false); editPaste->setEnabled(false);}KognitionApp::~KognitionApp(){}void KognitionApp::initActions(){ fileNew = KStdAction::openNew(this, SLOT(slotFileNew()), actionCollection()); fileOpen = KStdAction::open(this, SLOT(slotFileOpen()), actionCollection()); fileOpenRecent = KStdAction::openRecent(this, SLOT(slotFileOpenRecent(const KURL&)), actionCollection()); fileSave = KStdAction::save(this, SLOT(slotFileSave()), actionCollection()); fileSaveAs = KStdAction::saveAs(this, SLOT(slotFileSaveAs()), actionCollection()); fileClose = KStdAction::close(this, SLOT(slotFileClose()), actionCollection()); filePrint = KStdAction::print(this, SLOT(slotFilePrint()), actionCollection()); fileNewWindow = new KAction(i18n("New &Window"), "window_new", 0, this, SLOT(slotFileNewWindow()), actionCollection(),"file_new_window"); fileCreateNewDatabase = new KAction(i18n("Create New Database"), "filenew", 0, this, SLOT(slotFileCreateNewDatabase()), actionCollection(),"file_create_new_database"); fileFileAppendToDatabase = new KAction(i18n("Append To Database"), "attach", 0, this, SLOT(slotFileAppendToDatabase()), actionCollection(),"file_append_to_database"); fileSaveDatabaseAs = new KAction(i18n("Save Database As"), "filesave", 0, this, SLOT(slotFileSaveDatabaseAs()), actionCollection(),"file_Save_Database_As"); fileQuit = KStdAction::quit(this, SLOT(slotFileQuit()), actionCollection()); editReplaceChar = new KAction(i18n("Replace Char In Database"), "filenew", 0, this, SLOT(slotEditReplaceChar()), actionCollection(),"edit_replace_char"); editVariantChar = new KAction(i18n("Append Char Variant To Database"), "attach", 0, this, SLOT(slotEditVariantChar()), actionCollection(),"edit_variant_char"); editCut = KStdAction::cut(this, SLOT(slotEditCut()), actionCollection()); editCopy = KStdAction::copy(this, SLOT(slotEditCopy()), actionCollection()); editPaste = KStdAction::paste(this, SLOT(slotEditPaste()), actionCollection()); viewToolBar = KStdAction::showToolbar(this, SLOT(slotViewToolBar()), actionCollection()); viewStatusBar = KStdAction::showStatusbar(this, SLOT(slotViewStatusBar()), actionCollection()); debugImage = new KToggleAction(i18n("Show &character"), Key_C, this, SLOT(slotDebugImage()), actionCollection(),"debug_image"); debugHistogram = new KToggleAction(i18n("Show &histogram"), Key_H, this, SLOT(slotDebugHistogram()), actionCollection(),"debug_histogram"); debugBorderLists = new KToggleAction(i18n("Show &border lists"), Key_1, this, SLOT(slotDebugBorderLists()), actionCollection(),"debug_border_lists"); debugBorderAngles = new KToggleAction(i18n("Show border &angles"), Key_2, this, SLOT(slotDebugBorderAngles()), actionCollection(),"debug_border_angles"); debugCrossSections = new KToggleAction(i18n("Show cross sectio&ns"), Key_3, this, SLOT(slotDebugCrossSections()), actionCollection(),"debug_cross_sections"); debugSingularRegions = new KToggleAction(i18n("Show singular ®ions"), Key_4, this, SLOT(slotDebugSingularRegions()), actionCollection(),"debug_singular_regions"); debugSkeleton = new KToggleAction(i18n("Show &skeleton"), Key_5, this, SLOT(slotDebugSkeleton()), actionCollection(),"debug_skeleton"); debugDirections = new KToggleAction(i18n("Show &directions"), Key_6, this, SLOT(slotDebugDirections()), actionCollection(),"debug_directions"); debugPolySkeleton = new KToggleAction(i18n("Show &polygon skeleton"), Key_7, this, SLOT(slotDebugPolySkeleton()), actionCollection(),"debug_poly_skeleton"); debugAbstractChar = new KToggleAction(i18n("Show abstract &char"), Key_8, this, SLOT(slotDebugAbstractChar()), actionCollection(),"debug_abstract_char"); debugMatchingState = new KToggleAction(i18n("Show matching state"), Key_9, this, SLOT(slotDebugMatchingState()), actionCollection(),"debug_matching_state"); debugGrid = new KToggleAction(i18n("Show &grid"), Key_R, this, SLOT(slotDebugGrid()), actionCollection(),"debug_grid"); debugBW = new KToggleAction(i18n("Show in black/&white"), Key_W, this, SLOT(slotDebugBW()), actionCollection(),"debug_bw"); debugBorderAnglesSmooth = new KToggleAction(i18n("Show s&moothed border angles"), Key_S, this, SLOT(slotDebugBorderAnglesSmooth()), actionCollection(),"debug_border_angles_smooth"); debugGuessedLines = new KToggleAction(i18n("Show &guessed lines"), Key_G, this, SLOT(slotDebugGuessedLines()), actionCollection(),"debug_guessed_lines"); debugBoundingboxes = new KToggleAction(i18n("Show b&oundingboxes"), Key_B, this, SLOT(slotDebugBoundingboxes()), actionCollection(),"debug_boundingboxes"); debugMetaboxes = new KToggleAction(i18n("Show &metaboxes"), Key_M, this, SLOT(slotDebugMetaboxes()), actionCollection(),"debug_metaboxes"); debugBaseline = new KToggleAction(i18n("Show base&line"), Key_L, this, SLOT(slotDebugBaseline()), actionCollection(),"debug_baseline"); debugBasepoints = new KToggleAction(i18n("Show basepo&ints"), Key_P, this, SLOT(slotDebugBasepoints()), actionCollection(),"debug_basepoints"); debugMaxPart = new KToggleAction(i18n("Show &tallest character part"), Key_T, this, SLOT(slotDebugMaxPart()), actionCollection(),"debug_maxpart"); debugRememberSettings = new KToggleAction(i18n("Re&member Debug Settings"), 0, actionCollection(),"debug_remember_settings"); fileNewWindow->setStatusText(i18n("Opens a new application window")); fileNew->setStatusText(i18n("Creates a new document")); fileOpen->setStatusText(i18n("Opens an existing document")); fileOpenRecent->setStatusText(i18n("Opens a recently used file")); fileSave->setStatusText(i18n("Saves the actual document")); fileSaveAs->setStatusText(i18n("Saves the actual document as...")); fileClose->setStatusText(i18n("Closes the actual document")); filePrint ->setStatusText(i18n("Prints out the actual document")); fileQuit->setStatusText(i18n("Quits the application")); editCut->setStatusText(i18n("Cuts the selected section and puts it to the clipboard")); editCopy->setStatusText(i18n("Copies the selected section to the clipboard")); editPaste->setStatusText(i18n("Pastes the clipboard contents to actual position")); viewToolBar->setStatusText(i18n("Enables/disables the toolbar")); viewStatusBar->setStatusText(i18n("Enables/disables the statusbar")); // 最近文件列表的最大文件数 fileOpenRecent->setMaxItems(20); createGUI();}void KognitionApp::initStatusBar(){ // 状态条 statusBar()->insertItem(i18n("Ready."), ID_STATUS_MSG);}void KognitionApp::initDocument(){ doc = new KognitionDoc( this ); doc->newDocument(); setCentralWidget(doc); setCaption(doc->URL().fileName(),false);}void KognitionApp::openDocumentFile(const KURL& url){ slotStatusMsg(i18n("Opening file...")); doc->openDocument( url); fileOpenRecent->addURL( url ); slotStatusMsg(i18n("Ready."));}KognitionDoc *KognitionApp::getDocument() const{ return doc;}void KognitionApp::saveOptions(){ config->setGroup("General Options"); config->writeEntry("Geometry", size()); config->writeEntry("CharGeometry", doc->img->size()); config->writeEntry("CharPosition", doc->img->pos()); config->writeEntry("HistGeometry", doc->hist->size()); config->writeEntry("HistPosition", doc->hist->pos()); config->writeEntry("Show Toolbar", viewToolBar->isChecked()); config->writeEntry("Show Statusbar",viewStatusBar->isChecked()); config->writeEntry("ToolBarPos", (int) toolBar("mainToolBar")->barPos()); // 保存调试设定 if (debugRememberSettings->isChecked()) { config->writeEntry("Show character", debugImage->isChecked()); config->writeEntry("Show histogram", debugHistogram->isChecked()); config->writeEntry("Show tallest character part", debugMaxPart->isChecked()); config->writeEntry("Remember Debug Settings", true); config->writeEntry("Show border lists", debugBorderLists->isChecked()); config->writeEntry("Show border angles", debugBorderAngles->isChecked()); config->writeEntry("Show cross sections", debugCrossSections->isChecked()); config->writeEntry("Show singular regions", debugSingularRegions->isChecked()); config->writeEntry("Show skeleton", debugSkeleton->isChecked()); config->writeEntry("Show directions", debugDirections->isChecked()); config->writeEntry("Show polygon skeleton", debugPolySkeleton->isChecked()); config->writeEntry("Show abstract char", debugAbstractChar->isChecked()); config->writeEntry("Show matching state", debugMatchingState->isChecked()); config->writeEntry("Show grid", debugGrid->isChecked()); config->writeEntry("Show in black/white", debugBW->isChecked()); config->writeEntry("Show smoothed border angles", debugBorderAnglesSmooth->isChecked()); config->writeEntry("Show guessed lines", debugGuessedLines->isChecked()); config->writeEntry("Show boundingboxes", debugBoundingboxes->isChecked()); config->writeEntry("Show metaboxes", debugMetaboxes->isChecked()); config->writeEntry("Show baseline", debugBaseline->isChecked()); config->writeEntry("Show basepoints", debugBasepoints->isChecked()); } else config->writeEntry("Remember Debug Settings", false); fileOpenRecent->saveEntries(config,"Recent Files");}void KognitionApp::readOptions(){ config->setGroup("General Options"); // 工具栏状态设置 bool bViewToolbar = config->readBoolEntry("Show Toolbar", true); viewToolBar->setChecked(bViewToolbar); slotViewToolBar(); bool bViewStatusbar = config->readBoolEntry("Show Statusbar", true); viewStatusBar->setChecked(bViewStatusbar); slotViewStatusBar(); // 工具栏位置设置 KToolBar::BarPosition toolBarPos; toolBarPos=(KToolBar::BarPosition) config->readNumEntry("ToolBarPos", KToolBar::Top); toolBar("mainToolBar")->setBarPos(toolBarPos); // 初始化最近文件列表 fileOpenRecent->loadEntries(config,"Recent Files"); QSize size=config->readSizeEntry("Geometry"); if(!size.isEmpty()) resize(size); // 初始化字符窗口 size = config->readSizeEntry("CharGeometry"); if(!size.isEmpty()) doc->img->resize(size); QPoint pos = config->readPointEntry("CharPosition"); doc->img->move(pos); // 初始化直方图窗口 size = config->readSizeEntry("HistGeometry"); if(!size.isEmpty()) doc->hist->resize(size); pos = config->readPointEntry("HistPosition"); doc->hist->move(pos); // 恢复保存的调试设置 if (config->readBoolEntry("Remember Debug Settings", true)) { debugRememberSettings->setChecked(true); debugImage->setChecked(config->readBoolEntry("Show character", true)); debugHistogram->setChecked(config->readBoolEntry("Show histogram", false)); debugBorderLists->setChecked(config->readBoolEntry("Show border lists", false)); debugBorderAngles->setChecked(config->readBoolEntry("Show border angles", false)); debugCrossSections->setChecked(config->readBoolEntry("Show cross sections", false)); debugSingularRegions->setChecked(config->readBoolEntry("Show singular regions", false)); debugSkeleton->setChecked(config->readBoolEntry("Show skeleton", false)); debugDirections->setChecked(config->readBoolEntry("Show directions", false)); debugPolySkeleton->setChecked(config->readBoolEntry("Show polygon skeleton", false)); debugAbstractChar->setChecked(config->readBoolEntry("Show abstract char", false)); debugMatchingState->setChecked(config->readBoolEntry("Show matching state", false)); debugGrid->setChecked(config->readBoolEntry("Show grid", true)); debugBW->setChecked(config->readBoolEntry("Show in black/white", false)); debugBorderAnglesSmooth->setChecked(config->readBoolEntry("Show smoothed border angles", false)); debugGuessedLines->setChecked(config->readBoolEntry("Show guessed lines", false)); debugBoundingboxes->setChecked(config->readBoolEntry("Show boundingboxes", true)); debugMetaboxes->setChecked(config->readBoolEntry("Show metaboxes", false)); debugBaseline->setChecked(config->readBoolEntry("Show baseline", true)); debugBasepoints->setChecked(config->readBoolEntry("Show basepoints", true)); debugMaxPart->setChecked(config->readBoolEntry("Show tallest character part", true)); // 应用调试设置 slotDebugHistogram(); slotDebugImage(); slotDebugBorderLists(); slotDebugBorderAngles(); slotDebugCrossSections(); slotDebugSingularRegions(); slotDebugSkeleton(); slotDebugDirections(); slotDebugPolySkeleton(); slotDebugAbstractChar(); slotDebugMatchingState(); slotDebugGrid(); slotDebugBW(); slotDebugBorderAnglesSmooth(); slotDebugGuessedLines(); slotDebugBoundingboxes(); slotDebugMetaboxes(); slotDebugBaseline(); slotDebugBasepoints(); slotDebugMaxPart(); } else // 恢复缺省设置 { debugGrid->setChecked(true); // 应用调试设置 slotDebugGrid(); }}void KognitionApp::saveProperties(KConfig *_cfg){ if(doc->URL().fileName()!=i18n("Untitled") && !doc->isModified()) { } else { KURL url=doc->URL(); _cfg->writeEntry("filename", url.url()); _cfg->writeEntry("modified", doc->isModified()); QString tempname = kapp->tempSaveName(url.url()); QString tempurl= KURL::encode_string(tempname); KURL _url(tempurl); doc->saveDocument(_url); }}void KognitionApp::readProperties(KConfig* _cfg){ QString filename = _cfg->readEntry("filename", ""); KURL url(filename); bool modified = _cfg->readBoolEntry("modified", false); if(modified) { bool canRecover; QString tempname = kapp->checkRecoverFile(filename, canRecover); KURL _url(tempname); if(canRecover) { doc->openDocument(_url); doc->setModified(); setCaption(_url.fileName(),true); QFile::remove(tempname); } } else { if(!filename.isEmpty()) { doc->openDocument(url); setCaption(url.fileName(),false); } }}bool KognitionApp::queryClose(){ return doc->saveModified();}bool KognitionApp::queryExit(){ saveOptions(); return true;}void KognitionApp::slotFileNewWindow(){ slotStatusMsg(i18n("Opening a new application window...")); KognitionApp *new_window= new KognitionApp(); new_window->show(); slotStatusMsg(i18n("Ready."));}void KognitionApp::slotFileNew(){ slotStatusMsg(i18n("Creating new document...")); if(!doc->saveModified()) { } else { doc->newDocument(); setCaption(doc->URL().fileName(), false); } slotStatusMsg(i18n("Ready."));}void KognitionApp::slotFileOpen(){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -