| 1 | /* |
| 2 | Copyright (C) 2005-2014 Mario Stephan <[email protected]> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU Lesser General Public License as published |
| 6 | by the Free Software Foundation; either version 2.1 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public License |
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include "collectionsetupmodel.h" |
| 19 | #include <QDirModel> |
| 20 | #include <QTreeView> |
| 21 | |
| 22 | CollectionSetupModel::CollectionSetupModel() |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | Qt::ItemFlags CollectionSetupModel::flags(const QModelIndex& index) const |
| 27 | { |
| 28 | Qt::ItemFlags f = QDirModel::flags(index); |
| 29 | if (index.column() == 0) // make the first column checkable |
| 30 | f |= Qt::ItemIsUserCheckable; |
| 31 | return f; |
| 32 | } |
| 33 | |
| 34 | QVariant CollectionSetupModel::data(const QModelIndex& index, int role) const |
| 35 | { |
| 36 | if (index.isValid() && index.column() == 0 && role == Qt::CheckStateRole) { |
| 37 | // the item is checked only if we have stored its path |
| 38 | if (checkedPartially.contains(filePath(index))) { |
| 39 | return Qt::PartiallyChecked; |
| 40 | } else |
| 41 | return (checked.contains(filePath(index)) ? Qt::Checked : Qt::Unchecked); |
| 42 | } |
| 43 | return QDirModel::data(index, role); |
| 44 | } |
| 45 | |
| 46 | bool CollectionSetupModel::setData(const QModelIndex& index, const QVariant& value, int role) |
| 47 | { |
| 48 | if (index.isValid() && index.column() == 0 && role == Qt::CheckStateRole) { |
| 49 | // store checked paths, remove unchecked paths |
| 50 | QModelIndex idx = parent(index); |
| 51 | if (value.toInt() == Qt::Checked) { |
| 52 | checked.insert(filePath(index)); |
| 53 | // make parents always partially checked |
| 54 | while (idx.isValid()) { |
| 55 | checkedPartially.insert(filePath(idx)); |
| 56 | checked.remove(filePath(idx)); |
| 57 | idx = idx.parent(); |
| 58 | } |
| 59 | } else { |
| 60 | checked.remove(filePath(index)); |
| 61 | // make parent unchecked if index was his only child |
| 62 | while (idx.isValid()) { |
| 63 | bool hasChildren = false; |
| 64 | foreach (const QString& value, checked) |
| 65 | if (value.contains(filePath(idx))) |
| 66 | hasChildren = true; |
| 67 | if (!hasChildren) { |
| 68 | checkedPartially.remove(filePath(idx)); |
| 69 | checked.insert(filePath(idx)); |
| 70 | } |
| 71 | idx = idx.parent(); |
| 72 | } |
| 73 | } |
| 74 | emit dataChanged(parent(index), index); |
| 75 | return true; |
| 76 | } |
| 77 | return QDirModel::setData(index, value, role); |
| 78 | } |
| 79 | |
| 80 | QVariant CollectionSetupModel::headerData(int section, Qt::Orientation orientation, int role) const |
| 81 | { |
| 82 | if (orientation == Qt::Horizontal) { |
| 83 | if (role != Qt::DisplayRole) |
| 84 | return QVariant(); |
| 85 | switch (section) { |
| 86 | case 0: |
| 87 | return QString(tr("Select folders for music collection")); |
| 88 | default: |
| 89 | return QVariant(); |
| 90 | } |
| 91 | } |
| 92 | return QAbstractItemModel::headerData(section, orientation, role); |
| 93 | } |
| 94 | |
| 95 | QStringList CollectionSetupModel::dirsChecked() |
| 96 | { |
| 97 | QStringList list; |
| 98 | foreach (const QString& value, checked) |
| 99 | list << value; |
| 100 | return list; |
| 101 | } |
| 102 | |
| 103 | void CollectionSetupModel::setDirsChecked(QStringList list) |
| 104 | { |
| 105 | checked.clear(); |
| 106 | checkedPartially.clear(); |
| 107 | foreach (const QString& value, list) { |
| 108 | /* index parents Qt::PartiallyChecked */ |
| 109 | QModelIndex idx = parent(index(value)); |
| 110 | while (idx.isValid()) { |
| 111 | checkedPartially.insert(filePath(idx)); |
| 112 | idx = idx.parent(); |
| 113 | } |
| 114 | checked.insert(value); |
| 115 | } |
| 116 | } |