Skip to content

Commit

Permalink
update example : copycat, helloandroid, webserver
Browse files Browse the repository at this point in the history
  • Loading branch information
j2doll committed Feb 27, 2019
1 parent 3d1c9f5 commit deb03a0
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 48 deletions.
3 changes: 3 additions & 0 deletions Copycat/XlsxTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ XlsxTab::~XlsxTab()
if ( NULL != vLayout )
{
vLayout->deleteLater();
vLayout = NULL;
}

if ( NULL != table )
{
table->clear();
table->deleteLater();
table = NULL;
}

}
Expand Down
46 changes: 46 additions & 0 deletions HelloAndroid/DynArray2D.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// DynArray2D.h
#ifndef DYNARRAY2D_H
#define DYNARRAY2D_H

// Code from https://www.qtcentre.org/threads/31440-two-dimensional-array-size-determined-dynamically
// Some code is fixed by j2doll

template <typename T> class DynArray2D
{
public:
DynArray2D(int n, int m)
{
_n = n;
_array = new T*[n];
for(int i = 0; i < n; i++)
{
_array[i] = new T[m];
}
}

void setValue(int n, int m, T v)
{
_array[n][m] = v;
}

T getValue(int n, int m)
{
return _array[n][m];
}

~DynArray2D()
{
for (int i = 0 ; i < _n ; i++)
{
delete [] _array[i];
}
delete [] _array;
}

protected:
T **_array;
int _n;
};


#endif // DYNARRAY2D_H
3 changes: 2 additions & 1 deletion HelloAndroid/HelloAndroid.pro
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ DEFINES += QT_DEPRECATED_WARNINGS
include(../QXlsx/QXlsx.pri)

HEADERS += \
XlsxTableModel.h
XlsxTableModel.h \
DynArray2D.h

SOURCES += \
main.cpp \
Expand Down
118 changes: 93 additions & 25 deletions HelloAndroid/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
using namespace QXlsx;

#include "XlsxTableModel.h"
#include "DynArray2D.h"

std::string convertFromNumberToExcelColumn(int n);

int main(int argc, char *argv[])
{
Expand All @@ -40,44 +43,73 @@ int main(int argc, char *argv[])
QQmlContext* ctxt = engine.rootContext();

QXlsx::Document xlsx( ":/test.xlsx" ); // load xlsx
if (!xlsx.isLoadPackage())
if (!xlsx.load())
{
qDebug() << "[ERROR] Failed to load xlsx";
return (-1);
}

// A1 B1
// B2 C2
// A3 B3 C3
QList<QString> colTitle; // list of column title
QVector<CellLocation> vcl; // vector of cell(s) location
Worksheet* wsheet = (Worksheet*) xlsx.workbook()->activeSheet();
if ( NULL == wsheet )
{
qDebug() << "[ERROR] Failed to get active sheet";
return (-2);
}

QList<QString> colTitle;
int rowMax = -1;
int colMax = -1;
vcl = wsheet->getFullCells( &rowMax, &colMax );

colTitle.append(QString("A"));
colTitle.append(QString("B"));
colTitle.append(QString("C"));
Q_ASSERT( (-1) != rowMax ); // To CHECK
Q_ASSERT( (-1) != colMax );

QList<VLIST> xlsxData;
for (int ic = 0 ; ic < colMax ; ic++)
{
std::string strCol = convertFromNumberToExcelColumn(ic + 1);
QString colName = QString::fromStdString( strCol );
colTitle.append( colName );
}

// make cell matrix that has (colMax x rowMax) size.

VLIST vl1;
vl1.append( xlsx.read("A1") );
vl1.append( xlsx.read("B1") );
vl1.append( xlsx.read("C1") );
xlsxData.append( vl1 );
DynArray2D< std::string > dynIntArray(colMax, rowMax);

VLIST vl2;
vl2.append( xlsx.read("A2") );
vl2.append( xlsx.read("B2") );
vl2.append( xlsx.read("C2") );
xlsxData.append( vl2 );
for ( int icl = 0; icl < vcl.size(); ++icl )
{
CellLocation cl = vcl.at(icl); // cell location

// NOTICE: First cell of tableWidget is 0.
// But first cell of Qxlsx document is 1.
int row = cl.row - 1;
int col = cl.col - 1;

QSharedPointer<Cell> ptrCell = cl.cell; // cell pointer

// value of cell
QVariant var = cl.cell.data()->value();
QString str = var.toString();

// set string value to (col, row)
dynIntArray.setValue( col, row, str.toStdString() );
}

VLIST vl3;
vl3.append( xlsx.read("A3") );
vl3.append( xlsx.read("B3") );
vl3.append( xlsx.read("C3") );
xlsxData.append( vl3 );
QList<VLIST> xlsxData;
for (int ir = 0; ir < rowMax; ir++ )
{
VLIST vl;
for (int ic = 0; ic < colMax; ic++)
{
std::string value = dynIntArray.getValue( ic, ir );
vl.append( QString::fromStdString(value) );
}
xlsxData.append(vl);
}

// set model for tableview
XlsxTableModel xlsxTableModel(colTitle, xlsxData);
ctxt->setContextProperty( "xlsxModel", &xlsxTableModel ); // set model for tableview
ctxt->setContextProperty( "xlsxModel", &xlsxTableModel );

engine.load( QUrl(QStringLiteral("qrc:/main.qml")) ); // load QML
if ( engine.rootObjects().isEmpty() )
Expand All @@ -89,3 +121,39 @@ int main(int argc, char *argv[])
int ret = app.exec();
return ret;
}

std::string convertFromNumberToExcelColumn(int n)
{
// main code from https://www.geeksforgeeks.org/find-excel-column-name-given-number/
// Function to print Excel column name for a given column number

std::string stdString;

char str[1000]; // To store result (Excel column name)
int i = 0; // To store current index in str which is result

while ( n > 0 )
{
// Find remainder
int rem = n % 26;

// If remainder is 0, then a 'Z' must be there in output
if ( rem == 0 )
{
str[i++] = 'Z';
n = (n/26) - 1;
}
else // If remainder is non-zero
{
str[i++] = (rem-1) + 'A';
n = n / 26;
}
}
str[i] = '\0';

// Reverse the string and print result
std::reverse( str, str + strlen(str) );

stdString = str;
return stdString;
}
18 changes: 15 additions & 3 deletions HelloAndroid/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,36 @@
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4

Window {
id : mainWindow;
visible : true;
title : qsTr("Hello World");
title : qsTr("Hello Android");
/* width: 640; height: 480; */

Component
{
id : columnComponent;
TableViewColumn { resizable : true; movable : false; /*width: 100;*/ }
TableViewColumn {
resizable : true;
movable : false;
/*width: 100;*/
}
}

TableView {
TableView
{
id : mainTableView;
anchors.fill : parent;
model : xlsxModel;

frameVisible : true;
highlightOnFocus : true;

horizontalScrollBarPolicy : Qt.ScrollBarAlwaysOn;
verticalScrollBarPolicy : Qt.ScrollBarAlwaysOn;

resources:
{
var roleList = xlsxModel.customRoleNames;
Expand Down
Binary file modified HelloAndroid/test.xlsx
Binary file not shown.
5 changes: 3 additions & 2 deletions WebServer/WebServer.pro
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# WebServer.pro
#
# QXlsx https://github.com/QtExcel/QXlsx
# QXlsx https://github.com/QtExcel/QXlsx

TARGET = WebServer
TEMPLATE = app
Expand All @@ -11,9 +11,10 @@ QT += network
QT -= gui

CONFIG += console
CONFIG += c++14
CONFIG -= app_bundle

# C++14 or higher version is required.
CONFIG += c++14
QMAKE_CXXFLAGS += -std=c++14

macx {
Expand Down
33 changes: 16 additions & 17 deletions WebServer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int main(int argc, char *argv[])
ctx.response.send(g_htmlDoc);
});

quint16 listenPort = 3001;
quint16 listenPort = 3001; // default port
auto result = app.listen( listenPort );
if ( result.error() )
{
Expand All @@ -50,7 +50,9 @@ int main(int argc, char *argv[])
QString getHtml(QString strFilename)
{
QString ret;

ret = ret + QString("<html>\n");

ret = ret + QString("<head>\n");
ret = ret + QString("<title>") + strFilename + QString("</title>\n");
ret = ret + QString("<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>\n" );
Expand All @@ -59,10 +61,10 @@ QString getHtml(QString strFilename)
ret = ret + QString("<body>\n");

QString strTableStyle = \
"<style>\n"\
" table { border-collapse: collapse; } \n"\
" td, th { border: 1px solid black; } \n"\
"</style>\n";
"<style>\n"\
" table { border-collapse: collapse; } \n"\
" td, th { border: 1px solid black; } \n"\
"</style>\n";
ret = ret + strTableStyle;

if (!loadXlsx(strFilename, ret))
Expand Down Expand Up @@ -125,22 +127,19 @@ bool loadXlsx(QString fileName, QString& strHtml)

for ( int ic = 0; ic < clList.size(); ++ic )
{
// cell location
CellLocation cl = clList.at(ic);
// cell location
CellLocation cl = clList.at(ic);

int row = cl.row - 1;
int col = cl.col - 1;
int row = cl.row - 1;
int col = cl.col - 1;

////////////////////////////////////////////////////////////////////
// cell pointer
QSharedPointer<Cell> ptrCell = cl.cell;
QSharedPointer<Cell> ptrCell = cl.cell; // cell pointer

///////////////////////////////////////////////////////////////////
// value of cell
QVariant var = cl.cell.data()->value();
QString str = var.toString();
// value of cell
QVariant var = cl.cell.data()->value();
QString str = var.toString();

cellValues[row][col] = str;
cellValues[row][col] = str;
}

QString strTableRecord;
Expand Down

0 comments on commit deb03a0

Please sign in to comment.