summaryrefslogtreecommitdiff
path: root/src/plugins/texteditor/ifallbackpreferences.cpp
blob: d583d2d169fdc6b6fef407fc658a7ce4088719c5 (plain)
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
#include "ifallbackpreferences.h"

#include <utils/settingsutils.h>

#include <QtCore/QSettings>
#include <QtCore/QStringList>

using namespace TextEditor;

namespace TextEditor {
namespace Internal {

class IFallbackPreferencesPrivate
{
public:
    IFallbackPreferencesPrivate()
        : m_currentFallback(0)
    {}

    QList<IFallbackPreferences *> m_fallbacks;
    QMap<QString, IFallbackPreferences *> m_idToFallback;
    IFallbackPreferences *m_currentFallback;
    QString m_id;
    QString m_displayName;
};

}
}

IFallbackPreferences::IFallbackPreferences(
    const QList<IFallbackPreferences *> &fallbacks,
    QObject *parent) :
    QObject(parent),
    d(new Internal::IFallbackPreferencesPrivate)
{
    d->m_fallbacks = fallbacks;
    for (int i = 0; i < fallbacks.count(); i++) {
        IFallbackPreferences *fallback = fallbacks.at(i);
        d->m_idToFallback.insert(fallback->id(), fallback);
    }
}

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

QString IFallbackPreferences::id() const
{
    return d->m_id;
}

void IFallbackPreferences::setId(const QString &name)
{
    d->m_id = name;
}

QString IFallbackPreferences::displayName() const
{
    return d->m_displayName;
}

void IFallbackPreferences::setDisplayName(const QString &name)
{
    d->m_displayName = name;
}

QVariant IFallbackPreferences::currentValue() const
{
    return currentPreferences()->value();
}

IFallbackPreferences *IFallbackPreferences::currentPreferences() const
{
    IFallbackPreferences *prefs = (IFallbackPreferences *)this;
    while (prefs->currentFallback())
        prefs = prefs->currentFallback();
    return prefs;
}

QList<IFallbackPreferences *> IFallbackPreferences::fallbacks() const
{
    return d->m_fallbacks;
}

IFallbackPreferences *IFallbackPreferences::currentFallback() const
{
    return d->m_currentFallback;
}

void IFallbackPreferences::setCurrentFallback(IFallbackPreferences *fallback)
{
    if (fallback && !d->m_fallbacks.contains(fallback)) {
        // warning
        return;
    }
    if (d->m_currentFallback == fallback)
        return; // nothing changes

    if (d->m_currentFallback) {
        disconnect(d->m_currentFallback, SIGNAL(currentValueChanged(QVariant)),
                   this, SIGNAL(currentValueChanged(QVariant)));
    }
    d->m_currentFallback = fallback;
    if (d->m_currentFallback) {
        connect(d->m_currentFallback, SIGNAL(currentValueChanged(QVariant)),
                this, SIGNAL(currentValueChanged(QVariant)));
    }
    emit currentFallbackChanged(d->m_currentFallback);
    emit currentValueChanged(currentValue());
}

QString IFallbackPreferences::currentFallbackId() const
{
    if (currentFallback())
        return currentFallback()->id();
    return id(); // or 0?
}

void IFallbackPreferences::setCurrentFallback(const QString &id)
{
    setCurrentFallback(d->m_idToFallback.value(id));
}

void IFallbackPreferences::toSettings(const QString &category, QSettings *s) const
{
    Utils::toSettings(settingsSuffix(), category, s, this);
}

void IFallbackPreferences::fromSettings(const QString &category, const QSettings *s)
{
    Utils::fromSettings(settingsSuffix(), category, s, this);
}