1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-03-15 07:29:15 +01:00

+ notify table reordering some changes

This commit is contained in:
Nickolay 2011-09-19 00:30:57 +03:00
parent 1665ae8299
commit 1af42e1b32
11 changed files with 350 additions and 153 deletions

View File

@ -78,21 +78,22 @@ public:
QStringList getMessageSequence() const { return _messageSequence; }
void setMessageSequence(QStringList sequence) { _messageSequence = sequence; }
QString getRepeatFlag() const { return _repeatString; }
void setRepeatFlag(QString value) { _repeatString = value; }
QString retryString() const { return _repeatString; }
void setRetryString(QString value) { _repeatString = value; }
bool getRepeatTimeout() const { return _repeatTimeout; }
void setRepeatTimeout(bool value) { _repeatTimeout = value; }
int lifetime() const { return _expireTimeout; }
void setLifetime(int value) { _expireTimeout = value; }
int getExpireTimeout() const { return _expireTimeout; }
void setExpireTimeout(int value) { _expireTimeout = value; }
bool getEnableFlag() const { return _enableFlag; }
void setEnableFlag(bool value) { _enableFlag = value; }
bool mute() const { return _mute; }
void setMute(bool value) { _mute = value; }
void saveState(QSettings* settings) const;
void restoreState(QSettings* settings);
void seriaize(QDataStream& stream);
void deseriaize(QDataStream& stream);
QString parseNotifyMessage();
QTimer* getTimer() const { return _timer; }
@ -120,8 +121,6 @@ private:
//! according to its priority
QTimer* _expireTimer;
QStringList _messageSequence;
//! path to folder with sound files
@ -149,20 +148,15 @@ private:
double _spinBoxValue;
QString _repeatString;
//! time when next notification must be fired
bool _repeatTimeout;
//! how often or what periodicaly notification should be played
int _repeatTimerValue;
QString _repeatString;
//! time after event occured till notification became invalid
//! and will be removed from list
int _expireTimeout;
//! enables/disables playing of current notification
bool _enableFlag;
bool _mute;
};
Q_DECLARE_METATYPE(NotificationItem*)

View File

@ -47,8 +47,8 @@ NotificationItem::NotificationItem(QObject *parent)
, _sayOrder("Never")
, _spinBoxValue(0)
, _repeatString("Repeat Instantly")
, _repeatTimeout(true)
, _expireTimeout(15)
, _mute(false)
{
_timer = NULL;
@ -71,8 +71,9 @@ void NotificationItem::copyTo(NotificationItem* that) const
that->_sayOrder = _sayOrder;
that->_spinBoxValue = _spinBoxValue;
that->_repeatString = _repeatString;
that->_repeatTimeout = _repeatTimeout;
that->_expireTimeout = _expireTimeout;
that->_mute = _mute;
}
@ -88,8 +89,10 @@ void NotificationItem::saveState(QSettings* settings) const
settings->setValue(QLatin1String("Sound2"), getSound2());
settings->setValue(QLatin1String("Sound3"), getSound3());
settings->setValue(QLatin1String("SayOrder"), getSayOrder());
settings->setValue(QLatin1String("Repeat"), getRepeatFlag());
settings->setValue(QLatin1String("ExpireTimeout"), getExpireTimeout());
settings->setValue(QLatin1String("Repeat"), retryString());
settings->setValue(QLatin1String("ExpireTimeout"), lifetime());
settings->setValue(QLatin1String("Mute"), mute());
}
void NotificationItem::restoreState(QSettings* settings)
@ -105,10 +108,45 @@ void NotificationItem::restoreState(QSettings* settings)
setSound3(settings->value(QLatin1String("Sound3"), tr("")).toString());
setSayOrder(settings->value(QLatin1String("SayOrder"), tr("")).toString());
setSpinBoxValue(settings->value(QLatin1String("ValueSpinBox"), tr("")).toDouble());
setRepeatFlag(settings->value(QLatin1String("Repeat"), tr("")).toString());
setExpireTimeout(settings->value(QLatin1String("ExpireTimeout"), tr("")).toInt());
setRetryString(settings->value(QLatin1String("Repeat"), tr("")).toString());
setLifetime(settings->value(QLatin1String("ExpireTimeout"), tr("")).toInt());
setMute(settings->value(QLatin1String("Mute"), tr("")).toInt());
}
void NotificationItem::seriaize(QDataStream& stream)
{
stream << this->_soundCollectionPath;
stream << this->_currentLanguage;
stream << this->_dataObject;
stream << this->_objectField;
stream << this->_dataValue;
stream << this->_sound1;
stream << this->_sound2;
stream << this->_sound3;
stream << this->_sayOrder;
stream << this->_spinBoxValue;
stream << this->_repeatString;
stream << this->_expireTimeout;
stream << this->_mute;
}
void NotificationItem::deseriaize(QDataStream& stream)
{
stream >> this->_soundCollectionPath;
stream >> this->_currentLanguage;
stream >> this->_dataObject;
stream >> this->_objectField;
stream >> this->_dataValue;
stream >> this->_sound1;
stream >> this->_sound2;
stream >> this->_sound3;
stream >> this->_sayOrder;
stream >> this->_spinBoxValue;
stream >> this->_repeatString;
stream >> this->_expireTimeout;
stream >> this->_mute;
}
void NotificationItem::startTimer(int value) {
if (!_timer) {

View File

@ -30,11 +30,15 @@
#include "notifytablemodel.h"
#include "notifylogging.h"
NotifyItemDelegate::NotifyItemDelegate(QStringList items, QObject* parent)
NotifyItemDelegate::NotifyItemDelegate(QObject* parent)
: QItemDelegate(parent)
, _parent(parent)
, _items(items)
{
_titles << "Repeat Once"
<< "Repeat Instantly"
<< "Repeat 10 seconds"
<< "Repeat 30 seconds"
<< "Repeat 1 minute";
}
@ -44,7 +48,7 @@ QWidget *NotifyItemDelegate::createEditor(QWidget* parent, const QStyleOptionVie
if (eREPEAT_VALUE == index.column()) {
QComboBox* editor = new QComboBox(parent);
editor->clear();
editor->addItems(_items);
editor->addItems(_titles);
return editor;
} else {
if (eEXPIRE_TIME == index.column()) {

View File

@ -37,7 +37,7 @@ class NotifyItemDelegate : public QItemDelegate
Q_OBJECT
public:
NotifyItemDelegate(QStringList items, QObject *parent = 0);
NotifyItemDelegate(QObject *parent = 0);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &,
const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
@ -51,7 +51,7 @@ private slots:
private:
QObject* _parent;
QStringList _items;
QStringList _titles;
};

View File

@ -28,15 +28,16 @@
#include "notifylogging.h"
#ifdef DEBUG_NOTIFIES
#ifdef DEBUG_NOTIFIES_ENABLE
QDebug qNotifyDebug()
#endif
#ifndef DEBUG_NOTIFIES
QNoDebug qNotifyDebug()
#endif
{
#ifdef DEBUG_NOTIFIES
return qDebug();
}
#endif
#ifndef DEBUG_NOTIFIES_ENABLE
QNoDebug qNotifyDebug()
{
return QNoDebug();
}
#endif

View File

@ -31,12 +31,16 @@
#include "QDebug.h"
#ifdef DEBUG_NOTIFIES
#define DEBUG_NOTIFIES_ENABLE
#ifdef DEBUG_NOTIFIES_ENABLE
QDebug qNotifyDebug();
#endif
#ifndef DEBUG_NOTIFIES
#ifndef DEBUG_NOTIFIES_ENABLE
QNoDebug qNotifyDebug();
#endif
#define qNotifyDebug_if(test) if(test) qNotifyDebug()
#endif // NOTIFYLOGGING_H

View File

@ -272,8 +272,8 @@ void SoundNotifyPlugin::appendNotification(UAVObject *object)
if (nowPlayingConfiguration == ntf)
continue;
if (ntf->getRepeatFlag()!= "Repeat Instantly" &&
ntf->getRepeatFlag()!= "Repeat Once" && !ntf->firstStart)
if (ntf->retryString() != "Repeat Instantly" &&
ntf->retryString() != "Repeat Once" && !ntf->firstStart)
continue;
checkNotificationRule(ntf,object);
@ -288,46 +288,47 @@ void SoundNotifyPlugin::checkNotificationRule(NotificationItem* notification, UA
double threshold;
QString direction;
QString fieldName;
threshold = notification->getSpinBoxValue();
direction = notification->getValue();
fieldName = notification->getObjectField();
UAVObjectField* field = object->getField(fieldName);
if (field->getName()!="") {
double value = field->getDouble();
switch(direction[0].toAscii())
{
case 'E':
if (value==threshold)
condition = true;
break;
case 'G':
if (value>threshold)
condition = true;
break;
case 'L':
if (value<threshold)
condition = true;
break;
}
if (field->getName() == "")
return;
double value = field->getDouble();
switch(direction[0].toAscii())
{
case 'E':
condition = (value == threshold);
break;
case 'G':
condition = (value > threshold);
break;
case 'L':
condition = (value < threshold);
break;
}
if (condition)
{
if (!playNotification(notification))
{
if (!pendingNotifications.contains(notification))
{
notification->stopTimer();
if (!condition)
return;
qNotifyDebug() << "add to pending list - " << notification->parseNotifyMessage();
// if audio is busy, start expiration timer
//ms = (notification->getExpiredTimeout()[in sec])*1000
//QxtTimer::singleShot(notification->getExpireTimeout()*1000, this, SLOT(expirationTimerHandler(NotificationItem*)), qVariantFromValue(notification));
pendingNotifications.append(notification);
notification->startExpireTimer();
connect(notification->getExpireTimer(), SIGNAL(timeout()), this, SLOT(expireTimerHandler()));
}
if (!playNotification(notification))
{
if (!pendingNotifications.contains(notification))
{
notification->stopTimer();
qNotifyDebug() << "add to pending list - " << notification->parseNotifyMessage();
// if audio is busy, start expiration timer
//ms = (notification->getExpiredTimeout()[in sec])*1000
//QxtTimer::singleShot(notification->getExpireTimeout()*1000, this, SLOT(expirationTimerHandler(NotificationItem*)), qVariantFromValue(notification));
pendingNotifications.append(notification);
notification->startExpireTimer();
connect(notification->getExpireTimer(), SIGNAL(timeout()), this, SLOT(expireTimerHandler()));
}
}
}
@ -338,26 +339,26 @@ bool SoundNotifyPlugin::playNotification(NotificationItem* notification)
if (phonon.mo == NULL)
return false;
if (!notification->getEnableFlag()) return true;
if (!notification->mute())
return true;
qNotifyDebug() << "Phonon State: " << phonon.mo->state();
if ((phonon.mo->state()==Phonon::PausedState)
|| (phonon.mo->state()==Phonon::StoppedState)
|| phonon.firstPlay)
|| (phonon.mo->state()==Phonon::StoppedState)
|| phonon.firstPlay)
{
// don't fire expire timer
nowPlayingConfiguration = notification;
notification->stopExpireTimer();
if (notification->getRepeatFlag()=="Repeat Once") {
if (notification->retryString() == "Repeat Once") {
removedNotifies.append(lstNotifications.takeAt(lstNotifications.indexOf(notification)));
} else {
if (notification->getRepeatFlag()!="Repeat Instantly")
{
if (notification->retryString() != "Repeat Instantly") {
QRegExp rxlen("(\\d+)");
QString value;
int timer_value;
int pos = rxlen.indexIn(notification->getRepeatFlag());
int pos = rxlen.indexIn(notification->retryString());
if (pos > -1) {
value = rxlen.cap(1); // "189"
timer_value = (value.toInt()+8)*1000; //ms*1000 + average duration of meassage
@ -379,10 +380,9 @@ bool SoundNotifyPlugin::playNotification(NotificationItem* notification)
phonon.mo->play();
phonon.firstPlay = false; // On Linux, you sometimes have to nudge Phonon to play 1 time before
// the state is not "Loading" anymore.
}
else
} else {
return false; // if audio is busy
}
return true;
}

View File

@ -43,13 +43,13 @@
#include "notifyplugin.h"
#include "notifyitemdelegate.h"
#include "notifytablemodel.h"
#include "notifylogging.h"
NotifyPluginOptionsPage::NotifyPluginOptionsPage(/*NotificationItem *config,*/ QObject *parent)
: IOptionsPage(parent)
, objManager(*ExtensionSystem::PluginManager::instance()->getObject<UAVObjectManager>())
, owner(qobject_cast<SoundNotifyPlugin*>(parent))
, currentCollectionPath("")
, privListNotifications((qobject_cast<SoundNotifyPlugin*>(parent))->getListNotifications())
{
}
@ -66,15 +66,8 @@ QWidget *NotifyPluginOptionsPage::createPage(QWidget *parent)
//main layout
options_page->setupUi(optionsPageWidget);
delegateItems.clear();
listSoundFiles.clear();
delegateItems << "Repeat Once"
<< "Repeat Instantly"
<< "Repeat 10 seconds"
<< "Repeat 30 seconds"
<< "Repeat 1 minute";
options_page->SoundDirectoryPathChooser->setExpectedKind(Utils::PathChooser::Directory);
options_page->SoundDirectoryPathChooser->setPromptDialogTitle(tr("Choose sound collection directory"));
@ -95,19 +88,13 @@ QWidget *NotifyPluginOptionsPage::createPage(QWidget *parent)
//connect(this, SIGNAL(resetNotification()),owner, SLOT(resetNotification()));
privListNotifications.clear();
for (int i = 0; i < owner->getListNotifications().size(); ++i) {
NotificationItem* notification = new NotificationItem();
owner->getListNotifications().at(i)->copyTo(notification);
privListNotifications.append(notification);
}
// privListNotifications = ((qobject_cast<SoundNotifyPlugin*>(parent))->getListNotifications());
privListNotifications = owner->getListNotifications();
updateConfigView(owner->getCurrentNotification());
initRulesTable();
initButtons();
initRulesTableModel();
initRulesTableView();
initPhononPlayer();
return optionsPageWidget;
@ -129,31 +116,32 @@ void NotifyPluginOptionsPage::initButtons()
void NotifyPluginOptionsPage::initPhononPlayer()
{
notifySound = Phonon::createPlayer(Phonon::NotificationCategory);
connect(notifySound,SIGNAL(stateChanged(Phonon::State,Phonon::State)),
notifySound.reset(Phonon::createPlayer(Phonon::NotificationCategory));
connect(notifySound.data(),SIGNAL(stateChanged(Phonon::State,Phonon::State)),
this,SLOT(changeButtonText(Phonon::State,Phonon::State)));
connect(notifySound, SIGNAL(finished(void)), this, SLOT(onFinishedPlaying(void)));
connect(notifySound.data(), SIGNAL(finished(void)), this, SLOT(onFinishedPlaying(void)));
}
void NotifyPluginOptionsPage::initRulesTableModel()
void NotifyPluginOptionsPage::initRulesTable()
{
notifyRulesModel.reset(new NotifyTableModel(&privListNotifications));
notifyRulesSelection = new QItemSelectionModel(notifyRulesModel.data());
connect(notifyRulesSelection, SIGNAL(selectionChanged ( const QItemSelection &, const QItemSelection & )),
qNotifyDebug_if(_notifyRulesModel.isNull()) << "_notifyRulesModel.isNull())";
qNotifyDebug_if(!_notifyRulesSelection) << "_notifyRulesSelection.isNull())";
//QItemSelectionModel* selection = _notifyRulesSelection.take();
_notifyRulesModel.reset(new NotifyTableModel(privListNotifications));
_notifyRulesSelection = new QItemSelectionModel(_notifyRulesModel.data());
connect(_notifyRulesSelection, SIGNAL(selectionChanged ( const QItemSelection &, const QItemSelection & )),
this, SLOT(on_tableNotification_changeSelection( const QItemSelection & , const QItemSelection & )));
connect(this, SIGNAL(entryUpdated(int)),
notifyRulesModel.data(), SLOT(entryUpdated(int)));
_notifyRulesModel.data(), SLOT(entryUpdated(int)));
connect(this, SIGNAL(entryAdded(int)),
notifyRulesModel.data(), SLOT(entryAdded(int)));
}
_notifyRulesModel.data(), SLOT(entryAdded(int)));
options_page->notifyRulesView->setModel(_notifyRulesModel.data());
options_page->notifyRulesView->setSelectionModel(_notifyRulesSelection);
options_page->notifyRulesView->setItemDelegate(new NotifyItemDelegate(this));
void NotifyPluginOptionsPage::initRulesTableView()
{
options_page->notifyRulesView->setModel(notifyRulesModel.data());
options_page->notifyRulesView->resizeRowsToContents();
options_page->notifyRulesView->setSelectionModel(notifyRulesSelection);
options_page->notifyRulesView->setItemDelegate(new NotifyItemDelegate(delegateItems,this));
options_page->notifyRulesView->setColumnWidth(eMESSAGE_NAME,200);
options_page->notifyRulesView->setColumnWidth(eREPEAT_VALUE,120);
options_page->notifyRulesView->setColumnWidth(eEXPIRE_TIME,100);
@ -161,6 +149,9 @@ void NotifyPluginOptionsPage::initRulesTableView()
options_page->notifyRulesView->setDragEnabled(true);
options_page->notifyRulesView->setAcceptDrops(true);
options_page->notifyRulesView->setDropIndicatorShown(true);
options_page->notifyRulesView->setDragDropMode(QAbstractItemView::InternalMove);
}
void NotifyPluginOptionsPage::getOptionsPageValues(NotificationItem* notification)
@ -175,7 +166,6 @@ void NotifyPluginOptionsPage::getOptionsPageValues(NotificationItem* notificatio
notification->setSayOrder(options_page->SayOrder->currentText());
notification->setValue(options_page->Value->currentText());
notification->setSpinBoxValue(options_page->ValueSpinBox->value());
}
/*!
@ -191,7 +181,7 @@ void NotifyPluginOptionsPage::apply()
void NotifyPluginOptionsPage::finish()
{
disconnect(notifySound,SIGNAL(stateChanged(Phonon::State,Phonon::State)),
disconnect(notifySound.data(),SIGNAL(stateChanged(Phonon::State,Phonon::State)),
this,SLOT(changeButtonText(Phonon::State,Phonon::State)));
if (notifySound) {
notifySound->stop();
@ -272,10 +262,10 @@ void NotifyPluginOptionsPage::on_buttonTestSoundNotification_clicked()
{
NotificationItem* notification = NULL;
if (-1 == notifyRulesSelection->currentIndex().row())
if (-1 == _notifyRulesSelection->currentIndex().row())
return;
notifySound->clearQueue();
notification = privListNotifications.at(notifyRulesSelection->currentIndex().row());
notification = privListNotifications.at(_notifyRulesSelection->currentIndex().row());
notification->parseNotifyMessage();
QStringList sequence = notification->getMessageSequence();
Q_ASSERT(!!sequence.size());
@ -419,17 +409,17 @@ if ( ((!options_page->Sound2->currentText().size()) && (options_page->SayOrder->
}
privListNotifications.append(notification);
emit entryAdded(privListNotifications.size() - 1);
notifyRulesSelection->setCurrentIndex(notifyRulesModel->index(privListNotifications.size()-1,0,QModelIndex()),
_notifyRulesSelection->setCurrentIndex(_notifyRulesModel->index(privListNotifications.size()-1,0,QModelIndex()),
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
}
void NotifyPluginOptionsPage::on_buttonDeleteNotification_clicked()
{
notifyRulesModel->removeRow(notifyRulesSelection->currentIndex().row());
if (!notifyRulesModel->rowCount()
&& (notifyRulesSelection->currentIndex().row() > 0
&& notifyRulesSelection->currentIndex().row() < notifyRulesModel->rowCount()) )
_notifyRulesModel->removeRow(_notifyRulesSelection->currentIndex().row());
if (!_notifyRulesModel->rowCount()
&& (_notifyRulesSelection->currentIndex().row() > 0
&& _notifyRulesSelection->currentIndex().row() < _notifyRulesModel->rowCount()) )
{
options_page->buttonDelete->setEnabled(false);
options_page->buttonModify->setEnabled(false);
@ -442,9 +432,12 @@ void NotifyPluginOptionsPage::on_buttonModifyNotification_clicked()
{
NotificationItem* notification = new NotificationItem;
getOptionsPageValues(notification);
notification->setRepeatFlag(privListNotifications.at(notifyRulesSelection->currentIndex().row())->getRepeatFlag());
privListNotifications.replace(notifyRulesSelection->currentIndex().row(),notification);
entryUpdated(notifyRulesSelection->currentIndex().row());
notification->setRetryString(privListNotifications.at(_notifyRulesSelection->currentIndex().row())->retryString());
notification->setLifetime(privListNotifications.at(_notifyRulesSelection->currentIndex().row())->lifetime());
notification->setMute(privListNotifications.at(_notifyRulesSelection->currentIndex().row())->mute());
privListNotifications.replace(_notifyRulesSelection->currentIndex().row(),notification);
entryUpdated(_notifyRulesSelection->currentIndex().row());
}

View File

@ -42,13 +42,15 @@
#include <phonon/Path>
#include <phonon/AudioOutput>
#include <phonon/Global>
#include "ui_notifypluginoptionspage.h"
//#include "notifytablemodel.h"
class NotifyTableModel;
class NotificationItem;
class SoundNotifyPlugin;
namespace Ui {
class NotifyPluginOptionsPage;
};
using namespace Core;
@ -74,11 +76,11 @@ public:
void getOptionsPageValues(NotificationItem* notification);
private:
//Q_DISABLE_COPY(NotifyPluginOptionsPage)
Q_DISABLE_COPY(NotifyPluginOptionsPage)
void initButtons();
void initPhononPlayer();
void initRulesTableModel();
void initRulesTableView();
void initRulesTable();
private:
UAVObjectManager& objManager;
@ -86,14 +88,13 @@ private:
QStringList listDirCollections;
QStringList listSoundFiles;
QString currentCollectionPath;
int sizeNotifyList;
Phonon::MediaObject *sound1;
Phonon::MediaObject *sound2;
Phonon::MediaObject *notifySound;
QScopedPointer<Phonon::MediaObject> notifySound;
Phonon::AudioOutput *audioOutput;
QStringList delegateItems;
QScopedPointer<NotifyTableModel> notifyRulesModel;
QItemSelectionModel *notifyRulesSelection;
QScopedPointer<NotifyTableModel> _notifyRulesModel;
QItemSelectionModel* _notifyRulesSelection;
QList<NotificationItem*> privListNotifications;
QScopedPointer<Ui::NotifyPluginOptionsPage> options_page;

View File

@ -26,28 +26,41 @@
*/
#include "notifytablemodel.h"
#include "notifylogging.h"
#include <qdebug.h>
#include <QMimeData>
NotifyTableModel::NotifyTableModel(QList<NotificationItem*>* parentList, QObject* parent)
static int _dragStartRow = -1;
static int _dragNumRows = -1;
const char* mime_type_notify_table = "openpilot/notify_plugin_table";
NotifyTableModel::NotifyTableModel(QList<NotificationItem*>& parentList, QObject* parent)
: QAbstractTableModel(parent)
, _list(parentList)
{
_headerStrings << "Name" << "Repeats" << "Lifetime,sec" << "Enable";
_list.reset(parentList);
_headerStrings << "Name" << "Repeats" << "Lifetime,sec" << "Mute";
connect(this, SIGNAL(dragRows(int, int)), this, SLOT(dropRows(int, int)));
}
bool NotifyTableModel::setData(const QModelIndex &index,
const QVariant &value, int role)
{
if (index.isValid() && role == Qt::DisplayRole) {
if(eMESSAGE_NAME == index.column()) {
emit dataChanged(index, index);
return true;
}
}
if (index.isValid() && role == Qt::EditRole) {
if(eREPEAT_VALUE == index.column())
_list->at(index.row())->setRepeatFlag(value.toString());
_list.at(index.row())->setRetryString(value.toString());
else {
if(eEXPIRE_TIME == index.column())
_list->at(index.row())->setExpireTimeout(value.toInt());
_list.at(index.row())->setLifetime(value.toInt());
else {
if(eENABLE_NOTIFICATION == index.column())
_list->at(index.row())->setEnableFlag(value.toBool());
_list.at(index.row())->setMute(value.toBool());
}
}
emit dataChanged(index, index);
@ -63,7 +76,7 @@ QVariant NotifyTableModel::data(const QModelIndex &index, int role) const
return QVariant();
}
if (index.row() >= _list->size())
if (index.row() >= _list.size())
return QVariant();
if (role == Qt::DisplayRole || role == Qt::EditRole)
@ -71,16 +84,16 @@ QVariant NotifyTableModel::data(const QModelIndex &index, int role) const
switch(index.column())
{
case eMESSAGE_NAME:
return _list->at(index.row())->parseNotifyMessage();
return _list.at(index.row())->parseNotifyMessage();
case eREPEAT_VALUE:
return _list->at(index.row())->getRepeatFlag();
return _list.at(index.row())->retryString();
case eEXPIRE_TIME:
return _list->at(index.row())->getExpireTimeout();
return _list.at(index.row())->lifetime();
case eENABLE_NOTIFICATION:
return _list->at(index.row())->getEnableFlag();
return _list.at(index.row())->mute();
default:
return QVariant();
@ -112,7 +125,16 @@ QVariant NotifyTableModel::headerData(int section, Qt::Orientation orientation,
bool NotifyTableModel::insertRows(int position, int rows, const QModelIndex& index)
{
Q_UNUSED(index);
beginInsertRows(QModelIndex(), position, position+rows-1);
if((-1 == position) || (-1 == rows) )
return false;
beginInsertRows(QModelIndex(), position, position + rows - 1);
for (int row = 0; row < rows; ++row) {
_list.insert(position, new NotificationItem());
}
endInsertRows();
return true;
}
@ -120,10 +142,14 @@ bool NotifyTableModel::insertRows(int position, int rows, const QModelIndex& ind
bool NotifyTableModel::removeRows(int position, int rows, const QModelIndex& index)
{
Q_UNUSED(index);
beginRemoveRows(QModelIndex(), position, position+rows-1);
for (int row=0; row < rows; ++row) {
_list->removeAt(position);
if((-1 == position) || (-1 == rows) )
return false;
beginRemoveRows(QModelIndex(), position, position + rows - 1);
for (int row = 0; row < rows; ++row) {
_list.removeAt(position);
}
endRemoveRows();
@ -140,3 +166,122 @@ void NotifyTableModel::entryAdded(int position)
{
insertRows(position, 1,QModelIndex());
}
bool NotifyTableModel::dropMimeData( const QMimeData * data, Qt::DropAction action, int row,
int column, const QModelIndex& parent)
{
if (action == Qt::IgnoreAction)
return true;
if (!data->hasFormat(mime_type_notify_table))
return false;
int beginRow = -1;
if (row != -1)
beginRow = row;
else {
if (parent.isValid())
beginRow = parent.row();
else
beginRow = rowCount(QModelIndex());
}
if(-1 == beginRow)
return false;
QByteArray encodedData = data->data(mime_type_notify_table);
QDataStream stream(&encodedData, QIODevice::ReadOnly);
int rows = beginRow;
while(!stream.atEnd()) {
qint32 ptr;
stream >> ptr;
NotificationItem* item = reinterpret_cast<NotificationItem*>(ptr);
int dragged = _list.indexOf(item);
if(-1 == dragged || rows >= _list.size() || dragged == rows) {
qNotifyDebug() << "no such item";
return false;
}
removeRows(rows, 1, QModelIndex());
insertRows(rows, 1, QModelIndex());
_list.replace(dragged, item);
// _list.swap(dragged, rows);
++rows;
};
QModelIndex idxTopLeft = index(beginRow, 0, QModelIndex());
QModelIndex idxBotRight = index(beginRow, columnCount(QModelIndex()), QModelIndex());
emit dataChanged(idxTopLeft, idxBotRight);
//QStringList newItems;
//removeRows(_dragStartRow, _dragNumRows, QModelIndex());
//insertRows(beginRow, rows, QModelIndex());
// int rows = beginRow;
// while (!stream.atEnd()) {
// _list.at(index.row())->deserialize(stream);
// ++rows;
// }
// while(rows) {
// int column = 0;
// foreach (const QString& text, newItems) {
// QModelIndex idx = index(beginRow, column, QModelIndex());
// if(!column)
// setData(const_cast<const QModelIndex&>(idx), text, Qt::DisplayRole);
// else
// setData(const_cast<const QModelIndex&>(idx), text, Qt::EditRole);
// ++column;
// }
// ++beginRow;
// --rows;
// }
return true;
}
QMimeData* NotifyTableModel::mimeData(const QModelIndexList& indexes) const
{
QMimeData* mimeData = new QMimeData();
QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly);
int rows = 0;
foreach (const QModelIndex& index, indexes) {
if(!index.column()) {
qint32 item = reinterpret_cast<qint32>(_list.at(index.row()));
stream << item;
}
++rows;
}
// int numRows = 0;
// foreach (const QModelIndex& index, indexes) {
// if (index.isValid() && index.column()) {
// _list.at(index.row())->serialize(stream);
//// if(!index.column()) {
//// numRows++;
//// QString name = data(index, Qt::DisplayRole).toString();
//// stream << name;
//// } else {
//// QString text = data(index, Qt::EditRole).toString();
//// stream << text;
//// }
// }
// }
mimeData->setData(mime_type_notify_table, encodedData);
//emit dragRows(indexes.at(0).row(), rows);
dropRows(indexes.at(0).row(), rows);
//_dragStartRow = indexes.at(0).row();
//_dragNumRows = 1/*numRows*/;
return mimeData;
}
void NotifyTableModel::dropRows(int position, int count) const
{
for (int row = 0; row < count; ++row) {
_list.removeAt(position);
}
}

View File

@ -43,11 +43,11 @@ class NotifyTableModel : public QAbstractTableModel
public:
NotifyTableModel(QList<NotificationItem*>* parentList, QObject* parent = 0);
NotifyTableModel(QList<NotificationItem*>& parentList, QObject* parent = 0);
int rowCount(const QModelIndex& parent = QModelIndex()) const
{
return _list->count();
return _list.count();
}
int columnCount(const QModelIndex &/*parent*/) const
@ -68,17 +68,34 @@ public:
return Qt::MoveAction;
}
QStringList mimeTypes() const
{
QStringList types;
types << "application/vnd.text.list";
return types;
}
bool dropMimeData( const QMimeData * data, Qt::DropAction action, int row,
int column, const QModelIndex& parent);
QMimeData* mimeData(const QModelIndexList &indexes) const;
bool setData(const QModelIndex &index, const QVariant &value, int role);
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
bool insertRows(int position, int rows, const QModelIndex &index);
bool removeRows(int position, int rows, const QModelIndex &index);
signals:
void dragRows(int position, int count);
private slots:
void entryUpdated(int offset);
void entryAdded(int position);
void dropRows(int position, int count) const;
private:
QScopedPointer<QList<NotificationItem*> > _list;
mutable QList<NotificationItem*>& _list;
QStringList _headerStrings;
};