| 1 | /* |
| 2 | Copyright (C) 2009 Andres Cabrera |
| 3 | [email protected] |
| 4 | |
| 5 | This file is part of CsoundQt. |
| 6 | |
| 7 | CsoundQt is free software; you can redistribute it |
| 8 | and/or modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation; either |
| 10 | version 2.1 of the License, or (at your option) any later version. |
| 11 | |
| 12 | CsoundQt is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with Csound; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 20 | 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include "widgetpreset.h" |
| 24 | |
| 25 | WidgetPreset::WidgetPreset() |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | QString WidgetPreset::getXmlText() |
| 30 | { |
| 31 | QString out = QString(); |
| 32 | out += "<preset name=\"" + m_name + "\" number=\"" + QString::number(m_number) + "\" >\n"; |
| 33 | for (int i = 0; i < m_data.size(); i++) { |
| 34 | |
| 35 | if (m_data[i].mode & 1) { |
| 36 | out += "<value id=\"" + m_data[i].id + "\" mode=\"1\" >"; |
| 37 | out += QString::number(m_data[i].value, 'f', 8); |
| 38 | out += "</value>\n"; |
| 39 | } |
| 40 | if (m_data[i].mode & 2) { |
| 41 | out += "<value id=\"" + m_data[i].id + "\" mode=\"2\" >"; |
| 42 | out += QString::number(m_data[i].value2, 'f', 8); |
| 43 | out += "</value>\n"; |
| 44 | } |
| 45 | if (m_data[i].mode & 4) { |
| 46 | out += "<value id=\"" + m_data[i].id + "\" mode=\"4\" >"; |
| 47 | out += m_data[i].stringValue; |
| 48 | out += "</value>\n"; |
| 49 | } |
| 50 | } |
| 51 | out += "</preset>\n"; |
| 52 | // qDebug() << "WidgetPreset::getXmlText() " << out; |
| 53 | return out; |
| 54 | } |
| 55 | |
| 56 | QString WidgetPreset::getName() |
| 57 | { |
| 58 | return m_name; |
| 59 | } |
| 60 | |
| 61 | int WidgetPreset::getNumber() |
| 62 | { |
| 63 | return m_number; |
| 64 | } |
| 65 | |
| 66 | QStringList WidgetPreset::getWidgetIds() |
| 67 | { |
| 68 | QStringList list; |
| 69 | for (int i = 0; i < m_data.size(); i++) { |
| 70 | list << m_data[i].id; |
| 71 | } |
| 72 | return list; |
| 73 | } |
| 74 | |
| 75 | //int WidgetPreset::getMode(QString id) |
| 76 | //{ |
| 77 | // int index = idIndex(id); |
| 78 | // if (index >= 0 && index < m_data.size()) { |
| 79 | // return m_data[index].mode; |
| 80 | // } |
| 81 | // return -1; |
| 82 | //} |
| 83 | |
| 84 | int WidgetPreset::getMode(int index) |
| 85 | { |
| 86 | return m_data[index].mode; |
| 87 | } |
| 88 | |
| 89 | //double WidgetPreset::getValue(QString id) |
| 90 | //{ |
| 91 | // int index = idIndex(id); |
| 92 | // if (index >= 0 && index < m_data.size()) { |
| 93 | // return m_data[index].value; |
| 94 | // } |
| 95 | // qDebug() << "WidgetPreset::getValue UUid not recognized!"; |
| 96 | // return 0.0; |
| 97 | //} |
| 98 | |
| 99 | double WidgetPreset::getValue(int index) |
| 100 | { |
| 101 | return m_data[index].value; |
| 102 | } |
| 103 | |
| 104 | //double WidgetPreset::getValue2(QString id) |
| 105 | //{ |
| 106 | // int index = idIndex(id); |
| 107 | // if (index >= 0 && index < m_data.size()) { |
| 108 | // return m_data[index].value2; |
| 109 | // } |
| 110 | // qDebug() << "WidgetPreset::getValue2 UUid not recognized!"; |
| 111 | // return 0.0; |
| 112 | //} |
| 113 | |
| 114 | double WidgetPreset::getValue2(int index) |
| 115 | { |
| 116 | return m_data[index].value2; |
| 117 | } |
| 118 | |
| 119 | //QString WidgetPreset::getStringValue(QString id) |
| 120 | //{ |
| 121 | // int index = idIndex(id); |
| 122 | // if (index >= 0 && index < m_data.size()) { |
| 123 | // return m_data[index].stringValue; |
| 124 | // } |
| 125 | // qDebug() << "WidgetPreset::getStringValue UUid not recognized!"; |
| 126 | // return QString(); |
| 127 | //} |
| 128 | |
| 129 | QString WidgetPreset::getStringValue(int index) |
| 130 | { |
| 131 | return m_data[index].stringValue; |
| 132 | } |
| 133 | |
| 134 | void WidgetPreset::setName(QString name) |
| 135 | { |
| 136 | m_name = name; |
| 137 | } |
| 138 | |
| 139 | void WidgetPreset::setNumber(int number) |
| 140 | { |
| 141 | m_number = number; |
| 142 | } |
| 143 | |
| 144 | void WidgetPreset::addValue(QString id, double value) |
| 145 | { |
| 146 | int index = idIndex(id); |
| 147 | if (index < 0) { |
| 148 | PresetData p; |
| 149 | m_data.append(p); |
| 150 | index = m_data.size() - 1; |
| 151 | m_data[index].id = id; |
| 152 | m_data[index].mode = 0; |
| 153 | } |
| 154 | if (index >= m_data.size()) { |
| 155 | qDebug() << "WidgetPreset::setValue index out of range"; |
| 156 | return; |
| 157 | } |
| 158 | m_data[index].mode = m_data[index].mode | 1; |
| 159 | m_data[index].value = value; |
| 160 | // qDebug() << " WidgetPreset::addValue " << m_data[index].mode; |
| 161 | } |
| 162 | |
| 163 | void WidgetPreset::addValue2(QString id, double value) |
| 164 | { |
| 165 | int index = idIndex(id); |
| 166 | if (index < 0) { |
| 167 | PresetData p; |
| 168 | m_data.append(p); |
| 169 | index = m_data.size() - 1; |
| 170 | m_data[index].id = id; |
| 171 | m_data[index].mode = 0; |
| 172 | } |
| 173 | if (index >= m_data.size()) { |
| 174 | qDebug() << "WidgetPreset::setValue2 index out of range"; |
| 175 | return; |
| 176 | } |
| 177 | m_data[index].mode = m_data[index].mode | 2; |
| 178 | m_data[index].value2 = value; |
| 179 | } |
| 180 | |
| 181 | void WidgetPreset::addStringValue(QString id, QString value) |
| 182 | { |
| 183 | int index = idIndex(id); |
| 184 | if (index < 0) { |
| 185 | PresetData p; |
| 186 | m_data.append(p); |
| 187 | index = m_data.size() - 1; |
| 188 | m_data[index].id = id; |
| 189 | m_data[index].mode = 0; |
| 190 | } |
| 191 | if (index >= m_data.size()) { |
| 192 | qDebug() << "WidgetPreset::setStringValue index out of range"; |
| 193 | return; |
| 194 | } |
| 195 | m_data[index].mode = m_data[index].mode | 4; |
| 196 | m_data[index].stringValue = value; |
| 197 | // qDebug() << "WidgetPreset::addStringValue " << value; |
| 198 | } |
| 199 | |
| 200 | void WidgetPreset::clear() |
| 201 | { |
| 202 | m_data.clear(); |
| 203 | } |
| 204 | |
| 205 | //void WidgetPreset::purge() |
| 206 | //{ |
| 207 | // // Discard repeated entries. Is this really necessary? |
| 208 | // qDebug() << "Not implemented yet."; |
| 209 | //} |
| 210 | |
| 211 | int WidgetPreset::idIndex(QString id) |
| 212 | { |
| 213 | int index = -1; |
| 214 | for (int i = 0; i < m_data.size(); i++) { |
| 215 | // qDebug() << "WidgetPreset::idIndex " << id << "---" << m_data[i].id; |
| 216 | if (m_data[i].id == id) { |
| 217 | index = i; |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | return index; |
| 222 | } |