00001
00002
00004
00005
00006
00008
00009
00010
00011 #include "DasherInterface.h"
00012
00013 #include "CustomAlphabet.h"
00014 #include "DasherViewSquare.h"
00015 #include "PPMLanguageModel.h"
00016 #include <iostream>
00017 namespace {
00018 #include "stdio.h"
00019 }
00020 using namespace Dasher;
00021 using namespace std;
00022
00023
00024 const string CDasherInterface::EmptyString = "";
00025
00026
00027 CDasherInterface::CDasherInterface()
00028 : m_Alphabet(0),
00029 m_LanguageModel(0),
00030 m_DasherModel(0),
00031 m_DashEditbox(0),
00032 m_DasherScreen(0),
00033 m_DasherView(0),
00034 m_SettingsStore(0),
00035 m_SettingsUI(0),
00036 m_AlphIO(0),
00037 TrainContext(0),
00038 AlphabetID(""),
00039 LanguageModelID(-1),
00040 ViewID(-1),
00041 m_MaxBitRate(-1),
00042 m_DrawKeyboard(false),
00043 m_Orientation(Opts::LeftToRight),
00044 m_UserLocation("usr_"),
00045 m_SystemLocation("sys_"),
00046 m_TrainFile(""),
00047 m_DasherFont(""),
00048 m_EditFont(""),
00049 m_EditFontSize(0)
00050 {
00051 }
00052
00053
00054 CDasherInterface::~CDasherInterface()
00055 {
00056 if (m_LanguageModel)
00057 m_LanguageModel->ReleaseNodeContext(TrainContext);
00058 delete m_DasherModel;
00059 delete m_LanguageModel;
00060 delete m_Alphabet;
00061 delete m_DasherView;
00062
00063 }
00064
00065
00066 void CDasherInterface::SetSettingsStore(CSettingsStore* SettingsStore)
00067 {
00068 delete m_SettingsStore;
00069 m_SettingsStore = SettingsStore;
00070 this->SettingsDefaults(m_SettingsStore);
00071 }
00072
00073
00074 void CDasherInterface::SetSettingsUI(CDasherSettingsInterface* SettingsUI)
00075 {
00076 delete m_SettingsUI;
00077 m_SettingsUI = SettingsUI;
00078
00079 m_SettingsUI->SettingsDefaults(m_SettingsStore);
00080 }
00081
00082
00083 void CDasherInterface::SetUserLocation(std::string UserLocation)
00084 {
00085
00086
00087
00088 m_UserLocation = UserLocation;
00089 if (m_Alphabet!=0)
00090 m_TrainFile = m_UserLocation + m_Alphabet->GetTrainingFile();
00091 }
00092
00093
00094 void CDasherInterface::SetSystemLocation(std::string SystemLocation)
00095 {
00096
00097
00098
00099 m_SystemLocation = SystemLocation;
00100 }
00101
00102
00103 void CDasherInterface::CreateDasherModel()
00104 {
00105
00106 if (m_DashEditbox!=0 && m_LanguageModel!=0) {
00107 delete m_DasherModel;
00108 m_DasherModel = new CDasherModel(m_DashEditbox, m_LanguageModel, m_Dimensions);
00109 if (m_MaxBitRate>=0)
00110 m_DasherModel->SetMaxBitrate(m_MaxBitRate);
00111 if (ViewID!=-1)
00112 ChangeView(ViewID);
00113 }
00114
00115
00116 }
00117
00118
00119 void CDasherInterface::Start()
00120 {
00121 if (m_DasherModel!=0)
00122 m_DasherModel->Start();
00123 }
00124
00125
00126 void CDasherInterface::PauseAt(int MouseX, int MouseY)
00127 {
00128 if (m_DasherView!=0)
00129 m_DasherView->FlushAt(MouseX, MouseY);
00130 if (m_DashEditbox!=0) {
00131 m_DashEditbox->write_to_file();
00132 if (m_CopyAllOnStop)
00133 m_DashEditbox->CopyAll();
00134 }
00135 }
00136
00137
00138 void CDasherInterface::Unpause(unsigned long Time)
00139 {
00140 if (m_DashEditbox!=0)
00141 m_DashEditbox->unflush();
00142 if (m_DasherModel!=0)
00143 m_DasherModel->Reset_framerate(Time);
00144 }
00145
00146
00147 void CDasherInterface::Redraw()
00148 {
00149
00150 if (m_DasherView!=0) {
00151 m_DasherView->Render();
00152 m_DasherView->Display();
00153 }
00154
00155 }
00156
00157
00158 void CDasherInterface::TapOn(int MouseX, int MouseY, unsigned long Time)
00159 {
00160 if (m_DasherView!=0) {
00161 m_DasherView->TapOnDisplay(MouseX, MouseY, Time);
00162 m_DasherView->Render();
00163 if (m_DrawMouse==true) {
00164 m_DasherView->DrawMouse(MouseX, MouseY);
00165 }
00166 if (m_DrawKeyboard==true) {
00167 m_DasherView->DrawKeyboard();
00168 }
00169 m_DasherView->Display();
00170 }
00171 if (m_DasherModel!=0)
00172 m_DasherModel->NewFrame(Time);
00173 }
00174
00175
00176 void CDasherInterface::ChangeAlphabet(const std::string& NewAlphabetID)
00177 {
00178 if (m_SettingsUI!=0)
00179 m_SettingsUI->ChangeAlphabet(NewAlphabetID);
00180 if (m_SettingsStore!=0)
00181 m_SettingsStore->SetStringOption(Keys::ALPHABET_ID, NewAlphabetID);
00182
00183 AlphabetID = NewAlphabetID;
00184 if (!m_AlphIO)
00185 m_AlphIO = new CAlphIO(m_SystemLocation, m_UserLocation);
00186 CAlphIO::AlphInfo Info = m_AlphIO->GetInfo(NewAlphabetID);
00187
00188 CAlphabet* old = m_Alphabet;
00189 m_Alphabet = new CCustomAlphabet(Info);
00190
00191
00192
00193 m_TrainFile = m_UserLocation + m_Alphabet->GetTrainingFile();
00194
00195
00196 if (m_DashEditbox!=0)
00197 m_DashEditbox->SetInterface(this);
00198 if (m_DasherScreen!=0)
00199 m_DasherScreen->SetInterface(this);
00200 if (LanguageModelID!=-1 || m_LanguageModel)
00201 ChangeLanguageModel(LanguageModelID);
00202
00203 delete old;
00204
00205 Start();
00206
00207
00208
00209
00210
00211 if (m_Orientation==Opts::Alphabet)
00212 ChangeOrientation(Opts::Alphabet);
00213
00214 Redraw();
00215 }
00216
00217
00218 void CDasherInterface::ChangeMaxBitRate(double NewMaxBitRate)
00219 {
00220 m_MaxBitRate = NewMaxBitRate;
00221
00222 if (m_DasherModel!=0)
00223 m_DasherModel->SetMaxBitrate(m_MaxBitRate);
00224 if (m_SettingsUI!=0)
00225 m_SettingsUI->ChangeMaxBitRate(m_MaxBitRate);
00226 if (m_SettingsStore!=0)
00227 m_SettingsStore->SetLongOption(Keys::MAX_BITRATE_TIMES100, long(m_MaxBitRate*100) );
00228
00229 if (m_DrawKeyboard==true && m_DasherView!=NULL) {
00230 m_DasherView->DrawKeyboard();
00231 }
00232 }
00233
00234
00235 void CDasherInterface::ChangeLanguageModel(unsigned int NewLanguageModelID)
00236 {
00237 LanguageModelID = NewLanguageModelID;
00238 if (m_Alphabet!=0) {
00239 if (m_LanguageModel)
00240 m_LanguageModel->ReleaseNodeContext(TrainContext);
00241 TrainContext = 0;
00242 delete m_DasherModel;
00243 m_DasherModel = 0;
00244 delete m_LanguageModel;
00245
00246 m_LanguageModel = new CPPMLanguageModel(m_Alphabet,1<<10);
00247 TrainContext = m_LanguageModel->GetRootNodeContext();
00248 string T = m_Alphabet->GetTrainingFile();
00249 TrainFile(m_SystemLocation+T);
00250 TrainFile(m_UserLocation+T);
00251 CreateDasherModel();
00252 }
00253 }
00254
00255
00256 void CDasherInterface::ChangeScreen()
00257 {
00258 if (m_DasherView!=0) {
00259 m_DasherView->ChangeScreen(m_DasherScreen);
00260 } else {
00261 if (ViewID!=-1)
00262 ChangeView(ViewID);
00263 }
00264 }
00265
00266
00267 void CDasherInterface::ChangeScreen(CDasherScreen* NewScreen)
00268 {
00269 m_DasherScreen = NewScreen;
00270 m_DasherScreen->SetFont(m_DasherFont);
00271 m_DasherScreen->SetInterface(this);
00272 ChangeScreen();
00273 Redraw();
00274 }
00275
00276
00277 void CDasherInterface::ChangeView(unsigned int NewViewID)
00278 {
00279
00280 ViewID = NewViewID;
00281 if (m_DasherScreen!=0 && m_DasherModel!=0) {
00282 delete m_DasherView;
00283 if (m_Orientation==Opts::Alphabet)
00284 m_DasherView = new CDasherViewSquare(m_DasherScreen, *m_DasherModel, GetAlphabetOrientation());
00285 else
00286 m_DasherView = new CDasherViewSquare(m_DasherScreen, *m_DasherModel, m_Orientation);
00287 }
00288 }
00289
00290
00291 void CDasherInterface::ChangeOrientation(Opts::ScreenOrientations Orientation)
00292 {
00293 m_Orientation = Orientation;
00294 if (m_DasherView!=0) {
00295 if (Orientation==Opts::Alphabet)
00296 m_DasherView->ChangeOrientation(GetAlphabetOrientation());
00297 else
00298 m_DasherView->ChangeOrientation(Orientation);
00299 }
00300 if (m_SettingsUI!=0)
00301 m_SettingsUI->ChangeOrientation(Orientation);
00302 if (m_SettingsStore!=0)
00303 m_SettingsStore->SetLongOption(Keys::SCREEN_ORIENTATION, Orientation);
00304 }
00305
00306
00307 void CDasherInterface::SetFileEncoding(Opts::FileEncodingFormats Encoding)
00308 {
00309 if (m_SettingsUI!=0)
00310 m_SettingsUI->SetFileEncoding(Encoding);
00311 if (m_SettingsStore!=0)
00312 m_SettingsStore->SetLongOption(Keys::FILE_ENCODING, Encoding);
00313 if (m_DashEditbox)
00314 m_DashEditbox->SetEncoding(Encoding);
00315 }
00316
00317
00318 void CDasherInterface::ShowToolbar(bool Value)
00319 {
00320 if (m_SettingsUI!=0)
00321 m_SettingsUI->ShowToolbar(Value);
00322 if (m_SettingsStore!=0)
00323 m_SettingsStore->SetBoolOption(Keys::SHOW_TOOLBAR, Value);
00324 }
00325
00326
00327 void CDasherInterface::ShowToolbarText(bool Value)
00328 {
00329 if (m_SettingsUI!=0)
00330 m_SettingsUI->ShowToolbarText(Value);
00331 if (m_SettingsStore!=0)
00332 m_SettingsStore->SetBoolOption(Keys::SHOW_TOOLBAR_TEXT, Value);
00333 }
00334
00335
00336 void CDasherInterface::ShowToolbarLargeIcons(bool Value)
00337 {
00338 if (m_SettingsUI!=0)
00339 m_SettingsUI->ShowToolbarLargeIcons(Value);
00340 if (m_SettingsStore!=0)
00341 m_SettingsStore->SetBoolOption(Keys::SHOW_LARGE_ICONS, Value);
00342 }
00343
00344
00345 void CDasherInterface::ShowSpeedSlider(bool Value)
00346 {
00347 if (m_SettingsUI!=0)
00348 m_SettingsUI->ShowSpeedSlider(Value);
00349 if (m_SettingsStore!=0)
00350 m_SettingsStore->SetBoolOption(Keys::SHOW_SLIDER, Value);
00351 }
00352
00353
00354 void CDasherInterface::FixLayout(bool Value)
00355 {
00356 if (m_SettingsUI!=0)
00357 m_SettingsUI->FixLayout(Value);
00358 if (m_SettingsStore!=0)
00359 m_SettingsStore->SetBoolOption(Keys::FIX_LAYOUT, Value);
00360 }
00361
00362
00363 void CDasherInterface::TimeStampNewFiles(bool Value)
00364 {
00365 if (m_SettingsUI!=0)
00366 m_SettingsUI->TimeStampNewFiles(Value);
00367 if (m_SettingsStore!=0)
00368 m_SettingsStore->SetBoolOption(Keys::TIME_STAMP, Value);
00369 if (m_DashEditbox!=0)
00370 m_DashEditbox->TimeStampNewFiles(Value);
00371 }
00372
00373
00374 void CDasherInterface::CopyAllOnStop(bool Value)
00375 {
00376 m_CopyAllOnStop = Value;
00377 if (m_SettingsUI!=0)
00378 m_SettingsUI->CopyAllOnStop(Value);
00379 if (m_SettingsStore!=0)
00380 m_SettingsStore->SetBoolOption(Keys::COPY_ALL_ON_STOP, Value);
00381 }
00382
00383 void CDasherInterface::DrawMouse(bool Value)
00384 {
00385 m_DrawMouse = Value;
00386 if (m_SettingsUI!=0)
00387 m_SettingsUI->DrawMouse(Value);
00388 if (m_SettingsStore!=0)
00389 m_SettingsStore->SetBoolOption(Keys::DRAW_MOUSE, Value);
00390 }
00391
00392 void CDasherInterface::StartOnSpace(bool Value)
00393 {
00394 m_StartSpace = Value;
00395 if (m_SettingsUI!=0)
00396 m_SettingsUI->StartOnSpace(Value);
00397 if (m_SettingsStore!=0)
00398 m_SettingsStore->SetBoolOption(Keys::START_SPACE, Value);
00399 }
00400
00401 void CDasherInterface::StartOnLeft(bool Value)
00402 {
00403 m_StartLeft = Value;
00404 if (m_SettingsUI!=0)
00405 m_SettingsUI->StartOnLeft(Value);
00406 if (m_SettingsStore!=0)
00407 m_SettingsStore->SetBoolOption(Keys::START_MOUSE, Value);
00408 }
00409
00410 void CDasherInterface::KeyControl(bool Value)
00411 {
00412 m_KeyControl = Value;
00413 if (m_SettingsUI!=0)
00414 m_SettingsUI->KeyControl(Value);
00415 if (m_SettingsStore!=0)
00416 m_SettingsStore->SetBoolOption(Keys::KEY_CONTROL, Value);
00417 }
00418
00419 void CDasherInterface::WindowPause(bool Value)
00420 {
00421 m_KeyControl = Value;
00422 if (m_SettingsUI!=0)
00423 m_SettingsUI->WindowPause(Value);
00424 if (m_SettingsStore!=0)
00425 m_SettingsStore->SetBoolOption(Keys::WINDOW_PAUSE, Value);
00426 }
00427
00428 void CDasherInterface::SetEditFont(string Name, long Size)
00429 {
00430 m_EditFont = Name;
00431 m_EditFontSize = Size;
00432 if (m_DashEditbox)
00433 m_DashEditbox->SetFont(Name, Size);
00434 if (m_SettingsUI!=0)
00435 m_SettingsUI->SetEditFont(Name, Size);
00436 if (m_SettingsStore!=0) {
00437 m_SettingsStore->SetStringOption(Keys::EDIT_FONT, Name);
00438 m_SettingsStore->SetLongOption(Keys::EDIT_FONT_SIZE, Size);
00439 }
00440 }
00441
00442
00443 void CDasherInterface::SetDasherFont(string Name)
00444 {
00445 if (m_SettingsStore!=0)
00446 m_SettingsStore->SetStringOption(Keys::DASHER_FONT, Name);
00447 m_DasherFont = Name;
00448 if (m_DasherScreen!=0)
00449 m_DasherScreen->SetFont(Name);
00450 Redraw();
00451 }
00452
00453 void CDasherInterface::SetDasherFontSize(FontSize fontsize)
00454 {
00455 if (m_SettingsStore!=0)
00456 m_SettingsStore->SetLongOption(Keys::DASHER_FONTSIZE, fontsize);
00457 if (m_DasherScreen!=0) {
00458 m_DasherScreen->SetFontSize(fontsize);
00459 }
00460 Redraw();
00461 }
00462
00463 void CDasherInterface::SetDasherDimensions(bool Value)
00464 {
00465 m_Dimensions=Value;
00466 if (m_SettingsStore!=0)
00467 m_SettingsStore->SetBoolOption(Keys::DASHER_DIMENSIONS, Value);
00468 if (m_DasherModel!=0) {
00469 m_DasherModel->Set_dimensions(Value);
00470 }
00471 if (m_SettingsUI!=0) {
00472 m_SettingsUI->SetDasherDimensions(Value);
00473 }
00474 }
00475
00476
00477 unsigned int CDasherInterface::GetNumberSymbols()
00478 {
00479 if (m_Alphabet!=0)
00480 return m_Alphabet->GetNumberSymbols();
00481 else
00482 return 0;
00483 }
00484
00485
00486 const string& CDasherInterface::GetDisplayText(symbol Symbol)
00487 {
00488 if (m_Alphabet!=0)
00489 return m_Alphabet->GetDisplayText(Symbol);
00490 else
00491 return EmptyString;
00492 }
00493
00494
00495 const string& CDasherInterface::GetEditText(symbol Symbol)
00496 {
00497 if (m_Alphabet!=0)
00498 return m_Alphabet->GetText(Symbol);
00499 else
00500 return EmptyString;
00501 }
00502
00503 int CDasherInterface::GetTextColour(symbol Symbol)
00504 {
00505 if (m_Alphabet!=0)
00506 return m_Alphabet->GetTextColour(Symbol);
00507 else
00508 return 0;
00509 }
00510
00511
00512 Opts::ScreenOrientations CDasherInterface::GetAlphabetOrientation()
00513 {
00514 return m_Alphabet->GetOrientation();
00515 }
00516
00517
00518 Opts::AlphabetTypes CDasherInterface::GetAlphabetType()
00519 {
00520 return m_Alphabet->GetType();
00521 }
00522
00523
00524 const std::string& CDasherInterface::GetTrainFile()
00525 {
00526 return m_TrainFile;
00527 }
00528
00529
00530 void CDasherInterface::GetAlphabets(std::vector< std::string >* AlphabetList)
00531 {
00532 if (!m_AlphIO)
00533 m_AlphIO = new CAlphIO(m_SystemLocation, m_UserLocation);
00534 m_AlphIO->GetAlphabets(AlphabetList);
00535 }
00536
00537
00538 const CAlphIO::AlphInfo& CDasherInterface::GetInfo(const std::string& AlphID)
00539 {
00540 if (!m_AlphIO)
00541 m_AlphIO = new CAlphIO(m_SystemLocation, m_UserLocation);
00542
00543 return m_AlphIO->GetInfo(AlphID);
00544 }
00545
00546
00547 void CDasherInterface::SetInfo(const CAlphIO::AlphInfo& NewInfo)
00548 {
00549 if (!m_AlphIO)
00550 m_AlphIO = new CAlphIO(m_SystemLocation, m_UserLocation);
00551
00552 m_AlphIO->SetInfo(NewInfo);
00553 }
00554
00555
00556 void CDasherInterface::DeleteAlphabet(const std::string& AlphID)
00557 {
00558 if (!m_AlphIO)
00559 m_AlphIO = new CAlphIO(m_SystemLocation, m_UserLocation);
00560
00561 m_AlphIO->Delete(AlphID);
00562 }
00563
00564
00565 void CDasherInterface::ChangeEdit()
00566 {
00567 CreateDasherModel();
00568 if (m_DasherModel!=0)
00569 m_DasherModel->Start();
00570 if (m_DasherView!=0)
00571 m_DasherView->Render();
00572 }
00573
00574
00575 void CDasherInterface::ChangeEdit(CDashEditbox* NewEdit)
00576 {
00577 m_DashEditbox = NewEdit;
00578 m_DashEditbox->SetFont(m_EditFont, m_EditFontSize);
00579 m_DashEditbox->SetInterface(this);
00580 if (m_SettingsStore!=0)
00581 m_DashEditbox->TimeStampNewFiles(m_SettingsStore->GetBoolOption(Keys::TIME_STAMP));
00582 m_DashEditbox->New("");
00583 ChangeEdit();
00584 }
00585
00586
00587 void CDasherInterface::Train(string* TrainString, bool IsMore)
00588 {
00589 m_LanguageModel->LearnText(TrainContext, TrainString, IsMore);
00590 return;
00591 }
00592
00593
00594
00595
00596
00597
00598
00599
00600 void CDasherInterface::TrainFile(string Filename)
00601 {
00602 if (Filename=="")
00603 return;
00604
00605 FILE* InputFile;
00606 if ( (InputFile = fopen(Filename.c_str(), "r")) == (FILE*)0)
00607 return;
00608
00609 const int BufferSize = 1024;
00610 char InputBuffer[BufferSize];
00611 string StringBuffer;
00612 int NumberRead;
00613
00614 do {
00615 NumberRead = fread(InputBuffer, 1, BufferSize-1, InputFile);
00616 InputBuffer[NumberRead] = '\0';
00617 StringBuffer += InputBuffer;
00618 if (NumberRead == (BufferSize-1)) {
00619 m_LanguageModel->LearnText(TrainContext, &StringBuffer, true);
00620 } else {
00621 m_LanguageModel->LearnText(TrainContext, &StringBuffer, false);
00622 }
00623 } while (NumberRead==BufferSize-1);
00624
00625 fclose(InputFile);
00626 }
00627
00628 void CDasherInterface::GetFontSizes(std::vector<int> *FontSizes)
00629 {
00630 FontSizes->push_back(20);
00631 FontSizes->push_back(14);
00632 FontSizes->push_back(11);
00633 FontSizes->push_back(40);
00634 FontSizes->push_back(28);
00635 FontSizes->push_back(22);
00636 FontSizes->push_back(80);
00637 FontSizes->push_back(56);
00638 FontSizes->push_back(44);
00639 }
00640
00641
00642 double CDasherInterface::GetCurCPM()
00643 {
00644
00645 return 0;
00646 }
00647
00648
00649 double CDasherInterface::GetCurFPS()
00650 {
00651
00652 return 0;
00653 }