2013-04-05 22:46:56 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file notifytablemodel.cpp
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @brief
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
* @defgroup notifyplugin
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* 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, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "notifytablemodel.h"
|
|
|
|
#include "notifylogging.h"
|
|
|
|
#include <qdebug.h>
|
|
|
|
#include <QMimeData>
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
const char *mime_type_notify_table = "openpilot/notify_plugin_table";
|
2013-04-05 22:46:56 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
NotifyTableModel::NotifyTableModel(QList<NotificationItem *> & parentList, QObject *parent)
|
|
|
|
: QAbstractTableModel(parent)
|
|
|
|
, _list(parentList)
|
2013-04-05 22:46:56 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
_headerStrings << "Name" << "Repeats" << "Lifetime,sec" << "Mute";
|
|
|
|
connect(this, SIGNAL(dragRows(int, int)), this, SLOT(dropRows(int, int)));
|
2013-04-05 22:46:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool NotifyTableModel::setData(const QModelIndex &index,
|
2013-05-19 16:37:30 +02:00
|
|
|
const QVariant &value, int role)
|
2013-04-05 22:46:56 +02:00
|
|
|
{
|
|
|
|
if (index.isValid() && role == Qt::DisplayRole) {
|
|
|
|
if (eMessageName == index.column()) {
|
|
|
|
emit dataChanged(index, index);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (index.isValid() && role == Qt::EditRole) {
|
2013-05-19 16:37:30 +02:00
|
|
|
if (eRepeatValue == index.column()) {
|
|
|
|
_list.at(index.row())->setRetryValue(NotificationItem::retryValues.indexOf(value.toString()));
|
|
|
|
} else {
|
|
|
|
if (eExpireTimer == index.column()) {
|
2013-04-05 22:46:56 +02:00
|
|
|
_list.at(index.row())->setLifetime(value.toInt());
|
2013-05-19 16:37:30 +02:00
|
|
|
} else {
|
|
|
|
if (eTurnOn == index.column()) {
|
2013-04-05 22:46:56 +02:00
|
|
|
_list.at(index.row())->setMute(value.toBool());
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2013-04-05 22:46:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
emit dataChanged(index, index);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant NotifyTableModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid()) {
|
|
|
|
qWarning() << "NotifyTableModel::data - index.isValid()";
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (index.row() >= _list.size()) {
|
2013-04-05 22:46:56 +02:00
|
|
|
return QVariant();
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2013-04-05 22:46:56 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (role == Qt::DisplayRole || role == Qt::EditRole) {
|
|
|
|
switch (index.column()) {
|
2013-04-05 22:46:56 +02:00
|
|
|
case eMessageName:
|
|
|
|
return _list.at(index.row())->toString();
|
|
|
|
|
|
|
|
case eRepeatValue:
|
2013-05-19 16:37:30 +02:00
|
|
|
return NotificationItem::retryValues.at(_list.at(index.row())->retryValue());
|
2013-04-05 22:46:56 +02:00
|
|
|
|
|
|
|
case eExpireTimer:
|
|
|
|
return _list.at(index.row())->lifetime();
|
|
|
|
|
|
|
|
case eTurnOn:
|
|
|
|
return _list.at(index.row())->mute();
|
|
|
|
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
2013-05-19 16:37:30 +02:00
|
|
|
} else {
|
|
|
|
if (Qt::SizeHintRole == role) {
|
|
|
|
return QVariant(10);
|
2013-04-05 22:46:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant NotifyTableModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (role != Qt::DisplayRole) {
|
2013-04-05 22:46:56 +02:00
|
|
|
return QVariant();
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2013-04-05 22:46:56 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (orientation == Qt::Horizontal) {
|
2013-04-05 22:46:56 +02:00
|
|
|
return _headerStrings.at(section);
|
2013-05-19 16:37:30 +02:00
|
|
|
} else if (orientation == Qt::Vertical) {
|
|
|
|
return QString("%1").arg(section);
|
|
|
|
}
|
2013-04-05 22:46:56 +02:00
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
bool NotifyTableModel::insertRows(int position, int rows, const QModelIndex & index)
|
2013-04-05 22:46:56 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(index);
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (-1 == position || -1 == rows) {
|
2013-04-05 22:46:56 +02:00
|
|
|
return false;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2013-04-05 22:46:56 +02:00
|
|
|
|
|
|
|
beginInsertRows(QModelIndex(), position, position + rows - 1);
|
|
|
|
|
|
|
|
for (int i = 0; i < rows; ++i) {
|
|
|
|
_list.insert(position + i, new NotificationItem());
|
|
|
|
}
|
|
|
|
|
|
|
|
endInsertRows();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
bool NotifyTableModel::removeRows(int position, int rows, const QModelIndex & index)
|
2013-04-05 22:46:56 +02:00
|
|
|
{
|
|
|
|
Q_UNUSED(index);
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if ((-1 == position) || (-1 == rows)) {
|
2013-04-05 22:46:56 +02:00
|
|
|
return false;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2013-04-05 22:46:56 +02:00
|
|
|
|
|
|
|
beginRemoveRows(QModelIndex(), position, position + rows - 1);
|
|
|
|
|
|
|
|
for (int row = 0; row < rows; ++row) {
|
|
|
|
_list.removeAt(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
endRemoveRows();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotifyTableModel::entryUpdated(int offset)
|
|
|
|
{
|
|
|
|
QModelIndex idx = index(offset, 0);
|
|
|
|
emit dataChanged(idx, idx);
|
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
void NotifyTableModel::entryAdded(NotificationItem *item)
|
2013-04-05 22:46:56 +02:00
|
|
|
{
|
|
|
|
insertRows(rowCount(), 1, QModelIndex());
|
2013-05-19 16:37:30 +02:00
|
|
|
NotificationItem *tmp = _list.at(rowCount() - 1);
|
2013-04-05 22:46:56 +02:00
|
|
|
_list.replace(rowCount() - 1, item);
|
|
|
|
delete tmp;
|
|
|
|
entryUpdated(rowCount() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::DropActions NotifyTableModel::supportedDropActions() const
|
|
|
|
{
|
|
|
|
return Qt::MoveAction;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList NotifyTableModel::mimeTypes() const
|
|
|
|
{
|
|
|
|
QStringList types;
|
2013-05-19 16:37:30 +02:00
|
|
|
|
2013-04-05 22:46:56 +02:00
|
|
|
types << mime_type_notify_table;
|
|
|
|
return types;
|
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
bool NotifyTableModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row,
|
|
|
|
int column, const QModelIndex & parent)
|
2013-04-05 22:46:56 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (action == Qt::IgnoreAction) {
|
2013-04-05 22:46:56 +02:00
|
|
|
return true;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2013-04-05 22:46:56 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!data->hasFormat(mime_type_notify_table)) {
|
2013-04-05 22:46:56 +02:00
|
|
|
return false;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2013-04-05 22:46:56 +02:00
|
|
|
|
|
|
|
int beginRow = -1;
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (row != -1) {
|
2013-04-05 22:46:56 +02:00
|
|
|
beginRow = row;
|
2013-05-19 16:37:30 +02:00
|
|
|
} else {
|
|
|
|
if (parent.isValid()) {
|
2013-04-05 22:46:56 +02:00
|
|
|
beginRow = parent.row();
|
2013-05-19 16:37:30 +02:00
|
|
|
} else {
|
2013-04-05 22:46:56 +02:00
|
|
|
beginRow = rowCount(QModelIndex());
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2013-04-05 22:46:56 +02:00
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (-1 == beginRow) {
|
2013-04-05 22:46:56 +02:00
|
|
|
return false;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2013-04-05 22:46:56 +02:00
|
|
|
|
|
|
|
QByteArray encodedData = data->data(mime_type_notify_table);
|
|
|
|
QDataStream stream(&encodedData, QIODevice::ReadOnly);
|
|
|
|
int rows = beginRow;
|
|
|
|
// read next item from input MIME and drop into the table line by line
|
2013-05-19 16:37:30 +02:00
|
|
|
while (!stream.atEnd()) {
|
2013-04-05 22:46:56 +02:00
|
|
|
quintptr ptr;
|
|
|
|
stream >> ptr;
|
2013-05-19 16:37:30 +02:00
|
|
|
NotificationItem *item = reinterpret_cast<NotificationItem *>(ptr);
|
2013-04-05 22:46:56 +02:00
|
|
|
int dragged = _list.indexOf(item);
|
|
|
|
// we can drag item from top rows to bottom (DOWN_DIRECTION),
|
|
|
|
// or from bottom rows to top rows (UP_DIRECTION)
|
|
|
|
enum { UP_DIRECTION, DOWN_DIRECTION };
|
|
|
|
int direction = (dragged < rows) ? DOWN_DIRECTION : (dragged += 1, UP_DIRECTION);
|
|
|
|
// check drop bounds
|
2013-05-19 16:37:30 +02:00
|
|
|
if (dragged < 0 || ((dragged + 1) >= _list.size() && direction == DOWN_DIRECTION) || dragged == rows) {
|
2013-04-05 22:46:56 +02:00
|
|
|
qNotifyDebug() << "no such item";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// addiional check in case dropping of multiple rows
|
2013-05-19 16:37:30 +02:00
|
|
|
if (rows + direction > _list.size()) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-04-05 22:46:56 +02:00
|
|
|
|
|
|
|
bool success = insertRows(rows + direction, 1, QModelIndex());
|
|
|
|
Q_ASSERT(success);
|
|
|
|
_list.replace(rows + direction, item);
|
|
|
|
success = removeRows(dragged, 1, QModelIndex());
|
|
|
|
Q_ASSERT(success);
|
2013-05-19 16:37:30 +02:00
|
|
|
if (direction == UP_DIRECTION) {
|
2013-04-05 22:46:56 +02:00
|
|
|
++rows;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
;
|
2013-04-05 22:46:56 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
QModelIndex idxTopLeft = index(beginRow, 0, QModelIndex());
|
2013-04-05 22:46:56 +02:00
|
|
|
QModelIndex idxBotRight = index(beginRow, columnCount(QModelIndex()), QModelIndex());
|
|
|
|
emit dataChanged(idxTopLeft, idxBotRight);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
QMimeData *NotifyTableModel::mimeData(const QModelIndexList & indexes) const
|
2013-04-05 22:46:56 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
QMimeData *mimeData = new QMimeData();
|
2013-04-05 22:46:56 +02:00
|
|
|
QByteArray encodedData;
|
|
|
|
|
|
|
|
QDataStream stream(&encodedData, QIODevice::WriteOnly);
|
|
|
|
int rows = 0;
|
2013-05-19 16:37:30 +02:00
|
|
|
|
|
|
|
foreach(const QModelIndex &index, indexes) {
|
2013-04-05 22:46:56 +02:00
|
|
|
if (!index.column()) {
|
|
|
|
quintptr item = reinterpret_cast<quintptr>(_list.at(index.row()));
|
|
|
|
stream << item;
|
|
|
|
++rows;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mimeData->setData(mime_type_notify_table, encodedData);
|
|
|
|
return mimeData;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotifyTableModel::dropRows(int position, int count) const
|
|
|
|
{
|
|
|
|
for (int row = 0; row < count; ++row) {
|
|
|
|
_list.removeAt(position);
|
|
|
|
}
|
|
|
|
}
|