less Q3 (remove a lot of unused Q3 headers, port small things)
[kdenetwork.git] / kdict / matchview.cpp
blob16d6759ad2fb1f27aba050fa679f165a58a030d6
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(kapp->makeStdCaption(i18n("Match List")));
121 QVBoxLayout * boxLayout = new QVBoxLayout(this, 1, 0);
123 boxLayout->addSpacing(1);
124 w_strat = new QComboBox(false,this);
125 w_strat->setFixedHeight(w_strat->sizeHint().height());
126 connect(w_strat,SIGNAL(activated(int)),this,SLOT(strategySelected(int)));
127 boxLayout->addWidget(w_strat,0);
128 boxLayout->addSpacing(1);
130 w_list = new Q3ListView(this);
131 w_list->setFocusPolicy(Qt::StrongFocus);
132 w_list->header()->hide();
133 w_list->addColumn("foo");
134 w_list->setColumnWidthMode(0,Q3ListView::Maximum);
135 w_list->setColumnWidth(0,0);
136 w_list->setSelectionMode(Q3ListView::Extended);
137 w_list->setTreeStepSize(18);
138 w_list->setSorting(-1); // disable sorting
139 w_list->setMinimumHeight(w_strat->sizeHint().height());
140 connect(w_list,SIGNAL(selectionChanged()),SLOT(enableGetButton()));
141 connect(w_list,SIGNAL(returnPressed(Q3ListViewItem *)),SLOT(returnPressed(Q3ListViewItem *)));
142 connect(w_list,SIGNAL(doubleClicked(Q3ListViewItem *)),SLOT(getOneItem(Q3ListViewItem *)));
143 connect(w_list,SIGNAL(mouseButtonPressed(int, Q3ListViewItem *, const QPoint &, int)),
144 SLOT(mouseButtonPressed(int, Q3ListViewItem *, const QPoint &, int)));
145 connect(w_list,SIGNAL(rightButtonPressed(Q3ListViewItem *,const QPoint &,int)),SLOT(buildPopupMenu(Q3ListViewItem *,const QPoint &,int)));
146 boxLayout->addWidget(w_list,1);
148 boxLayout->addSpacing(1);
149 w_get = new QPushButton(i18n("&Get Selected"),this);
150 w_get->setFixedHeight(w_get->sizeHint().height()-3);
151 w_get->setMinimumWidth(w_get->sizeHint().width()-20);
152 w_get->setEnabled(false);
153 connect(w_get, SIGNAL(clicked()), this, SLOT(getSelected()));
154 boxLayout->addWidget(w_get,0);
156 w_getAll = new QPushButton(i18n("Get &All"),this);
157 w_getAll->setFixedHeight(w_getAll->sizeHint().height()-3);
158 w_getAll->setMinimumWidth(w_getAll->sizeHint().width()-20);
159 w_getAll->setEnabled(false);
160 connect(w_getAll, SIGNAL(clicked()), this, SLOT(getAll()));
161 boxLayout->addWidget(w_getAll,0);
162 connect(interface,SIGNAL(matchReady(const QStringList &)),this,SLOT(newList(const QStringList &)));
163 rightBtnMenu = new KMenu();
167 MatchView::~MatchView()
172 void MatchView::updateStrategyCombo()
174 w_strat->clear();
175 w_strat->insertStringList(global->strategies);
176 w_strat->setCurrentItem(global->currentStrategy);
180 bool MatchView::selectStrategy(const QString &strategy) const
182 int newCurrent = global->strategies.findIndex(strategy);
183 if (newCurrent == -1)
184 return false;
185 else {
186 global->currentStrategy = newCurrent;
187 w_strat->setCurrentItem(global->currentStrategy);
188 return true;
193 void MatchView::match(const QString &query)
195 interface->match(query.utf8());
199 void MatchView::closeEvent ( QCloseEvent * e )
201 e->accept(); // hides the widget
202 emit(windowClosed());
206 void MatchView::strategySelected(int num)
208 global->currentStrategy = num;
212 void MatchView::enableGetButton()
214 if (w_getAll->isEnabled()) {
215 w_get->setEnabled(true);
216 getOn = true;
221 void MatchView::mouseButtonPressed(int button, Q3ListViewItem *, const QPoint &, int)
223 if (button == Qt::MidButton)
224 emit(clipboardRequested());
228 void MatchView::returnPressed(Q3ListViewItem *)
230 getSelected();
234 void MatchView::getOneItem(Q3ListViewItem *i)
236 QStringList defines;
238 if ((!i->childCount())&&(i->parent()))
239 defines.append(((MatchViewItem *)(i))->command);
240 else {
241 i = i->firstChild();
242 while (i) {
243 defines.append(((MatchViewItem *)(i))->command);
244 i = i->nextSibling();
248 doGet(defines);
252 void MatchView::getSelected()
254 QStringList defines;
255 MatchViewItem *top = static_cast<MatchViewItem*>(w_list->firstChild());
256 MatchViewItem *sub;
258 while (top) {
259 if (top->isSelected()&&(!top->subEntrys.isEmpty())) {
260 QString command;
261 QStringList::iterator it;
262 for (it = top->subEntrys.begin(); it != top->subEntrys.end(); ++it) {
263 command = "define ";
264 command += (*it);
265 command += "\r\n";
266 defines.append(command);
268 } else {
269 sub = static_cast<MatchViewItem*>(top->firstChild());
270 while (sub) {
271 if (top->isSelected()||sub->isSelected())
272 defines.append(sub->command);
273 sub = static_cast<MatchViewItem*>(sub->nextSibling());
276 top = static_cast<MatchViewItem*>(top->nextSibling());
278 doGet(defines);
282 void MatchView::getAll()
284 QStringList defines;
285 MatchViewItem *top = static_cast<MatchViewItem*>(w_list->firstChild());
286 MatchViewItem *sub;
288 while (top) {
289 if (!top->subEntrys.isEmpty()) {
290 QString command;
291 QStringList::iterator it;
292 for (it = top->subEntrys.begin(); it != top->subEntrys.end(); ++it) {
293 command = "define ";
294 command += (*it);
295 command += "\r\n";
296 defines.append(command);
298 } else {
299 sub = static_cast<MatchViewItem*>(top->firstChild());
300 while (sub) {
301 defines.append(sub->command);
302 sub = static_cast<MatchViewItem*>(sub->nextSibling());
305 top = static_cast<MatchViewItem*>(top->nextSibling());
307 doGet(defines);
311 void MatchView::doGet(QStringList &defines)
313 if (defines.count() > 0) {
314 if (defines.count() > global->maxDefinitions) {
315 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.")
316 .arg(defines.count()).arg(global->maxDefinitions));
317 while (defines.count()>global->maxDefinitions)
318 defines.pop_back();
320 interface->getDefinitions(defines);
325 void MatchView::newList(const QStringList &matches)
327 MatchViewItem *top=0;
328 bool initialOpen = (matches.count()<200);
329 int numDb = 0;
331 rightBtnMenu->hide();
332 w_list->clear();
333 w_list->setColumnWidth(0,0);
334 w_list->setUpdatesEnabled(false);
335 w_get->setEnabled(false);
336 getOn = false;
338 if (matches.isEmpty()) {
339 w_list->setColumnWidth(0,w_get->width()-5);
340 w_list->setRootIsDecorated(false);
341 w_getAll->setEnabled(false);
342 getAllOn = false;
343 top = new MatchViewItem(w_list,top,i18n(" No Hits"));
344 } else {
345 w_list->setRootIsDecorated(true);
346 w_getAll->setEnabled(true);
347 getAllOn = true;
348 QString lastDb, db, match;
350 QStringList::const_iterator it;
351 for (it = matches.begin(); it != matches.end(); ++it) {
352 db = (*it).section(' ', 0, 0);
354 if (db != lastDb) {
355 numDb++;
356 if (top) {
357 top->setOpen(initialOpen);
358 top = new MatchViewItem(w_list, top, db);
359 } else
360 top = new MatchViewItem(w_list, db);
361 top->setExpandable(true);
362 lastDb = db;
365 if (top)
366 top->subEntrys.append(*it);
369 if ((numDb == 1)||(initialOpen))
370 top->setOpen(true);
373 w_list->setUpdatesEnabled(true);
374 w_list->repaint();
375 w_list->setFocus();
379 // construct the right-mouse-button-popup-menu on demand
380 void MatchView::buildPopupMenu(Q3ListViewItem *i, const QPoint &_point, int)
382 rightBtnMenu->clear();
384 if ((i!=0L)&&(i->isExpandable()||i->parent())) {
385 popupCurrent = (MatchViewItem *)(i);
386 rightBtnMenu->insertItem(i18n("&Get"),this,SLOT(popupGetCurrent()));
387 if (!i->isExpandable()) { // toplevel item -> only "get"
388 rightBtnMenu->insertItem(i18n("&Match"),this,SLOT(popupMatchCurrent()));
389 rightBtnMenu->insertItem(i18n("&Define"),this,SLOT(popupDefineCurrent()));
391 rightBtnMenu->insertSeparator();
394 QString text = kapp->clipboard()->text(QClipboard::Clipboard);
395 if (text.isEmpty()) {
396 text = kapp->clipboard()->text(QClipboard::Selection);
398 if (!text.isEmpty()) {
399 popupClip = kapp->clipboard()->text();
400 rightBtnMenu->insertItem(i18n("Match &Clipboard Content"),this,SLOT(popupMatchClip()));
401 rightBtnMenu->insertItem(SmallIcon("define_clip"),i18n("D&efine Clipboard Content"),this,SLOT(popupDefineClip()));
402 rightBtnMenu->insertSeparator();
405 int ID = rightBtnMenu->insertItem(i18n("Get &Selected"),this,SLOT(getSelected()));
406 rightBtnMenu->setItemEnabled(ID,getOn);
407 ID = rightBtnMenu->insertItem(i18n("Get &All"),this,SLOT(getAll()));
408 rightBtnMenu->setItemEnabled(ID,getAllOn);
410 if (w_list->childCount()) {
411 rightBtnMenu->insertSeparator();
412 rightBtnMenu->insertItem(i18n("E&xpand List"),this,SLOT(expandList()));
413 rightBtnMenu->insertItem(i18n("C&ollapse List"),this,SLOT(collapseList()));
416 rightBtnMenu->popup(_point);
420 void MatchView::popupGetCurrent()
422 getOneItem(popupCurrent);
426 void MatchView::popupDefineCurrent()
428 emit(defineRequested(popupCurrent->text(0)));
432 void MatchView::popupMatchCurrent()
434 emit(matchRequested(popupCurrent->text(0)));
438 void MatchView::popupDefineClip()
440 emit(defineRequested(popupClip));
444 void MatchView::popupMatchClip()
446 emit(matchRequested(popupClip));
450 void MatchView::expandList()
452 Q3ListViewItem *top = w_list->firstChild();
454 while (top) {
455 w_list->setOpen(top,true);
456 top = top->nextSibling();
461 void MatchView::collapseList()
463 w_list->setCurrentItem(w_list->firstChild());
464 Q3ListViewItem *top = w_list->firstChild();
466 while (top) {
467 w_list->setOpen(top,false);
468 top = top->nextSibling();
472 //--------------------------------
474 #include "matchview.moc"