groupeditdialog.cpp Example File
samplephonebook/groupeditdialog.cpp
#include "groupeditdialog.h"
#include <QtGui>
#include "qcontact.h"
#include "qcontactmanager.h"
#include "qcontactdetailfilter.h"
#include "qcontactname.h"
GroupEditDialog::GroupEditDialog(QWidget *parent, QContactManager *contactManager)
: QDialog(parent), cm(contactManager)
{
Q_ASSERT(contactManager);
setWindowTitle(tr("Groups Edit"));
QFormLayout *layout = new QFormLayout;
groupNameEdit = new QLineEdit(this);
listWidget = new QListWidget(this);
addButton = new QPushButton(tr("Add"), this);
saveButton = new QPushButton(tr("Save"), this);
deleteButton = new QPushButton(tr("Delete"), this);
doneButton = new QPushButton(tr("Done"), this);
layout->addRow(tr("Group Name"), groupNameEdit);
layout->addRow(listWidget);
QHBoxLayout *row1Layout = new QHBoxLayout;
row1Layout->addWidget(addButton);
row1Layout->addWidget(saveButton);
row1Layout->addWidget(deleteButton);
layout->addRow(row1Layout);
QHBoxLayout *row2Layout = new QHBoxLayout;
row2Layout->addStretch(1);
row2Layout->addWidget(doneButton);
layout->addRow(row2Layout);
setLayout(layout);
repopulateGroupList();
connect(addButton, SIGNAL(clicked()),
this, SLOT(addButtonClicked()));
connect(deleteButton, SIGNAL(clicked()),
this, SLOT(deleteButtonClicked()));
connect(saveButton, SIGNAL(clicked()),
this, SLOT(saveButtonClicked()));
connect(doneButton, SIGNAL(clicked()),
this, SLOT(doneButtonClicked()));
connect(listWidget, SIGNAL(itemActivated(QListWidgetItem *)),
this, SLOT(groupItemActivated(QListWidgetItem *)));
connect(listWidget, SIGNAL(currentItemChanged (QListWidgetItem *, QListWidgetItem *)),
this, SLOT(groupItemChanged (QListWidgetItem *, QListWidgetItem *)));
}
void GroupEditDialog::repopulateGroupList()
{
listWidget->clear();
QContactDetailFilter groupFilter;
groupFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
groupFilter.setValue(QString(QLatin1String(QContactType::TypeGroup)));
QList<QContactLocalId> grpList = cm->contacts(groupFilter);
for (int index=0; index < grpList.count(); index++){
QContact grp = cm->contact(grpList[index]);
QListWidgetItem *item = new QListWidgetItem(grp.displayLabel(), listWidget);
item->setData(Qt::UserRole + 1, grpList[index]);
}
}
void GroupEditDialog::show()
{
repopulateGroupList();
}
void GroupEditDialog::addButtonClicked()
{
if (!groupNameEdit->text().isEmpty()){
QContact grp;
grp.setType(QContactType::TypeGroup);
bool result;
QContactName groupName;
groupName.setCustomLabel(groupNameEdit->text());
grp.saveDetail(&groupName);
result = cm->saveContact(&grp);
if (!result){
QMessageBox::information(this, tr("Group Add"),
tr("Failed to add Group '%1'.").arg(grp.displayLabel()));
}else{
repopulateGroupList();
for (int index = 0; index < listWidget->count();index++){
if (listWidget->item(index)->data(Qt::UserRole + 1).toUInt() == grp.id().localId()){
listWidget->setCurrentItem(listWidget->item(index));
break;
}
}
}
}else{
QMessageBox::information(this, tr("Empty Field"),
tr("Please enter a name."));
groupNameEdit->setFocus();
}
}
void GroupEditDialog::saveButtonClicked()
{
if (listWidget->currentItem()){
QContactLocalId grpID = listWidget->currentItem()->data(Qt::UserRole + 1).toUInt();
QContact grp = cm->contact(grpID);
bool result = false;
if (!grp.isEmpty()){
QContactName groupName;
groupName.setCustomLabel(groupNameEdit->text());
grp.saveDetail(&groupName);
result = cm->saveContact(&grp);
}
if (!result){
QMessageBox::information(this, tr("Save Group"),
tr("Failed to save Group '%1'.").arg(listWidget->currentItem()->text()));
}else{
repopulateGroupList();
for (int index = 0; index < listWidget->count();index++){
if (listWidget->item(index)->data(Qt::UserRole + 1).toUInt() == grp.id().localId()){
listWidget->setCurrentItem(listWidget->item(index));
break;
}
}
}
}
}
void GroupEditDialog::deleteButtonClicked()
{
if (listWidget->currentItem()){
int lastCurrentRow = listWidget->currentRow();
QContactLocalId grpID = listWidget->currentItem()->data(Qt::UserRole + 1).toUInt();
if (!cm->removeContact(grpID)){
QMessageBox::information(this, tr("Remove Group"),
tr("Failed to removed Group '%1'.").arg(listWidget->currentItem()->text()));
}else{
repopulateGroupList();
if (lastCurrentRow < listWidget->count()){
listWidget->setCurrentRow(lastCurrentRow);
}else {
if (listWidget->count())
listWidget->setCurrentRow(listWidget->count() - 1);
}
}
}
}
void GroupEditDialog::doneButtonClicked()
{
accept();
}
void GroupEditDialog::groupItemActivated(QListWidgetItem * item)
{
if (item)
groupNameEdit->setText(listWidget->currentItem()->text());
else
groupNameEdit->clear();
}
void GroupEditDialog::groupItemChanged ( QListWidgetItem * current, QListWidgetItem * )
{
groupItemActivated(current);
}