summaryrefslogtreecommitdiffstats
path: root/library/optionsitem.cpp
blob: 91d117d0a802cb557119720db2549113c6aab92e (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "optionsitem.h"

#include <QtCore/QString>
#include <QtGui/QAbstractButton>
#include <QtGui/QBoxLayout>
#include <QtGui/QFormLayout>
#include <QtGui/QLabel>

OptionsItem::OptionsItem(const QString &name, QWidget *inputWidget, bool fullWidth, QObject *parent)
    : QObject(parent)
    , mLabel(0)
    , mInputWidget(inputWidget)
    , mAdvancedPage(0)
    , mHidden(false)
    , mAdvanced(false)
{
    if (fullWidth)
        mType = FullWidthType;
    else {
        mType = DefaultType;

        mInputLayout = new QHBoxLayout();
        mInputLayout->addStretch();
        mInputLayout->addWidget(mInputWidget);
    }
    if (name != 0 && !name.isEmpty())
        mLabel = new QLabel(name);
}

OptionsItem::~OptionsItem()
{}

bool OptionsItem::addTag(const QString &tag)
{
    if (!mTags.contains(tag.toLower())) {
        mTags.append(tag.toLower());
        return true;
    }
    return false;
}

QLabel *OptionsItem::label() const
{
    return mLabel;
}

QWidget *OptionsItem::inputWidget() const
{
    return mInputWidget;
}

QHBoxLayout *OptionsItem::inputLayout() const
{
    return mInputLayout;
}

bool OptionsItem::setAdvancedPage(OptionsItem *advancedPage)
{
    QAbstractButton *inputButton = qobject_cast<QAbstractButton *>(mInputWidget);
    if (!inputButton)
        return false;
    mAdvancedPage = advancedPage;
    mType = AdvancedType;
    mAdvancedPage->setTags(mTags);
    mAdvancedPage->setVisible(false, true);
    disconnect(inputButton, SIGNAL(clicked()), this, SLOT(buttonClicked()));
    connect(inputButton, SIGNAL(clicked()), SLOT(buttonClicked()));
    return true;
}

OptionsItem *OptionsItem::advancedPage() const
{
    return mAdvancedPage;
}

void OptionsItem::setVisible(bool visible, bool explicitly)
{
    if (mLabel)
        mLabel->setVisible(visible);
    mInputWidget->setVisible(visible);
    if (!visible && explicitly)
        mHidden = true;
    else
        mHidden = false;
}

bool OptionsItem::addTags(const QStringList &tags)
{
    bool ret = true;
    foreach(const QString &tag, tags)
        if (!addTag(tag))
            ret = false;
    return ret;
}

bool OptionsItem::removeTag(const QString &tag)
{
    return (mTags.removeAll(tag.toLower()) > 0);
}

bool OptionsItem::removeTags(const QStringList &tags)
{
    bool ret = true;
    foreach (const QString &tag, tags)
        if (!removeTag(tag))
            ret = false;
    return ret;
}

bool OptionsItem::setTags(const QStringList &tags)
{
    mTags.clear();
    return addTags(tags);
}   

bool OptionsItem::matchTag(const QString &tag) const
{
    if (mLabel && mLabel->text().toLower().contains(tag.toLower()))
        return true;
    foreach (const QString &tagInList, mTags)
        if (tagInList.contains(tag.toLower()))
            return true;
    return false;
}

OptionsItem::OptionsType OptionsItem::type() const
{
    return mType;
}

bool OptionsItem::isExplicitlyHidden() const
{
    return mHidden;
}

void OptionsItem::setAdvanced(bool advanced)
{
    mAdvanced = advanced;
}

bool OptionsItem::isAdvanced() const
{
    return mAdvanced;
}

void OptionsItem::buttonClicked()
{
    emit toggleAdvanced(this);
}