/src/export/exporter.cpp

https://github.com/albar965/littlenavmap · C++ · 118 lines · 84 code · 18 blank · 16 comment · 14 complexity · 64f1ac04116496729dc45c890c5a8c58 MD5 · raw file

  1. /*****************************************************************************
  2. * Copyright 2015-2020 Alexander Barthel alex@littlenavmap.org
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *****************************************************************************/
  17. #include "export/exporter.h"
  18. #include "search/sqlcontroller.h"
  19. #include "gui/dialog.h"
  20. #include "gui/errorhandler.h"
  21. #include "search/column.h"
  22. #include <QDebug>
  23. #include <QUrl>
  24. #include <QDesktopServices>
  25. #include <QApplication>
  26. #include <QSqlField>
  27. #include <QSqlRecord>
  28. using atools::gui::Dialog;
  29. using atools::gui::ErrorHandler;
  30. Exporter::Exporter(QWidget *parentWidget, SqlController *controllerObj)
  31. : controller(controllerObj)
  32. {
  33. dialog = new Dialog(parentWidget);
  34. errorHandler = new ErrorHandler(parentWidget);
  35. }
  36. Exporter::~Exporter()
  37. {
  38. delete dialog;
  39. delete errorHandler;
  40. }
  41. void Exporter::createVisualColumnIndex(int cnt, QVector<int>& visualToIndex)
  42. {
  43. visualToIndex.clear();
  44. visualToIndex.fill(-1, cnt);
  45. for(int i = 0; i < cnt; i++)
  46. {
  47. int vindex = controller->getColumnVisualIndex(i);
  48. Q_ASSERT(vindex >= 0);
  49. if(controller->isColumnVisibleInView(i))
  50. visualToIndex[vindex] = i;
  51. }
  52. }
  53. QStringList Exporter::headerNames(int cnt, const QVector<int>& visualToIndex)
  54. {
  55. QStringList columnNames;
  56. for(int i = 0; i < cnt; i++)
  57. if(visualToIndex[i] != -1)
  58. {
  59. QString cname = controller->getColumnDescriptor(visualToIndex[i])->getDisplayName();
  60. columnNames.append(cname.replace("-\n", "").replace("\n", " "));
  61. }
  62. return columnNames;
  63. }
  64. QStringList Exporter::headerNames(int cnt)
  65. {
  66. QStringList columnNames;
  67. for(int i = 0; i < cnt; i++)
  68. {
  69. QString cname = controller->getColumnDescriptor(i)->getDisplayName();
  70. columnNames.append(cname.replace("-\n", "").replace("\n", " "));
  71. }
  72. return columnNames;
  73. }
  74. void Exporter::openDocument(const QString& file)
  75. {
  76. QUrl url(QUrl::fromLocalFile(file));
  77. qDebug() << "HtmlExporter opening" << url;
  78. if(!QDesktopServices::openUrl(url))
  79. {
  80. qWarning() << "openUrl failed for" << url;
  81. atools::gui::Dialog::warning(parentWidget,
  82. tr("Cannot open file \"%1\"").arg(file),
  83. QMessageBox::Close, QMessageBox::NoButton);
  84. }
  85. }
  86. void Exporter::fillRecord(const QVariantList& values, const QStringList& cols, QSqlRecord& rec)
  87. {
  88. Q_ASSERT(values.size() == cols.size());
  89. if(rec.isEmpty())
  90. for(int i = 0; i < values.size(); i++)
  91. rec.append(QSqlField(cols.at(i), values.at(i).type()));
  92. for(int i = 0; i < values.size(); i++)
  93. {
  94. QVariant val = values.at(i);
  95. if(val.type() == QVariant::String && val.toString().isEmpty())
  96. rec.setNull(i);
  97. else
  98. rec.setValue(i, values.at(i));
  99. }
  100. }