~showard314/ubuntu/utopic/qtiplot/utopic_1311721

« back to all changes in this revision

Viewing changes to qtiplot/src/table/Table.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Howard
  • Date: 2011-05-07 17:00:26 UTC
  • mfrom: (1.1.9 upstream) (2.1.10 sid)
  • Revision ID: james.westby@ubuntu.com-20110507170026-77wvcv8uc6955fff
* moved debian/build.conf into debian/patches/03_build_conf.patch
  to track changes to build.conf between releases. Updated
  debian/rules accordingly.
* New upstream release. (Closes: #599450)
* Refreshed patches.
* debian/control B-D on libalglib-dev, libtamuanova-dev,
  libqtexengine-dev
* debian/rules allows for parallel builds
* Byte compile python modules with dh_python2, removed debian/*.post{rm,inst}
  dropped dependency on depricated python-central (Closes: #587669)
* Added shared-mime-info xml data in debian/qtiplot.sharedmimeinfo
  (LP: #184307)
* 04_qwtplot3d_static.patch added to build a modified static
  library of qwt3dplot
* 05_link_gl2ps.patch added to use Debian gl2ps library.
* Policy 3.9.2, no changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#include <ImportASCIIDialog.h>
33
33
#include <muParserScript.h>
34
34
#include <ApplicationWindow.h>
 
35
#include <ExcelFileConverter.h>
 
36
#include <ImportExportPlugin.h>
35
37
 
36
38
#include <QMessageBox>
37
39
#include <QDateTime>
47
49
#include <QProgressDialog>
48
50
#include <QFile>
49
51
#include <QRegion>
 
52
#if QT_VERSION >= 0x040500
50
53
#include <QTextDocumentWriter>
 
54
#endif
51
55
#include <QTextTable>
52
56
 
53
57
#include <q3paintdevicemetrics.h>
59
63
#include <gsl/gsl_sort.h>
60
64
#include <gsl/gsl_sort_vector.h>
61
65
#include <gsl/gsl_heapsort.h>
62
 
 
63
 
#ifdef XLS_IMPORT
64
 
    #include <ExcelFormat.h>
65
 
    using namespace ExcelFormat;
66
 
#endif
 
66
#include <gsl/gsl_rng.h>
 
67
#include <gsl/gsl_randist.h>
67
68
 
68
69
Table::Table(ScriptingEnv *env, int r, int c, const QString& label, ApplicationWindow* parent, const QString& name, Qt::WFlags f)
69
70
: MdiSubWindow(label,parent,name,f), scripted(env)
372
373
void Table::setPlotDesignation(PlotDesignation pd, bool rightColumns)
373
374
{
374
375
        if (rightColumns){
375
 
             int cols = d_table->numCols();
376
 
             for (int i = selectedCol; i<cols; i++){
377
 
            col_plot_type[i] = pd;
378
 
            if (pd == Label)
379
 
                colTypes[i] = Text;
380
 
            else if (pd != None)
381
 
                colTypes[i] = Numeric;
382
 
        }
 
376
                 int cols = d_table->numCols();
 
377
                 for (int i = selectedCol; i<cols; i++){
 
378
                        col_plot_type[i] = pd;
 
379
                        if (pd == Table::Label)
 
380
                                colTypes[i] = Text;
 
381
                }
383
382
        } else {
384
 
        QStringList list = selectedColumns();
385
 
        for (int i=0; i<(int) list.count(); i++){
386
 
            int col = colIndex(list[i]);
387
 
            col_plot_type[col] = pd;
388
 
            if (pd == Label)
389
 
                colTypes[col] = Text;
390
 
            else if (pd != None)
391
 
                colTypes[col] = Numeric;
392
 
        }
 
383
                QStringList list = selectedColumns();
 
384
                for (int i = 0; i < list.count(); i++){
 
385
                        int col = colIndex(list[i]);
 
386
                        col_plot_type[col] = pd;
 
387
                        if (pd == Table::Label)
 
388
                                colTypes[col] = Text;
 
389
                }
393
390
        }
394
391
 
395
392
        setHeaderColType();
399
396
void Table::setColPlotDesignation(int col, PlotDesignation pd)
400
397
{
401
398
        if (col < 0 || col >= d_table->numCols() || col_plot_type[col] == pd)
402
 
        return;
 
399
                return;
403
400
 
404
401
        col_plot_type[col] = pd;
405
 
        if (pd == Label)
406
 
        colTypes[col] = Text;
407
402
}
408
403
 
409
404
void Table::columnNumericFormat(int col, int *f, int *precision)
559
554
    char f;
560
555
    columnNumericFormat(col, &f, &prec);
561
556
 
 
557
        setAutoUpdateValues(false);
 
558
 
562
559
    if (mup->codeLines() == 1){
563
560
        for (int i = startRow; i <= endRow; i++){
564
561
            *r = i + 1.0;
565
 
            d_table->setText(i, col, mup->evalSingleLineToString(loc, f, prec));
 
562
                        d_table->setText(i, col, mup->evalSingleLineToString(loc, f, prec));
566
563
        }
567
564
        } else {
568
565
        QVariant ret;
582
579
        if (notifyChanges)
583
580
        emit modifiedData(this, colName(col));
584
581
        emit modifiedWindow(this);
 
582
 
 
583
        setAutoUpdateValues(applicationWindow()->autoUpdateTableValues());
585
584
        QApplication::restoreOverrideCursor();
586
585
        return true;
587
586
}
606
605
                resizeRows(endRow + 1);
607
606
 
608
607
        QString cmd = commands[col];
609
 
        if (cmd.isEmpty() || colTypes[col] != Numeric){
610
 
                for (int i=startRow; i<=endRow; i++)
 
608
        if (cmd.isEmpty()){
 
609
                for (int i = startRow; i <= endRow; i++)
611
610
                        d_table->setText(i, col, cmd);
612
 
        if (notifyChanges)
613
 
            emit modifiedData(this, colName(col));
614
 
        emit modifiedWindow(this);
615
 
        return true;
 
611
                if (notifyChanges)
 
612
                        emit modifiedData(this, colName(col));
 
613
                emit modifiedWindow(this);
 
614
                return true;
616
615
        }
617
616
 
618
617
        QApplication::setOverrideCursor(Qt::WaitCursor);
631
630
    char f;
632
631
    columnNumericFormat(col, &f, &prec);
633
632
 
 
633
        setAutoUpdateValues(false);
 
634
 
634
635
        colscript->setDouble(col + 1.0, "j");
635
636
        colscript->setDouble(startRow + 1.0, "sr");
636
637
        colscript->setDouble(endRow + 1.0, "er");
648
649
                }
649
650
        }
650
651
        if (notifyChanges)
651
 
        emit modifiedData(this, colName(col));
 
652
                emit modifiedData(this, colName(col));
652
653
        emit modifiedWindow(this);
 
654
 
 
655
        setAutoUpdateValues(applicationWindow()->autoUpdateTableValues());
 
656
 
653
657
        QApplication::restoreOverrideCursor();
654
658
        return true;
655
659
}
871
875
                                t << QString::number(i) + "\t";
872
876
                                for (int j=0; j<cols; j++){
873
877
                                if (colTypes[j] == Numeric && !d_table->text(i, j).isEmpty())
874
 
                        t << QString::number(cell(i, j), 'e', 14) + "\t";
 
878
                                                t << QString::number(cell(i, j), 'g', 14) + "\t";
875
879
                                        else
876
880
                                                t << d_table->text(i, j) + "\t";
877
881
                                }
878
882
                if (colTypes[cols] == Numeric && !d_table->text(i, cols).isEmpty())
879
 
                        t << QString::number(cell(i, cols), 'e', 14) + "\n";
 
883
                                        t << QString::number(cell(i, cols), 'g', 14) + "\n";
880
884
                                else
881
885
                                        t << d_table->text(i, cols) + "\n";
882
886
                        }
897
901
        return xcol;
898
902
}
899
903
 
 
904
QString Table::colLabel(int col)
 
905
{
 
906
        if (col < 0 || col >= d_table->numCols())
 
907
                return QString::null;
 
908
 
 
909
        return col_label[col];
 
910
}
 
911
 
900
912
QString Table::comment(int col)
901
913
{
902
914
        if (col < 0 || col >= d_table->numCols())
917
929
 
918
930
        if (d_show_comments)
919
931
                setHeaderColType();
 
932
 
 
933
        emit modifiedWindow(this);
920
934
}
921
935
 
922
936
void Table::setColumnWidth(int width, bool allCols)
939
953
        }
940
954
}
941
955
 
942
 
void Table::adjustColumnsWidth()
 
956
void Table::adjustColumnsWidth(bool selection)
943
957
{
944
958
        int cols = d_table->numCols();
945
 
        for (int i = 0; i < cols; i++){
946
 
                if(d_table->isColumnSelected (i, true))
 
959
 
 
960
        if (selection){
 
961
                for (int i = 0; i < cols; i++){
 
962
                        if(d_table->isColumnSelected (i, true))
 
963
                                d_table->adjustColumn(i);
 
964
                }
 
965
        } else {
 
966
                for (int i = 0; i < cols; i++)
947
967
                        d_table->adjustColumn(i);
948
968
        }
949
969
 
1250
1270
        col_plot_type << pd;
1251
1271
 
1252
1272
        setHeaderColType();
 
1273
        emit addedCol(objectName() + "_" + QString::number(max+1));
1253
1274
        emit modifiedWindow(this);
1254
1275
}
1255
1276
 
1270
1291
                commands << "";
1271
1292
                colTypes << Numeric;
1272
1293
                col_format << "0/" + QString::number(d_numeric_precision);
1273
 
                col_label << QString::number(max+i);
 
1294
                QString label = QString::number(max + i);
 
1295
                col_label << label;
1274
1296
                col_plot_type << Y;
 
1297
                emit addedCol(objectName() + "_" + label);
1275
1298
        }
1276
1299
}
1277
1300
 
1280
1303
        if (d_table->isColumnReadOnly(selectedCol))
1281
1304
                return;
1282
1305
 
1283
 
        for (int i=0; i<d_table->numRows(); i++){
1284
 
                if (d_table->isSelected (i, selectedCol))
 
1306
        for (int i = 0; i < d_table->numRows(); i++){
 
1307
                if (d_table->isSelected(i, selectedCol))
1285
1308
                        d_table->setText(i, selectedCol, "");
1286
1309
        }
1287
1310
 
1357
1380
 
1358
1381
void Table::clearSelection()
1359
1382
{
1360
 
        QStringList list=selectedColumns();
 
1383
        QStringList list = selectedColumns();
1361
1384
        int n = int(list.count());
1362
1385
 
1363
 
        if (n>0){
 
1386
        if (n > 0){
1364
1387
                QStringList lstReadOnly;
1365
1388
                for (int i=0; i<list.count(); i++){
1366
1389
                        QString name = list[i];
1372
1395
                        QMessageBox::warning(this, tr("QtiPlot - Error"),
1373
1396
                tr("The folowing columns")+":\n"+ lstReadOnly.join("\n") + "\n"+ tr("are read only!"));
1374
1397
        }
1375
 
 
1376
 
                for (int i=0; i<n; i++){
1377
 
                        QString name = list[i];
1378
 
                        selectedCol = colIndex(name);
 
1398
                for (int i = 0; i < n; i++){
 
1399
                        selectedCol = colIndex(list[i]);
1379
1400
                        clearCol();
1380
1401
                }
1381
1402
        } else {
1451
1472
                Q3TableSelection sel = d_table->selection(0);
1452
1473
                int right = sel.rightCol();
1453
1474
                int bottom = sel.bottomRow();
1454
 
                for (int i = sel.topRow(); i<bottom; i++){
 
1475
                if (right < 0 || bottom < 0)
 
1476
                        text = d_table->text(d_table->currentRow(), d_table->currentColumn());
 
1477
                else {
 
1478
                        for (int i = sel.topRow(); i<bottom; i++){
 
1479
                                for (int j = sel.leftCol(); j<right; j++)
 
1480
                                        text += d_table->text(i, j) + "\t";
 
1481
                                text += d_table->text(i, right) + eol;
 
1482
                        }
1455
1483
                        for (int j = sel.leftCol(); j<right; j++)
1456
 
                                text += d_table->text(i, j) + "\t";
1457
 
                        text += d_table->text(i, right) + eol;
 
1484
                                        text += d_table->text(bottom, j) + "\t";
 
1485
                                text += d_table->text(bottom, right);
1458
1486
                }
1459
 
                for (int j = sel.leftCol(); j<right; j++)
1460
 
                                text += d_table->text(bottom, j) + "\t";
1461
 
                        text += d_table->text(bottom, right);
1462
1487
        }
1463
1488
 
1464
1489
        // Copy text into the clipboard
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);
1497
 
                if (!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)){
1501
 
                                case 0:
1502
 
                                break;
1503
 
                                case 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);
 
1523
                        if (!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);
 
1533
 
 
1534
                                if (msgBox.exec() == QMessageBox::Cancel)
 
1535
                                        return;
 
1536
 
 
1537
                                if (msgBox.clickedButton() == namesButton){
1504
1538
                                        pasteHeader = true;
1505
1539
                                        linesList.pop_front();
1506
1540
                                        rows--;
1507
 
                                break;
1508
 
                                case 2:
 
1541
                                } else if (msgBox.clickedButton() == commentsButton){
1509
1542
                                        pasteComments = true;
1510
1543
                                        linesList.pop_front();
1511
1544
                                        rows--;
 
1545
                                }
 
1546
 
1512
1547
                                break;
1513
1548
                        }
1514
 
                        break;
1515
1549
                }
1516
1550
        }
1517
1551
 
1545
1579
        }
1546
1580
        if (lstReadOnly.count() > 0){
1547
1581
                QMessageBox::warning(this, tr("QtiPlot - Error"),
1548
 
        tr("The folowing columns")+":\n"+ lstReadOnly.join("\n") + "\n"+ tr("are read only!"));
 
1582
                tr("The folowing columns") + ":\n" + lstReadOnly.join("\n") + "\n" + tr("are read only!"));
1549
1583
    }
1550
1584
 
1551
1585
        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
1552
1586
 
1553
1587
        QStringList colLabels;
1554
 
        for (int i = 0; i < left & i < col_label.size(); i++)
 
1588
        for (int i = 0; i < left && i < col_label.size(); i++)
1555
1589
                colLabels << col_label[i];
1556
1590
        for (int i = left + cols; i < col_label.size(); i++)
1557
1591
                colLabels << col_label[i];
1674
1708
 
1675
1709
void Table::normalizeSelection()
1676
1710
{
1677
 
        QStringList s = selectedColumns();
1678
 
        QStringList lstReadOnly;
1679
 
        for (int i=0; i<(int)s.count(); i++){
1680
 
                int col = colIndex(s[i]);
1681
 
                if (d_table->isColumnReadOnly(col))
1682
 
                        lstReadOnly << col_label[col];
1683
 
        }
1684
 
 
1685
 
        if (lstReadOnly.count() > 0){
1686
 
                QMessageBox::warning(this, tr("QtiPlot - Error"),
1687
 
        tr("The folowing columns")+":\n"+ lstReadOnly.join("\n") + "\n"+ tr("are read only!"));
1688
 
    }
1689
 
 
1690
 
        for (int i=0; i<(int)s.count(); i++)
1691
 
                normalizeCol(colIndex(s[i]));
 
1711
        QStringList lst = writableSelectedColumns();
 
1712
        if (lst.isEmpty())
 
1713
                return;
 
1714
 
 
1715
        for (int i = 0; i < lst.count(); i++)
 
1716
                normalizeCol(colIndex(lst[i]));
1692
1717
 
1693
1718
        emit modifiedWindow(this);
1694
1719
}
1706
1731
        tr("The folowing columns")+":\n"+ lstReadOnly.join("\n") + "\n"+ tr("are read only!"));
1707
1732
    }
1708
1733
 
1709
 
        for (int i=0; i<d_table->numCols(); i++)
 
1734
        for (int i = 0; i < d_table->numCols(); i++)
1710
1735
                normalizeCol(i);
1711
1736
 
1712
1737
        emit modifiedWindow(this);
2046
2071
 
2047
2072
void Table::saveToMemory()
2048
2073
{
2049
 
    int rows = d_table->numRows();
2050
 
    int cols = d_table->numCols();
 
2074
        int rows = d_table->numRows();
 
2075
        int cols = d_table->numCols();
2051
2076
 
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];
2055
2080
 
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;}
2059
2084
 
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);
2066
 
                                }
2067
 
            }
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);
 
2093
                        }
 
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;
2073
 
                                }
2074
 
            }
2075
 
        }
 
2099
                        }
 
2100
                }
 
2101
        }
2076
2102
 
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);
2082
 
                if (!s.isEmpty())
2083
 
                    d_saved_cells[col][row] = l.toDouble(s);
2084
 
            }
2085
 
            }
 
2105
                if (colTypes[col] == Numeric){
 
2106
                        for (int row = 0; row < rows; row++){
 
2107
                                QString s = d_table->text(row, col);
 
2108
                                if (!s.isEmpty())
 
2109
                                        d_saved_cells[col][row] = l.toDouble(s);
 
2110
                        }
 
2111
                }
2086
2112
        }
2087
2113
}
2088
2114
 
2097
2123
 
2098
2124
void Table::setTextFormat(int col)
2099
2125
{
2100
 
        if (col >= 0 && col < colTypes.count())
 
2126
        if (col >= 0 && col < colTypes.count() && colTypes[col] != Text)
2101
2127
                colTypes[col] = Text;
2102
2128
}
2103
2129
 
 
2130
void Table::setColNumericFormat(int col)
 
2131
{
 
2132
        if (colTypes[col] == Numeric || (col < 0 && col >= colTypes.count()))
 
2133
                return;
 
2134
 
 
2135
        colTypes[col] = Numeric;
 
2136
}
 
2137
 
2104
2138
void Table::setColNumericFormat(int f, int prec, int col, bool updateCells)
2105
2139
{
2106
2140
        if (prec < 0)
2113
2147
                columnNumericFormat(col, &old_f, &old_prec);
2114
2148
                if (old_f == f && old_prec == prec)
2115
2149
                        return;
2116
 
        }
 
2150
        } else if (!updateCells)
 
2151
                emit modifiedData(this, colName(col));
2117
2152
 
2118
2153
        colTypes[col] = Numeric;
2119
2154
        col_format[col] = QString::number(f) + "/" + QString::number(prec);
2148
2183
        col_format = lst;
2149
2184
}
2150
2185
 
 
2186
QDateTime Table::dateTime(double val)
 
2187
{
 
2188
        QDateTime d = QDateTime(QDate::fromJulianDay((int)val + 1));
 
2189
        double msecs = (val - floor(val))*864e5;
 
2190
        d.setTime(d.time().addMSecs(qRound(msecs)));
 
2191
 
 
2192
        return d;
 
2193
}
 
2194
 
2151
2195
bool Table::setDateFormat(const QString& format, int col, bool updateCells)
2152
2196
{
2153
2197
        if (colTypes[col] == Date && col_format[col] == format)
2167
2211
                                }
2168
2212
 
2169
2213
                                if (d_saved_cells){
2170
 
                                        d = QDateTime(QDate::fromJulianDay((int)d_saved_cells[col][i] + 1));
2171
 
                                        double msecs = (d_saved_cells[col][i] - floor(d_saved_cells[col][i]))*864e5;
2172
 
                                        d.setTime(d.time().addMSecs(qRound(msecs)));
 
2214
                                        d = dateTime(d_saved_cells[col][i]);
2173
2215
                                        if (d.isValid())
2174
2216
                                                d_table->setText(i, col, d.toString(format));
2175
2217
                                }
2185
2227
                        d_saved_cells[col][i] = dt.date().toJulianDay() - 1 + (double)ref.msecsTo(dt.time())/864.0e5;
2186
2228
                }
2187
2229
        }
 
2230
        emit modifiedData(this, colName(col));
2188
2231
        return true;
2189
2232
}
2190
2233
 
2227
2270
                        d_saved_cells[col][i] = ref.msecsTo(t);
2228
2271
                }
2229
2272
        }
 
2273
        emit modifiedData(this, colName(col));
2230
2274
        return true;
2231
2275
}
2232
2276
 
2241
2285
        if (!updateCells)
2242
2286
        return;
2243
2287
 
2244
 
    for (int i=0; i<numRows(); i++){
 
2288
        for (int i = 0; i < numRows(); i++){
2245
2289
        QString t = d_table->text(i,col);
2246
2290
        if (!t.isEmpty()){
2247
2291
            int day;
2260
2304
                d_table->setText(i, col, QDate::longMonthName(day));
2261
2305
        }
2262
2306
    }
 
2307
        emit modifiedData(this, colName(col));
2263
2308
}
2264
2309
 
2265
2310
void Table::setDayFormat(const QString& format, int col, bool updateCells)
2273
2318
        if (!updateCells)
2274
2319
        return;
2275
2320
 
2276
 
        for (int i=0; i<numRows(); i++){
 
2321
        for (int i = 0; i < numRows(); i++){
2277
2322
        QString t = d_table->text(i,col);
2278
2323
        if (!t.isEmpty()){
2279
2324
            int day;
2292
2337
                d_table->setText(i, col, QDate::longDayName(day));
2293
2338
        }
2294
2339
    }
 
2340
        emit modifiedData(this, colName(col));
2295
2341
}
2296
2342
 
2297
2343
void Table::setRandomValues()
2298
2344
{
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;
2306
 
        }
2307
 
 
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!"));
2311
 
    }
2312
 
 
2313
 
        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
2314
 
 
2315
 
    time_t tmp;
2316
 
    srand(time(&tmp));
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))
2322
 
            continue;
2323
 
 
2324
 
                int prec;
2325
 
                char f;
2326
 
                columnNumericFormat(selectedCol, &f, &prec);
2327
 
 
2328
 
                for (int i=0; i<rows; i++)
2329
 
                        d_table->setText(i, selectedCol, locale().toString(double(rand())/double(RAND_MAX), f, prec));
2330
 
 
2331
 
                emit modifiedData(this, name);
2332
 
        }
2333
 
 
2334
 
        emit modifiedWindow(this);
 
2345
        QStringList list = writableSelectedColumns();
 
2346
        if (list.isEmpty())
 
2347
                return;
 
2348
 
 
2349
        Q3TableSelection selection = getSelection();
 
2350
        for (int i = 0; i < list.count(); i++)
 
2351
                setRandomValues(colIndex(list[i]), selection.topRow(), selection.bottomRow());
 
2352
 
 
2353
        emit modifiedWindow(this);
 
2354
}
 
2355
 
 
2356
void Table::setRandomValues(int col, int startRow, int endRow)
 
2357
{
 
2358
        if (col < 0 || col >= d_table->numCols() || d_table->isColumnReadOnly(col))
 
2359
                return;
 
2360
 
 
2361
        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
 
2362
 
 
2363
        int rows = d_table->numRows();
 
2364
        if (startRow < 0 || startRow >= rows)
 
2365
                startRow = 0;
 
2366
        if (endRow < 0 || endRow >= rows)
 
2367
                endRow = rows - 1;
 
2368
 
 
2369
        int prec;
 
2370
        char f;
 
2371
        columnNumericFormat(col, &f, &prec);
 
2372
 
 
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));
 
2376
 
 
2377
        emit modifiedData(this, colName(col));
 
2378
 
 
2379
        QApplication::restoreOverrideCursor();
 
2380
}
 
2381
 
 
2382
void Table::setNormalRandomValues()
 
2383
{
 
2384
        QStringList list = writableSelectedColumns();
 
2385
        if (list.isEmpty())
 
2386
                return;
 
2387
 
 
2388
        Q3TableSelection selection = getSelection();
 
2389
        for (int i = 0; i < list.count(); i++)
 
2390
                setNormalRandomValues(colIndex(list[i]), selection.topRow(), selection.bottomRow());
 
2391
 
 
2392
        emit modifiedWindow(this);
 
2393
}
 
2394
 
 
2395
void Table::setNormalRandomValues(int col, int startRow, int endRow, double sigma)
 
2396
{
 
2397
        if (col < 0 || col >= d_table->numCols() || d_table->isColumnReadOnly(col))
 
2398
                return;
 
2399
 
 
2400
        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
 
2401
 
 
2402
        int rows = d_table->numRows();
 
2403
        if (startRow < 0 || startRow >= rows)
 
2404
                startRow = 0;
 
2405
        if (endRow < 0 || endRow >= rows)
 
2406
                endRow = rows - 1;
 
2407
 
 
2408
        gsl_rng_env_setup();
 
2409
        const gsl_rng_type * T = gsl_rng_default;
 
2410
        gsl_rng * r = gsl_rng_alloc (T);
 
2411
        if (!r)
 
2412
                return;
 
2413
 
 
2414
        int prec;
 
2415
        char f;
 
2416
        columnNumericFormat(col, &f, &prec);
 
2417
 
 
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));
 
2421
        gsl_rng_free (r);
 
2422
 
 
2423
        emit modifiedData(this, colName(col));
2335
2424
        QApplication::restoreOverrideCursor();
2336
2425
}
2337
2426
 
2392
2481
 
2393
2482
int Table::colIndex(const QString& name)
2394
2483
{
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);
2398
2487
}
2399
2488
 
2400
2489
void Table::setHeaderColType()
2401
2490
{
2402
2491
        int xcols = 0;
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)
2405
2494
                        xcols++;
2406
2495
        }
2407
2496
 
2408
2497
        if (xcols > 1){
2409
2498
                xcols = 0;
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){
2414
 
                                if(xcols>0)
2415
 
                                        setColumnHeader(i, col_label[i]+"[Y"+ QString::number(xcols) +"]");
2416
 
                                else
2417
 
                                        setColumnHeader(i, col_label[i]+"[Y]");
2418
 
                        } else if (col_plot_type[i] == Z){
2419
 
                                if(xcols>0)
2420
 
                                        setColumnHeader(i, col_label[i]+"[Z"+ QString::number(xcols) +"]");
2421
 
                                else
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]");
2429
 
                        else
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())
 
2501
                                continue;
 
2502
                        int colType = col_plot_type[i];
 
2503
                        QString label = col_label[i];
 
2504
                        switch(colType){
 
2505
                                case X:
 
2506
                                        setColumnHeader(i, label + "[X" + QString::number(++xcols) + "]");
 
2507
                                break;
 
2508
                                case Y:
 
2509
                                        if (xcols > 0)
 
2510
                                                setColumnHeader(i, label + "[Y" + QString::number(xcols) + "]");
 
2511
                                        else
 
2512
                                                setColumnHeader(i, label + "[Y]");
 
2513
                                break;
 
2514
                                case Z:
 
2515
                                        if (xcols > 0)
 
2516
                                                setColumnHeader(i, label + "[Z" + QString::number(xcols) + "]");
 
2517
                                        else
 
2518
                                                setColumnHeader(i, label + "[Z]");
 
2519
                                break;
 
2520
                                case xErr:
 
2521
                                        setColumnHeader(i, label + "[xEr]");
 
2522
                                break;
 
2523
                                case yErr:
 
2524
                                        setColumnHeader(i, label + "[yEr]");
 
2525
                                break;
 
2526
                                case Label:
 
2527
                                        setColumnHeader(i, label + "[L]");
 
2528
                                break;
 
2529
                                default:
 
2530
                                        setColumnHeader(i, label);
 
2531
                                break;
 
2532
                        }
2431
2533
                }
2432
2534
        } else {
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]");
2446
 
                        else
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())
 
2537
                                continue;
 
2538
                        int colType = col_plot_type[i];
 
2539
                        QString label = col_label[i];
 
2540
                        switch(colType){
 
2541
                                case X:
 
2542
                                        setColumnHeader(i, label + "[X]");
 
2543
                                break;
 
2544
                                case Y:
 
2545
                                        setColumnHeader(i, label + "[Y]");
 
2546
                                break;
 
2547
                                case Z:
 
2548
                                        setColumnHeader(i, label + "[Z]");
 
2549
                                break;
 
2550
                                case xErr:
 
2551
                                        setColumnHeader(i, label + "[xEr]");
 
2552
                                break;
 
2553
                                case yErr:
 
2554
                                        setColumnHeader(i, label + "[yEr]");
 
2555
                                break;
 
2556
                                case Label:
 
2557
                                        setColumnHeader(i, label + "[L]");
 
2558
                                break;
 
2559
                                default:
 
2560
                                        setColumnHeader(i, label);
 
2561
                                break;
 
2562
                        }
2448
2563
                }
2449
2564
        }
2450
2565
}
2451
2566
 
 
2567
QStringList Table::writableSelectedColumns()
 
2568
{
 
2569
        Q3TableSelection selection = getSelection();
 
2570
        QStringList list = selectedColumns();
 
2571
        if (list.isEmpty()){
 
2572
                for (int i = selection.leftCol(); i <= selection.rightCol(); i++)
 
2573
                        list << colName(i);
 
2574
        }
 
2575
 
 
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;
 
2582
        }
 
2583
 
 
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!"));
 
2587
        }
 
2588
        return list;
 
2589
}
 
2590
 
2452
2591
void Table::setAscValues()
2453
2592
{
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;
2461
 
        }
2462
 
 
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!"));
2466
 
    }
 
2593
        QStringList list = writableSelectedColumns();
 
2594
        if (list.isEmpty())
 
2595
                return;
2467
2596
 
2468
2597
        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
2469
 
        int rows = d_table->numRows();
2470
 
        for (int j=0; j<(int) list.count(); j++){
 
2598
 
 
2599
        Q3TableSelection selection = getSelection();
 
2600
        for (int j = 0; j < list.count(); j++){
2471
2601
                QString name = list[j];
2472
2602
                selectedCol = colIndex(name);
2473
2603
 
2483
2613
                char f;
2484
2614
                columnNumericFormat(selectedCol, &f, &prec);
2485
2615
 
2486
 
                for (int i=0; i<rows; i++)
2487
 
                        setText(i, selectedCol, QString::number(i+1, f, prec));
 
2616
                for (int i = selection.topRow(); i <= selection.bottomRow(); i++)
 
2617
                        setText(i, selectedCol, QString::number(i + 1, f, prec));
2488
2618
 
2489
2619
                emit modifiedData(this, name);
2490
2620
        }
2641
2771
                        s = s.simplifyWhiteSpace();
2642
2772
                else if (stripSpaces)
2643
2773
                        s = s.stripWhiteSpace();
2644
 
                line = s.split(sep, QString::SkipEmptyParts);
 
2774
                line = s.split(sep, QString::KeepEmptyParts);
2645
2775
                for (int i=0; i<line.size(); i++){
2646
2776
                        int aux = startCol + i;
2647
2777
                        if (aux < comments.size())
2739
2869
        }
2740
2870
}
2741
2871
 
2742
 
#ifdef XLS_IMPORT
 
2872
bool Table::exportOdsSpreadsheet(const QString& fname, bool withLabels, bool exportComments, bool exportSelection)
 
2873
{
 
2874
        QString name = fname;
 
2875
        QString ext = QFileInfo(fname).completeSuffix();
 
2876
        name.replace("." + ext, ".xls");
 
2877
        if (!exportExcel(name, withLabels, exportComments, exportSelection))
 
2878
                return false;
 
2879
 
 
2880
        QFile::remove(fname);
 
2881
        ExcelFileConverter(name, applicationWindow());
 
2882
        return true;
 
2883
}
 
2884
 
2743
2885
bool Table::exportExcel(const QString& fname, bool withLabels, bool exportComments, bool exportSelection)
2744
2886
{
 
2887
        if (!applicationWindow())
 
2888
                return false;
 
2889
        ImportExportPlugin *ep = applicationWindow()->exportPlugin("xls");
 
2890
        if (!ep)
 
2891
                return false;
 
2892
 
 
2893
        return ep->exportTable(this, fname, withLabels, exportComments, exportSelection);
 
2894
}
 
2895
 
 
2896
bool Table::exportODF(const QString& fname, bool withLabels, bool exportComments, bool exportSelection)
 
2897
{
 
2898
#if QT_VERSION >= 0x040500
2745
2899
        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
2746
2900
 
2747
2901
        int rows = d_table->numRows();
2749
2903
        int selectedCols = 0;
2750
2904
        int topRow = 0, bottomRow = 0;
2751
2905
        int *sCols = 0;
 
2906
        int r = rows;
2752
2907
        if (exportSelection){
2753
2908
                for (int i=0; i<cols; i++){
2754
2909
                        if (d_table->isColumnSelected(i))
2777
2932
                                break;
2778
2933
                        }
2779
2934
                }
 
2935
 
 
2936
                r = abs(bottomRow - topRow) + 1;
2780
2937
        }
2781
2938
 
2782
2939
        int aux = selectedCols;
2783
2940
        if (!selectedCols)
2784
 
                aux = cols;
2785
 
 
2786
 
        aux = QMIN(aux, 256);
 
2941
                        aux = cols;
 
2942
 
 
2943
        QTextDocument *document = new QTextDocument();
 
2944
        QTextCursor cursor = QTextCursor(document);
 
2945
 
 
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));
2787
2953
 
2788
2954
        int headerRows = 0;
2789
2955
        if (withLabels)
2790
2956
                headerRows++;
2791
2957
        if (exportComments)
2792
2958
                headerRows++;
2793
 
 
2794
 
        BasicExcel xls;
2795
 
        xls.New(1);
2796
 
        BasicExcelWorksheet* sheet = xls.GetWorksheet((size_t)0);
2797
 
 
2798
 
        XLSFormatManager fmt_mgr(xls);
2799
 
        ExcelFont font_bold;
2800
 
        font_bold._weight = 700;
2801
 
        CellFormat fmt_bold(fmt_mgr, font_bold);
 
2959
        r += headerRows;
 
2960
 
 
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);
2806
2968
                }
2807
2969
        }
2808
2970
 
2809
 
        int startRow = 0;
2810
2971
        if (withLabels){
2811
 
                startRow++;
2812
 
 
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]]);
2820
2978
                                else
2821
 
                                        cell->Set(("C" + header[sCols[i]]).toStdString().c_str());
 
2979
                                        cursor.insertText("C" + header[sCols[i]]);
 
2980
                                cursor.movePosition(QTextCursor::NextCell);
2822
2981
                        }
2823
2982
                } else {
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);
2828
2987
                                }
2829
2988
                        } else {
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);
2833
2992
                                }
2834
2993
                        }
2835
2994
                }
2838
2997
        if (exportComments){
2839
2998
                if (exportSelection && selectedCols){
2840
2999
                        for (int i = 0; i < aux; i++){
2841
 
                                QString s = comments[sCols[i]];
2842
 
                                if (s.isEmpty())
2843
 
                                        continue;
2844
 
                                sheet->Cell(startRow, i)->Set(s.toStdString().c_str());
 
3000
                                cursor.insertText(comments[sCols[i]]);
 
3001
                                cursor.movePosition(QTextCursor::NextCell);
2845
3002
                        }
2846
3003
                } else {
2847
3004
                        for (int i = 0; i < aux; i++){
2848
 
                                QString s = comments[i];
2849
 
                                if (s.isEmpty())
2850
 
                                        continue;
2851
 
                                sheet->Cell(startRow, i)->Set(s.toStdString().c_str());
 
3005
                                cursor.insertText(comments[i]);
 
3006
                                cursor.movePosition(QTextCursor::NextCell);
2852
3007
                        }
2853
3008
                }
2854
 
                startRow++;
2855
3009
        }
2856
3010
 
2857
3011
        if (exportSelection && selectedCols){
2858
3012
                for (int i = topRow; i <= bottomRow; i++){
2859
3013
                        for (int j = 0; j < aux; j++){
2860
 
                                int col = sCols[j];
2861
 
                                QString s = d_table->text(i, col);
2862
 
                                if (s.isEmpty())
2863
 
                                        continue;
2864
 
                                if (columnType(col) == Numeric)
2865
 
                                        sheet->Cell(startRow, j)->Set(cell(i, col));
2866
 
                                else
2867
 
                                        sheet->Cell(startRow, j)->Set(s.toStdString().c_str());
 
3014
                                cursor.insertText(d_table->text(i, sCols[j]));
 
3015
                                cursor.movePosition(QTextCursor::NextCell);
2868
3016
                        }
2869
 
                        startRow++;
2870
 
                        if (startRow > 65536)
2871
 
                                break;
2872
3017
                }
2873
3018
                delete [] sCols;
2874
3019
        } else {
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);
2878
 
                                if (s.isEmpty())
2879
 
                                        continue;
2880
 
                                if (columnType(j) == Numeric)
2881
 
                                        sheet->Cell(startRow, j)->Set(cell(i, j));
2882
 
                                else
2883
 
                                        sheet->Cell(startRow, j)->Set(s.toStdString().c_str());
 
3022
                                cursor.insertText(d_table->text(i, j));
 
3023
                                cursor.movePosition(QTextCursor::NextCell);
2884
3024
                        }
2885
 
                        startRow++;
2886
 
                        if (startRow > 65536)
2887
 
                                break;
2888
3025
                }
2889
3026
        }
2890
3027
 
2891
 
        xls.SaveAs(fname);
 
3028
        QTextDocumentWriter writer(fname);
 
3029
        if (fname.endsWith(".html"))
 
3030
                writer.setFormat("HTML");
 
3031
        writer.write(document);
2892
3032
 
2893
3033
        QApplication::restoreOverrideCursor();
2894
3034
        return true;
2895
 
}
2896
3035
#endif
2897
 
 
2898
 
bool Table::exportODF(const QString& fname, bool withLabels, bool exportComments, bool exportSelection)
2899
 
{
2900
 
        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
2901
 
 
2902
 
        int rows = d_table->numRows();
2903
 
        int cols = d_table->numCols();
2904
 
        int selectedCols = 0;
2905
 
        int topRow = 0, bottomRow = 0;
2906
 
        int *sCols = 0;
2907
 
        int r = rows;
2908
 
        if (exportSelection){
2909
 
                for (int i=0; i<cols; i++){
2910
 
                        if (d_table->isColumnSelected(i))
2911
 
                                selectedCols++;
2912
 
                }
2913
 
 
2914
 
                sCols = new int[selectedCols];
2915
 
                int aux = 0;
2916
 
                for (int i=0; i<cols; i++){
2917
 
                        if (d_table->isColumnSelected(i)){
2918
 
                                sCols[aux] = i;
2919
 
                                aux++;
2920
 
                        }
2921
 
                }
2922
 
 
2923
 
                for (int i=0; i<rows; i++){
2924
 
                        if (d_table->isRowSelected(i)){
2925
 
                                topRow = i;
2926
 
                                break;
2927
 
                        }
2928
 
                }
2929
 
 
2930
 
                for (int i = rows - 1; i > 0; i--){
2931
 
                        if (d_table->isRowSelected(i)){
2932
 
                                bottomRow = i;
2933
 
                                break;
2934
 
                        }
2935
 
                }
2936
 
 
2937
 
                r = abs(bottomRow - topRow) + 1;
2938
 
        }
2939
 
 
2940
 
        int aux = selectedCols;
2941
 
        if (!selectedCols)
2942
 
                aux = cols;
2943
 
 
2944
 
        QTextDocument *document = new QTextDocument();
2945
 
        QTextCursor cursor = QTextCursor(document);
2946
 
 
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));
2954
 
 
2955
 
        int headerRows = 0;
2956
 
        if (withLabels)
2957
 
                headerRows++;
2958
 
        if (exportComments)
2959
 
                headerRows++;
2960
 
        r += headerRows;
2961
 
 
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);
2969
 
                }
2970
 
        }
2971
 
 
2972
 
        if (withLabels){
2973
 
                QStringList header = colNames();
2974
 
                QStringList ls = header.grep ( QRegExp ("\\D"));
2975
 
                if (exportSelection && selectedCols){
2976
 
                        for (int i = 0; i < aux; i++){
2977
 
                                if (ls.count()>0)
2978
 
                                        cursor.insertText(header[sCols[i]]);
2979
 
                                else
2980
 
                                        cursor.insertText("C" + header[sCols[i]]);
2981
 
                                cursor.movePosition(QTextCursor::NextCell);
2982
 
                        }
2983
 
                } else {
2984
 
                        if (ls.count() > 0){
2985
 
                                for (int j = 0; j < aux; j++){
2986
 
                                        cursor.insertText(header[j]);
2987
 
                                        cursor.movePosition(QTextCursor::NextCell);
2988
 
                                }
2989
 
                        } else {
2990
 
                                for (int j = 0; j < aux; j++){
2991
 
                                        cursor.insertText("C" + header[j]);
2992
 
                                        cursor.movePosition(QTextCursor::NextCell);
2993
 
                                }
2994
 
                        }
2995
 
                }
2996
 
        }// finished writting labels
2997
 
 
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);
3003
 
                        }
3004
 
                } else {
3005
 
                    for (int i = 0; i < aux; i++){
3006
 
                        cursor.insertText(comments[i]);
3007
 
                        cursor.movePosition(QTextCursor::NextCell);
3008
 
                    }
3009
 
        }
3010
 
        }
3011
 
 
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);
3017
 
                        }
3018
 
                }
3019
 
                delete [] sCols;
3020
 
        } else {
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);
3025
 
                        }
3026
 
                }
3027
 
        }
3028
 
 
3029
 
        QTextDocumentWriter writer(fname);
3030
 
        if (fname.endsWith(".html"))
3031
 
                writer.setFormat("HTML");
3032
 
        writer.write(document);
3033
 
 
3034
 
        QApplication::restoreOverrideCursor();
3035
 
        return true;
 
3036
        return false;
3036
3037
}
3037
3038
 
3038
 
 
3039
3039
bool Table::exportASCII(const QString& fname, const QString& separator,
3040
3040
                bool withLabels, bool exportComments, bool exportSelection)
3041
3041
{
 
3042
        if (!applicationWindow())
 
3043
                return false;
 
3044
 
 
3045
        if (fname.endsWith(".tex")){
 
3046
                ImportExportPlugin *ep = applicationWindow()->exportPlugin("tex");
 
3047
                if (!ep)
 
3048
                        return false;
 
3049
                return ep->exportTable(this, fname, withLabels, exportComments, exportSelection);
 
3050
        } else if (fname.endsWith(".csv")){
 
3051
                ImportExportPlugin *ep = applicationWindow()->exportPlugin("csv");
 
3052
                if (!ep)
 
3053
                        return false;
 
3054
                return ep->exportTable(this, fname, withLabels, exportComments, exportSelection);
 
3055
        }
 
3056
 
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));
3046
3062
                return false;
3047
3063
        }
3048
3064
 
3049
3065
        if (fname.endsWith(".odf") || fname.endsWith(".html")){
3050
3066
                f.close();
3051
3067
                return exportODF(fname, withLabels, exportComments, exportSelection);
3052
 
        }
3053
 
#ifdef XLS_IMPORT
3054
 
        else if (fname.endsWith(".xls")){
 
3068
        } else if (fname.endsWith(".xls")){
3055
3069
                f.close();
3056
3070
                return exportExcel(fname, withLabels, exportComments, exportSelection);
 
3071
        } else if (fname.endsWith(".ods")){
 
3072
                f.close();
 
3073
                return exportOdsSpreadsheet(fname, withLabels, exportComments, exportSelection);
3057
3074
        }
3058
 
#endif
3059
3075
 
3060
3076
        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
3061
3077
 
3062
3078
        QTextStream t( &f );
3063
3079
        QString eol = applicationWindow()->endOfLine();
3064
3080
        QString sep = separator;
3065
 
        bool exportTex = (fname.endsWith(".tex")) ? true : false;
3066
 
        if (exportTex){
3067
 
                t << "\\begin{tabular}{|";
3068
 
                eol = "\\\\\\hline\n";
3069
 
                sep = " & ";
3070
 
        }
3071
3081
 
3072
3082
        int rows = d_table->numRows();
3073
3083
        int cols = d_table->numCols();
3108
3118
        if (!selectedCols)
3109
3119
                aux = cols - 1;
3110
3120
 
3111
 
        if (exportTex){
3112
 
                for (int i = 0; i <= aux; i++)
3113
 
                        t << "c|";
3114
 
                t << "}\\hline\n";
3115
 
        }
3116
 
 
3117
3121
        if (withLabels){
3118
3122
                QStringList header = colNames();
3119
3123
                QStringList ls = header.grep ( QRegExp ("\\D"));
3120
 
                if (exportSelection && selectedCols){
 
3124
                if (exportSelection && selectedCols){
3121
3125
                        for (int i = 0; i < aux; i++){
3122
3126
                                if (ls.count()>0)
3123
3127
                                        t << header[sCols[i]] + sep;
3145
3149
        }// finished writting labels
3146
3150
 
3147
3151
        if (exportComments){
3148
 
                if (exportSelection && selectedCols){
 
3152
                if (exportSelection && selectedCols){
3149
3153
                        for (int i = 0; i < aux; i++)
3150
 
                            t << comments[sCols[i]] + sep;
 
3154
                                t << comments[sCols[i]] + sep;
3151
3155
                        if (aux >= 0)
3152
 
                            t << comments[sCols[aux]] + eol;
 
3156
                                t << comments[sCols[aux]] + eol;
3153
3157
                } else {
3154
 
                    for (int i = 0; i < aux; i++)
3155
 
                        t << comments[i] + sep;
3156
 
                    t << comments[aux] + eol;
3157
 
                }
 
3158
                        for (int i = 0; i < aux; i++)
 
3159
                                t << comments[i] + sep;
 
3160
                        t << comments[aux] + eol;
 
3161
                }
3158
3162
        }
3159
3163
 
3160
 
        if (exportSelection && selectedCols){
 
3164
        if (exportSelection && selectedCols){
3161
3165
                for (int i = topRow; i <= bottomRow; i++){
3162
3166
                        for (int j = 0; j < aux; j++)
3163
3167
                                t << d_table->text(i, sCols[j]) + sep;
3173
3177
                }
3174
3178
        }
3175
3179
 
3176
 
        if (exportTex)
3177
 
                t << "\\end{tabular}\n";
3178
 
 
3179
3180
        f.close();
3180
3181
        QApplication::restoreOverrideCursor();
3181
3182
        return true;
3565
3566
        if (d_show_comments){
3566
3567
                QString s = label;
3567
3568
                int lines = d_table->columnWidth(index)/head->fontMetrics().boundingRect("_").width();
3568
 
                head->setLabel(index, s.remove("\n") + "\n" + QString(lines, '_') + "\n" + comments[index]);
 
3569
                if (index >= 0 && index < comments.size())
 
3570
                        head->setLabel(index, s.remove("\n") + "\n" + QString(lines, '_') + "\n" + comments[index]);
3569
3571
        } else
3570
3572
                head->setLabel(index, label);
3571
3573
}
3634
3636
    if (col < 0 || col >= d_table->numCols())
3635
3637
        return;
3636
3638
 
 
3639
        if (d_table->isColumnReadOnly(col) == on)
 
3640
                return;
 
3641
 
3637
3642
        d_table->setColumnReadOnly(col, on);
 
3643
        emit modifiedWindow(this);
3638
3644
}
3639
3645
 
3640
3646
void Table::moveColumn(int, int fromIndex, int toIndex)
3715
3721
 
3716
3722
void Table::hideColumn(int col, bool hide)
3717
3723
{
3718
 
        if(hide)
 
3724
        if (hide)
3719
3725
                d_table->hideColumn(col);
3720
3726
        else
3721
3727
                d_table->showColumn(col);
 
3728
 
 
3729
        emit modifiedWindow(this);
3722
3730
}
3723
3731
 
3724
3732
void Table::hideSelectedColumns()
3824
3832
        return 0.0;
3825
3833
}
3826
3834
 
3827
 
double Table::min(int col, int startRow, int endRow)
 
3835
double Table::minColumnValue(int col, int startRow, int endRow)
3828
3836
{
3829
3837
        if (col < 0 || col >= d_table->numCols())
3830
3838
                return 0.0;
3851
3859
        return min;
3852
3860
}
3853
3861
 
3854
 
double Table::max(int col, int startRow, int endRow)
 
3862
double Table::maxColumnValue(int col, int startRow, int endRow)
3855
3863
{
3856
3864
        if (col < 0 || col >= d_table->numCols())
3857
3865
                return 0.0;