1491
1516
bool allNumbers = true;
1492
1517
bool pasteComments = false;
1493
1518
bool pasteHeader = false;
1494
for (int i = 0; i < firstLine.size(); i++)
1495
{//verify if the strings in the line used to rename the columns are not all numbers
1496
clipboardLocale.toDouble(firstLine[i], &allNumbers);
1498
switch(QMessageBox::question(this, tr("QtiPlot") + " - " + tr("Paste operation"),
1499
tr("How should QtiPlot interpret first clipboard line?"),
1500
tr("&Values"), tr("Column &Names"), tr("&Comments"), 1, 1)){
1519
if (rows > 1 && applicationWindow() && applicationWindow()->d_show_table_paste_dialog){
1520
for (int i = 0; i < firstLine.size(); i++){
1521
//verify if the strings in the line used to rename the columns are not all numbers
1522
clipboardLocale.toDouble(firstLine[i], &allNumbers);
1524
QMessageBox msgBox(this);
1525
msgBox.setIconPixmap(QPixmap(":/qtiplot_logo.png"));
1526
msgBox.setWindowTitle(tr("QtiPlot") + " - " + tr("Paste operation"));
1527
msgBox.setText(tr("How should QtiPlot interpret first clipboard line?"));
1528
msgBox.addButton(tr("&Values"), QMessageBox::AcceptRole);
1529
QPushButton *namesButton = msgBox.addButton(tr("Column &Names"), QMessageBox::AcceptRole);
1530
msgBox.setDefaultButton(namesButton);
1531
QAbstractButton *commentsButton = msgBox.addButton(tr("&Comments"), QMessageBox::AcceptRole);
1532
msgBox.addButton(QMessageBox::Cancel);
1534
if (msgBox.exec() == QMessageBox::Cancel)
1537
if (msgBox.clickedButton() == namesButton){
1504
1538
pasteHeader = true;
1505
1539
linesList.pop_front();
1541
} else if (msgBox.clickedButton() == commentsButton){
1509
1542
pasteComments = true;
1510
1543
linesList.pop_front();
2047
2072
void Table::saveToMemory()
2049
int rows = d_table->numRows();
2050
int cols = d_table->numCols();
2074
int rows = d_table->numRows();
2075
int cols = d_table->numCols();
2052
d_saved_cells = new double* [cols];
2077
d_saved_cells = new double* [cols];
2053
2078
for ( int i = 0; i < cols; ++i)
2054
2079
d_saved_cells[i] = new double [rows];
2056
for (int col = 0; col < cols; col++){// initialize the matrix to zero
2057
for (int row = 0; row < rows; row++)
2058
d_saved_cells[col][row] = 0.0;}
2081
for (int col = 0; col < cols; col++){// initialize the matrix to zero
2082
for (int row = 0; row < rows; row++)
2083
d_saved_cells[col][row] = 0.0;}
2060
2085
for (int col = 0; col < cols; col++){
2061
if (colTypes[col] == Time){
2086
int colType = colTypes[col];
2087
QString fmt = col_format[col].trimmed();
2088
if (colType == Time){
2062
2089
QTime ref = QTime(0, 0);
2063
2090
for (int row = 0; row <rows; row++){
2064
QTime t = QTime::fromString(d_table->text(row, col), col_format[col]);
2065
d_saved_cells[col][row] = ref.msecsTo(t);
2068
else if (colTypes[col] == Date){
2091
QTime t = QTime::fromString(d_table->text(row, col).trimmed(), fmt);
2092
d_saved_cells[col][row] = ref.msecsTo(t);
2094
} else if (colType == Date){
2069
2095
QTime ref = QTime(0, 0);
2070
2096
for (int row = 0; row < rows; row++){
2071
QDateTime dt = QDateTime::fromString(d_table->text(row, col), col_format[col]);
2097
QDateTime dt = QDateTime::fromString(d_table->text(row, col).trimmed(), fmt);
2072
2098
d_saved_cells[col][row] = dt.date().toJulianDay() - 1 + (double)ref.msecsTo(dt.time())/864.0e5;
2077
QLocale l = locale();
2103
QLocale l = locale();
2078
2104
for (int col = 0; col < cols; col++){
2079
if (colTypes[col] == Numeric){
2080
for (int row=0; row < rows; row++){
2081
QString s = d_table->text(row, col);
2083
d_saved_cells[col][row] = l.toDouble(s);
2105
if (colTypes[col] == Numeric){
2106
for (int row = 0; row < rows; row++){
2107
QString s = d_table->text(row, col);
2109
d_saved_cells[col][row] = l.toDouble(s);
2292
2337
d_table->setText(i, col, QDate::longDayName(day));
2340
emit modifiedData(this, colName(col));
2297
2343
void Table::setRandomValues()
2299
QStringList list=selectedColumns();
2300
QStringList lstReadOnly;
2301
for (int i=0; i<list.count(); i++){
2302
QString name = list[i];
2303
int col = colIndex(name);
2304
if (d_table->isColumnReadOnly(col))
2305
lstReadOnly << name;
2308
if (lstReadOnly.count() > 0){
2309
QMessageBox::warning(this, tr("QtiPlot - Error"),
2310
tr("The folowing columns")+":\n"+ lstReadOnly.join("\n") + "\n"+ tr("are read only!"));
2313
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
2317
int rows=d_table->numRows();
2318
for (int j=0; j<(int) list.count(); j++){
2319
QString name=list[j];
2320
selectedCol=colIndex(name);
2321
if (d_table->isColumnReadOnly(selectedCol))
2326
columnNumericFormat(selectedCol, &f, &prec);
2328
for (int i=0; i<rows; i++)
2329
d_table->setText(i, selectedCol, locale().toString(double(rand())/double(RAND_MAX), f, prec));
2331
emit modifiedData(this, name);
2334
emit modifiedWindow(this);
2345
QStringList list = writableSelectedColumns();
2349
Q3TableSelection selection = getSelection();
2350
for (int i = 0; i < list.count(); i++)
2351
setRandomValues(colIndex(list[i]), selection.topRow(), selection.bottomRow());
2353
emit modifiedWindow(this);
2356
void Table::setRandomValues(int col, int startRow, int endRow)
2358
if (col < 0 || col >= d_table->numCols() || d_table->isColumnReadOnly(col))
2361
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
2363
int rows = d_table->numRows();
2364
if (startRow < 0 || startRow >= rows)
2366
if (endRow < 0 || endRow >= rows)
2371
columnNumericFormat(col, &f, &prec);
2373
srand(time(NULL) + col);
2374
for (int i = startRow; i <= endRow; i++)
2375
d_table->setText(i, col, locale().toString(double(rand())/double(RAND_MAX), f, prec));
2377
emit modifiedData(this, colName(col));
2379
QApplication::restoreOverrideCursor();
2382
void Table::setNormalRandomValues()
2384
QStringList list = writableSelectedColumns();
2388
Q3TableSelection selection = getSelection();
2389
for (int i = 0; i < list.count(); i++)
2390
setNormalRandomValues(colIndex(list[i]), selection.topRow(), selection.bottomRow());
2392
emit modifiedWindow(this);
2395
void Table::setNormalRandomValues(int col, int startRow, int endRow, double sigma)
2397
if (col < 0 || col >= d_table->numCols() || d_table->isColumnReadOnly(col))
2400
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
2402
int rows = d_table->numRows();
2403
if (startRow < 0 || startRow >= rows)
2405
if (endRow < 0 || endRow >= rows)
2408
gsl_rng_env_setup();
2409
const gsl_rng_type * T = gsl_rng_default;
2410
gsl_rng * r = gsl_rng_alloc (T);
2416
columnNumericFormat(col, &f, &prec);
2418
gsl_rng_set(r, time(NULL) + col);
2419
for (int i = startRow; i <= endRow; i++)
2420
d_table->setText(i, col, locale().toString(gsl_ran_gaussian_ziggurat(r, sigma), f, prec));
2423
emit modifiedData(this, colName(col));
2335
2424
QApplication::restoreOverrideCursor();
2393
2482
int Table::colIndex(const QString& name)
2395
int pos = name.find("_",false);
2396
QString label = name.right(name.length()-pos-1);
2397
return col_label.findIndex(label);
2484
int pos = name.lastIndexOf("_");
2485
QString label = name.right(name.length() - pos - 1);
2486
return col_label.indexOf(label);
2400
2489
void Table::setHeaderColType()
2403
for (int j=0; j < d_table->numCols(); j++){
2492
for (int j = 0; j < d_table->numCols(); j++){
2404
2493
if (col_plot_type[j] == X)
2408
2497
if (xcols > 1){
2410
for (int i=0; i < d_table->numCols(); i++){
2411
if (col_plot_type[i] == X)
2412
setColumnHeader(i, col_label[i]+"[X" + QString::number(++xcols) +"]");
2413
else if (col_plot_type[i] == Y){
2415
setColumnHeader(i, col_label[i]+"[Y"+ QString::number(xcols) +"]");
2417
setColumnHeader(i, col_label[i]+"[Y]");
2418
} else if (col_plot_type[i] == Z){
2420
setColumnHeader(i, col_label[i]+"[Z"+ QString::number(xcols) +"]");
2422
setColumnHeader(i, col_label[i]+"[Z]");
2423
} else if (col_plot_type[i] == xErr)
2424
setColumnHeader(i, col_label[i]+"[xEr]");
2425
else if (col_plot_type[i] == yErr)
2426
setColumnHeader(i, col_label[i]+"[yEr]");
2427
else if (col_plot_type[i] == Label)
2428
setColumnHeader(i, col_label[i]+"[L]");
2430
setColumnHeader(i, col_label[i]);
2499
for (int i = 0; i < d_table->numCols(); i++){
2500
if (i >= col_label.size() || i >= col_plot_type.size())
2502
int colType = col_plot_type[i];
2503
QString label = col_label[i];
2506
setColumnHeader(i, label + "[X" + QString::number(++xcols) + "]");
2510
setColumnHeader(i, label + "[Y" + QString::number(xcols) + "]");
2512
setColumnHeader(i, label + "[Y]");
2516
setColumnHeader(i, label + "[Z" + QString::number(xcols) + "]");
2518
setColumnHeader(i, label + "[Z]");
2521
setColumnHeader(i, label + "[xEr]");
2524
setColumnHeader(i, label + "[yEr]");
2527
setColumnHeader(i, label + "[L]");
2530
setColumnHeader(i, label);
2433
for (int i=0; i < d_table->numCols(); i++){
2434
if (col_plot_type[i] == X)
2435
setColumnHeader(i, col_label[i]+"[X]");
2436
else if (col_plot_type[i] == Y)
2437
setColumnHeader(i, col_label[i]+"[Y]");
2438
else if (col_plot_type[i] == Z)
2439
setColumnHeader(i, col_label[i]+"[Z]");
2440
else if (col_plot_type[i] == xErr)
2441
setColumnHeader(i, col_label[i]+"[xEr]");
2442
else if (col_plot_type[i] == yErr)
2443
setColumnHeader(i, col_label[i]+"[yEr]");
2444
else if (col_plot_type[i] == Label)
2445
setColumnHeader(i, col_label[i]+"[L]");
2447
setColumnHeader(i, col_label[i]);
2535
for (int i = 0; i < d_table->numCols(); i++){
2536
if (i >= col_label.size() || i >= col_plot_type.size())
2538
int colType = col_plot_type[i];
2539
QString label = col_label[i];
2542
setColumnHeader(i, label + "[X]");
2545
setColumnHeader(i, label + "[Y]");
2548
setColumnHeader(i, label + "[Z]");
2551
setColumnHeader(i, label + "[xEr]");
2554
setColumnHeader(i, label + "[yEr]");
2557
setColumnHeader(i, label + "[L]");
2560
setColumnHeader(i, label);
2567
QStringList Table::writableSelectedColumns()
2569
Q3TableSelection selection = getSelection();
2570
QStringList list = selectedColumns();
2571
if (list.isEmpty()){
2572
for (int i = selection.leftCol(); i <= selection.rightCol(); i++)
2576
QStringList lstReadOnly;
2577
for (int i = 0; i < list.count(); i++){
2578
QString name = list[i];
2579
int col = colIndex(name);
2580
if (d_table->isColumnReadOnly(col))
2581
lstReadOnly << name;
2584
if (!lstReadOnly.isEmpty()){
2585
QMessageBox::warning(this, tr("QtiPlot - Error"),
2586
tr("The folowing columns") + ":\n" + lstReadOnly.join("\n") + "\n" + tr("are read only!"));
2452
2591
void Table::setAscValues()
2454
QStringList list=selectedColumns();
2455
QStringList lstReadOnly;
2456
for (int i=0; i<list.count(); i++){
2457
QString name = list[i];
2458
int col = colIndex(name);
2459
if (d_table->isColumnReadOnly(col))
2460
lstReadOnly << name;
2463
if (lstReadOnly.count() > 0){
2464
QMessageBox::warning(this, tr("QtiPlot - Error"),
2465
tr("The folowing columns")+":\n"+ lstReadOnly.join("\n") + "\n"+ tr("are read only!"));
2593
QStringList list = writableSelectedColumns();
2468
2597
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
2469
int rows = d_table->numRows();
2470
for (int j=0; j<(int) list.count(); j++){
2599
Q3TableSelection selection = getSelection();
2600
for (int j = 0; j < list.count(); j++){
2471
2601
QString name = list[j];
2472
2602
selectedCol = colIndex(name);
2936
r = abs(bottomRow - topRow) + 1;
2782
2939
int aux = selectedCols;
2783
2940
if (!selectedCols)
2786
aux = QMIN(aux, 256);
2943
QTextDocument *document = new QTextDocument();
2944
QTextCursor cursor = QTextCursor(document);
2946
QTextTableFormat tableFormat;
2947
tableFormat.setAlignment(Qt::AlignCenter);
2948
tableFormat.setCellPadding(0);
2949
tableFormat.setHeaderRowCount(0);
2950
tableFormat.setBorder (1);
2951
tableFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
2952
tableFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100));
2788
2954
int headerRows = 0;
2789
2955
if (withLabels)
2791
2957
if (exportComments)
2796
BasicExcelWorksheet* sheet = xls.GetWorksheet((size_t)0);
2798
XLSFormatManager fmt_mgr(xls);
2799
ExcelFont font_bold;
2800
font_bold._weight = 700;
2801
CellFormat fmt_bold(fmt_mgr, font_bold);
2961
QTextTable *table = cursor.insertTable(r, aux, tableFormat);
2962
QTextCharFormat cellFormat;
2963
cellFormat.setBackground (QBrush(Qt::gray));
2802
2964
for (int i = 0; i < aux; i++){
2803
2965
for (int j = 0; j < headerRows; j++){
2804
BasicExcelCell* cell = sheet->Cell(j, i);
2805
cell->SetFormat(fmt_bold);
2966
QTextTableCell cell = table->cellAt(j, i);
2967
cell.setFormat(cellFormat);
2810
2971
if (withLabels){
2813
2972
QStringList header = colNames();
2814
2973
QStringList ls = header.grep ( QRegExp ("\\D"));
2815
2974
if (exportSelection && selectedCols){
2816
2975
for (int i = 0; i < aux; i++){
2817
BasicExcelCell* cell = sheet->Cell(0, i);
2818
2976
if (ls.count()>0)
2819
cell->Set(header[sCols[i]].toStdString().c_str());
2977
cursor.insertText(header[sCols[i]]);
2821
cell->Set(("C" + header[sCols[i]]).toStdString().c_str());
2979
cursor.insertText("C" + header[sCols[i]]);
2980
cursor.movePosition(QTextCursor::NextCell);
2824
2983
if (ls.count() > 0){
2825
2984
for (int j = 0; j < aux; j++){
2826
BasicExcelCell* cell = sheet->Cell(0, j);
2827
cell->Set(header[j].toStdString().c_str());
2985
cursor.insertText(header[j]);
2986
cursor.movePosition(QTextCursor::NextCell);
2830
2989
for (int j = 0; j < aux; j++){
2831
BasicExcelCell* cell = sheet->Cell(0, j);
2832
cell->Set(("C" + header[j]).toStdString().c_str());
2990
cursor.insertText("C" + header[j]);
2991
cursor.movePosition(QTextCursor::NextCell);
2838
2997
if (exportComments){
2839
2998
if (exportSelection && selectedCols){
2840
2999
for (int i = 0; i < aux; i++){
2841
QString s = comments[sCols[i]];
2844
sheet->Cell(startRow, i)->Set(s.toStdString().c_str());
3000
cursor.insertText(comments[sCols[i]]);
3001
cursor.movePosition(QTextCursor::NextCell);
2847
3004
for (int i = 0; i < aux; i++){
2848
QString s = comments[i];
2851
sheet->Cell(startRow, i)->Set(s.toStdString().c_str());
3005
cursor.insertText(comments[i]);
3006
cursor.movePosition(QTextCursor::NextCell);
2857
3011
if (exportSelection && selectedCols){
2858
3012
for (int i = topRow; i <= bottomRow; i++){
2859
3013
for (int j = 0; j < aux; j++){
2861
QString s = d_table->text(i, col);
2864
if (columnType(col) == Numeric)
2865
sheet->Cell(startRow, j)->Set(cell(i, col));
2867
sheet->Cell(startRow, j)->Set(s.toStdString().c_str());
3014
cursor.insertText(d_table->text(i, sCols[j]));
3015
cursor.movePosition(QTextCursor::NextCell);
2870
if (startRow > 65536)
2873
3018
delete [] sCols;
2875
for (int i = 0; i < rows; i++){
3020
for (int i = 0; i < rows; i++) {
2876
3021
for (int j = 0; j < aux; j++){
2877
QString s = d_table->text(i, j);
2880
if (columnType(j) == Numeric)
2881
sheet->Cell(startRow, j)->Set(cell(i, j));
2883
sheet->Cell(startRow, j)->Set(s.toStdString().c_str());
3022
cursor.insertText(d_table->text(i, j));
3023
cursor.movePosition(QTextCursor::NextCell);
2886
if (startRow > 65536)
3028
QTextDocumentWriter writer(fname);
3029
if (fname.endsWith(".html"))
3030
writer.setFormat("HTML");
3031
writer.write(document);
2893
3033
QApplication::restoreOverrideCursor();
2898
bool Table::exportODF(const QString& fname, bool withLabels, bool exportComments, bool exportSelection)
2900
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
2902
int rows = d_table->numRows();
2903
int cols = d_table->numCols();
2904
int selectedCols = 0;
2905
int topRow = 0, bottomRow = 0;
2908
if (exportSelection){
2909
for (int i=0; i<cols; i++){
2910
if (d_table->isColumnSelected(i))
2914
sCols = new int[selectedCols];
2916
for (int i=0; i<cols; i++){
2917
if (d_table->isColumnSelected(i)){
2923
for (int i=0; i<rows; i++){
2924
if (d_table->isRowSelected(i)){
2930
for (int i = rows - 1; i > 0; i--){
2931
if (d_table->isRowSelected(i)){
2937
r = abs(bottomRow - topRow) + 1;
2940
int aux = selectedCols;
2944
QTextDocument *document = new QTextDocument();
2945
QTextCursor cursor = QTextCursor(document);
2947
QTextTableFormat tableFormat;
2948
tableFormat.setAlignment(Qt::AlignCenter);
2949
tableFormat.setCellPadding(0);
2950
tableFormat.setHeaderRowCount(0);
2951
tableFormat.setBorder (1);
2952
tableFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
2953
tableFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100));
2962
QTextTable *table = cursor.insertTable(r, aux, tableFormat);
2963
QTextCharFormat cellFormat;
2964
cellFormat.setBackground (QBrush(Qt::gray));
2965
for (int i = 0; i < aux; i++){
2966
for (int j = 0; j < headerRows; j++){
2967
QTextTableCell cell = table->cellAt(j, i);
2968
cell.setFormat(cellFormat);
2973
QStringList header = colNames();
2974
QStringList ls = header.grep ( QRegExp ("\\D"));
2975
if (exportSelection && selectedCols){
2976
for (int i = 0; i < aux; i++){
2978
cursor.insertText(header[sCols[i]]);
2980
cursor.insertText("C" + header[sCols[i]]);
2981
cursor.movePosition(QTextCursor::NextCell);
2984
if (ls.count() > 0){
2985
for (int j = 0; j < aux; j++){
2986
cursor.insertText(header[j]);
2987
cursor.movePosition(QTextCursor::NextCell);
2990
for (int j = 0; j < aux; j++){
2991
cursor.insertText("C" + header[j]);
2992
cursor.movePosition(QTextCursor::NextCell);
2996
}// finished writting labels
2998
if (exportComments){
2999
if (exportSelection && selectedCols){
3000
for (int i = 0; i < aux; i++){
3001
cursor.insertText(comments[sCols[i]]);
3002
cursor.movePosition(QTextCursor::NextCell);
3005
for (int i = 0; i < aux; i++){
3006
cursor.insertText(comments[i]);
3007
cursor.movePosition(QTextCursor::NextCell);
3012
if (exportSelection && selectedCols){
3013
for (int i = topRow; i <= bottomRow; i++){
3014
for (int j = 0; j < aux; j++){
3015
cursor.insertText(d_table->text(i, sCols[j]));
3016
cursor.movePosition(QTextCursor::NextCell);
3021
for (int i = 0; i < rows; i++) {
3022
for (int j = 0; j < aux; j++){
3023
cursor.insertText(d_table->text(i, j));
3024
cursor.movePosition(QTextCursor::NextCell);
3029
QTextDocumentWriter writer(fname);
3030
if (fname.endsWith(".html"))
3031
writer.setFormat("HTML");
3032
writer.write(document);
3034
QApplication::restoreOverrideCursor();
3039
3039
bool Table::exportASCII(const QString& fname, const QString& separator,
3040
3040
bool withLabels, bool exportComments, bool exportSelection)
3042
if (!applicationWindow())
3045
if (fname.endsWith(".tex")){
3046
ImportExportPlugin *ep = applicationWindow()->exportPlugin("tex");
3049
return ep->exportTable(this, fname, withLabels, exportComments, exportSelection);
3050
} else if (fname.endsWith(".csv")){
3051
ImportExportPlugin *ep = applicationWindow()->exportPlugin("csv");
3054
return ep->exportTable(this, fname, withLabels, exportComments, exportSelection);
3042
3057
QFile f(fname);
3043
3058
if ( !f.open( QIODevice::WriteOnly ) ){
3044
3059
QMessageBox::critical(0, tr("QtiPlot - ASCII Export Error"),
3045
tr("Could not write to file: <br><h4>"+fname+ "</h4><p>Please verify that you have the right to write to this location!").arg(fname));
3060
tr("Could not write to file: <br><h4>" + fname +
3061
"</h4><p>Please verify that you have the right to write to this location!").arg(fname));
3049
3065
if (fname.endsWith(".odf") || fname.endsWith(".html")){
3051
3067
return exportODF(fname, withLabels, exportComments, exportSelection);
3054
else if (fname.endsWith(".xls")){
3068
} else if (fname.endsWith(".xls")){
3056
3070
return exportExcel(fname, withLabels, exportComments, exportSelection);
3071
} else if (fname.endsWith(".ods")){
3073
return exportOdsSpreadsheet(fname, withLabels, exportComments, exportSelection);
3060
3076
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
3062
3078
QTextStream t( &f );
3063
3079
QString eol = applicationWindow()->endOfLine();
3064
3080
QString sep = separator;
3065
bool exportTex = (fname.endsWith(".tex")) ? true : false;
3067
t << "\\begin{tabular}{|";
3068
eol = "\\\\\\hline\n";
3072
3082
int rows = d_table->numRows();
3073
3083
int cols = d_table->numCols();