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
|
/**************************************************************************
* Copyright (C) 2009 Ben Cooksley <bcooksley@kde.org> *
* Copyright (C) 2007 Will Stephenson <wstephenson@kde.org> *
* *
* 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) any later version. *
* *
* 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, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
* 02110-1301, USA. *
***************************************************************************/
#include "MenuModel.h"
#include <KCategorizedSortFilterProxyModel>
#include <QIcon>
#include "MenuItem.h"
class MenuModel::Private {
public:
Private() {}
MenuItem *rootItem = nullptr;
QList<MenuItem*> exceptions;
};
MenuModel::MenuModel( MenuItem * menuRoot, QObject *parent )
: QAbstractItemModel( parent )
, d( new Private() )
{
d->rootItem = menuRoot;
}
MenuModel::~MenuModel()
{
d->exceptions.clear();
delete d;
}
QHash<int, QByteArray> MenuModel::roleNames() const
{
QHash<int, QByteArray> names = QAbstractItemModel::roleNames();
names[DepthRole] = "DepthRole";
names[IsCategoryRole] = "IsCategoryRole";
names[DefaultIndicatorRole] = "showDefaultIndicator";
return names;
}
int MenuModel::columnCount( const QModelIndex &parent ) const
{
Q_UNUSED( parent );
return 1;
}
int MenuModel::rowCount( const QModelIndex &parent ) const
{
MenuItem * mi;
if ( parent.isValid() ) {
mi = static_cast<MenuItem*>( parent.internalPointer() );
} else {
mi = d->rootItem;
}
return childrenList(mi).count();
}
QVariant MenuModel::data( const QModelIndex &index, int role ) const
{
MenuItem * mi = nullptr;
QVariant theData;
if ( !index.isValid() ) {
return QVariant();
}
mi = static_cast<MenuItem*>( index.internalPointer() );
switch ( role ) {
case Qt::DisplayRole:
theData.setValue( mi->name() );
break;
case Qt::ToolTipRole:
theData.setValue( mi->service()->comment() );
break;
case Qt::DecorationRole:
theData = QVariant( QIcon::fromTheme( mi->service()->icon() ) );
break;
case KCategorizedSortFilterProxyModel::CategorySortRole:
if ( mi->parent() ) {
theData.setValue( QStringLiteral("%1%2").arg( QString::number(mi->parent()->weight()), 5, QLatin1Char('0') ).arg( mi->parent()->name() ) );
}
break;
case KCategorizedSortFilterProxyModel::CategoryDisplayRole: {
MenuItem *candidate = mi->parent();
// The model has an invisible single root item.
// So to get the "root category" we don't go up all the way
// To the actual root, but to the list of the first childs.
// That's why we check for candidate->parent()->parent()
while ( candidate && candidate->parent() && candidate->parent()->parent() ) {
candidate = candidate->parent();
}
if (candidate) {
theData.setValue( candidate->name() );
}
break;
}
case MenuModel::MenuItemRole:
theData.setValue( mi );
break;
case MenuModel::UserFilterRole:
theData.setValue( mi->keywords().join( QString() ) );
break;
case MenuModel::UserSortRole:
theData.setValue( QStringLiteral("%1").arg( QString::number(mi->weight()), 5, QLatin1Char('0') ) );
break;
case MenuModel::DepthRole: {
MenuItem *candidate = mi;
// -1 excludes the invisible root, having main categories at depth 0
int depth = -1;
while ( candidate && candidate->parent() ) {
candidate = candidate->parent();
++depth;
}
theData.setValue( depth );
break;
}
case MenuModel::IsCategoryRole:
theData.setValue( mi->menu() );
break;
case MenuModel::DefaultIndicatorRole:
theData.setValue(mi->showDefaultIndicator());
break;
default:
break;
}
return theData;
}
Qt::ItemFlags MenuModel::flags( const QModelIndex &index ) const
{
if ( !index.isValid() ) {
return Qt::NoItemFlags;
}
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
QModelIndex MenuModel::index( int row, int column, const QModelIndex &parent ) const
{
if ( !hasIndex(row, column, parent) ) {
return QModelIndex();
}
MenuItem *parentItem;
if ( !parent.isValid() ) {
parentItem = d->rootItem;
} else {
parentItem = static_cast<MenuItem*>( parent.internalPointer() );
}
MenuItem *childItem = childrenList(parentItem).value(row);
if ( childItem ) {
return createIndex( row, column, childItem );
} else {
return QModelIndex();
}
}
QModelIndex MenuModel::parent( const QModelIndex &index ) const
{
MenuItem *childItem = static_cast<MenuItem*>( index.internalPointer() );
if( !childItem ) {
return QModelIndex();
}
MenuItem * parent = parentItem(childItem);
MenuItem * grandParent = parentItem(parent);
int childRow = 0;
if( grandParent ) {
childRow = childrenList( grandParent ).indexOf( parent );
}
if ( parent == d->rootItem ) {
return QModelIndex();
}
return createIndex( childRow, 0, parent );
}
QList<MenuItem*> MenuModel::childrenList( MenuItem * parent ) const
{
QList<MenuItem*> children = parent->children();
foreach( MenuItem * child, children ) {
if( d->exceptions.contains( child ) ) {
children.removeOne(child);
children.append(child->children());
}
}
return children;
}
MenuItem * MenuModel::parentItem( MenuItem * child ) const
{
MenuItem * parent = child->parent();
if( d->exceptions.contains(parent) ) {
parent = parentItem(parent);
}
return parent;
}
QModelIndex MenuModel::indexForItem( MenuItem * item ) const
{
MenuItem * parent = parentItem(item);
if (!parent) {
return QModelIndex();
}
const int row = childrenList(parent).indexOf(item);
if (row < 0) {
return QModelIndex();
}
return createIndex(row, 0, item);
}
MenuItem * MenuModel::rootItem() const
{
return d->rootItem;
}
void MenuModel::addException( MenuItem * exception )
{
if( exception == d->rootItem ) {
return;
}
d->exceptions.append(exception);
}
void MenuModel::removeException( MenuItem * exception )
{
d->exceptions.removeAll(exception);
}
|