| 1 | /* |
| 2 | Copyright (C) 2010 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 "qutesheet.h" |
| 24 | |
| 25 | QuteSheet::QuteSheet(QObject *parent, EventSheet *sheet) : |
| 26 | QObject(parent) |
| 27 | { |
| 28 | setSheetPointer(sheet); |
| 29 | } |
| 30 | |
| 31 | void QuteSheet::setSheetPointer(EventSheet *sheet) |
| 32 | { |
| 33 | m_sheet = sheet; |
| 34 | } |
| 35 | |
| 36 | QList<QList <QVariant > > QuteSheet::data() |
| 37 | { |
| 38 | return m_sheet->getData(); |
| 39 | } |
| 40 | |
| 41 | bool QuteSheet::testConsistency() |
| 42 | { |
| 43 | // if (total_rows != data.size()) { |
| 44 | // raise InconsistentData(d.num_rows, "Inconsistent number of rows.") |
| 45 | // } |
| 46 | // if ( total_cols != len(transpose(data))) { |
| 47 | // raise InconsistentData(d.num_cols, "Inconsistent number of columns.") |
| 48 | // } |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | void QuteSheet::setSheetCellValue(int row, int column, QVariant data) |
| 53 | { |
| 54 | if (m_sheet != 0) { |
| 55 | m_sheet->setCell(row,column, data); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | QList<QVariant> QuteSheet::getValuesByRow(QList<QList <QVariant > > data) |
| 60 | { |
| 61 | QList<QVariant> cells; |
| 62 | if (m_sheet != 0) { |
| 63 | foreach (QList<QVariant> row, data) { |
| 64 | foreach (QVariant element, row) { |
| 65 | cells << element; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | return cells; |
| 70 | } |
| 71 | |
| 72 | bool QuteSheet::setRows(QList<QList <QVariant > > new_data, |
| 73 | int new_startRow, int new_startCol, |
| 74 | int new_numRows, int new_numCols) |
| 75 | { |
| 76 | (void) new_data; |
| 77 | //! Set the data for the text output organized by row |
| 78 | if (new_startRow == -1) { |
| 79 | new_startRow = startRow; |
| 80 | } |
| 81 | if (new_startCol == -1) { |
| 82 | new_startCol = startCol; |
| 83 | } |
| 84 | if (new_numRows == -1) { |
| 85 | new_numRows = numRows; |
| 86 | } |
| 87 | if (new_numCols == -1) { |
| 88 | new_numCols = numCols; |
| 89 | } |
| 90 | // out_text = '__@ ' + str(new_startRow) + ' ' + str(new_startCol) |
| 91 | // out_text += ' ' + str(new_numRows) + ' ' + str(new_numCols) + '\n' |
| 92 | // out_text += self._rowsToText(new_data) |
| 93 | // self._write_out_file(out_text) |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | bool QuteSheet::setValues(QList <QVariant > new_data, |
| 99 | int new_startRow, int new_startCol, |
| 100 | int new_numRows, int new_numCols) |
| 101 | { |
| 102 | (void) new_data; |
| 103 | //! """Set the data for the text output as a list of individual cells organized by rows""" |
| 104 | if (new_startRow == -1) { |
| 105 | new_startRow = startRow; |
| 106 | } |
| 107 | if (new_startCol == -1) { |
| 108 | new_startCol = startCol; |
| 109 | } |
| 110 | if (new_numRows == -1) { |
| 111 | new_numRows = numRows; |
| 112 | } |
| 113 | if (new_numCols == -1) { |
| 114 | new_numCols = numCols; |
| 115 | } |
| 116 | // out_data = [] |
| 117 | // for i in range(new_numRows): |
| 118 | // out_data.append([]) |
| 119 | // for j in range(new_numCols): |
| 120 | // if len(new_data) > 0: |
| 121 | // out_data[i].append(new_data.pop(0)) |
| 122 | // set_rows(out_data, new_startRow, new_startCol, new_numRows, new_numCols) |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | bool QuteSheet::setText(QString new_data, |
| 127 | int new_startRow, int new_startCol, |
| 128 | int new_numRows, int new_numCols) |
| 129 | { |
| 130 | (void) new_data; |
| 131 | //! """Set data from a text string, each line is interpreted as a row and each cell must be separated by a space.""" |
| 132 | if (new_startRow == -1) { |
| 133 | new_startRow = startRow; |
| 134 | } |
| 135 | if (new_startCol == -1) { |
| 136 | new_startCol = startCol; |
| 137 | } |
| 138 | if (new_numRows == -1) { |
| 139 | new_numRows = numRows; |
| 140 | } |
| 141 | if (new_numCols == -1) { |
| 142 | new_numCols = numCols; |
| 143 | } |
| 144 | // out_text = '__@ ' + str(new_startRow) + ' ' + str(new_startCol) |
| 145 | // out_text += ' ' + str(new_numRows) + str(new_numCols) + '\n' |
| 146 | // out_text += text |
| 147 | // _write_out_file(out_text) |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | QList<QList <QVariant> > QuteSheet::sort(QList<QList <QVariant> > vectors, int p_field) |
| 152 | { |
| 153 | (void) p_field; |
| 154 | (void) vectors; |
| 155 | //! Sort vectors by elements in a certain index (third (p2) by default) |
| 156 | // qDebug() << "QuteSheet::sort not implemented yet."; |
| 157 | QList<QList <QVariant> > new_data; |
| 158 | // for r in rows: |
| 159 | // if len(r) < p_field + 1: |
| 160 | // new_data.append(r) |
| 161 | // else: |
| 162 | // count = 0 |
| 163 | // while count < len(new_data): |
| 164 | // if type(r[p_field]) != str and r[p_field] <= new_data[count][p_field]: |
| 165 | // break |
| 166 | // count += 1 |
| 167 | // new_data.insert(count, r) |
| 168 | return new_data; |
| 169 | } |
| 170 | |
| 171 | QList<QList <QVariant > > QuteSheet::transpose(QList<QList <QVariant > > mtx) |
| 172 | { |
| 173 | QList<QList<QVariant> > newMtx; |
| 174 | // TODO make this more efficient |
| 175 | if (mtx.isEmpty()) { |
| 176 | return newMtx; |
| 177 | } |
| 178 | for (int j = 0; j < mtx[0].size(); j++) { |
| 179 | newMtx << QList<QVariant>(); |
| 180 | } |
| 181 | for (int i = 0; i < mtx.size(); i++) { |
| 182 | for (int j = 0; j < mtx[i].size(); j++) { |
| 183 | newMtx[j] << mtx[i][j]; |
| 184 | } |
| 185 | } |
| 186 | return newMtx; |
| 187 | } |
| 188 | |
| 189 | QList<QList <QVariant > > QuteSheet::someCols(QList<QList <QVariant > > data, |
| 190 | int row, |
| 191 | int num_rows, |
| 192 | int col, |
| 193 | int num_cols) |
| 194 | { |
| 195 | (void) data; |
| 196 | (void) num_rows; |
| 197 | (void) row; |
| 198 | (void) num_rows; |
| 199 | (void) col; |
| 200 | (void) num_cols; |
| 201 | QList<QList <QVariant > >rows; |
| 202 | // for r in range(row, row + num_rows): |
| 203 | // new_row = [] |
| 204 | // for c in range(col, col + num_cols): |
| 205 | // new_row.append(data[r][c]) |
| 206 | // rows.append(new_row) |
| 207 | return rows; |
| 208 | } |
| 209 | |
| 210 | QList<QList <QVariant > > QuteSheet::allCols(QList<QList <QVariant > > data, |
| 211 | int row, |
| 212 | int num_rows) |
| 213 | { |
| 214 | (void) data; |
| 215 | (void) row; |
| 216 | (void) num_rows; |
| 217 | QList<QList <QVariant > > rows; |
| 218 | // for r in range(row, row + num_rows): |
| 219 | // rows.append(data[r]) |
| 220 | return rows; |
| 221 | } |
| 222 | |
| 223 | QString QuteSheet::rowsToText(QList<QList <QVariant > > data) |
| 224 | { |
| 225 | QString out_text = ""; |
| 226 | foreach (QList<QVariant> row, data) { |
| 227 | foreach (QVariant cell, row) { |
| 228 | out_text += cell.toString() + " "; |
| 229 | } |
| 230 | out_text.chop(1); |
| 231 | out_text += "\n"; |
| 232 | } |
| 233 | return out_text; |
| 234 | } |