ProfileModel.cpp 4.5 KB
Newer Older
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
1
2
3
4
5
6
7
#include "ProfileModel.h"
#include "ProfileManager.h"
#include "Profile.h"

#include <KLocalizedString>

#include <QIcon>
8
#include <QDebug>
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
9
10
11

using namespace Konsole;

Tomaz  Canabrava's avatar
Tomaz Canabrava committed
12
13
14
15
16
17
18
ProfileModel* ProfileModel::instance()
{
    static ProfileModel self;
    return &self;
}

ProfileModel::ProfileModel()
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
19
20
21
22
23
24
25
{
    connect(ProfileManager::instance(), &ProfileManager::profileAdded,
            this, &ProfileModel::add);
    connect(ProfileManager::instance(), &ProfileManager::profileRemoved,
            this, &ProfileModel::remove);
    connect(ProfileManager::instance(), &ProfileManager::profileChanged,
            this, &ProfileModel::update);
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
26
    populate();
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
27
28
29
30
}
int ProfileModel::rowCount(const QModelIndex &unused) const
{
    Q_UNUSED(unused);
31
32

    // All profiles  plus the default profile that's always at index 0 on
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
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
    return m_profiles.count();
}

int ProfileModel::columnCount(const QModelIndex& unused) const
{
    Q_UNUSED(unused);
    return COLUMNS;
}

QVariant ProfileModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Vertical) {
        return {};
    }
    if (role != Qt::DisplayRole) {
        return {};
    }

    switch(section) {
        case NAME: return i18nc("@title:column Profile name", "Name");
        case SHORTCUT: return i18nc("@title:column Profile keyboard shortcut", "Shortcut");
    }
    return {};
}

QVariant ProfileModel::data(const QModelIndex& idx, int role) const
{
    if (!idx.isValid()) {
        return {};
    }

64
65
66
    if (idx.row() > m_profiles.count()) {
        return {};
    }
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
67

68
    auto profile = m_profiles.at(idx.row());
69

Tomaz  Canabrava's avatar
Tomaz Canabrava committed
70
71
72
    switch (idx.column()) {
    case NAME: {
        switch (role) {
73
        case Qt::DisplayRole: {
74
75
76
            return QStringLiteral("%1%2").arg(
                profile->name(),
                ProfileManager::instance()->defaultProfile() == profile ? i18n("(Default)") : QString());
77
        }
78
        case Qt::DecorationRole: return QIcon::fromTheme(profile->icon());
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
79
        case Qt::FontRole: {
80
                if (ProfileManager::instance()->defaultProfile() == profile ) {
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
81
82
83
84
85
86
87
88
89
                    QFont font;
                    font.setItalic(true);
                    return font;
                }
            }
        }
    }
    break;
    case SHORTCUT: {
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
90
        auto shortcut = ProfileManager::instance()->shortcut(profile);
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
91
        switch (role) {
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
92
93
            case Qt::DisplayRole: return shortcut;
            case Qt::EditRole: return shortcut;
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
94
            case Qt::ToolTipRole: return i18nc("@info:tooltip", "Double click to change shortcut");
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
        }
        break;
    }
    case PROFILE: {
        switch(role) {
            case ProfilePtrRole: return QVariant::fromValue(profile);
        }
        break;
    }
    }

    return {};
}

Qt::ItemFlags ProfileModel::flags(const QModelIndex& idx) const
{
    auto currentFlags = QAbstractTableModel::flags(idx);

    switch(idx.column()) {
        case NAME: return currentFlags & (~Qt::ItemIsEditable);
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
115
        case SHORTCUT: return currentFlags | Qt::ItemIsEditable;
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
116
117
118
119
120
121
122
123
124
125
126
        default: return currentFlags;
    }
    return currentFlags;
}

bool ProfileModel::setData(const QModelIndex &idx, const QVariant &value, int role)
{
    if (!idx.isValid()) {
        return false;
    }

Tomaz  Canabrava's avatar
Tomaz Canabrava committed
127
    if (idx.column() != SHORTCUT) {
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
128
129
130
        return false;
    }

Tomaz  Canabrava's avatar
Tomaz Canabrava committed
131
    if (role != Qt::EditRole && role != Qt::DisplayRole) {
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
132
133
134
135
        return false;
    }

    auto profile = m_profiles.at(idx.row());
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
136
137
    if (idx.column() == SHORTCUT) {
        auto sequence = QKeySequence::fromString(value.toString());
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
138
139
        ProfileManager::instance()->setShortcut(profile, sequence);
        emit dataChanged(idx, idx, {Qt::DisplayRole});
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
140
        return true;
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
141
142
    }

Tomaz  Canabrava's avatar
Tomaz Canabrava committed
143
    return false;
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
144
145
146
147
148
149
150
151
152
153
}

void ProfileModel::populate()
{
    beginResetModel();
    m_profiles = ProfileManager::instance()->allProfiles();
    ProfileManager::instance()->sortProfiles(m_profiles);
    endResetModel();
}

154
void ProfileModel::add(QExplicitlySharedDataPointer<Profile> profile)
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
155
156
157
158
159
160
{
    // The model is too small for this to matter.
    Q_UNUSED(profile);
    populate();
}

161
void ProfileModel::remove(QExplicitlySharedDataPointer<Profile> profile)
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
162
163
164
165
166
167
{
    // The model is too small for this to matter.
    Q_UNUSED(profile);
    populate();
}

168
169
170
171
172
173
void ProfileModel::setDefault(QExplicitlySharedDataPointer<Profile> profile)
{
    emit dataChanged(index(0, 0), index(0, COLUMNS-1), {Qt::DisplayRole});
}

void ProfileModel::update(QExplicitlySharedDataPointer<Profile> profile)
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
174
175
176
177
{
    int row = m_profiles.indexOf(profile);
    dataChanged(index(row, 0), index(row, COLUMNS-1));
}