~ubuntu-branches/debian/sid/chessx/sid

« back to all changes in this revision

Viewing changes to src/gui/engineoptionmodel.cpp

  • Committer: Package Import Robot
  • Author(s): Niklas Fiekas
  • Date: 2013-10-31 17:02:37 UTC
  • Revision ID: package-import@ubuntu.com-20131031170237-wghf5j9jlv28gmls
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
*   Copyright (C) 2012 by Jens Nissen jens-chessx@gmx.net                   *
 
3
****************************************************************************/
 
4
 
 
5
#include "engineoptionmodel.h"
 
6
#include "engineoptiondata.h"
 
7
 
 
8
EngineOptionModel::EngineOptionModel(QObject *parent) :
 
9
    QStandardItemModel(parent)
 
10
{
 
11
    m_columnNames << tr("Name") << tr("Default") << tr("Min") << tr("Max") << tr("Value");
 
12
}
 
13
 
 
14
QModelIndex EngineOptionModel::index(int row, int column, const QModelIndex &parent) const
 
15
{
 
16
    if(parent.isValid())
 
17
    {
 
18
        return QModelIndex();
 
19
    }
 
20
    return createIndex(row, column, (void*) 0);
 
21
}
 
22
 
 
23
QModelIndex EngineOptionModel::parent(const QModelIndex &) const
 
24
{
 
25
    return QModelIndex();
 
26
}
 
27
 
 
28
int EngineOptionModel::rowCount(const QModelIndex &parent) const
 
29
{
 
30
    if(parent.isValid())
 
31
    {
 
32
        return 0;
 
33
    }
 
34
    return m_pOptionDataList ? m_pOptionDataList->size() : 0;
 
35
}
 
36
 
 
37
int EngineOptionModel::columnCount(const QModelIndex &) const
 
38
{
 
39
    return m_columnNames.count();
 
40
}
 
41
 
 
42
bool EngineOptionModel::hasChildren(const QModelIndex &parent) const
 
43
{
 
44
    return !parent.isValid();
 
45
}
 
46
 
 
47
QVariant EngineOptionModel::data(const QModelIndex &index, int role) const
 
48
{
 
49
    if(!m_pOptionDataList || !m_pValueMap)
 
50
    {
 
51
        return QVariant();
 
52
    }
 
53
 
 
54
    if(index.isValid())
 
55
    {
 
56
        const EngineOptionData* pOptionData = &m_pOptionDataList->at(index.row());
 
57
        Q_ASSERT(pOptionData);
 
58
        if((role == Qt::DisplayRole) || (role == Qt::EditRole))
 
59
        {
 
60
            switch(index.column())
 
61
            {
 
62
            case 0:
 
63
                return pOptionData->m_name;
 
64
            case 1:
 
65
                return pOptionData->m_defVal;
 
66
            case 2:
 
67
                return pOptionData->m_minVal;
 
68
            case 3:
 
69
                return pOptionData->m_maxVal;
 
70
            case 4:
 
71
                switch(pOptionData->m_type)
 
72
                {
 
73
                case OPT_TYPE_BUTTON:
 
74
                    if(m_pValueMap->contains(pOptionData->m_name))
 
75
                    {
 
76
                        return (*m_pValueMap)[pOptionData->m_name].toBool();
 
77
                    }
 
78
                    return false;
 
79
                    break;
 
80
                case OPT_TYPE_CHECK:
 
81
                    if(m_pValueMap->contains(pOptionData->m_name))
 
82
                    {
 
83
                        return (*m_pValueMap)[pOptionData->m_name].toBool();
 
84
                    }
 
85
                    return QVariant(pOptionData->m_defVal).toBool();
 
86
                    break;
 
87
                case OPT_TYPE_SPIN:
 
88
                    if(m_pValueMap->contains(pOptionData->m_name))
 
89
                    {
 
90
                        return (*m_pValueMap)[pOptionData->m_name].toInt();
 
91
                    }
 
92
                    return pOptionData->m_defVal.toInt();
 
93
                    break;
 
94
                case OPT_TYPE_STRING:
 
95
                case OPT_TYPE_COMBO:
 
96
                    if(m_pValueMap->contains(pOptionData->m_name))
 
97
                    {
 
98
                        return (*m_pValueMap)[pOptionData->m_name];
 
99
                    }
 
100
                    return pOptionData->m_defVal;
 
101
                    break;
 
102
                }
 
103
                break;
 
104
            }
 
105
        }
 
106
        else if(role == Qt::BackgroundRole)
 
107
        {
 
108
            if(index.column() != 4)
 
109
            {
 
110
                return QColor(Qt::lightGray);
 
111
            }
 
112
            else
 
113
            {
 
114
                return QColor(Qt::white);
 
115
            }
 
116
        }
 
117
        else if(role == Qt::ForegroundRole)
 
118
        {
 
119
            if(index.column() == 4)
 
120
            {
 
121
                if((!m_pValueMap->contains(pOptionData->m_name)
 
122
                        || (*m_pValueMap)[pOptionData->m_name] == pOptionData->m_defVal))
 
123
                {
 
124
                    return QColor(Qt::darkGray);
 
125
                }
 
126
                else
 
127
                {
 
128
                    return QColor(Qt::black);
 
129
                }
 
130
            }
 
131
        }
 
132
    }
 
133
 
 
134
    return QVariant();
 
135
}
 
136
 
 
137
bool EngineOptionModel::setData(const QModelIndex & index, const QVariant & value, int role)
 
138
{
 
139
    if(m_pOptionDataList && m_pValueMap && (role == Qt::EditRole) && index.isValid() && (index.column() == 4))
 
140
    {
 
141
        const EngineOptionData* pOptionData = &m_pOptionDataList->at(index.row());
 
142
        (*m_pValueMap)[pOptionData->m_name] = value;
 
143
        return true;
 
144
    }
 
145
    return false;
 
146
}
 
147
 
 
148
QVariant EngineOptionModel::headerData(int section, Qt::Orientation orientation, int role) const
 
149
{
 
150
    if(role != Qt::DisplayRole)
 
151
    {
 
152
        return QVariant();
 
153
    }
 
154
 
 
155
    if(orientation == Qt::Horizontal)
 
156
    {
 
157
        return QString("%1").arg(m_columnNames.at(section));
 
158
    }
 
159
    else
 
160
    {
 
161
        return QString("%1").arg(section);
 
162
    }
 
163
}
 
164
 
 
165
void EngineOptionModel::resetModel()
 
166
{
 
167
    beginResetModel();
 
168
    endResetModel();
 
169
}
 
170
 
 
171
QStringList EngineOptionModel::getSelections(const QModelIndex& index)
 
172
{
 
173
    QStringList list;
 
174
    if(index.isValid())
 
175
    {
 
176
        const EngineOptionData* pOptionData = &m_pOptionDataList->at(index.row());
 
177
        return pOptionData->m_varVals;
 
178
    }
 
179
    return list;
 
180
}