SidebarMode.cpp 21.5 KB
Newer Older
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
/**************************************************************************
 * Copyright (C) 2009 by Ben Cooksley <bcooksley@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 "SidebarMode.h"

#include "MenuItem.h"
#include "MenuModel.h"
#include "ModuleView.h"
#include "MenuProxyModel.h"
#include "BaseData.h"
Marco Martin's avatar
Marco Martin committed
27
#include "ToolTips/tooltipmanager.h"
28
29
30
31
32

#include <QHBoxLayout>

#include <QAction>
#include <KAboutData>
33
#include <KCModuleInfo>
34
35
36
#include <KStandardAction>
#include <KLocalizedString>
#include <KIconLoader>
Marco Martin's avatar
Marco Martin committed
37
#include <KLocalizedContext>
38
#include <KServiceTypeTrader>
Marco Martin's avatar
Marco Martin committed
39
40
#include <KXmlGuiWindow>
#include <KActionCollection>
Marco Martin's avatar
Marco Martin committed
41
42
#include <KPackage/Package>
#include <KPackage/PackageLoader>
43
#include <KDeclarative/KDeclarative>
Marco Martin's avatar
Marco Martin committed
44
#include <QStandardItemModel>
Marco Martin's avatar
Marco Martin committed
45
46
47
#include <QQuickWidget>
#include <QQmlEngine>
#include <QQmlContext>
Marco Martin's avatar
Marco Martin committed
48
49
#include <QGraphicsOpacityEffect>
#include <QLabel>
50
#include <QMenu>
51
52
#include <QDebug>

53
54
55
56
57
58
59
60
61
#include <KActivities/Stats/ResultModel>
#include <KActivities/Stats/ResultSet>
#include <KActivities/Stats/Terms>

namespace KAStats = KActivities::Stats;

using namespace KAStats;
using namespace KAStats::Terms;

62
63
K_PLUGIN_FACTORY( SidebarModeFactory, registerPlugin<SidebarMode>(); )

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
FocusHackWidget::FocusHackWidget(QWidget *parent)
    : QWidget(parent)
{}
FocusHackWidget::~FocusHackWidget()
{}

void FocusHackWidget::focusNext()
{
    focusNextChild();
}

void FocusHackWidget::focusPrevious()
{
    focusNextPrevChild(false);
}

80
81
82
class SubcategoryModel : public QStandardItemModel
{
public:
Laurent Montel's avatar
Laurent Montel committed
83
    explicit SubcategoryModel(QAbstractItemModel *parentModel, QObject *parent = nullptr)
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
        : QStandardItemModel(parent),
          m_parentModel(parentModel)
    {}

    void setParentIndex(const QModelIndex &activeModule)
    {
        blockSignals(true);
        //make the view receive a single signal when the new subcategory is loaded,
        //never make the view believe there are zero items if this is not the final count
        //this avoids the brief flash it had
        clear();
        const int subRows = m_parentModel->rowCount(activeModule);
        if ( subRows > 1) {
            for (int i = 0; i < subRows; ++i) {
                const QModelIndex& index = m_parentModel->index(i, 0, activeModule);
                QStandardItem *item = new QStandardItem(m_parentModel->data(index, Qt::DecorationRole).value<QIcon>(), m_parentModel->data(index, Qt::DisplayRole).toString());
                item->setData(index.data(Qt::UserRole), Qt::UserRole);
                appendRow(item);
            }
        }
        blockSignals(false);
        beginResetModel();
        endResetModel();
    }

private:
    QAbstractItemModel *m_parentModel;
};

113
114
115
class MostUsedModel : public QSortFilterProxyModel
{
public:
Laurent Montel's avatar
Laurent Montel committed
116
    explicit MostUsedModel(QObject *parent = nullptr)
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
        : QSortFilterProxyModel (parent)
    {
        sort(0, Qt::DescendingOrder);
        setSortRole(ResultModel::ScoreRole);
        setDynamicSortFilter(true);
        //prepare default items
        m_defaultModel = new QStandardItemModel(this);
        QStandardItem *item = new QStandardItem();
        item->setData(QUrl(QStringLiteral("kcm:kcm_lookandfeel.desktop")), ResultModel::ResourceRole);
        m_defaultModel->appendRow(item);
        item = new QStandardItem();
        item->setData(QUrl(QStringLiteral("kcm:user_manager.desktop")), ResultModel::ResourceRole);
        m_defaultModel->appendRow(item);
        item = new QStandardItem();
        item->setData(QUrl(QStringLiteral("kcm:screenlocker.desktop")), ResultModel::ResourceRole);
        m_defaultModel->appendRow(item);
        item = new QStandardItem();
        item->setData(QUrl(QStringLiteral("kcm:powerdevilprofilesconfig.desktop")), ResultModel::ResourceRole);
        m_defaultModel->appendRow(item);
        item = new QStandardItem();
        item->setData(QUrl(QStringLiteral("kcm:kcm_kscreen.desktop")), ResultModel::ResourceRole);
        m_defaultModel->appendRow(item);
    }

    void setResultModel(ResultModel *model)
    {
        if (m_resultModel == model) {
            return;
        }

        auto updateModel = [this]() {
            if (m_resultModel->rowCount() >= 5) {
                setSourceModel(m_resultModel);
            } else {
                setSourceModel(m_defaultModel);
            }
        };

        m_resultModel = model;

        connect(m_resultModel, &QAbstractItemModel::rowsInserted, this, updateModel);
        connect(m_resultModel, &QAbstractItemModel::rowsRemoved, this, updateModel);

        updateModel();
    }

Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
163
    QHash<int, QByteArray> roleNames() const override
164
165
166
167
168
169
170
171
    {
        QHash<int, QByteArray> roleNames;
        roleNames.insert(Qt::DisplayRole, "display");
        roleNames.insert(Qt::DecorationRole, "decoration");
        roleNames.insert(ResultModel::ScoreRole, "score");
        return roleNames;
    }

172
173
174
175
176
177
178
    bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
    {
        const QString desktopName = sourceModel()->index(source_row, 0, source_parent).data(ResultModel::ResourceRole).toUrl().path();
        KService::Ptr service = KService::serviceByStorageId(desktopName);
        return service;
    }

179
180
181
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
    {
        MenuItem *mi;
182
        const QString desktopName = QSortFilterProxyModel::data(index, ResultModel::ResourceRole).toUrl().path();
183
184
185
186
187
188
189
190
191
192

        if (m_menuItems.contains(desktopName)) {
            mi = m_menuItems.value(desktopName);
        } else {
            mi = new MenuItem(false, nullptr);
            const_cast<MostUsedModel *>(this)->m_menuItems.insert(desktopName, mi);

            KService::Ptr service = KService::serviceByStorageId(desktopName);

            if (!service || !service->isValid()) {
193
194
                qWarning()<<desktopName;
                m_resultModel->forgetResource(QStringLiteral("kcm:") % desktopName);
195
196
197
198
199
200
201
202
203
                return QVariant();
            }
            mi->setService(service);
        }

        switch (role) {
            case Qt::UserRole:
                return QVariant::fromValue(mi);
            case Qt::DisplayRole:
204
205
206
207
208
                if (mi->service() && mi->service()->isValid()) {
                    return mi->service()->name();
                } else {
                    return QVariant();
                }
209
            case Qt::DecorationRole:
210
211
212
213
214
                if (mi->service() && mi->service()->isValid()) {
                    return mi->service()->icon();
                } else {
                    return QVariant();
                }
215
            case ResultModel::ScoreRole:
216
                return QSortFilterProxyModel::data(index, ResultModel::ScoreRole).toInt();
217
218
219
220
221
222
223
224
225
226
227
            default:
                return QVariant();
        }
    }

private:
    QHash<QString, MenuItem *> m_menuItems;
    QStandardItemModel *m_defaultModel;
    ResultModel *m_resultModel;
};

228
229
class SidebarMode::Private {
public:
Marco Martin's avatar
Marco Martin committed
230
231
232
233
234
235
236
237
    Private()
      : quickWidget( nullptr ),
        moduleView( nullptr ),
        collection( nullptr ),
        activeCategory( -1 ),
        activeSubCategory( -1 )
    {}

238
239
240
241
    virtual ~Private() {
        delete aboutIcon;
    }

Laurent Montel's avatar
Laurent Montel committed
242
    ToolTipManager *toolTipManager = nullptr;
Nate Graham's avatar
Nate Graham committed
243
    ToolTipManager *subCategoryToolTipManager = nullptr;
Tien Do Nam's avatar
Tien Do Nam committed
244
    ToolTipManager *mostUsedToolTipManager = nullptr;
Laurent Montel's avatar
Laurent Montel committed
245
    QQuickWidget * quickWidget = nullptr;
Marco Martin's avatar
Marco Martin committed
246
    KPackage::Package package;
Laurent Montel's avatar
Laurent Montel committed
247
248
249
250
251
    SubcategoryModel * subCategoryModel = nullptr;
    MostUsedModel * mostUsedModel = nullptr;
    FocusHackWidget * mainWidget = nullptr;
    QQuickWidget * placeHolderWidget = nullptr;
    QHBoxLayout * mainLayout = nullptr;
252
    KDeclarative::KDeclarative kdeclarative;
Laurent Montel's avatar
Laurent Montel committed
253
254
255
256
257
    MenuProxyModel * categorizedModel = nullptr;
    MenuProxyModel * searchModel = nullptr;
    KAboutData * aboutIcon = nullptr;
    ModuleView * moduleView = nullptr;
    KActionCollection *collection = nullptr;
Marco Martin's avatar
Marco Martin committed
258
    QPersistentModelIndex activeCategoryIndex;
Marco Martin's avatar
Marco Martin committed
259
260
    int activeCategory;
    int activeSubCategory;
261
262
263
264
265
266
267
268
269
    bool m_actionMenuVisible = false;
    void setActionMenuVisible(SidebarMode* sidebarMode, const bool &actionMenuVisible)
    {
        if (m_actionMenuVisible == actionMenuVisible) {
            return;
        }
        m_actionMenuVisible = actionMenuVisible;
        emit sidebarMode->actionMenuVisibleChanged();
    }
270
271
272
273
274
275
};

SidebarMode::SidebarMode( QObject *parent, const QVariantList& )
    : BaseMode( parent )
    , d( new Private() )
{
Marco Martin's avatar
Marco Martin committed
276
    qApp->setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);
277
278
    d->aboutIcon = new KAboutData( QStringLiteral("SidebarView"), i18n( "Sidebar View" ),
                                 QStringLiteral("1.0"), i18n( "Provides a categorized sidebar for control modules." ),
279
                                 KAboutLicense::GPL, i18n( "(c) 2017, Marco Martin" ) );
280
281
282
    d->aboutIcon->addAuthor( i18n( "Marco Martin" ), i18n( "Author" ), QStringLiteral("mart@kde.org") );
    d->aboutIcon->addAuthor( i18n( "Ben Cooksley" ), i18n( "Author" ), QStringLiteral("bcooksley@kde.org") );
    d->aboutIcon->addAuthor( i18n( "Mathias Soeken" ), i18n( "Developer" ), QStringLiteral("msoeken@informatik.uni-bremen.de") );
283
284

    qmlRegisterType<QAction>();
285
    qmlRegisterType<QAbstractItemModel>();
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
}

SidebarMode::~SidebarMode()
{
    delete d;
}

KAboutData * SidebarMode::aboutData()
{
    return d->aboutIcon;
}

ModuleView * SidebarMode::moduleView() const
{
    return d->moduleView;
}

QWidget * SidebarMode::mainWidget()
{
Marco Martin's avatar
Marco Martin committed
305
    if( !d->quickWidget ) {
306
307
308
309
310
        initWidget();
    }
    return d->mainWidget;
}

Marco Martin's avatar
Marco Martin committed
311
312
QAbstractItemModel * SidebarMode::categoryModel() const
{
313
    return d->searchModel;
Marco Martin's avatar
Marco Martin committed
314
315
316
317
318
319
320
}

QAbstractItemModel * SidebarMode::subCategoryModel() const
{
    return d->subCategoryModel;
}

321
322
323
324
325
QAbstractItemModel * SidebarMode::mostUsedModel() const
{
    return d->mostUsedModel;
}

326
327
328
QList<QAbstractItemView*> SidebarMode::views() const
{
    QList<QAbstractItemView*> list;
Marco Martin's avatar
Marco Martin committed
329
    //list.append( d->categoryView );
330
331
332
333
334
335
336
337
338
339
    return list;
}

void SidebarMode::initEvent()
{
    MenuModel * model = new MenuModel( rootItem(), this );
    foreach( MenuItem * child, rootItem()->children() ) {
        model->addException( child );
    }

340
341
342
343
344
    d->categorizedModel = new MenuProxyModel( this );
    d->categorizedModel->setCategorizedModel( true );
    d->categorizedModel->setSourceModel( model );
    d->categorizedModel->sort( 0 );
    d->categorizedModel->setFilterHighlightsEntries( false );
Marco Martin's avatar
Marco Martin committed
345

346
347
348
349
    d->searchModel = new MenuProxyModel( this );
    d->searchModel->setFilterHighlightsEntries( false );
    d->searchModel->setSourceModel( d->categorizedModel );
    connect( d->searchModel, &MenuProxyModel::filterRegExpChanged, this, [this] () {
Marco Martin's avatar
Marco Martin committed
350
351
352
353
354
        if (d->activeCategoryIndex.isValid() && d->activeCategoryIndex.row() >= 0) {
            d->subCategoryModel->setParentIndex( d->activeCategoryIndex );
            emit activeCategoryChanged();
        }
    });
355

356
357
    d->mostUsedModel = new MostUsedModel( this );

358
    d->subCategoryModel = new SubcategoryModel( d->searchModel, this );
359
    d->mainWidget = new FocusHackWidget();
360
    d->mainWidget->installEventFilter(this);
361
362
363
364
    d->mainLayout = new QHBoxLayout(d->mainWidget);
    d->mainLayout->setContentsMargins(0, 0, 0, 0);
    d->moduleView = new ModuleView( d->mainWidget );
    connect( d->moduleView, &ModuleView::moduleChanged, this, &SidebarMode::moduleLoaded );
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
365
    d->quickWidget = nullptr;
Marco Martin's avatar
Marco Martin committed
366
    moduleView()->setFaceType(KPageView::Plain);
367
368
}

369
QAction *SidebarMode::action(const QString &name) const
Marco Martin's avatar
Marco Martin committed
370
371
{
    if (!d->collection) {
372
        return nullptr;
Marco Martin's avatar
Marco Martin committed
373
374
    }

375
376
377
378
379
380
381
    return d->collection->action(name);
}

QString SidebarMode::actionIconName(const QString &name) const
{
    if (QAction *a = action(name)) {
        return a->icon().name();
Marco Martin's avatar
Marco Martin committed
382
    }
383
384

    return QString();
Marco Martin's avatar
Marco Martin committed
385
386
}

Marco Martin's avatar
Marco Martin committed
387
388
void SidebarMode::requestToolTip(int index, const QRectF &rect)
{
Marco Martin's avatar
Marco Martin committed
389
    if (showToolTips()) {
390
        d->toolTipManager->requestToolTip(d->searchModel->index(index, 0), rect.toRect());
Marco Martin's avatar
Marco Martin committed
391
    }
Marco Martin's avatar
Marco Martin committed
392
393
}

Nate Graham's avatar
Nate Graham committed
394
395
396
397
398
399
400
void SidebarMode::requestSubCategoryToolTip(int index, const QRectF &rect)
{
    if (showToolTips()) {
        d->subCategoryToolTipManager->requestToolTip(d->subCategoryModel->index(index, 0), rect.toRect());
    }
}

Tien Do Nam's avatar
Tien Do Nam committed
401
402
403
404
405
406
407
void SidebarMode::requestMostUsedToolTip(int index, const QRectF &rect)
{
    if (showToolTips()) {
        d->mostUsedToolTipManager->requestToolTip(d->mostUsedModel->index(index, 0), rect.toRect());
    }
}

Marco Martin's avatar
Marco Martin committed
408
409
410
411
412
void SidebarMode::hideToolTip()
{
    d->toolTipManager->hideToolTip();
}

Nate Graham's avatar
Nate Graham committed
413
414
415
416
417
void SidebarMode::hideSubCategoryToolTip()
{
    d->subCategoryToolTipManager->hideToolTip();
}

Tien Do Nam's avatar
Tien Do Nam committed
418
419
420
421
422
void SidebarMode::hideMostUsedToolTip()
{
    d->mostUsedToolTipManager->hideToolTip();
}

Laurent Montel's avatar
Laurent Montel committed
423
void SidebarMode::loadMostUsed(int index)
424
425
426
427
428
429
{
    const QModelIndex idx = d->mostUsedModel->index(index, 0);
    d->moduleView->closeModules();
    d->moduleView->loadModule( idx );
}

430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
void SidebarMode::showActionMenu(const QPoint &position)
{
    QMenu *menu = new QMenu();
    connect(menu, &QMenu::aboutToHide, this, [this] () { d->setActionMenuVisible(this, false); } );
    menu->setAttribute(Qt::WA_DeleteOnClose);

    const QStringList actionList { QStringLiteral("configure"), QStringLiteral("help_contents"), QStringLiteral("help_about_app"), QStringLiteral("help_about_kde") };
    for (const QString &actionName : actionList) {
        menu->addAction(d->collection->action(actionName));
    }

    menu->popup(position);
    d->setActionMenuVisible(this, true);
}

445
446
447
void SidebarMode::changeModule( const QModelIndex& activeModule )
{
    d->moduleView->closeModules();
Marco Martin's avatar
Marco Martin committed
448

449
    const int subRows = d->searchModel->rowCount(activeModule);
Marco Martin's avatar
Marco Martin committed
450
451
452
    if ( subRows < 2) {
        d->moduleView->loadModule( activeModule );
    } else {
453
        d->moduleView->loadModule( d->searchModel->index(0, 0, activeModule) );
Marco Martin's avatar
Marco Martin committed
454
    }
455
456

    d->subCategoryModel->setParentIndex( activeModule );
457
458
459
460
}

void SidebarMode::moduleLoaded()
{
Marco Martin's avatar
Marco Martin committed
461
462
    d->placeHolderWidget->hide();
    d->moduleView->show();
463
464
}

Marco Martin's avatar
Marco Martin committed
465
466
int SidebarMode::activeCategory() const
{
467
    return d->searchModel->mapFromSource(d->searchModel->sourceModel()->index(d->activeCategory, 0)).row();
Marco Martin's avatar
Marco Martin committed
468
469
470
471
}

void SidebarMode::setActiveCategory(int cat)
{
Marco Martin's avatar
Marco Martin committed
472
473
474
    const QModelIndex idx = d->searchModel->index(cat, 0);
    const int newCategoryRow = d->searchModel->mapToSource(idx).row();

475
476
477
478
    if (d->activeCategory == newCategoryRow) {
        return;
    }
    if( !d->moduleView->resolveChanges() ) {
Marco Martin's avatar
Marco Martin committed
479
480
481
        return;
    }

Marco Martin's avatar
Marco Martin committed
482
    d->activeCategoryIndex = idx;
Marco Martin's avatar
Marco Martin committed
483
484
    d->activeCategory = newCategoryRow;

485
    changeModule(idx);
Marco Martin's avatar
Marco Martin committed
486
487
488
489
490
491
492
493
494
495
496
    d->activeSubCategory = 0;
    emit activeCategoryChanged();
    emit activeSubCategoryChanged();
}

void SidebarMode::setActiveSubCategory(int cat)
{
    if (d->activeSubCategory == cat) {
        return;
    }

497
498
499
500
    if( !d->moduleView->resolveChanges() ) {
        return;
    }

Marco Martin's avatar
Marco Martin committed
501
502
503
504
505
506
    d->activeSubCategory = cat;
    d->moduleView->closeModules();
    d->moduleView->loadModule( d->subCategoryModel->index(cat, 0) );
    emit activeSubCategoryChanged();
}

507
508
509
510
511
int SidebarMode::width() const
{
    return d->mainWidget->width();
}

512
513
514
515
516
517
518
519
520
521
bool SidebarMode::actionMenuVisible() const
{
    return d->m_actionMenuVisible;
}

int SidebarMode::activeSubCategory() const
{
    return d->activeSubCategory;
}

522
523
524
void SidebarMode::initWidget()
{
    // Create the widgets
Marco Martin's avatar
Marco Martin committed
525

Marco Martin's avatar
Marco Martin committed
526
527
528
    if (!KMainWindow::memberList().isEmpty()) {
        KXmlGuiWindow *mainWindow = qobject_cast<KXmlGuiWindow *>(KMainWindow::memberList().first());
        if (mainWindow) {
Marco Martin's avatar
Marco Martin committed
529
            d->collection = mainWindow->actionCollection();
Marco Martin's avatar
Marco Martin committed
530
531
        }
    }
Marco Martin's avatar
Marco Martin committed
532
533

    d->quickWidget = new QQuickWidget(d->mainWidget);
534
    d->quickWidget->quickWindow()->setTitle(i18n("Sidebar"));
Marco Martin's avatar
Marco Martin committed
535
    d->quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
536

537
538
    d->quickWidget->engine()->rootContext()->setContextProperty(QStringLiteral("systemsettings"), this);
    d->package = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("KPackage/GenericQML"));
Marco Martin's avatar
Marco Martin committed
539
    d->package.setPath(QStringLiteral("org.kde.systemsettings.sidebar"));
540
541

    d->kdeclarative.setDeclarativeEngine(d->quickWidget->engine());
542
543
    d->kdeclarative.setupEngine(d->quickWidget->engine());
    d->kdeclarative.setupContext();
544

545
    d->quickWidget->setSource(QUrl::fromLocalFile(d->package.filePath("mainscript")));
546

547
548
549
550
551
552
    if (!d->quickWidget->rootObject()) {
        for (const auto &err : d->quickWidget->errors()) {
            qWarning() << err.toString();
        }
        qFatal("Fatal error while loading the sidebar view qml component");
    }
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
    const int rootImplicitWidth = d->quickWidget->rootObject()->property("implicitWidth").toInt();
    if (rootImplicitWidth != 0) {
        d->quickWidget->setFixedWidth(rootImplicitWidth);
    } else {
        d->quickWidget->setFixedWidth(240);
    }
    connect(d->quickWidget->rootObject(), &QQuickItem::implicitWidthChanged,
            this, [this]() {
                const int rootImplicitWidth = d->quickWidget->rootObject()->property("implicitWidth").toInt();
                if (rootImplicitWidth != 0) {
                    d->quickWidget->setFixedWidth(rootImplicitWidth);
                } else {
                    d->quickWidget->setFixedWidth(240);
                }
            });
568
569
    connect(d->quickWidget->rootObject(), SIGNAL(focusNextRequest()), d->mainWidget, SLOT(focusNext()));
    connect(d->quickWidget->rootObject(), SIGNAL(focusPreviousRequest()), d->mainWidget, SLOT(focusPrevious()));
570

Marco Martin's avatar
Marco Martin committed
571
    d->quickWidget->installEventFilter(this);
572

Marco Martin's avatar
Marco Martin committed
573
    d->placeHolderWidget = new QQuickWidget(d->mainWidget);
574
    d->placeHolderWidget->quickWindow()->setTitle(i18n("Most Used"));
Marco Martin's avatar
Marco Martin committed
575
576
    d->placeHolderWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
    d->placeHolderWidget->engine()->rootContext()->setContextObject(new KLocalizedContext(d->placeHolderWidget));
577
578
    d->placeHolderWidget->engine()->rootContext()->setContextProperty(QStringLiteral("systemsettings"), this);
    d->placeHolderWidget->setSource(QUrl::fromLocalFile(d->package.filePath("ui", QStringLiteral("introPage.qml"))));
579
580
581
    connect(d->placeHolderWidget->rootObject(), SIGNAL(focusNextRequest()), d->mainWidget, SLOT(focusNext()));
    connect(d->placeHolderWidget->rootObject(), SIGNAL(focusPreviousRequest()), d->mainWidget, SLOT(focusPrevious()));
    d->placeHolderWidget->installEventFilter(this);
Marco Martin's avatar
Marco Martin committed
582

Marco Martin's avatar
Marco Martin committed
583
    d->mainLayout->addWidget( d->quickWidget );
Marco Martin's avatar
Marco Martin committed
584
    d->moduleView->hide();
585
    d->mainLayout->addWidget( d->moduleView );
Marco Martin's avatar
Marco Martin committed
586
    d->mainLayout->addWidget( d->placeHolderWidget );
587
    emit changeToolBarItems(BaseMode::NoItems);
588

Tien Do Nam's avatar
Tien Do Nam committed
589
590
591
592
    d->toolTipManager = new ToolTipManager(d->searchModel, d->quickWidget, ToolTipManager::ToolTipPosition::Right);
    d->subCategoryToolTipManager = new ToolTipManager(d->subCategoryModel, d->quickWidget, ToolTipManager::ToolTipPosition::Right);
    d->mostUsedToolTipManager = new ToolTipManager(d->mostUsedModel, d->placeHolderWidget, ToolTipManager::ToolTipPosition::BottomCenter);

593
    d->mostUsedModel->setResultModel(new ResultModel( AllResources | Agent(QStringLiteral("org.kde.systemsettings")) | HighScoredFirst | Limit(5), this));
594
595
}

Marco Martin's avatar
Marco Martin committed
596
597
bool SidebarMode::eventFilter(QObject* watched, QEvent* event)
{
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
    //FIXME: those are all workarounds around the QQuickWidget brokeness
    if ((watched == d->quickWidget || watched == d->placeHolderWidget)
         && event->type() == QEvent::KeyPress) {
        //allow tab navigation inside the qquickwidget
        QKeyEvent *ke = static_cast<QKeyEvent *>(event);
        QQuickWidget *qqw = static_cast<QQuickWidget *>(watched);
        if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
            QCoreApplication::sendEvent(qqw->quickWindow(), event);
            return true;
        }
    } else if ((watched == d->quickWidget || watched == d->placeHolderWidget)
                && event->type() == QEvent::FocusIn) {
        QFocusEvent *fe = static_cast<QFocusEvent *>(event);
        QQuickWidget *qqw = static_cast<QQuickWidget *>(watched);
        if (qqw && qqw->rootObject()) {
            if (fe->reason() == Qt::TabFocusReason) {
                QMetaObject::invokeMethod(qqw->rootObject(), "focusFirstChild");
            } else if (fe->reason() == Qt::BacktabFocusReason) {
                QMetaObject::invokeMethod(qqw->rootObject(), "focusLastChild");
            }
        }
    } else if (watched == d->quickWidget && event->type() == QEvent::Leave) {
Marco Martin's avatar
Marco Martin committed
620
        QCoreApplication::sendEvent(d->quickWidget->quickWindow(), event);
621
622
    } else if (watched == d->mainWidget && event->type() == QEvent::Resize) {
        emit widthChanged();
623
624
    } else if (watched == d->mainWidget && event->type() == QEvent::Show) {
        emit changeToolBarItems(BaseMode::NoItems);
Marco Martin's avatar
Marco Martin committed
625
626
627
628
    }
    return BaseMode::eventFilter(watched, event);
}

629
630
void SidebarMode::giveFocus()
{
Marco Martin's avatar
Marco Martin committed
631
    d->quickWidget->setFocus();
632
633
634
}

#include "SidebarMode.moc"