void arnNumericMatrixViewer::Moltiplica_Hndl(void)
{
    bool ok = false;
    double res = QInputDialog::getDouble(
        tr( "Aggiungi" ),
        tr( "Inserisci Numero." ),
        0, -2147483647, 2147483647, prec + 3, &ok, this, "InizializeValueBox" );
    if ( ok ) { // user entered something and pressed OK
        for (int indxSel = 0; indxSel < pMatrix->numSelections(); indxSel++) {
            Q3TableSelection sel = pMatrix->selection(indxSel);
            for (int indR = sel.topRow(); indR <= sel.bottomRow(); indR++) {
                for (int indC = sel.leftCol(); indC <= sel.rightCol(); indC++) {
                    if ((indR < pMatrix->numRows()) && (indC < pMatrix->numCols())) {
                        double val = pMatrix->text(indR,indC).toDouble();
                        val *= res;
                        setValue(indR,indC,val);
                        doValueChange(indR,indC);
                        }
                    }
                }
            }
        }
	else {;}// user pressed Cancel

    pMatrix->clearSelection();
}
void arnNumericMatrixViewer::Copia_Hndl(void)
{
	QString aTxt("MATRIX\n");
	int minR, minC;
	minR = pMatrix->numRows();
	minC = pMatrix->numCols();
	for (int indxSel = 0; indxSel < pMatrix->numSelections(); indxSel++) {
		Q3TableSelection sel = pMatrix->selection(indxSel);
		for (int indR = sel.topRow(); indR <= sel.bottomRow(); indR++) {
			for (int indC = sel.leftCol(); indC <= sel.rightCol(); indC++) {
				if ((indR < pMatrix->numRows()) && (indC < pMatrix->numCols())) {
					if (indR < minR) minR = indR;
					if (indC < minC) minC = indC;
					}
				}
			}
		}
	for (int indxSel = 0; indxSel < pMatrix->numSelections(); indxSel++) {
		Q3TableSelection sel = pMatrix->selection(indxSel);
		for (int indR = sel.topRow(); indR <= sel.bottomRow(); indR++) {
			for (int indC = sel.leftCol(); indC <= sel.rightCol(); indC++) {
				if ((indR < pMatrix->numRows()) && (indC < pMatrix->numCols())) {
					QString r,c;
					r.setNum(indR - minR);
					c.setNum(indC - minC);
					aTxt += r; aTxt += ",";
					aTxt += c; aTxt += ",";
					aTxt += pMatrix->text(indR,indC);
					aTxt += "\n";
					}
				}
			}
		}

	if (pClip) {
		if (pClip->supportsSelection()) {
			//pClip->clear(QClipboard::Clipboard);
			//pClip->clear(QClipboard::Selection);
			pClip->setText( aTxt, QClipboard::Selection);
			pMatrix->clearSelection();
			Incolla->setEnabled(true);
			}
		else {
			//pClip->clear(QClipboard::Clipboard);
			//pClip->clear(QClipboard::Selection);
			pClip->setText( aTxt, QClipboard::Clipboard);
			pMatrix->clearSelection();
			Incolla->setEnabled(true);
			}
		}
}
void arnBitValueMatrixViewer::Azzera_Hndl(void)
{
    for (int indxSel = 0; indxSel < pMatrix->numSelections(); indxSel++) {
        Q3TableSelection sel = pMatrix->selection(indxSel);
        for (int indR = sel.topRow(); indR <= sel.bottomRow(); indR++) {
            for (int indC = sel.leftCol(); indC <= sel.rightCol(); indC++) {
                if ((indR < pMatrix->numRows()) && (indC < pMatrix->numCols())) {
                    setValue(indR,indC,0);
                    }
                }
            }
        }

    pMatrix->clearSelection();
	//if (pVector) pVector->invalidate();
}
void arnNumericMatrixViewer::Azzera_Hndl(void)
{
    for (int indxSel = 0; indxSel < pMatrix->numSelections(); indxSel++) {
        Q3TableSelection sel = pMatrix->selection(indxSel);
        for (int indR = sel.topRow(); indR <= sel.bottomRow(); indR++) {
            for (int indC = sel.leftCol(); indC <= sel.rightCol(); indC++) {
                if ((indR < pMatrix->numRows()) && (indC < pMatrix->numCols())) {
                    setValue(indR,indC,0);
                    doValueChange(indR,indC);
                    }
                }
            }
        }

    pMatrix->clearSelection();
}
void arnBitValueMatrixViewer::selectionChangedHndl(void)
{
	if (!pMatrix) return;
	if (!pVector) return;
	if (pMatrix->numSelections() > 1) return;
	Q3TableSelection sel = pMatrix->selection(0);
	if (sel.leftCol() < sel.rightCol()) return;
	if ((sel.bottomRow() - sel.topRow() + 1) != pMatrix->numRows()) return;
	//arnDebug("%d,%d,%d,%d\n",sel.topRow(),sel.bottomRow(),sel.leftCol(),sel.rightCol());
	for (int indR = sel.topRow(); indR <= sel.bottomRow(); indR++) {
		pVector->setValue(indR,pMatrix->text(indR,sel.leftCol()).toDouble());
	}
}
void arnNumericMatrixViewer::InterpolaY_Hndl(void)
{
    for (int indxSel = 0; indxSel < pMatrix->numSelections(); indxSel++) {
        Q3TableSelection sel = pMatrix->selection(indxSel);
        for (int indC = sel.leftCol() ; indC <= sel.rightCol(); indC++) {
            double valP1= pMatrix->text(sel.topRow(),indC).toDouble();
            double valP2 = pMatrix->text(sel.bottomRow(),indC).toDouble();
            double step = (valP2 - valP1) / (sel.bottomRow() - sel.topRow());
            for (int indR = sel.topRow() + 1; indR < sel.bottomRow(); indR++) {
                valP1 += step;
                setValue(indR,indC,valP1);
                doValueChange(indR,indC);
                }
            }
        }

    pMatrix->clearSelection();
}
void arnBitValueMatrixViewer::InterpolaX_Hndl(void)
{
    for (int indxSel = 0; indxSel < pMatrix->numSelections(); indxSel++) {
        Q3TableSelection sel = pMatrix->selection(indxSel);
        for (int indR = sel.topRow(); indR <= sel.bottomRow(); indR++) {
            double valP1= pMatrix->text(indR,sel.leftCol()).toDouble();
            double valP2 = pMatrix->text(indR,sel.rightCol()).toDouble();
            double step = (valP2 - valP1) / (sel.rightCol() - sel.leftCol());
            for (int indC = sel.leftCol() + 1; indC < sel.rightCol(); indC++) {
                valP1 += step;
                setValue(indR,indC,valP1);
                }
            }
        }

    pMatrix->clearSelection();
	//if (pVector) pVector->invalidate();
}
void SetColValuesDialog::setTable(Table* w)
{
	table = w;
	QStringList colNames = w->colNames();
	int cols = w->numCols();
	for (int i=0; i<cols; i++)
		boxColumn->insertItem("col(\""+colNames[i]+"\")", i);

	int s = w->table()->currentSelection();
	if (s >= 0) {
		Q3TableSelection sel = w->table()->selection(s);
		w->setSelectedCol(sel.leftCol());

		start->setValue(sel.topRow() + 1);
		end->setValue(sel.bottomRow() + 1);
	} else {
		start->setValue(1);
		end->setValue(w->numRows());
	}

	updateColumn(w->selectedColumn());
}
void arnBitValueMatrixViewer::Inizializza_Hndl(void)
{
    bool ok = false;
    double res = QInputDialog::getDouble(
        tr( "Inizializza" ),
        tr( "Inserisci Numero." ),
        0, -2147483647, 2147483647, prec + 3, &ok, this, "InizializeValueBox" );
    if ( ok ) { // user entered something and pressed OK
        for (int indxSel = 0; indxSel < pMatrix->numSelections(); indxSel++) {
            Q3TableSelection sel = pMatrix->selection(indxSel);
            for (int indR = sel.topRow(); indR <= sel.bottomRow(); indR++) {
                for (int indC = sel.leftCol(); indC <= sel.rightCol(); indC++) {
                    if ((indR < pMatrix->numRows()) && (indC < pMatrix->numCols())) {
                        setValue(indR,indC,res);
                        }
                    }
                }
            }
        }
	else {;}// user pressed Cancel

    pMatrix->clearSelection();
	//if (pVector) pVector->invalidate();
}
FrequencyCountDialog::FrequencyCountDialog(Table *t, QWidget* parent, Qt::WFlags fl )
    : QDialog( parent, fl ),
    d_source_table(t),
    d_result_table(NULL),
    d_col_name(""),
    d_col_values(NULL),
	d_bins(10)
{
	setObjectName( "FrequencyCountDialog" );
	setWindowTitle(tr("QtiPlot - Frequency count"));
	setSizeGripEnabled( true );
	setAttribute(Qt::WA_DeleteOnClose);

    QGroupBox *gb1 = new QGroupBox();
    QGridLayout *gl1 = new QGridLayout(gb1);

	ApplicationWindow *app = (ApplicationWindow *)parent;
	double min = 0.0, max = 0.0, step = 0.0;
	if (t){
        int col = -1;
        int sr = 0;
        int er = t->numRows();
        int ts = t->table()->currentSelection();
        if (ts >= 0){
            Q3TableSelection sel = t->table()->selection(ts);
            sr = sel.topRow();
            er = sel.bottomRow() + 1;
            col = sel.leftCol();
            d_col_name = t->colName(col);
        }
        int size = 0;
        for (int i = sr; i < er; i++){
            if (!t->text(i, col).isEmpty())
                size++;
        }

        if (size > 1)
            d_col_values = gsl_vector_alloc(size);

        if (d_col_values){
            int aux = 0;
            for (int i = sr; i < er; i++){
                if (!t->text(i, col).isEmpty()){
                    gsl_vector_set(d_col_values, aux, t->cell(i, col));
                    aux++;
                }
            }

            gsl_sort_vector(d_col_values);

            min = floor(gsl_vector_get(d_col_values, 0));
            max = ceil(gsl_vector_get(d_col_values, size - 1));
            step = (max - min)/(double)d_bins;

            int p = app->d_decimal_digits;
            double *data = d_col_values->data;
            QLocale l = app->locale();
            QString s = "[" + QDateTime::currentDateTime().toString(Qt::LocalDate)+ " \"" + t->objectName() + "\"]\n";
            s += tr("Statistics on %1").arg(d_col_name) + ":\n";
            s += tr("Mean") + " = " + l.toString(gsl_stats_mean (data, 1, size), 'f', p) + "\n";
            s += tr("Standard Deviation") + " = " + l.toString(gsl_stats_sd(data, 1, size), 'f', p) + "\n";
            s += tr("Median") + " = " + l.toString(gsl_stats_median_from_sorted_data(data, 1, size), 'f', p) + "\n";
            s += tr("Size") + " = " + QString::number(size) + "\n";
            s += "--------------------------------------------------------------------------------------\n";
            app->updateLog(s);
        }
	}

    gl1->addWidget(new QLabel(tr("From Minimum")), 0, 0);

	boxStart = new DoubleSpinBox();
	boxStart->setLocale(app->locale());
	boxStart->setValue(min);
	boxStart->setDecimals(app->d_decimal_digits);
	gl1->addWidget(boxStart, 0, 1);

    gl1->addWidget(new QLabel(tr("To Maximum")), 1, 0);

    boxEnd = new DoubleSpinBox();
	boxEnd->setLocale(app->locale());
    boxEnd->setValue(max);
    boxEnd->setDecimals(app->d_decimal_digits);
    gl1->addWidget(boxEnd, 1, 1);

    gl1->addWidget(new QLabel(tr("Step Size")), 2, 0);

    boxStep = new DoubleSpinBox();
	boxStep->setLocale(app->locale());
    boxStep->setValue(step);
    boxStep->setDecimals(app->d_decimal_digits);
    gl1->addWidget(boxStep, 2, 1);

    gl1->setRowStretch(3, 1);
	gl1->setColumnStretch(1, 1);

	buttonApply = new QPushButton(tr( "&Apply" ));
    buttonApply->setDefault( true );
    buttonCancel = new QPushButton(tr( "&Cancel" ));
    buttonOk = new QPushButton(tr( "&Ok" ));

    QVBoxLayout *vl = new QVBoxLayout();
 	vl->addWidget(buttonApply);
 	vl->addWidget(buttonOk);
	vl->addWidget(buttonCancel);
    vl->addStretch();

    QHBoxLayout *hb = new QHBoxLayout(this);
    hb->addWidget(gb1, 1);
    hb->addLayout(vl);

	connect( buttonApply, SIGNAL( clicked() ), this, SLOT( apply() ) );
    connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
    connect( buttonOk, SIGNAL( clicked() ), this, SLOT( accept() ) );
}