![]() |
Mixxx
|
00001 #include <QtGui> 00002 00003 #include "library/treeitem.h" 00004 #include "library/treeitemmodel.h" 00005 00006 /* 00007 * Just a word about how the TreeItem objects and TreeItemModels are used in general: 00008 * TreeItems are used by the TreeItemModel class to display tree 00009 * structures in the sidebar. 00010 * 00011 * The constructor has 4 arguments: 00012 * 1. argument represents a name shown in the sidebar view later on 00013 * 2. argument represents the absolute path of this tree item 00014 * 3. argument is a library feature object. 00015 * This is necessary because in sidebar.cpp we hanlde 'activateChid' events 00016 * 4. the parent TreeItem object 00017 * The constructor does not add this TreeItem object to the parent's child list 00018 * 00019 * In case of no arguments, the standard constructor creates a 00020 * root item that is not visible in the sidebar. 00021 * 00022 * Once the TreeItem objects are inserted to models, the models take care of their 00023 * deletion. 00024 * 00025 * Examples on how to use TreeItem and TreeItemModels can be found in 00026 * - playlistfeature.cpp 00027 * - cratefeature.cpp 00028 * - *feature.cpp 00029 */ 00030 TreeItemModel::TreeItemModel(QObject *parent) 00031 : QAbstractItemModel(parent) { 00032 00033 m_rootItem = new TreeItem(); 00034 00035 } 00036 00037 TreeItemModel::~TreeItemModel() { 00038 delete m_rootItem; 00039 } 00040 00041 //Our Treeview Model supports exactly a single column 00042 int TreeItemModel::columnCount(const QModelIndex &parent) const { 00043 return 1; 00044 } 00045 00046 QVariant TreeItemModel::data(const QModelIndex &index, int role) const { 00047 if (!index.isValid()) 00048 return QVariant(); 00049 00050 if (role != Qt::DisplayRole) 00051 return QVariant(); 00052 00053 TreeItem *item = static_cast<TreeItem*>(index.internalPointer()); 00054 00055 return item->data(); 00056 } 00057 00058 Qt::ItemFlags TreeItemModel::flags(const QModelIndex &index) const { 00059 if (!index.isValid()) 00060 return 0; 00061 00062 return Qt::ItemIsEnabled | Qt::ItemIsSelectable; 00063 } 00064 00065 QVariant TreeItemModel::headerData(int section, Qt::Orientation orientation, int role) const { 00066 return QVariant(); 00067 } 00068 00069 QModelIndex TreeItemModel::index(int row, int column, const QModelIndex &parent) const { 00070 if (!hasIndex(row, column, parent)) 00071 return QModelIndex(); 00072 00073 TreeItem *parentItem = NULL; 00074 00075 if (!parent.isValid()) 00076 parentItem = m_rootItem; 00077 else 00078 parentItem = static_cast<TreeItem*>(parent.internalPointer()); 00079 00080 TreeItem *childItem = parentItem->child(row); 00081 if (childItem) 00082 return createIndex(row, column, childItem); 00083 else 00084 return QModelIndex(); 00085 } 00086 00087 QModelIndex TreeItemModel::parent(const QModelIndex &index) const { 00088 if (!index.isValid()) 00089 return QModelIndex(); 00090 00091 TreeItem *childItem = static_cast<TreeItem*>(index.internalPointer()); 00092 TreeItem *parentItem = childItem->parent(); 00093 00094 if (parentItem == m_rootItem) 00095 return QModelIndex(); 00096 00097 return createIndex(parentItem->row(), 0, parentItem); 00098 } 00099 00100 int TreeItemModel::rowCount(const QModelIndex &parent) const { 00101 if (parent.column() > 0) 00102 return 0; 00103 00104 TreeItem *parentItem = NULL; 00105 //qDebug() << "parent data: " << parent.data(); 00106 if (!parent.isValid()){ 00107 parentItem = m_rootItem; 00108 } 00109 else{ 00110 parentItem = static_cast<TreeItem*>(parent.internalPointer()); 00111 00112 } 00113 00114 //qDebug() << "TreeItem data: " << parent.internalPointer(); 00115 00116 return parentItem->childCount(); 00117 } 00118 00123 void TreeItemModel::setRootItem(TreeItem *item) { 00124 if(m_rootItem) delete m_rootItem; 00125 00126 m_rootItem = item; 00127 reset(); 00128 } 00129 00134 bool TreeItemModel::insertRows(QList<TreeItem*> &data, int position, int rows, const QModelIndex &parent) { 00135 TreeItem *parentItem = getItem(parent); 00136 bool success; 00137 00138 beginInsertRows(parent, position, position + rows - 1); 00139 success = parentItem->insertChildren(data, position, rows); 00140 endInsertRows(); 00141 00142 return success; 00143 } 00144 00145 bool TreeItemModel::removeRows(int position, int rows, const QModelIndex &parent) { 00146 TreeItem *parentItem = getItem(parent); 00147 bool success = true; 00148 00149 beginRemoveRows(parent, position, position + rows - 1); 00150 success = parentItem->removeChildren(position, rows); 00151 endRemoveRows(); 00152 00153 return success; 00154 } 00155 00156 TreeItem* TreeItemModel::getItem(const QModelIndex &index) const { 00157 if (index.isValid()) { 00158 TreeItem *item = static_cast<TreeItem*>(index.internalPointer()); 00159 if (item) return item; 00160 } 00161 return m_rootItem; 00162 }