"Fossies" - the Fresh Open Source Software Archive

Member "kydpdict-0.9.5/src/ydpdictionary.cpp" (3 Nov 2007, 13313 Bytes) of package /linux/privat/old/kydpdict-0.9.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ydpdictionary.cpp" see the Fossies "Dox" file reference documentation.

    1 /***************************************************************************
    2  *                                                                         *
    3  *   This program is free software; you can redistribute it and/or modify  *
    4  *   it under the terms of the GNU General Public License as published by  *
    5  *   the Free Software Foundation; either version 2 of the License, or     *
    6  *   (at your option) any later version.                                   *
    7  *                                                                         *
    8  ***************************************************************************/
    9 
   10 #include <qlistbox.h>
   11 #include <qmessagebox.h>
   12 #include <qprocess.h>
   13 
   14 #include "kydpdict.h"
   15 #include "kydpconfig.h"
   16 #include "ydpconverter.h"
   17 #include "ydpdictionary.h"
   18 #include "ydpdictionary.moc"
   19 
   20 KeyEater* keyEater;
   21 
   22 QString ydpDictionary::GetTip(int index) {
   23     return QString("XXXYYYZZZ");
   24 }
   25 
   26 int ydpDictionary::GetTipNumber(int type) {
   27 
   28 static int gram,dom,flag=-1;
   29 int i;
   30 
   31     if (flag<0) {
   32     gram = 0; dom = 0;
   33     for (i=0;!(GetTip(i).startsWith("XXXYYYZZZ"));i++) {
   34         if (GetInputTip(i)[0].category() == QChar::Letter_Uppercase)
   35         dom++;
   36         else
   37         gram++;
   38     }
   39     flag = 0;
   40     }
   41     switch (type) {
   42     case 0:
   43         return gram+dom;
   44         break;
   45     case 1:
   46         return gram;
   47         break;
   48     case 2:
   49         return dom;
   50         break;
   51     default:
   52         return -1;
   53         break;
   54     }
   55 }
   56 
   57 QString ydpDictionary::GetInputTip(int index) {
   58     QString tmp = GetTip(index);
   59     return tmp.mid(0,tmp.find(':'));
   60 }
   61 
   62 QString ydpDictionary::GetOutputTip(int index) {
   63     QString tmp = GetTip(index);
   64     return tmp.mid(tmp.find(':')+1);
   65 }
   66 
   67 QString ydpDictionary::GetInfoPage(void) {
   68     QString gram, dom;
   69 
   70     for (int i=0; i<GetTipNumber(0); i++) {
   71     if (GetInputTip(i)[0].category() == QChar::Letter_Uppercase) {
   72         dom  += "<a name=\""+ GetInputTip(i) + "\"></a><h4><font color=\"red\">"+ GetInputTip(i) + "</font></h4>" + GetOutputTip(i) + "<hr>";
   73     } else {
   74         gram += "<a name=\""+ GetInputTip(i) + "\"></a><h4><font color=\"red\">"+ GetInputTip(i) + "</font></h4>" + GetOutputTip(i) + "<hr>";
   75     }
   76     }
   77 
   78     QString output = "<h2>Skróty występujące w tłumaczeniach</h2>";
   79     output += "<h3>Część I - GRAMATYKA</h3>";
   80     output += gram;
   81     output += "<h3>Część II - DZIEDZINY</h3>";
   82     output += dom;
   83 
   84     return output;
   85 }
   86 
   87 unsigned int ydpDictionary::fix32(unsigned int x)
   88 {
   89 #ifndef WORDS_BIGENDIAN
   90     return x;
   91 #else
   92     return (unsigned int)
   93         (((x & (unsigned int) 0x000000ffU) << 24) |
   94                  ((x & (unsigned int) 0x0000ff00U) << 8) |
   95                  ((x & (unsigned int) 0x00ff0000U) >> 8) |
   96                  ((x & (unsigned int) 0xff000000U) >> 24));
   97 #endif      
   98 }
   99 
  100 unsigned short ydpDictionary::fix16(unsigned short x)
  101 {
  102 #ifndef WORDS_BIGENDIAN
  103     return x;
  104 #else
  105     return (unsigned short)
  106         (((x & (unsigned short) 0x00ffU) << 8) |
  107                  ((x & (unsigned short) 0xff00U) >> 8));
  108 #endif
  109 }
  110 
  111 ydpDictionary::ydpDictionary(kydpConfig *config, QListBox *listBox, ydpConverter *converter)
  112 {
  113     int i;
  114 
  115     dictList = listBox;
  116     cnf = config;
  117     cvt = converter;
  118     keyEater = new KeyEater(this);
  119     if (dictList != NULL)
  120         dictList->installEventFilter(keyEater);
  121     for (i=0;i<4;i++) {
  122         dictCache[i].wordCount = -1;
  123         dictCache[i].words = NULL;
  124     }
  125 }
  126 
  127 ydpDictionary::~ydpDictionary()
  128 {
  129     int i,j;
  130 
  131     for (i=0;i<4;i++) {
  132     if (dictCache[i].wordCount>0) {
  133         if (dictCache[i].words) {
  134         for (j=0;j<dictCache[i].wordCount;j++)
  135             delete [] dictCache[i].words[j];
  136         delete [] dictCache[i].words;
  137         }
  138     }
  139     }
  140 }
  141 
  142 QString ydpDictionary::GetDefinition (int index)
  143 {
  144     if (ReadDefinition(index) == 0)
  145     return rtf2html(curDefinition);
  146     else
  147     return (tr("Error reading data file!"));
  148 }
  149 
  150 int ydpDictionary::OpenDictionary(void)
  151 {
  152     /* 0-english, eng->pol; 1-english, pol->eng; 2-german, ger->pol; 3-german, pol->ger */
  153     int i = 0;
  154 
  155     if (cnf->language == LANG_DEUTSCH)
  156     i+=2;
  157     if (!(cnf->toPolish))
  158     i++;
  159     if (dictCache[i].wordCount>0) {
  160     wordCount = dictCache[i].wordCount;
  161     words = dictCache[i].words;
  162     } else {
  163     dictCache[i].wordCount = wordCount;
  164     dictCache[i].words = words;
  165     }
  166     /* don't want to do ListRefresh yet and dictList can't be empty */
  167     dictList->blockSignals(TRUE);
  168     dictList->insertItem(QString("dummy"));
  169     dictList->insertItem(QString("dummy"));
  170     ListRefresh(0);
  171     dictList->blockSignals(FALSE);
  172     emit dictionaryChanged(wordCount,words,cvt);
  173     return 0;
  174 }
  175 
  176 int ydpDictionary::CheckDictionary(void) {
  177     return 1;   // always fail
  178 }
  179 
  180 int ydpDictionary::GetDictionaryInfo(void) {
  181     bool toPolish = cnf->toPolish;
  182     int language = cnf->language;
  183     int bits = 0;
  184 
  185     cnf->language = LANG_ENGLISH;
  186     cnf->toPolish = true;
  187     if (CheckDictionary())
  188     bits |= hasEnglish2Polish;
  189     cnf->toPolish = false;
  190     if (CheckDictionary())
  191     bits |= hasPolish2English;
  192     cnf->language = LANG_DEUTSCH;
  193     cnf->toPolish = true;
  194     if (CheckDictionary())
  195     bits |= hasGerman2Polish;
  196     cnf->toPolish = false;
  197     if (CheckDictionary())
  198     bits |= hasPolish2German;
  199     cnf->language = language; cnf->toPolish = toPolish;
  200     return bits;
  201 }
  202 
  203 void ydpDictionary::CloseDictionary()
  204 {
  205     dictList->clear();
  206 }
  207 
  208 void ydpDictionary::UpdateFName(void)
  209 {
  210 
  211 }
  212 
  213 KeyEater::KeyEater(ydpDictionary *parent)
  214 {
  215     myDict = parent;
  216 }
  217 
  218 KeyEater::~KeyEater()
  219 {
  220 
  221 }
  222 
  223 bool KeyEater::eventFilter(QObject *o, QEvent *e) {
  224     QListBox *dictList = (QListBox*)o;
  225     if ( e->type() == QEvent::Wheel ) {
  226     QWheelEvent *k = (QWheelEvent *)e;
  227     if (k->orientation() == Qt::Vertical) {
  228         if (k->delta() < 0)
  229         myDict->ListScrollPageUp();
  230         else
  231         myDict->ListScrollPageDown();
  232         return true;    // don't pass this down
  233     }
  234     }
  235     if ( e->type() == QEvent::KeyPress ) {
  236     QKeyEvent *k = (QKeyEvent *)e;
  237     if ((dictList->currentItem()==0)&&((k->key()==Qt::Key_Up)||(k->key()==Qt::Key_Prior))) {
  238         if (k->key()==Qt::Key_Up)
  239         myDict->ListScroll1Down();
  240         if (k->key()==Qt::Key_Prior)
  241         myDict->ListScrollPageDown();
  242         return true;    // don't pass this down
  243     }
  244     if ((dictList->currentItem()+1==(int)dictList->count())&&((k->key()==Qt::Key_Down)||(k->key()==Qt::Key_Next))) {
  245         if (k->key()==Qt::Key_Down)
  246         myDict->ListScroll1Up();
  247         if (k->key()==Qt::Key_Next)
  248         myDict->ListScrollPageUp();
  249         return true;    // don't pass this down
  250     }
  251     return false;   // process event
  252     } else
  253     return false;   // process event
  254 }
  255 
  256 // offset == 1 - o linie, !=1 - o strone widoczna
  257 void ydpDictionary::ListScrollUp(int offset) {
  258     int i,spacefor,itemh;
  259 
  260     itemh = dictList->itemHeight();
  261     if (itemh==0) {
  262     printf("codename: upper larch!!! mail this to ytm@elysium.pl!!!\n");
  263     itemh=10;
  264     }
  265     spacefor=dictList->height() / itemh;
  266 
  267     if (topitem+spacefor == wordCount)
  268     return;
  269 
  270     if (offset != 1) {
  271     ListRefresh(topitem+spacefor);
  272     dictList->blockSignals(TRUE);
  273     dictList->setCurrentItem(spacefor-2);   // prepare to force highlight signal
  274     dictList->blockSignals(FALSE);
  275     dictList->setCurrentItem(spacefor-1);   // restore correct item
  276     } else {
  277     dictList->setAutoUpdate(FALSE);
  278     dictList->blockSignals(TRUE);
  279     // przepisac o 1 w gore, w ostatni wpisac nowy, current ustawic na przedostatni
  280     for (i=1;i<spacefor;i++)
  281         dictList->changeItem(dictList->text(i),i-1);
  282 
  283     dictList->changeItem(cvt->toUnicode(words[topitem+spacefor]),i-1);
  284     topitem++;
  285 
  286     dictList->setCurrentItem(spacefor-2);   // prepare to force highlight signal
  287     dictList->setAutoUpdate(TRUE);
  288     dictList->blockSignals(FALSE);
  289     dictList->setCurrentItem(spacefor-1);   // restore correct item
  290     }
  291 }
  292 
  293 void ydpDictionary::ListScrollDown(int offset) {
  294     int i, spacefor, itemh;
  295 
  296     if (topitem==0)
  297     return;
  298 
  299     itemh = dictList->itemHeight();
  300     if (itemh == 0) {
  301     printf("codename: lower larch!!! mail this to ytm@elysium.pl!!!\n");
  302     itemh=10;
  303     }
  304     spacefor=dictList->height()/itemh;
  305 
  306     if (offset != 1) {
  307     ListRefresh(topitem-spacefor);
  308     dictList->setCurrentItem(0);
  309     } else {
  310     dictList->setAutoUpdate(FALSE);
  311     dictList->blockSignals(TRUE);
  312     for (i=spacefor-1;i!=0;i--)
  313         dictList->changeItem(dictList->text(i-1),i);
  314     topitem--;
  315     dictList->changeItem(cvt->toUnicode(words[topitem]),0);
  316 
  317     dictList->setCurrentItem(1);    // prepare to force highlight signal
  318     dictList->setAutoUpdate(TRUE);
  319     dictList->blockSignals(FALSE);
  320     dictList->setCurrentItem(0);    // restore correct index
  321     }
  322 }
  323 
  324 void ydpDictionary::ListScroll1Up() 
  325 {
  326     if (dictList->currentItem()+1==(int)dictList->count()) {
  327     ListScrollUp(1);
  328     } else {
  329     dictList->setCurrentItem(dictList->currentItem()+1);
  330     }
  331 }
  332 
  333 void ydpDictionary::ListScrollPageUp()
  334 {
  335     ListScrollUp(20);   // anything but 1 for page
  336 }
  337 
  338 void ydpDictionary::ListScroll1Down()
  339 {
  340     if (dictList->currentItem()==0) {
  341     ListScrollDown(1);
  342     } else {
  343     dictList->setCurrentItem(dictList->currentItem()-1);
  344     }
  345 }
  346 
  347 void ydpDictionary::ListScrollPageDown()
  348 {
  349     ListScrollDown(20); // anything but 1 for page
  350 }
  351 
  352 void ydpDictionary::ListRefresh(int index)
  353 {
  354 /* refresh list so index is visible and dictList length is correct */
  355     unsigned int spacefor,itemh;
  356     bool needRefresh=false;
  357     static unsigned int lastspacefor;
  358     int i;
  359 
  360     if (index<0)
  361     index=0;
  362 
  363     /* now, before showing QMessageBox with bad paths error, ListRefresh gots called
  364        and we have nothing yet, so we must quit. Moving connect signal lower should help,
  365        but it wouldn't be obvious why one connect signal is in a different place */
  366     itemh = dictList->itemHeight();
  367     if (itemh==0)
  368     return;
  369     /* handle size change */
  370     spacefor=dictList->height()/itemh;
  371     dictList->setAutoUpdate(FALSE);
  372     if (spacefor!=lastspacefor) {
  373     needRefresh=true;
  374     if (dictList->count()<spacefor) {
  375         int i,j;
  376         j = spacefor-dictList->count();
  377         for (i=0;i<j;i++)
  378         dictList->insertItem(QString("dummy"));
  379     } else {
  380         int i,j;
  381         j = dictList->count()-spacefor;
  382         for (i=0;i<j;i++)
  383         dictList->removeItem(dictList->count()-1);
  384     }
  385     }
  386     dictList->setAutoUpdate(TRUE);
  387 
  388     /* if currently visible - do nothing */
  389     if (((index>topitem)&&(index<(int)(topitem+dictList->count())))&&(!needRefresh))
  390     return;
  391 
  392     /* fix for out of bounds */
  393     if ((int)((index+dictList->count()))>wordCount)
  394     index=wordCount-dictList->count();
  395 
  396     topitem = index;
  397 
  398     dictList->blockSignals(TRUE);
  399     for (i=0;i<(int)dictList->count();i++)
  400     dictList->changeItem(cvt->toUnicode(words[index+i]),i);
  401     dictList->blockSignals(FALSE);
  402 }
  403 
  404 QString ydpDictionary::stripDelimiters(QString word)
  405 {
  406     QString lstring;
  407     unsigned int i;
  408     unsigned int wlen = word.length();
  409 
  410     for (i=0; i<wlen; i++) {
  411     if (word.at(i).isLetterOrNumber() || word.at(i).isSpace())
  412         lstring += word.at(i);
  413     }
  414 
  415     return lstring;
  416 }
  417 
  418 /*
  419 // naive search, moderately fast if convertion is fast
  420 int ydpDictionary::FindWord(QString word)
  421 {
  422     int i, score, maxscore=0, maxitem=0;
  423 
  424     if (word.length() == 0)
  425     return 0;
  426 
  427     word = stripDelimiters(word.lower());
  428     word.truncate(20);
  429 
  430     for (i=0;i<wordCount;i++) {
  431     score = ScoreWord(word, cvt->toUnicode(words[i]));
  432     if (score>maxscore) {
  433             maxscore = score;
  434         maxitem = i;
  435     }
  436     }
  437     return maxitem;
  438 }
  439 */
  440 int ydpDictionary::FindWord(QString word) {
  441 
  442     int a, b, middle, result;
  443     int i, score, maxscore=0, maxitem=0;
  444 
  445     if (word.length() == 0)
  446     return 0;
  447 
  448     word = stripDelimiters(word.lower());
  449     word.truncate(20);
  450     QCString newWord = cvt->fromUnicode(word);
  451 
  452     a = 0; b = wordCount;
  453 
  454     // binary search to narrow range to 30 (arbitrary)
  455     while (b-a >= 30) { /* there are problems with 'for', 'f.o.r.' etc. without margin */
  456     middle = a + (b-a)/2;
  457     result = cvt->localeCompare(newWord, cvt->toLocal(words[middle]));
  458     if (result==0) {
  459         a = middle; b = middle;
  460     } else {
  461         if (result<0)
  462         b = middle;
  463         else
  464         a = middle;
  465     }
  466     }
  467 
  468     // lower and upper bounds for naive search
  469     a = a - 150; if (a<0) a = 0;
  470     b = a + 2000; if (b>wordCount) b = wordCount;
  471 
  472     // now a naive search
  473     for (i=a;i<b;i++) {
  474     score = cvt->scoreWord(newWord, cvt->toLocal(words[i]));
  475     if (score>maxscore) {
  476             maxscore = score;
  477         maxitem = i;
  478     }
  479     if ((maxscore>1) && (i>maxitem+2000))
  480         break;
  481     }
  482     return maxitem;
  483 }
  484 
  485 int ydpDictionary::ReadDefinition(int index)
  486 {
  487     return 0;
  488 }
  489 
  490 QString ydpDictionary::SampleName(QString path, int index)
  491 {
  492     return QString("");
  493 }
  494 
  495 int ydpDictionary::Play(int index, kydpConfig *config)
  496 {
  497     QFile fd;
  498     QString pathdir, samplename;
  499     static QProcess proc;
  500 
  501     switch (config->language) {
  502     case LANG_DEUTSCH:
  503         pathdir = config->cd2Path;
  504         break;
  505     case LANG_ENGLISH:
  506         pathdir = config->cdPath;
  507         break;
  508     default:
  509         break;      // cos poszlo nie tak?
  510     }
  511 
  512     fd.setName(pathdir.latin1());
  513     if (!(fd.exists()))
  514     return 0;       // nie ma co tracic czasu jesli katalogu nie ma
  515 
  516     samplename = SampleName(pathdir,index);
  517     if (samplename.length() == 0)
  518     return 0;       // pliku również może nie być
  519 
  520     fd.setName(config->player);
  521     if (!(fd.exists()))
  522     return 0;       // ani odtwarzacza...
  523 
  524     if (proc.isRunning())
  525     proc.kill();        // nie ma litosci dla skurwysynow! BUM! BUM!
  526 
  527     proc.clearArguments();
  528     proc.addArgument( config->player );
  529     proc.addArgument( samplename );
  530     proc.start();
  531 
  532     return 1;
  533 }
  534 
  535 QString ydpDictionary::rtf2html (QString definition)
  536 {
  537     return definition;
  538 }