2018-09-27 17:41:21 +09:00
|
|
|
|
|
|
|
#include <QtGlobal>
|
|
|
|
#include <QString>
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QMessageBox>
|
2018-09-28 18:08:40 +09:00
|
|
|
#include <QVBoxLayout>
|
2018-10-04 13:24:28 +09:00
|
|
|
#include <QPrinter>
|
|
|
|
#include <QPrintPreviewDialog>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QVector>
|
|
|
|
#include <QList>
|
|
|
|
#include <QSharedPointer>
|
|
|
|
|
|
|
|
#include "xlsxcelllocation.h"
|
|
|
|
#include "xlsxcell.h"
|
|
|
|
#include "XlsxTableModel.h"
|
|
|
|
#include "tableprinter.h"
|
2018-09-27 17:41:21 +09:00
|
|
|
#include "MainWindow.h"
|
|
|
|
#include "ui_MainWindow.h"
|
|
|
|
|
|
|
|
MainWindow::MainWindow(QWidget *parent) :
|
|
|
|
QMainWindow(parent),
|
|
|
|
ui(new Ui::MainWindow)
|
|
|
|
{
|
|
|
|
xlsxDoc = NULL;
|
|
|
|
tabWidget = NULL;
|
|
|
|
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
|
|
|
tabWidget = new QTabWidget(this);
|
2018-09-28 18:08:40 +09:00
|
|
|
setCentralWidget(tabWidget);
|
2018-09-27 17:41:21 +09:00
|
|
|
|
2018-09-28 21:20:29 +09:00
|
|
|
this->setWindowTitle(QString("Copycat"));
|
2018-09-27 17:41:21 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
MainWindow::~MainWindow()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
|
|
|
|
if ( NULL != xlsxDoc )
|
|
|
|
{
|
|
|
|
delete xlsxDoc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::on_action_Quit_triggered()
|
|
|
|
{
|
2018-09-28 21:20:29 +09:00
|
|
|
// quit
|
2018-09-27 17:41:21 +09:00
|
|
|
this->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::on_action_Open_triggered()
|
|
|
|
{
|
2018-09-27 19:08:49 +09:00
|
|
|
// open file dialog
|
2018-09-27 17:41:21 +09:00
|
|
|
QString fileName = QFileDialog::getOpenFileName(this,
|
|
|
|
tr("Open Excel"), ".", tr("Excel Files (*.xlsx)"));
|
|
|
|
|
2018-09-28 18:08:40 +09:00
|
|
|
if ( ! loadXlsx(fileName) ) // load xlsx
|
2018-09-27 17:41:21 +09:00
|
|
|
{
|
|
|
|
QMessageBox msgBox;
|
|
|
|
QString alertMsg = QString("Failed to load file.\n %1").arg(fileName);
|
|
|
|
msgBox.setText(alertMsg);
|
|
|
|
msgBox.exec();
|
|
|
|
return;
|
|
|
|
}
|
2018-09-27 19:08:49 +09:00
|
|
|
|
2018-09-28 18:08:40 +09:00
|
|
|
this->setWindowTitle(fileName);
|
|
|
|
|
2018-09-27 19:23:21 +09:00
|
|
|
}
|
2018-09-27 17:41:21 +09:00
|
|
|
|
2018-09-27 19:23:21 +09:00
|
|
|
bool MainWindow::loadXlsx(QString fileName)
|
|
|
|
{
|
2018-09-28 18:08:40 +09:00
|
|
|
// tried to load xlsx using temporary document
|
|
|
|
QXlsx::Document xlsxTmp(fileName);
|
|
|
|
if (!xlsxTmp.isLoadPackage())
|
|
|
|
{
|
2018-09-28 21:20:29 +09:00
|
|
|
return false; // failed to load
|
2018-09-28 18:08:40 +09:00
|
|
|
}
|
|
|
|
|
2018-09-27 17:41:21 +09:00
|
|
|
// clear xlsxDoc
|
|
|
|
if ( NULL != xlsxDoc )
|
|
|
|
{
|
|
|
|
delete xlsxDoc;
|
|
|
|
xlsxDoc = NULL;
|
|
|
|
}
|
|
|
|
|
2018-09-28 18:08:40 +09:00
|
|
|
// load new xlsx using new document
|
2018-09-27 17:41:21 +09:00
|
|
|
xlsxDoc = new QXlsx::Document(fileName);
|
|
|
|
xlsxDoc->isLoadPackage();
|
|
|
|
|
2018-09-27 19:23:21 +09:00
|
|
|
// clear tab widget
|
2018-09-27 19:04:11 +09:00
|
|
|
tabWidget->clear();
|
|
|
|
// Removes all the pages, but does not delete them.
|
2018-09-28 18:08:40 +09:00
|
|
|
// Calling this function is equivalent to calling removeTab()
|
|
|
|
// until the tab widget is empty.
|
2018-09-27 17:41:21 +09:00
|
|
|
|
2018-09-28 18:08:40 +09:00
|
|
|
// clear sub-items of every tab
|
2018-09-28 21:20:29 +09:00
|
|
|
foreach ( XlsxTab* ptrTab, xlsxTabList )
|
|
|
|
{
|
2018-09-28 18:08:40 +09:00
|
|
|
if ( NULL == ptrTab )
|
|
|
|
continue;
|
|
|
|
delete ptrTab;
|
|
|
|
}
|
|
|
|
xlsxTabList.clear();
|
2018-09-27 19:08:49 +09:00
|
|
|
|
2018-09-28 18:08:40 +09:00
|
|
|
int sheetIndexNumber = 0;
|
2018-10-04 17:35:49 +09:00
|
|
|
int activeSheetNumber = -1;
|
|
|
|
|
|
|
|
AbstractSheet* activeSheet = xlsxDoc->workbook()->activeSheet();
|
|
|
|
// NOTICE: active sheet is lastest sheet. It's not focused sheet.
|
|
|
|
|
2018-09-28 18:08:40 +09:00
|
|
|
foreach( QString curretnSheetName, xlsxDoc->sheetNames() )
|
|
|
|
{
|
|
|
|
QXlsx::AbstractSheet* currentSheet = xlsxDoc->sheet( curretnSheetName );
|
|
|
|
if ( NULL == currentSheet )
|
|
|
|
continue;
|
2018-09-27 17:41:21 +09:00
|
|
|
|
2018-10-04 17:35:49 +09:00
|
|
|
if ( activeSheet == currentSheet )
|
|
|
|
{
|
|
|
|
// current sheet is active sheet.
|
|
|
|
activeSheetNumber = sheetIndexNumber;
|
|
|
|
}
|
|
|
|
|
2018-09-28 18:08:40 +09:00
|
|
|
XlsxTab* newSheet = new XlsxTab( this, currentSheet, sheetIndexNumber ); // create new tab
|
|
|
|
xlsxTabList.push_back( newSheet ); // append to xlsx pointer list
|
2018-10-03 20:04:33 +09:00
|
|
|
tabWidget->addTab( newSheet, curretnSheetName ); // add tab widget
|
|
|
|
sheetIndexNumber++; // increase sheet index number
|
2018-09-27 17:41:21 +09:00
|
|
|
}
|
|
|
|
|
2018-10-04 17:35:49 +09:00
|
|
|
if ( (-1) != activeSheetNumber )
|
|
|
|
tabWidget->setCurrentIndex(activeSheetNumber);
|
|
|
|
|
2018-09-28 18:08:40 +09:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::on_action_About_triggered()
|
|
|
|
{
|
2018-09-30 22:08:38 +09:00
|
|
|
QString text = "QXlsx<br />"
|
|
|
|
"<a href=\"https://github.com/j2doll/QXlsx\">https://github.com/j2doll/QXlsx</a><br />"
|
|
|
|
"MIT License<br />" ;
|
2018-10-04 13:24:28 +09:00
|
|
|
QMessageBox::about(this, "QXlsx", text);
|
2018-09-28 21:20:29 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::on_action_New_triggered()
|
|
|
|
{
|
2018-10-04 13:24:28 +09:00
|
|
|
// TODO: new document
|
2018-09-28 21:20:29 +09:00
|
|
|
QMessageBox msgBox;
|
|
|
|
msgBox.setText( "New" );
|
2018-09-28 18:08:40 +09:00
|
|
|
msgBox.exec();
|
2018-09-27 17:41:21 +09:00
|
|
|
}
|
2018-09-28 21:20:29 +09:00
|
|
|
|
|
|
|
void MainWindow::on_action_Save_triggered()
|
|
|
|
{
|
2018-10-04 13:24:28 +09:00
|
|
|
// TODO: save document
|
2018-09-28 21:20:29 +09:00
|
|
|
QMessageBox msgBox;
|
|
|
|
msgBox.setText( "Save" );
|
|
|
|
msgBox.exec();
|
|
|
|
}
|
|
|
|
|
2018-10-04 13:24:28 +09:00
|
|
|
void MainWindow::on_action_Print_triggered()
|
|
|
|
{
|
|
|
|
if ( NULL == xlsxDoc )
|
|
|
|
return;
|
|
|
|
|
|
|
|
QPrintPreviewDialog dialog;
|
|
|
|
connect(&dialog, SIGNAL(paintRequested(QPrinter*)), this, SLOT(print(QPrinter*)));
|
|
|
|
dialog.exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::print(QPrinter *printer)
|
|
|
|
{
|
|
|
|
if ( NULL == xlsxDoc )
|
|
|
|
return;
|
|
|
|
|
2018-10-04 17:35:49 +09:00
|
|
|
Worksheet* wsheet = (Worksheet*) xlsxDoc->workbook()->activeSheet();
|
|
|
|
if ( NULL == wsheet )
|
|
|
|
return;
|
2018-10-04 13:24:28 +09:00
|
|
|
|
2018-10-04 17:35:49 +09:00
|
|
|
QList<QString> colTitle;
|
2018-10-04 13:24:28 +09:00
|
|
|
QList<VLIST> xlsxData;
|
|
|
|
QVector<int> printColumnStretch;
|
|
|
|
int sheetIndexNumber = 0;
|
|
|
|
|
2018-10-04 17:35:49 +09:00
|
|
|
int maxRow = -1;
|
|
|
|
int maxCol = -1;
|
|
|
|
QVector<CellLocation> clList = wsheet->getFullCells( &maxRow, &maxCol );
|
|
|
|
|
|
|
|
QString arr[maxRow][maxCol];
|
2018-10-04 13:24:28 +09:00
|
|
|
|
2018-10-04 17:35:49 +09:00
|
|
|
for ( int ic = 0; ic < clList.size(); ++ic )
|
|
|
|
{
|
|
|
|
CellLocation cl = clList.at(ic);
|
2018-10-04 13:24:28 +09:00
|
|
|
|
2018-10-04 17:35:49 +09:00
|
|
|
int row = cl.row;
|
|
|
|
int col = cl.col;
|
2018-10-04 13:24:28 +09:00
|
|
|
|
2018-10-04 17:35:49 +09:00
|
|
|
QSharedPointer<Cell> ptrCell = cl.cell; // cell pointer
|
2018-10-04 13:24:28 +09:00
|
|
|
|
2018-10-04 17:35:49 +09:00
|
|
|
QString strValue = ptrCell->value().toString();
|
2018-10-04 13:24:28 +09:00
|
|
|
|
2018-10-04 17:35:49 +09:00
|
|
|
arr[row - 1][col - 1] = strValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int ir = 0 ; ir < maxRow; ir++)
|
|
|
|
{
|
|
|
|
VLIST vl;
|
2018-10-04 13:24:28 +09:00
|
|
|
|
2018-10-04 17:35:49 +09:00
|
|
|
for (int ic = 0 ; ic < maxCol; ic++)
|
|
|
|
{
|
|
|
|
QString strValue = arr[ir][ic];
|
|
|
|
if ( strValue.isNull() )
|
|
|
|
strValue = QString("");
|
2018-10-04 13:24:28 +09:00
|
|
|
|
2018-10-04 17:35:49 +09:00
|
|
|
vl.append( strValue );
|
2018-10-04 13:24:28 +09:00
|
|
|
}
|
|
|
|
|
2018-10-04 17:35:49 +09:00
|
|
|
xlsxData.append( vl );
|
|
|
|
}
|
2018-10-04 13:24:28 +09:00
|
|
|
|
2018-10-04 17:35:49 +09:00
|
|
|
QVector<QString> printHeaders;
|
2018-10-04 13:24:28 +09:00
|
|
|
|
2018-10-04 17:35:49 +09:00
|
|
|
for (int ic = 0 ; ic < maxCol; ic++)
|
|
|
|
{
|
|
|
|
QString strCol = QString("%1").arg(ic + 1);
|
|
|
|
colTitle.append( strCol );
|
2018-10-04 13:24:28 +09:00
|
|
|
|
2018-10-04 17:35:49 +09:00
|
|
|
printHeaders.append( strCol );
|
2018-10-04 13:24:28 +09:00
|
|
|
|
2018-10-04 17:35:49 +09:00
|
|
|
printColumnStretch.append( wsheet->columnWidth( (ic + 1) ) ); // TODO: check this code
|
2018-10-04 13:24:28 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
XlsxTableModel xlsxTableModel(colTitle, xlsxData);
|
|
|
|
|
2018-10-04 17:35:49 +09:00
|
|
|
/*
|
|
|
|
for ( int j = 0; j < maxRow; j++ )
|
|
|
|
{
|
|
|
|
qDebug() << "===================";
|
|
|
|
for( int i = 0; i < maxCol; i++ )
|
|
|
|
{
|
|
|
|
QString strVal = xlsxTableModel.data( xlsxTableModel.index(j, i), Qt::DisplayRole).toString();
|
|
|
|
qDebug() << strVal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2018-10-04 13:24:28 +09:00
|
|
|
QPainter painter;
|
|
|
|
if ( !painter.begin(printer) )
|
|
|
|
{
|
|
|
|
QMessageBox msgBox;
|
|
|
|
msgBox.setText( "Can't start printer" );
|
2018-10-04 17:35:49 +09:00
|
|
|
msgBox.setIcon( QMessageBox::Critical );
|
2018-10-04 13:24:28 +09:00
|
|
|
msgBox.exec();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// print table
|
|
|
|
TablePrinter tablePrinter(&painter, printer);
|
2018-10-04 17:35:49 +09:00
|
|
|
if(!tablePrinter.printTable( &xlsxTableModel, printColumnStretch, printHeaders ))
|
2018-10-04 13:24:28 +09:00
|
|
|
{
|
|
|
|
QMessageBox msgBox;
|
|
|
|
msgBox.setText( tablePrinter.lastError() );
|
2018-10-04 17:35:49 +09:00
|
|
|
msgBox.setIcon( QMessageBox::Warning );
|
2018-10-04 13:24:28 +09:00
|
|
|
msgBox.exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
painter.end();
|
|
|
|
}
|