2013-10-08 15:07:03 +08:00
|
|
|
/* treeitem.cpp
|
|
|
|
|
2015-01-31 22:00:00 +08:00
|
|
|
Copyright (c) 2015, Nikolaj Schlej. All rights reserved.
|
2013-12-29 23:13:46 +08:00
|
|
|
This program and the accompanying materials
|
|
|
|
are licensed and made available under the terms and conditions of the BSD License
|
|
|
|
which accompanies this distribution. The full text of the license may be found at
|
|
|
|
http://opensource.org/licenses/bsd-license.php
|
2013-10-08 15:07:03 +08:00
|
|
|
|
2013-12-29 23:13:46 +08:00
|
|
|
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
2013-10-08 15:07:03 +08:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
#include <QObject>
|
2013-10-08 15:07:03 +08:00
|
|
|
#include "treeitem.h"
|
2014-02-27 17:14:41 +08:00
|
|
|
#include "types.h"
|
2013-10-08 15:07:03 +08:00
|
|
|
|
2015-02-06 16:47:19 +08:00
|
|
|
TreeItem::TreeItem(const UINT8 type, const UINT8 subtype, const UINT8 compression,
|
2014-07-25 07:59:51 +08:00
|
|
|
const QString & name, const QString & text, const QString & info,
|
2015-02-06 16:47:19 +08:00
|
|
|
const QByteArray & header, const QByteArray & body, const QByteArray & parsingData,
|
2015-01-31 22:00:00 +08:00
|
|
|
TreeItem *parent) :
|
|
|
|
itemAction(Actions::NoAction),
|
|
|
|
itemType(type),
|
2015-02-06 16:47:19 +08:00
|
|
|
itemSubtype(subtype),
|
2015-01-31 22:00:00 +08:00
|
|
|
itemCompression(compression),
|
|
|
|
itemName(name),
|
|
|
|
itemText(text),
|
|
|
|
itemInfo(info),
|
|
|
|
itemHeader(header),
|
|
|
|
itemBody(body),
|
2015-02-06 16:47:19 +08:00
|
|
|
itemParsingData(parsingData),
|
2015-01-31 22:00:00 +08:00
|
|
|
parentItem(parent)
|
2013-11-07 21:46:28 +08:00
|
|
|
{
|
2013-10-08 15:07:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TreeItem::~TreeItem()
|
|
|
|
{
|
|
|
|
qDeleteAll(childItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TreeItem::appendChild(TreeItem *item)
|
|
|
|
{
|
|
|
|
childItems.append(item);
|
|
|
|
}
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
void TreeItem::prependChild(TreeItem *item)
|
|
|
|
{
|
|
|
|
childItems.prepend(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT8 TreeItem::insertChildBefore(TreeItem *item, TreeItem *newItem)
|
|
|
|
{
|
|
|
|
int index = childItems.indexOf(item);
|
|
|
|
if (index == -1)
|
|
|
|
return ERR_ITEM_NOT_FOUND;
|
|
|
|
childItems.insert(index, newItem);
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT8 TreeItem::insertChildAfter(TreeItem *item, TreeItem *newItem)
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
2013-11-07 21:46:28 +08:00
|
|
|
int index = childItems.indexOf(item);
|
|
|
|
if (index == -1)
|
|
|
|
return ERR_ITEM_NOT_FOUND;
|
|
|
|
childItems.insert(index + 1, newItem);
|
|
|
|
return ERR_SUCCESS;
|
2013-10-08 15:07:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TreeItem *TreeItem::child(int row)
|
|
|
|
{
|
2013-11-07 21:46:28 +08:00
|
|
|
return childItems.value(row, NULL);
|
2013-10-08 15:07:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int TreeItem::childCount() const
|
|
|
|
{
|
|
|
|
return childItems.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
int TreeItem::columnCount() const
|
|
|
|
{
|
2013-11-07 21:46:28 +08:00
|
|
|
return 5;
|
2013-10-08 15:07:03 +08:00
|
|
|
}
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
QVariant TreeItem::data(int column) const
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
2014-07-25 07:59:51 +08:00
|
|
|
switch (column)
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
2015-01-31 22:00:00 +08:00
|
|
|
case 0: // Name
|
2013-11-14 18:40:39 +08:00
|
|
|
return itemName;
|
2015-01-31 22:00:00 +08:00
|
|
|
case 1: // Action
|
2014-02-27 17:14:41 +08:00
|
|
|
return actionTypeToQString(itemAction);
|
2015-01-31 22:00:00 +08:00
|
|
|
case 2: // Type
|
|
|
|
return itemTypeToQString(itemType);
|
2015-02-06 16:47:19 +08:00
|
|
|
case 3: // Subtype
|
|
|
|
return itemSubtypeToQString(itemType, itemSubtype);
|
2015-01-31 22:00:00 +08:00
|
|
|
case 4: // Text
|
2013-10-08 15:07:03 +08:00
|
|
|
return itemText;
|
|
|
|
default:
|
2013-11-07 21:46:28 +08:00
|
|
|
return QVariant();
|
2013-10-08 15:07:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TreeItem *TreeItem::parent()
|
|
|
|
{
|
|
|
|
return parentItem;
|
|
|
|
}
|
|
|
|
|
2015-01-31 22:00:00 +08:00
|
|
|
QString TreeItem::name() const
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
2015-01-31 22:00:00 +08:00
|
|
|
return itemName;
|
2013-10-08 15:07:03 +08:00
|
|
|
}
|
|
|
|
|
2015-01-31 22:00:00 +08:00
|
|
|
void TreeItem::setName(const QString &name)
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
2015-01-31 22:00:00 +08:00
|
|
|
itemName = name;
|
2013-10-08 15:07:03 +08:00
|
|
|
}
|
|
|
|
|
2015-01-31 22:00:00 +08:00
|
|
|
QString TreeItem::text() const
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
2015-01-31 22:00:00 +08:00
|
|
|
return itemText;
|
2013-10-08 15:07:03 +08:00
|
|
|
}
|
|
|
|
|
2015-01-31 22:00:00 +08:00
|
|
|
void TreeItem::setText(const QString &text)
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
2015-01-31 22:00:00 +08:00
|
|
|
itemText = text;
|
2013-10-08 15:07:03 +08:00
|
|
|
}
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
QString TreeItem::info() const
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
|
|
|
return itemInfo;
|
|
|
|
}
|
|
|
|
|
2015-01-31 22:00:00 +08:00
|
|
|
void TreeItem::addInfo(const QString &info)
|
|
|
|
{
|
|
|
|
itemInfo += info;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TreeItem::setInfo(const QString &info)
|
|
|
|
{
|
|
|
|
itemInfo = info;
|
|
|
|
}
|
|
|
|
|
2013-10-08 15:07:03 +08:00
|
|
|
int TreeItem::row() const
|
|
|
|
{
|
|
|
|
if (parentItem)
|
|
|
|
return parentItem->childItems.indexOf(const_cast<TreeItem*>(this));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
UINT8 TreeItem::type() const
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
|
|
|
return itemType;
|
|
|
|
}
|
|
|
|
|
2015-01-31 22:00:00 +08:00
|
|
|
void TreeItem::setType(const UINT8 type)
|
|
|
|
{
|
|
|
|
itemType = type;
|
|
|
|
}
|
|
|
|
|
2015-02-06 16:47:19 +08:00
|
|
|
UINT8 TreeItem::subtype() const
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
2015-02-06 16:47:19 +08:00
|
|
|
return itemSubtype;
|
2013-10-08 15:07:03 +08:00
|
|
|
}
|
|
|
|
|
2015-02-06 16:47:19 +08:00
|
|
|
void TreeItem::setSubtype(const UINT8 subtype)
|
2015-01-31 22:00:00 +08:00
|
|
|
{
|
2015-02-06 16:47:19 +08:00
|
|
|
itemSubtype = subtype;
|
2015-01-31 22:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
UINT8 TreeItem::compression() const
|
2013-10-15 23:19:15 +08:00
|
|
|
{
|
2013-11-07 21:46:28 +08:00
|
|
|
return itemCompression;
|
2013-10-15 23:19:15 +08:00
|
|
|
}
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
QByteArray TreeItem::header() const
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
|
|
|
return itemHeader;
|
|
|
|
}
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
QByteArray TreeItem::body() const
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
|
|
|
return itemBody;
|
|
|
|
}
|
|
|
|
|
2015-02-06 16:47:19 +08:00
|
|
|
QByteArray TreeItem::parsingData() const
|
|
|
|
{
|
|
|
|
return itemParsingData;
|
|
|
|
}
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
bool TreeItem::hasEmptyHeader() const
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
|
|
|
return itemHeader.isEmpty();
|
|
|
|
}
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
bool TreeItem::hasEmptyBody() const
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
|
|
|
return itemBody.isEmpty();
|
2013-11-07 21:46:28 +08:00
|
|
|
}
|
|
|
|
|
2015-02-06 16:47:19 +08:00
|
|
|
bool TreeItem::hasEmptyParsingData() const
|
|
|
|
{
|
|
|
|
return itemParsingData.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TreeItem::setParsingData(const QByteArray & data)
|
|
|
|
{
|
|
|
|
itemParsingData = data;
|
|
|
|
}
|
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
UINT8 TreeItem::action() const
|
|
|
|
{
|
|
|
|
return itemAction;
|
|
|
|
}
|
2013-11-18 23:23:59 +08:00
|
|
|
|
2013-11-07 21:46:28 +08:00
|
|
|
void TreeItem::setAction(const UINT8 action)
|
|
|
|
{
|
|
|
|
itemAction = action;
|
2013-11-18 23:23:59 +08:00
|
|
|
|
2013-12-29 23:13:46 +08:00
|
|
|
// On insert action, set insert action for children
|
2014-02-27 17:14:41 +08:00
|
|
|
if (action == Actions::Insert)
|
2014-07-25 07:59:51 +08:00
|
|
|
for (int i = 0; i < childCount(); i++)
|
2014-02-27 17:14:41 +08:00
|
|
|
child(i)->setAction(Actions::Insert);
|
2013-12-29 23:13:46 +08:00
|
|
|
|
|
|
|
// Set rebuild action for parent, if it has no action now
|
2014-02-27 17:14:41 +08:00
|
|
|
if (parentItem && parentItem->type() != Types::Root
|
|
|
|
&& parentItem->action() == Actions::NoAction)
|
|
|
|
parentItem->setAction(Actions::Rebuild);
|
2013-11-18 23:23:59 +08:00
|
|
|
}
|
|
|
|
|