source: SMSSender/src/ui/VCAddressBook/vcaddressbook.cpp @ 55:33dc8614a999

3.0
Last change on this file since 55:33dc8614a999 was 55:33dc8614a999, checked in by Sämy Zehnder <saemy.zehnder@…>, 12 years ago
  • Added group-/contacttablemodel
  • Updated accountmodel
  • Implemented MVC in addressbook
File size: 8.0 KB
Line 
1#include "vcaddressbook.h"
2
3#include <QMessageBox>
4#include <QDebug>
5
6#include "../completers/bettercompleter.h"
7
8#include "../VCEditContact/vceditcontact.h"
9#include "../VCEditGroup/vceditgroup.h"
10
11#include "../UIHelper.h"
12
13#include <snumber.h>
14#include "../../business/BCContactManager.h"
15#include "../../business/BCGroupManager.h"
16#include "../../business/BusinessFactory.h"
17
18#include <typeconvert.h>
19
20
21const int CONTACT_COL_INDEX_IMAGE  = 0;
22const int CONTACT_COL_INDEX_NAME   = 1;
23const int CONTACT_COL_INDEX_NUMBER = 2;
24
25const int GROUP_COL_INDEX_IMAGE    = 0;
26const int GROUP_COL_INDEX_NAME     = 1;
27const int GROUP_COL_INDEX_MEMBERS  = 2;
28
29
30VCAddressBook::VCAddressBook(QWidget *parent) : QDialog(parent) {
31        ui.setupUi(this);
32
33        /* Contacts */
34    contactModel_      = new ContactTableModel(this);
35    contactProxyModel_ = new BetterCompletionModel(this);
36    contactProxyModel_->setFilterKeyColumn(-1); // Filter from all
37    contactProxyModel_->setSourceModel(contactModel_);
38    connect(ui.edtContactFilter, SIGNAL(textChanged(QString)),
39            contactProxyModel_, SLOT(setFilterFixedString(QString)));
40
41    ui.tblViewContacts->setModel(contactProxyModel_);
42    ui.tblViewContacts->resizeRowsToContents();
43    ui.tblViewContacts->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
44    ui.tblViewContacts->horizontalHeader()->setResizeMode(ContactTableModel::ColAliases, QHeaderView::Stretch);
45    ui.tblViewContacts->sortByColumn(ContactTableModel::ColName, Qt::AscendingOrder);
46    ui.tblViewContacts->resizeColumnsToContents();
47    ui.tblViewContacts->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
48
49    connect(ui.tblViewContacts, SIGNAL(doubleClicked(const QModelIndex&)),
50            this, SLOT(on_btnEditContact_clicked()));
51    connect(ui.tblViewContacts->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
52            this, SLOT(on_tblViewContacts_selectionChanged()));
53
54    on_tblViewContacts_selectionChanged(); // Set initial state
55
56
57    /* Groups */
58    groupModel_      = new GroupTableModel(this);
59    groupProxyModel_ = new BetterCompletionModel(this);
60    groupProxyModel_->setFilterKeyColumn(-1); // Filter from all
61    groupProxyModel_->setSourceModel(groupModel_);
62    connect(ui.edtGroupFilter, SIGNAL(textChanged(QString)),
63            groupProxyModel_, SLOT(setFilterFixedString(QString)));
64
65    ui.tblViewGroups->setModel(groupProxyModel_);
66    ui.tblViewGroups->resizeRowsToContents();
67    ui.tblViewGroups->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
68    ui.tblViewGroups->horizontalHeader()->setResizeMode(GroupTableModel::ColMembers, QHeaderView::Stretch);
69    ui.tblViewGroups->sortByColumn(ContactTableModel::ColName, Qt::AscendingOrder);
70    ui.tblViewGroups->resizeColumnsToContents();
71    ui.tblViewGroups->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
72
73    connect(ui.tblViewGroups, SIGNAL(doubleClicked(const QModelIndex&)),
74            this, SLOT(on_btnEditGroup_clicked()));
75    connect(ui.tblViewGroups->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
76            this, SLOT(on_tblViewGroups_selectionChanged()));
77
78    on_tblViewGroups_selectionChanged(); // Set initial state
79}
80
81/*QSet<IContact*> VCAddressBook::getFilteredContactList(){
82    if (actualContactFilterStr_ == ""){
83        return contactList_;
84    }
85
86    QSet<IContact*> contactList;
87    foreach (IContact* contact, contactList_) {
88        bool containsFilter = false;
89
90        containsFilter = containsFilter || contact->name().contains(actualContactFilterStr_, Qt::CaseInsensitive);
91
92        SNumber number = contact->number();
93        QString numberStr;
94        // +41791234567
95        numberStr = number.toString("+CCAAAu");
96        numberStr.remove(" ");
97        containsFilter = containsFilter || numberStr.contains(actualContactFilterStr_, Qt::CaseInsensitive);
98        // +410791234567
99        numberStr = number.toString("+ccaaau");
100        containsFilter = containsFilter || numberStr.contains(actualContactFilterStr_, Qt::CaseInsensitive);
101
102        QStringList aliases = contact->aliases();
103        for (int x = 0; x < aliases.size() && !containsFilter; ++x){
104            QString alias = aliases.at(x);
105            alias.remove(" ");
106            containsFilter = containsFilter || alias.contains(actualContactFilterStr_, Qt::CaseInsensitive);
107        }
108
109        if (containsFilter){
110            contactList.insert(contact);
111        }
112    }
113
114    return contactList;
115}*/
116
117/* Contacts */
118IContact* VCAddressBook::getContactFromRow(int rowId) const {
119    QModelIndex idx = contactProxyModel_->mapToSource(contactProxyModel_->index(rowId, 0));
120    return contactModel_->dataObject(idx);
121}
122
123IContact* VCAddressBook::getSelectedContact() const {
124    QItemSelectionModel* ism = ui.tblViewContacts->selectionModel();
125    if (!ism->hasSelection())
126        return NULL;
127
128    int row = ism->selectedRows().first().row();
129    return getContactFromRow(row);
130}
131
132
133void VCAddressBook::editContact(IContact* contact){
134    VCEditContact* ec = new VCEditContact(contact, this);
135    if (ec->exec() == QDialog::Accepted){ // Wait for return
136        BCContactManager::instance()->saveContact(contact);
137    }
138}
139
140
141/* Groups */
142IGroup* VCAddressBook::getGroupFromRow(int rowId) const {
143    QModelIndex idx = groupProxyModel_->mapToSource(groupProxyModel_->index(rowId, 0));
144    return groupModel_->dataObject(idx);
145}
146
147IGroup* VCAddressBook::getSelectedGroup() const {
148    QItemSelectionModel* ism = ui.tblViewGroups->selectionModel();
149    if (!ism->hasSelection())
150        return NULL;
151
152    int row = ism->selectedRows().first().row();
153    return getGroupFromRow(row);
154}
155
156
157void VCAddressBook::editGroup(IGroup* group){
158    VCEditGroup* eg = new VCEditGroup(group, this);
159    if (eg->exec() == QDialog::Accepted){ // Wait for return
160        BCGroupManager::instance()->saveGroup(group);
161    }
162}
163
164
165// Slots...
166
167/* Contacts */
168void VCAddressBook::on_btnAddContact_clicked(){
169    editContact(BusinessFactory::instance()->getContactInstance());
170}
171
172void VCAddressBook::on_btnEditContact_clicked(){
173    IContact* contact = getSelectedContact();
174    if (contact == NULL)
175        return;
176
177    editContact(contact);
178}
179
180void VCAddressBook::on_btnRemoveContact_clicked(){
181    IContact* contact = getSelectedContact();
182    if (contact == NULL)
183        return;
184
185    if (QMessageBox::Yes == QMessageBox::warning(this, tr("SMSSender"),
186                                                 tr("Do you really want to remove this contact?"),
187                                                 QMessageBox::Yes,
188                                                 QMessageBox::No | QMessageBox::Default | QMessageBox::Escape)){
189
190        //TODO: do this eventually over the model...
191        BCContactManager::instance()->removeContact(contact->id());
192    }
193}
194
195void VCAddressBook::on_tblViewContacts_selectionChanged() {
196    ui.btnEditContact->setEnabled(ui.tblViewContacts->selectionModel()->hasSelection());
197    ui.btnRemoveContact->setEnabled(ui.tblViewContacts->selectionModel()->hasSelection());
198}
199
200
201/* Groups */
202
203void VCAddressBook::on_btnAddGroup_clicked(){
204    editGroup(BusinessFactory::instance()->getGroupInstance());
205}
206
207void VCAddressBook::on_btnEditGroup_clicked(){
208    IGroup* group = getSelectedGroup();
209    if (group == NULL)
210        return;
211
212    editGroup(group);
213}
214
215void VCAddressBook::on_btnRemoveGroup_clicked(){
216    IGroup* group = getSelectedGroup();
217    if (group == NULL)
218        return;
219
220    if (QMessageBox::Yes == QMessageBox::warning(this, tr("SMSSender"),
221                                                 tr("Do you really want to remove this group?"),
222                                                 QMessageBox::Yes,
223                                                 QMessageBox::No | QMessageBox::Default | QMessageBox::Escape)){
224
225        //TODO: do this eventually over the model...
226        BCGroupManager::instance()->removeGroup(group->id());
227    }
228}
229
230void VCAddressBook::on_tblViewGroups_selectionChanged() {
231    ui.btnEditGroup->setEnabled(ui.tblViewGroups->selectionModel()->hasSelection());
232    ui.btnRemoveGroup->setEnabled(ui.tblViewGroups->selectionModel()->hasSelection());
233}
Note: See TracBrowser for help on using the repository browser.