📄 bcgroupview.cpp
字号:
/*************************************************************************** bcgroupview.cpp ------------------- begin : Sat Oct 13 2001 copyright : (C) 2001 by Robby Stephenson email : robby@periapsis.org ***************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of version 2 of the GNU General Public License as * * published by the Free Software Foundation; * * * ***************************************************************************/#include "bcgroupview.h"#include "bcattribute.h"#include "bcunitgroup.h"#include <klineeditdlg.h>#include <klocale.h>#include <kdebug.h>#include <kiconloader.h>#include <kglobal.h>#include <qstring.h>#include <qstringlist.h>#include <qcolor.h>#include <qregexp.h>#include <qheader.h>// by default don't show the number of itemsBCGroupView::BCGroupView(QWidget* parent_, const char* name_/*=0*/) : KListView(parent_, name_), m_showCount(false) { // the app name isn't translated addColumn(QString::fromLatin1("Bookcase")); // hide the header since there's only one column header()->hide(); setRootIsDecorated(true); setTreeStepSize(10); // turn off the alternate background color setAlternateBackground(QColor()); QPixmap rename, expand, collapse, remove; rename = KGlobal::iconLoader()->loadIcon(QString::fromLatin1("editclear"), KIcon::Small); expand = KGlobal::iconLoader()->loadIcon(QString::fromLatin1("2downarrow"), KIcon::Small); collapse = KGlobal::iconLoader()->loadIcon(QString::fromLatin1("2uparrow"), KIcon::Small); remove = KGlobal::iconLoader()->loadIcon(QString::fromLatin1("remove"), KIcon::Small); // TODO: maybe allow this to be customized m_groupOpenPixmap = KGlobal::iconLoader()->loadIcon(QString::fromLatin1("folder_open"), KIcon::Small); m_groupClosedPixmap = KGlobal::iconLoader()->loadIcon(QString::fromLatin1("folder"), KIcon::Small); m_collMenu.insertItem(rename, i18n("Rename Collection"), this, SLOT(slotHandleRename())); m_groupMenu.insertItem(expand, i18n("Expand All Groups"), this, SLOT(slotExpandAll())); m_groupMenu.insertItem(collapse, i18n("Collapse All Groups"), this, SLOT(slotCollapseAll())); m_unitMenu.insertItem(remove, i18n("Delete Book"), this, SLOT(slotHandleDelete())); connect(this, SIGNAL(rightButtonPressed(QListViewItem*, const QPoint&, int)), SLOT(slotRMB(QListViewItem*, const QPoint&, int)));// connect(this, SIGNAL(clicked(QListViewItem*)), SLOT(slotSelected(QListViewItem*))); connect(this, SIGNAL(selectionChanged(QListViewItem*)), SLOT(slotSelected(QListViewItem*))); connect(this, SIGNAL(doubleClicked(QListViewItem*)), SLOT(slotToggleItem(QListViewItem*))); connect(this, SIGNAL(expanded(QListViewItem*)), SLOT(slotExpanded(QListViewItem*))); connect(this, SIGNAL(collapsed(QListViewItem*)), SLOT(slotCollapsed(QListViewItem*)));}ParentItem* BCGroupView::insertItem(ParentItem* collItem_, BCUnitGroup* group_) { QString text = group_->groupName(); ParentItem* par = new ParentItem(collItem_, text); par->setPixmap(0, m_groupClosedPixmap); par->setCount(group_->count()); QString key = groupKey(collItem_, group_); m_groupDict.insert(key, par); return par;}ParentItem* BCGroupView::locateItem(ParentItem* collItem_, BCUnitGroup* group_) { QString key = groupKey(collItem_, group_); ParentItem* par = m_groupDict.find(key); if(par) { return par; } return insertItem(collItem_, group_);}ParentItem* BCGroupView::locateItem(BCCollection* coll_) { ParentItem* root = 0; ParentItem* par; // iterate over the collections, which are the top-level children QListViewItem* collItem = firstChild(); for( ; collItem; collItem = collItem->nextSibling()) { // find the collItem matching the unit's collection and insert item inside par = static_cast<ParentItem*>(collItem); if(par->id() == coll_->id()) { root = par; break; } } if(!root) {// kdDebug() << "BCGroupView::locateItem() - adding new collection" << endl; root = new ParentItem(this, coll_->title(), coll_->id()); } return root;}const QString& BCGroupView::collGroupBy(const QString& type_) const { return m_collGroupBy.find(type_).data();}void BCGroupView::slotReset() { // don't really need to clear the collGroupBy map m_groupDict.clear(); clear();}void BCGroupView::slotRemoveItem(BCCollection* coll_) { if(!coll_) { kdWarning() << "BCGroupView::slotRemoveItem() - null coll pointer!" << endl; return; }// kdDebug() << "BCGroupView::slotRemoveItem(BCCollection) - " << coll_->title() << endl; ParentItem* collItem = locateItem(coll_); // first remove all groups in collection from dict QListViewItem* groupItem = collItem->firstChild(); for( ; groupItem; groupItem = groupItem->nextSibling()) { QString key = groupKey(coll_, groupItem); m_groupDict.remove(key); } // automatically deletes all children delete collItem;}void BCGroupView::slotModifyGroup(BCCollection* coll_, BCUnitGroup* group_) { if(!coll_ || !group_) { kdWarning() << "BCGroupView::slotModifyGroup() - null coll or group pointer!" << endl; return; } // if the units aren't grouped by attribute of the modified group, // we don't care, so return QString unitName = coll_->unitName(); if(m_collGroupBy.contains(unitName) && m_collGroupBy.find(unitName).data() != group_->attributeName()) { return; } // kdDebug() << "BCGroupView::slotModifyGroup() - " << group_->attributeName() << endl; ParentItem* root = locateItem(coll_); ParentItem* par = locateItem(root, group_); //make a copy of the group BCUnitGroup leftover(*group_); // first delete all items in this view but no longer in the group QListViewItem* item = par->firstChild(); QListViewItem* next; while(item) { BCUnit* unit = static_cast<BCUnitItem*>(item)->unit(); if(group_->containsRef(unit)) { leftover.removeRef(unit); item = item->nextSibling(); } else { // if it's not in the group, delete it// kdDebug() << "\tdeleting unit - " << unit->title() << endl; next = item->nextSibling(); delete item; item = next; } } // if there are no more child items in the group, no units in the leftover group // then delete the parent item if(par->childCount() == 0 && leftover.isEmpty()) { QString key = groupKey(coll_, par); m_groupDict.remove(key); delete par; return; } QPixmap icon; icon = KGlobal::iconLoader()->loadIcon(coll_->iconName(), KIcon::User); // in case the number of units in the group changed par->repaint(); // next add new listViewItems for items in the group, but not currently in the view QPtrListIterator<BCUnit> it(leftover); for( ; it.current(); ++it) { QString title = it.current()->title(); BCUnitItem* item = new BCUnitItem(par, title, it.current()); item->setPixmap(0, icon); if(isUpdatesEnabled()) { ensureItemVisible(item); setSelected(item, true); par->setOpen(true); } } // need to refresh triggerUpdate(); root->setOpen(true);}void BCGroupView::slotSelected(QListViewItem* item_) { // catch the case of a NULL pointer, i.e. not clicking on an item if(!item_) { emit signalClear(); return; } // unitItems always have a depth of 2 if(item_->depth() == 2) { BCUnitItem* item = static_cast<BCUnitItem*>(item_); if(item->unit()) { emit signalUnitSelected(item->unit()); } } else if(item_->depth() == 0) { // collections are at the root ParentItem* item = static_cast<ParentItem*>(item_); emit signalCollectionSelected(item->id()); }}void BCGroupView::slotSetSelected(BCUnit* unit_) {// kdDebug() << "BCGroupView::slotSetSelected()" << endl; // if unit_ is null pointer, set no selected if(!unit_) { setSelected(currentItem(), false); return; } if(selectedItem() && unit_ == static_cast<BCUnitItem*>(selectedItem())->unit()) { return; } // have to find a group whose attribute is the same as currently shown QString unitName = unit_->collection()->unitName(); if(!m_collGroupBy.contains(unitName)) { kdDebug() << "BCGroupView::slotSetSelected() - no group attribute for " << unitName << endl; return; } BCUnitGroup* group = 0; QString currentGroup = m_collGroupBy.find(unitName).data(); QPtrListIterator<BCUnitGroup> groupIt(unit_->groups()); for( ; groupIt.current(); ++groupIt) { if(groupIt.current()->attributeName() == currentGroup) { group = groupIt.current(); break; } } if(!group) { kdDebug() << "BCGroupView::slotSetSelected() - unit is not in any current groups!" << endl; return; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -