Add cell alignment control
This commit is contained in:
@@ -17,6 +17,7 @@ int main()
|
|||||||
QXlsx::Format *format1 = workbook.addFormat();
|
QXlsx::Format *format1 = workbook.addFormat();
|
||||||
format1->setFontColor(QColor(Qt::red));
|
format1->setFontColor(QColor(Qt::red));
|
||||||
format1->setFontSize(15);
|
format1->setFontSize(15);
|
||||||
|
format1->setHorizontalAlignment(QXlsx::Format::AlignHCenter);
|
||||||
sheet->write("A1", "Hello Qt!", format1);
|
sheet->write("A1", "Hello Qt!", format1);
|
||||||
sheet->write("B3", 12345, format1);
|
sheet->write("B3", 12345, format1);
|
||||||
|
|
||||||
|
|||||||
@@ -46,6 +46,13 @@ Format::Format()
|
|||||||
m_font.redundant = false;
|
m_font.redundant = false;
|
||||||
m_font.index = 0;
|
m_font.index = 0;
|
||||||
|
|
||||||
|
m_alignment.alignH = AlignHGeneral;
|
||||||
|
m_alignment.alignV = AlignBottom;
|
||||||
|
m_alignment.wrap = false;
|
||||||
|
m_alignment.rotation = 0;
|
||||||
|
m_alignment.indent = 0;
|
||||||
|
m_alignment.shinkToFit = false;
|
||||||
|
|
||||||
m_is_dxf_fomat = false;
|
m_is_dxf_fomat = false;
|
||||||
|
|
||||||
m_xf_index = 0;
|
m_xf_index = 0;
|
||||||
@@ -152,6 +159,156 @@ void Format::setFontName(const QString &name)
|
|||||||
m_font.name = name;
|
m_font.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Format::HorizontalAlignment Format::horizontalAlignment() const
|
||||||
|
{
|
||||||
|
return m_alignment.alignH;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Format::setHorizontalAlignment(HorizontalAlignment align)
|
||||||
|
{
|
||||||
|
if (m_alignment.indent &&(align != AlignHGeneral && align != AlignLeft &&
|
||||||
|
align != AlignRight && align != AlignHDistributed)) {
|
||||||
|
m_alignment.indent = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_alignment.shinkToFit && (align == AlignHFill || align == AlignHJustify
|
||||||
|
|| align == AlignHDistributed)) {
|
||||||
|
m_alignment.shinkToFit = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_alignment.alignH = align;
|
||||||
|
}
|
||||||
|
|
||||||
|
Format::VerticalAlignment Format::verticalAlignment() const
|
||||||
|
{
|
||||||
|
return m_alignment.alignV;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Format::setVerticalAlignment(VerticalAlignment align)
|
||||||
|
{
|
||||||
|
m_alignment.alignV = align;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Format::textWrap() const
|
||||||
|
{
|
||||||
|
return m_alignment.wrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Format::setTextWarp(bool wrap)
|
||||||
|
{
|
||||||
|
if (wrap && m_alignment.shinkToFit)
|
||||||
|
m_alignment.shinkToFit = false;
|
||||||
|
|
||||||
|
m_alignment.wrap = wrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Format::rotation() const
|
||||||
|
{
|
||||||
|
return m_alignment.rotation;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Format::setRotation(int rotation)
|
||||||
|
{
|
||||||
|
m_alignment.rotation = rotation;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Format::indent() const
|
||||||
|
{
|
||||||
|
return m_alignment.indent;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Format::setIndent(int indent)
|
||||||
|
{
|
||||||
|
if (indent && (m_alignment.alignH != AlignHGeneral
|
||||||
|
&& m_alignment.alignH != AlignLeft
|
||||||
|
&& m_alignment.alignH != AlignRight
|
||||||
|
&& m_alignment.alignH != AlignHJustify)) {
|
||||||
|
m_alignment.alignH = AlignLeft;
|
||||||
|
}
|
||||||
|
m_alignment.indent = indent;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Format::shrinkToFit() const
|
||||||
|
{
|
||||||
|
return m_alignment.shinkToFit;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Format::setShrinkToFit(bool shink)
|
||||||
|
{
|
||||||
|
if (shink && m_alignment.wrap)
|
||||||
|
m_alignment.wrap = false;
|
||||||
|
if (shink && (m_alignment.alignH == AlignHFill
|
||||||
|
|| m_alignment.alignH == AlignHJustify
|
||||||
|
|| m_alignment.alignH == AlignHDistributed)) {
|
||||||
|
m_alignment.alignH = AlignLeft;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_alignment.shinkToFit = shink;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Format::alignmentChanged() const
|
||||||
|
{
|
||||||
|
return m_alignment.alignH != AlignHGeneral
|
||||||
|
|| m_alignment.alignV != AlignBottom
|
||||||
|
|| m_alignment.indent != 0
|
||||||
|
|| m_alignment.wrap
|
||||||
|
|| m_alignment.rotation != 0
|
||||||
|
|| m_alignment.shinkToFit;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString Format::horizontalAlignmentString() const
|
||||||
|
{
|
||||||
|
QString alignH;
|
||||||
|
switch (m_alignment.alignH) {
|
||||||
|
case Format::AlignLeft:
|
||||||
|
alignH = "left";
|
||||||
|
break;
|
||||||
|
case Format::AlignHCenter:
|
||||||
|
alignH = "center";
|
||||||
|
break;
|
||||||
|
case Format::AlignRight:
|
||||||
|
alignH = "right";
|
||||||
|
break;
|
||||||
|
case Format::AlignHFill:
|
||||||
|
alignH = "fill";
|
||||||
|
break;
|
||||||
|
case Format::AlignHJustify:
|
||||||
|
alignH = "justify";
|
||||||
|
break;
|
||||||
|
case Format::AlignHMerge:
|
||||||
|
alignH = "centerContinuous";
|
||||||
|
break;
|
||||||
|
case Format::AlignHDistributed:
|
||||||
|
alignH = "distributed";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return alignH;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString Format::verticalAlignmentString() const
|
||||||
|
{
|
||||||
|
QString align;
|
||||||
|
switch (m_alignment.alignV) {
|
||||||
|
case AlignTop:
|
||||||
|
align = "top";
|
||||||
|
break;
|
||||||
|
case AlignVCenter:
|
||||||
|
align = "center";
|
||||||
|
break;
|
||||||
|
case AlignVJustify:
|
||||||
|
align = "justify";
|
||||||
|
break;
|
||||||
|
case AlignVDistributed:
|
||||||
|
align = "distributed";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return align;
|
||||||
|
}
|
||||||
|
|
||||||
bool Format::isDxfFormat() const
|
bool Format::isDxfFormat() const
|
||||||
{
|
{
|
||||||
return m_is_dxf_fomat;
|
return m_is_dxf_fomat;
|
||||||
|
|||||||
+49
-2
@@ -52,6 +52,27 @@ public:
|
|||||||
FontUnderlineDoubleAccounting
|
FontUnderlineDoubleAccounting
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum HorizontalAlignment
|
||||||
|
{
|
||||||
|
AlignHGeneral,
|
||||||
|
AlignLeft,
|
||||||
|
AlignHCenter,
|
||||||
|
AlignRight,
|
||||||
|
AlignHFill,
|
||||||
|
AlignHJustify,
|
||||||
|
AlignHMerge,
|
||||||
|
AlignHDistributed
|
||||||
|
};
|
||||||
|
|
||||||
|
enum VerticalAlignment
|
||||||
|
{
|
||||||
|
AlignTop,
|
||||||
|
AlignVCenter,
|
||||||
|
AlignBottom,
|
||||||
|
AlignVJustify,
|
||||||
|
AlignVDistributed
|
||||||
|
};
|
||||||
|
|
||||||
int fontSize() const;
|
int fontSize() const;
|
||||||
void setFontSize(int size);
|
void setFontSize(int size);
|
||||||
bool fontItalic() const;
|
bool fontItalic() const;
|
||||||
@@ -71,15 +92,28 @@ public:
|
|||||||
QString fontName() const;
|
QString fontName() const;
|
||||||
void setFontName(const QString &);
|
void setFontName(const QString &);
|
||||||
|
|
||||||
|
HorizontalAlignment horizontalAlignment() const;
|
||||||
|
void setHorizontalAlignment(HorizontalAlignment align);
|
||||||
|
VerticalAlignment verticalAlignment() const;
|
||||||
|
void setVerticalAlignment(VerticalAlignment align);
|
||||||
|
bool textWrap() const;
|
||||||
|
void setTextWarp(bool textWrap);
|
||||||
|
int rotation() const;
|
||||||
|
void setRotation(int rotation);
|
||||||
|
int indent() const;
|
||||||
|
void setIndent(int indent);
|
||||||
|
bool shrinkToFit() const;
|
||||||
|
void setShrinkToFit(bool shink);
|
||||||
|
|
||||||
void setForegroundColor(const QColor &color);
|
void setForegroundColor(const QColor &color);
|
||||||
void setBackgroundColor(const QColor &color);
|
void setBackgroundColor(const QColor &color);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class Styles;
|
friend class Styles;
|
||||||
friend class Worksheet;
|
friend class Worksheet;
|
||||||
explicit Format();
|
Format();
|
||||||
|
|
||||||
struct Font
|
struct FontData
|
||||||
{
|
{
|
||||||
int size;
|
int size;
|
||||||
bool italic;
|
bool italic;
|
||||||
@@ -109,6 +143,19 @@ private:
|
|||||||
bool fontShadow() const {return m_font.shadow;}
|
bool fontShadow() const {return m_font.shadow;}
|
||||||
QString fontScheme() const {return m_font.scheme;}
|
QString fontScheme() const {return m_font.scheme;}
|
||||||
|
|
||||||
|
struct AlignmentData
|
||||||
|
{
|
||||||
|
HorizontalAlignment alignH;
|
||||||
|
VerticalAlignment alignV;
|
||||||
|
bool wrap;
|
||||||
|
int rotation;
|
||||||
|
int indent;
|
||||||
|
bool shinkToFit;
|
||||||
|
} m_alignment;
|
||||||
|
|
||||||
|
bool alignmentChanged() const;
|
||||||
|
QString horizontalAlignmentString() const;
|
||||||
|
QString verticalAlignmentString() const;
|
||||||
|
|
||||||
bool isDxfFormat() const;
|
bool isDxfFormat() const;
|
||||||
int xfIndex() const {return m_xf_index;}
|
int xfIndex() const {return m_xf_index;}
|
||||||
|
|||||||
@@ -238,6 +238,27 @@ void Styles::writeCellXfs(XmlStreamWriter &writer)
|
|||||||
writer.writeAttribute("applyFont", "1");
|
writer.writeAttribute("applyFont", "1");
|
||||||
if (format->fillIndex() > 0)
|
if (format->fillIndex() > 0)
|
||||||
writer.writeAttribute("applyBorder", "1");
|
writer.writeAttribute("applyBorder", "1");
|
||||||
|
if (format->alignmentChanged())
|
||||||
|
writer.writeAttribute("applyAlignment", "1");
|
||||||
|
|
||||||
|
if (format->alignmentChanged()) {
|
||||||
|
writer.writeEmptyElement("alignment");
|
||||||
|
QString alignH = format->horizontalAlignmentString();
|
||||||
|
if (!alignH.isEmpty())
|
||||||
|
writer.writeAttribute("horizontal", alignH);
|
||||||
|
QString alignV = format->verticalAlignmentString();
|
||||||
|
if (!alignV.isEmpty())
|
||||||
|
writer.writeAttribute("vertical", alignV);
|
||||||
|
if (format->indent())
|
||||||
|
writer.writeAttribute("indent", QString::number(format->indent()));
|
||||||
|
if (format->textWrap())
|
||||||
|
writer.writeAttribute("wrapText", "1");
|
||||||
|
if (format->shrinkToFit())
|
||||||
|
writer.writeAttribute("shrinkToFit", "1");
|
||||||
|
if (format->shrinkToFit())
|
||||||
|
writer.writeAttribute("shrinkToFit", "1");
|
||||||
|
}
|
||||||
|
|
||||||
writer.writeEndElement();//xf
|
writer.writeEndElement();//xf
|
||||||
}
|
}
|
||||||
writer.writeEndElement();//cellXfs
|
writer.writeEndElement();//cellXfs
|
||||||
|
|||||||
@@ -33,6 +33,7 @@
|
|||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
#include <QPoint>
|
#include <QPoint>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user