File: centralwidget.cpp

package info (click to toggle)
ktorrent 25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,104 kB
  • sloc: cpp: 40,482; xml: 1,163; python: 182; sh: 10; makefile: 5
file content (112 lines) | stat: -rw-r--r-- 3,023 bytes parent folder | download
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
/*
    SPDX-FileCopyrightText: 2009 Joris Guisson <joris.guisson@gmail.com>
    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include <KConfigGroup>
#include <QAction>
#include <QIcon>

#include "centralwidget.h"
#include <interfaces/activity.h>

namespace kt
{
CentralWidget::CentralWidget(QWidget *parent)
    : QStackedWidget(parent)
{
    activity_switching_group = new QActionGroup(this);
    connect(activity_switching_group, &QActionGroup::triggered, this, &CentralWidget::switchActivity);
}

CentralWidget::~CentralWidget()
{
}

void CentralWidget::loadState(KSharedConfigPtr cfg)
{
    KConfigGroup g = cfg->group(QStringLiteral("MainWindow"));
    int idx = g.readEntry("current_activity", 0);
    Activity *act = (Activity *)widget(idx);
    if (act)
        setCurrentActivity(act);

    const QList<QAction *> actions = activity_switching_group->actions();
    for (QAction *a : actions) {
        if (a->data().value<QObject *>() == act || act == nullptr) {
            a->setChecked(true);
            break;
        }
    }

    for (QAction *a : actions) {
        a->setPriority((QAction::Priority)g.readEntry(QLatin1String("Priority_") + a->objectName(), (int)QAction::NormalPriority));
    }
}

void CentralWidget::saveState(KSharedConfigPtr cfg)
{
    KConfigGroup g = cfg->group(QStringLiteral("MainWindow"));
    g.writeEntry("current_activity", currentIndex());

    const QList<QAction *> actions = activity_switching_group->actions();
    for (QAction *a : actions) {
        g.writeEntry(QLatin1String("Priority_") + a->objectName(), (int)a->priority());
    }
}

QAction *CentralWidget::addActivity(Activity *act)
{
    QAction *a = new QAction(QIcon::fromTheme(act->icon()), act->name(), this);
    // act->name() is i18n'ed, use <icon name, weight> as uniq id
    a->setObjectName(act->icon() + QLatin1String("_wght_") + QString::number(act->weight()));
    activity_switching_group->addAction(a);
    a->setCheckable(true);
    a->setToolTip(act->toolTip());
    a->setData(QVariant::fromValue(act));
    addWidget(act);
    return a;
}

void CentralWidget::removeActivity(Activity *act)
{
    const QList<QAction *> actions = activity_switching_group->actions();
    for (QAction *a : actions) {
        if (a->data().value<QObject *>() == act) {
            activity_switching_group->removeAction(a);
            a->deleteLater();
            break;
        }
    }
    removeWidget(act);
}

void CentralWidget::setCurrentActivity(Activity *act)
{
    setCurrentWidget(act);
}

Activity *CentralWidget::currentActivity()
{
    return (Activity *)currentWidget();
}

QList<QAction *> CentralWidget::activitySwitchingActions()
{
    return activity_switching_group->actions();
}

void CentralWidget::switchActivity(QAction *action)
{
    for (int i = 0; i < count(); i++) {
        Activity *act = (Activity *)widget(i);
        if (action->data().value<QObject *>() == act) {
            Q_EMIT changeActivity(act);
            break;
        }
    }
}

}

#include "moc_centralwidget.cpp"