/guitone-1.0rc5/src/model/InventoryItem.cpp
C++ | 557 lines | 460 code | 69 blank | 28 comment | 158 complexity | ca3eab2e97be174e2772674761dc5610 MD5 | raw file
Possible License(s): GPL-3.0
- /***************************************************************************
- * Copyright (C) 2006 by Thomas Keller *
- * me@thomaskeller.biz *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation, either version 3 of the License, or *
- * (at your option) any later version. *
- * *
- * This program is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
- * GNU General Public License for more details. *
- * *
- * You should have received a copy of the GNU General Public License *
- * along with this program. If not, see <http://www.gnu.org/licenses/>. *
- ***************************************************************************/
- #include "InventoryItem.h"
- #include <QStringList>
- #include <QFont>
- ModelItem::ModelItem(const QString & l) : parentItem(0), label(l)
- {}
- ModelItem::ModelItem(const ModelItem * other)
- {
- parentItem = other->parent();
- children = other->getChildren();
- label = other->getLabel();
- }
- ModelItem::~ModelItem()
- {
- deleteAllChildren();
- }
- void ModelItem::setLabel(const QString & l)
- {
- label = l;
- }
- QString ModelItem::getLabel() const
- {
- return label;
- }
- void ModelItem::deleteAllChildren()
- {
- qDeleteAll(children);
- children.clear();
- }
- void ModelItem::appendChild(ModelItem * child)
- {
- child->setParent(this);
- children.append(child);
- }
- void ModelItem::removeChild(ModelItem * child)
- {
- int idx = children.indexOf(child);
- if (idx == -1) return;
- children.removeAt(idx);
- }
- void ModelItem::setChildren(QList<ModelItem *> items)
- {
- deleteAllChildren();
- foreach (ModelItem * item, items)
- {
- appendChild(item);
- }
- }
- QList<ModelItem *> ModelItem::getChildren() const
- {
- return children;
- }
- void ModelItem::setParent(ModelItem * p)
- {
- parentItem = p;
- }
- ModelItem * ModelItem::parent() const
- {
- return parentItem;
- }
- ModelItem * ModelItem::child(int row) const
- {
- if (row < children.size())
- {
- return children.value(row);
- }
- return 0;
- }
- int ModelItem::childCount() const
- {
- return children.count();
- }
- int ModelItem::row() const
- {
- if (parentItem)
- {
- return parentItem->children.indexOf(const_cast<ModelItem *>(this));
- }
- return 0;
- }
- QVariant ModelItem::data(int column, int role) const
- {
- if (role == Qt::DisplayRole)
- {
- // return column headers for root item
- if (parentItem == this)
- {
- switch (column)
- {
- case 0: return QVariant(QString(tr("File")));
- case 1: return QVariant(QString(tr("Status")));
- default: return QVariant();
- }
- }
- switch (column)
- {
- case 0: return QVariant(label);
- default: return QVariant();
- }
- }
- if (role == Qt::DecorationRole && column == 0)
- {
- IconProvider * provider = IconProvider::singleton();
- return provider->getIcon(this);
- }
- return QVariant();
- }
- bool ModelItem::isRoot() const
- {
- return this == parentItem;
- }
- const int InventoryItem::RenameSource = 1;
- const int InventoryItem::RenameTarget = 2;
- const int InventoryItem::Added = 4;
- const int InventoryItem::Dropped = 8;
- const int InventoryItem::Missing = 16;
- const int InventoryItem::Known = 32;
- const int InventoryItem::Unknown = 64;
- const int InventoryItem::Ignored = 128;
- const int InventoryItem::Invalid = 256;
- const int InventoryItem::ContentsChanged = 512;
- const int InventoryItem::AttributesChanged = 1024;
- QString InventoryItem::translateFileType(const FileType & t)
- {
- switch (t)
- {
- case File: return tr("File");
- case Directory: return tr("Directory");
- case None: return tr("None");
- default: return "-";
- }
- }
- InventoryItem::InventoryItem(const Stanza & stanza)
- : ModelItem(), fs_type(Undefined), old_type(Undefined), new_type(Undefined),
- status(0), aboutToBeExpanded(false)
- {
- foreach (const StanzaEntry & en, stanza)
- {
- if (en.sym == "path")
- {
- I(en.vals.size() == 1);
- path = en.vals.at(0);
- continue;
- }
- if (en.sym == "old_path")
- {
- I(en.vals.size() == 1);
- old_path = en.vals.at(0);
- continue;
- }
- if (en.sym == "new_path")
- {
- I(en.vals.size() == 1);
- new_path = en.vals.at(0);
- continue;
- }
- if (en.sym == "old_type")
- {
- I(en.vals.size() == 1);
- if (en.vals.at(0) == "file")
- old_type = File;
- else if (en.vals.at(0) == "directory")
- old_type = Directory;
- else
- I(false);
- continue;
- }
- if (en.sym == "new_type")
- {
- I(en.vals.size() == 1);
- if (en.vals.at(0) == "file")
- new_type = File;
- else if (en.vals.at(0) == "directory")
- new_type = Directory;
- else
- I(false);
- continue;
- }
- if (en.sym == "fs_type")
- {
- I(en.vals.size() == 1);
- if (en.vals.at(0) == "file")
- fs_type = File;
- else if (en.vals.at(0) == "directory")
- fs_type = Directory;
- else if (en.vals.at(0) == "none")
- fs_type = None;
- else
- I(false);
- continue;
- }
- if (en.sym == "status")
- {
- I(en.vals.size() > 0);
- foreach (const QString & val, en.vals)
- {
- if (val == "rename_source")
- status |= RenameSource;
- else if (val == "rename_target")
- status |= RenameTarget;
-