dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
notifytablemodel.cpp
Go to the documentation of this file.
1 
16 /*
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful, but
23  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25  * for more details.
26  *
27  * You should have received a copy of the GNU General Public License along
28  * with this program; if not, see <http://www.gnu.org/licenses/>
29  */
30 
31 #include "notifytablemodel.h"
32 #include "notifylogging.h"
33 #include <qdebug.h>
34 #include <QMimeData>
35 #include <QDataStream>
36 
37 const char *mime_type_notify_table = "openpilot/notify_plugin_table";
38 
40  : QAbstractTableModel(parent)
41  , _list(parentList)
42 {
43  _headerStrings << "Name"
44  << "Repeats"
45  << "Lifetime,sec"
46  << "Mute";
47  connect(this, &NotifyTableModel::dragRows, this, &NotifyTableModel::dropRows);
48 }
49 
50 bool NotifyTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
51 {
52  if (index.isValid() && role == Qt::DisplayRole) {
53  if (eMessageName == index.column()) {
54  emit dataChanged(index, index);
55  return true;
56  }
57  }
58  if (index.isValid() && role == Qt::EditRole) {
59  if (eRepeatValue == index.column())
60  _list.at(index.row())
61  ->setRetryValue(NotificationItem::retryValues.indexOf(value.toString()));
62  else {
63  if (eExpireTimer == index.column())
64  _list.at(index.row())->setLifetime(value.toInt());
65  else {
66  if (eTurnOn == index.column())
67  _list.at(index.row())->setMute(value.toBool());
68  }
69  }
70  emit dataChanged(index, index);
71  return true;
72  }
73  return false;
74 }
75 
76 QVariant NotifyTableModel::data(const QModelIndex &index, int role) const
77 {
78  if (!index.isValid()) {
79  qWarning() << "NotifyTableModel::data - index.isValid()";
80  return QVariant();
81  }
82 
83  if (index.row() >= _list.size())
84  return QVariant();
85 
86  if (role == Qt::DisplayRole || role == Qt::EditRole) {
87  switch (index.column()) {
88  case eMessageName:
89  return _list.at(index.row())->toString();
90 
91  case eRepeatValue:
92  return (NotificationItem::retryValues.at(_list.at(index.row())->retryValue()));
93 
94  case eExpireTimer:
95  return _list.at(index.row())->lifetime();
96 
97  case eTurnOn:
98  return _list.at(index.row())->mute();
99 
100  default:
101  return QVariant();
102  }
103  } else {
104  if (Qt::SizeHintRole == role) {
105  return QVariant(10);
106  }
107  }
108  return QVariant();
109 }
110 
111 QVariant NotifyTableModel::headerData(int section, Qt::Orientation orientation, int role) const
112 {
113  if (role != Qt::DisplayRole)
114  return QVariant();
115 
116  if (orientation == Qt::Horizontal)
117  return _headerStrings.at(section);
118  else if (orientation == Qt::Vertical)
119  return QString("%1").arg(section);
120 
121  return QVariant();
122 }
123 
124 bool NotifyTableModel::insertRows(int position, int rows, const QModelIndex &index)
125 {
126  Q_UNUSED(index);
127 
128  if (-1 == position || -1 == rows)
129  return false;
130 
131  beginInsertRows(QModelIndex(), position, position + rows - 1);
132 
133  for (int i = 0; i < rows; ++i) {
134  _list.insert(position + i, new NotificationItem());
135  }
136 
137  endInsertRows();
138  return true;
139 }
140 
141 bool NotifyTableModel::removeRows(int position, int rows, const QModelIndex &index)
142 {
143  Q_UNUSED(index);
144 
145  if ((-1 == position) || (-1 == rows))
146  return false;
147 
148  beginRemoveRows(QModelIndex(), position, position + rows - 1);
149 
150  for (int row = 0; row < rows; ++row) {
151  _list.removeAt(position);
152  }
153 
154  endRemoveRows();
155  return true;
156 }
157 
159 {
160  QModelIndex idx = index(offset, 0);
161  emit dataChanged(idx, idx);
162 }
163 
165 {
166  insertRows(rowCount(), 1, QModelIndex());
167  NotificationItem *tmp = _list.at(rowCount() - 1);
168  _list.replace(rowCount() - 1, item);
169  delete tmp;
170  entryUpdated(rowCount() - 1);
171 }
172 
174 {
175  return Qt::MoveAction;
176 }
177 
178 QStringList NotifyTableModel::mimeTypes() const
179 {
180  QStringList types;
181  types << mime_type_notify_table;
182  return types;
183 }
184 
185 bool NotifyTableModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row,
186  int column, const QModelIndex &parent)
187 {
188  Q_UNUSED(column);
189  if (action == Qt::IgnoreAction)
190  return true;
191 
192  if (!data->hasFormat(mime_type_notify_table))
193  return false;
194 
195  int beginRow = -1;
196 
197  if (row != -1)
198  beginRow = row;
199  else {
200  if (parent.isValid())
201  beginRow = parent.row();
202  else
203  beginRow = rowCount(QModelIndex());
204  }
205 
206  if (-1 == beginRow)
207  return false;
208 
209  QByteArray encodedData = data->data(mime_type_notify_table);
210  QDataStream stream(&encodedData, QIODevice::ReadOnly);
211  int rows = beginRow;
212  // read next item from input MIME and drop into the table line by line
213  while (!stream.atEnd()) {
214  quintptr ptr;
215  stream >> ptr;
216  NotificationItem *item = reinterpret_cast<NotificationItem *>(ptr);
217  int dragged = _list.indexOf(item);
218  // we can drag item from top rows to bottom (DOWN_DIRECTION),
219  // or from bottom rows to top rows (UP_DIRECTION)
220  enum { UP_DIRECTION, DOWN_DIRECTION };
221  int direction = (dragged < rows) ? DOWN_DIRECTION : (dragged += 1, UP_DIRECTION);
222  // check drop bounds
223  if (dragged < 0 || ((dragged + 1) >= _list.size() && direction == DOWN_DIRECTION)
224  || dragged == rows) {
225  qNotifyDebug() << "no such item";
226  continue;
227  }
228  // addiional check in case dropping of multiple rows
229  if (rows + direction > _list.size())
230  continue;
231 
232  insertRows(rows + direction, 1, QModelIndex());
233  _list.replace(rows + direction, item);
234  removeRows(dragged, 1, QModelIndex());
235  if (direction == UP_DIRECTION)
236  ++rows;
237  };
238 
239  QModelIndex idxTopLeft = index(beginRow, 0, QModelIndex());
240  QModelIndex idxBotRight = index(beginRow, columnCount(QModelIndex()), QModelIndex());
241  emit dataChanged(idxTopLeft, idxBotRight);
242  return true;
243 }
244 
245 QMimeData *NotifyTableModel::mimeData(const QModelIndexList &indexes) const
246 {
247  QMimeData *mimeData = new QMimeData();
248  QByteArray encodedData;
249 
250  QDataStream stream(&encodedData, QIODevice::WriteOnly);
251  int rows = 0;
252  foreach (const QModelIndex &index, indexes) {
253  if (!index.column()) {
254  quintptr item = reinterpret_cast<quintptr>(_list.at(index.row()));
255  stream << item;
256  ++rows;
257  }
258  }
259  mimeData->setData(mime_type_notify_table, encodedData);
260  return mimeData;
261 }
262 
263 void NotifyTableModel::dropRows(int position, int count) const
264 {
265  for (int row = 0; row < count; ++row) {
266  _list.removeAt(position);
267  }
268 }
Uses to logging only inside notify plugin, can be convinient turned on/off.
NotifyTableModel(QList< NotificationItem * > &parentList, QObject *parent=nullptr)
int columnCount(const QModelIndex &) const
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
for i
Definition: OPPlots.m:140
QStringList mimeTypes() const
int rowCount(const QModelIndex &parent=QModelIndex()) const
DataFields data
QDebug qNotifyDebug()
void entryAdded(NotificationItem *item)
QMimeData * mimeData(const QModelIndexList &indexes) const
const char * mime_type_notify_table
static QStringList retryValues
bool removeRows(int position, int rows, const QModelIndex &index)
bool setData(const QModelIndex &index, const QVariant &value, int role)
bool insertRows(int position, int rows, const QModelIndex &index)
QVariant data(const QModelIndex &index, int role) const
Qt::DropActions supportedDropActions() const
void dragRows(int position, int count)
void entryUpdated(int offset)
QVariant headerData(int section, Qt::Orientation orientation, int role) const