source: SMSSender/frontend/gui/ui/editgroup.cpp @ 337:02ee15b6736a

separation-frontend-backend
Last change on this file since 337:02ee15b6736a was 337:02ee15b6736a, checked in by Sämy Zehnder <saemy.zehnder@…>, 6 years ago
  • Adjusts translations to new layout and completes them where possible.
  • Adds translation as a default to every sub-project.
File size: 7.5 KB
Line 
1/*
2 smssender - A frontend for fast and easy SMS sending over different gateways.
3 Copyright (C) 2007-2014, gorrión. See http://smssender.gorrion.ch
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18#include "editgroup.h"
19
20#include <QFileDialog>
21#include <QFileInfo>
22#include <QImageReader>
23#include <QMenu>
24#include <QMessageBox>
25
26#include "frontend/business/contactmanager.h"
27#include "frontend/business/groupmanager.h"
28
29namespace UI {
30
31EditGroup::EditGroup(QWidget *parent, SGroup *group,
32                     const GroupManager &groupManager, const ContactManager &contactManager)
33    : QDialog(parent)
34    , m_groupManager(groupManager)
35    , m_contactManager(contactManager)
36    , m_group(group)
37{
38    ui.setupUi(this);
39
40    defaultIcon_ = ui.btnIcon->icon();
41
42    ui.edtName->setText(group->name());
43
44    contactNameToContact_.clear();
45
46    // Load member contacts
47    ui.lstMemberContacts->clear();
48    QSet<const SContact*> members = group->contacts();
49    foreach (const SContact *contact, members) {
50        ui.lstMemberContacts->addItem(contact->name());
51        contactNameToContact_.insert(contact->name(), contact);
52    }
53    ui.lstMemberContacts->sortItems();
54
55    // Load available contacts
56    ui.lstAvailableContacts->clear();
57    QSet<const SContact*> available;
58    foreach (const SContact *contact, m_contactManager.contacts()) {
59        available.insert(contact);
60    }
61
62    available.subtract(members);
63    foreach (const SContact *contact, available) {
64        ui.lstAvailableContacts->addItem(contact->name());
65        contactNameToContact_.insert(contact->name(), contact);
66    }
67    ui.lstAvailableContacts->sortItems();
68
69    setGroupImage(group->image());
70}
71
72
73// Icon
74
75void EditGroup::addBtnIconMenu(){
76    QMenu* menu = new QMenu(ui.btnIcon);
77
78    QAction* action;
79    QIcon icon;
80
81    icon = QIcon();
82    icon.addPixmap(QPixmap(QString::fromUtf8(":/ico/replace.png")), QIcon::Normal, QIcon::Off);
83    action = new QAction(icon, tr("Change icon"), this);
84    menu->addAction(action);
85    connect(action, SIGNAL(activated()), this, SLOT(on_actionChangeIcon_activated()));
86
87    icon = QIcon();
88    icon.addPixmap(QPixmap(QString::fromUtf8(":/ico/remove.png")), QIcon::Normal, QIcon::Off);
89    action = new QAction(icon, tr("Remove icon"), this);
90    menu->addAction(action);
91    connect(action, SIGNAL(activated()), this, SLOT(on_actionRemoveIcon_activated()));
92
93    ui.btnIcon->setMenu(menu);
94}
95
96void EditGroup::setGroupImageFromFile(const QString& filename){
97    if ((filename != "") && (QFileInfo(filename).exists())){
98        QImage img(filename);
99        setGroupImage(img);
100    }else{
101        setGroupImage(QImage());
102    }
103}
104void EditGroup::setGroupImage(const QImage& image){
105    if (!image.isNull()){
106        ui.btnIcon->setIcon(QPixmap::fromImage(image));
107
108        groupImage_ = image;
109        addBtnIconMenu();
110    } else {
111        ui.btnIcon->setIcon(defaultIcon_);
112
113        groupImage_ = QImage();
114        removeBtnIconMenu();
115    }
116}
117
118void EditGroup::removeBtnIconMenu(){
119    ui.btnIcon->setMenu(NULL);
120}
121
122void EditGroup::on_btnIcon_clicked(){
123    on_actionChangeIcon_activated();
124}
125
126void EditGroup::on_actionChangeIcon_activated(){
127    QList<QByteArray> formatList = QImageReader::supportedImageFormats();
128    QString formats = tr("Images") + " (";
129    for (QList<QByteArray>::iterator it = formatList.begin(); it != formatList.end(); ++it){
130        formats += "*." + *it + " ";
131    }
132    formats.chop(1);
133    formats += ")";
134
135    QString filename = QFileDialog::getOpenFileName(this, tr("Open File"), "", formats);
136    if ((filename != "") && (QFileInfo(filename).exists())){
137        setGroupImageFromFile(filename);
138    }
139}
140
141void EditGroup::on_actionRemoveIcon_activated(){
142    setGroupImage(QImage());
143}
144
145
146// Members...
147
148void EditGroup::on_lstAvailableContacts_itemSelectionChanged(){
149    ui.btnAddMember->setEnabled(ui.lstAvailableContacts->selectedItems().count() > 0);
150}
151void EditGroup::on_lstMemberContacts_itemSelectionChanged(){
152    ui.btnRemoveMember->setEnabled(ui.lstMemberContacts->selectedItems().count() > 0);
153}
154
155void EditGroup::on_btnAddMember_clicked(){
156    if (ui.lstAvailableContacts->selectedItems().count() == 0){
157        return;
158    }
159
160    QListIterator<QListWidgetItem *> i(ui.lstAvailableContacts->selectedItems());
161    while (i.hasNext()){
162        QListWidgetItem* item = i.next();
163        ui.lstAvailableContacts->takeItem(ui.lstAvailableContacts->row(item));
164        ui.lstMemberContacts->addItem(item);
165        ui.lstMemberContacts->setCurrentItem(item, QItemSelectionModel::ClearAndSelect);
166    }
167}
168
169void EditGroup::on_lstAvailableContacts_itemDoubleClicked(QListWidgetItem*){
170    on_btnAddMember_clicked();
171}
172void EditGroup::on_lstMemberContacts_itemDoubleClicked(QListWidgetItem*){
173    on_btnRemoveMember_clicked();
174}
175
176void EditGroup::on_btnRemoveMember_clicked(){
177    if (ui.lstMemberContacts->selectedItems().count() == 0){
178        return;
179    }
180
181    QListIterator<QListWidgetItem *> i(ui.lstMemberContacts->selectedItems());
182    while (i.hasNext()){
183        QListWidgetItem* item = i.next();
184        ui.lstMemberContacts->takeItem(ui.lstMemberContacts->row(item));
185        ui.lstAvailableContacts->addItem(item);
186        ui.lstAvailableContacts->setCurrentItem(item, QItemSelectionModel::ClearAndSelect);
187    }
188}
189
190
191// Navigation...
192
193bool EditGroup::validate() {
194    ui.edtName->setText(ui.edtName->text().trimmed());
195
196    QString errorMessages = "";
197
198
199    // Validate the name
200    if (ui.edtName->text().isEmpty()) {
201        errorMessages += tr("Please enter a name for this group.") + "\n";
202    }
203
204    // Validate the name for double usage
205    QStringList reservedNames;
206    foreach (const SGroup *group, m_groupManager.groups()) {
207        if (group == m_group)
208            continue;
209
210        reservedNames.append(group->name());
211    }
212    foreach (const SContact *contact, m_contactManager.contacts()) {
213        reservedNames.append(contact->name());
214        reservedNames.append(contact->aliases());
215    }
216
217    if (reservedNames.contains(ui.edtName->text(), Qt::CaseInsensitive)) {
218        errorMessages += tr("The name of this group is already in use.") + "\n";
219    }
220
221
222    // Check for empty contact list
223    if (ui.lstMemberContacts->count() == 0) {
224        errorMessages += tr("The group should contain at least one contact.") + "\n";
225    }
226
227    // Show message if validation failed
228    errorMessages.remove(QRegExp("[\n]+$"));
229    if (!errorMessages.isEmpty()) {
230        QMessageBox::warning(this, tr("Edit group"), errorMessages, QMessageBox::Ok, QMessageBox::Ok);
231        return false;
232    }
233
234    return true;
235}
236
237void EditGroup::on_btnsNavigation_accepted(){
238    if (!validate()){
239        return;
240    }
241
242    m_group->setName(ui.edtName->text());
243
244    QSet<const SContact*> contacts;
245    for (int x = 0; x < ui.lstMemberContacts->count(); ++x){
246        contacts.insert(contactNameToContact_.value(ui.lstMemberContacts->item(x)->text()));
247    }
248    m_group->setContacts(contacts);
249
250    m_group->setImage(groupImage_);
251
252    done(QDialog::Accepted);
253}
254
255} // namespace UI
Note: See TracBrowser for help on using the repository browser.