squawk/ui/models/info/phones.cpp

226 lines
7.0 KiB
C++
Raw Permalink Normal View History

2019-11-06 15:14:49 +00:00
/*
* Squawk messenger.
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2023-02-01 15:56:00 +00:00
#include "phones.h"
2019-11-06 15:14:49 +00:00
#include "shared/icons.h"
2023-08-15 15:28:25 +00:00
#include "shared/defines.h"
#include <QCoreApplication>
2023-02-01 15:56:00 +00:00
Models::Phones::Phones(bool p_edit, QObject* parent):
2019-11-06 15:14:49 +00:00
QAbstractTableModel(parent),
edit(p_edit),
2023-02-01 15:56:00 +00:00
deque() {}
2019-11-06 15:14:49 +00:00
2023-02-01 15:56:00 +00:00
int Models::Phones::columnCount(const QModelIndex& parent) const {
2023-08-15 15:28:25 +00:00
SHARED_UNUSED(parent);
return 4;
}
2023-02-01 15:56:00 +00:00
int Models::Phones::rowCount(const QModelIndex& parent) const {
2023-08-15 15:28:25 +00:00
SHARED_UNUSED(parent);
return deque.size();
}
2019-11-06 15:14:49 +00:00
2023-02-01 15:56:00 +00:00
void Models::Phones::revertPreferred(quint32 row) {
setData(createIndex(row, 3), !isPreferred(row));
2019-11-06 15:14:49 +00:00
}
2023-02-01 15:56:00 +00:00
QString Models::Phones::getPhone(quint32 row) const {
return deque[row].number;}
QVariant Models::Phones::data(const QModelIndex& index, int role) const {
2019-11-06 15:14:49 +00:00
if (index.isValid()) {
int col = index.column();
switch (col) {
case 0:
switch (role) {
case Qt::DisplayRole:
case Qt::EditRole:
return deque[index.row()].number;
default:
return QVariant();
}
break;
case 1:
switch (role) {
case Qt::DisplayRole:
return QCoreApplication::translate("Global", Shared::VCard::Phone::roleNames[deque[index.row()].role].toStdString().c_str());
2019-11-06 15:14:49 +00:00
case Qt::EditRole:
return deque[index.row()].role;
default:
return QVariant();
}
break;
case 2:
switch (role) {
case Qt::DisplayRole:
return QCoreApplication::translate("Global", Shared::VCard::Phone::typeNames[deque[index.row()].type].toStdString().c_str());
2019-11-06 15:14:49 +00:00
case Qt::EditRole:
return deque[index.row()].type;
default:
return QVariant();
}
break;
case 3:
switch (role) {
case Qt::DisplayRole:
return QVariant();
case Qt::DecorationRole:
2023-08-15 15:28:25 +00:00
if (deque[index.row()].prefered)
2019-11-06 15:14:49 +00:00
return Shared::icon("favorite", false);
2023-08-15 15:28:25 +00:00
2019-11-06 15:14:49 +00:00
return QVariant();
default:
return QVariant();
}
break;
default:
return QVariant();
}
}
return QVariant();
}
2023-02-01 15:56:00 +00:00
QModelIndex Models::Phones::addNewEmptyLine() {
2019-11-06 15:14:49 +00:00
beginInsertRows(QModelIndex(), deque.size(), deque.size());
deque.emplace_back("", Shared::VCard::Phone::other);
endInsertRows();
return createIndex(deque.size() - 1, 0, &(deque.back()));
}
2023-02-01 15:56:00 +00:00
Qt::ItemFlags Models::Phones::flags(const QModelIndex& index) const {
2019-11-06 15:14:49 +00:00
Qt::ItemFlags f = QAbstractTableModel::flags(index);
2023-08-15 15:28:25 +00:00
if (edit && index.column() != 3)
2019-11-06 15:14:49 +00:00
f = Qt::ItemIsEditable | f;
2023-08-15 15:28:25 +00:00
2019-11-06 15:14:49 +00:00
return f;
}
2023-02-03 18:43:13 +00:00
bool Models::Phones::setEditable(bool editable) {
if (edit != editable) {
edit = editable;
if (deque.size() > 0) {
int lastRow = deque.size() - 1;
QModelIndex begin = createIndex(0, 0, &(deque[0]));
QModelIndex end = createIndex(lastRow, columnCount() - 1, &(deque[lastRow]));
emit dataChanged(begin, end);
}
return true;
}
return false;
}
2023-02-01 15:56:00 +00:00
bool Models::Phones::dropPrefered() {
2019-11-06 15:14:49 +00:00
bool dropped = false;
int i = 0;
for (Shared::VCard::Phone& phone : deque) {
if (phone.prefered) {
phone.prefered = false;
QModelIndex ci = createIndex(i, 2, &phone);
emit dataChanged(ci, ci);
dropped = true;
}
++i;
}
return dropped;
}
2023-02-01 15:56:00 +00:00
void Models::Phones::getPhones(std::deque<Shared::VCard::Phone>& phones) const {
2023-08-15 15:28:25 +00:00
for (const Shared::VCard::Phone& my : deque)
2019-11-06 15:14:49 +00:00
phones.emplace_back(my);
}
2023-02-01 15:56:00 +00:00
bool Models::Phones::isPreferred(quint32 row) const {
2023-08-15 15:28:25 +00:00
if (row < deque.size())
2019-11-06 15:14:49 +00:00
return deque[row].prefered;
2023-08-15 15:28:25 +00:00
else
2019-11-06 15:14:49 +00:00
return false;
}
2023-02-01 15:56:00 +00:00
void Models::Phones::removeLines(quint32 index, quint32 count) {
2019-11-06 15:14:49 +00:00
if (index < deque.size()) {
quint32 maxCount = deque.size() - index;
2023-08-15 15:28:25 +00:00
if (count > maxCount)
2019-11-06 15:14:49 +00:00
count = maxCount;
if (count > 0) {
beginRemoveRows(QModelIndex(), index, index + count - 1);
std::deque<Shared::VCard::Phone>::const_iterator itr = deque.begin() + index;
std::deque<Shared::VCard::Phone>::const_iterator end = itr + count;
deque.erase(itr, end);
endRemoveRows();
}
}
}
2023-02-01 15:56:00 +00:00
bool Models::Phones::setData(const QModelIndex& index, const QVariant& value, int role) {
2019-11-06 15:14:49 +00:00
if (role == Qt::EditRole && checkIndex(index)) {
Shared::VCard::Phone& item = deque[index.row()];
switch (index.column()) {
case 0:
item.number = value.toString();
return true;
case 1: {
quint8 newRole = value.toUInt();
2023-08-15 15:28:25 +00:00
if (newRole > Shared::VCard::Phone::work)
2019-11-06 15:14:49 +00:00
return false;
2023-08-15 15:28:25 +00:00
2019-11-06 15:14:49 +00:00
item.role = static_cast<Shared::VCard::Phone::Role>(newRole);
return true;
}
case 2: {
quint8 newType = value.toUInt();
2023-08-15 15:28:25 +00:00
if (newType > Shared::VCard::Phone::other)
2019-11-06 15:14:49 +00:00
return false;
2023-08-15 15:28:25 +00:00
2019-11-06 15:14:49 +00:00
item.type = static_cast<Shared::VCard::Phone::Type>(newType);
return true;
}
case 3: {
bool newDef = value.toBool();
if (newDef != item.prefered) {
if (newDef) {
//dropPrefered();
}
item.prefered = newDef;
return true;
}
}
}
return true;
}
return false;
}
2023-02-01 15:56:00 +00:00
void Models::Phones::setPhones(const std::deque<Shared::VCard::Phone>& phones) {
2023-08-15 15:28:25 +00:00
if (deque.size() > 0)
2019-11-06 15:14:49 +00:00
removeLines(0, deque.size());
2023-08-15 15:28:25 +00:00
2019-11-06 15:14:49 +00:00
if (phones.size() > 0) {
beginInsertRows(QModelIndex(), 0, phones.size() - 1);
2023-08-15 15:28:25 +00:00
for (const Shared::VCard::Phone& comming : phones)
2019-11-06 15:14:49 +00:00
deque.emplace_back(comming);
2023-08-15 15:28:25 +00:00
2019-11-06 15:14:49 +00:00
endInsertRows();
}
}