1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
/*
* This file is part of KDevelop
* Copyright 2013 Milian Wolff <mail@milianw.de>
*
* 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 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* 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 "filtermodel.h"
#include <QIcon>
#include <KLocalizedString>
using namespace KDevelop;
FilterModel::FilterModel(QObject* parent)
: QAbstractTableModel(parent)
, m_ignoredLastInsert(false)
{
}
FilterModel::~FilterModel()
{
}
SerializedFilters FilterModel::filters() const
{
return m_filters;
}
void FilterModel::setFilters(const SerializedFilters& filters)
{
beginResetModel();
m_filters = filters;
endResetModel();
}
void FilterModel::moveFilterUp(int row)
{
beginMoveRows(QModelIndex(), row, row, QModelIndex(), row - 1);
qSwap(m_filters[row], m_filters[row - 1]);
endMoveRows();
}
void FilterModel::moveFilterDown(int row)
{
beginMoveRows(QModelIndex(), row, row, QModelIndex(), row + 2);
qSwap(m_filters[row], m_filters[row + 1]);
endMoveRows();
}
int FilterModel::rowCount(const QModelIndex& parent) const
{
if (parent.isValid()) {
return 0;
}
return m_filters.size();
}
int FilterModel::columnCount(const QModelIndex& /*parent*/) const
{
return NUM_COLUMNS;
}
QVariant FilterModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation != Qt::Horizontal || role != Qt::DisplayRole) {
return QVariant();
}
Q_ASSERT(section >= 0 && section < NUM_COLUMNS);
if (section == Pattern) {
return i18nc("@title:column", "Pattern");
} else if (section == Targets) {
return i18nc("@title:column", "Targets");
} else if (section == Inclusive) {
return i18nc("@title:column", "Action");
}
return QVariant();
}
QVariant FilterModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
Q_ASSERT(!index.parent().isValid());
Q_ASSERT(index.row() >= 0 && index.row() < m_filters.size());
Q_ASSERT(index.column() >= 0 && index.column() < NUM_COLUMNS);
if (role != Qt::DisplayRole && role != Qt::DecorationRole
&& role != Qt::EditRole && role != Qt::ToolTipRole)
{
return QVariant();
}
const SerializedFilter& filter = m_filters.at(index.row());
const int column = index.column();
if (column == Pattern) {
if (role == Qt::DecorationRole) {
return QVariant();
} else if (role == Qt::ToolTipRole) {
return i18n(
"The wildcard pattern defines whether a file or folder is included in a project or not.<br />"
"The pattern is matched case-sensitively against the items relative path to the project root. "
"The relative path starts with a forward slash, trailing slashes of folders are removed.<br />"
"Patterns ending on <code>\"/\"</code> are implicitly considered to match against folders only.<br />"
"Patterns which do not explicitly start with either <code>\"/\"</code> or <code>\"*\"</code> implicitly get <code>\"*/\"</code> prepended and thus match any item with a relative path ending on the given pattern."
);
}
return filter.pattern;
} else if (column == Targets) {
if (role == Qt::EditRole) {
return static_cast<int>(filter.targets);
} else if (role == Qt::ToolTipRole) {
return i18n("The target defines what type of item the filter is matched against.<br />Filters either apply only to files, only to folders or to both.");
}
if (filter.targets & Filter::Files && filter.targets & Filter::Folders) {
if (role == Qt::DecorationRole) {
return QIcon::fromTheme(QStringLiteral("document-open"));
}
return i18nc("@item", "Files and Folders");
} else if (filter.targets & Filter::Folders) {
if (role == Qt::DecorationRole) {
return QIcon::fromTheme(QStringLiteral("folder"));
}
return i18nc("@item", "Folders");
} else {
if (role == Qt::DecorationRole) {
return QIcon::fromTheme(QStringLiteral("text-plain"));
}
return i18nc("@item", "Files");
}
} else if (column == Inclusive) {
if (role == Qt::EditRole) {
return static_cast<int>(filter.type);
} else if (role == Qt::ToolTipRole) {
return i18n("Filters by default exclude items from the project. Inclusive patterns can be used to include items which where matched by previous exclusive patterns.<br />E.g. to only include files ending on <code>\".cpp\"</code> in your project, you could exclude all files via <code>\"*\"</code> and then apply an inclusive <code>\"*.cpp\"</code> pattern.");
}
if (filter.type == Filter::Inclusive) {
if (role == Qt::DecorationRole) {
return QIcon::fromTheme(QStringLiteral("list-add"));
}
return i18nc("@item", "Include");
} else {
if (role == Qt::DecorationRole) {
return QIcon::fromTheme(QStringLiteral("list-remove"));
}
return i18nc("@item", "Exclude");
}
}
return QVariant();
}
bool FilterModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
if (!index.isValid()) {
return false;
}
Q_ASSERT(!index.parent().isValid());
Q_ASSERT(index.row() >= 0 && index.row() < m_filters.size());
Q_ASSERT(index.column() >= 0 && index.column() < NUM_COLUMNS);
if (role != Qt::EditRole && role != Qt::DisplayRole) {
return false;
}
SerializedFilter& filter = m_filters[index.row()];
const int column = index.column();
if (column == Pattern) {
filter.pattern = value.toString();
} else if (column == Targets) {
filter.targets = static_cast<Filter::Targets>(value.toInt());
} else if (column == Inclusive) {
filter.type = static_cast<Filter::Type>(value.toInt());
}
emit dataChanged(index, index);
return true;
}
Qt::DropActions FilterModel::supportedDropActions() const
{
return Qt::MoveAction;
}
Qt::ItemFlags FilterModel::flags(const QModelIndex& index) const
{
Qt::ItemFlags baseFlags = QAbstractTableModel::flags(index);
if (index.isValid() && !index.parent().isValid()) {
return baseFlags | Qt::ItemIsEditable | Qt::ItemIsDragEnabled;
}
return baseFlags | Qt::ItemIsDropEnabled;
}
bool FilterModel::insertRows(int row, int count, const QModelIndex& parent)
{
Q_ASSERT(!parent.isValid());
Q_ASSERT(count == 1);
if (row == -1) {
// after end of list and we cannot just append either as then the
// later setData events will fails...
m_ignoredLastInsert = true;
return false;
}
m_ignoredLastInsert = false;
Q_ASSERT(row >= 0 && row <= m_filters.size());
Q_ASSERT(row + count - 1 <= m_filters.size());
beginInsertRows(parent, row, row + count - 1);
for (int i = 0; i < count; ++i) {
m_filters.insert(row + i, SerializedFilter());
}
endInsertRows();
return true;
}
bool FilterModel::removeRows(int row, int count, const QModelIndex& parent)
{
Q_ASSERT(!parent.isValid());
Q_ASSERT(count == 1);
Q_ASSERT(row >= 0 && row < m_filters.size());
Q_ASSERT(row + count <= m_filters.size());
if (m_ignoredLastInsert) {
return false;
}
beginRemoveRows(parent, row, row + count - 1);
m_filters.remove(row, count);
endRemoveRows();
return true;
}
QMap<int, QVariant> FilterModel::itemData(const QModelIndex& index) const
{
QMap<int, QVariant> ret;
if (!index.isValid()) {
return ret;
}
Q_ASSERT(!index.parent().isValid());
Q_ASSERT(index.row() >= 0 && index.row() < m_filters.size());
const SerializedFilter& filter = m_filters.at(index.row());
ret.insert(Qt::UserRole + Pattern, filter.pattern);
ret.insert(Qt::UserRole + Inclusive, static_cast<int>(filter.type));
ret.insert(Qt::UserRole + Targets, static_cast<int>(filter.targets));
return ret;
}
bool FilterModel::setItemData(const QModelIndex& index, const QMap< int, QVariant >& roles)
{
Q_ASSERT(index.isValid());
Q_ASSERT(!index.parent().isValid());
Q_ASSERT(index.row() >= 0 && index.row() < m_filters.size());
Q_ASSERT(roles.size() == 3);
Q_ASSERT(roles.contains(Qt::UserRole + Pattern));
Q_ASSERT(roles.contains(Qt::UserRole + Inclusive));
Q_ASSERT(roles.contains(Qt::UserRole + Targets));
if (m_ignoredLastInsert) {
return false;
}
SerializedFilter& filter = m_filters[index.row()];
filter.pattern = roles[Qt::UserRole + Pattern].toString();
filter.type = Filter::Type(roles[Qt::UserRole + Inclusive].toInt());
filter.targets = Filter::Targets(roles[Qt::UserRole + Targets].toInt());
return true;
}
|