PageRenderTime 25ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/ground/gcs/src/plugins/uavsettingsimportexport/importsummary.cpp

https://gitlab.com/sonium/librepilot
C++ | 161 lines | 104 code | 19 blank | 38 comment | 11 complexity | a5371d9e34f5f663193f26c370faa72d MD5 | raw file
  1. /**
  2. ******************************************************************************
  3. *
  4. * @file importsummary.cpp
  5. * @author (C) 2011 The OpenPilot Team, http://www.openpilot.org
  6. * @addtogroup GCSPlugins GCS Plugins
  7. * @{
  8. * @addtogroup UAVSettingsImportExport UAVSettings Import/Export Plugin
  9. * @{
  10. * @brief UAVSettings Import/Export Plugin
  11. *****************************************************************************/
  12. /*
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful, but
  19. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  20. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  21. * for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License along
  24. * with this program; if not, write to the Free Software Foundation, Inc.,
  25. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. #include "importsummary.h"
  28. ImportSummaryDialog::ImportSummaryDialog(QWidget *parent) :
  29. QDialog(parent),
  30. ui(new Ui::ImportSummaryDialog)
  31. {
  32. ui->setupUi(this);
  33. setWindowTitle(tr("Import Summary"));
  34. ui->importSummaryList->setColumnCount(3);
  35. ui->importSummaryList->setRowCount(0);
  36. QStringList header;
  37. header.append("Save");
  38. header.append("Name");
  39. header.append("Status");
  40. ui->importSummaryList->setHorizontalHeaderLabels(header);
  41. ui->progressBar->setValue(0);
  42. connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(close()));
  43. connect(ui->saveToFlash, SIGNAL(clicked()), this, SLOT(doTheSaving()));
  44. // Connect the help button
  45. connect(ui->helpButton, SIGNAL(clicked()), this, SLOT(openHelp()));
  46. }
  47. ImportSummaryDialog::~ImportSummaryDialog()
  48. {
  49. delete ui;
  50. }
  51. /*
  52. Open the right page on the wiki
  53. */
  54. void ImportSummaryDialog::openHelp()
  55. {
  56. QDesktopServices::openUrl(QUrl(tr("http://wiki.openpilot.org/display/Doc/UAV+Settings+import-export"), QUrl::StrictMode));
  57. }
  58. /*
  59. Adds a new line about a UAVObject along with its status
  60. (whether it got saved OK or not)
  61. */
  62. void ImportSummaryDialog::addLine(QString uavObjectName, QString text, bool status)
  63. {
  64. ui->importSummaryList->setRowCount(ui->importSummaryList->rowCount() + 1);
  65. int row = ui->importSummaryList->rowCount() - 1;
  66. ui->importSummaryList->setCellWidget(row, 0, new QCheckBox(ui->importSummaryList));
  67. QTableWidgetItem *objName = new QTableWidgetItem(uavObjectName);
  68. ui->importSummaryList->setItem(row, 1, objName);
  69. QCheckBox *box = dynamic_cast<QCheckBox *>(ui->importSummaryList->cellWidget(row, 0));
  70. ui->importSummaryList->setItem(row, 2, new QTableWidgetItem(text));
  71. // Disable editability and selectability in table elements
  72. ui->importSummaryList->item(row, 1)->setFlags(!Qt::ItemIsEditable);
  73. ui->importSummaryList->item(row, 2)->setFlags(!Qt::ItemIsEditable);
  74. if (status) {
  75. box->setChecked(true);
  76. } else {
  77. box->setChecked(false);
  78. box->setEnabled(false);
  79. }
  80. this->repaint();
  81. this->showEvent(NULL);
  82. }
  83. /*
  84. Saves every checked UAVObjet in the list to Flash
  85. */
  86. void ImportSummaryDialog::doTheSaving()
  87. {
  88. int itemCount = 0;
  89. ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
  90. UAVObjectManager *objManager = pm->getObject<UAVObjectManager>();
  91. UAVObjectUtilManager *utilManager = pm->getObject<UAVObjectUtilManager>();
  92. connect(utilManager, SIGNAL(saveCompleted(int, bool)), this, SLOT(updateSaveCompletion()));
  93. for (int i = 0; i < ui->importSummaryList->rowCount(); i++) {
  94. QCheckBox *box = dynamic_cast<QCheckBox *>(ui->importSummaryList->cellWidget(i, 0));
  95. if (box->isChecked()) {
  96. ++itemCount;
  97. }
  98. }
  99. if (itemCount == 0) {
  100. return;
  101. }
  102. ui->progressBar->setMaximum(itemCount + 1);
  103. ui->progressBar->setValue(1);
  104. for (int i = 0; i < ui->importSummaryList->rowCount(); i++) {
  105. QString uavObjectName = ui->importSummaryList->item(i, 1)->text();
  106. QCheckBox *box = dynamic_cast<QCheckBox *>(ui->importSummaryList->cellWidget(i, 0));
  107. if (box->isChecked()) {
  108. UAVObject *obj = objManager->getObject(uavObjectName);
  109. utilManager->saveObjectToSD(obj);
  110. this->repaint();
  111. }
  112. }
  113. ui->saveToFlash->setEnabled(false);
  114. ui->closeButton->setEnabled(false);
  115. }
  116. void ImportSummaryDialog::updateSaveCompletion()
  117. {
  118. ui->progressBar->setValue(ui->progressBar->value() + 1);
  119. if (ui->progressBar->value() == ui->progressBar->maximum()) {
  120. ui->saveToFlash->setEnabled(true);
  121. ui->closeButton->setEnabled(true);
  122. }
  123. }
  124. void ImportSummaryDialog::changeEvent(QEvent *e)
  125. {
  126. QDialog::changeEvent(e);
  127. switch (e->type()) {
  128. case QEvent::LanguageChange:
  129. ui->retranslateUi(this);
  130. break;
  131. default:
  132. break;
  133. }
  134. }
  135. void ImportSummaryDialog::showEvent(QShowEvent *event)
  136. {
  137. Q_UNUSED(event)
  138. ui->importSummaryList->resizeColumnsToContents();
  139. int width = ui->importSummaryList->width() - (ui->importSummaryList->columnWidth(0) +
  140. ui->importSummaryList->columnWidth(2));
  141. ui->importSummaryList->setColumnWidth(1, width - 15);
  142. }