void Matrix::clearSelection() { bool colSelection = false; for (int i=0; i<d_table->numCols(); i++) { if(d_table->isColumnSelected (i,TRUE)) { colSelection = true; for (int j=0; j<d_table->numRows(); j++) d_table->setText(j, i, ""); } } if (colSelection) { emit modifiedWindow(this); return; } else { QTableSelection sel=d_table->selection(d_table->currentSelection()); for (int i= sel.topRow();i<=sel.bottomRow();i++) { for (int j= sel.leftCol();j<= sel.rightCol();j++) d_table->setText(i, j, ""); } emit modifiedWindow(this); } }
void Matrix::clearSelection() { if (d_view_type == ImageView) return; QItemSelectionModel *selModel = d_table_view->selectionModel(); if (!selModel || !selModel->hasSelection()) return; const QItemSelectionRange sel = selModel->selection()[0]; int startRow = sel.top(); int endRow = sel.bottom(); int startCol = sel.left(); int endCol = sel.right(); double *buffer = d_matrix_model->dataCopy(startRow, endRow, startCol, endCol); if (buffer){ d_undo_stack->push(new MatrixUndoCommand(d_matrix_model, Clear, startRow, endRow, startCol, endCol, buffer, tr("Clear Selection"))); emit modifiedWindow(this); modifiedData(this); } else if (ignoreUndo()){ d_matrix_model->clear(startRow, endRow, startCol, endCol); emit modifiedWindow(this); modifiedData(this); } }
void Matrix::deleteSelectedColumns() { QItemSelectionModel *selModel = d_table_view->selectionModel(); if (!selModel || !selModel->hasSelection()) return; int startCol = -1; int count = 0; int cols = numCols(); for (int i=0; i<cols; i++){ if (selModel->isColumnSelected(i, QModelIndex())){ if (startCol < 0) startCol = i; ++count; } } if (startCol < 0 || !count) return; double *buffer = d_matrix_model->dataCopy(0, numRows() - 1, startCol, startCol + count - 1); if (buffer){ d_undo_stack->push(new MatrixDeleteColsCommand(d_matrix_model, startCol, count, buffer, tr("Delete Columns") + " " + QString::number(startCol + 1) + " - " + QString::number(startCol + count))); emit modifiedWindow(this); } else if (ignoreUndo()){ d_matrix_model->removeColumns(startCol, count); d_table_view->reset(); emit modifiedWindow(this); } }
void Matrix::importImage(const QString& fn) { QImage image(fn); if (image.isNull()) return; x_start = 0.0; x_end = image.width() - 1.0; y_start = 0.0; y_end = image.height() - 1.0; double *buffer = d_matrix_model->dataCopy(); if (buffer){ d_undo_stack->push(new MatrixSetImageCommand(d_matrix_model, image, d_view_type, 0, numRows() - 1, 0, numCols() - 1, buffer, tr("Import Image") + " \"" + fn + "\"")); emit modifiedWindow(this); modifiedData(this); } else if (ignoreUndo()){ d_matrix_model->setImage(image); setViewType(ImageView, false); displayImage(image); emit modifiedWindow(this); modifiedData(this); } setWindowLabel(fn); }
void Matrix::fft(bool inverse) { double *buffer = d_matrix_model->dataCopy(); if (buffer){ QString commandText = inverse ? tr("Inverse FFT") : tr("Forward FFT"); d_undo_stack->push(new MatrixFftCommand(inverse, d_matrix_model, 0, numRows() - 1, 0, numCols() - 1, buffer, commandText)); emit modifiedWindow(this); } else if (ignoreUndo()){ d_matrix_model->fft(inverse); emit modifiedWindow(this); } }
bool Matrix::muParserCalculate(int startRow, int endRow, int startCol, int endCol) { double *buffer = d_matrix_model->dataCopy(startRow, endRow, startCol, endCol); if (buffer){ d_undo_stack->push(new MatrixUndoCommand(d_matrix_model, MuParserCalculate, startRow, endRow, startCol, endCol, buffer, tr("Calculate Values"))); emit modifiedWindow(this); return true; } else if(ignoreUndo()){ d_matrix_model->muParserCalculate(startRow, endRow, startCol, endCol); emit modifiedWindow(this); return true; } return false; }
void Matrix::setNumericFormat(const QChar& f, int prec) { if (txt_format == f && num_precision == prec) return; QApplication::setOverrideCursor(waitCursor); txt_format = f; num_precision = prec; int rows=d_table->numRows(); int cols=d_table->numCols(); for (int i=0; i<rows; i++) { for (int j=0; j<cols; j++) { QString t = d_table->text(i, j); if (!t.isEmpty()) { double val = dMatrix[i][j]; t = t.setNum(val, txt_format, num_precision); d_table->setText(i, j, t); } } } emit modifiedWindow(this); QApplication::restoreOverrideCursor(); }
// TODO: Mirror matrix horizontally/vertically would also be nice void Matrix::transpose() { allow_modification_signals = false; int i, j; int rows = numRows(); int cols = numCols(); int temp_size = qMax(rows, cols); QString temp; // blow up matrix to a square one d_table->setColumnCount(temp_size); d_table->setRowCount(temp_size); for(i = 0; i<temp_size; i++) for(j = 0; j<=i; j++) { temp = text(i,j); setText(i, j, text(j,i)); setText(j, i, temp); } // shrink matrix to desired size d_table->setColumnCount(rows); d_table->setRowCount(cols); allow_modification_signals = true; emit modifiedWindow(this); }
// TODO: Port this class to the model/view framework and make // sure the diplayed precision and the saved precision // are independent void Matrix::setNumericFormat(const QChar& f, int prec) { if (txt_format == f && num_precision == prec) return; allow_modification_signals = false; QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); txt_format = f; num_precision = prec; int rows = numRows(); int cols = numCols(); for(int i=0; i<rows; i++) { for(int j=0; j<cols; j++) { QString t = text(i, j); if (!t.isEmpty()) setCell(i, j, dMatrix[i][j]); } } allow_modification_signals = true; emit modifiedWindow(this); QApplication::restoreOverrideCursor(); }
void Matrix::setDefaultColorMap() { d_color_map_type = Default; d_color_map = applicationWindow()->d_3D_color_map; if (d_view_type == ImageView) displayImage(d_matrix_model->renderImage()); emit modifiedWindow(this); }
void Matrix::importImage(const QImage& image) { if (image.isNull()) return; double *buffer = d_matrix_model->dataCopy(); if (buffer){ d_undo_stack->push(new MatrixSetImageCommand(d_matrix_model, image, d_view_type, 0, numRows() - 1, 0, numCols() - 1, buffer, tr("Import Image"))); emit modifiedWindow(this); } else if (ignoreUndo()){ d_matrix_model->setImage(image); setViewType(ImageView, false); displayImage(image); emit modifiedWindow(this); } }
void Matrix::setGrayScale() { d_color_map_type = GrayScale; d_color_map = QwtLinearColorMap(Qt::black, Qt::white); if (d_view_type == ImageView) displayImage(d_matrix_model->renderImage()); emit modifiedWindow(this); }
void Matrix::insertColumn() { int cr = d_table->currentColumn(); if (d_table->isColumnSelected (cr, true)) { d_table->insertColumns(cr,1); emit modifiedWindow(this); } }
void Matrix::insertRow() { int cr = d_table->currentRow(); if (d_table->isRowSelected (cr, true)) { d_table->insertRows(cr,1); emit modifiedWindow(this); } }
bool Matrix::calculate(int startRow, int endRow, int startCol, int endCol, bool forceMuParser) { if (QString(scriptEnv->name()) == "muParser" || forceMuParser) return muParserCalculate(startRow, endRow, startCol, endCol); double *buffer = d_matrix_model->dataCopy(startRow, endRow, startCol, endCol); if (buffer){ d_undo_stack->push(new MatrixUndoCommand(d_matrix_model, Calculate, startRow, endRow, startCol, endCol, buffer, tr("Calculate Values"))); emit modifiedWindow(this); return true; } else if(ignoreUndo()){ d_matrix_model->calculate(startRow, endRow, startCol, endCol); emit modifiedWindow(this); return true; } return false; }
void Matrix::setColorMap(const QwtLinearColorMap& map) { d_color_map_type = Custom; d_color_map = map; if (d_view_type == ImageView) displayImage(d_matrix_model->renderImage()); emit modifiedWindow(this); }
void Matrix::setColumnsWidth(int width) { if (width == columnsWidth()) return; for (int i=0; i<d_table->numCols(); i++) d_table->setColumnWidth (i, width); emit modifiedWindow(this); }
void Matrix::setHeaderViewType(HeaderViewType type) { if (d_header_view_type == type) return; d_header_view_type = type; if (d_view_type == TableView) resetView(); emit modifiedWindow(this); }
void Matrix::setNumericFormat(const QChar& f, int prec) { if (txt_format == f && num_precision == prec) return; txt_format = f; num_precision = prec; resetView(); emit modifiedWindow(this); QApplication::restoreOverrideCursor(); }
void Matrix::setCoordinates(double xs, double xe, double ys, double ye) { if (x_start == xs && x_end == xe && y_start == ys && y_end == ye) return; x_start = xs; x_end = xe; y_start = ys; y_end = ye; emit modifiedWindow(this); }
void Matrix::setMatrixDimensions(int rows, int cols) { int r = numRows(); int c = numCols(); if (r == rows && c == cols) return; if (rows < r || cols < c) { QString msg_text = tr("Deleting rows/columns from the matrix!","set matrix dimensions"); msg_text += tr("<p>Do you really want to continue?","set matrix dimensions"); switch( QMessageBox::information(0,tr("QtiPlot"), msg_text,tr("Yes"), tr("Cancel"), 0, 1 ) ) { case 0: // Yes QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); if (cols != c) d_table->setColumnCount(cols); if (rows != r) d_table->setRowCount(rows); QApplication::restoreOverrideCursor(); emit modifiedWindow(this); break; case 1: // Cancel return; break; } } else { QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); if (cols != c) d_table->setColumnCount(cols); if (rows != r) d_table->setRowCount(rows); QApplication::restoreOverrideCursor(); emit modifiedWindow(this); } }
void Matrix::setMatrixDimensions(int rows, int cols) { int r = d_table->numRows(); int c = d_table->numCols(); if (r == rows && c == cols) return; if (rows < r || cols < c) { QString text="Deleting rows/columns from the matrix!"; text+="<p>Do you really want to continue?"; switch( QMessageBox::information(0,"QtiPlot", tr(text),tr("Yes"), tr("Cancel"), 0, 1 ) ) { case 0: QApplication::setOverrideCursor(waitCursor); if (cols != c) d_table->setNumCols(cols); if (rows != r) d_table->setNumRows(rows); QApplication::restoreOverrideCursor(); emit modifiedWindow(this); break; case 1: return; break; } } else { QApplication::setOverrideCursor(waitCursor); if (cols != c) d_table->setNumCols(cols); if (rows != r) d_table->setNumRows(rows); QApplication::restoreOverrideCursor(); emit modifiedWindow(this); } }
void Matrix::setRainbowColorMap() { d_color_map_type = Rainbow; d_color_map = QwtLinearColorMap(Qt::blue, Qt::red); d_color_map.addColorStop(0.25, Qt::cyan); d_color_map.addColorStop(0.5, Qt::green); d_color_map.addColorStop(0.75, Qt::yellow); if (d_view_type == ImageView) displayImage(d_matrix_model->renderImage()); emit modifiedWindow(this); }
void Matrix::deleteSelectedRows() { QTableSelection sel=d_table->selection(d_table->currentSelection()); int top=sel.topRow(); int bottom=sel.bottomRow(); QMemArray<int> rows(bottom-top+1); for (int i=top; i<=bottom; i++) rows[i-top]= i; d_table->removeRows(rows); emit modifiedWindow(this); }
void Matrix::setColumnsWidth(int width) { if (d_column_width == width) return; d_column_width = width; d_table_view->horizontalHeader()->setDefaultSectionSize(d_column_width); if (d_view_type == TableView){ int cols = numCols(); for(int i=0; i<cols; i++) d_table_view->setColumnWidth(i, width); } emit modifiedWindow(this); }
void Matrix::deleteSelectedColumns() { QMemArray<int> cols; int n=0; for (int i=0; i<d_table->numCols(); i++) { if (d_table->isColumnSelected (i, true)) { n++; cols.resize(n); cols[n-1]= i; } } d_table->removeColumns(cols); emit modifiedWindow(this); }
void Matrix::invert() { allow_modification_signals = false; int rows = numRows(); int cols = numCols(); if (rows != cols) { QMessageBox::critical(0,tr("QtiPlot - Error"), tr("Inversion failed, the matrix is not square!")); allow_modification_signals = true; return; } QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); gsl_matrix *A = gsl_matrix_alloc(rows, cols); int i, j; for(i=0; i<rows; i++) { for(j=0; j<cols; j++) { QString s = text(i,j); gsl_matrix_set(A, i, j, s.toDouble()); } } gsl_permutation * p = gsl_permutation_alloc(cols); gsl_linalg_LU_decomp(A, p, &i); gsl_matrix *inverse = gsl_matrix_alloc(rows, cols); gsl_linalg_LU_invert(A, p, inverse); gsl_matrix_free(A); gsl_permutation_free(p); for(i=0; i<rows; i++) { for(j=0; j<cols; j++) setCell(i, j, gsl_matrix_get(inverse, i, j)); } gsl_matrix_free(inverse); QApplication::restoreOverrideCursor(); allow_modification_signals = true; emit modifiedWindow(this); }
bool Matrix::calculate(int startRow, int endRow, int startCol, int endCol) { QApplication::setOverrideCursor(waitCursor); Script *script = scriptEnv->newScript(formula_str, this, QString("<%1>").arg(name())); connect(script, SIGNAL(error(const QString&,const QString&,int)), scriptEnv, SIGNAL(error(const QString&,const QString&,int))); connect(script, SIGNAL(print(const QString&)), parent()->parent()->parent(), SLOT(scriptPrint(const QString&))); if (!script->compile()) { QApplication::restoreOverrideCursor(); return false; } int rows=d_table->numRows(); int cols=d_table->numCols(); if (endCol >= cols) d_table->setNumCols(endCol+1); if (endRow >= rows) d_table->setNumRows(endRow+1); QVariant ret; saveCellsToMemory(); for (int row=startRow; row<=endRow; row++) for (int col=startCol; col<=endCol; col++) { script->setInt(row+1, "i"); script->setInt(row+1, "row"); script->setInt(col+1, "j"); script->setInt(col+1, "col"); ret = script->eval(); if (ret.type()==QVariant::Int || ret.type()==QVariant::UInt || ret.type()==QVariant::LongLong || ret.type()==QVariant::ULongLong) d_table->setText(row, col, ret.toString()); else if(ret.canCast(QVariant::Double)) d_table->setText(row,col,QString::number(ret.toDouble(),txt_format, num_precision)); else { d_table->setText(row,col,""); QApplication::restoreOverrideCursor(); return false; } } forgetSavedCells(); emit modifiedWindow(this); QApplication::restoreOverrideCursor(); return true; }
void Matrix::insertColumn() { QItemSelectionModel *selModel = d_table_view->selectionModel(); if (!selModel || !selModel->hasSelection()) return; QModelIndex index = selModel->currentIndex(); if (!index.isValid()) return; if (!d_matrix_model->canResize(numRows(), numCols() + 1)) return; d_undo_stack->push(new MatrixInsertColCommand(d_matrix_model, index.column(), tr("Insert Column") + " " + QString::number(index.column() + 1))); d_table_view->reset(); emit modifiedWindow(this); }
void Matrix::cellEdited(int row,int col) { Script *script = scriptEnv->newScript(d_table->text(row,col),this,QString("<%1_%2_%3>").arg(name()).arg(row).arg(col)); connect(script, SIGNAL(error(const QString&,const QString&,int)), scriptEnv, SIGNAL(error(const QString&,const QString&,int))); script->setInt(row+1, "row"); script->setInt(row+1, "i"); script->setInt(col+1, "col"); script->setInt(col+1, "j"); QVariant ret = script->eval(); if(ret.type()==QVariant::Int || ret.type()==QVariant::UInt || ret.type()==QVariant::LongLong || ret.type()==QVariant::ULongLong) d_table->setText(row, col, ret.toString()); else if(ret.canCast(QVariant::Double)) d_table->setText(row, col, QString::number(ret.toDouble(), txt_format, num_precision)); else d_table->setText(row, col, ""); emit modifiedWindow(this); }