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>
24 #include <kpopupmenu.h>
26 #include <kapplication.h>
27 #include <kiconloader.h>
28 #include <kmessagebox.h>
32 #include "matchview.h"
35 //********* MatchViewItem ********************************************
38 MatchViewItem::MatchViewItem(QListView
*view
, const QString
&text
)
39 : QListViewItem(view
,text
)
44 MatchViewItem::MatchViewItem(QListView
*view
,QListViewItem
*after
,const QString
&text
)
45 : QListViewItem(view
,after
,text
)
50 MatchViewItem::MatchViewItem(QListViewItem
*item
,const QString
&text
,const QString
&commandStr
)
51 : QListViewItem(item
,text
), command(commandStr
)
56 MatchViewItem::MatchViewItem(QListViewItem
*item
,QListViewItem
*after
,const QString
&text
,const QString
&commandStr
)
57 : QListViewItem(item
,after
,text
), command(commandStr
)
62 MatchViewItem::~MatchViewItem()
67 void MatchViewItem::setOpen(bool o
)
69 if (o
&& !childCount()) {
70 listView()->setUpdatesEnabled(false);
73 QString command
, label
;
74 QRegExp
exp("\"*\"", true, true);
75 QStringList::iterator it
;
76 for (it
= subEntrys
.begin(); it
!= subEntrys
.end(); ++it
) {
82 label
= label
.mid(1, label
.length()-2); // remove quotes
84 sub
= new MatchViewItem(this, sub
, label
, command
);
86 sub
= new MatchViewItem(this, label
, command
);
91 listView()->setUpdatesEnabled(true);
95 QListViewItem::setOpen(o
);
99 void MatchViewItem::paintCell(QPainter
*p
, const QColorGroup
&cg
, int column
, int width
, int alignment
)
101 if(command
.isEmpty()) {
102 QFont font
=p
->font();
106 QListViewItem::paintCell(p
,cg
,column
,width
,alignment
);
110 //********* MatchView ******************************************
113 MatchView::MatchView(QWidget
*parent
, const char *name
)
114 : QWidget(parent
,name
),getOn(false),getAllOn(false)
116 setCaption(kapp
->makeStdCaption(i18n("Match List")));
118 QVBoxLayout
* boxLayout
= new QVBoxLayout(this, 1, 0);
120 boxLayout
->addSpacing(1);
121 w_strat
= new QComboBox(false,this);
122 w_strat
->setFixedHeight(w_strat
->sizeHint().height());
123 connect(w_strat
,SIGNAL(activated(int)),this,SLOT(strategySelected(int)));
124 boxLayout
->addWidget(w_strat
,0);
125 boxLayout
->addSpacing(1);
127 w_list
= new QListView(this);
128 w_list
->setFocusPolicy(QWidget::StrongFocus
);
129 w_list
->header()->hide();
130 w_list
->addColumn("foo");
131 w_list
->setColumnWidthMode(0,QListView::Maximum
);
132 w_list
->setColumnWidth(0,0);
133 w_list
->setSelectionMode(QListView::Extended
);
134 w_list
->setTreeStepSize(18);
135 w_list
->setSorting(-1); // disable sorting
136 w_list
->setMinimumHeight(w_strat
->sizeHint().height());
137 connect(w_list
,SIGNAL(selectionChanged()),SLOT(enableGetButton()));
138 connect(w_list
,SIGNAL(returnPressed(QListViewItem
*)),SLOT(returnPressed(QListViewItem
*)));
139 connect(w_list
,SIGNAL(doubleClicked(QListViewItem
*)),SLOT(getOneItem(QListViewItem
*)));
140 connect(w_list
,SIGNAL(mouseButtonPressed(int, QListViewItem
*, const QPoint
&, int)),
141 SLOT(mouseButtonPressed(int, QListViewItem
*, const QPoint
&, int)));
142 connect(w_list
,SIGNAL(rightButtonPressed(QListViewItem
*,const QPoint
&,int)),SLOT(buildPopupMenu(QListViewItem
*,const QPoint
&,int)));
143 boxLayout
->addWidget(w_list
,1);
145 boxLayout
->addSpacing(1);
146 w_get
= new QPushButton(i18n("&Get Selected"),this);
147 w_get
->setFixedHeight(w_get
->sizeHint().height()-3);
148 w_get
->setMinimumWidth(w_get
->sizeHint().width()-20);
149 w_get
->setEnabled(false);
150 connect(w_get
, SIGNAL(clicked()), this, SLOT(getSelected()));
151 boxLayout
->addWidget(w_get
,0);
153 w_getAll
= new QPushButton(i18n("Get &All"),this);
154 w_getAll
->setFixedHeight(w_getAll
->sizeHint().height()-3);
155 w_getAll
->setMinimumWidth(w_getAll
->sizeHint().width()-20);
156 w_getAll
->setEnabled(false);
157 connect(w_getAll
, SIGNAL(clicked()), this, SLOT(getAll()));
158 boxLayout
->addWidget(w_getAll
,0);
159 connect(interface
,SIGNAL(matchReady(const QStringList
&)),this,SLOT(newList(const QStringList
&)));
160 rightBtnMenu
= new KPopupMenu();
164 MatchView::~MatchView()
169 void MatchView::updateStrategyCombo()
172 w_strat
->insertStringList(global
->strategies
);
173 w_strat
->setCurrentItem(global
->currentStrategy
);
177 bool MatchView::selectStrategy(const QString
&strategy
) const
179 int newCurrent
= global
->strategies
.findIndex(strategy
);
180 if (newCurrent
== -1)
183 global
->currentStrategy
= newCurrent
;
184 w_strat
->setCurrentItem(global
->currentStrategy
);
190 void MatchView::match(const QString
&query
)
192 interface
->match(query
.utf8());
196 void MatchView::closeEvent ( QCloseEvent
* e
)
198 e
->accept(); // hides the widget
199 emit(windowClosed());
203 void MatchView::strategySelected(int num
)
205 global
->currentStrategy
= num
;
209 void MatchView::enableGetButton()
211 if (w_getAll
->isEnabled()) {
212 w_get
->setEnabled(true);
218 void MatchView::mouseButtonPressed(int button
, QListViewItem
*, const QPoint
&, int)
220 if (button
== MidButton
)
221 emit(clipboardRequested());
225 void MatchView::returnPressed(QListViewItem
*)
231 void MatchView::getOneItem(QListViewItem
*i
)
235 if ((!i
->childCount())&&(i
->parent()))
236 defines
.append(((MatchViewItem
*)(i
))->command
);
240 defines
.append(((MatchViewItem
*)(i
))->command
);
241 i
= i
->nextSibling();
249 void MatchView::getSelected()
252 MatchViewItem
*top
= static_cast<MatchViewItem
*>(w_list
->firstChild());
256 if (top
->isSelected()&&(!top
->subEntrys
.isEmpty())) {
258 QStringList::iterator it
;
259 for (it
= top
->subEntrys
.begin(); it
!= top
->subEntrys
.end(); ++it
) {
263 defines
.append(command
);
266 sub
= static_cast<MatchViewItem
*>(top
->firstChild());
268 if (top
->isSelected()||sub
->isSelected())
269 defines
.append(sub
->command
);
270 sub
= static_cast<MatchViewItem
*>(sub
->nextSibling());
273 top
= static_cast<MatchViewItem
*>(top
->nextSibling());
279 void MatchView::getAll()
282 MatchViewItem
*top
= static_cast<MatchViewItem
*>(w_list
->firstChild());
286 if (!top
->subEntrys
.isEmpty()) {
288 QStringList::iterator it
;
289 for (it
= top
->subEntrys
.begin(); it
!= top
->subEntrys
.end(); ++it
) {
293 defines
.append(command
);
296 sub
= static_cast<MatchViewItem
*>(top
->firstChild());
298 defines
.append(sub
->command
);
299 sub
= static_cast<MatchViewItem
*>(sub
->nextSibling());
302 top
= static_cast<MatchViewItem
*>(top
->nextSibling());
308 void MatchView::doGet(QStringList
&defines
)
310 if (defines
.count() > 0) {
311 if (defines
.count() > global
->maxDefinitions
) {
312 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.")
313 .arg(defines
.count()).arg(global
->maxDefinitions
));
314 while (defines
.count()>global
->maxDefinitions
)
317 interface
->getDefinitions(defines
);
322 void MatchView::newList(const QStringList
&matches
)
324 MatchViewItem
*top
=0;
325 bool initialOpen
= (matches
.count()<200);
328 rightBtnMenu
->hide();
330 w_list
->setColumnWidth(0,0);
331 w_list
->setUpdatesEnabled(false);
332 w_get
->setEnabled(false);
335 if (matches
.isEmpty()) {
336 w_list
->setColumnWidth(0,w_get
->width()-5);
337 w_list
->setRootIsDecorated(false);
338 w_getAll
->setEnabled(false);
340 top
= new MatchViewItem(w_list
,top
,i18n(" No Hits"));
342 w_list
->setRootIsDecorated(true);
343 w_getAll
->setEnabled(true);
345 QString lastDb
, db
, match
;
347 QStringList::const_iterator it
;
348 for (it
= matches
.begin(); it
!= matches
.end(); ++it
) {
349 db
= (*it
).section(' ', 0, 0);
354 top
->setOpen(initialOpen
);
355 top
= new MatchViewItem(w_list
, top
, db
);
357 top
= new MatchViewItem(w_list
, db
);
358 top
->setExpandable(true);
363 top
->subEntrys
.append(*it
);
366 if ((numDb
== 1)||(initialOpen
))
370 w_list
->setUpdatesEnabled(true);
376 // construct the right-mouse-button-popup-menu on demand
377 void MatchView::buildPopupMenu(QListViewItem
*i
, const QPoint
&_point
, int)
379 rightBtnMenu
->clear();
381 if ((i
!=0L)&&(i
->isExpandable()||i
->parent())) {
382 popupCurrent
= (MatchViewItem
*)(i
);
383 rightBtnMenu
->insertItem(i18n("&Get"),this,SLOT(popupGetCurrent()));
384 if (!i
->isExpandable()) { // toplevel item -> only "get"
385 rightBtnMenu
->insertItem(i18n("&Match"),this,SLOT(popupMatchCurrent()));
386 rightBtnMenu
->insertItem(i18n("&Define"),this,SLOT(popupDefineCurrent()));
388 rightBtnMenu
->insertSeparator();
391 kapp
->clipboard()->setSelectionMode(false);
392 QString text
= kapp
->clipboard()->text();
393 if (text
.isEmpty()) {
394 kapp
->clipboard()->setSelectionMode(true);
395 text
= kapp
->clipboard()->text();
397 if (!text
.isEmpty()) {
398 popupClip
= kapp
->clipboard()->text();
399 rightBtnMenu
->insertItem(i18n("Match &Clipboard Content"),this,SLOT(popupMatchClip()));
400 rightBtnMenu
->insertItem(SmallIcon("define_clip"),i18n("D&efine Clipboard Content"),this,SLOT(popupDefineClip()));
401 rightBtnMenu
->insertSeparator();
404 int ID
= rightBtnMenu
->insertItem(i18n("Get &Selected"),this,SLOT(getSelected()));
405 rightBtnMenu
->setItemEnabled(ID
,getOn
);
406 ID
= rightBtnMenu
->insertItem(i18n("Get &All"),this,SLOT(getAll()));
407 rightBtnMenu
->setItemEnabled(ID
,getAllOn
);
409 if (w_list
->childCount()) {
410 rightBtnMenu
->insertSeparator();
411 rightBtnMenu
->insertItem(i18n("E&xpand List"),this,SLOT(expandList()));
412 rightBtnMenu
->insertItem(i18n("C&ollapse List"),this,SLOT(collapseList()));
415 rightBtnMenu
->popup(_point
);
419 void MatchView::popupGetCurrent()
421 getOneItem(popupCurrent
);
425 void MatchView::popupDefineCurrent()
427 emit(defineRequested(popupCurrent
->text(0)));
431 void MatchView::popupMatchCurrent()
433 emit(matchRequested(popupCurrent
->text(0)));
437 void MatchView::popupDefineClip()
439 emit(defineRequested(popupClip
));
443 void MatchView::popupMatchClip()
445 emit(matchRequested(popupClip
));
449 void MatchView::expandList()
451 QListViewItem
*top
= w_list
->firstChild();
454 w_list
->setOpen(top
,true);
455 top
= top
->nextSibling();
460 void MatchView::collapseList()
462 w_list
->setCurrentItem(w_list
->firstChild());
463 QListViewItem
*top
= w_list
->firstChild();
466 w_list
->setOpen(top
,false);
467 top
= top
->nextSibling();
471 //--------------------------------
473 #include "matchview.moc"