1
0
mirror of https://github.com/QtExcel/QXlsx.git synced 2025-01-30 05:02:52 +08:00
QXlsx/Copycat/XlsxTab.cpp

251 lines
7.7 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-10-03 20:04:33 +09:00
#include <QBrush>
2018-09-28 18:08:40 +09:00
#include <QDebug>
#include <QTextCharFormat>
2018-09-28 18:08:40 +09:00
2018-09-30 22:08:38 +09:00
#include "XlsxTab.h"
#include "xlsxcell.h"
2018-12-18 18:20:27 +09:00
#include "xlsxformat.h"
2018-09-30 22:08:38 +09:00
2018-09-28 18:08:40 +09:00
XlsxTab::XlsxTab(QWidget* parent,
2018-12-18 18:20:27 +09:00
QXlsx::Document* ptrDoc,
2018-09-28 18:08:40 +09:00
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;
2018-10-03 20:04:33 +09:00
if ( NULL == ptrSheet )
return;
2018-09-28 18:08:40 +09:00
2018-10-03 20:04:33 +09:00
table = new QTableWidget;
2018-09-27 17:41:21 +09:00
2018-10-03 20:04:33 +09:00
// set layout
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(table);
this->setLayout(layout);
2018-09-28 21:20:29 +09:00
2018-12-18 18:20:27 +09:00
document = ptrDoc; // set document
2018-10-03 20:04:33 +09:00
sheet = ptrSheet; // set sheet data
sheetIndex = SheetIndex; // set shett index
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
2018-10-21 21:34:06 +09:00
// NOTE: This part should be modified later.
// The maximum value of the sheet should be set to an appropriate value.
2018-09-28 21:20:29 +09:00
table->setRowCount( maxRow );
table->setColumnCount( maxCol );
for ( int ic = 0; ic < clList.size(); ++ic )
{
2018-10-21 21:34:06 +09:00
// cell location
2018-09-28 18:08:40 +09:00
CellLocation cl = clList.at(ic);
2018-10-21 21:34:06 +09:00
////////////////////////////////////////////////////////////////////
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-10-03 20:04:33 +09:00
2018-10-21 21:34:06 +09:00
////////////////////////////////////////////////////////////////////
// cell pointer
QSharedPointer<Cell> ptrCell = cl.cell;
2018-10-03 20:04:33 +09:00
2018-10-21 21:34:06 +09:00
////////////////////////////////////////////////////////////////////
2018-10-03 20:04:33 +09:00
// create new item of table widget
QTableWidgetItem* newItem = new QTableWidgetItem();
2018-09-28 18:08:40 +09:00
2018-10-21 21:34:06 +09:00
///////////////////////////////////////////////////////////////////
// value of cell
2018-09-28 18:08:40 +09:00
QVariant var = cl.cell.data()->value();
QString str = var.toString();
2018-10-21 21:34:06 +09:00
////////////////////////////////////////////////////////////////////
2018-10-03 20:04:33 +09:00
// set text
newItem->setText( str );
2018-09-30 22:08:38 +09:00
2018-10-21 21:34:06 +09:00
////////////////////////////////////////////////////////////////////
2018-09-30 22:08:38 +09:00
// set item
table->setItem( row, col, newItem );
2018-10-21 21:34:06 +09:00
////////////////////////////////////////////////////////////////////
// row height and column width
double dRowHeight = wsheet->rowHeight( cl.row );
double dColWidth = wsheet->columnWidth( cl.col );
// dRowHeight = dRowHeight * double(2.0);
// dColWidth = dColWidth * double(2.0);
// TODO: define ratio of widget col/row
2018-12-18 18:20:27 +09:00
// table->setRowHeight( row, dRowHeight );
// table->setColumnWidth( col, dColWidth );
2018-10-21 21:34:06 +09:00
////////////////////////////////////////////////////////////////////
2018-09-30 22:08:38 +09:00
// font
newItem->setFont( ptrCell->format().font() );
2018-10-21 21:34:06 +09:00
////////////////////////////////////////////////////////////////////
2018-09-30 22:08:38 +09:00
// font color
2018-10-03 20:04:33 +09:00
if ( ptrCell->format().fontColor().isValid() )
{
newItem->setTextColor( ptrCell->format().fontColor() );
}
2018-09-30 22:08:38 +09:00
2018-12-18 18:20:27 +09:00
////////////////////////////////////////////////////////////////////
// background color
{
QColor clrForeGround = ptrCell->format().patternForegroundColor();
if ( clrForeGround.isValid() )
{
// qDebug() << "[debug] ForeGround : " << clrForeGround;
}
QColor clrBackGround = ptrCell->format().patternBackgroundColor();
if ( clrBackGround.isValid() )
{
// TODO: You must use various patterns.
newItem->setBackground( Qt::SolidPattern );
newItem->setBackgroundColor( clrBackGround );
}
}
2018-10-21 21:34:06 +09:00
////////////////////////////////////////////////////////////////////
2018-10-03 20:04:33 +09:00
// pattern
Format::FillPattern fp = ptrCell->format().fillPattern();
Qt::BrushStyle qbs = Qt::NoBrush;
switch(fp)
{
case Format::PatternNone : qbs = Qt::NoBrush; break;
case Format::PatternSolid : qbs = Qt::SolidPattern; break;
case Format::PatternMediumGray :
case Format::PatternDarkGray :
case Format::PatternLightGray :
case Format::PatternDarkHorizontal :
case Format::PatternDarkVertical :
case Format::PatternDarkDown :
case Format::PatternDarkUp :
case Format::PatternDarkGrid :
case Format::PatternDarkTrellis :
case Format::PatternLightHorizontal :
case Format::PatternLightVertical :
case Format::PatternLightDown :
case Format::PatternLightUp :
case Format::PatternLightTrellis :
case Format::PatternGray125 :
case Format::PatternGray0625 :
case Format::PatternLightGrid :
2018-10-03 20:04:33 +09:00
default:
break;
}
2018-10-21 21:34:06 +09:00
/*
2018-10-03 20:04:33 +09:00
QBrush qbr( ptrCell->format().patternForegroundColor(), qbs );
newItem->setBackground( qbr );
newItem->setBackgroundColor( ptrCell->format().patternBackgroundColor() );
*/
2018-09-30 22:08:38 +09:00
2018-10-21 21:34:06 +09:00
////////////////////////////////////////////////////////////////////
// set alignment
2018-09-30 22:08:38 +09:00
int alignment = 0;
Format::HorizontalAlignment ha = ptrCell->format().horizontalAlignment();
switch(ha)
{
case Format::AlignHCenter :
case Format::AlignHFill :
case Format::AlignHMerge :
case Format::AlignHDistributed :
2018-09-30 22:08:38 +09:00
alignment = alignment | Qt::AlignHCenter;
break;
case Format::AlignRight :
2018-09-30 22:08:38 +09:00
alignment = alignment | Qt::AlignRight;
break;
case Format::AlignHJustify :
2018-09-30 22:08:38 +09:00
alignment = alignment | Qt::AlignJustify;
break;
case Format::AlignLeft :
case Format::AlignHGeneral :
2018-09-30 22:08:38 +09:00
default:
alignment = alignment | Qt::AlignLeft;
break;
}
Format::VerticalAlignment va = ptrCell->format().verticalAlignment();
switch(va)
{
case Format::AlignTop :
2018-09-30 22:08:38 +09:00
alignment = alignment | Qt::AlignTop;
break;
case Format::AlignVCenter :
2018-09-30 22:08:38 +09:00
alignment = alignment | Qt::AlignVCenter;
break;
case Format::AlignBottom :
2018-09-30 22:08:38 +09:00
alignment = alignment | Qt::AlignBottom;
break;
case Format::AlignVJustify :
case Format::AlignVDistributed :
2018-09-30 22:08:38 +09:00
default:
// alignment = alignment | (int)(Qt::AlignBaseline);
alignment = alignment | QTextCharFormat::AlignBaseline;
2018-09-30 22:08:38 +09:00
break;
}
2018-10-21 21:34:06 +09:00
newItem->setTextAlignment( alignment );
////////////////////////////////////////////////////////////////////
2018-10-03 20:04:33 +09:00
2018-09-28 18:08:40 +09:00
}
return true;
}