Logo Search packages:      
Sourcecode: qalculate-kde version File versions  Download package

qalculateinsertmatrixvectordialog.cpp

/***************************************************************************
 *   Copyright (C) 2005 by Niklas Knutsson   *
 *   nq@altern.org   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "qalculateinsertmatrixvectordialog.h"
#include "qalculate_kde_utils.h"

#include <qgrid.h>
#include <klineedit.h>
#include <qpushbutton.h>
#include <kcombobox.h>
#include <qlabel.h>
#include <klocale.h>
#include <qvbox.h>
#include <qhbox.h>
#include <qcheckbox.h>
#include <kmessagebox.h>
#include <qspinbox.h>
#include <qlayout.h>
#include <qbuttongroup.h>
#include <qradiobutton.h>
#include <qtable.h>
#include <kapplication.h>
#include <kdeversion.h>
#include <kstdguiitem.h>

extern PrintOptions printops;

QalculateInsertMatrixVectorDialog::QalculateInsertMatrixVectorDialog(QWidget *parent, const char *name) : KDialogBase(parent, name, true, i18n("Edit Variable"), Ok | Cancel | User1, Ok, true, KGuiItem(i18n("Insert Selection"))) {

      setMainWidget(new QWidget(this));
      QGridLayout *grid = new QGridLayout(mainWidget(), 1, 1, 0, spacingHint());
      grid->addWidget(new QLabel(i18n("Rows:"), mainWidget()), 4, 0);
      rowsBox = new QSpinBox(1, 1000, 1, mainWidget());
      grid->addWidget(rowsBox, 4, 1);
      grid->addWidget(new QLabel(i18n("Columns:"), mainWidget()), 5, 0);
      columnsBox = new QSpinBox(1, 1000, 1, mainWidget());
      grid->addWidget(columnsBox, 5, 1);
      QHBoxLayout *hbox = new QHBoxLayout(0, 0, spacingHint());
      grid->addMultiCellLayout(hbox, 6, 6, 0, 1);
      elementsLabel = new QLabel(i18n("Elements:"), mainWidget());
      hbox->addWidget(elementsLabel);
      hbox->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum));
      QButtonGroup *group = new QButtonGroup();
      matrixButton = new QRadioButton(i18n("Matrix"), mainWidget());
      group->insert(matrixButton, 0);
      hbox->addWidget(matrixButton);
      vectorButton = new QRadioButton(i18n("Vector"), mainWidget());
      group->insert(vectorButton, 1);
      hbox->addWidget(vectorButton);
      elementsTable = new QTable(4, 4, mainWidget());
      grid->addMultiCellWidget(elementsTable, 7, 7, 0, 1);
      elementsTable->setSelectionMode(QTable::Single);
      
      insertSelectionButton = actionButton(User1);
      onSelectionChanged();

      connect(insertSelectionButton, SIGNAL(clicked()), this, SLOT(insertSelection()));
      connect(elementsTable, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
      connect(rowsBox, SIGNAL(valueChanged(int)), this, SLOT(rowsChanged(int)));
      connect(columnsBox, SIGNAL(valueChanged(int)), this, SLOT(columnsChanged(int)));
      connect(group, SIGNAL(clicked(int)), this, SLOT(typeChanged(int)));

}

QalculateInsertMatrixVectorDialog::~QalculateInsertMatrixVectorDialog() {}

void QalculateInsertMatrixVectorDialog::onSelectionChanged() {
      insertSelectionButton->setEnabled(elementsTable->currentSelection() >= 0);
}
void QalculateInsertMatrixVectorDialog::typeChanged(int i) {
      if(i == 0) {
            elementsLabel->setText(i18n("Elements:"));
            bool b = true;
            for(int index_r = elementsTable->numRows() - 1; index_r >= 0 && b; index_r--) {
                  for(int index_c = elementsTable->numCols() - 1; index_c >= 0; index_c--) {
                        if(elementsTable->text(index_r, index_c).isEmpty()) {
                              elementsTable->setText(index_r, index_c, "0");
                        } else {
                              b = false;
                              break;
                        }
                  }
            }
      } else {
            elementsLabel->setText(i18n("Components (in horizontal order):"));
      }
}
void QalculateInsertMatrixVectorDialog::rowsChanged(int i) {
      int r = elementsTable->numRows();
      elementsTable->setNumRows(i);
      if(matrixButton->isChecked()) {
            for(int index_r = r; index_r < i; index_r++) {
                  for(int index_c = 0; index_c < elementsTable->numCols(); index_c++) {
                        elementsTable->setText(index_r, index_c, "0");
                  }
            }
      }
}
void QalculateInsertMatrixVectorDialog::columnsChanged(int i) {
      int c = elementsTable->numCols();
      elementsTable->setNumCols(i);
      if(matrixButton->isChecked()) {
            for(int index_r = 0; index_r < elementsTable->numRows(); index_r++) {
                  for(int index_c = c; index_c < i; index_c++) {
                        elementsTable->setText(index_r, index_c, "0");
                  }
            }
      }
}

QString QalculateInsertMatrixVectorDialog::newVector() {
      return editMatrixVector(NULL, true);
}
QString QalculateInsertMatrixVectorDialog::newMatrix() {
      return editMatrixVector(NULL, false);
}

void QalculateInsertMatrixVectorDialog::slotOk() {
      int r = rowsBox->value();
      int c = columnsBox->value();
      if(vectorButton->isChecked()) {
            bool b = false;
            matrixstr = "[";
            for(int index_r = 0; index_r < r; index_r++) {
                  for(int index_c = 0; index_c < c; index_c++) {
                        if(!elementsTable->text(index_r, index_c).isEmpty()) {
                              QString str2 = elementsTable->text(index_r, index_c).stripWhiteSpace();
                              if(!str2.isEmpty()) {
                                    if(b) {
                                          matrixstr += ", ";
                                    } else {
                                          b = true;
                                    }
                                    matrixstr += str2;
                              }
                        }
                  }
            }
            matrixstr += "]";
      } else {
            matrixstr = "[";
            bool b1 = false;
            for(int index_r = 0; index_r < r; index_r++) {
                  if(b1) {
                        matrixstr += ", ";
                  } else {
                        b1 = true;
                  }
                  matrixstr += "[";
                  bool b2 = false;
                  for(int index_c = 0; index_c < c; index_c++) {
                        if(b2) {
                              matrixstr += ", ";
                        } else {
                              b2 = true;
                        }
                        matrixstr += elementsTable->text(index_r, index_c).stripWhiteSpace();                     
                  }
                  matrixstr += "]";
            }
            matrixstr += "]";
      }                             
      accept();
}
void QalculateInsertMatrixVectorDialog::insertSelection() {
      QTableSelection sel = elementsTable->selection(elementsTable->currentSelection());
      if(vectorButton->isChecked()) {
            bool b = false;
            matrixstr = "[";
            for(int index_r = sel.topRow(); index_r <= sel.bottomRow(); index_r++) {
                  for(int index_c = sel.leftCol(); index_c <= sel.rightCol(); index_c++) {
                        if(!elementsTable->text(index_r, index_c).isEmpty()) {
                              QString str2 = elementsTable->text(index_r, index_c).stripWhiteSpace();
                              if(!str2.isEmpty()) {
                                    if(b) {
                                          matrixstr += ", ";
                                    } else {
                                          b = true;
                                    }
                                    matrixstr += str2;
                              }
                        }
                  }
            }
            matrixstr += "]";
      } else {
            matrixstr = "[";
            bool b1 = false;
            for(int index_r = sel.topRow(); index_r <= sel.bottomRow(); index_r++) {
                  if(b1) {
                        matrixstr += ", ";
                  } else {
                        b1 = true;
                  }
                  matrixstr += "[";
                  bool b2 = false;
                  for(int index_c = sel.leftCol(); index_c <= sel.rightCol(); index_c++) {
                        if(b2) {
                              matrixstr += ", ";
                        } else {
                              b2 = true;
                        }
                        matrixstr += elementsTable->text(index_r, index_c).stripWhiteSpace();                     
                  }
                  matrixstr += "]";
            }
            matrixstr += "]";
      }                             
      accept();
}

QString QalculateInsertMatrixVectorDialog::editMatrixVector(const MathStructure *initial_value, bool create_vector, bool is_text_struct, bool is_result) {

      if(initial_value && !initial_value->isVector()) {
            return NULL;
      }

      if(initial_value) {
            create_vector = !initial_value->isMatrix();
      }
      if(create_vector) {
            vectorButton->setChecked(true);
      } else {
            matrixButton->setChecked(true);
      }

      if(is_result) {
#if KDE_VERSION_MAJOR < 4 && KDE_VERSION_MINOR < 2
            setButtonOKText(i18n("Insert"));
            setButtonCancelText(KStdGuiItem::close().text());
#else
#if KDE_VERSION_MAJOR < 4 && KDE_VERSION_MINOR < 3
            setButtonOK(KGuiItem(i18n("Insert")));
#else
            setButtonOK(KStdGuiItem::insert()); 
#endif
            setButtonCancel(KStdGuiItem::close());
#endif            
            actionButton(Cancel)->setDefault(true);
            actionButton(Cancel)->setFocus();
            if(create_vector) {
                  setCaption(i18n("Vector Result"));
            } else {
                  setCaption(i18n("Matrix Result"));
            }
      } else {
#if KDE_VERSION_MAJOR < 4 && KDE_VERSION_MINOR < 2
            setButtonOKText(KStdGuiItem::ok().text());
            setButtonCancelText(KStdGuiItem::cancel().text());
#else
            setButtonOK(KStdGuiItem::ok());
            setButtonCancel(KStdGuiItem::cancel());
#endif      
            actionButton(Ok)->setDefault(true);
            elementsTable->setFocus();
            if(create_vector) {
                  setCaption(i18n("Vector"));
            } else {
                  setCaption(i18n("Matrix"));
            }
      }

      int r = 4, c = 4;
      if(create_vector) {
            if(initial_value) {
                  r = initial_value->components();
                  c = (int) ::sqrt(r) + 4;
                  if(r % c > 0) {
                        r = r / c + 1;
                  } else {
                        r = r / c;
                  }
            } else {
                  c = 8;
                  r = 3;
            }
      } else if(initial_value) {
            c = initial_value->columns();
            r = initial_value->rows();
      }

      rowsBox->setValue(r);
      columnsBox->setValue(c);
      elementsTable->setNumRows(r);
      elementsTable->setNumCols(c);

      int timeout;
      if(initial_value) timeout = 3000000 / (r * c);
      else timeout = 3000000;
      printops.can_display_unicode_string_arg = (void*) elementsTable;
      for(int index_r = 0; index_r < r; index_r++) {
            for(int index_c = 0; index_c < c; index_c++) {
                  if(create_vector) {
                        if(initial_value && index_r * c + index_c < (int) initial_value->components()) {
                              if(is_text_struct) elementsTable->setText(index_r, index_c, initial_value->getComponent(index_r * c + index_c + 1)->symbol().c_str());
                              else elementsTable->setText(index_r, index_c, CALCULATOR->printMathStructureTimeOut(*initial_value->getComponent(index_r * c + index_c + 1), timeout, printops).c_str());
                        } else {
                              elementsTable->setText(index_r, index_c, "");
                        }
                  } else {
                        if(initial_value) {
                              if(is_text_struct) elementsTable->setText(index_r, index_c, initial_value->getElement(index_r + 1, index_c + 1)->symbol().c_str());
                              else elementsTable->setText(index_r, index_c, CALCULATOR->printMathStructureTimeOut(*initial_value->getElement(index_r + 1, index_c + 1), timeout, printops).c_str());
                        } else {
                              elementsTable->setText(index_r, index_c, "0");
                        }
                  }
            }
      }
      printops.can_display_unicode_string_arg = NULL;
      matrixstr = "";
      
      onSelectionChanged();

      exec();

      return matrixstr;
}


#include "qalculateinsertmatrixvectordialog.moc"


Generated by  Doxygen 1.6.0   Back to index