From kde-commits Mon Sep 30 19:50:05 2013 From: Stephane Mankowski Date: Mon, 30 Sep 2013 19:50:05 +0000 To: kde-commits Subject: [skrooge] /: feature: Export HTML and ODT from tables Message-Id: X-MARC-Message: https://marc.info/?l=kde-commits&m=138057061613418 Git commit 5dce36999c030be7b310a470ae400e7b4f750797 by Stephane Mankowski. Committed on 30/09/2013 at 19:45. Pushed by smankowski into branch 'master'. feature: Export HTML and ODT from tables M +1 -2 skgbasegui/skgtablewithgraph.cpp M +34 -42 skgbasegui/skgtreeview.cpp M +2 -3 skgbasegui/skgtreeview.h M +1 -1 tests/skgbankguitest/skgtesttreeview.cpp http://commits.kde.org/skrooge/5dce36999c030be7b310a470ae400e7b4f750797 diff --git a/skgbasegui/skgtablewithgraph.cpp b/skgbasegui/skgtablewithgrap= h.cpp index 548791c..9b3c38c 100644 --- a/skgbasegui/skgtablewithgraph.cpp +++ b/skgbasegui/skgtablewithgraph.cpp @@ -2294,8 +2294,7 @@ void SKGTableWithGraph::onExport() { _SKGTRACEIN(10, "SKGTableWithGraph::onExport"); SKGError err; - QString lastCodecUsed =3D QTextCodec::codecForLocale()->name(); - QString fileName =3D SKGMainPanel::getSaveFileName("kfiledialog:///IMP= EXP", "text/csv text/plain" , this, QString(), &lastCodecUsed); + QString fileName =3D SKGMainPanel::getSaveFileName("kfiledialog:///IMP= EXP", "text/csv text/plain" , this); if (!fileName.isEmpty()) { err =3D exportInFile(fileName); = diff --git a/skgbasegui/skgtreeview.cpp b/skgbasegui/skgtreeview.cpp index 12b6664..0137938 100644 --- a/skgbasegui/skgtreeview.cpp +++ b/skgbasegui/skgtreeview.cpp @@ -992,62 +992,55 @@ QTextBrowser* SKGTreeView::getTextBrowser() const } = = -SKGStringListList SKGTreeView::getTable() const +SKGStringListList SKGTreeView::getTable(const QModelIndex& iIndex) const { // Build table SKGStringListList table; = // Get header names if (m_model) { + // Header int nb =3D m_model->columnCount(); - QStringList cols; - for (int i =3D 0; i < nb; ++i) { - cols.append(m_model->headerData(i, Qt::Horizontal, Qt::UserRol= e).toString().split('|').at(0)); + if (!iIndex.isValid()) { + QStringList cols; + for (int i =3D 0; i < nb; ++i) { + cols.append(m_model->headerData(i, Qt::Horizontal, Qt::Use= rRole).toString().split('|').at(0)); + } + table.append(cols); } - table.append(cols); = // Get content - table.append(getTableContent()); - } - return table; -} - -SKGStringListList SKGTreeView::getTableContent(const QModelIndex& iIndex) = const -{ - // Build table - SKGStringListList table; - - int nb =3D m_model->columnCount(); - int nb2 =3D m_model->rowCount(iIndex); - for (int i =3D 0; i < nb2; ++i) { - QStringList row; - for (int j =3D 0; j < nb; j++) { - // We have to check the type for 214849 - QModelIndex idx =3D m_model->index(i, j, iIndex); - - SKGServices::AttributeType type =3D m_model->getAttributeType(= j); - QString display =3D m_model->data(idx, type =3D=3D SKGServices= ::FLOAT || m_model->getObject(idx).getTable().isEmpty() ? Qt::DisplayRole = : Qt::UserRole).toString(); - if (display.isEmpty()) display =3D m_model->data(idx, Qt::Disp= layRole).toString(); - row.append(display); - } + int nb2 =3D m_model->rowCount(iIndex); + for (int i =3D 0; i < nb2; ++i) { + QStringList row; + for (int j =3D 0; j < nb; j++) { + // We have to check the type for 214849 + QModelIndex idx =3D m_model->index(i, j, iIndex); + + SKGServices::AttributeType type =3D m_model->getAttributeT= ype(j); + QString display =3D m_model->data(idx, type =3D=3D SKGServ= ices::FLOAT || m_model->getObject(idx).getTable().isEmpty() ? Qt::DisplayR= ole : Qt::UserRole).toString(); + if (display.isEmpty()) { + display =3D m_model->data(idx, Qt::DisplayRole).toStri= ng(); + } + row.append(display); + } = - table.append(row); + table.append(row); = - QModelIndex idx0 =3D m_model->index(i, 0, iIndex); - if (m_model->hasChildren(idx0)) { - table.append(getTableContent(idx0)); + QModelIndex idx0 =3D m_model->index(i, 0, iIndex); + if (m_model->hasChildren(idx0)) { + table.append(getTable(idx0)); + } } } - return table; } = - SKGError SKGTreeView::exportInFile(const QString& iFileName) { SKGError err; _SKGTRACEIN(10, "SKGTreeView::exportInFile"); - QString lastCodecUsed =3D QTextCodec::codecForLocale()->name(); + QString codec =3D QTextCodec::codecForLocale()->name(); QString extension =3D QFileInfo(iFileName).suffix().toUpper(); if (extension =3D=3D "CSV") { // Write file @@ -1055,7 +1048,7 @@ SKGError SKGTreeView::exportInFile(const QString& iFi= leName) if (!file.open()) err.setReturnCode(ERR_INVALIDARG).setMessage(i18= nc("Error message", "Save file '%1' failed" , iFileName)); else { QTextStream out(&file); - out.setCodec(lastCodecUsed.toAscii().constData()); + out.setCodec(codec.toAscii().constData()); QStringList dump =3D SKGServices::tableToDump(getTable(), SKGS= ervices::DUMP_CSV); int nbl =3D dump.count(); for (int i =3D 0; i < nbl; ++i) { @@ -1104,10 +1097,10 @@ SKGError SKGTreeView::exportInFile(const QString& i= FileName) if (!file.open()) err.setReturnCode(ERR_INVALIDARG).setMessage(i18= nc("Error message", "Save file '%1' failed" , iFileName)); else { QTextStream out(&file); - out.setCodec(lastCodecUsed.toAscii().constData()); + out.setCodec(codec.toAscii().constData()); QTextBrowser* tb =3D getTextBrowser(); if (tb) { - out << tb->toHtml() << endl; + out << tb->toHtml().replace("", "") << endl; = delete tb; } @@ -1116,7 +1109,7 @@ SKGError SKGTreeView::exportInFile(const QString& iFi= leName) // Close file file.finalize(); file.close(); - } if (extension =3D=3D "ODT") { + } else if (extension =3D=3D "ODT") { QTextBrowser* tb =3D getTextBrowser(); if (tb) { QTextDocument doc; @@ -1133,7 +1126,7 @@ SKGError SKGTreeView::exportInFile(const QString& iFi= leName) if (!file.open()) err.setReturnCode(ERR_INVALIDARG).setMessage(i18= nc("Error message", "Save file '%1' failed" , iFileName)); else { QTextStream out(&file); - out.setCodec(lastCodecUsed.toAscii().constData()); + out.setCodec(codec.toAscii().constData()); QStringList dump =3D SKGServices::tableToDump(getTable(), SKGS= ervices::DUMP_TEXT); int nbl =3D dump.count(); for (int i =3D 0; i < nbl; ++i) { @@ -1153,8 +1146,7 @@ SKGError SKGTreeView::exportInFile(const QString& iFi= leName) void SKGTreeView::onExport() { _SKGTRACEIN(10, "SKGTreeView::onExport"); - QString lastCodecUsed =3D QTextCodec::codecForLocale()->name(); - QString fileName =3D SKGMainPanel::getSaveFileName("kfiledialog:///IMP= EXP", "text/csv text/plain text/html application/vnd.oasis.opendocument.tex= t image/svg+xml application/pdf" , this, QString(), &lastCodecUsed); + QString fileName =3D SKGMainPanel::getSaveFileName("kfiledialog:///IMP= EXP", "text/csv text/plain text/html application/vnd.oasis.opendocument.tex= t image/svg+xml application/pdf" , this); if (!fileName.isEmpty()) { SKGError err =3D exportInFile(fileName); SKGMainPanel::displayErrorMessage(err); diff --git a/skgbasegui/skgtreeview.h b/skgbasegui/skgtreeview.h index 782dc81..2130d1b 100644 --- a/skgbasegui/skgtreeview.h +++ b/skgbasegui/skgtreeview.h @@ -103,9 +103,10 @@ public: = /** * Get the table content + * @param iIndex the line index * @return the table content */ - virtual SKGStringListList getTable() const; + virtual SKGStringListList getTable(const QModelIndex& iIndex =3D QMode= lIndex()) const; = /** * Get the current selection @@ -312,8 +313,6 @@ private Q_SLOTS: void onActionTriggered(int action); void onRangeChanged(); = - SKGStringListList getTableContent(const QModelIndex& iIndex =3D QModel= Index()) const; - private: Q_DISABLE_COPY(SKGTreeView) = diff --git a/tests/skgbankguitest/skgtesttreeview.cpp b/tests/skgbankguites= t/skgtesttreeview.cpp index fe00458..a8e5c8a 100644 --- a/tests/skgbankguitest/skgtesttreeview.cpp +++ b/tests/skgbankguitest/skgtesttreeview.cpp @@ -57,7 +57,7 @@ void SKGTESTTreeView::Test() tree->exportInFile(SKGTest::getTestPath("OUT") % "/skgtesttreeview/SKG= TreeView.pdf"); tree->exportInFile(SKGTest::getTestPath("OUT") % "/skgtesttreeview/SKG= TreeView.html"); tree->exportInFile(SKGTest::getTestPath("OUT") % "/skgtesttreeview/SKG= TreeView.odt"); - = + tree->getTable(); tree->getFirstSelectedObject(); =