SVN_SILENT made messages (.desktop file)
[kdeadmin.git] / kpackage / kplview.cpp
blobf486821887e3f6ad768c1845fa56d348e9876535
1 /*
2 ** Copyright (C) 1999,2000 Toivo Pedaste <toivo@ucs.uwa.edu.au>
3 **
4 */
6 /*
7 ** This program is free software; you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License as published by
9 ** the Free Software Foundation; either version 2 of the License, or
10 ** (at your option) any later version.
12 ** This program is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ** GNU General Public License for more details.
17 ** You should have received a copy of the GNU General Public License
18 ** along with this program in a file called COPYING; if not, write to
19 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 ** MA 02110-1301, USA.
24 ** Bug reports and questions can be sent to kde-devel@kde.org
27 #include <QtGui/QPixmap>
28 #include <Qt3Support/Q3PtrStack>
29 //Added by qt3to4:
30 #include <QtGui/QMouseEvent>
31 #include <QtGui/QFrame>
32 #include <Qt3Support/Q3PtrList>
33 #include <kdebug.h>
34 #include <klocale.h>
35 #include <kglobal.h>
37 // kpackage.headers
38 #include "kpackage.h"
39 #include "pkgInterface.h"
40 #include "packageInfo.h"
41 #include "managementWidget.h"
42 #include "packageDisplay.h"
43 #include "kplview.h"
45 #define MPOS 1
47 KpTreeList::KpTreeList( QWidget *parent ) :
48 K3ListView (parent)
50 markPkg = 0;
51 setShowSortIndicator(true);
53 Q3PtrStack<QString> stack();
55 setFrameStyle(QFrame::Panel|QFrame::Sunken);
56 setLineWidth(2);
57 setItemMargin(2);
58 addColumn(i18n("Package"));
59 setColumnWidthMode(0,Q3ListView::Manual);
60 addColumn(i18n("Mark"));
61 setColumnWidthMode(1,Q3ListView::Manual);
62 addColumn(i18n("Summary"));
63 setColumnWidthMode(2,Q3ListView::Manual);
64 addColumn(i18n("Size"));
65 setColumnWidthMode(3,Q3ListView::Manual);
66 addColumn(i18n("Version"));
67 setColumnWidthMode(4,Q3ListView::Manual);
68 addColumn(i18n("Old Version"));
69 setColumnWidthMode(5,Q3ListView::Manual);
71 // setAllColumnsShowFocus(true);
72 // setColumnAlignment(3, Qt::AlignRight);
73 setRootIsDecorated(true);
74 update();
75 readTreePos();
76 show();
80 void KpTreeList::clear()
82 markPkg = 0;
83 emit cleared();
84 K3ListView::clear();
87 KpTreeListItem *KpTreeList::firstChild()
89 return (KpTreeListItem *)K3ListView::firstChild();
92 KpTreeListItem *KpTreeList::currentItem()
94 return (KpTreeListItem *)K3ListView::currentItem();
97 void KpTreeList::contentsMousePressEvent ( QMouseEvent * e )
99 bool markUpdate = false;
101 if (e->button() == Qt::LeftButton) {
102 if (inMark(e->x())) {
103 QPoint vp = contentsToViewport(e->pos());
104 KpTreeListItem *i = ( KpTreeListItem *)itemAt( vp );
105 if (i && i->childCount() == 0) {
106 if (e->modifiers() == Qt::ShiftButton) {
107 if (i->childCount() == 0) {
108 i->setMark(true);
109 markUpdate = true;
111 KpTreeListItem *item = i;
112 while ((item = (KpTreeListItem *)item->itemAbove()) && !item->marked) {;}
113 if (item) {
114 item = i;
115 while ((item = (KpTreeListItem *)item->itemAbove()) && !item->marked) {
116 if (item->childCount() == 0) {
117 item->setMark(true);
118 markUpdate = true;
121 } else {
122 item = i;
123 while ((item = (KpTreeListItem *)item->itemBelow()) && !item->marked) {;}
124 if (item) {
125 item = i;
126 while ((item = (KpTreeListItem *)item->itemBelow()) && !item->marked) {
127 if (item->childCount() == 0) {
128 item->setMark(true);
129 markUpdate = true;
134 } else {
135 i->toggleMark();
136 markUpdate = true;
138 setCurrentItem(i);
139 } else
140 K3ListView::contentsMousePressEvent (e);
141 } else
142 K3ListView::contentsMousePressEvent (e);
145 if (markUpdate)
146 emit updateMarked();
150 bool KpTreeList::inMark(int x) {
151 int st = 0;
152 int i;
154 for (i = 0; i < MPOS; i++)
155 st += columnWidth(i);
156 return (x >= st && x <= st + columnWidth(MPOS));
159 ///////////////////////////////////////////////////////////////////////////
161 void KpTreeList::expand()
163 KpTreeListItem *item = firstChild();
165 if (!item)
166 return;
168 do {
169 expandChild(item->firstChild());
170 } while ((item = item->nextSibling()));
174 void KpTreeList::expandChild(KpTreeListItem *it)
176 do {
177 if (it->childCount() > 0) {
178 expandChild(it->firstChild());
179 it->setVisible(true);
181 } while ((it = it->nextSibling()));
184 ///////////////////////////////////////////////////////////////////////////
186 void KpTreeList::sweep(bool init)
188 KpTreeListItem *item = firstChild();
190 if (!item)
191 return;
193 if (init) {
194 expand();
197 do {
198 int ret = sweepChild(item->firstChild());
199 if (!ret) {
200 item->setVisible(false);
202 } while ((item = item->nextSibling()));
205 int KpTreeList::sweepChild(KpTreeListItem *it)
207 int ret, shown = 0;
209 do {
210 if (it->childCount() > 0) {
211 ret = sweepChild(it->firstChild());
212 // kDebug()<<"VV="<<it->text(0)<<" "<<it->isVisible()<<" " << ret << "\n";
213 if (!ret) {
214 it->setVisible(false);
215 } else {
216 if (it->isVisible())
217 shown += ret;
219 } else {
220 // kDebug()<<"VV="<<it->text(0)<<" "<<it->isVisible()<<"\n";
221 if (!it->info->display(treeType)) {
222 it->setVisible(false);
223 } else {
224 if (it->isVisible())
225 shown++;
228 } while ((it = it->nextSibling()));
229 return shown;
233 ///////////////////////////////////////////////////////////////////////////
234 void KpTreeList::clearMarked(KpTreeListItem *item)
236 while (item) {
237 if (item->childCount() > 0) {
238 clearMarked(item->firstChild());
240 item->setMark(false);
241 item = item->nextSibling();
245 void KpTreeList::markAll(KpTreeListItem *item)
247 while (item) {
248 if (item->childCount() > 0) {
249 markAll(item->firstChild());
251 else {
252 if (item->info->display(treeType)) {
253 item->setMark(true);
256 item = item->nextSibling();
260 void KpTreeList::findMarked(KpTreeListItem *item, Q3PtrList<KpTreeListItem> &selList)
262 while (item) {
263 if (item->childCount() > 0) {
264 findMarked(item->firstChild(), selList);
266 if (item->marked) {
267 selList.insert(0,item);
269 item = item->nextSibling();
273 void KpTreeList::countMarked(KpTreeListItem *item, int &cntInstall, int &cntUnInstall)
275 while (item) {
276 if (item->childCount() > 0) {
277 countMarked(item->firstChild(), cntInstall, cntUnInstall);
279 if (item->marked) {
280 if (item->info->isInstallable())
281 cntInstall++;
282 else
283 cntUnInstall++;
285 item = item->nextSibling();
289 ///////////////////////////////////////////////////////////////////////////
290 void KpTreeList::expandTree(KpTreeList *list)
292 KpTreeListItem *item = list->firstChild();
294 while (item) {
295 if (item->childCount() > 0) {
296 item->setOpen(true);
297 expandTree(item);
299 item = item->nextSibling();
304 void KpTreeList::expandTree(KpTreeListItem *pitem)
306 KpTreeListItem *item = pitem->firstChild();
308 while (item) {
309 if (item->childCount() > 0) {
310 item->setOpen(true);
311 expandTree(item);
313 item = item->nextSibling();
317 void KpTreeList::collapseTree(KpTreeList *list)
319 KpTreeListItem *item = list->firstChild();
321 while (item) {
322 if (item->childCount() > 0) {
323 collapseTree(item);
325 item = item->nextSibling();
329 void KpTreeList::collapseTree(KpTreeListItem *pitem)
331 int n = 0;
332 KpTreeListItem *item = pitem->firstChild();
334 while (item) {
335 if (item->childCount() > 0) {
336 n++;
337 collapseTree(item);
339 item = item->nextSibling();
341 if (n)
342 pitem->setOpen(true);
343 else
344 pitem->setOpen(false);
347 ///////////////////////////////////////////////////////////////////////////
348 // A package has been highlighted in the list tree
349 void KpTreeList::packageHighlighted(Q3ListViewItem *item, packageDisplayWidget *packageDisplay)
351 KpTreeListItem *sel = (KpTreeListItem *)item;
353 if (!sel || sel->childCount()) {
354 packageDisplay->changePackage(0);
355 } else if (sel) {
356 if (!notPress) {
357 int n = stack.at();
358 int num = stack.count();
359 for (int i = num - 1; i >= n; i--) {
360 stack.remove(i);
362 kpkg->disableNext();
365 // Disable the tree list while we do this
366 setEnabled(false);
368 // Tell everything that is interested to change packages
369 packageDisplay->changePackage(sel->info);
371 // Re-enable the treeList and uninstall button
372 setEnabled(true);
373 setFocus();
375 if (!notPress) {
376 stack.append(sel);
380 notPress = false;
383 KpTreeListItem *KpTreeList::search( const QString &str, const QString &head,
384 KpTreeListItem *start)
386 KpTreeListItem *item = firstChild();
388 searchCitem = start;
389 searchSkip = false;
390 searchSubstr = false;
391 searchStr = str;
392 searchResult = 0;
394 do {
395 if (item->text(0) == head) {
396 searchChild(item->firstChild());
397 if (searchResult != 0)
398 return searchResult;
400 } while ((item = item->nextSibling()));
401 return 0;
404 KpTreeListItem *KpTreeList::search(const QString &str, bool subStr, bool wrap,
405 bool start=false)
407 kDebug() << "KpTreeList::search2\n";
408 if (!firstChild())
409 return 0;
411 if (start)
412 searchCitem = 0;
413 else
414 searchCitem = currentItem();
415 searchSkip = !wrap;
416 searchSubstr = subStr;
417 searchStr = str;
418 searchResult = 0;
420 searchChild(firstChild());
422 return changePack(searchResult);
425 bool KpTreeList::searchChild(KpTreeListItem *it)
427 do {
428 if (!searchSkip) {
429 QString s = it->text(0);
430 // kDebug() << "s='" << s << "'='" << searchStr << "\n";
431 if ((it->childCount() == 0) && (it->info->display(treeType)) &&
432 (searchSubstr ? s.contains(searchStr, Qt::CaseInsensitive) : s == searchStr)) {
433 searchResult = it;
434 return true;
438 if (searchCitem == it) {
439 if (searchSkip) {
440 searchSkip = false;
441 } else {
442 return true;
446 if (it->childCount() > 0) {
447 if (searchChild(it->firstChild()))
448 return true;
450 } while ((it = it->nextSibling()));
451 return false;
454 KpTreeListItem *KpTreeList::changePack(KpTreeListItem *searchResult, bool push)
456 if (searchResult) {
457 Q3ListViewItem *i;
459 i = searchResult;
460 while ((i = i->parent())) {
461 i->setOpen(true);
463 if (push) {
464 stack.append(searchResult);
465 kpkg->enablePrevious();
468 notPress = true;
469 setSelected(searchResult,true);
470 setCurrentItem(searchResult);
471 ensureItemVisible(searchResult);
472 return searchResult;
473 } else {
474 return 0;
478 void KpTreeList::next()
480 int n = stack.at();
481 KpTreeListItem *s = stack.at(n + 1);
482 if (s) {
483 changePack(s, false);
485 if (n >= int(stack.count() - 2)) {
486 kpkg->disableNext();
488 if (n >= 0) {
489 kpkg->enablePrevious();
493 void KpTreeList::previous()
495 int n = stack.at();
496 KpTreeListItem *s = stack.at(n-1);
498 if (s) {
499 changePack(s, false);
500 kpkg->enableNext();
502 if (n <= 1) {
503 kpkg->disablePrevious();
505 if (n < int(stack.count() - 2)) {
506 kpkg->enableNext();
510 void KpTreeList::stackRemove(KpTreeListItem *pack)
512 int n = stack.find(pack);
513 if (n >= 0) {
514 if (n == 0) {
515 kpkg->disablePrevious();
517 if (n >= int(stack.count() - 1)) {
518 kpkg->disableNext();
520 stack.remove(pack);
525 //////////////////////////////////////////////////////////////////////////////
527 void KpTreeList::writeTreePos()
530 KConfigGroup config(KGlobal::config(), "Treelist");
531 saveLayout(config);
532 return;
534 int i;
535 QString colpos;
536 for (i = 0; i < 6; i++) {
537 colpos.setNum(i);
538 config.writeEntry(colpos,columnWidth(i));
542 void KpTreeList::readTreePos()
544 kDebug() << "KpTreeList::readTreePos()" << "\n";
545 KConfigGroup config(KGlobal::config(), "Treelist");
546 restoreLayout(config);
547 return;
549 int i, n;
550 int num[] = {185,37,180,54,95,95};
552 return;
555 QString colpos;
556 for (i = 0; i < 6; i++) {
557 colpos.setNum(i);
558 n = config.readEntry(colpos,num[i]);
559 if (n == 0)
560 n = 10;
561 setColumnWidth(i,n);
565 //////////////////////////////////////////////////////////////////////////////
566 //////////////////////////////////////////////////////////////////////////////
567 KpTreeListItem::KpTreeListItem( Q3ListViewItem *parent, packageInfo* pinfo,
568 const QPixmap& thePixmap,
569 QString label1, QString label2 ,
570 QString label3 , QString label4 ,
571 QString label5 , QString label6 ,
572 QString label7 , QString label8
574 ) : Q3ListViewItem(parent, label1, label2, label3, label4, label5,
575 label6, label7, label8)
577 info = pinfo;
578 marked = false;
579 setPixmap(0, thePixmap);
580 if (!label2.isNull())
581 setPixmap(1,info->interface->markUnInst);
584 KpTreeListItem::KpTreeListItem( K3ListView *parent, packageInfo* pinfo,
585 const QPixmap& thePixmap,
586 QString label1, QString label2 ,
587 QString label3 , QString label4 ,
588 QString label5 , QString label6 ,
589 QString label7 , QString label8
590 ) : Q3ListViewItem(parent, label1, label2, label3, label4, label5,
591 label6, label7, label8)
593 info = pinfo;
594 marked = false;
595 setPixmap(0, thePixmap);
596 if (!label2.isNull())
597 setPixmap(1,info->interface->markUnInst);
600 KpTreeListItem *KpTreeListItem::firstChild()
602 return (KpTreeListItem *)Q3ListViewItem::firstChild();
605 KpTreeListItem *KpTreeListItem::nextSibling()
607 return (KpTreeListItem *)Q3ListViewItem::nextSibling();
610 void KpTreeListItem::toggleMark()
612 marked = ! marked;
613 if (marked)
614 setPixmap(1,info->interface->markInst);
615 else
616 setPixmap(1,info->interface->markUnInst);
619 void KpTreeListItem::setMark(bool mark)
621 marked = mark;
622 if (mark)
623 setPixmap(1,info->interface->markInst);
624 else {
625 if (info)
626 setPixmap(1,info->interface->markUnInst);
630 void KpTreeListItem::hide()
632 setHeight(0);
635 void KpTreeListItem::show()
637 setup();
641 int KpTreeListItem::compare( Q3ListViewItem *i, int col, bool ascending ) const
642 { // Make sorting more certain
643 if (col == 3) { // size column
644 QString k, j;
645 int p;
646 float vk, vj;
647 QRegExp rx("([0-9.]+)(.*)");
649 j = key( col, ascending );
650 p = rx.indexIn(j);
651 if (p != -1) {
652 if (rx.cap(2) == "MB") {
653 vj = rx.cap(1).toFloat() * 1000000;
654 } else if (rx.cap(2) == "kB") {
655 vj = rx.cap(1).toFloat() * 1000;
656 } else {
657 vj = rx.cap(1).toFloat();
661 k = i->key( col, ascending );
662 p = rx.indexIn(k);
663 if (p != -1) {
664 if (rx.cap(2) == "MB") {
665 vk = rx.cap(1).toFloat() * 1000000;
666 } else if (rx.cap(2) == "kB") {
667 vk = rx.cap(1).toFloat() * 1000;
668 } else {
669 vk = rx.cap(1).toFloat();
673 // kDebug() << k << "=" << vk << " " << j << "=" << vj << "\n";
674 if (vj < vk)
675 return -1;
676 else if (vj == vk)
677 return 0;
678 else
679 return 1;
680 } else {
681 return Q3ListViewItem::compare(i, col, ascending);
685 #include "kplview.moc"