| 1 | /* |
| 2 | * Copyright 2014 Mihai Niculescu <[email protected]> |
| 3 | * |
| 4 | * This file is part of EqualX Project (https://launchpad.net/equalx/) |
| 5 | * |
| 6 | * EqualX is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * EqualX is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include <QVariant> |
| 21 | #include "BookmarksPanel/BookmarkItem.h" |
| 22 | #include "BookmarksPanel/BookmarksItemModel.h" |
| 23 | |
| 24 | BookmarkItem::BookmarkItem(BookmarkItem *parent) |
| 25 | : mModel(0), |
| 26 | mParent(parent) |
| 27 | { |
| 28 | if(mParent) |
| 29 | mModel = mParent->model(); |
| 30 | } |
| 31 | |
| 32 | BookmarkItem::BookmarkItem(const LibraryModelData &data, BookmarkItem *parent) |
| 33 | : mModel(0), |
| 34 | mData(data), |
| 35 | mParent(parent) |
| 36 | { |
| 37 | if(mParent) |
| 38 | mModel = mParent->model(); |
| 39 | } |
| 40 | |
| 41 | BookmarkItem::~BookmarkItem() |
| 42 | { |
| 43 | qDeleteAll(mChildren); |
| 44 | } |
| 45 | |
| 46 | void BookmarkItem::appendChild(BookmarkItem *child) |
| 47 | { |
| 48 | mChildren.append(child); |
| 49 | } |
| 50 | |
| 51 | void BookmarkItem::insertChild(int row, BookmarkItem *child) |
| 52 | { |
| 53 | mChildren.insert(row, child); |
| 54 | } |
| 55 | |
| 56 | BookmarkItem *BookmarkItem::child(int row) |
| 57 | { |
| 58 | int minSize = 0; |
| 59 | int maxSize = mChildren.size()-1; |
| 60 | |
| 61 | if(row<minSize) row = minSize; |
| 62 | if(row>maxSize) row = maxSize; |
| 63 | |
| 64 | return mChildren.value(row); |
| 65 | } |
| 66 | |
| 67 | BookmarkItem *BookmarkItem::constChild(int row) const |
| 68 | { |
| 69 | return mChildren.value(row); |
| 70 | } |
| 71 | |
| 72 | void BookmarkItem::clear() |
| 73 | { |
| 74 | if(!mChildren.isEmpty()){ |
| 75 | qDeleteAll(mChildren); |
| 76 | mChildren.clear(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | int BookmarkItem::childCount() const |
| 81 | { |
| 82 | return mChildren.size(); |
| 83 | } |
| 84 | |
| 85 | int BookmarkItem::columnCount() const |
| 86 | { |
| 87 | return 1; |
| 88 | } |
| 89 | |
| 90 | LibraryModelData BookmarkItem::data(int /*column*/) const |
| 91 | { |
| 92 | return mData; |
| 93 | } |
| 94 | |
| 95 | void BookmarkItem::setData(const LibraryModelData &data) |
| 96 | { |
| 97 | mData.id = data.id; |
| 98 | mData.name = data.name; |
| 99 | mData.type = data.type; |
| 100 | mData.created = data.created; |
| 101 | } |
| 102 | |
| 103 | QModelIndex BookmarkItem::index() const |
| 104 | { |
| 105 | if(mParent==0) |
| 106 | return QModelIndex(); |
| 107 | |
| 108 | return mModel->index(row(),0, mParent->index()); |
| 109 | } |
| 110 | |
| 111 | bool BookmarkItem::hasChildren() const |
| 112 | { |
| 113 | return (!mChildren.isEmpty()); |
| 114 | } |
| 115 | |
| 116 | int BookmarkItem::row() const |
| 117 | { |
| 118 | if(mParent){ |
| 119 | return mParent->mChildren.indexOf(const_cast<BookmarkItem*>(this)); |
| 120 | } |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | int BookmarkItem::rowCount() const |
| 126 | { |
| 127 | return childCount(); |
| 128 | } |
| 129 | |
| 130 | BookmarkItem *BookmarkItem::parent() const |
| 131 | { |
| 132 | return mParent; |
| 133 | } |
| 134 | |
| 135 | BookmarksItemModel *BookmarkItem::model() const |
| 136 | { |
| 137 | return mModel; |
| 138 | } |
| 139 | |
| 140 | void BookmarkItem::removeChild(int row) |
| 141 | { |
| 142 | int minSize = 0; |
| 143 | int maxSize = mChildren.size()-1; |
| 144 | |
| 145 | if(row<minSize) row = minSize; |
| 146 | if(row>maxSize) row = maxSize; |
| 147 | |
| 148 | mChildren.removeAt(row); |
| 149 | } |
| 150 | |
| 151 | void BookmarkItem::setChildren(const QList<BookmarkItem *> childList) |
| 152 | { |
| 153 | mChildren = childList; |
| 154 | } |
| 155 |