Now the systrayicon change it's color when a download is in progress. I simply change...
[kdenetwork.git] / kdict / toplevel.cpp
blobee334cbd1a08857f87e0fa53ddb7e4459388628e
1 /* -------------------------------------------------------------
3 toplevel.cpp (part of The KDE Dictionary Client)
5 Copyright (C) 2000-2001 Christian Gebauer <gebauer@kde.org>
6 (C) by Matthias Hölzer 1998
8 This file is distributed under the Artistic License.
9 See LICENSE for details.
11 -------------------------------------------------------------
13 TopLevel The toplevel widget of Kdict.
15 ------------------------------------------------------------- */
17 #include <qclipboard.h>
19 #include <kstdaccel.h>
20 #include <kstdaction.h>
21 #include <kapplication.h>
22 #include <kstatusbar.h>
23 #include <klocale.h>
24 #include <kwin.h>
25 #include <kedittoolbar.h>
26 #include <kdebug.h>
27 #include <dcopclient.h>
29 #include "actions.h"
30 #include "dict.h"
31 #include "options.h"
32 #include "queryview.h"
33 #include "matchview.h"
34 #include "sets.h"
35 #include "toplevel.h"
38 // cut a QString and add "..."
39 QString getShortString(QString str,unsigned int length)
41 if (str.length()>length) {
42 str.truncate(length-3);
43 str.append("...");
45 return str;
49 DictInterface *interface;
50 GlobalData *global;
53 TopLevel::TopLevel(QWidget* parent, const char* name)
54 : DCOPObject("KDictIface"), KMainWindow(parent, name, WType_TopLevel),
55 optDlg(0L), setsDlg(0L), stopRef(0)
57 kapp->dcopClient()->setDefaultObject(objId());
58 kapp->setMainWidget(this);
60 global = new GlobalData();
61 global->topLevel = this;
62 global->read();
63 interface = new DictInterface();
64 connect(interface,SIGNAL(infoReady()),SLOT(stratDbChanged()));
65 connect(interface,SIGNAL(started(const QString&)),SLOT(clientStarted(const QString&)));
66 connect(interface,SIGNAL(stopped(const QString&)),SLOT(clientStopped(const QString&)));
68 queryView = new QueryView(this);
69 connect(queryView,SIGNAL(defineRequested(const QString&)),SLOT(define(const QString&)));
70 connect(queryView,SIGNAL(matchRequested(const QString&)),SLOT(match(const QString&)));
71 connect(queryView,SIGNAL(clipboardRequested()),SLOT(defineClipboard()));
72 connect(queryView,SIGNAL(enableCopy(bool)),SLOT(enableCopy(bool)));
73 connect(queryView,SIGNAL(enablePrintSave()),SLOT(enablePrintSave()));
74 connect(queryView,SIGNAL(renderingStarted()),SLOT(renderingStarted()));
75 connect(queryView,SIGNAL(renderingStopped()),SLOT(renderingStopped()));
76 connect(queryView,SIGNAL(newCaption(const QString&)),SLOT(newCaption(const QString&)));
78 matchView = new MatchView();
79 connect(matchView,SIGNAL(defineRequested(const QString&)),SLOT(define(const QString&)));
80 connect(matchView,SIGNAL(matchRequested(const QString&)),SLOT(match(const QString&)));
81 connect(matchView,SIGNAL(clipboardRequested()),SLOT(matchClipboard()));
82 connect(matchView,SIGNAL(windowClosed()),SLOT(toggleMatchListShow()));
83 connect(&resetStatusbarTimer,SIGNAL(timeout()),SLOT(resetStatusbar()));
85 setupStatusBar();
86 setupActions();
87 recreateGUI();
88 buildHistMenu();
90 if (global->showMatchList)
91 { // show splitter, html view & match list
92 splitter = new QSplitter(QSplitter::Horizontal,this);
93 splitter->setOpaqueResize( KGlobalSettings::opaqueResize() );
94 queryView->reparent(splitter,0,queryView->pos(),true);
95 matchView->reparent(splitter,0,matchView->pos(),true);
96 setCentralWidget(splitter);
97 splitter->setResizeMode(matchView,QSplitter::KeepSize);
98 adjustMatchViewSize();
100 else
101 { // show only html view
102 setCentralWidget(queryView);
103 matchView->hide();
106 //apply settings
107 resize(600,390);
108 applyMainWindowSettings(KGlobal::config(),"toplevel_options");
109 stratDbChanged(); // fill combos, build menus
111 actQueryCombo->setFocus(); // place cursor in combobox
115 TopLevel::~TopLevel()
120 void TopLevel::normalStartup()
122 if (global->defineClipboard)
123 defineClipboard();
126 // ******* DCOP Interface ********************************************************
128 void TopLevel::quit()
130 kdDebug(5004) << "*DCOP call* TopLevel::quit()" << endl;
131 kapp->closeAllWindows();
135 void TopLevel::makeActiveWindow()
137 kdDebug(5004) << "*DCOP call* TopLevel::makeActiveWindow()" << endl;
138 raiseWindow();
142 void TopLevel::definePhrase(QString phrase)
144 kdDebug(5004) << "*DCOP call* TopLevel::definePhrase()" << endl;
145 define(phrase);
146 raiseWindow();
150 void TopLevel::matchPhrase(QString phrase)
152 kdDebug(5004) << "*DCOP call* TopLevel::matchPhrase()" << endl;
153 match(phrase);
154 raiseWindow();
158 void TopLevel::defineClipboardContent()
160 kdDebug(5004) << "*DCOP call* TopLevel::defineClipboardContent()" << endl;
161 defineClipboard();
162 raiseWindow();
166 void TopLevel::matchClipboardContent()
168 kdDebug(5004) << "*DCOP call* TopLevel::matchClipboardContent()" << endl;
169 matchClipboard();
170 raiseWindow();
174 QStringList TopLevel::getDatabases()
176 kdDebug(5004) << "*DCOP call* TopLevel::getDatabases()" << endl;
177 return global->databases;
181 QString TopLevel::currentDatabase()
183 kdDebug(5004) << "*DCOP call* TopLevel::currentDatabase()" << endl;
184 return global->databases[global->currentDatabase];
188 QStringList TopLevel::getStrategies()
190 kdDebug(5004) << "*DCOP call* TopLevel::getStrategies()" << endl;
191 return global->strategies;
195 QString TopLevel::currentStrategy()
197 kdDebug(5004) << "*DCOP call* TopLevel::currentStrategy()" << endl;
198 return global->strategies[global->currentStrategy];
202 bool TopLevel::setDatabase(QString db)
204 kdDebug(5004) << "*DCOP call* TopLevel::setDatabase()" << endl;
206 int newCurrent = global->databases.findIndex(db);
207 if (newCurrent == -1)
208 return false;
209 else {
210 global->currentDatabase = newCurrent;
211 actDbCombo->setCurrentItem(global->currentDatabase);
212 return true;
217 bool TopLevel::setStrategy(QString strategy)
219 kdDebug(5004) << "*DCOP call* TopLevel::setStrategy()" << endl;
221 return matchView->selectStrategy(strategy);
225 bool TopLevel::historyGoBack()
227 kdDebug(5004) << "*DCOP call* TopLevel::historyGoBack()" << endl;
229 if (!queryView->browseBackPossible())
230 return false;
231 else {
232 queryView->browseBack();
233 return true;
238 bool TopLevel::historyGoForward()
240 kdDebug(5004) << "*DCOP call* TopLevel::historyGoForward()" << endl;
242 if (!queryView->browseForwardPossible())
243 return false;
244 else {
245 queryView->browseForward();
246 return true;
250 // *******************************************************************************
252 void TopLevel::define(const QString &query)
254 kdDebug(5004) << "TopLevel::define()" << endl;
255 actQueryCombo->setEditText(query);
256 doDefine();
260 void TopLevel::defineClipboard()
262 kdDebug(5004) << "TopLevel::defineClipboard()" << endl;
263 kapp->clipboard()->setSelectionMode(true);
264 QString text = kapp->clipboard()->text();
265 if (text.isEmpty()) {
266 kapp->clipboard()->setSelectionMode(false);
267 text = kapp->clipboard()->text();
269 define(text);
273 void TopLevel::match(const QString &query)
275 kdDebug(5004) << "TopLevel::match()" << endl;
276 actQueryCombo->setEditText(query);
277 doMatch();
281 void TopLevel::matchClipboard()
283 kdDebug(5004) << "TopLevel::matchClipboard()" << endl;
284 kapp->clipboard()->setSelectionMode(true);
285 QString text = kapp->clipboard()->text();
286 if (text.isEmpty()) {
287 kapp->clipboard()->setSelectionMode(false);
288 text = kapp->clipboard()->text();
290 match(text);
294 bool TopLevel::queryClose()
296 kdDebug(5004) << "TopLevel::queryClose()" << endl;
298 saveMainWindowSettings(KGlobal::config(),"toplevel_options");
299 saveMatchViewSize();
300 global->queryComboCompletionMode = actQueryCombo->completionMode();
302 global->write();
304 return true;
308 void TopLevel::setupActions()
310 // file menu...
311 actSave = KStdAction::save(queryView, SLOT(saveQuery()), actionCollection());
312 actSave->setText(i18n("&Save As..."));
313 actSave->setEnabled(false);
314 actPrint = KStdAction::print(queryView, SLOT(printQuery()), actionCollection());
315 actPrint->setEnabled(false);
316 actStartQuery = new KAction(i18n("St&art Query"),"reload", 0 , this,
317 SLOT(doDefine()), actionCollection(), "start_query");
318 actStopQuery = new KAction(i18n("St&op Query"),"stop", 0 , this,
319 SLOT(stopClients()), actionCollection(), "stop_query");
320 actStopQuery->setEnabled(false);
321 KStdAction::quit(kapp, SLOT(closeAllWindows()), actionCollection());
323 // edit menu...
324 actCopy = KStdAction::copy(queryView, SLOT(copySelection()), actionCollection());
325 actCopy->setEnabled(false);
326 KStdAction::selectAll(queryView, SLOT(selectAll()), actionCollection());
327 new KAction(i18n("&Define Clipboard Content"), "define_clip", 0 , this,
328 SLOT(defineClipboard()), actionCollection(), "define_clipboard");
329 new KAction(i18n("&Match Clipboard Content"), 0 , this,
330 SLOT(matchClipboard()), actionCollection(), "match_clipboard");
331 KStdAction::find(queryView, SLOT(showFindDialog()), actionCollection());
333 // history menu...
334 actBack = new KToolBarPopupAction(i18n("&Back"), "back", KStdAccel::shortcut(KStdAccel::Back),
335 queryView, SLOT(browseBack()), actionCollection(),"browse_back");
336 actBack->setDelayed(true);
337 actBack->setStickyMenu(false);
338 actBack->setEnabled(false);
339 actForward = new KToolBarPopupAction(i18n("&Forward"), "forward", KStdAccel::shortcut(KStdAccel::Forward),
340 queryView, SLOT(browseForward()), actionCollection(),"browse_forward");
341 actForward->setDelayed(true);
342 actForward->setStickyMenu(false);
343 actForward->setEnabled(false);
344 new KAction(i18n("&Clear History"), 0 , this,
345 SLOT(clearQueryHistory()), actionCollection(), "clear_history");
347 // server menu...
348 new KAction(i18n("&Get Capabilities"), 0 , interface,
349 SLOT(updateServer()), actionCollection(), "get_capabilities");
350 new KAction(i18n("Edit &Database Sets..."), "edit", 0 , this,
351 SLOT(showSetsDialog()), actionCollection(), "edit_sets");
352 new KAction(i18n("&Summary"), 0 , interface,
353 SLOT(showDatabases()), actionCollection(), "db_summary");
354 new KAction(i18n("S&trategy Information"), 0 , interface,
355 SLOT(showStrategies()), actionCollection(), "strategy_info");
356 new KAction(i18n("&Server Information"), 0 , interface,
357 SLOT(showInfo()), actionCollection(), "server_info");
359 // settings menu...
360 createStandardStatusBarAction();
361 setStandardToolBarMenuEnabled(true);
363 actShowMatchList = new KToggleAction(i18n("Show &Match List"), 0 , this,
364 SLOT(toggleMatchListShow()), actionCollection(), "show_match");
365 actShowMatchList->setCheckedState(i18n("Hide &Match List"));
366 actShowMatchList->setChecked(global->showMatchList);
367 KStdAction::keyBindings(guiFactory(), SLOT(configureShortcuts()),
368 actionCollection());
369 KStdAction::configureToolbars(this, SLOT(slotConfToolbar()), actionCollection());
370 KStdAction::preferences(this, SLOT(showOptionsDialog()), actionCollection());
372 // toolbar...
373 new KAction(i18n("Clear Input Field"), "query_erase", 0 , this,
374 SLOT(clearInput()), actionCollection(), "clear_query");
376 actQueryLabel = new DictLabelAction(i18n("&Look for:"), actionCollection(), "look_label");
377 actQueryCombo = new DictComboAction(i18n("Query"), actionCollection(), "query_combo",true,true);
378 connect(actQueryCombo,SIGNAL(activated(const QString &)), SLOT(define(const QString&)));
379 actQueryCombo->setCompletionMode(global->queryComboCompletionMode);
380 actDbLabel = new DictLabelAction(i18n("&in"), actionCollection(), "in_label");
381 actDbCombo = new DictComboAction(i18n("Databases"), actionCollection(), "db_combo",false,false);
382 connect(actDbCombo,SIGNAL(activated(int)),SLOT(databaseSelected(int)));
383 actDefineBtn = new DictButtonAction(i18n("&Define"), this, SLOT(doDefine()), actionCollection(), "define_btn");
384 actMatchBtn = new DictButtonAction(i18n("&Match"), this, SLOT(doMatch()), actionCollection(), "match_btn");
386 queryView->setActions(actBack,actForward,actQueryCombo);
390 void TopLevel::setupStatusBar()
392 statusBar()->insertItem(i18n(" Ready "),0,2);
393 statusBar()->setItemAlignment(0,AlignLeft | AlignVCenter);
395 QString serverInfo;
396 if (global->authEnabled)
397 serverInfo = QString(" %1@%2:%3 ").arg(getShortString(global->user,50))
398 .arg(getShortString(global->server,50))
399 .arg(global->port);
400 else
401 serverInfo = QString(" %1:%3 ").arg(getShortString(global->server,50))
402 .arg(global->port);
403 statusBar()->insertItem(serverInfo, 1,3);
404 statusBar()->setItemAlignment(1,AlignLeft | AlignVCenter);
408 void TopLevel::recreateGUI()
410 kdDebug(5004) << "TopLevel::recreateGUI()" << endl;
411 createGUI("kdictui.rc", false);
412 actQueryCombo->setList(global->queryHistory);
413 actQueryCombo->clearEdit();
414 actQueryLabel->setBuddy(actQueryCombo->widget());
416 actDbCombo->setList(global->databases);
417 actDbCombo->setCurrentItem(global->currentDatabase);
418 actDbLabel->setBuddy(actDbCombo->widget());
419 int bwidth;
420 if (actDefineBtn->widthHint() > actMatchBtn->widthHint())
421 bwidth = actDefineBtn->widthHint();
422 else
423 bwidth = actMatchBtn->widthHint();
424 actDefineBtn->setWidth(bwidth);
425 actMatchBtn->setWidth(bwidth);
429 // add text in the query-combobox to the history
430 void TopLevel::addCurrentInputToHistory()
432 QString text(actQueryCombo->currentText());
434 // maintain queryHistory
435 global->queryHistory.remove(text); // no double entrys
436 global->queryHistory.prepend(text); // prepend new item
437 while (global->queryHistory.count()>global->maxHistEntrys) // shorten list
438 global->queryHistory.remove(global->queryHistory.fromLast());
440 actQueryCombo->setList(global->queryHistory);
441 actQueryCombo->setCurrentItem(0);
442 buildHistMenu();
447 // erase text in query-combobox
448 void TopLevel::clearInput()
450 actQueryCombo->clearEdit();
451 actQueryCombo->setFocus(); // place cursor in combobox
455 // define text in the combobox
456 void TopLevel::doDefine()
458 QString text(actQueryCombo->currentText());
460 if (!text.isEmpty())
462 addCurrentInputToHistory();
463 actQueryCombo->selectAll();
464 interface->define(text);
469 void TopLevel::doMatch()
471 QString text(actQueryCombo->currentText());
473 if (!text.isEmpty())
475 addCurrentInputToHistory();
476 actQueryCombo->selectAll();
478 if (!global->showMatchList)
480 toggleMatchListShow();
483 matchView->match(text);
484 setCaption(getShortString(text.simplifyWhiteSpace(),70));
489 void TopLevel::stopClients()
491 interface->stop();
492 queryView->stop();
496 // rebuild history menu on demand
497 void TopLevel::buildHistMenu()
499 unplugActionList("history_items");
501 historyActionList.setAutoDelete(true);
502 historyActionList.clear();
504 unsigned int i = 0;
505 while ((i<10)&&(i<global->queryHistory.count())) {
506 historyActionList.append( new KAction(getShortString(global->queryHistory[i],70), 0, this, SLOT(queryHistMenu()),
507 (QObject*)0, global->queryHistory[i].utf8().data()) );
508 i++;
511 plugActionList("history_items", historyActionList);
515 // process a query via the history menu
516 void TopLevel::queryHistMenu()
518 QCString name = sender()->name();
519 if (!name.isEmpty())
520 define(QString::fromUtf8(name));
524 void TopLevel::clearQueryHistory()
526 global->queryHistory.clear();
527 actQueryCombo->clear();
528 buildHistMenu();
532 // fill combos, rebuild menus
533 void TopLevel::stratDbChanged()
535 actDbCombo->setList(global->databases);
536 actDbCombo->setCurrentItem(global->currentDatabase);
537 matchView->updateStrategyCombo();
539 unplugActionList("db_detail");
541 dbActionList.setAutoDelete(true);
542 dbActionList.clear();
544 for (unsigned int i=0;i<global->serverDatabases.count();i++)
545 dbActionList.append( new KAction(global->serverDatabases[i], 0, this, SLOT(dbInfoMenuClicked()),
546 (QObject*)0, global->serverDatabases[i].utf8().data()) );
548 plugActionList("db_detail", dbActionList);
552 void TopLevel::dbInfoMenuClicked()
554 QCString name = sender()->name();
555 if (!name.isEmpty())
556 interface->showDbInfo(name);
560 void TopLevel::databaseSelected(int num)
562 global->currentDatabase = num;
566 void TopLevel::enableCopy(bool selected)
568 actCopy->setEnabled(selected);
572 void TopLevel::enablePrintSave()
574 actSave->setEnabled(true);
575 actPrint->setEnabled(true);
579 void TopLevel::clientStarted(const QString &message)
581 statusBar()->changeItem(message,0);
582 resetStatusbarTimer.stop();
583 stopRef++;
584 actStopQuery->setEnabled(stopRef>0); // enable stop-icon
585 kapp->setOverrideCursor(waitCursor);
589 void TopLevel::clientStopped(const QString &message)
591 statusBar()->changeItem(message,0);
592 resetStatusbarTimer.start(4000);
593 if (stopRef > 0)
594 stopRef--;
595 actStopQuery->setEnabled(stopRef>0); // disable stop-icon
596 kapp->restoreOverrideCursor();
600 void TopLevel::resetStatusbar()
602 resetStatusbarTimer.stop();
603 statusBar()->changeItem(i18n(" Ready "),0);
607 void TopLevel::renderingStarted()
609 stopRef++;
610 actStopQuery->setEnabled(stopRef>0); // disable stop-icon
611 kapp->setOverrideCursor(waitCursor);
615 void TopLevel::renderingStopped()
617 if (stopRef > 0)
618 stopRef--;
619 actStopQuery->setEnabled(stopRef>0); // disable stop-icon
620 kapp->restoreOverrideCursor();
624 void TopLevel::newCaption(const QString &s)
626 setCaption(s);
629 void TopLevel::toggleMatchListShow()
631 saveMatchViewSize();
632 if (global->showMatchList) // list is visible -> hide it
634 global->showMatchList = false;
635 queryView->reparent(this,0,queryView->pos(),true);
636 matchView->reparent(this,0,matchView->pos(),true);
637 matchView->hide();
638 delete splitter;
639 setCentralWidget(queryView);
641 else // list is not visible -> show it
643 global->showMatchList = true;
644 splitter = new QSplitter(QSplitter::Horizontal,this);
645 splitter->setOpaqueResize( KGlobalSettings::opaqueResize() );
646 setCentralWidget(splitter);
647 splitter->show();
648 queryView->reparent(splitter,0,queryView->pos(),true);
649 matchView->reparent(splitter,0,matchView->pos(),true);
650 splitter->setResizeMode(matchView,QSplitter::KeepSize);
651 adjustMatchViewSize();
654 actShowMatchList->setChecked(global->showMatchList);
658 void TopLevel::saveMatchViewSize()
660 if (global->showMatchList)
662 global->splitterSizes = splitter->sizes();
667 void TopLevel::adjustMatchViewSize()
669 if (global->splitterSizes.count()==2)
671 splitter->setSizes(global->splitterSizes);
676 void TopLevel::slotConfToolbar()
678 saveMainWindowSettings(KGlobal::config(),"toplevel_options");
679 KEditToolbar dlg(actionCollection(), "kdictui.rc");
680 connect(&dlg,SIGNAL( newToolbarConfig() ), this, SLOT( slotNewToolbarConfig() ));
681 dlg.exec();
685 void TopLevel::slotNewToolbarConfig()
687 recreateGUI();
688 applyMainWindowSettings(KGlobal::config(),"toplevel_options");
689 buildHistMenu(); // actionlists must be inserted
690 stratDbChanged();
694 void TopLevel::showSetsDialog()
696 if (!setsDlg) {
697 setsDlg = new DbSetsDialog(this);
698 connect(setsDlg,SIGNAL(setsChanged()),this,SLOT(setsChanged()));
699 connect(setsDlg,SIGNAL(dialogClosed()),this,SLOT(hideSetsDialog()));
700 setsDlg->show();
701 } else {
702 KWin::activateWindow(setsDlg->winId());
707 void TopLevel::hideSetsDialog()
709 if (setsDlg) {
710 setsDlg->delayedDestruct();
711 setsDlg = 0L;
716 void TopLevel::setsChanged()
718 actDbCombo->setList(global->databases);
719 actDbCombo->setCurrentItem(global->currentDatabase);
723 void TopLevel::showOptionsDialog()
725 if (!optDlg) {
726 optDlg = new OptionsDialog(this);
727 connect(optDlg,SIGNAL(optionsChanged()),this,SLOT(optionsChanged()));
728 connect(optDlg,SIGNAL(finished()),this,SLOT(hideOptionsDialog()));
729 optDlg->show();
730 } else {
731 KWin::activateWindow(optDlg->winId());
736 void TopLevel::hideOptionsDialog()
738 if (optDlg) {
739 optDlg->delayedDestruct();
740 optDlg=0;
745 void TopLevel::optionsChanged()
747 QString serverInfo;
748 if (global->authEnabled)
749 serverInfo = QString(" %1@%2:%3 ").arg(getShortString(global->user,50))
750 .arg(getShortString(global->server,50))
751 .arg(global->port);
752 else
753 serverInfo = QString(" %1:%3 ").arg(getShortString(global->server,50))
754 .arg(global->port);
755 statusBar()->changeItem(serverInfo,1);
756 interface->serverChanged(); // inform client
757 queryView->optionsChanged(); // inform html-view
760 void TopLevel::raiseWindow()
762 // Bypass focus stealing prevention
763 kapp->updateUserTimestamp();
765 KWin::WindowInfo info = KWin::windowInfo( winId() );
767 if ( !info.isOnCurrentDesktop() )
769 KWin::setOnDesktop( winId(), KWin::currentDesktop() );
772 KWin::activateWindow(winId());
776 //--------------------------------
778 #include "toplevel.moc"