summaryrefslogtreecommitdiffstats
path: root/kdict/applet/kdictapplet.cpp
blob: ad9078640a5c0112bef78da19b267d9e5b849eb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* -------------------------------------------------------------

   kdictapplet.h (part of The KDE Dictionary Client)

   Copyright (C) 2001 Christian Gebauer <gebauer@kde.org>

   The applet is loosely based on the "Run" applet included in KDE.
   Copyright (c) 2000 Matthias Elter <elter@kde.org> (Artistic License)

   This file is distributed under the Artistic License.
   See LICENSE for details.

    -------------------------------------------------------------

    PopupBox        helper class
    DictApplet      a small kicker-applet

 ------------------------------------------------------------- */

#include <qlabel.h>
#include <qpushbutton.h>
#include <qtimer.h>
#include <qlayout.h>
#include <qtooltip.h>

#include <kconfig.h>
#include <kcombobox.h>
#include <kiconloader.h>
#include <klocale.h>
#include <dcopclient.h>
#include <kapplication.h>
#include <kprocess.h>

#include "kdictapplet.h"

//*********  PopupBox  ********************************************

PopupBox::PopupBox()
 : QHBox(0, 0, WStyle_Customize | WType_Popup ), popupEnabled(true)
{
}


PopupBox::~PopupBox()
{}


bool PopupBox::showBox()
{
  if (!popupEnabled)    // prevents that the popup is shown again immediatly
    return false;
  else {
    show();
    return true;
  }
}


void PopupBox::hideEvent(QHideEvent *)
{
  emit(hidden());
  popupEnabled = false;
  QTimer::singleShot(100, this, SLOT(enablePopup()));
}


void PopupBox::enablePopup()
{
  popupEnabled = true;
}


//*********  DictApplet  ********************************************

extern "C"
{
  KDE_EXPORT KPanelApplet* init(QWidget *parent, const QString& configFile)
  {
    KGlobal::locale()->insertCatalogue("kdictapplet");
    return new DictApplet(configFile, KPanelApplet::Stretch, 0, parent, "kdictapplet");
  }
}


DictApplet::DictApplet(const QString& configFile, Type type, int actions, QWidget *parent, const char *name)
  : KPanelApplet(configFile, type, actions, parent, name), waiting(0)
{
  // first the widgets for a horizontal panel
  baseWidget = new QWidget(this);
  QGridLayout *baseLay = new QGridLayout(baseWidget,2,6,0,1);

  textLabel = new QLabel(i18n("Dictionary:"), baseWidget);
  textLabel->setBackgroundOrigin(AncestorOrigin);
  QFont f(textLabel->font());
  f.setPixelSize(12);
  textLabel->setFont(f);
  baseLay->addWidget(textLabel,0,1);
  QToolTip::add(textLabel,i18n("Look up a word or phrase with Kdict"));

  iconLabel = new QLabel(baseWidget);
  iconLabel->setBackgroundOrigin(AncestorOrigin);
  QPixmap pm = KGlobal::iconLoader()->loadIcon("kdict", KIcon::Panel, KIcon::SizeSmall, KIcon::DefaultState, 0L, true);
  iconLabel->setPixmap(pm);
  baseLay->addWidget(iconLabel,1,0);
  iconLabel->setAlignment(Qt::AlignCenter | Qt::AlignVCenter);
  iconLabel->setFixedWidth(pm.width()+4);
  QToolTip::add(iconLabel,i18n("Look up a word or phrase with Kdict"));

  f.setPixelSize(10);
  clipboardBtn = new QPushButton(i18n("C"),baseWidget);
  clipboardBtn->setBackgroundOrigin(AncestorOrigin);
  clipboardBtn->setFont(f);
  clipboardBtn->setFixedSize(16,16);
  connect(clipboardBtn, SIGNAL(clicked()), SLOT(queryClipboard()));
  baseLay->addWidget(clipboardBtn,0,3);
  QToolTip::add(clipboardBtn,i18n("Define selected text"));

  defineBtn = new QPushButton(i18n("D"),baseWidget);
  defineBtn->setBackgroundOrigin(AncestorOrigin);
  defineBtn->setFont(f);
  defineBtn->setFixedSize(16,16);
  defineBtn->setEnabled(false);
  connect(defineBtn, SIGNAL(clicked()), SLOT(startDefine()));
  baseLay->addWidget(defineBtn,0,4);
  QToolTip::add(defineBtn,i18n("Define word/phrase"));

  matchBtn = new QPushButton(i18n("M"),baseWidget);
  matchBtn->setBackgroundOrigin(AncestorOrigin);
  matchBtn->setFont(f);
  matchBtn->setFixedSize(16,16);
  matchBtn->setEnabled(false);
  connect(matchBtn, SIGNAL(clicked()), SLOT(startMatch()));
  baseLay->addWidget(matchBtn,0,5);
  QToolTip::add(matchBtn,i18n("Find matching definitions"));

  completionObject = new KCompletion();

  internalCombo = new KHistoryCombo(baseWidget);
  internalCombo->setBackgroundOrigin(AncestorOrigin);
  internalCombo->setCompletionObject(completionObject);
  internalCombo->setFocus();
  internalCombo->clearEdit();
  internalCombo->lineEdit()->installEventFilter( this );
  connect(internalCombo, SIGNAL(returnPressed(const QString&)), SLOT(startQuery(const QString&)));
  connect(internalCombo, SIGNAL(textChanged(const QString&)), SLOT(comboTextChanged(const QString&)));
  QToolTip::add(internalCombo,i18n("Look up a word or phrase with Kdict"));

  baseLay->addMultiCellWidget(internalCombo,1,1,1,5);

  baseLay->setColStretch(2,1);
	
  // widgets for a vertical panel
  verticalBtn = new QPushButton(this);
  connect(verticalBtn, SIGNAL(pressed()), SLOT(showExternalCombo()));	
  QToolTip::add(verticalBtn,i18n("Look up a word or phrase with Kdict"));

  popupBox = new PopupBox();
  popupBox->setFixedSize(160, 22);
  connect(popupBox, SIGNAL(hidden()), SLOT(externalComboHidden()));
  externalCombo = new KHistoryCombo(popupBox);
  externalCombo->setCompletionObject(completionObject);  
  connect(externalCombo, SIGNAL(returnPressed(const QString&)), SLOT(startQuery(const QString&)));
  externalCombo->setFixedSize(160, externalCombo->sizeHint().height());
  
  connect(internalCombo, SIGNAL(completionModeChanged(KGlobalSettings::Completion)),
          this, SLOT(updateCompletionMode(KGlobalSettings::Completion)));
  connect(externalCombo, SIGNAL(completionModeChanged(KGlobalSettings::Completion)),
          this, SLOT(updateCompletionMode(KGlobalSettings::Completion)));

  // restore history and completion list
  KConfig *c = config();
  c->setGroup("General");

  QStringList list = c->readListEntry("Completion list");
  completionObject->setItems(list);
  int mode = c->readNumEntry("Completion mode", 
                             KGlobalSettings::completionMode());
  internalCombo->setCompletionMode((KGlobalSettings::Completion)mode);  
  externalCombo->setCompletionMode((KGlobalSettings::Completion)mode);  

  list = c->readListEntry("History list");
  internalCombo->setHistoryItems(list);  
  externalCombo->setHistoryItems(list);
}


DictApplet::~DictApplet()
{
  // save history and completion list
  KConfig *c = config();
  c->setGroup("General");

  QStringList list = completionObject->items();
  c->writeEntry("Completion list", list);
  c->writeEntry("Completion mode", (int) internalCombo->completionMode());
  
  list = internalCombo->historyItems();
  c->writeEntry("History list", list);  
  c->sync();
  
  delete completionObject;
}


int DictApplet::widthForHeight(int height) const
{
  if (height >= 38)
    return textLabel->sizeHint().width()+55;
  else
    return textLabel->sizeHint().width()+25;
}


int DictApplet::heightForWidth(int width) const
{
  return width;
}


void DictApplet::resizeEvent(QResizeEvent*)
{
  if (orientation() == Horizontal) {
    verticalBtn->hide();
    baseWidget->show();
    baseWidget->setFixedSize(width(),height());

    if (height() < internalCombo->sizeHint().height())
      internalCombo->setFixedHeight(height());
    else
      internalCombo->setFixedHeight(internalCombo->sizeHint().height());

    if (height() >= 38) {
      textLabel->show();
      clipboardBtn->show();
      defineBtn->show();
      matchBtn->show();
      iconLabel->hide();
      internalCombo->setFixedWidth(width());
    } else {
      textLabel->hide();
      clipboardBtn->hide();
      defineBtn->hide();
      matchBtn->hide();
      iconLabel->show();
      internalCombo->setFixedWidth(width()-iconLabel->width()-1);
    }

    baseWidget->updateGeometry();
	} else 	{     // orientation() == Vertical
    verticalBtn->show();
    baseWidget->hide();
    verticalBtn->setFixedSize(width(),width());

    KIcon::StdSizes sz = 	width() < 32 ?	KIcon::SizeSmall : (width() < 48 ? KIcon::SizeMedium : KIcon::SizeLarge);
    QPixmap pm = KGlobal::iconLoader()->loadIcon("kdict", KIcon::Panel, sz, KIcon::DefaultState, 0L, true);
    verticalBtn->setPixmap(pm);
	}
}


bool DictApplet::eventFilter( QObject *o, QEvent * e)
{
  if (e->type() == QEvent::MouseButtonRelease)
  	emit requestFocus();

  return KPanelApplet::eventFilter(o, e);
}


void DictApplet::sendCommand(const QCString &fun, const QString &data)
{
  if (waiting > 0) {
    waiting = 1;
    delayedFunc = fun.copy();
    delayedData = data;
    return;
  }

  DCOPClient *client = kapp->dcopClient();
  if (!client->isApplicationRegistered("kdict")) {
    KApplication::startServiceByDesktopName("kdict");
    waiting = 1;
    delayedFunc = fun.copy();
    delayedData = data;
    QTimer::singleShot(100, this, SLOT(sendDelayedCommand()));
    return;
  } else {
    QCStringList list = client->remoteObjects("kdict");
    if (list.findIndex("KDictIface")==-1) {
      waiting = 1;
      delayedFunc = fun.copy();
      delayedData = data;
      QTimer::singleShot(100, this, SLOT(sendDelayedCommand()));
      return;
    }
  }

  client->send("kdict","default",fun,data);
}


void DictApplet::sendDelayedCommand()
{
  if (waiting > 100) {   // timeout after ten seconds
    waiting = 0;
    return;
  }

  DCOPClient *client = kapp->dcopClient();
  if (!client->isApplicationRegistered("kdict")) {
    waiting++;
    QTimer::singleShot(100, this, SLOT(sendDelayedCommand()));
    return;
  } else {
    QCStringList list = client->remoteObjects("kdict");
    if (list.findIndex("KDictIface")==-1) {
      waiting++;
      QTimer::singleShot(100, this, SLOT(sendDelayedCommand()));
      return;
    }
  }

  client->send("kdict","default",delayedFunc,delayedData);
  waiting = 0;
}


void DictApplet::startQuery(const QString &s)
{
  QString query = s.stripWhiteSpace();
  if (query.isEmpty())
    return;

  internalCombo->addToHistory(query);
  externalCombo->addToHistory(query);
  internalCombo->clearEdit();
  externalCombo->clearEdit();

  sendCommand("definePhrase(QString)",query);

  if (orientation() == Vertical)
    popupBox->hide();
}


void DictApplet::comboTextChanged(const QString &s)
{
  defineBtn->setEnabled(!s.isEmpty());
  matchBtn->setEnabled(!s.isEmpty());
}


void DictApplet::queryClipboard()
{
  sendCommand("defineClipboardContent()",QString::null);
}


void DictApplet::startDefine()
{
  startQuery(internalCombo->currentText());
}


void DictApplet::startMatch()
{
  QString query = internalCombo->currentText().stripWhiteSpace();
  internalCombo->addToHistory(query);
  externalCombo->addToHistory(query);
  internalCombo->clearEdit();
  externalCombo->clearEdit();

  sendCommand("matchPhrase(QString)",query);
}


void DictApplet::showExternalCombo()
{
  QPoint p;
  if (position() == pLeft)
    p = mapToGlobal(QPoint(-popupBox->width()-1, 0));
  else
    p = mapToGlobal(QPoint(width()+1, 0));
  popupBox->move(p);
  if (popupBox->showBox())
    externalCombo->setFocus();
  else
    verticalBtn->setDown(false);
}


void DictApplet::externalComboHidden()
{
  verticalBtn->setDown(false);
}

void DictApplet::updateCompletionMode(KGlobalSettings::Completion mode)
{
  internalCombo->setCompletionMode(mode);
  externalCombo->setCompletionMode(mode);
}

//--------------------------------

#include "kdictapplet.moc"