1
0
mirror of https://github.com/QtExcel/QXlsx.git synced 2025-01-16 04:42:53 +08:00
QXlsx/Copycat/XlsxTab.cpp

159 lines
4.3 KiB
C++
Raw Normal View History

2018-09-27 17:41:21 +09:00
// XlsxTab.cpp
2018-09-30 22:08:38 +09:00
#include <QtGlobal>
#include <QObject>
#include <QString>
#include <QSharedPointer>
2018-09-28 18:08:40 +09:00
#include <QLayout>
#include <QVBoxLayout>
#include <QVariant>
2018-09-30 22:08:38 +09:00
#include <QFont>
2018-09-28 18:08:40 +09:00
#include <QDebug>
2018-09-30 22:08:38 +09:00
#include "XlsxTab.h"
#include "xlsxcell.h"
2018-09-28 18:08:40 +09:00
XlsxTab::XlsxTab(QWidget* parent,
QXlsx::AbstractSheet* ptrSheet,
int SheetIndex)
2018-09-27 17:41:21 +09:00
: QWidget(parent)
{
table = NULL;
2018-09-28 18:08:40 +09:00
sheet = NULL;
sheetIndex = -1;
if ( NULL != ptrSheet )
{
table = new QTableWidget;
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(table);
this->setLayout(layout);
2018-09-27 17:41:21 +09:00
2018-09-28 18:08:40 +09:00
sheet = ptrSheet; // set sheet data
sheetIndex = SheetIndex; // set shett index
2018-09-28 21:20:29 +09:00
if ( ! setSheet() )
{
}
2018-09-27 17:41:21 +09:00
}
}
XlsxTab::~XlsxTab()
{
if ( NULL != table )
{
table->deleteLater();
}
}
2018-09-28 18:08:40 +09:00
bool XlsxTab::setSheet()
{
if ( NULL == sheet )
return false;
if ( NULL == table )
return false;
2018-09-28 21:20:29 +09:00
// set active sheet
2018-09-28 18:08:40 +09:00
sheet->workbook()->setActiveSheet( sheetIndex );
Worksheet* wsheet = (Worksheet*) sheet->workbook()->activeSheet();
if ( NULL == wsheet )
return false;
2018-09-28 21:20:29 +09:00
// get full cells of sheet
2018-09-28 18:08:40 +09:00
int maxRow = -1;
int maxCol = -1;
2018-09-28 21:20:29 +09:00
QVector<CellLocation> clList = wsheet->getFullCells( &maxRow, &maxCol );
// set max count of row,col
table->setRowCount( maxRow );
table->setColumnCount( maxCol );
for ( int ic = 0; ic < clList.size(); ++ic )
{
2018-09-28 18:08:40 +09:00
CellLocation cl = clList.at(ic);
2018-09-28 21:20:29 +09:00
// First cell of tableWidget is 0.
// But first cell of Qxlsx document is 1.
2018-09-28 18:08:40 +09:00
int row = cl.row - 1;
int col = cl.col - 1;
2018-09-30 22:08:38 +09:00
QSharedPointer<Cell> ptrCell = cl.cell;
2018-09-28 18:08:40 +09:00
QVariant var = cl.cell.data()->value();
QString str = var.toString();
QTableWidgetItem *newItem = new QTableWidgetItem(str);
2018-09-30 22:08:38 +09:00
// set item
table->setItem( row, col, newItem );
// font
newItem->setFont( ptrCell->format().font() );
// font color
newItem->setTextColor( ptrCell->format().fontColor() );
// ptrCell->format().patternBackgroundColor()
// ptrCell->format().patternForegroundColor()
// newItem->setBackgroundColor( ptrCell->format().patternBackgroundColor() );
// newItem->setBackgroundColor( ptrCell->format().patternForegroundColor() );
// newItem->setBackgroundColor( ptrCell->format().diagonalBorderColor() );
// alignment
int alignment = 0;
Format::HorizontalAlignment ha = ptrCell->format().horizontalAlignment();
switch(ha)
{
case Format::HorizontalAlignment::AlignHCenter :
case Format::HorizontalAlignment::AlignHFill :
case Format::HorizontalAlignment::AlignHMerge :
case Format::HorizontalAlignment::AlignHDistributed :
alignment = alignment | Qt::AlignHCenter;
break;
case Format::HorizontalAlignment::AlignRight :
alignment = alignment | Qt::AlignRight;
break;
case Format::HorizontalAlignment::AlignHJustify :
alignment = alignment | Qt::AlignJustify;
break;
case Format::HorizontalAlignment::AlignLeft :
case Format::HorizontalAlignment::AlignHGeneral :
default:
alignment = alignment | Qt::AlignLeft;
break;
}
Format::VerticalAlignment va = ptrCell->format().verticalAlignment();
switch(va)
{
case Format::VerticalAlignment::AlignTop :
alignment = alignment | Qt::AlignTop;
break;
case Format::VerticalAlignment::AlignVCenter :
alignment = alignment | Qt::AlignVCenter;
break;
case Format::VerticalAlignment::AlignBottom :
alignment = alignment | Qt::AlignBottom;
break;
case Format::VerticalAlignment::AlignVJustify :
case Format::VerticalAlignment::AlignVDistributed :
default:
alignment = alignment | Qt::AlignBaseline;
break;
}
newItem->setTextAlignment( alignment );
2018-09-28 18:08:40 +09:00
}
return true;
}