2015-07-11 18:56:02 +08:00
|
|
|
/*
|
2018-11-25 21:53:26 +08:00
|
|
|
Copyright (c) 2008-2018 Jan W. Krieger (<jan@jkrieger.de>)
|
2015-07-11 18:56:02 +08:00
|
|
|
|
2015-07-12 22:34:27 +08:00
|
|
|
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
|
|
This software is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License (LGPL) as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License (LGPL) for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License (LGPL)
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** \file jkqtpdatastorage.cpp
|
|
|
|
* \ingroup jkqtpdatastorage
|
|
|
|
*/
|
|
|
|
|
2018-11-26 03:25:44 +08:00
|
|
|
#include "jkqtplotter/jkqtpdatastorage.h"
|
2015-07-11 18:56:02 +08:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QtGlobal>
|
|
|
|
|
|
|
|
|
|
|
|
/**************************************************************************************************************************
|
|
|
|
* JKQTPcolumn
|
|
|
|
**************************************************************************************************************************/
|
2018-11-18 18:59:30 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
JKQTPcolumn::JKQTPcolumn()
|
|
|
|
{
|
|
|
|
datastore=nullptr;
|
|
|
|
name="";
|
|
|
|
datastoreItem=0;
|
|
|
|
datastoreOffset=0;
|
|
|
|
valid=false;
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-11-18 18:59:30 +08:00
|
|
|
JKQTPcolumn::JKQTPcolumn(JKQTPdatastore *datastore, const QString &name, size_t datastoreItem, size_t datastoreOffset)
|
|
|
|
{
|
|
|
|
this->datastore=datastore;
|
|
|
|
this->datastoreItem=datastoreItem;
|
|
|
|
this->datastoreOffset=datastoreOffset;
|
|
|
|
this->name=name;
|
|
|
|
valid=true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-11-18 18:59:30 +08:00
|
|
|
JKQTPcolumn::~JKQTPcolumn()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t JKQTPcolumn::getRows() const {
|
2015-07-11 18:56:02 +08:00
|
|
|
if (!valid || !datastore) return 0;
|
|
|
|
JKQTPdatastoreItem* i=datastore->getItem(datastoreItem);
|
|
|
|
if (i) return i->get_rows();
|
|
|
|
else return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-18 18:59:30 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
void JKQTPcolumn::copyData(QVector<double> ©To) const
|
|
|
|
{
|
2018-12-28 22:17:40 +08:00
|
|
|
const double* d=getPointer(0);
|
2015-07-11 18:56:02 +08:00
|
|
|
copyTo.clear();
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t i, cnt=getRows();
|
2015-07-11 18:56:02 +08:00
|
|
|
if (cnt>0) {
|
2018-12-28 22:17:40 +08:00
|
|
|
copyTo.resize(static_cast<int>(cnt));
|
2015-07-11 18:56:02 +08:00
|
|
|
for (i=0; i<cnt; i++) {
|
|
|
|
copyTo[i]=d[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 18:59:30 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
QVector<double> JKQTPcolumn::copyData()
|
|
|
|
{
|
|
|
|
QVector<double> d;
|
|
|
|
copyData(d);
|
|
|
|
return d;
|
2015-08-21 19:49:11 +08:00
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
|
|
|
2018-11-18 18:59:30 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-12-28 22:17:40 +08:00
|
|
|
const double *JKQTPcolumn::getPointer(size_t n) const
|
|
|
|
{
|
|
|
|
if (!datastore) return nullptr;
|
|
|
|
if (!datastore->getItem(datastoreItem)) return nullptr;
|
|
|
|
return datastore->getItem(datastoreItem)->getPointer(datastoreOffset, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
double *JKQTPcolumn::getPointer(size_t n)
|
2015-07-11 18:56:02 +08:00
|
|
|
{
|
2018-11-27 05:14:41 +08:00
|
|
|
if (!datastore) return nullptr;
|
|
|
|
if (!datastore->getItem(datastoreItem)) return nullptr;
|
2015-07-11 18:56:02 +08:00
|
|
|
return datastore->getItem(datastoreItem)->getPointer(datastoreOffset, n);
|
2015-08-21 19:49:11 +08:00
|
|
|
}
|
2015-07-11 18:56:02 +08:00
|
|
|
|
2015-08-21 19:49:11 +08:00
|
|
|
|
2018-11-18 18:59:30 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
void JKQTPcolumn::copy(double* data, size_t N, size_t offset) {
|
2015-07-11 18:56:02 +08:00
|
|
|
if (!datastore) return ;
|
|
|
|
JKQTPdatastoreItem* it=datastore->getItem(datastoreItem);
|
|
|
|
if (!it) return;
|
2018-11-18 18:59:30 +08:00
|
|
|
for (size_t i=0; i<N; i++) {
|
2015-07-11 18:56:02 +08:00
|
|
|
it->set(datastoreOffset, i+offset, data[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 18:59:30 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
void JKQTPcolumn::exchange(double value, double replace)
|
|
|
|
{
|
|
|
|
if (!datastore) return ;
|
2018-11-18 18:59:30 +08:00
|
|
|
for (size_t i=0; i<getRows(); i++) {
|
2015-07-11 18:56:02 +08:00
|
|
|
double v=getValue(i);
|
|
|
|
if (v==value) v=replace;
|
|
|
|
setValue(i, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 18:59:30 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
void JKQTPcolumn::subtract(double value)
|
|
|
|
{
|
|
|
|
if (!datastore) return ;
|
|
|
|
double* data=getPointer();
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t N=getRows();
|
2015-07-11 18:56:02 +08:00
|
|
|
if (data){
|
2018-11-18 18:59:30 +08:00
|
|
|
for (size_t i=0; i<N; i++) {
|
2015-07-11 18:56:02 +08:00
|
|
|
data[i]=data[i]-value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-18 18:59:30 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
void JKQTPcolumn::scale(double factor)
|
|
|
|
{
|
|
|
|
if (!datastore) return ;
|
|
|
|
double* data=getPointer();
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t N=getRows();
|
2015-07-11 18:56:02 +08:00
|
|
|
if (data){
|
2018-11-18 18:59:30 +08:00
|
|
|
for (size_t i=0; i<N; i++) {
|
2015-07-11 18:56:02 +08:00
|
|
|
data[i]=data[i]*factor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 06:57:20 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
void JKQTPcolumn::setAll(double value)
|
|
|
|
{
|
|
|
|
if (!datastore) return ;
|
|
|
|
double* data=getPointer();
|
|
|
|
size_t N=getRows();
|
|
|
|
if (data){
|
|
|
|
for (size_t i=0; i<N; i++) {
|
|
|
|
data[i]=value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
|
|
/**************************************************************************************************************************
|
|
|
|
* JKQTPdatastoreItem
|
|
|
|
**************************************************************************************************************************/
|
2018-11-18 18:59:30 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
JKQTPdatastoreItem::JKQTPdatastoreItem(){
|
|
|
|
this->dataformat=JKQTPsingleColumn;
|
|
|
|
this->allocated=false;
|
2018-11-18 18:59:30 +08:00
|
|
|
this->data=nullptr;
|
2015-07-11 18:56:02 +08:00
|
|
|
this->columns=0;
|
|
|
|
this->rows=0;
|
|
|
|
this->internal=true;
|
|
|
|
}
|
|
|
|
|
2018-11-18 18:59:30 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
JKQTPdatastoreItem::JKQTPdatastoreItem(size_t columns, size_t rows){
|
2015-07-11 18:56:02 +08:00
|
|
|
this->internal=true;
|
|
|
|
this->allocated=false;
|
|
|
|
if (columns>1) {
|
|
|
|
this->dataformat=JKQTPmatrixRow;
|
|
|
|
this->data=(double*)calloc(columns*rows, sizeof(double));
|
|
|
|
} else {
|
|
|
|
this->dataformat=JKQTPsingleColumn;
|
|
|
|
this->data=(double*)calloc(rows, sizeof(double));
|
|
|
|
}
|
|
|
|
this->columns=columns;
|
|
|
|
this->rows=rows;
|
|
|
|
this->allocated=true;
|
|
|
|
}
|
|
|
|
|
2018-11-18 18:59:30 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
void JKQTPdatastoreItem::resizeColumns(size_t new_rows) {
|
|
|
|
if (internal && allocated && data!=nullptr) {
|
2015-07-11 18:56:02 +08:00
|
|
|
free(data);
|
2018-11-18 18:59:30 +08:00
|
|
|
data=nullptr;
|
2015-07-11 18:56:02 +08:00
|
|
|
}
|
|
|
|
data=(double*)calloc(columns*new_rows, sizeof(double));
|
|
|
|
rows=new_rows;
|
|
|
|
internal=true;
|
|
|
|
allocated=true;
|
|
|
|
}
|
|
|
|
|
2018-11-18 18:59:30 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
JKQTPdatastoreItem::JKQTPdatastoreItem(JKQTPdatastoreItemFormat dataformat, double* data, size_t columns, size_t rows){
|
2015-07-11 18:56:02 +08:00
|
|
|
this->dataformat=dataformat;
|
|
|
|
this->allocated=true;
|
|
|
|
this->data=data;
|
|
|
|
this->columns=columns;
|
|
|
|
this->rows=rows;
|
|
|
|
this->internal=false;
|
|
|
|
}
|
|
|
|
|
2018-11-18 18:59:30 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
JKQTPdatastoreItem::JKQTPdatastoreItem(JKQTPdatastoreItemFormat dataformat, double *data, size_t columns, size_t rows, bool internal)
|
2015-07-11 18:56:02 +08:00
|
|
|
{
|
|
|
|
this->dataformat=dataformat;
|
|
|
|
this->allocated=true;
|
|
|
|
this->data=data;
|
|
|
|
this->columns=columns;
|
|
|
|
this->rows=rows;
|
|
|
|
this->internal=internal;
|
|
|
|
}
|
|
|
|
|
2018-11-18 18:59:30 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
JKQTPdatastoreItem::~JKQTPdatastoreItem(){
|
2018-11-18 18:59:30 +08:00
|
|
|
if (internal && allocated && data!=nullptr) {
|
2015-07-11 18:56:02 +08:00
|
|
|
free(data);
|
2018-11-18 18:59:30 +08:00
|
|
|
data=nullptr;
|
2015-07-11 18:56:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**************************************************************************************************************************
|
|
|
|
* JKQTPdatastore
|
|
|
|
**************************************************************************************************************************/
|
|
|
|
|
2018-11-18 18:59:30 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
size_t JKQTPdatastore::addItem(JKQTPdatastoreItem* item) {
|
|
|
|
/*items.push_back(item);
|
|
|
|
return items.size()-1;*/
|
|
|
|
items.insert(maxItemID, item);
|
|
|
|
maxItemID++;
|
|
|
|
return maxItemID-1;
|
|
|
|
}
|
|
|
|
|
2018-11-18 18:59:30 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
size_t JKQTPdatastore::addColumn(JKQTPcolumn col) {
|
|
|
|
columns.insert(maxColumnsID, col);
|
|
|
|
maxColumnsID++;
|
|
|
|
return maxColumnsID-1;
|
|
|
|
}
|
|
|
|
|
2018-11-18 18:59:30 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
JKQTPdatastore::JKQTPdatastore()
|
|
|
|
{
|
|
|
|
maxItemID=0;
|
|
|
|
maxColumnsID=0;
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
void JKQTPdatastore::clear(){
|
|
|
|
maxItemID=0;
|
|
|
|
maxColumnsID=0;
|
|
|
|
/*if (items.size()>0) {
|
|
|
|
for (size_t i=0; i<items.size(); i++) {
|
|
|
|
delete items[i];
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
QMapIterator<size_t, JKQTPdatastoreItem*> it(items);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
delete it.value();
|
|
|
|
}
|
|
|
|
items.clear();
|
|
|
|
columns.clear();
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-11-18 18:59:30 +08:00
|
|
|
void JKQTPdatastore::deleteAllColumns(const QString& name, bool removeItems) {
|
2015-07-11 18:56:02 +08:00
|
|
|
QList<size_t> ids;
|
|
|
|
QMapIterator<size_t, JKQTPcolumn> it(columns);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
if (it.value().get_name()==name) {
|
|
|
|
ids.append(it.key());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i=0; i<ids.size(); i++) {
|
|
|
|
deleteColumn(ids[i], removeItems);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
void JKQTPdatastore::deleteAllPrefixedColumns(QString prefix, bool removeItems) {
|
|
|
|
QList<size_t> ids;
|
|
|
|
QMapIterator<size_t, JKQTPcolumn> it(columns);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
if (it.value().get_name().startsWith(prefix)) {
|
|
|
|
ids.append(it.key());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i=0; i<ids.size(); i++) {
|
|
|
|
deleteColumn(ids[i], removeItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
void JKQTPdatastore::deleteColumn(size_t column, bool removeItems) {
|
|
|
|
if (removeItems) {
|
|
|
|
size_t dsitem=columns[column].get_datastoreItem();
|
|
|
|
int cnt=0;
|
|
|
|
QMapIterator<size_t, JKQTPcolumn> it(columns);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
if (it.value().get_datastoreItem()==dsitem) {
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cnt<=1) {
|
|
|
|
delete items[dsitem];
|
|
|
|
items.remove(dsitem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
columns.remove(column);
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-11-18 18:59:30 +08:00
|
|
|
int JKQTPdatastore::getColumnNum(const QString& name) {
|
2015-07-11 18:56:02 +08:00
|
|
|
if (columns.size()<=0) return -1;
|
|
|
|
QMapIterator<size_t, JKQTPcolumn> it(columns);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
if (it.value().get_name()==name) return it.key();
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t JKQTPdatastore::ensureColumnNum(const QString& name) {
|
2015-07-11 18:56:02 +08:00
|
|
|
if (columns.size()<=0) return -1;
|
|
|
|
QMapIterator<size_t, JKQTPcolumn> it(columns);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
if (it.value().get_name()==name) return it.key();
|
|
|
|
}
|
|
|
|
return addColumn(0, name);
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-08-21 19:49:11 +08:00
|
|
|
JKQTPcolumn JKQTPdatastore::getColumn(size_t i) const
|
|
|
|
{
|
|
|
|
return columns.value(i);
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-12-16 21:59:39 +08:00
|
|
|
JKQTPcolumn JKQTPdatastore::getColumn(int i) const
|
|
|
|
{
|
|
|
|
return columns.value(i);
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t JKQTPdatastore::addCopiedItem(JKQTPdatastoreItemFormat dataformat, double* data, size_t columnsnum, size_t rows) {
|
|
|
|
JKQTPdatastoreItem* it=nullptr;
|
2015-07-11 18:56:02 +08:00
|
|
|
if ((dataformat==JKQTPsingleColumn)||(columnsnum==1)) {
|
|
|
|
return addCopiedItem(data, rows);
|
|
|
|
} else if (dataformat==JKQTPmatrixColumn) {
|
|
|
|
it=new JKQTPdatastoreItem(columnsnum, rows);
|
|
|
|
for (size_t c=0; c<columnsnum; c++) {
|
2018-11-18 18:59:30 +08:00
|
|
|
for (size_t r=0; r<rows; r++) {
|
2015-07-11 18:56:02 +08:00
|
|
|
it->set(c, r, data[c*rows+r]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (dataformat==JKQTPmatrixColumn) {
|
|
|
|
it=new JKQTPdatastoreItem(columnsnum, rows);
|
2018-11-18 18:59:30 +08:00
|
|
|
for (size_t r=0; r<rows; r++) {
|
2015-07-11 18:56:02 +08:00
|
|
|
for (size_t c=0; c<columnsnum; c++) {
|
|
|
|
it->set(c, r, data[r*columnsnum+c]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*items.push_back(it);
|
|
|
|
return items.size()-1;*/
|
|
|
|
return addItem(it);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t JKQTPdatastore::addItem(size_t rows) {
|
2015-07-11 18:56:02 +08:00
|
|
|
/*items.push_back(new JKQTPdatastoreItem(1, rows));
|
|
|
|
return items.size()-1;*/
|
|
|
|
return addItem(new JKQTPdatastoreItem(1, rows));
|
|
|
|
};
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t JKQTPdatastore::addItem(size_t columnsnum, size_t rows) {
|
2015-07-11 18:56:02 +08:00
|
|
|
/*items.push_back(new JKQTPdatastoreItem(columnsnum, rows));
|
|
|
|
return items.size()-1;*/
|
|
|
|
return addItem(new JKQTPdatastoreItem(columnsnum, rows));
|
|
|
|
};
|
|
|
|
|
|
|
|
/** \brief add one external column to the datastore. It contains \a rows rows.*/
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t JKQTPdatastore::addItem(double* data, size_t rows) {
|
2015-07-11 18:56:02 +08:00
|
|
|
/*items.push_back(new JKQTPdatastoreItem(JKQTPsingleColumn, data, 1, rows));
|
|
|
|
return items.size()-1;*/
|
|
|
|
return addItem(new JKQTPdatastoreItem(JKQTPsingleColumn, data, 1, rows));
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t JKQTPdatastore::addInternalItem(double *data, size_t rows)
|
2015-07-11 18:56:02 +08:00
|
|
|
{
|
|
|
|
JKQTPdatastoreItem* dsi=new JKQTPdatastoreItem(JKQTPsingleColumn, data, 1, rows, true);
|
|
|
|
return addItem(dsi);
|
|
|
|
};
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t JKQTPdatastore::addItem(JKQTPdatastoreItemFormat dataformat, double* data, size_t columnsnum, size_t rows) {
|
2015-07-11 18:56:02 +08:00
|
|
|
/*items.push_back(new JKQTPdatastoreItem(dataformat, data, columnsnum, rows));
|
|
|
|
return items.size()-1;*/
|
|
|
|
return addItem(new JKQTPdatastoreItem(dataformat, data, columnsnum, rows));
|
|
|
|
};
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t JKQTPdatastore::addCopiedItem(const double* data, size_t rows) {
|
2015-07-11 18:56:02 +08:00
|
|
|
JKQTPdatastoreItem* it=new JKQTPdatastoreItem(1, rows);
|
|
|
|
if (data) {
|
2018-11-18 18:59:30 +08:00
|
|
|
for (size_t i=0; i<rows; i++) {
|
2015-07-11 18:56:02 +08:00
|
|
|
it->set(0, i, data[i]);
|
|
|
|
//std::cout<<"copy@"<<i<<" = "<<data[i]<<std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*items.push_back(it);
|
|
|
|
return items.size()-1;*/
|
|
|
|
return addItem(it);
|
|
|
|
};
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t JKQTPdatastore::addColumnForItem(size_t itemID, size_t columnInItem, const QString& name) {
|
2015-07-11 18:56:02 +08:00
|
|
|
/*JKQTPcolumn c(this, name, itemID, columnInItem);
|
|
|
|
columns.push_back(c);
|
|
|
|
return columns.size()-1;*/
|
|
|
|
return addColumn(JKQTPcolumn(this, name, itemID, columnInItem));
|
|
|
|
};
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t JKQTPdatastore::addColumn(size_t rows, const QString& name) {
|
2015-07-11 18:56:02 +08:00
|
|
|
//items.push_back(new JKQTPdatastoreItem(1, rows));
|
|
|
|
//return addColumnForItem(items.size()-1, 0, name);
|
|
|
|
size_t item= addItem(new JKQTPdatastoreItem(1, rows));
|
|
|
|
return addColumnForItem(item, 0, name);
|
|
|
|
};
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t JKQTPdatastore::addColumn(double* data, size_t rows, const QString& name) {
|
2015-07-11 18:56:02 +08:00
|
|
|
//items.push_back(new JKQTPdatastoreItem(JKQTPsingleColumn, data, 1, rows));
|
|
|
|
//std::cout<<"added item\n";
|
|
|
|
//size_t it=items.size()-1;
|
|
|
|
//std::cout<<"adding column\n";
|
|
|
|
size_t it=addItem(new JKQTPdatastoreItem(JKQTPsingleColumn, data, 1, rows));
|
|
|
|
return addColumnForItem(it, 0, name);
|
|
|
|
};
|
2018-12-28 22:17:40 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t JKQTPdatastore::addInternalColumn(double* data, size_t rows, const QString& name) {
|
2015-07-11 18:56:02 +08:00
|
|
|
//items.push_back(new JKQTPdatastoreItem(JKQTPsingleColumn, data, 1, rows));
|
|
|
|
//std::cout<<"added item\n";
|
|
|
|
//size_t it=items.size()-1;
|
|
|
|
//std::cout<<"adding column\n";
|
|
|
|
size_t it=addItem(new JKQTPdatastoreItem(JKQTPsingleColumn, data, 1, rows,true));
|
|
|
|
return addColumnForItem(it, 0, name);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-08-02 19:36:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t JKQTPdatastore::copyColumn(size_t old_column, size_t start, size_t stride, const QString& name)
|
2015-07-11 18:56:02 +08:00
|
|
|
{
|
|
|
|
JKQTPcolumn old=columns[old_column];
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t rows=old.getRows();
|
2015-07-11 18:56:02 +08:00
|
|
|
double* d=(double*)malloc(rows*sizeof(double));
|
|
|
|
double* dd=old.getPointer(0);
|
|
|
|
int j=0;
|
2018-11-18 18:59:30 +08:00
|
|
|
for (size_t i=start; i<rows; i+=stride) {
|
2015-07-11 18:56:02 +08:00
|
|
|
d[j]=dd[i];
|
|
|
|
//qDebug()<<old_column<<name<<": "<<j<<i<<d[j];
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
size_t n=addCopiedColumn(d, j, name);
|
|
|
|
free(d);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t JKQTPdatastore::copyColumn(size_t old_column, const QString& name)
|
2015-08-02 19:36:54 +08:00
|
|
|
{
|
|
|
|
return copyColumn(old_column, 0, 1, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t JKQTPdatastore::addLinearColumn(size_t rows, double start, double end, const QString& name) {
|
2015-07-11 18:56:02 +08:00
|
|
|
double delta=(end-start)/(double)(rows-1);
|
|
|
|
JKQTPdatastoreItem* it=new JKQTPdatastoreItem(1, rows);
|
2018-11-18 18:59:30 +08:00
|
|
|
for (size_t i=0; i<rows; i++) {
|
2015-07-11 18:56:02 +08:00
|
|
|
it->set(0, i, start+(double)i * delta);
|
|
|
|
//std::cout<<"copy@"<<i<<" = "<<data[i]<<std::endl;
|
|
|
|
}
|
|
|
|
/*items.push_back(it);
|
|
|
|
size_t itemid=items.size()-1;*/
|
|
|
|
size_t itemid= addItem(it);
|
|
|
|
return addColumnForItem(itemid, 0, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-12-16 21:59:39 +08:00
|
|
|
int JKQTPdatastore::getNextLowerIndex(size_t column, size_t row, int start, int end) const
|
2015-08-21 19:49:11 +08:00
|
|
|
{
|
|
|
|
const JKQTPcolumn& col=columns[column];
|
|
|
|
if (start<0 && end>=0) return getNextLowerIndex(column, row, 0, end);
|
|
|
|
else if (start>=0 && end<0) return getNextLowerIndex(column, row, start, col.getRows()-1);
|
|
|
|
else if (start<0 && end<0) return getNextLowerIndex(column, row, 0, col.getRows()-1);
|
|
|
|
else {
|
|
|
|
double d=0;
|
|
|
|
const double v=col.getValue(row);
|
2018-12-16 21:59:39 +08:00
|
|
|
int res=-1;
|
|
|
|
for ( int i=start; i<=end; i++) {
|
|
|
|
if (i!=(int)row) {
|
2015-08-21 19:49:11 +08:00
|
|
|
const double v1=col.getValue(i);
|
|
|
|
const double dd=v1-v;
|
|
|
|
if ((dd<0) && ((fabs(dd)<d)||(d==0.0))) {
|
|
|
|
res=i;
|
|
|
|
d=fabs(dd);
|
|
|
|
//std::cout<<" getNextLowerIndex("<<column<<", "<<row<<", "<<start<<", "<<end<<"): i="<<i<<": OK res="<<res<<" d="<<d<<"\n";
|
|
|
|
} /*else {
|
|
|
|
std::cout<<" getNextLowerIndex("<<column<<", "<<row<<", "<<start<<", "<<end<<"): i="<<i<<": FAIL res="<<res<<" dd="<<dd<<" v1="<<v1<<" v="<<v<<"\n";
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-12-16 21:59:39 +08:00
|
|
|
int JKQTPdatastore::getNextLowerIndex(size_t column, size_t row) const
|
2015-08-21 19:49:11 +08:00
|
|
|
{
|
|
|
|
return getNextLowerIndex(column, row, 0, columns[column].getRows()-1);
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-12-16 21:59:39 +08:00
|
|
|
int JKQTPdatastore::getNextHigherIndex(size_t column, size_t row, int start, int end) const
|
2015-08-21 19:49:11 +08:00
|
|
|
{
|
|
|
|
const JKQTPcolumn& col=columns[column];
|
|
|
|
if (start<0 && end>=0) return getNextHigherIndex(column, row, 0, end);
|
|
|
|
else if (start>=0 && end<0) return getNextHigherIndex(column, row, start, col.getRows()-1);
|
|
|
|
else if (start<0 && end<0) return getNextHigherIndex(column, row, 0, col.getRows()-1);
|
|
|
|
else {
|
|
|
|
double d=0;
|
|
|
|
const double v=col.getValue(row);
|
2018-12-16 21:59:39 +08:00
|
|
|
int res=-1;
|
|
|
|
for ( int i=start; i<=end; i++) {
|
|
|
|
if (i!=(int)row) {
|
2015-08-21 19:49:11 +08:00
|
|
|
const double v1=col.getValue(i);
|
|
|
|
const double dd=v1-v;
|
|
|
|
if ((dd>0) && ((fabs(dd)<d)||(d==0.0))) {
|
|
|
|
res=i;
|
|
|
|
d=fabs(dd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-12-16 21:59:39 +08:00
|
|
|
int JKQTPdatastore::getNextHigherIndex(size_t column, size_t row) const
|
2015-08-21 19:49:11 +08:00
|
|
|
{
|
|
|
|
return getNextHigherIndex(column, row, 0, columns[column].getRows()-1);
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-12-16 21:59:39 +08:00
|
|
|
int JKQTPdatastore::getNextLowerIndex(int column, size_t row, int start, int end) const {
|
|
|
|
return getNextLowerIndex(static_cast<size_t>(column), row, start, end);
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-12-16 21:59:39 +08:00
|
|
|
int JKQTPdatastore::getNextLowerIndex(int column, size_t row) const {
|
|
|
|
return getNextLowerIndex(static_cast<size_t>(column), row);
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-12-16 21:59:39 +08:00
|
|
|
int JKQTPdatastore::getNextHigherIndex(int column, size_t row, int start, int end) const {
|
|
|
|
return getNextHigherIndex(static_cast<size_t>(column), row, start, end);
|
2015-08-21 19:49:11 +08:00
|
|
|
}
|
|
|
|
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
|
|
|
2018-12-16 21:59:39 +08:00
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t JKQTPdatastore::getMaxRows() {
|
|
|
|
size_t res=0;
|
2015-07-11 18:56:02 +08:00
|
|
|
/*for (size_t i=0; i<columns.size(); i++) {
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t r=columns[i].getRows();
|
2015-07-11 18:56:02 +08:00
|
|
|
if (r>res) res=r;
|
|
|
|
}*/
|
|
|
|
QMapIterator<size_t, JKQTPcolumn> it(columns);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t r=it.value().getRows();
|
2015-07-11 18:56:02 +08:00
|
|
|
if (r>res) res=r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
void JKQTPdatastore::saveCSV(QString filename, QSet<int> userColumns, QString separator, QString decimal_separator, QString comment, QString aroundStrings, char floatformat) {
|
|
|
|
//std::cout<<filename<<"\n";
|
|
|
|
|
|
|
|
// find out the decimal and the thousand separator
|
|
|
|
QLocale loc=QLocale::c();
|
|
|
|
loc.setNumberOptions(QLocale::OmitGroupSeparator);
|
2015-08-02 19:36:54 +08:00
|
|
|
//QChar dsep=loc.decimalPoint();
|
2015-07-11 18:56:02 +08:00
|
|
|
QFile f(filename);
|
|
|
|
if (!f.open(QIODevice::WriteOnly|QIODevice::Text)) return;
|
|
|
|
QTextStream txt(&f);
|
|
|
|
saveCSV(txt, userColumns, separator, decimal_separator, comment, aroundStrings, floatformat);
|
|
|
|
f.close();
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
void JKQTPdatastore::saveMatlab(QString filename, QSet<int> userColumns) {
|
|
|
|
//std::cout<<filename<<"\n";
|
|
|
|
|
|
|
|
// find out the decimal and the thousand separator
|
|
|
|
QLocale loc=QLocale::c();
|
|
|
|
loc.setNumberOptions(QLocale::OmitGroupSeparator);
|
2015-08-02 19:36:54 +08:00
|
|
|
//QChar dsep=loc.decimalPoint();
|
2015-07-11 18:56:02 +08:00
|
|
|
QFile f(filename);
|
|
|
|
if (!f.open(QIODevice::WriteOnly|QIODevice::Text)) return;
|
|
|
|
QTextStream txt(&f);
|
|
|
|
saveMatlab(txt, userColumns);
|
|
|
|
f.close();
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
QStringList JKQTPdatastore::getColumnNames() const {
|
|
|
|
QStringList names;
|
|
|
|
QMapIterator<size_t, JKQTPcolumn> it(columns);
|
|
|
|
int col=0;
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
names.append(it.value().get_name());
|
|
|
|
col++;
|
|
|
|
}
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
void JKQTPdatastore::saveMatlab(QTextStream &txt, QSet<int> userColumns) {
|
|
|
|
//std::cout<<filename<<"\n";
|
|
|
|
|
|
|
|
// find out the decimal and the thousand separator
|
|
|
|
QLocale loc=QLocale::c();
|
|
|
|
loc.setNumberOptions(QLocale::OmitGroupSeparator);
|
|
|
|
QSet<QString> varnames;
|
|
|
|
|
|
|
|
|
|
|
|
txt.setLocale(loc);
|
|
|
|
/* if (comment.size()>0) {
|
|
|
|
txt<<comment<<" ";
|
|
|
|
bool first=true;
|
|
|
|
QMapIterator<size_t, JKQTPcolumn> it(columns);
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
if (!first) txt<<separator;
|
|
|
|
txt<<aroundStrings<<it.value().get_name()<<aroundStrings;
|
|
|
|
first=false;
|
|
|
|
}
|
|
|
|
txt<<"\n";
|
|
|
|
}*/
|
|
|
|
QMapIterator<size_t, JKQTPcolumn> it(columns);
|
|
|
|
int col=0;
|
|
|
|
int i=0;
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
|
|
|
|
if (userColumns.isEmpty() || userColumns.contains(i)) {
|
|
|
|
|
|
|
|
// convert the column name into a variable name (name cleanup!) If the column name is empty, we use a default name ("column")
|
|
|
|
QString newvarbase=jkqtp_to_valid_variable_name(it.value().get_name().toStdString()).c_str();
|
|
|
|
if (newvarbase.isEmpty()) newvarbase="column";
|
|
|
|
int cnt=1;
|
|
|
|
QString newvar=newvarbase;
|
|
|
|
// now we make sure that the name is not already used, i.e. we compare it to all variable names used so far and if it has already been used,
|
|
|
|
// an increasing number is added to the name.
|
|
|
|
while (varnames.contains(newvar)) {
|
|
|
|
newvar=newvarbase+QString::number(cnt);
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
varnames.insert(newvar);
|
|
|
|
txt<<QString("% data from columne %1 ('%2')\n").arg(col+1).arg(it.value().get_name());
|
|
|
|
txt<<QString("%1 = [ ").arg(newvar);
|
2018-11-18 18:59:30 +08:00
|
|
|
for (size_t i=0; i<it.value().getRows(); i++) {
|
2015-07-11 18:56:02 +08:00
|
|
|
txt<<loc.toString(get(it.key(), i))<<" ";
|
|
|
|
|
|
|
|
}
|
|
|
|
txt<<"];\n\n";
|
|
|
|
col++;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
void JKQTPdatastore::saveCSV(QTextStream& txt, QSet<int> userColumns, QString separator, QString decimal_separator, QString comment, QString aroundStrings, char floatformat) {
|
|
|
|
//std::cout<<filename<<"\n";
|
|
|
|
|
|
|
|
// find out the decimal and the thousand separator
|
|
|
|
QLocale loc=QLocale::c();
|
|
|
|
loc.setNumberOptions(QLocale::OmitGroupSeparator);
|
|
|
|
QChar dsep=loc.decimalPoint();
|
|
|
|
|
|
|
|
|
|
|
|
txt.setLocale(loc);
|
|
|
|
if (comment.size()>0) {
|
|
|
|
txt<<comment<<" ";
|
|
|
|
bool first=true;
|
|
|
|
QMapIterator<size_t, JKQTPcolumn> it(columns);
|
|
|
|
int i=0;
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
if (userColumns.isEmpty() || userColumns.contains(i)) {
|
|
|
|
if (!first) txt<<separator;
|
|
|
|
txt<<aroundStrings<<it.value().get_name()<<aroundStrings;
|
|
|
|
first=false;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
txt<<"\n";
|
|
|
|
}
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t rows=getMaxRows();
|
|
|
|
for (size_t i=0; i<rows; i++) {
|
2015-07-11 18:56:02 +08:00
|
|
|
bool first=true;
|
|
|
|
QMapIterator<size_t, JKQTPcolumn> it(columns);
|
|
|
|
int j=0;
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
if (userColumns.isEmpty() || userColumns.contains(j)) {
|
|
|
|
if (!first) txt<<separator;
|
|
|
|
first=false;
|
|
|
|
if (it.value().getRows()>i) {
|
|
|
|
QString num=QString("%1").arg(get(it.key(), i), 0, floatformat);
|
|
|
|
num=num.replace(dsep, decimal_separator);
|
|
|
|
txt<<num;
|
|
|
|
}
|
|
|
|
//std::cout<<"("<<i<<", "<<c<<")\n";
|
|
|
|
}
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
txt<<"\n";
|
|
|
|
}
|
|
|
|
txt<<"\n";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
void JKQTPdatastore::saveSYLK(QString filename, QSet<int> userColumns, QString floatformat) {
|
|
|
|
Q_UNUSED(floatformat)
|
|
|
|
// find out the decimal and the thousand separator
|
|
|
|
QLocale loc=QLocale::c();
|
|
|
|
loc.setNumberOptions(QLocale::OmitGroupSeparator);
|
2015-08-02 19:36:54 +08:00
|
|
|
//QChar dsep=loc.decimalPoint();
|
2015-07-11 18:56:02 +08:00
|
|
|
QFile f(filename);
|
|
|
|
if (!f.open(QIODevice::WriteOnly|QIODevice::Text)) return;
|
|
|
|
QTextStream txt(&f);
|
|
|
|
txt.setLocale(loc);
|
|
|
|
// write SYLK header
|
|
|
|
txt<<"ID;P\n";
|
|
|
|
|
|
|
|
// write column headers
|
|
|
|
QMapIterator<size_t, JKQTPcolumn> it(columns);
|
|
|
|
size_t c=1;
|
|
|
|
int i=0;
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
if (userColumns.isEmpty() || userColumns.contains(i)) {
|
|
|
|
txt<<"C;Y1;X"<<c<<";K\""<<it.value().get_name()<<"\"\n";
|
|
|
|
txt<<"F;Y1;X"<<c<<";SDB\n";
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t rows=getMaxRows();
|
|
|
|
for (size_t i=0; i<rows; i++) {
|
2015-07-11 18:56:02 +08:00
|
|
|
QMapIterator<size_t, JKQTPcolumn> it(columns);
|
|
|
|
c=1;
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
if (userColumns.isEmpty() || userColumns.contains(i)) {
|
|
|
|
if (it.value().getRows()>i) {
|
|
|
|
txt<<QString("C;X%1;Y%2;N;K%3\n").arg(c).arg(i+2).arg(get(it.key(), i));
|
|
|
|
}
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
txt<<"E\n\n";
|
|
|
|
f.close();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
QList<QVector<double> > JKQTPdatastore::getData(QStringList *columnNames, QSet<int> userColumns)
|
|
|
|
{
|
|
|
|
QStringList cl;
|
|
|
|
QList<QVector<double> > res;
|
|
|
|
|
|
|
|
// write column headers
|
|
|
|
QMapIterator<size_t, JKQTPcolumn> it(columns);
|
|
|
|
size_t c=1;
|
|
|
|
int i=0;
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
if (userColumns.isEmpty() || userColumns.contains(i)) {
|
|
|
|
QVector<double> dat;
|
2018-11-18 18:59:30 +08:00
|
|
|
for (size_t i=0; i<it.value().getRows(); i++) {
|
2015-07-11 18:56:02 +08:00
|
|
|
dat<<it.value().getValue(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
cl<<it.value().get_name();
|
|
|
|
res<<dat;
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (columnNames) *columnNames=cl;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
void JKQTPdatastore::saveDIF(QString filename, QSet<int> userColumns, QString floatformat) {
|
|
|
|
Q_UNUSED(floatformat)
|
|
|
|
// find out the decimal and the thousand separator
|
|
|
|
QLocale loc=QLocale::c();
|
|
|
|
loc.setNumberOptions(QLocale::OmitGroupSeparator);
|
2015-08-02 19:36:54 +08:00
|
|
|
//QChar dsep=loc.decimalPoint();
|
2015-07-11 18:56:02 +08:00
|
|
|
QFile f(filename);
|
|
|
|
if (!f.open(QIODevice::WriteOnly|QIODevice::Text)) return;
|
|
|
|
QTextStream txt(&f);
|
|
|
|
txt.setLocale(loc);
|
2018-11-18 18:59:30 +08:00
|
|
|
size_t rows=getMaxRows();
|
2015-07-11 18:56:02 +08:00
|
|
|
|
|
|
|
// write DIF header
|
|
|
|
txt<<QString("TABLE\n0,1\n\"\"\n");
|
|
|
|
if (userColumns.isEmpty()) txt<<QString("VECTORS\n0,%1\n\"\"\n").arg(columns.size());
|
|
|
|
else txt<<QString("VECTORS\n0,%1\n\"\"\n").arg(userColumns.size());
|
|
|
|
txt<<QString("TUPLES\n0,%1\n\"\"\n").arg(rows);
|
|
|
|
|
|
|
|
txt<<QString("-1,0\nBOT\n");
|
|
|
|
|
|
|
|
// write column headers
|
|
|
|
QMapIterator<size_t, JKQTPcolumn> it(columns);
|
|
|
|
size_t c=1;
|
|
|
|
int i=0;
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
if (userColumns.isEmpty() || userColumns.contains(i)) {
|
|
|
|
txt<<QString("1,0\n\"%1\"\n").arg(columns[c].get_name());
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-11-18 18:59:30 +08:00
|
|
|
for (size_t i=0; i<rows; i++) {
|
2015-07-11 18:56:02 +08:00
|
|
|
txt<<QString("-1,0\nBOT\n");
|
|
|
|
QMapIterator<size_t, JKQTPcolumn> it(columns);
|
|
|
|
c=1;
|
|
|
|
int j=0;
|
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
if (userColumns.isEmpty() || userColumns.contains(j)) {
|
|
|
|
//qDebug()<<i<<"/"<<rows<<", "<<c;
|
|
|
|
if (i<it.value().getRows()) {
|
|
|
|
txt<<QString("0,%1\nV\n").arg(get(it.key(), i));
|
|
|
|
}
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
txt<<"-1,0\nEOT\n";
|
|
|
|
f.close();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
JKQTPdatastoreModel::JKQTPdatastoreModel(JKQTPdatastore *datastore, QObject *parent):
|
|
|
|
QAbstractTableModel(parent)
|
|
|
|
{
|
|
|
|
this->datastore=datastore;
|
|
|
|
reloadModel();
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
JKQTPdatastoreModel::~JKQTPdatastoreModel()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
QVariant JKQTPdatastoreModel::data(const QModelIndex &index, int role) const {
|
|
|
|
int row=index.row();
|
|
|
|
int column=index.column();
|
|
|
|
if (datastore) {
|
|
|
|
if (role==Qt::DisplayRole || role==Qt::EditRole) {
|
2018-12-28 22:17:40 +08:00
|
|
|
int col=static_cast<int>(datastore->getColumnIDs().value(column, -1));
|
|
|
|
if (col>-1 && row>=0 && row<static_cast<int>(datastore->getColumn(col).getRows())) {
|
|
|
|
return datastore->get(col, static_cast<size_t>(row));
|
2015-07-11 18:56:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
Qt::ItemFlags JKQTPdatastoreModel::flags(const QModelIndex &/*index*/) const {
|
|
|
|
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
QVariant JKQTPdatastoreModel::headerData(int section, Qt::Orientation orientation, int role) const {
|
|
|
|
if (datastore) {
|
|
|
|
if (role==Qt::DisplayRole) {
|
|
|
|
if (orientation==Qt::Horizontal) {
|
|
|
|
if (section>=0 && section<(int64_t)datastore->getColumnCount()) {
|
|
|
|
//qDebug()<<"columns: "<<datastore->getColumnNames().size()<<datastore->getColumnIDs().size()<<": "<<datastore->getColumnNames();
|
|
|
|
return datastore->getColumnNames().value(section);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return QString::number(section+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
int JKQTPdatastoreModel::rowCount(const QModelIndex &/*parent*/) const {
|
|
|
|
if (datastore) {
|
2018-12-28 22:17:40 +08:00
|
|
|
return static_cast<int>(datastore->getMaxRows());
|
2015-07-11 18:56:02 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
int JKQTPdatastoreModel::columnCount(const QModelIndex &/*parent*/) const {
|
|
|
|
if (datastore) {
|
2018-12-28 22:17:40 +08:00
|
|
|
return static_cast<int>(datastore->getColumnCount());
|
2015-07-11 18:56:02 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2015-07-11 18:56:02 +08:00
|
|
|
void JKQTPdatastoreModel::reloadModel()
|
|
|
|
{
|
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
|
|
|
|
beginResetModel();
|
|
|
|
endResetModel();
|
|
|
|
#else
|
|
|
|
reset();
|
|
|
|
#endif
|
|
|
|
}
|
2018-12-16 21:59:39 +08:00
|
|
|
|
2018-12-28 22:17:40 +08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
2018-12-16 21:59:39 +08:00
|
|
|
int JKQTPdatastore::getNextHigherIndex(int column, size_t row) const {
|
|
|
|
return getNextHigherIndex(static_cast<size_t>(column), row);
|
|
|
|
}
|