"Fossies" - the Fresh Open Source Software Archive

Member "kydpdict-0.9.5/src/kydpdict.cpp" (29 Mar 2009, 29579 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 "kydpdict.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 <stdlib.h>
   11 
   12 #include <qlistbox.h>
   13 #include <qcombobox.h>
   14 #include <qmenubar.h>
   15 #include <qpopupmenu.h>
   16 #include <qclipboard.h>
   17 #include <qmainwindow.h>
   18 #include <qtimer.h>
   19 #include <qsplitter.h>
   20 #include <qtoolbar.h>
   21 #include <qtoolbutton.h>
   22 #include <qscrollbar.h>
   23 #include <qimage.h>
   24 #include <qpixmap.h>
   25 #include <qframe.h>
   26 #include <qmessagebox.h>
   27 #include <qdialog.h>
   28 #include <qtextbrowser.h>
   29 #include <qhbox.h>
   30 #include <qvbox.h>
   31 #include <qlayout.h>
   32 #include <qmainwindow.h>
   33 #include <qvaluelist.h>
   34 #include <qapplication.h>
   35 #include <qtextcodec.h>
   36 #include <qgrid.h>
   37 #include <qaccel.h>
   38 #include <qcursor.h>
   39 #include <qlineedit.h>
   40 #include <qiconset.h>
   41 
   42 #define COMBO_HISTORY_SIZE  25
   43 #define TIMER_PERIOD        1000
   44 #define LAST_ESC_PERIOD     750
   45 
   46 /* 16x16 */
   47 #include "../icons/conf.xpm"
   48 #include "../icons/exit.xpm"
   49 #include "../icons/babelfish_small.xpm"
   50 #include "../icons/help.xpm"
   51 /* glyphs */
   52 #include "../icons/f1.xpm"
   53 #include "../icons/f2.xpm"
   54 #include "../icons/f3.xpm"
   55 #include "../icons/f4.xpm"
   56 #include "../icons/f5.xpm"
   57 #include "../icons/f6.xpm"
   58 #include "../icons/f7.xpm"
   59 #include "../icons/f8.xpm"
   60 #include "../icons/f9.xpm"
   61 #include "../icons/f10.xpm"
   62 #include "../icons/f11.xpm"
   63 /* 22x22 */
   64 #include "../icons/en_pl.xpm"
   65 #include "../icons/en_pl_dis.xpm"
   66 #include "../icons/pl_en.xpm"
   67 #include "../icons/pl_en_dis.xpm"
   68 #include "../icons/de_pl.xpm"
   69 #include "../icons/de_pl_dis.xpm"
   70 #include "../icons/pl_de.xpm"
   71 #include "../icons/pl_de_dis.xpm"
   72 #include "../icons/configure.xpm"
   73 #include "../icons/loop.xpm"
   74 #include "../icons/player_play.xpm"
   75 #include "../icons/clipboard.xpm"
   76 #include "../icons/babelfish.xpm"
   77 
   78 #include "ydpdictionary.h"
   79 #include "ydpconverter.h"
   80 #include "kydpconfig.h"
   81 #include "ydpconfigure.h"
   82 #include "kdynamictip.h"
   83 #include "ydpfuzzysearch.h"
   84 #include "dock_widget.h"
   85 #include "engine_dict.h"
   86 #include "engine_pwn.h"
   87 #include "engine_pwn2004.h"
   88 #include "engine_sap.h"
   89 #include "engine_ydp.h"
   90 
   91 #ifndef WITHOUT_X11
   92 // this is for updateUserTimestamp
   93 #if QT_VERSION >= 0x030300
   94 #include <X11/Xlib.h>
   95 #include <X11/Xatom.h>
   96 extern Time qt_x_user_time; // this is from Qt
   97 #endif
   98 #endif
   99 
  100 #include "kydpdict.h"
  101 #include "kydpdict.moc"
  102 
  103 Kydpdict::Kydpdict(QWidget *parent, const char *name) : QMainWindow(parent, name)
  104 {
  105     bool wasMaximized;
  106 
  107     updateUserTimestamp();
  108     tipsVisible = false;
  109 
  110     toolBar = new QToolBar(this, "toolbar");
  111     toolBar->setLabel(tr("Kydpdict toolbar"));
  112     toolBar->setCloseMode(QToolBar::Undocked);
  113 
  114     QFrame *centralFrame = new QFrame(this);
  115     splitterH = new QSplitter(centralFrame, "splitter");
  116     splitterV = new QSplitter(Qt::Vertical, splitterH);
  117     QHBox *hbox1 = new QHBox(splitterV);
  118     QHBox *hbox2 = new QHBox(splitterV);
  119     wordInput = new QComboBox( hbox1, "wordInput");
  120     hbox1->setMinimumHeight(20);
  121     dictList = new QListBox( hbox2, "dictList" );
  122     scrollBar = new QScrollBar( Qt::Vertical, hbox2, "scrollBar");
  123     scrollBar->setMaximumWidth(20);
  124     RTFOutput = new QTextBrowser (splitterH, "RTFOutput");
  125     setCentralWidget(centralFrame);
  126 
  127     RTFOutput->setTextFormat( RichText );
  128     RTFOutput->setReadOnly(TRUE);
  129     RTFOutput->setLineWidth( 0 );
  130     RTFOutput->setLinkUnderline (FALSE);
  131 
  132     dictList->setBottomScrollBar(FALSE);
  133     dictList->setAutoScrollBar(FALSE);
  134     dictList->setLineWidth( 0 );
  135 
  136     wordInput->setMaxCount(20);
  137     wordInput->setDuplicatesEnabled(false);
  138     wordInput->setEditable(true);
  139     wordInput->setAutoCompletion(false);
  140     wordInput->setInsertionPolicy(QComboBox::AtTop);
  141 
  142     cb = QApplication::clipboard();
  143 
  144     menu = menuBar();
  145 
  146     config = new kydpConfig;
  147 
  148     switch (config->engine) {
  149         case ENGINE_SAP:
  150         myConvert = new ConvertSAP();
  151         myDict = new EngineSAP(config,dictList,myConvert);
  152         break;
  153         case ENGINE_PWN:
  154         myConvert = new ConvertPWN();
  155         myDict = new EnginePWN(config,dictList,myConvert);
  156         break;
  157         case ENGINE_PWN2004:
  158         myConvert = new ConvertPWN2004();
  159         myDict = new EnginePWN2004(config,dictList,myConvert);
  160         break;
  161         case ENGINE_YDP:
  162         myConvert = new ConvertYDP();
  163         myDict = new EngineYDP(config,dictList,myConvert);
  164         break;
  165         default:
  166         // XXX this is terrible, warn user
  167         exit(1);
  168         break;
  169     }
  170 
  171 // XXX enable later
  172 //  myConvert = new ConvertDICT();
  173 //  myDict = new EngineDICT(config,dictList,myConvert);
  174 
  175     mySearch = new ydpFuzzySearch(config, parent);  // if exchanged with 'this' -> is always over kydpdict window
  176     // this has to be before OpenDictionary so the notification will be passed
  177     connect(myDict, SIGNAL(dictionaryChanged(const int, char **, ydpConverter *)), mySearch, SLOT(updateDictionary(const int, char **, ydpConverter *)));
  178     connect(mySearch, SIGNAL(textChanged(const QString&)), this, SLOT(newFromLine(const QString&)));
  179 
  180     wasMaximized = config->kMaximized;
  181     setGeometry (config->kGeometryX, config->kGeometryY, config->kGeometryW, config->kGeometryH);
  182 
  183     QValueList<int> splittersizes;
  184     splittersizes.append(config->spH1);
  185     splittersizes.append(config->spH2);
  186     splitterH->setSizes(splittersizes);
  187 
  188     QValueList<int> splittersizesV;
  189     splittersizesV.append(config->spV1);
  190     splittersizesV.append(config->spV2);
  191     splitterV->setSizes(splittersizesV);
  192 
  193     m_checkTimer = new QTimer(this, "timer");
  194     m_checkTimer->start(TIMER_PERIOD, FALSE);
  195     escTimer = new QTimer(this, "esctimer");
  196     lastEdit = "";
  197     lastSelection = "";
  198     lastClipboard = "";
  199     eventLock = false;
  200 
  201     int caps;
  202 testagain:
  203     caps = myDict->GetDictionaryInfo();
  204     if (caps == 0) {
  205         QMessageBox::critical( this, tr("Error"),
  206         tr( "Kydpdict couldn't open or find dictionary data files.\n"
  207             "This is a problem with current configuration. For more information\n"
  208             "please read README distributed with this program.\n"
  209             "A configuration window will be opened now so you can do setup."));
  210         Configure(TRUE);
  211         goto testagain;
  212     }
  213     // let's try with last config first
  214     // compare last config to current caps, if failed - find useful config
  215     if (!myDict->CheckDictionary()) {
  216         config->language = LANG_ENGLISH;
  217 testagain2:
  218         if (!myDict->CheckDictionary()) {
  219         config->toPolish = !config->toPolish;
  220         if (!myDict->CheckDictionary()) {
  221             config->language++;
  222         }
  223         goto testagain2;
  224         }
  225     }
  226     config->save();
  227     myDict->OpenDictionary();
  228 
  229     wordInput->clear();
  230 
  231     QPopupMenu *file = new QPopupMenu( this );
  232     Q_CHECK_PTR( file );
  233     file->insertItem(tr("&Fuzzy search"), this, SLOT(FuzzySearch()), QKeySequence( tr("Ctrl+F", "File|Fuzzy search") ) );
  234     file->insertItem(QPixmap(exit_xpm), tr("&Quit"), qApp, SLOT(quit()), QKeySequence( tr("Ctrl+Q", "File|Quit") ) );
  235 
  236     QPopupMenu *settings = new QPopupMenu( this );
  237     Q_CHECK_PTR( settings );
  238     settings->insertItem(QPixmap(conf_xpm), tr("&Settings"), this, SLOT(ConfigureKydpdict()), QKeySequence( tr("Ctrl+S", "Options|Settings") ) );
  239     toolBarMenuItem = settings->insertItem(tr("Show &toolbar"), this, SLOT(ShowToolbar()), QKeySequence( tr("Ctrl+T", "Options|Settings") ) );
  240     settings->insertSeparator();
  241     but_PlEn = NULL; but_PlDe = NULL; polToEng = 0; engToPol = 0;
  242     but_EnPl = NULL; but_DePl = NULL; polToGer = 0; gerToPol = 0;
  243     QIconSet but_IconSet;
  244     if (myDict->GetDictionaryInfo() & hasPolish2English) {
  245         polToEng = settings->insertItem("POL --> ENG", this, SLOT(SwapPolToEng()), QKeySequence( tr("Ctrl+;", "Options|POL --> ENG") ) );
  246         but_IconSet.setPixmap(QPixmap(pl_en_xpm), QIconSet::Automatic, QIconSet::Normal);
  247             but_IconSet.setPixmap(QPixmap(pl_en_dis_xpm), QIconSet::Automatic, QIconSet::Disabled);
  248         but_PlEn = new QToolButton(but_IconSet, "POL --> ENG", QString::null, this, SLOT(SwapPolToEng()), toolBar, "butPlEn" );
  249     }
  250     if (myDict->GetDictionaryInfo() & hasEnglish2Polish) {
  251         engToPol = settings->insertItem("ENG --> POL", this, SLOT(SwapEngToPol()), QKeySequence( tr("Ctrl+.", "Options|ENG --> POL") ) );
  252         but_IconSet.setPixmap(QPixmap(en_pl_xpm), QIconSet::Automatic, QIconSet::Normal);
  253         but_IconSet.setPixmap(QPixmap(en_pl_dis_xpm), QIconSet::Automatic, QIconSet::Disabled);
  254         but_EnPl = new QToolButton(but_IconSet, "ENG --> POL", QString::null, this, SLOT(SwapEngToPol()), toolBar, "butEnPl" );
  255     }
  256     if (myDict->GetDictionaryInfo() & hasPolish2German) {
  257         polToGer = settings->insertItem("POL --> GER", this, SLOT(SwapPolToGer()), QKeySequence( tr("Ctrl+:", "Options|POL --> GER") ) );
  258         but_IconSet.setPixmap(QPixmap(pl_de_xpm), QIconSet::Automatic, QIconSet::Normal);
  259         but_IconSet.setPixmap(QPixmap(pl_de_dis_xpm), QIconSet::Automatic, QIconSet::Disabled);
  260         but_PlDe = new QToolButton(but_IconSet, "POL --> GER", QString::null, this, SLOT(SwapPolToGer()), toolBar, "butPlDe" );
  261     }
  262     if (myDict->GetDictionaryInfo() & hasGerman2Polish) {
  263         gerToPol = settings->insertItem("GER --> POL", this, SLOT(SwapGerToPol()), QKeySequence( tr("Ctrl+>", "Options|GER --> POL") ) );
  264         but_IconSet.setPixmap(QPixmap(de_pl_xpm), QIconSet::Automatic, QIconSet::Normal);
  265         but_IconSet.setPixmap(QPixmap(de_pl_dis_xpm), QIconSet::Automatic, QIconSet::Disabled);
  266         but_DePl = new QToolButton(but_IconSet, "GER --> POL", QString::null, this, SLOT(SwapGerToPol()), toolBar, "butDePl" );
  267     }
  268     settings->insertSeparator();
  269     settings->insertItem( tr("Swap direction"), this, SLOT(SwapLanguages()), QKeySequence( tr("Ctrl+`", "Options|Swap direction") ) );
  270     settings->insertSeparator();
  271     settings->insertItem( tr("&Play sample"), this, SLOT(PlayCurrent()), QKeySequence( tr("Ctrl+P", "Options|Play sample") ) );
  272     settings->setCheckable(TRUE);
  273 
  274     QPopupMenu *help = new QPopupMenu( this );
  275     Q_CHECK_PTR( help );
  276     help->insertItem(QPixmap(babelfish_small_xpm), tr("About"), this, SLOT(ShowAbout()));
  277     help->insertItem(QPixmap(help_xpm), tr("About Qt"), this, SLOT(ShowAboutQt()));
  278 
  279     trayMenu = new QPopupMenu( this );
  280     Q_CHECK_PTR( trayMenu );
  281     trayMenu->insertItem(QPixmap(conf_xpm), tr("&Settings"), this, SLOT(ConfigureKydpdict()) );
  282     trayMenu->insertSeparator();
  283     trayMenu->insertItem( tr("Swap direction"), this, SLOT(SwapLanguages()) );
  284     trayMenuItemClipTrack = trayMenu->insertItem( tr("C&lipboard tracking"), this, SLOT(ToggleClipTracking()), QKeySequence(tr("Ctrl+L", "Tray|Clipboard tracking")));
  285     trayMenu->insertSeparator();
  286     trayMenu->insertItem(QPixmap(babelfish_small_xpm), tr("About"), this, SLOT(ShowAbout()));
  287     trayMenu->insertItem(QPixmap(exit_xpm), tr("&Quit"), qApp, SLOT(quit()) );
  288 
  289     Q_CHECK_PTR( menu );
  290     menu->insertItem(  tr("&File"), file );
  291     menu->insertItem(  tr("&Options"), settings );
  292     menu->insertItem(  tr("&Help"), help );
  293 
  294     myDynamicTip = new DynamicTip( RTFOutput );
  295 
  296     toolBar->addSeparator();
  297     but_SwapLang = new QToolButton(QIconSet(QPixmap(loop_xpm)), tr("Swap direction"), QString::null, this, SLOT(SwapLanguages()), toolBar, "but_swaplang" );
  298     toolBar->addSeparator();
  299     but_Play = new QToolButton(QIconSet(QPixmap(player_play_xpm)), tr("Play"), QString::null, this, SLOT(PlayCurrent()), toolBar, "but_play" );
  300     toolBar->addSeparator();
  301     but_Clipboard = new QToolButton(QIconSet(QPixmap(clipboard_xpm)), tr("Toggle clipboard tracking"), QString::null, this, SLOT(ToggleClipTracking()), toolBar, "but_clipboard");
  302     but_Clipboard->setToggleButton(TRUE);
  303     but_Clipboard->setOn(config->clipTracking);
  304     toolBar->addSeparator();
  305     but_Settings = new QToolButton(QIconSet(QPixmap(configure_xpm)), tr("Settings"), QString::null, this, SLOT(ConfigureKydpdict()), toolBar, "but_settings" );
  306 
  307     connect (dictList, SIGNAL(highlighted(int)), this, SLOT(newFromList(int)));
  308     connect (dictList, SIGNAL(selected(int)), this, SLOT(PlayCurrent()));
  309     connect (dictList, SIGNAL(clicked(QListBoxItem*)), this, SLOT(newFromClick(QListBoxItem*)));
  310     connect (wordInput, SIGNAL(textChanged(const QString&)), this, SLOT(newFromLine(const QString&)));
  311     connect (wordInput, SIGNAL(activated(int)), this, SLOT(PlayCurrent()));
  312     connect (RTFOutput, SIGNAL(highlighted(const QString &)), this, SLOT(handleTip(const QString &)));
  313     connect (RTFOutput, SIGNAL(linkClicked(const QString &)), this, SLOT(handleLink(const QString &)));
  314     connect (m_checkTimer, SIGNAL(timeout()), this, SLOT(newFromSelection()));
  315     connect (toolBar, SIGNAL(visibilityChanged(bool)), this, SLOT(ToolbarShowHide(bool)));
  316     connect (cb, SIGNAL(selectionChanged() ), SLOT(newFromSelection()));
  317     connect (cb, SIGNAL(dataChanged() ), SLOT(newFromSelection()));
  318 
  319     connect (scrollBar, SIGNAL(nextLine()), myDict, SLOT(ListScroll1Up()));
  320     connect (scrollBar, SIGNAL(nextPage()), myDict, SLOT(ListScrollPageUp()));
  321     connect (scrollBar, SIGNAL(prevLine()), myDict, SLOT(ListScroll1Down()));
  322     connect (scrollBar, SIGNAL(prevPage()), myDict, SLOT(ListScrollPageDown()));
  323     connect (scrollBar, SIGNAL(sliderMoved(int)), myDict, SLOT(ListRefresh(int)));
  324 
  325     QGridLayout *grid = new QGridLayout(centralFrame, 1, 1);
  326     grid->addWidget(splitterH, 0, 0);
  327 
  328     QAccel *accel = new QAccel(this);
  329     accel->connectItem(accel->insertItem(Key_Escape), this, SLOT(onEscaped()));
  330     UpdateLook();
  331 
  332     if (!config->toolBarVisible)
  333         toolBar->hide();
  334 
  335     cb->clear(QClipboard::Selection);
  336     cb->clear(QClipboard::Clipboard);
  337     this->show();
  338     myDict->ListRefresh(0);
  339     dictList->setCurrentItem(0);
  340     wordInput->clearEdit();
  341 
  342 #ifndef WITHOUT_X11
  343     if (config->dock) {
  344         trayicon = new TrayIcon(this,"trayicon");
  345         connect(trayicon,SIGNAL(mousePressLeftButton()),this,SLOT(trayIconLeftClick()));
  346         connect(trayicon,SIGNAL(mousePressRightButton()),this,SLOT(trayIconRightClick()));
  347         trayicon->show();
  348     }
  349 #endif
  350     setFocusProxy(wordInput);
  351     RTFOutput->setFocusProxy(wordInput);
  352 
  353     if (wasMaximized)
  354         this->showMaximized();
  355 
  356     if (config->startHidden)
  357         if (config->dock)
  358             this->hide();
  359         else
  360         this->showMinimized();
  361 }
  362 
  363 Kydpdict::~Kydpdict()
  364 {
  365         myDict->CloseDictionary();
  366     delete myDynamicTip;
  367     delete m_checkTimer;
  368     delete escTimer;
  369     delete splitterH;
  370 }
  371 
  372 void Kydpdict::flushConfig(void)
  373 {
  374     QSize aSize;
  375     QPoint aPosition;
  376 
  377     config->kMaximized = this->isMaximized();
  378 
  379     aSize = this->size();
  380     config->kGeometryW = aSize.width();
  381     config->kGeometryH = aSize.height();
  382 
  383     aPosition = this->pos();
  384     config->kGeometryX = aPosition.x();
  385     config->kGeometryY = aPosition.y();
  386 
  387     QValueList<int> list = splitterH->sizes();
  388         QValueList<int>::Iterator it = list.begin();
  389     config->spH1 = *it;
  390     it++;
  391     config->spH2 = *it;
  392 
  393     QValueList<int> list2 = splitterV->sizes();
  394         QValueList<int>::Iterator it2 = list2.begin();
  395     config->spV1 = *it2;
  396     it2++;
  397     config->spV2 = *it2;
  398 
  399     config->save();
  400 }
  401 
  402 void Kydpdict::resizeEvent(QResizeEvent *)
  403 {
  404     int item;
  405 
  406     item=dictList->currentItem();
  407     myDict->ListRefresh(myDict->topitem);
  408     dictList->setCurrentItem(item);
  409     scrollBar->setPageStep(dictList->numItemsVisible());
  410 }
  411 
  412 void Kydpdict::onEscaped()
  413 {
  414     if (!wordInput->hasFocus())
  415     wordInput->setFocus();
  416     else
  417     if (wordInput->currentText().isEmpty() && (!(escTimer->isActive()))) {
  418         if (config->dock)
  419             this->hide();
  420         else
  421         this->showMinimized();
  422     } else {
  423         wordInput->clearEdit();
  424         escTimer->start(LAST_ESC_PERIOD,TRUE);
  425     }
  426 }
  427 
  428 void Kydpdict::trayIconLeftClick()
  429 {
  430     if (this->isMinimized()) {
  431     this->showNormal();
  432     } else {
  433     switch (this->isVisible()) {
  434         case 0:
  435         this->show();
  436         this->setFocus();
  437         this->setActiveWindow();
  438         break;
  439         case 1:
  440         this->hide();
  441         break;
  442     }
  443     }
  444 }
  445 
  446 void Kydpdict::trayIconRightClick()
  447 {
  448     trayMenu->exec(QCursor::pos());
  449 }
  450 
  451 void Kydpdict::windowActivationChange(bool oldActive)
  452 {
  453     if (isActiveWindow()) {
  454     m_checkTimer->stop();
  455     wordInput->setFocus();
  456     wordInput->lineEdit()->selectAll();
  457     } else {
  458     wordInput->lineEdit()->deselect();
  459     lastClipboard = cb->text(QClipboard::Clipboard).stripWhiteSpace();   // protect from catching own copy in clipboard
  460     m_checkTimer->start(TIMER_PERIOD,FALSE);
  461     }
  462 }
  463 
  464 void Kydpdict::hideEvent( QHideEvent *ahideEvent)
  465 {
  466     QMainWindow::hideEvent(ahideEvent);
  467     m_checkTimer->stop();
  468 }
  469 
  470 void Kydpdict::closeEvent( QCloseEvent *acloseEvent )
  471 {
  472     if (config->dock) {
  473     this->hide();
  474     acloseEvent->ignore();
  475     } else
  476     QMainWindow::closeEvent(acloseEvent);
  477 }
  478 
  479 void Kydpdict::showEntry(QString *aEntry, int aindex)
  480 {
  481 // when aindex is set aEntity is ignored (only for performance reason)
  482     if (aindex < 0) {
  483     int lindex = myDict->FindWord(*aEntry);
  484     myDict->ListRefresh(lindex);
  485     dictList->setCurrentItem(lindex - myDict->topitem);
  486     scrollBar->setValue(lindex);
  487     if (aindex == -2)   // from selection or clipboard
  488         wordInput->setEditText(*aEntry);
  489     aindex = lindex - myDict->topitem;
  490     } else {
  491     wordInput->setEditText(dictList->text(dictList->currentItem()));
  492     wordInput->lineEdit()->selectAll();
  493     }
  494 
  495     RTFOutput->setText(myDict->GetDefinition(myDict->topitem + dictList->currentItem()));
  496     RTFOutput->setCursorPosition(0, 0);
  497 
  498     UpdateHistory();
  499     tipsVisible = false;
  500 
  501     if (config->autoPlay)
  502     PlaySelected (dictList->currentItem());
  503 
  504     this->show();
  505     this->raise();
  506 
  507     if (this->isMinimized())
  508     this->showNormal();
  509 
  510     if (config->setFocusOnSelf)
  511     this->setActiveWindow();
  512 }
  513 
  514 void Kydpdict::newFromLine(const QString& aEntry)
  515 {
  516     if (eventLock)
  517     return;
  518 
  519     eventLock = true;
  520     if (!aEntry.isEmpty()) {
  521     QString lentry = aEntry.stripWhiteSpace();
  522     if ((lentry.length())&&(lentry != lastEdit)) {
  523         lastEdit = lentry;
  524         showEntry(&lentry);
  525     }
  526     }
  527     eventLock = false;
  528 }
  529 
  530 void Kydpdict::newFromList(int aindex)
  531 {
  532     if (eventLock)
  533       return;
  534 
  535     eventLock = true;
  536     if (aindex >= 0) {
  537     showEntry(NULL, aindex);
  538     lastEdit = "";
  539     }
  540     eventLock = false;
  541 }
  542 
  543 void Kydpdict::newFromSelection()
  544 {
  545     // do nothing if clipboard tracking is disabled
  546     if (!(config->clipTracking))
  547     return;
  548     // do nothing if minimized
  549     if (this->isMinimized())
  550     return;
  551     // do nothing if it comes from us
  552     if (((RTFOutput->hasSelectedText())||(wordInput->lineEdit()->hasSelectedText())) && (config->ignoreOwnSelection))
  553         return;
  554     if (eventLock)
  555     return;
  556 
  557     eventLock = true;
  558     updateUserTimestamp();
  559 
  560     // get selection - if selection and clipboard are changed simultaneously selection
  561     // is chosen an clipboard is ignored
  562     QString lnewEntry = cb->text(QClipboard::Selection).stripWhiteSpace(); 
  563     if (lnewEntry.length() && lnewEntry != lastSelection)
  564     lastSelection = lnewEntry;
  565     else
  566     lnewEntry = "";
  567 
  568     if (lnewEntry.isEmpty()) {
  569     // get clipboard if selection empty or unchanged
  570     lnewEntry = cb->text(QClipboard::Clipboard).stripWhiteSpace();    
  571     if (lnewEntry.length() && lnewEntry != lastClipboard) {
  572         lastClipboard = lnewEntry;
  573         if (lastSelection == lastClipboard)
  574         lnewEntry = "";
  575     } else
  576         lnewEntry = "";
  577     } else
  578     lastClipboard = cb->text(QClipboard::Clipboard).stripWhiteSpace();
  579     // prevent from spurious apperance (in next second after selection was shown)
  580 
  581     if (lnewEntry.length())
  582     showEntry(&lnewEntry, -2);
  583 
  584     eventLock = false;
  585 }
  586 
  587 void Kydpdict::newFromClick(QListBoxItem *lbi)
  588 {
  589     if (tipsVisible)
  590     newFromList(dictList->currentItem());
  591 }
  592 
  593 void Kydpdict::showEvent(QShowEvent *ashowEvent)
  594 {
  595     static bool sfirstStart = true;
  596 
  597     if (sfirstStart)
  598     sfirstStart = false;
  599     else
  600     newFromSelection();
  601     QMainWindow::showEvent(ashowEvent);
  602     m_checkTimer->start(TIMER_PERIOD, FALSE);
  603 }
  604 
  605 void Kydpdict::FuzzySearch()
  606 {
  607     mySearch->show();
  608 }
  609 
  610 void Kydpdict::PlaySelected (int index)
  611 {
  612     if ((index>=0) && config->toPolish)
  613     myDict->Play(index+myDict->topitem,config);
  614 }
  615 
  616 void Kydpdict::PlayCurrent ()
  617 {
  618     UpdateHistory();
  619     PlaySelected(dictList->currentItem());
  620 }
  621 
  622 void Kydpdict::UpdateHistory(void)
  623 {
  624     int i;
  625     QString content = (dictList->currentText()).simplifyWhiteSpace();
  626 
  627     if (content.length()<1)
  628     return;
  629     if ((wordInput->listBox())->findItem(content, Qt::ExactMatch) != 0)
  630     return;
  631 
  632     wordInput->blockSignals( TRUE );
  633 
  634     if ((wordInput->listBox())->count() < COMBO_HISTORY_SIZE) {
  635     wordInput->insertItem(content);
  636     } else {
  637     for (i=0;i<COMBO_HISTORY_SIZE;i++)
  638         (wordInput->listBox())->changeItem( (wordInput->listBox())->text(i+1), i );
  639     (wordInput->listBox())->changeItem( content, COMBO_HISTORY_SIZE-1 );
  640     }
  641     wordInput->blockSignals( FALSE );
  642 }
  643 
  644 void Kydpdict::SwapLanguages ()
  645 {
  646     SwapLang(!(config->toPolish), config->language);
  647 }
  648 
  649 void Kydpdict::SwapEngToPol ()
  650 {
  651     SwapLang(1, LANG_ENGLISH);
  652 }
  653 
  654 void Kydpdict::SwapPolToEng ()
  655 {
  656     SwapLang(0, LANG_ENGLISH);
  657 }
  658 
  659 void Kydpdict::SwapGerToPol ()
  660 {
  661     SwapLang(1, LANG_DEUTSCH);
  662 }
  663 
  664 void Kydpdict::SwapPolToGer ()
  665 {
  666     SwapLang(0, LANG_DEUTSCH);
  667 }
  668 
  669 void Kydpdict::SwapLang (bool toPolish, int language)
  670 {
  671     int a;
  672 
  673     switch (language) {
  674         case LANG_ENGLISH:
  675         if (((toPolish) && (!(myDict->GetDictionaryInfo() & hasEnglish2Polish))) ||
  676            ((!toPolish) && (!(myDict->GetDictionaryInfo() & hasPolish2English))))
  677            return;
  678         break;
  679         case LANG_DEUTSCH:
  680         if (((toPolish) && (!(myDict->GetDictionaryInfo() & hasGerman2Polish))) ||
  681            ((!toPolish) && (!(myDict->GetDictionaryInfo() & hasPolish2German))))
  682         break;
  683     }
  684 
  685     if (!((config->toPolish == toPolish) && (config->language == language))) {
  686         QString word = wordInput->currentText();
  687         myDict->CloseDictionary();
  688         config->toPolish = toPolish;
  689         config->language = language;
  690         config->save();
  691         do {
  692             a = myDict->OpenDictionary();
  693             if (a) Configure(TRUE);
  694         } while (a);
  695         UpdateLook();
  696         lastEdit = "";
  697         newFromLine(word);
  698     }
  699 }
  700 
  701 void Kydpdict::ShowAbout()
  702 {
  703     cb->blockSignals( TRUE );
  704     QMessageBox aboutBox;
  705 
  706     aboutBox.setCaption("Kydpdict " VERSION);
  707     aboutBox.setText(
  708         tr("This is frontend to various dictionaries.\n"
  709         "Author: Maciej Witkowiak.\n\n"
  710         "YDP Engine by Maciej Witkowiak, Andrzej Para\n"
  711         "(based on ydpdict by Wojtek Kaniewski)\n\n"
  712         "PWN Oxford 2003 Engine by Maciej Witkowiak\n\n"
  713         "SAP Engine by Maciej Witkowiak\n"
  714         "(based on sap by Bohdan R. Rau)"));
  715     aboutBox.setIconPixmap(QPixmap(babelfish_xpm));
  716     aboutBox.exec();
  717 
  718     cb->blockSignals( FALSE );
  719 }
  720 
  721 void Kydpdict::ShowAboutQt()
  722 {
  723     cb->blockSignals( TRUE );
  724     QMessageBox::aboutQt(this, "Kydpdict");
  725     cb->blockSignals( FALSE );
  726 }
  727 
  728 void Kydpdict::ToggleClipTracking()
  729 {
  730     config->clipTracking=!config->clipTracking;
  731     config->save();
  732     but_Clipboard->setOn(config->clipTracking);
  733     trayMenu->setItemChecked(trayMenuItemClipTrack, config->clipTracking);
  734     if (config->clipTracking) {
  735         cb->clear(QClipboard::Selection);
  736         cb->clear(QClipboard::Clipboard);
  737     }
  738 }
  739 
  740 void Kydpdict::ConfigureKydpdict()
  741 {
  742     Configure(FALSE);
  743     UpdateLook();
  744 }
  745 
  746 void Kydpdict::ToolbarShowHide(bool visible)
  747 {
  748     menu->setItemChecked(toolBarMenuItem, visible);
  749     if (visible != config->toolBarVisible) {
  750     config->toolBarVisible = visible;
  751     config->save();
  752     if (config->toolBarVisible)
  753         toolBar->show();
  754     else
  755         toolBar->hide();
  756     }
  757 }
  758 
  759 void Kydpdict::ShowToolbar()
  760 {
  761     ToolbarShowHide(!config->toolBarVisible);
  762 }
  763 
  764 void Kydpdict::Configure(bool status)
  765 {
  766     int result;
  767 
  768     ydpConfigure *myConf = new ydpConfigure(config);
  769 
  770     m_checkTimer->stop();
  771     cb->blockSignals( TRUE );
  772 
  773     result = myConf->exec();
  774 
  775     if (!(result == QDialog::Accepted) && status ) {
  776         QMessageBox::critical( this, tr("Error"),
  777         tr("Kydpdict can't work with incorrect path to dictionary files.\n"
  778            "In order to use this program you have to have data files from Windows\n"
  779             "dictionary installation. For more information please read README.\n"
  780             "Good bye!"));
  781         delete myConf;
  782         exit(1);
  783     }
  784 
  785     delete myConf;
  786 
  787     if (result == QDialog::Accepted)    // just a performance gain
  788         config->save();
  789 
  790     if (cb->supportsSelection())
  791         cb->clear(QClipboard::Selection);
  792     else
  793         cb->clear(QClipboard::Clipboard);
  794     cb->blockSignals( FALSE );
  795     m_checkTimer->start(TIMER_PERIOD, FALSE);
  796 }
  797 
  798 void Kydpdict::UpdateLook()
  799 {
  800     QBrush paper;
  801     QPixmap pixmap;
  802 
  803         if(QString::compare(config->kBckgrndPix, "NoBackground") != 0 && pixmap.load(config->kBckgrndPix)) {
  804         paper.setPixmap(pixmap);
  805         RTFOutput->setPaper( paper );
  806         dictList->setPaletteBackgroundPixmap( pixmap );
  807         dictList->setStaticBackground(TRUE);
  808     } else {
  809         RTFOutput->setPaper(QColor(config->kBckgrndKol));
  810         dictList->setPaletteBackgroundColor(QColor(config->kBckgrndKol));
  811         wordInput->setBackgroundMode(PaletteBase);
  812         wordInput->setPaletteBackgroundColor(config->kBckgrndKol);
  813     }
  814 
  815     dictList->setPaletteForegroundColor ( QColor(config->kFontKolor4) );
  816     if (but_EnPl) but_EnPl->setEnabled(TRUE);
  817     if (but_PlEn) but_PlEn->setEnabled(TRUE);
  818     if (but_DePl) but_DePl->setEnabled(TRUE);
  819     if (but_PlDe) but_PlDe->setEnabled(TRUE);
  820     but_Clipboard->setOn(config->clipTracking);
  821     trayMenu->setItemChecked(trayMenuItemClipTrack, config->clipTracking);
  822 
  823     switch (config->language) { //mozna dopisac kolejne case'y dla kolejnych jezykow
  824         case LANG_DEUTSCH:
  825             if (gerToPol) menu->setItemChecked(gerToPol, config->toPolish);
  826             if (polToGer) menu->setItemChecked(polToGer, !config->toPolish);
  827             if (polToEng) menu->setItemChecked(polToEng, FALSE);
  828             if (engToPol) menu->setItemChecked(engToPol, FALSE);
  829             if(config->toPolish) {
  830                 this->setCaption("De --> Pl - Kydpdict");
  831                 if (but_DePl) but_DePl->setEnabled(FALSE);
  832             } else {
  833                 this->setCaption("Pl --> De - Kydpdict");
  834                 if (but_PlDe) but_PlDe->setEnabled(FALSE);
  835             }
  836             break;
  837         case LANG_ENGLISH:
  838             if (engToPol) menu->setItemChecked(engToPol, config->toPolish);
  839             if (polToEng) menu->setItemChecked(polToEng, !config->toPolish);
  840             if (gerToPol) menu->setItemChecked(gerToPol, FALSE);
  841             if (polToGer) menu->setItemChecked(polToGer, FALSE);
  842             if(config->toPolish) {
  843                 this->setCaption("En --> Pl - Kydpdict");
  844                 if (but_EnPl) but_EnPl->setEnabled(FALSE);
  845             } else {
  846                 this->setCaption("Pl --> En - Kydpdict");
  847                 if (but_PlEn) but_PlEn->setEnabled(FALSE);
  848             }
  849             break;
  850         default:
  851             break; //cos jest nie tak
  852     }
  853 
  854     QString tmp = ".\tc " + config->kFontKolor4;
  855 
  856     char *tst = const_cast<char*>((const char*)tmp);
  857     f1_xpm[2] = tst;
  858     f2_xpm[2] = tst;
  859     f3_xpm[2] = tst;
  860     f4_xpm[2] = tst;
  861     f5_xpm[2] = tst;
  862     f6_xpm[2] = tst;
  863     f7_xpm[2] = tst;
  864     f8_xpm[2] = tst;
  865     f9_xpm[2] = tst;
  866     f10_xpm[2] = tst;
  867     f11_xpm[2] = tst;
  868 
  869     QMimeSourceFactory::defaultFactory()->setImage( "f1", QImage(f1_xpm) );
  870     QMimeSourceFactory::defaultFactory()->setImage( "f2", QImage(f2_xpm) );
  871     QMimeSourceFactory::defaultFactory()->setImage( "f3", QImage(f3_xpm) );
  872     QMimeSourceFactory::defaultFactory()->setImage( "f4", QImage(f4_xpm) );
  873     QMimeSourceFactory::defaultFactory()->setImage( "f5", QImage(f5_xpm) );
  874     QMimeSourceFactory::defaultFactory()->setImage( "f6", QImage(f6_xpm) );
  875     QMimeSourceFactory::defaultFactory()->setImage( "f7", QImage(f7_xpm) );
  876     QMimeSourceFactory::defaultFactory()->setImage( "f8", QImage(f8_xpm) );
  877     QMimeSourceFactory::defaultFactory()->setImage( "f9", QImage(f9_xpm) );
  878     QMimeSourceFactory::defaultFactory()->setImage( "f10", QImage(f10_xpm) );
  879     QMimeSourceFactory::defaultFactory()->setImage( "f11", QImage(f11_xpm) );
  880 
  881     RTFOutput->setFont(config->fontTransFont);
  882     dictList->setFont(config->fontTransFont);
  883 // XXX seems to be a bug in MacOS Qt - drop-down arrow is overwritten with combo contents
  884 #ifndef Q_WS_MACX
  885     wordInput->setFont(config->fontTransFont);
  886 #endif
  887 
  888     // this refresh is required for font update
  889     int item;
  890     item = dictList->currentItem();
  891     myDict->ListRefresh(myDict->topitem);
  892     dictList->setCurrentItem(item);
  893 
  894     scrollBar->setMaxValue(myDict->wordCount);
  895 }
  896 
  897 void Kydpdict::handleLink( const QString & href )
  898 {
  899     QString link = href;
  900 
  901     tipsVisible = true;
  902 
  903     if (!((link.startsWith("0") || link.startsWith("1")))) {
  904         RTFOutput->setText(myDict->GetInfoPage());
  905         RTFOutput->scrollToAnchor(link);
  906     } else {
  907         if (link.startsWith("1"))
  908             SwapLanguages();
  909 
  910         link.remove(0,1);
  911         if (link.compare(dictList->currentText()) != 0) {
  912             newFromLine(link);
  913         } else {
  914             // it is required to do something with RTFOutput even when it's not needed
  915             RTFOutput->setText(RTFOutput->text());
  916         }
  917     }
  918 }
  919 
  920 void Kydpdict::handleTip( const QString & href )
  921 {
  922     static QString lastone;
  923     int i;
  924 
  925     if ((href.length() == 0) && (lastone.length() == 0))
  926     return;
  927 
  928     lastone = href;
  929 
  930     for (i=0; i< myDict->GetTipNumber(0); i++) {
  931     if (myDict->GetInputTip(i).compare(href)==0) {
  932         myDynamicTip->tekst = myDict->GetOutputTip(i);
  933         return;
  934     }
  935     }
  936     myDynamicTip->tekst = "";
  937 }
  938 
  939 void Kydpdict::updateUserTimestamp(void) {
  940 #ifndef WITHOUT_X11
  941 #if QT_VERSION >= 0x030300
  942 #if defined Q_WS_X11
  943     // this is from kapplication.cpp, as suggested by klipper.cpp, quote:
  944     // QClipboard uses qt_x_user_time as the timestamp for selection operations.
  945     // It is updated mainly from user actions, but Klipper polls the clipboard
  946     // without any user action triggering it, so qt_x_user_time may be old,
  947     // which could possibly lead to QClipboard reporting empty clipboard.
  948     // Therefore, qt_x_user_time needs to be updated to current X server timestamp.
  949     unsigned long time;
  950     // get current X timestamp
  951     Window w = XCreateSimpleWindow( qt_xdisplay(), qt_xrootwin(), 0, 0, 1, 1, 0, 0, 0 );
  952     XSelectInput( qt_xdisplay(), w, PropertyChangeMask );
  953     unsigned char data[ 1 ];
  954     XChangeProperty( qt_xdisplay(), w, XA_ATOM, XA_ATOM, 8, PropModeAppend, data, 1 );
  955     XEvent ev;
  956     XWindowEvent( qt_xdisplay(), w, PropertyChangeMask, &ev );
  957     time = ev.xproperty.time;
  958     XDestroyWindow( qt_xdisplay(), w );
  959     qt_x_user_time = time;
  960 #endif
  961 #endif
  962 #endif
  963 }