Work done by Alessandro Praduroux and me:
[kdenetwork.git] / kdict / matchview.cpp
blobd3efcde2fe61406a6a314429b7ee7db6d50130c5
1 /* -------------------------------------------------------------
3 matchview.cpp (part of The KDE Dictionary Client)
5 Copyright (C) 2000-2001 Christian Gebauer <gebauer@kde.org>
7 This file is distributed under the Artistic License.
8 See LICENSE for details.
10 -------------------------------------------------------------
12 MatchView This widget contains the list of matching definitions
14 ------------------------------------------------------------- */
16 #include <qclipboard.h>
17 #include <qcombobox.h>
18 #include <qpushbutton.h>
19 #include <q3header.h>
20 #include <qlayout.h>
21 #include <qpainter.h>
22 #include <qregexp.h>
23 //Added by qt3to4:
24 #include <QVBoxLayout>
25 #include <QCloseEvent>
27 #include <kmenu.h>
28 #include <klocale.h>
29 #include <kapplication.h>
30 #include <kiconloader.h>
31 #include <kmessagebox.h>
33 #include "dict.h"
34 #include "options.h"
35 #include "matchview.h"
38 //********* MatchViewItem ********************************************
41 MatchViewItem::MatchViewItem(Q3ListView *view, const QString &text)
42 : Q3ListViewItem(view,text)
47 MatchViewItem::MatchViewItem(Q3ListView *view,Q3ListViewItem *after,const QString &text)
48 : Q3ListViewItem(view,after,text)
53 MatchViewItem::MatchViewItem(Q3ListViewItem *item,const QString &text,const QString &commandStr)
54 : Q3ListViewItem(item,text), command(commandStr)
59 MatchViewItem::MatchViewItem(Q3ListViewItem *item,Q3ListViewItem *after,const QString &text,const QString &commandStr)
60 : Q3ListViewItem(item,after,text), command(commandStr)
65 MatchViewItem::~MatchViewItem()
70 void MatchViewItem::setOpen(bool o)
72 if (o && !childCount()) {
73 listView()->setUpdatesEnabled(false);
75 MatchViewItem *sub=0;
76 QString command, label;
77 QRegExp exp("\"*\"", true, true);
78 QStringList::iterator it;
79 for (it = subEntrys.begin(); it != subEntrys.end(); ++it) {
80 command = "define ";
81 command += (*it);
82 command += "\r\n";
83 exp.search((*it));
84 label = exp.cap();
85 label = label.mid(1, label.length()-2); // remove quotes
86 if (sub)
87 sub = new MatchViewItem(this, sub, label, command);
88 else
89 sub = new MatchViewItem(this, label, command);
92 subEntrys.clear();
94 listView()->setUpdatesEnabled(true);
97 if (childCount())
98 Q3ListViewItem::setOpen(o);
102 void MatchViewItem::paintCell(QPainter *p, const QColorGroup &cg, int column, int width, int alignment)
104 if(command.isEmpty()) {
105 QFont font=p->font();
106 font.setBold(true);
107 p->setFont(font);
109 Q3ListViewItem::paintCell(p,cg,column,width,alignment);
113 //********* MatchView ******************************************
116 MatchView::MatchView(QWidget *parent, const char *name)
117 : QWidget(parent,name),getOn(false),getAllOn(false)
119 setCaption(KDialog::makeStandardCaption(i18n("Match List"), this));
121 QVBoxLayout * boxLayout = new QVBoxLayout(this);
122 boxLayout->setSpacing(0);
123 boxLayout->setMargin(1);
125 boxLayout->addSpacing(1);
126 w_strat = new QComboBox(false,this);
127 w_strat->setFixedHeight(w_strat->sizeHint().height());
128 connect(w_strat,SIGNAL(activated(int)),this,SLOT(strategySelected(int)));
129 boxLayout->addWidget(w_strat,0);
130 boxLayout->addSpacing(1);
132 w_list = new Q3ListView(this);
133 w_list->setFocusPolicy(Qt::StrongFocus);
134 w_list->header()->hide();
135 w_list->addColumn("foo");
136 w_list->setColumnWidthMode(0,Q3ListView::Maximum);
137 w_list->setColumnWidth(0,0);
138 w_list->setSelectionMode(Q3ListView::Extended);
139 w_list->setTreeStepSize(18);
140 w_list->setSorting(-1); // disable sorting
141 w_list->setMinimumHeight(w_strat->sizeHint().height());
142 connect(w_list,SIGNAL(selectionChanged()),SLOT(enableGetButton()));
143 connect(w_list,SIGNAL(returnPressed(Q3ListViewItem *)),SLOT(returnPressed(Q3ListViewItem *)));
144 connect(w_list,SIGNAL(doubleClicked(Q3ListViewItem *)),SLOT(getOneItem(Q3ListViewItem *)));
145 connect(w_list,SIGNAL(mouseButtonPressed(int, Q3ListViewItem *, const QPoint &, int)),
146 SLOT(mouseButtonPressed(int, Q3ListViewItem *, const QPoint &, int)));
147 connect(w_list,SIGNAL(rightButtonPressed(Q3ListViewItem *,const QPoint &,int)),SLOT(buildPopupMenu(Q3ListViewItem *,const QPoint &,int)));
148 boxLayout->addWidget(w_list,1);
150 boxLayout->addSpacing(1);
151 w_get = new QPushButton(i18n("&Get Selected"),this);
152 w_get->setFixedHeight(w_get->sizeHint().height()-3);
153 w_get->setMinimumWidth(w_get->sizeHint().width()-20);
154 w_get->setEnabled(false);
155 connect(w_get, SIGNAL(clicked()), this, SLOT(getSelected()));
156 boxLayout->addWidget(w_get,0);
158 w_getAll = new QPushButton(i18n("Get &All"),this);
159 w_getAll->setFixedHeight(w_getAll->sizeHint().height()-3);
160 w_getAll->setMinimumWidth(w_getAll->sizeHint().width()-20);
161 w_getAll->setEnabled(false);
162 connect(w_getAll, SIGNAL(clicked()), this, SLOT(getAll()));
163 boxLayout->addWidget(w_getAll,0);
164 connect(interface,SIGNAL(matchReady(const QStringList &)),this,SLOT(newList(const QStringList &)));
165 rightBtnMenu = new KMenu();
169 MatchView::~MatchView()
174 void MatchView::updateStrategyCombo()
176 w_strat->clear();
177 w_strat->insertStringList(global->strategies);
178 w_strat->setCurrentItem(global->currentStrategy);
182 bool MatchView::selectStrategy(const QString &strategy) const
184 int newCurrent = global->strategies.findIndex(strategy);
185 if (newCurrent == -1)
186 return false;
187 else {
188 global->currentStrategy = newCurrent;
189 w_strat->setCurrentItem(global->currentStrategy);
190 return true;
195 void MatchView::match(const QString &query)
197 interface->match(query.utf8());
201 void MatchView::closeEvent ( QCloseEvent * e )
203 e->accept(); // hides the widget
204 emit(windowClosed());
208 void MatchView::strategySelected(int num)
210 global->currentStrategy = num;
214 void MatchView::enableGetButton()
216 if (w_getAll->isEnabled()) {
217 w_get->setEnabled(true);
218 getOn = true;
223 void MatchView::mouseButtonPressed(int button, Q3ListViewItem *, const QPoint &, int)
225 if (button == Qt::MidButton)
226 emit(clipboardRequested());
230 void MatchView::returnPressed(Q3ListViewItem *)
232 getSelected();
236 void MatchView::getOneItem(Q3ListViewItem *i)
238 QStringList defines;
240 if ((!i->childCount())&&(i->parent()))
241 defines.append(((MatchViewItem *)(i))->command);
242 else {
243 i = i->firstChild();
244 while (i) {
245 defines.append(((MatchViewItem *)(i))->command);
246 i = i->nextSibling();
250 doGet(defines);
254 void MatchView::getSelected()
256 QStringList defines;
257 MatchViewItem *top = static_cast<MatchViewItem*>(w_list->firstChild());
258 MatchViewItem *sub;
260 while (top) {
261 if (top->isSelected()&&(!top->subEntrys.isEmpty())) {
262 QString command;
263 QStringList::iterator it;
264 for (it = top->subEntrys.begin(); it != top->subEntrys.end(); ++it) {
265 command = "define ";
266 command += (*it);
267 command += "\r\n";
268 defines.append(command);
270 } else {
271 sub = static_cast<MatchViewItem*>(top->firstChild());
272 while (sub) {
273 if (top->isSelected()||sub->isSelected())
274 defines.append(sub->command);
275 sub = static_cast<MatchViewItem*>(sub->nextSibling());
278 top = static_cast<MatchViewItem*>(top->nextSibling());
280 doGet(defines);
284 void MatchView::getAll()
286 QStringList defines;
287 MatchViewItem *top = static_cast<MatchViewItem*>(w_list->firstChild());
288 MatchViewItem *sub;
290 while (top) {
291 if (!top->subEntrys.isEmpty()) {
292 QString command;
293 QStringList::iterator it;
294 for (it = top->subEntrys.begin(); it != top->subEntrys.end(); ++it) {
295 command = "define ";
296 command += (*it);
297 command += "\r\n";
298 defines.append(command);
300 } else {
301 sub = static_cast<MatchViewItem*>(top->firstChild());
302 while (sub) {
303 defines.append(sub->command);
304 sub = static_cast<MatchViewItem*>(sub->nextSibling());
307 top = static_cast<MatchViewItem*>(top->nextSibling());
309 doGet(defines);
313 void MatchView::doGet(QStringList &defines)
315 if (defines.count() > 0) {
316 if (defines.count() > global->maxDefinitions) {
317 KMessageBox::sorry(global->topLevel,i18n("You have selected %1 definitions,\nbut Kdict will fetch only the first %2 definitions.\nYou can modify this limit in the Preferences Dialog.",
318 defines.count(), global->maxDefinitions));
319 while (defines.count()>global->maxDefinitions)
320 defines.pop_back();
322 interface->getDefinitions(defines);
327 void MatchView::newList(const QStringList &matches)
329 MatchViewItem *top=0;
330 bool initialOpen = (matches.count()<200);
331 int numDb = 0;
333 rightBtnMenu->hide();
334 w_list->clear();
335 w_list->setColumnWidth(0,0);
336 w_list->setUpdatesEnabled(false);
337 w_get->setEnabled(false);
338 getOn = false;
340 if (matches.isEmpty()) {
341 w_list->setColumnWidth(0,w_get->width()-5);
342 w_list->setRootIsDecorated(false);
343 w_getAll->setEnabled(false);
344 getAllOn = false;
345 top = new MatchViewItem(w_list,top,i18n(" No Hits"));
346 } else {
347 w_list->setRootIsDecorated(true);
348 w_getAll->setEnabled(true);
349 getAllOn = true;
350 QString lastDb, db, match;
352 QStringList::const_iterator it;
353 for (it = matches.begin(); it != matches.end(); ++it) {
354 db = (*it).section(' ', 0, 0);
356 if (db != lastDb) {
357 numDb++;
358 if (top) {
359 top->setOpen(initialOpen);
360 top = new MatchViewItem(w_list, top, db);
361 } else
362 top = new MatchViewItem(w_list, db);
363 top->setExpandable(true);
364 lastDb = db;
367 if (top)
368 top->subEntrys.append(*it);
371 if ((numDb == 1)||(initialOpen))
372 top->setOpen(true);
375 w_list->setUpdatesEnabled(true);
376 w_list->repaint();
377 w_list->setFocus();
381 // construct the right-mouse-button-popup-menu on demand
382 void MatchView::buildPopupMenu(Q3ListViewItem *i, const QPoint &_point, int)
384 rightBtnMenu->clear();
386 if ((i!=0L)&&(i->isExpandable()||i->parent())) {
387 popupCurrent = (MatchViewItem *)(i);
388 rightBtnMenu->insertItem(i18n("&Get"),this,SLOT(popupGetCurrent()));
389 if (!i->isExpandable()) { // toplevel item -> only "get"
390 rightBtnMenu->insertItem(i18n("&Match"),this,SLOT(popupMatchCurrent()));
391 rightBtnMenu->insertItem(i18n("&Define"),this,SLOT(popupDefineCurrent()));
393 rightBtnMenu->insertSeparator();
396 QString text = kapp->clipboard()->text(QClipboard::Clipboard);
397 if (text.isEmpty()) {
398 text = kapp->clipboard()->text(QClipboard::Selection);
400 if (!text.isEmpty()) {
401 popupClip = kapp->clipboard()->text();
402 rightBtnMenu->insertItem(i18n("Match &Clipboard Content"),this,SLOT(popupMatchClip()));
403 rightBtnMenu->insertItem(SmallIcon("define_clip"),i18n("D&efine Clipboard Content"),this,SLOT(popupDefineClip()));
404 rightBtnMenu->insertSeparator();
407 int ID = rightBtnMenu->insertItem(i18n("Get &Selected"),this,SLOT(getSelected()));
408 rightBtnMenu->setItemEnabled(ID,getOn);
409 ID = rightBtnMenu->insertItem(i18n("Get &All"),this,SLOT(getAll()));
410 rightBtnMenu->setItemEnabled(ID,getAllOn);
412 if (w_list->childCount()) {
413 rightBtnMenu->insertSeparator();
414 rightBtnMenu->insertItem(i18n("E&xpand List"),this,SLOT(expandList()));
415 rightBtnMenu->insertItem(i18n("C&ollapse List"),this,SLOT(collapseList()));
418 rightBtnMenu->popup(_point);
422 void MatchView::popupGetCurrent()
424 getOneItem(popupCurrent);
428 void MatchView::popupDefineCurrent()
430 emit(defineRequested(popupCurrent->text(0)));
434 void MatchView::popupMatchCurrent()
436 emit(matchRequested(popupCurrent->text(0)));
440 void MatchView::popupDefineClip()
442 emit(defineRequested(popupClip));
446 void MatchView::popupMatchClip()
448 emit(matchRequested(popupClip));
452 void MatchView::expandList()
454 Q3ListViewItem *top = w_list->firstChild();
456 while (top) {
457 w_list->setOpen(top,true);
458 top = top->nextSibling();
463 void MatchView::collapseList()
465 w_list->setCurrentItem(w_list->firstChild());
466 Q3ListViewItem *top = w_list->firstChild();
468 while (top) {
469 w_list->setOpen(top,false);
470 top = top->nextSibling();
474 //--------------------------------
476 #include "matchview.moc"