addresslist.cpp
来自「Qtopia下的邮件处理程序」· C++ 代码 · 共 753 行 · 第 1/2 页
CPP
753 行
case Qt::Key_Enter: { const QTreeWidgetItem* current = currentItem(); setItemSelected(current,!isItemSelected(current)); } break; default: QTreeWidget::keyPressEvent( e ); }}AddressItem::AddressItem(QTreeWidget *parent, const EMailAddress &a, int i) : QTreeWidgetItem(parent), _address(a), show(i){ ensurePixmaps(); setColumns();}QString AddressItem::key(int c, bool) const{ if ( c == 0 ) { QString s = QString::number( fromContacts() ); s += text(1).toLower(); return s; } return text(c).toLower();}void AddressItem::setColumns(){ if ( _address.fromContacts ) setIcon(0, *pm_contacts);// else// setPixmap(0, *pm_email);//TODO fix multiple columns focus problem// if ( show == (int) AddressPicker::Name || show == (int) AddressPicker::NameAndEmail )// setText(1, _address.name ); setText(0, _address.name ); if ( show == (int) AddressPicker::Email || show == (int) AddressPicker::NameAndEmail ) setText(2, _address.email );}#ifdef QTOPIA4_TODOvoid AddressItem::paintCell( QPainter *p, const QColorGroup &cg, int column, int width, int alignment ){ QColorGroup _cg( cg ); //while testing if ( _address.deleted ) _cg.setColor( QColorGroup::Text, Qt::red );// else if ( !_address.fromContacts )// _cg.setColor( QColorGroup::Text, Qt::blue ); QListWidgetItem::paintCell( p, _cg, column, width, alignment ); _cg.setColor( QColorGroup::Text, column );}#endifAddressPicker::AddressPicker(AddressType atype, QWidget *parent, const QString name, Qt::WFlags fl) : QDialog(parent, fl){ setObjectName( name ); _atype = atype; list = addressList(); init();// m_Category = -2; list->load(); connect(list, SIGNAL(updated()), this, SLOT(listAddresses()) ); listAddresses(); setWindowTitle( tr("Select Contact") ); addressView->setFocus(); //hack to get focus on first item QModelIndex m = addressView->model()->index( 0, 0 ); addressView->setCurrentIndex(m); addressView->setItemSelected(addressView->topLevelItem(0),false); }AddressPicker::~AddressPicker(){ // Don't really have to save here, but let's do it to ensure we don't lose many addresses // in case of a crash happening somewhere addressList()->save();}AddressList* AddressPicker::addressList(){ if ( !alist ) alist = new AddressList(); return alist;}void AddressPicker::init(){ QGridLayout *grid = new QGridLayout(this);#ifdef QTOPIA_PHONE grid->setMargin(0); grid->setSpacing(0);#else#ifdef QTOPAI4_TODO grid->setMargin(3); grid->setSpacing(4); combo = new QComboBox(this); connect(combo, SIGNAL(activated(int)), this, SLOT(comboSelected(int)) ); combo->insertItem( tr("Contacts and Email address list") ); combo->insertItem( tr("Contacts only") ); combo->insertItem( tr("Email address list only") );#endif#endif addressView = new AddressView(this); connect(addressView, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), this, SLOT(updateListButtons()) ); addressView->setSelectionMode( QAbstractItemView::MultiSelection ); addressView->setSelectionBehavior(QAbstractItemView::SelectRows); QStringList columns; //TODO multiple columns focus problem// columns << "@"; if ( _atype == Name || _atype == NameAndEmail ) columns << tr( "Name" ); if ( _atype == Email || _atype == NameAndEmail ) columns << tr (" Email "); addressView->setColumnCount(columns.count()); addressView->setHeaderLabels(columns); addressView->setRootIsDecorated(false); //addressView->setSortingEnabled(true); setTabOrder(addressView,addressView); #ifdef QTOPIA4_TODO#ifndef QTOPIA_PHONE sel = new CategorySelect(this); Categories c; c.load( categoryFileName() ); QArray<int> vl( 0 ); sel->setCategories(vl, tr("Email"), tr("Email") ); sel->setAllCategories( true ); connect(sel, SIGNAL(signalSelected(int)), this, SLOT(setCategoryFilter(int)) ); deleteButton = new QToolButton(this); connect(deleteButton, SIGNAL(clicked()), this, SLOT(removeFromList()) ); deleteButton->setPixmap( QIcon( ":icon/trash" ).pixmap() ); addButton = new QToolButton(this); connect(addButton, SIGNAL(clicked()), this, SLOT(addToContacts()) ); addButton->setText( tr( "Add to Contacts" ) ); QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum ); grid->addMultiCellWidget( combo, 0, 0, 0, 2); grid->addMultiCellWidget(addressView, 1, 1, 0, 2); grid->addMultiCellWidget(sel, 2, 2, 0, 2); grid->addWidget(deleteButton, 3, 0); grid->addWidget(addButton, 3, 1); grid->addItem(spacer, 3, 2);//#else#endif#endif grid->addWidget(addressView, 0, 0 );// #endif}void AddressPicker::listAddresses(){ addressView->clear(); #ifdef QTOPIA_PHONE int filter = 0;#else int filter = combo->currentIndex();#endif if ( filter != 2 ) { QList<EMailAddress> cList = list->contacts(); for (QList<EMailAddress>::Iterator it = cList.begin(); it != cList.end(); ++it) { listEntry( *it ); } } if ( filter != 1 ) { QList<EMailAddress> cList = list->emailContacts(); for (QList<EMailAddress>::Iterator eit = cList.begin(); eit != cList.end(); ++eit) { listEntry( *eit ); } } updateListButtons(); for(int i =0; i < addressView->columnCount();i++) addressView->resizeColumnToContents(i); //addressView->fitHeaders( addressView->width() );}void AddressPicker::listEntry(const EMailAddress &a){#ifdef QTOPIA4_TODO// if ( useCategories() ) {// QStringList cats = a.categories;// if ( m_Category == -1 ) {// if ( cats.count() > 0 )// return;// } else if ( m_Category != -2 ) {// if (cats.indexOf(m_Category) == -1)// return;// }// }#endif if ( a.fromContacts ) { (void) new AddressItem(addressView, a, _atype); } else { if ( !a.deleted && !a.merged ) (void) new AddressItem(addressView, a, _atype); }}bool AddressPicker::useCategories(){#ifdef QTOPIA_PHONE return false;#else return combo->currentIndex() != 2;#endif};QStringList AddressPicker::addressesSelected(){ QStringList list; QList<QTreeWidgetItem*> selected = addressView->selectedItems(); QList<QTreeWidgetItem*>::Iterator it; for (it = selected.begin() ; it != selected.end(); ++it) { EMailAddress a = ((AddressItem *)(*it))->address(); switch ( _atype ) { case Name: list.append( a.name ); break; case Email: list.append(a.email ); break; default: list.append( buildAddress( a.name, a.email ) ); break; } } return list;}void AddressPicker::setPreviousAddresses(QStringList &l){ QString str; AddressItem *currentItem = 0; int count = 0; QTreeWidgetItem* firstItem = addressView->headerItem(); for ( int index = 0; index < firstItem->childCount(); ++index){ currentItem = (AddressItem *) firstItem->child(index); EMailAddress a = currentItem->address(); str = buildAddress( a.name, a.email ); for (QStringList::Iterator it = l.begin(); it != l.end(); ++it) { if ( str == *it ) { addressView->setItemSelected((QTreeWidgetItem *) currentItem,true); it = l.erase( it ); count++; break; } } } if ( currentItem && count == 1 ) addressView->scrollToItem( (QTreeWidgetItem *) currentItem);}void AddressPicker::setCategoryFilter(int i){ Q_UNUSED(i)#ifdef QTOPIA4_TODO if ( m_Category != i ) { m_Category = i; listAddresses(); }#endif}void AddressPicker::comboSelected(int){#ifndef QTOPIA_PHONE if ( !useCategories() ) sel->hide(); else sel->show();#endif listAddresses();}void AddressPicker::updateListButtons(){#ifdef QTOPIA4_TODO bool enable = false; QListWidgetItemIterator it(addressView); for ( ; it.current(); ++it) { if ( it.current()->isSelected() ) { if ( ((AddressItem *) it.current())->fromContacts() ) { enable = false; break; } else { enable = true; } } }#ifndef QTOPIA_PHONE deleteButton->setEnabled(enable); addButton->setEnabled(enable);#endif#endif}void AddressPicker::addToContacts(){ QList<EMailAddress> adr; QList<QTreeWidgetItem*> selected = addressView->selectedItems(); QList<QTreeWidgetItem*>::iterator itView; for (itView = selected.begin() ; itView != selected.end(); ++itView) { EMailAddress a = ((AddressItem *) (*itView))->address(); if ( !a.fromContacts ) { adr.append(a); } } //avoid uodate signals while we're adding contacts disconnect(list, SIGNAL(updated()), this, SLOT(listAddresses()) ); for ( QList<EMailAddress>::Iterator it = adr.begin(); it != adr.end(); ++it) { QContact c = contact((*it).name, (*it).email); if ( adr.count() == 1 ) {#ifndef QT_NO_COP QCopServiceRequest e( "Contacts", "addAndEditContact(QContact)" ); e << c; e.send();#endif } else { list->addContact( c ); } } if ( adr.count() ) listAddresses(); // reconnect signals as we're done adding addresses connect(list, SIGNAL(updated()), this, SLOT(listAddresses()) );}void AddressPicker::removeFromList(){ QList<QTreeWidgetItem*> selected = addressView->selectedItems(); QList<QTreeWidgetItem*>::iterator it; for (it = selected.begin() ; it != selected.end(); ++it) { //strictly not necessary, but lets be safe if ( !((AddressItem *) (*it))->fromContacts() ) { list->deleteAddress( ((AddressItem *) (*it))->address() ); } } listAddresses();}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?