1
0
forked from blue/squawk
squawk/ui/models/item.cpp

151 lines
4.3 KiB
C++
Raw Normal View History

2019-04-03 15:09:29 +00:00
#include "item.h"
Models::Item::Item(Type p_type, const QMap<QString, QVariant> &p_data, Item *p_parent):
2019-04-03 18:15:36 +00:00
QObject(),
2019-04-03 15:09:29 +00:00
type(p_type),
name(p_data.value("name").toString()),
childItems(),
parent(p_parent)
{}
Models::Item::~Item()
{
std::deque<Item*>::const_iterator itr = childItems.begin();
std::deque<Item*>::const_iterator end = childItems.end();
for (;itr != end; ++itr) {
delete (*itr);
}
}
void Models::Item::setName(const QString& p_name)
{
2019-04-03 18:15:36 +00:00
if (name != p_name) {
name = p_name;
2019-04-07 14:02:41 +00:00
changed(0);
2019-04-03 18:15:36 +00:00
}
2019-04-03 15:09:29 +00:00
}
void Models::Item::appendChild(Models::Item* child)
{
2019-04-07 14:02:41 +00:00
bool moving = false;
int oldRow = child->row();
int newRow = this->childCount();
if (child->parent != 0) {
moving = true;
emit childIsAboutToBeMoved(child->parent, oldRow, oldRow, this, newRow);
child->parent->_removeChild(oldRow);
} else {
emit childIsAboutToBeInserted(this, newRow, newRow);
}
2019-04-03 15:09:29 +00:00
childItems.push_back(child);
2019-04-07 14:02:41 +00:00
child->parent = this;
2019-04-09 15:04:08 +00:00
QObject::connect(child, SIGNAL(childChanged(Models::Item*, int, int)), this, SIGNAL(childChanged(Models::Item*, int, int)));
2019-04-07 14:02:41 +00:00
QObject::connect(child, SIGNAL(childIsAboutToBeInserted(Item*, int, int)), this, SIGNAL(childIsAboutToBeInserted(Item*, int, int)));
QObject::connect(child, SIGNAL(childInserted()), this, SIGNAL(childInserted()));
QObject::connect(child, SIGNAL(childIsAboutToBeRemoved(Item*, int, int)), this, SIGNAL(childIsAboutToBeRemoved(Item*, int, int)));
QObject::connect(child, SIGNAL(childRemoved()), this, SIGNAL(childRemoved()));
QObject::connect(child, SIGNAL(childIsAboutToBeMoved(Item*, int, int, Item*, int)), this, SIGNAL(childIsAboutToBeMoved(Item*, int, int, Item*, int)));
QObject::connect(child, SIGNAL(childMoved()), this, SIGNAL(childMoved()));
if (moving) {
emit childMoved();
} else {
emit childInserted();
}
2019-04-03 15:09:29 +00:00
}
Models::Item * Models::Item::child(int row)
{
return childItems[row];
}
int Models::Item::childCount() const
{
return childItems.size();
}
int Models::Item::row() const
{
if (parent != 0) {
std::deque<Item*>::const_iterator itr = parent->childItems.begin();
std::deque<Item*>::const_iterator end = parent->childItems.end();
for (int i = 0; itr != end; ++itr, ++i) {
if (*itr == this) {
return i;
}
}
}
return 0; //TODO not sure how it helps, i copy-pasted it from the example
}
Models::Item * Models::Item::parentItem()
{
return parent;
}
2019-04-09 15:04:08 +00:00
const Models::Item * Models::Item::parentItemConst() const
{
return parent;
}
2019-04-03 15:09:29 +00:00
int Models::Item::columnCount() const
{
return 1;
}
QString Models::Item::getName() const
{
return name;
}
QVariant Models::Item::data(int column) const
{
if (column != 0) {
return QVariant();
}
return name;
}
2019-04-05 15:12:59 +00:00
void Models::Item::removeChild(int index)
{
2019-04-07 14:02:41 +00:00
emit childIsAboutToBeRemoved(this, index, index);
_removeChild(index);
2019-04-07 14:02:41 +00:00
emit childRemoved();
2019-04-05 15:12:59 +00:00
}
2019-04-07 14:02:41 +00:00
void Models::Item::_removeChild(int index)
2019-04-05 15:12:59 +00:00
{
2019-04-07 14:02:41 +00:00
Item* child = childItems[index];
2019-05-29 15:05:54 +00:00
QObject::disconnect(child, SIGNAL(childChanged(Models::Item*, int, int)), this, SIGNAL(childChanged(Models::Item*, int, int)));
QObject::disconnect(child, SIGNAL(childIsAboutToBeInserted(Item*, int, int)), this, SIGNAL(childIsAboutToBeInserted(Item*, int, int)));
QObject::disconnect(child, SIGNAL(childInserted()), this, SIGNAL(childInserted()));
QObject::disconnect(child, SIGNAL(childIsAboutToBeRemoved(Item*, int, int)), this, SIGNAL(childIsAboutToBeRemoved(Item*, int, int)));
QObject::disconnect(child, SIGNAL(childRemoved()), this, SIGNAL(childRemoved()));
QObject::disconnect(child, SIGNAL(childIsAboutToBeMoved(Item*, int, int, Item*, int)), this, SIGNAL(childIsAboutToBeMoved(Item*, int, int, Item*, int)));
QObject::disconnect(child, SIGNAL(childMoved()), this, SIGNAL(childMoved()));
2019-04-07 14:02:41 +00:00
childItems.erase(childItems.begin() + index);
child->parent = 0;
2019-04-05 15:12:59 +00:00
}
2019-04-07 14:02:41 +00:00
void Models::Item::changed(int col)
{
if (parent != 0) {
emit childChanged(this, row(), col);
}
}
void Models::Item::toOfflineState()
{
for (std::deque<Item*>::iterator itr = childItems.begin(), end = childItems.end(); itr != end; ++itr) {
Item* it = *itr;
it->toOfflineState();
}
}