qt/LedOK/program/etable.cpp
Gangphon 18c2b148d0 qt
2026-01-28 15:44:41 +08:00

647 lines
29 KiB
C++

#include "etable.h"
#include "gutil/qgui.h"
#include "main.h"
#include <QSpinBox>
#include <QLineEdit>
#include <QPainter>
#include <QFontComboBox>
#include <QButtonGroup>
#include <QBuffer>
#include "xlsxdocument.h"
#include "xlsxformat.h"
#include "xlsxworkbook.h"
ETable::ETable(int row, int col, EBase *multiWin) : EBase(multiWin) {
init(row, col);
}
ETable::ETable(const JObj &json, const QString &dir, EBase *multiWin) : EBase(multiWin) {
mType = EBase::Table;
setBaseAttr(json);
name = json["name"].toStr();
direction = json["direction"].toStr();
speed = json["speed"].toInt();
img = QPixmap(dir+"/"+name+".png");
init();
edGridColor->setColor(json["gridColor"].toString());
QXlsx::Document doc(dir+"/"+name+".xlsx");
auto sheet = doc.currentWorksheet();
if(doc.isLoadPackage() && sheet) read(sheet);
else {
table->setRowCount(8);
table->setColumnCount(4);
}
}
void ETable::init(int row, int col) {
mType = EBase::Table;
dlg.setWindowModality(Qt::ApplicationModal);
dlg.resize(1024, 800);
dlg.setWindowTitle(tr("Table Editor"));
auto vBox = new VBox(&dlg);
vBox->setContentsMargins(0, 0, 0, 0);
vBox->setSpacing(4);
table = row==0 ? new PaintTableWidget : new PaintTableWidget(row, col);
table->ebase = this;
auto hBox = new HBox(vBox);
auto vv = new VBox(hBox);
auto hh = new HBox(vv);
auto edFontFamily = new QFontComboBox;
edFontFamily->setEditable(false);
edFontFamily->setCurrentFont(QFont("Calibri"));
connect(edFontFamily, &QFontComboBox::currentFontChanged, table, [=](const QFont &f) {
if(isAutoSetting) return;
auto ranges = table->selectedRanges();
for(auto range : ranges) for(int r=range.topRow(); r<=range.bottomRow(); r++) for(int c=range.leftColumn(); c<=range.rightColumn(); c++) {
auto item = table->itemValid(r, c);
auto ft = item->font();
ft.setFamily(f.family());
item->setFont(ft);
}
});
hh->addWidget(edFontFamily);
hh = new HBox(vv);
auto edFontSize = new QSpinBox;
edFontSize->setRange(4, 9999);
edFontSize->setValue(11);
connect(edFontSize, &QSpinBox::valueChanged, table, [=](int value) {
if(isAutoSetting) return;
if(value <= 0) return;
auto ranges = table->selectedRanges();
for(auto range : ranges) for(int r=range.topRow(); r<=range.bottomRow(); r++) for(int c=range.leftColumn(); c<=range.rightColumn(); c++) {
auto item = table->itemValid(r, c);
auto ft = item->font();
ft.setPointSize(value);
item->setFont(ft);
}
});
hh->addWidget(edFontSize);
auto edFontBold = new QPushButton("B");
edFontBold->setToolTip(tr("Bold"));
edFontBold->setStyleSheet("QPushButton{background: #ddd; color: #444; font-size: 16px; font-weight: bold;} QPushButton:checked{background: #29c; color: #fff;}");
edFontBold->setFixedSize(26, 26);
edFontBold->setCheckable(true);
connect(edFontBold, &QPushButton::toggled, this, [=](bool checked) {
if(isAutoSetting) return;
auto ranges = table->selectedRanges();
for(auto range : ranges) for(int r=range.topRow(); r<=range.bottomRow(); r++) for(int c=range.leftColumn(); c<=range.rightColumn(); c++) {
auto item = table->itemValid(r, c);
auto ft = item->font();
ft.setBold(checked);
item->setFont(ft);
}
});
hh->addWidget(edFontBold);
auto edFontItalic = new QPushButton("I");
edFontItalic->setToolTip(tr("Italic"));
edFontItalic->setStyleSheet("QPushButton{background: #ddd; color: #444; font-size: 16px; font-weight: bold; font-style: italic;} QPushButton:checked{background: #29c; color: #fff;}");
edFontItalic->setFixedSize(26, 26);
edFontItalic->setCheckable(true);
connect(edFontItalic, &QPushButton::toggled, this, [=](bool checked) {
if(isAutoSetting) return;
auto ranges = table->selectedRanges();
for(auto range : ranges) for(int r=range.topRow(); r<=range.bottomRow(); r++) for(int c=range.leftColumn(); c<=range.rightColumn(); c++) {
auto item = table->itemValid(r, c);
auto ft = item->font();
ft.setItalic(checked);
item->setFont(ft);
}
});
hh->addWidget(edFontItalic);
auto edFontUnderline = new QPushButton("U");
edFontUnderline->setToolTip(tr("Underline"));
edFontUnderline->setStyleSheet("QPushButton{background: #ddd; color: #444; font-size: 16px; font-weight: bold; text-decoration: underline;} QPushButton:checked{background: #29c; color: #fff;}");
edFontUnderline->setFixedSize(26, 26);
edFontUnderline->setCheckable(true);
connect(edFontUnderline, &QPushButton::toggled, this, [=](bool checked) {
if(isAutoSetting) return;
auto ranges = table->selectedRanges();
for(auto range : ranges) for(int r=range.topRow(); r<=range.bottomRow(); r++) for(int c=range.leftColumn(); c<=range.rightColumn(); c++) {
auto item = table->itemValid(r, c);
auto ft = item->font();
ft.setUnderline(checked);
item->setFont(ft);
}
});
hh->addWidget(edFontUnderline);
auto edTextColor = new LoColorSelector("T", Qt::white);
edTextColor->setToolTip(tr("Text Color"));
edTextColor->setFixedSize(26, 26);
connect(edTextColor, &LoColorSelector::sColorChanged, this, [=](const QColor &color) {
if(isAutoSetting) return;
if(! color.isValid()) return;
auto ranges = table->selectedRanges();
for(auto range : ranges) for(int r=range.topRow(); r<=range.bottomRow(); r++) for(int c=range.leftColumn(); c<=range.rightColumn(); c++) {
auto item = table->itemValid(r, c);
item->setForeground(color);
}
});
hh->addWidget(edTextColor);
auto edBackColor = new LoColorSelector("B", Qt::transparent);
edBackColor->setToolTip(tr("Back Color"));
edBackColor->setFixedSize(26, 26);
connect(edBackColor, &LoColorSelector::sColorChanged, this, [=](const QColor &color) {
if(isAutoSetting) return;
if(! color.isValid()) return;
auto ranges = table->selectedRanges();
for(auto range : ranges) for(int r=range.topRow(); r<=range.bottomRow(); r++) for(int c=range.leftColumn(); c<=range.rightColumn(); c++) {
auto item = table->itemValid(r, c);
item->setBackground(color);
}
});
hh->addWidget(edBackColor);
edGridColor = new LoColorSelector("G", Qt::white);
edGridColor->setToolTip(tr("Grid Color"));
edGridColor->setFixedSize(26, 26);
connect(edGridColor, &LoColorSelector::sColorChanged, this, [=](const QColor &color) {
if(isAutoSetting) return;
if(! color.isValid()) return;
table->setStyleSheet("QTableWidget { gridline-color: "+color.name()+";}");
});
hh->addWidget(edGridColor);
auto line = new QFrame;
line->setFrameShape(QFrame::VLine);
line->setFrameShadow(QFrame::Sunken);
hBox->addWidget(line, 1);
vv = new VBox(hBox);
hh = new HBox(vv);
auto edAlignHL = new QPushButton(QIcon(":/res/program/TextAlignHL.png"), "");
edAlignHL->setStyleSheet("QPushButton{border: none; background: #bbb;} QPushButton:checked{background: #29c;}");
edAlignHL->setFixedSize(26, 26);
edAlignHL->setIconSize(QSize(26, 26));
edAlignHL->setCheckable(true);
hh->addWidget(edAlignHL);
auto edAlignHC = new QPushButton(QIcon(":/res/program/TextAlignHC.png"), "");
edAlignHC->setStyleSheet("QPushButton{border: none; background: #bbb;} QPushButton:checked{background: #29c;}");
edAlignHC->setFixedSize(26, 26);
edAlignHC->setIconSize(QSize(26, 26));
edAlignHC->setCheckable(true);
hh->addWidget(edAlignHC);
auto edAlignHR = new QPushButton(QIcon(":/res/program/TextAlignHR.png"), "");
edAlignHR->setStyleSheet("QPushButton{border: none; background: #bbb;} QPushButton:checked{background: #29c;}");
edAlignHR->setFixedSize(26, 26);
edAlignHR->setIconSize(QSize(26, 26));
edAlignHR->setCheckable(true);
hh->addWidget(edAlignHR);
hh = new HBox(vv);
auto edAlignVT = new QPushButton(QIcon(":/res/program/TextAlignVT.png"), "");
edAlignVT->setStyleSheet("QPushButton{border: none; background: #bbb;} QPushButton:checked{background: #29c;}");
edAlignVT->setFixedSize(26, 26);
edAlignVT->setIconSize(QSize(26, 26));
edAlignVT->setCheckable(true);
hh->addWidget(edAlignVT);
auto edAlignVC = new QPushButton(QIcon(":/res/program/TextAlignVC.png"), "");
edAlignVC->setStyleSheet("QPushButton{border: none; background: #bbb;} QPushButton:checked{background: #29c;}");
edAlignVC->setFixedSize(26, 26);
edAlignVC->setIconSize(QSize(26, 26));
edAlignVC->setCheckable(true);
hh->addWidget(edAlignVC);
auto edAlignVB = new QPushButton(QIcon(":/res/program/TextAlignVB.png"), "");
edAlignVB->setStyleSheet("QPushButton{border: none; background: #bbb;} QPushButton:checked{background: #29c;}");
edAlignVB->setFixedSize(26, 26);
edAlignVB->setIconSize(QSize(26, 26));
edAlignVB->setCheckable(true);
hh->addWidget(edAlignVB);
auto edAlignH = new QButtonGroup(this);
edAlignH->addButton(edAlignHL, Qt::AlignLeft);
edAlignH->addButton(edAlignHC, Qt::AlignHCenter);
edAlignH->addButton(edAlignHR, Qt::AlignRight);
connect(edAlignH, &QButtonGroup::idClicked, this, [=](int value) {
if(isAutoSetting) return;
auto ranges = table->selectedRanges();
for(auto range : ranges) for(int r=range.topRow(); r<=range.bottomRow(); r++) for(int c=range.leftColumn(); c<=range.rightColumn(); c++) {
auto item = table->itemValid(r, c);
auto align = item->textAlignment();
align = (align & Qt::AlignVertical_Mask) | value;
item->setTextAlignment(align);
}
});
auto edAlignV = new QButtonGroup(this);
edAlignV->addButton(edAlignVT, Qt::AlignTop);
edAlignV->addButton(edAlignVC, Qt::AlignVCenter);
edAlignV->addButton(edAlignVB, Qt::AlignBottom);
connect(edAlignV, &QButtonGroup::idClicked, this, [=](int value) {
if(isAutoSetting) return;
auto ranges = table->selectedRanges();
for(auto range : ranges) for(int r=range.topRow(); r<=range.bottomRow(); r++) for(int c=range.leftColumn(); c<=range.rightColumn(); c++) {
auto item = table->itemValid(r, c);
auto align = item->textAlignment();
align = (align & Qt::AlignHorizontal_Mask) | value;
item->setTextAlignment(align);
}
});
line = new QFrame;
line->setFrameShape(QFrame::VLine);
line->setFrameShadow(QFrame::Sunken);
hBox->addWidget(line, 1);
auto grid = new Grid(hBox);
grid->addLabel(tr("Col Width"), 0, 0, Qt::AlignRight|Qt::AlignVCenter);
grid->addLabel(tr("Row Height"), 1, 0, Qt::AlignRight|Qt::AlignVCenter);
auto edColWidth = new QSpinBox;
edColWidth->setRange(4, 9999);
edColWidth->setValue(64);
connect(edColWidth, &QSpinBox::valueChanged, table, [=](int value) {
if(isAutoSetting) return;
if(value <= 0) return;
auto ranges = table->selectedRanges();
for(auto range : ranges) for(int c=range.leftColumn(); c<=range.rightColumn(); c++) {
table->setColumnWidth(c, value);
}
});
grid->addWidget(edColWidth, 0, 1);
auto edRowHeight = new QSpinBox;
edRowHeight->setRange(4, 9999);
edRowHeight->setValue(18);
connect(edRowHeight, &QSpinBox::valueChanged, table, [=](int value) {
if(isAutoSetting) return;
if(value <= 0) return;
auto ranges = table->selectedRanges();
for(auto range : ranges) for(int r=range.topRow(); r<=range.bottomRow(); r++) {
table->setRowHeight(r, value);
}
});
grid->addWidget(edRowHeight, 1, 1);
line = new QFrame;
line->setFrameShape(QFrame::VLine);
line->setFrameShadow(QFrame::Sunken);
hBox->addWidget(line, 1);
grid = new Grid(hBox);
auto edInsRow = new QPushButton(tr("Insert Row"));
connect(edInsRow, &QPushButton::clicked, table, [=] {
auto ranges = table->selectedRanges();
table->insertRow(ranges.isEmpty() ? table->rowCount() : ranges[0].topRow());
});
grid->addWidget(edInsRow, 0, 0);
auto edInsCol = new QPushButton(tr("Insert Col"));
connect(edInsCol, &QPushButton::clicked, table, [=] {
auto ranges = table->selectedRanges();
table->insertColumn(ranges.isEmpty() ? table->columnCount() : ranges[0].leftColumn());
});
grid->addWidget(edInsCol, 0, 1);
auto edDelRow = new QPushButton(tr("Delete Row"));
connect(edDelRow, &QPushButton::clicked, table, [=] {
auto ranges = table->selectedRanges();
for(auto range = ranges.end()-1; range>=ranges.begin(); range--) for(auto row = range->bottomRow(); row>=range->topRow(); row--) table->removeRow(row);
});
grid->addWidget(edDelRow, 1, 0);
auto edDelCol = new QPushButton(tr("Delete Col"));
connect(edDelCol, &QPushButton::clicked, table, [=] {
auto ranges = table->selectedRanges();
for(auto range = ranges.end()-1; range>=ranges.begin(); range--) for(auto col = range->rightColumn(); col>=range->leftColumn(); col--) table->removeColumn(col);
});
grid->addWidget(edDelCol, 1, 1);
auto edMerge = new QPushButton(tr("Merge"));
connect(edMerge, &QPushButton::clicked, table, [=] {
auto ranges = table->selectedRanges();
table->clearSelection();
qDebug()<<ranges.size();
for(auto range : ranges) table->setSpan(range.topRow(), range.leftColumn(), range.rowCount(), range.columnCount());
});
grid->addWidget(edMerge, 0, 2);
auto edUnmerge = new QPushButton(tr("Unmerge"));
connect(edUnmerge, &QPushButton::clicked, table, [=] {
auto ranges = table->selectedRanges();
for(auto range : ranges) table->setSpan(range.topRow(), range.leftColumn(), 1, 1);
});
grid->addWidget(edUnmerge, 1, 2);
line = new QFrame;
line->setFrameShape(QFrame::VLine);
line->setFrameShadow(QFrame::Sunken);
hBox->addWidget(line, 1);
auto edClear = new QPushButton(QIcon(":/res/program/Clean.png"), tr("Clear Data"));
hBox->addWidget(edClear);
hBox->addStretch();
connect(table, &QTableWidget::itemSelectionChanged, table, [=] {
auto ranges = table->selectedRanges();
if(ranges.isEmpty()) return;
auto item = table->item(ranges[0].topRow(), ranges[0].leftColumn());
isAutoSetting = true;
auto ft = item ? item->font() : table->font();
edFontFamily->setCurrentFont(QFont(ft.family()));
edFontSize->setValue(ft.pointSize());
edFontBold->setChecked(ft.bold());
edFontItalic->setChecked(ft.italic());
edFontUnderline->setChecked(ft.underline());
edTextColor->setColor(item ? BrushToColor(item->foreground(), Qt::white) : Qt::white);
edBackColor->setColor(item ? BrushToColor(item->background(), Qt::transparent) : Qt::transparent);
if(item) {
if(item->textAlignment() & Qt::AlignHCenter) edAlignHC->setChecked(true);
else if(item->textAlignment() & Qt::AlignRight) edAlignHR->setChecked(true);
else edAlignHL->setChecked(true);
if(item->textAlignment() & Qt::AlignTop) edAlignVT->setChecked(true);
else if(item->textAlignment() & Qt::AlignBottom) edAlignVB->setChecked(true);
else edAlignVC->setChecked(true);
} else {
edAlignHL->setChecked(true);
edAlignVC->setChecked(true);
}
edColWidth->setValue(table->columnWidth(ranges[0].leftColumn()));
edRowHeight->setValue(table->rowHeight(ranges[0].topRow()));
isAutoSetting = false;
});
auto pal = table->palette();
pal.setBrush(QPalette::Active, QPalette::WindowText, QBrush({255,255,255,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Disabled, QPalette::WindowText, QBrush({157,157,157,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Inactive, QPalette::WindowText, QBrush({255,255,255,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Active, QPalette::Text, QBrush({255,255,255,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Disabled, QPalette::Text, QBrush({157,157,157,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Inactive, QPalette::Text, QBrush({255,255,255,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Active, QPalette::ButtonText, QBrush({255,255,255,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Disabled, QPalette::ButtonText, QBrush({157,157,157,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Inactive, QPalette::ButtonText, QBrush({255,255,255,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Active, QPalette::Base, QBrush({0,0,0,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Disabled, QPalette::Base, QBrush({0,0,0,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Inactive, QPalette::Base, QBrush({0,0,0,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Active, QPalette::Window, QBrush({0,0,0,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Disabled, QPalette::Window, QBrush({0,0,0,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Inactive, QPalette::Window, QBrush({0,0,0,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Active, QPalette::Shadow, QBrush({0,0,0,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Disabled, QPalette::Shadow, QBrush({0,0,0,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Inactive, QPalette::Shadow, QBrush({0,0,0,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Active, QPalette::Highlight, QBrush({0,204,106,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Disabled, QPalette::Highlight, QBrush({0,204,106,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Inactive, QPalette::Highlight, QBrush({30,30,30,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Active, QPalette::HighlightedText, QBrush({255,255,255,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Disabled, QPalette::HighlightedText, QBrush({255,255,255,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Inactive, QPalette::HighlightedText, QBrush({255,255,255,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Active, QPalette::Link, QBrush({0,204,106,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Disabled, QPalette::Link, QBrush({48,140,198,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Inactive, QPalette::Link, QBrush({0,204,106,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Active, QPalette::LinkVisited, QBrush({0,63,19,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Disabled, QPalette::LinkVisited, QBrush({255,0,255,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Inactive, QPalette::LinkVisited, QBrush({0,63,19,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Active, QPalette::AlternateBase, QBrush({0,63,19,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Disabled, QPalette::AlternateBase, QBrush({52,52,52,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Inactive, QPalette::AlternateBase, QBrush({0,63,19,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Active, QPalette::NoRole, QBrush({0,0,0,255}, Qt::NoBrush));
pal.setBrush(QPalette::Disabled, QPalette::NoRole, QBrush({0,0,0,255}, Qt::NoBrush));
pal.setBrush(QPalette::Inactive, QPalette::NoRole, QBrush({0,0,0,255}, Qt::NoBrush));
pal.setBrush(QPalette::Active, QPalette::ToolTipBase, QBrush({60,60,60,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Disabled, QPalette::ToolTipBase, QBrush({255,255,220,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Inactive, QPalette::ToolTipBase, QBrush({60,60,60,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Active, QPalette::ToolTipText, QBrush({212,212,212,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Disabled, QPalette::ToolTipText, QBrush({0,0,0,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Inactive, QPalette::ToolTipText, QBrush({212,212,212,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Active, QPalette::PlaceholderText, QBrush({255,255,255,128}, Qt::SolidPattern));
pal.setBrush(QPalette::Disabled, QPalette::PlaceholderText, QBrush({255,255,255,128}, Qt::SolidPattern));
pal.setBrush(QPalette::Inactive, QPalette::PlaceholderText, QBrush({255,255,255,128}, Qt::SolidPattern));
pal.setBrush(QPalette::Active, QPalette::NColorRoles, QBrush({157,157,157,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Disabled, QPalette::NColorRoles, QBrush({255,255,255,255}, Qt::SolidPattern));
pal.setBrush(QPalette::Inactive, QPalette::Highlight, pal.brush(QPalette::Active, QPalette::Highlight));
table->setPalette(pal);
auto ft = table->font();
ft.setFamily("Calibri");
ft.setPointSize(11);
ft.setStyleStrategy(QFont::NoAntialias);
table->setFont(ft);
table->viewport()->setFont(ft);
auto item = new QTableWidgetItem;
item->setFont(ft);
table->setItemPrototype(item);
table->setStyleSheet("QTableWidget { gridline-color: #fff;}");
//table->setStyleSheet("QTableWidget::item { border: 0.5px solid #fff;}");
table->setRowHeight(20);
table->setColWidth(64);
vBox->addWidget(table);
connect(&dlg, &EmitCloseWidget::onClose, this, &ETable::grabImg);
}
void PaintTableWidget::paintEvent(QPaintEvent *event) {
TableWidget::paintEvent(event);
if(ebase) {
QPainter painter(viewport());
painter.setPen(QPen(Qt::green));
auto rect = ebase->innerRect();
painter.drawRect(columnViewportPosition(0)-1, rowViewportPosition(0)-1, rect.width(), rect.height());
}
}
JObj ETable::attrJson() {
if(png.isEmpty()) {
QBuffer buf(&png);
img.save(&buf, "PNG");
QCryptographicHash cryptoHash(QCryptographicHash::Md5);
cryptoHash.addData(png);
name = QString::fromLatin1(cryptoHash.result().toHex());
}
JObj obj;
addBaseAttr(obj);
obj["elementType"] = "Table";
obj["name"] = name;
obj["direction"] = direction;
obj["speed"] = speed;
obj["gridColor"] = edGridColor->color.name(QColor::HexArgb);
return obj;
}
bool ETable::save(const QString &pageDir) {
if(png.isEmpty()) {
QBuffer buf(&png);
img.save(&buf, "PNG");
QCryptographicHash cryptoHash(QCryptographicHash::Md5);
cryptoHash.addData(png);
name = QString::fromLatin1(cryptoHash.result().toHex());
}
QFile file(pageDir+"/"+name+".png");
if(! file.open(QFile::WriteOnly)) return false;
file.write(png);
file.close();
QXlsx::Document doc;
for(int r=0; r<table->rowCount(); r++) doc.setRowHeight(r+1, table->rowHeight(r) * 3.0 / 4);
for(int c=0; c<table->columnCount(); c++) doc.setColumnWidth(c+1, table->columnWidth(c) / 7.2);
for(int r=0; r<table->rowCount(); r++) for(int c=0; c<table->columnCount(); c++) {
auto item = table->item(r, c);
if(item==0) continue;
QXlsx::Format format;
format.setFont(item->font());
format.setFontColor(BrushToColor(item->foreground()));
format.setPatternBackgroundColor(BrushToColor(item->background()));
if(item->textAlignment() & Qt::AlignLeft) format.setHorizontalAlignment(QXlsx::Format::AlignLeft);
else if(item->textAlignment() & Qt::AlignHCenter) format.setHorizontalAlignment(QXlsx::Format::AlignHCenter);
else if(item->textAlignment() & Qt::AlignRight) format.setHorizontalAlignment(QXlsx::Format::AlignRight);
if(item->textAlignment() & Qt::AlignTop) format.setVerticalAlignment(QXlsx::Format::AlignTop);
else if(item->textAlignment() & Qt::AlignBottom) format.setVerticalAlignment(QXlsx::Format::AlignBottom);
else format.setVerticalAlignment(QXlsx::Format::AlignVCenter);
doc.write(r+1, c+1, item->text(), format);
auto rs = table->rowSpan(r, c);
auto cs = table->columnSpan(r, c);
if(rs>1 || cs>1) doc.mergeCells(QXlsx::CellRange(r+1, c+1, r+rs, c+cs));
}
doc.saveAs(pageDir+"/"+name+".xlsx");
return true;
}
int ETable::read(QXlsx::Worksheet *sheet) {
int rowCnt, colCnt;
auto cells = sheet->getFullCells(&rowCnt, &colCnt);
qDebug()<<"rowCnt colCnt"<<rowCnt<<colCnt;
auto dimension = sheet->dimension();
if(dimension.rowCount() > rowCnt) rowCnt = dimension.rowCount();
if(dimension.columnCount() > colCnt) colCnt = dimension.columnCount();
table->setRowCount(rowCnt);
table->setColumnCount(colCnt);
for(int r=0; r<rowCnt; r++) table->setRowHeight(r, sheet->rowHeight(r+1) * 4 / 3);
for(int c=0; c<colCnt; c++) table->setColumnWidth(c, sheet->columnWidth(c+1) * 7.2);
for(auto &cell : cells) {
auto fmt = cell.cell->format();
auto item = new QTableWidgetItem(cell.cell->readValue().toString());
auto ft = fmt.font();
ft.setStyleStrategy(QFont::NoAntialias);
item->setFont(ft);
auto color = fmt.fontColor();
if(color.isValid()) item->setForeground(color);
color = fmt.patternBackgroundColor();
if(color.isValid()) item->setBackground(color);
auto ha = fmt.horizontalAlignment();
auto va = fmt.verticalAlignment();
auto hAlign = Qt::AlignLeft;
auto vAlign = Qt::AlignVCenter;
if(ha==QXlsx::Format::AlignHCenter) hAlign = Qt::AlignHCenter;
else if(ha==QXlsx::Format::AlignRight) hAlign = Qt::AlignRight;
if(va==QXlsx::Format::AlignTop) vAlign = Qt::AlignTop;
else if(va==QXlsx::Format::AlignBottom) vAlign = Qt::AlignBottom;
item->setTextAlignment(hAlign | vAlign);
table->setItem(cell.row-1, cell.col-1, item);
if(cell.row<=12 && cell.col<=12) qDebug()<<cell.row<<cell.col<<cell.cell->cellType()<<fmt.font()<<fmt.fontSize()<<fmt.fontColor()<<fmt.patternBackgroundColor()<<"va"<<va;
//cell.cell->cellType();
}
auto mergeds = sheet->mergedCells();
for(auto &merged : mergeds) table->setSpan(merged.firstRow()-1, merged.firstColumn()-1, merged.rowCount(), merged.columnCount());
return 0;
}
void ETable::grabImg() {
table->ebase = 0;
table->clearSelection();
int width = 0, height = 0;
for(int r=0; r<table->rowCount(); r++) height += table->rowHeight(r);
for(int c=0; c<table->columnCount(); c++) width += table->columnWidth(c);
auto size = dlg.size();
dlg.resize(width+80, height+200);
img = QPixmap(width+20, height+20);
QPainter painter(&img);
painter.translate(1, 1);
table->viewport()->render(&painter);
painter.translate(-1, -1);
painter.setPen(QPen(edGridColor->color));
painter.drawLine(0,0, width, 0);
painter.drawLine(0,0, 0, height);
table->ebase = this;
dlg.resize(size);
png.clear();
update();
}
void ETable::paint(QPainter *painter, const QStyleOptionGraphicsItem *a, QWidget *b) {
auto inner = innerRect();
painter->setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform);
painter->drawPixmap(inner.left(), inner.top(), inner.width(), inner.height(), img, 0, 0, inner.width(), inner.height());
EBase::paint(painter, a, b);
}
void ETable::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *) {
dlg.show();
}
QWidget* ETable::attrWgt() {
auto wgtAttr = new QWidget;
auto vBox = new VBox(wgtAttr);
vBox->setContentsMargins(6, 0, 6, 0);
if(mMultiWin) vBox->setSpacing(3);
addBaseAttrWgt(vBox);
auto hBox = new HBox(vBox);
hBox->addLabel(translate("","Basic Properties"));
auto line = new QFrame;
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Sunken);
hBox->addWidget(line, 1);
hBox = new HBox(vBox);
auto label = hBox->addLabel(tr("Direction"));
label->setMinimumWidth(80);
label->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
auto edDirection = new QComboBox;
edDirection->addItem(tr("Static"), "");
edDirection->addItem(tr("Bottom -> Top"), "top");
edDirection->addItem(tr("Right -> Left"), "left");
edDirection->addItem(tr("Top -> Bottom"), "bottom");
edDirection->addItem(tr("Left -> Right"), "right");
SetCurData(edDirection, direction);
connect(edDirection, &QComboBox::currentIndexChanged, this, [=] {
direction = edDirection->currentData().toString();
});
hBox->addWidget(edDirection);
label = hBox->addLabel(tr("Speed"));
label->setMinimumWidth(80);
label->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
auto edSpeed = new QComboBox;
edSpeed->setEditable(true);
edSpeed->setMinimumWidth(50);
edSpeed->addItem("600");
edSpeed->addItem("540");
edSpeed->addItem("480");
edSpeed->addItem("420");
edSpeed->addItem("360");
edSpeed->addItem("300");
edSpeed->addItem("240");
edSpeed->addItem("180");
edSpeed->addItem("120");
edSpeed->addItem("60");
edSpeed->addItem("30");
edSpeed->setMaxVisibleItems(edSpeed->count());
auto text = QString::number(speed);
if(SetCurText(edSpeed, text)==-1) {
SetCurText(edSpeed, "60");
edSpeed->setCurrentText(text);
}
connect(edSpeed, &QComboBox::editTextChanged, this, [=](const QString &text) {
bool ok;
auto speed = text.toInt(&ok);
if(ok) this->speed = speed;
});
hBox->addWidget(edSpeed);
hBox->addLabel("px/s");
hBox->addStretch();
vBox->addStretch();
return wgtAttr;
}