BE::Contacts::Contacts(AbookAddressbook *abook): m_abook(abook), m_dirty(false) { m_currentContact = 0; QImage img(QDir::homePath() + QLatin1String("/.abook/incognito.png")); if (!img.isNull()) m_incognitoPic = QPixmap::fromImage(img.scaled(160,160,Qt::KeepAspectRatio,Qt::SmoothTransformation)); m_ui = new Ui::Contacts; m_ui->setupUi(this); #if QT_VERSION >= 0x040700 m_ui->filter->setPlaceholderText(tr("Filter")); #endif m_ui2 = new Ui::OneContact; m_ui2->setupUi(m_ui->oneContact); fields << Field(AbookAddressbook::Name, m_ui2->name, QLatin1String("name")) << Field(AbookAddressbook::Mail, m_ui2->mail, QLatin1String("email")) << Field(AbookAddressbook::Address, m_ui2->address, QLatin1String("address")) << Field(AbookAddressbook::City, m_ui2->city, QLatin1String("city")) << Field(AbookAddressbook::State, m_ui2->state, QLatin1String("state")) << Field(AbookAddressbook::ZIP, m_ui2->zip, QLatin1String("zip")) << Field(AbookAddressbook::Country, m_ui2->country, QLatin1String("country")) << Field(AbookAddressbook::Phone, m_ui2->phone, QLatin1String("phone")) << Field(AbookAddressbook::Workphone, m_ui2->workphone, QLatin1String("workphone")) << Field(AbookAddressbook::Fax, m_ui2->fax, QLatin1String("fax")) << Field(AbookAddressbook::Mobile, m_ui2->mobile, QLatin1String("mobile")) << Field(AbookAddressbook::Nick, m_ui2->nick, QLatin1String("nick")) << Field(AbookAddressbook::URL, m_ui2->url, QLatin1String("url")) << Field(AbookAddressbook::Notes, m_ui2->notes, QLatin1String("notes")) << Field(AbookAddressbook::Anniversary, m_ui2->anniversary, QLatin1String("anniversary")) << Field(AbookAddressbook::Photo, m_ui2->photo, QLatin1String("photo")); m_sortFilterProxy = new QSortFilterProxyModel(this); m_sortFilterProxy->setFilterCaseSensitivity(Qt::CaseInsensitive); m_sortFilterProxy->setFilterKeyColumn(-1); m_sortFilterProxy->setSourceModel(m_abook->model()); connect (m_ui->filter, SIGNAL(textChanged(QString)), m_sortFilterProxy, SLOT(setFilterWildcard(QString))); m_ui->filter->installEventFilter(this); QFont fnt = m_ui2->name->font(); fnt.setPointSize(fnt.pointSize()*2); m_ui2->name->setFont(fnt); for (QList<Field>::const_iterator it = fields.constBegin(), end = fields.constEnd(); it != end; ++it) { it->label->installEventFilter(this); } m_ui->contacts->setModel(m_sortFilterProxy); m_ui->contacts->setSelectionMode(QAbstractItemView::SingleSelection); connect (m_ui->contacts->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), SLOT(setContact(QModelIndex))); QModelIndex idx = m_sortFilterProxy->index(0,0); if (idx.isValid()) m_ui->contacts->setCurrentIndex(idx); m_ui->contacts->installEventFilter(this); connect (m_ui->add, SIGNAL(clicked()), SLOT(addContact())); connect (m_ui->remove, SIGNAL(clicked()), SLOT(removeCurrentContact())); connect (qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), SLOT(updateFocusPolicy(QWidget*, QWidget*))); // cheat to correct the focuspolicies ;-) updateFocusPolicy(m_ui2->name, m_ui->filter); Q_FOREACH(const Field &field, fields) { if (QTextDocument *doc = field.label->findChild<QTextDocument*>()) connect(doc, SIGNAL(contentsChanged()), SLOT(updateLabel())); } }
bool BE::Contacts::eventFilter(QObject *o, QEvent *e) { if (o == m_ui->filter) { if (e->type() == QEvent::KeyPress) if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Down) m_ui->contacts->setFocus(); return false; } if (o == m_ui->contacts) { if (e->type() == QEvent::KeyPress) { QKeyEvent *ke = static_cast<QKeyEvent*>(e); if (ke->key() == Qt::Key_Up && !m_ui->contacts->currentIndex().row()) { m_ui->filter->setFocus(); return true; } else if (ke->key() == Qt::Key_Delete) { removeCurrentContact(); return true; } } return false; } if (!m_currentContact) return false; switch (e->type()) { case QEvent::DragEnter: case QEvent::DragMove: case QEvent::Drop: { if (o != m_ui2->photo) return false; QDropEvent *de = static_cast<QDropEvent*>(e); if (!de->mimeData()) return false; QList<QUrl> urls = de->mimeData()->urls(); if (urls.isEmpty()) return false; QUrl url = urls.first(); if ( #if QT_VERSION >= QT_VERSION_CHECK(4, 8, 0) url.isLocalFile() #else (url.scheme() == QLatin1String("file")) #endif && QImageReader(url.path()).canRead()) { if (e->type() == QEvent::Drop) importPhoto(url.path()); else de->acceptProposedAction(); } return false; } case QEvent::KeyPress: { const int key = static_cast<QKeyEvent*>(e)->key(); if (key == Qt::Key_Delete && o == m_ui2->photo) { // reset photo if (m_currentContact) m_currentContact->setData(QString(), Gui::AbookAddressbook::Photo); m_ui2->photo->setPixmap(m_incognitoPic); } else if (key == Qt::Key_Escape && o != m_ui2->photo) if (QLabel *l = qobject_cast<QLabel*>(o)) { setText(l, l->text()); return true; // prevent closing the dialog! } } default: return false; } return false; }