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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
|
/*
SPDX-FileCopyrightText: 2014-2015 Eike Hein <hein@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "rootmodel.h"
#include "actionlist.h"
#include "kastatsfavoritesmodel.h"
#include "recentusagemodel.h"
#include "systemmodel.h"
#include <KLocalizedString>
#include <QCollator>
GroupEntry::GroupEntry(AppsModel *parentModel, const QString &name, const QString &iconName, AbstractModel *childModel)
: AbstractGroupEntry(parentModel)
, m_name(name)
, m_iconName(iconName)
, m_childModel(childModel)
{
QObject::connect(parentModel, &RootModel::cleared, childModel, &AbstractModel::deleteLater);
QObject::connect(childModel, &AbstractModel::countChanged, [parentModel, this] {
if (parentModel) {
parentModel->entryChanged(this);
}
});
}
QString GroupEntry::name() const
{
return m_name;
}
QString GroupEntry::icon() const
{
return m_iconName;
}
bool GroupEntry::hasChildren() const
{
return m_childModel && m_childModel->count() > 0;
}
AbstractModel *GroupEntry::childModel() const
{
return m_childModel;
}
RootModel::RootModel(QObject *parent)
: AppsModel(QString(), parent)
, m_favorites(new KAStatsFavoritesModel(this))
, m_systemModel(nullptr)
, m_showAllApps(false)
, m_showAllAppsCategorized(false)
, m_showRecentApps(true)
, m_showRecentDocs(true)
, m_recentOrdering(RecentUsageModel::Recent)
, m_showPowerSession(true)
, m_showFavoritesPlaceholder(false)
, m_recentAppsModel(nullptr)
, m_recentDocsModel(nullptr)
{
}
RootModel::~RootModel()
{
}
QVariant RootModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() >= m_entryList.count()) {
return QVariant();
}
if (role == Kicker::HasActionListRole || role == Kicker::ActionListRole) {
const AbstractEntry *entry = m_entryList.at(index.row());
if (entry->type() == AbstractEntry::GroupType) {
const GroupEntry *group = static_cast<const GroupEntry *>(entry);
AbstractModel *model = group->childModel();
if (model == m_recentAppsModel || model == m_recentDocsModel) {
if (role == Kicker::HasActionListRole) {
return true;
} else if (role == Kicker::ActionListRole) {
QVariantList actionList;
actionList << model->actions();
actionList << Kicker::createSeparatorActionItem();
actionList << Kicker::createActionItem(i18n("Hide %1", group->name()), QStringLiteral("view-hidden"), QStringLiteral("hideCategory"));
return actionList;
}
}
}
}
return AppsModel::data(index, role);
}
bool RootModel::trigger(int row, const QString &actionId, const QVariant &argument)
{
const AbstractEntry *entry = m_entryList.at(row);
if (entry->type() == AbstractEntry::GroupType) {
if (actionId == QLatin1String("hideCategory")) {
AbstractModel *model = entry->childModel();
if (model == m_recentAppsModel) {
setShowRecentApps(false);
return true;
} else if (model == m_recentDocsModel) {
setShowRecentDocs(false);
return true;
}
} else if (entry->childModel()->hasActions()) {
return entry->childModel()->trigger(-1, actionId, QVariant());
}
}
return AppsModel::trigger(row, actionId, argument);
}
bool RootModel::showAllApps() const
{
return m_showAllApps;
}
void RootModel::setShowAllApps(bool show)
{
if (m_showAllApps != show) {
m_showAllApps = show;
refresh();
Q_EMIT showAllAppsChanged();
}
}
bool RootModel::showAllAppsCategorized() const
{
return m_showAllAppsCategorized;
}
void RootModel::setShowAllAppsCategorized(bool showCategorized)
{
if (m_showAllAppsCategorized != showCategorized) {
m_showAllAppsCategorized = showCategorized;
refresh();
Q_EMIT showAllAppsCategorizedChanged();
}
}
bool RootModel::showRecentApps() const
{
return m_showRecentApps;
}
void RootModel::setShowRecentApps(bool show)
{
if (show != m_showRecentApps) {
m_showRecentApps = show;
refresh();
Q_EMIT showRecentAppsChanged();
}
}
bool RootModel::showRecentDocs() const
{
return m_showRecentDocs;
}
void RootModel::setShowRecentDocs(bool show)
{
if (show != m_showRecentDocs) {
m_showRecentDocs = show;
refresh();
Q_EMIT showRecentDocsChanged();
}
}
int RootModel::recentOrdering() const
{
return m_recentOrdering;
}
void RootModel::setRecentOrdering(int ordering)
{
if (ordering != m_recentOrdering) {
m_recentOrdering = ordering;
refresh();
Q_EMIT recentOrderingChanged();
}
}
bool RootModel::showPowerSession() const
{
return m_showPowerSession;
}
void RootModel::setShowPowerSession(bool show)
{
if (show != m_showPowerSession) {
m_showPowerSession = show;
refresh();
Q_EMIT showPowerSessionChanged();
}
}
bool RootModel::showFavoritesPlaceholder() const
{
return m_showFavoritesPlaceholder;
}
void RootModel::setShowFavoritesPlaceholder(bool show)
{
if (show != m_showFavoritesPlaceholder) {
m_showFavoritesPlaceholder = show;
refresh();
Q_EMIT showFavoritesPlaceholderChanged();
}
}
AbstractModel *RootModel::favoritesModel()
{
return m_favorites;
}
AbstractModel *RootModel::systemFavoritesModel()
{
if (m_systemModel) {
return m_systemModel->favoritesModel();
}
return nullptr;
}
void RootModel::refresh()
{
if (!m_complete) {
return;
}
beginResetModel();
AppsModel::refreshInternal();
AppsModel *allModel = nullptr;
m_recentAppsModel = nullptr;
m_recentDocsModel = nullptr;
if (m_showAllApps) {
QHash<QString, AbstractEntry *> appsHash;
std::function<void(AbstractEntry *)> processEntry = [&](AbstractEntry *entry) {
if (entry->type() == AbstractEntry::RunnableType) {
AppEntry *appEntry = static_cast<AppEntry *>(entry);
appsHash.insert(appEntry->service()->menuId(), appEntry);
} else if (entry->type() == AbstractEntry::GroupType) {
GroupEntry *groupEntry = static_cast<GroupEntry *>(entry);
AbstractModel *model = groupEntry->childModel();
if (!model) {
return;
}
for (int i = 0; i < model->count(); ++i) {
processEntry(static_cast<AbstractEntry *>(model->index(i, 0).internalPointer()));
}
}
};
for (AbstractEntry *entry : std::as_const(m_entryList)) {
processEntry(entry);
}
QList<AbstractEntry *> apps(appsHash.values());
sortEntries(apps);
if (!m_showAllAppsCategorized && !m_paginate) { // The app list built above goes into a model.
allModel = new AppsModel(apps, false, this);
} else if (m_paginate) { // We turn the apps list into a subtree of pages.
m_favorites = new KAStatsFavoritesModel(this);
Q_EMIT favoritesModelChanged();
QList<AbstractEntry *> groups;
int at = 0;
QList<AbstractEntry *> page;
page.reserve(m_pageSize);
for (AbstractEntry *app : std::as_const(apps)) {
page.append(app);
if (at == (m_pageSize - 1)) {
at = 0;
AppsModel *model = new AppsModel(page, false, this);
groups.append(new GroupEntry(this, QString(), QString(), model));
page.clear();
} else {
++at;
}
}
if (!page.isEmpty()) {
AppsModel *model = new AppsModel(page, false, this);
groups.append(new GroupEntry(this, QString(), QString(), model));
}
groups.prepend(new GroupEntry(this, QString(), QString(), m_favorites));
allModel = new AppsModel(groups, true, this);
} else { // We turn the apps list into a subtree of apps by starting letter.
QList<AbstractEntry *> groups;
QHash<QString, QList<AbstractEntry *>> m_categoryHash;
for (const AbstractEntry *groupEntry : std::as_const(m_entryList)) {
AbstractModel *model = groupEntry->childModel();
if (!model)
continue;
for (int i = 0; i < model->count(); ++i) {
AbstractEntry *appEntry = static_cast<AbstractEntry *>(model->index(i, 0).internalPointer());
// App entry's group stores a transliterated first character of the name. Prefer to use that.
QString name = appEntry->group();
if (name.isEmpty()) {
name = appEntry->name();
}
if (name.isEmpty()) {
continue;
}
const QChar &first = name.at(0).toUpper();
m_categoryHash[first.isDigit() ? QStringLiteral("0-9") : first].append(appEntry);
}
}
QHashIterator<QString, QList<AbstractEntry *>> i(m_categoryHash);
while (i.hasNext()) {
i.next();
AppsModel *model = new AppsModel(i.value(), false, this);
model->setDescription(i.key());
groups.append(new GroupEntry(this, i.key(), QString(), model));
}
allModel = new AppsModel(groups, true, this);
}
allModel->setDescription(QStringLiteral("KICKER_ALL_MODEL")); // Intentionally no i18n.
}
int separatorPosition = 0;
if (allModel) {
m_entryList.prepend(new GroupEntry(this, i18n("All Applications"), QStringLiteral("applications-all-symbolic"), allModel));
++separatorPosition;
}
if (m_showFavoritesPlaceholder) {
// This entry is a placeholder and shouldn't ever be visible
QList<AbstractEntry *> placeholderList;
AppsModel *placeholderModel = new AppsModel(placeholderList, false, this);
// Favorites group containing a placeholder entry, so it would be considered as a group, not an entry
QList<AbstractEntry *> placeholderEntry;
placeholderEntry.append(new GroupEntry(this, //
i18n("This shouldn't be visible! Use KICKER_FAVORITES_MODEL"),
QStringLiteral("dialog-warning"),
placeholderModel));
AppsModel *favoritesPlaceholderModel = new AppsModel(placeholderEntry, false, this);
favoritesPlaceholderModel->setDescription(QStringLiteral("KICKER_FAVORITES_MODEL")); // Intentionally no i18n.
m_entryList.prepend(new GroupEntry(this, i18n("Favorites"), QStringLiteral("bookmarks"), favoritesPlaceholderModel));
++separatorPosition;
}
if (m_showRecentDocs) {
m_recentDocsModel = new RecentUsageModel(this, RecentUsageModel::OnlyDocs, m_recentOrdering);
m_entryList.prepend(new GroupEntry(this,
m_recentOrdering == RecentUsageModel::Recent ? i18n("Recent Files") : i18n("Often Used Files"),
m_recentOrdering == RecentUsageModel::Recent ? QStringLiteral("view-history") : QStringLiteral("office-chart-pie"),
m_recentDocsModel));
++separatorPosition;
}
if (m_showRecentApps) {
m_recentAppsModel = new RecentUsageModel(this, RecentUsageModel::OnlyApps, m_recentOrdering);
m_entryList.prepend(new GroupEntry(this,
m_recentOrdering == RecentUsageModel::Recent ? i18n("Recent Applications") : i18n("Often Used Applications"),
m_recentOrdering == RecentUsageModel::Recent ? QStringLiteral("view-history") : QStringLiteral("office-chart-pie"),
m_recentAppsModel));
++separatorPosition;
}
if (m_showSeparators && separatorPosition > 0) {
m_entryList.insert(separatorPosition, new SeparatorEntry(this));
++m_separatorCount;
}
m_systemModel = new SystemModel(this);
QObject::connect(m_systemModel, &SystemModel::sessionManagementStateChanged, this, &RootModel::refresh);
if (m_showPowerSession) {
m_entryList << new GroupEntry(this, i18n("Power / Session"), QStringLiteral("system-log-out"), m_systemModel);
}
endResetModel();
m_favorites->refresh();
Q_EMIT systemFavoritesModelChanged();
Q_EMIT countChanged();
Q_EMIT separatorCountChanged();
Q_EMIT refreshed();
}
#include "moc_rootmodel.cpp"
|