Qt: Coloringrules Mime is encoded as JSON

Encode coloring rules as json objects to avoid encoding issues

Change-Id: I4b5369fef3c0f9e73cbb08edf14de6535ff35026
Reviewed-on: https://code.wireshark.org/review/34947
Petri-Dish: Roland Knall <rknall@gmail.com>
Tested-by: Petri Dish Buildbot
Reviewed-by: Roland Knall <rknall@gmail.com>
This commit is contained in:
Roland Knall 2019-11-04 10:37:51 +00:00
parent 6fbb1d5c66
commit a8052b9f38
2 changed files with 58 additions and 51 deletions

View File

@ -1405,32 +1405,21 @@ Display filters are being dragged and dropped by utilizing this mime type.
Coloring Rules are being used for dragging and dropping color rules inside the
coloring rules dialog.
[cols="1,3,1", options="header"]
|===
|Name
|Description
|Qt/C++ Type
|disabled
|Is the coloring rule enabled/disabled
|bool
|name
|Name to be displayed for it
|QString
|filter
|The display filter
|QString
|foreground
|Foreground Color
|QColor
|background
|Background Color
|QColor
|===
[source,json]
----
{
"coloringrules" :
[
{
"disabled": false,
"name": "UDP Ports for 8080",
"filter": "udp.port == 8080",
"foreground": "[0x0000, 0x0000, 0x0000]",
"background": "[0xFFFF, 0xFFFF, 0xFFFF]"
}
]
}
----
==== Filter List

View File

@ -21,6 +21,9 @@
#include <ui/qt/utils/wireshark_mime_data.h>
#include <QMimeData>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
ColoringRuleItem::ColoringRuleItem(bool disabled, QString name, QString filter, QColor foreground, QColor background, ColoringRuleItem* parent)
: ModelHelperTreeItem<ColoringRuleItem>(parent),
@ -383,7 +386,7 @@ QVariant ColoringRulesModel::headerData(int section, Qt::Orientation orientation
Qt::DropActions ColoringRulesModel::supportedDropActions() const
{
return Qt::MoveAction;
return Qt::MoveAction | Qt::CopyAction;
}
QStringList ColoringRulesModel::mimeTypes() const
@ -398,20 +401,27 @@ QMimeData* ColoringRulesModel::mimeData(const QModelIndexList &indexes) const
return NULL;
QMimeData *mimeData = new QMimeData();
QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly);
foreach (const QModelIndex &index, indexes) {
//use first column as "filter"
if (index.column() == 0) {
//Retrieve "native" data to save lots of conversions in the process
ColoringRuleItem* item = root_->child(index.row());
stream << item->disabled_ << item->name_ << item->filter_ << item->foreground_ << item->background_;
QJsonArray data;
foreach ( const QModelIndex & index, indexes )
{
if ( index.column() == 0 )
{
ColoringRuleItem * item = root_->child(index.row());
QJsonObject entry;
entry["disabled"] = item->disabled_;
entry["name"] = item->name_;
entry["filter"] = item->filter_;
entry["foreground"] = qVariantFromValue(item->foreground_).toString();
entry["background"] = qVariantFromValue(item->background_).toString();
data.append(entry);
}
}
QJsonObject dataSet;
dataSet["coloringrules"] = data;
QByteArray encodedData = QJsonDocument(dataSet).toJson();
mimeData->setData(WiresharkMimeData::ColoringRulesMimeType, encodedData);
return mimeData;
}
@ -436,27 +446,35 @@ bool ColoringRulesModel::dropMimeData(const QMimeData *data, Qt::DropAction acti
else
beginRow = rowCount();
bool disabled;
QString name;
QString filter;
QColor foreground;
QColor background;
ColoringRuleItem* item;
QList<QVariant> rules;
QByteArray encodedData = data->data(WiresharkMimeData::ColoringRulesMimeType);
QDataStream stream(&encodedData, QIODevice::ReadOnly);
int rows = 0;
QJsonDocument encodedData = QJsonDocument::fromJson(data->data(WiresharkMimeData::ColoringRulesMimeType));
if ( ! encodedData.isObject() || ! encodedData.object().contains("coloringrules") )
return false;
while (!stream.atEnd()) {
stream >> disabled >> name >> filter >> foreground >> background;
QJsonArray dataArray = encodedData.object()["coloringrules"].toArray();
item = new ColoringRuleItem(disabled, name, filter, foreground, background, root_);
for ( int row = 0; row < dataArray.count(); row++ )
{
QJsonObject entry = dataArray.at(row).toObject();
if ( ! entry.contains("foreground") || ! entry.contains("background") || ! entry.contains("filter") )
continue;
QColor fgColor = entry["foreground"].toVariant().value<QColor>();
QColor bgColor = entry["background"].toVariant().value<QColor>();
ColoringRuleItem * item = new ColoringRuleItem(
entry["disabled"].toVariant().toBool(),
entry["name"].toString(),
entry["filter"].toString(),
fgColor,
bgColor,
root_);
rules.append(VariantPointer<ColoringRuleItem>::asQVariant(item));
++rows;
}
insertRows(beginRow, rows, QModelIndex());
insertRows(beginRow, rules.count(), QModelIndex());
for (int i = 0; i < rules.count(); i++) {
QModelIndex idx = index(beginRow, 0, QModelIndex());
setData(idx, rules[i], Qt::UserRole);