![]() |
Mixxx
|
00001 // TreeItem.cpp 00002 // Created 10/02/2010 by Tobias Rafreider 00003 00004 #include <QStringList> 00005 00006 #include "library/treeitem.h" 00007 00008 /* 00009 * Just a word about how the TreeItem objects and TreeItemModels are used in general: 00010 * TreeItems are used by the TreeItemModel class to display tree 00011 * structures in the sidebar. 00012 * 00013 * The constructor has 4 arguments: 00014 * 1. argument represents a name shown in the sidebar view later on 00015 * 2. argument represents the absolute path of this tree item 00016 * 3. argument is a library feature object. 00017 * This is necessary because in sidebar.cpp we hanlde 'activateChid' events 00018 * 4. the parent TreeItem object 00019 * The constructor does not add this TreeItem object to the parent's child list 00020 * 00021 * In case of no arguments, the standard constructor creates a 00022 * root item that is not visible in the sidebar. 00023 * 00024 * Once the TreeItem objects are inserted to models, the models take care of their 00025 * deletion. 00026 * 00027 * Examples on how to use TreeItem and TreeItemModels can be found in 00028 * - playlistfeature.cpp 00029 * - cratefeature.cpp 00030 * - *feature.cpp 00031 */ 00032 TreeItem::TreeItem(const QString &data, const QString &data_path, LibraryFeature* feature, TreeItem* parent) { 00033 m_data = data; 00034 m_dataPath = data_path; 00035 m_parentItem = parent; 00036 m_feature = feature; 00037 } 00038 00039 TreeItem::TreeItem() { 00040 m_data = "$root"; 00041 m_dataPath = "$root"; 00042 m_parentItem = NULL; 00043 m_feature = NULL; 00044 } 00045 00046 TreeItem::~TreeItem() { 00047 qDeleteAll(m_childItems); 00048 } 00049 00050 void TreeItem::appendChild(TreeItem *item) { 00051 m_childItems.append(item); 00052 } 00053 00054 TreeItem *TreeItem::child(int row) { 00055 return m_childItems.value(row); 00056 } 00057 00058 int TreeItem::childCount() const { 00059 return m_childItems.count(); 00060 } 00061 00062 QVariant TreeItem::data() const { 00063 return m_data; 00064 } 00065 00066 QVariant TreeItem::dataPath() const { 00067 return m_dataPath; 00068 } 00069 00070 bool TreeItem::isPlaylist() const { 00071 return (m_childItems.count() == 0); 00072 } 00073 00074 bool TreeItem::isFolder() const { 00075 return (m_childItems.count() != 0); 00076 } 00077 00078 TreeItem *TreeItem::parent() { 00079 return m_parentItem; 00080 } 00081 00082 int TreeItem::row() const { 00083 if (m_parentItem) { 00084 return m_parentItem->m_childItems.indexOf(const_cast<TreeItem*>(this)); 00085 } 00086 00087 return 0; 00088 } 00089 00090 LibraryFeature* TreeItem::getFeature() { 00091 return m_feature; 00092 } 00093 00094 bool TreeItem::insertChildren(QList<TreeItem*> &data, int position, int count) { 00095 if (position < 0 || position > m_childItems.size()) 00096 return false; 00097 00098 for (int row = 0; row < count; ++row) { 00099 TreeItem* item = data.at(row); 00100 m_childItems.insert(row, item); 00101 } 00102 00103 return true; 00104 } 00105 00106 bool TreeItem::removeChildren(int position, int count) { 00107 if (position < 0 || position + count > m_childItems.size()) 00108 return false; 00109 00110 for (int row = 0; row < count; ++row){ 00111 //Remove from list to avoid invalid pointers 00112 TreeItem* item = m_childItems.takeAt(position); 00113 if(item) delete item; 00114 } 00115 return true; 00116 } 00117 00118 bool TreeItem::setData(const QVariant &data, const QVariant &data_path) { 00119 m_data = data.toString(); 00120 m_dataPath = data_path.toString(); 00121 return true; 00122 } 00123 00124 QIcon TreeItem::getIcon() { 00125 return m_icon; 00126 } 00127 00128 void TreeItem::setIcon(const QIcon& icon) { 00129 m_icon = icon; 00130 }