mainwindow.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // PV_DB Translator
  2. // Copyright (C) 2019 nastys
  3. //
  4. // This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  5. //
  6. // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  7. //
  8. // You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
  9. #include "mainwindow.h"
  10. #include "ui_mainwindow.h"
  11. #include <QFile>
  12. #include <QTextStream>
  13. #include <QMessageBox>
  14. #include <QFileDialog>
  15. MainWindow::MainWindow(QWidget *parent) :
  16. QMainWindow(parent),
  17. ui(new Ui::MainWindow)
  18. {
  19. ui->setupUi(this);
  20. }
  21. MainWindow::~MainWindow()
  22. {
  23. delete ui;
  24. }
  25. void MainWindow::on_pushButton_Copy_clicked()
  26. {
  27. QFile copyFrom(ui->lineEdit_copyFrom->text()), copyTo(ui->lineEdit_copyTo->text()), saveAs(ui->lineEdit_saveAs->text());
  28. if (!(copyFrom.open(QIODevice::ReadOnly)))
  29. {
  30. QMessageBox::critical(this, "Error", "Could not open file (from).");
  31. return;
  32. }
  33. if (!(copyTo.open(QIODevice::ReadOnly)))
  34. {
  35. QMessageBox::critical(this, "Error", "Could not open file (to).");
  36. return;
  37. }
  38. saveAs.remove();
  39. if (!(saveAs.open(QIODevice::ReadWrite)))
  40. {
  41. QMessageBox::critical(this, "Error", "Could not open file (output).");
  42. return;
  43. }
  44. QTextStream streamFrom(&copyFrom), streamTo(&copyTo), streamOut(&saveAs);
  45. streamFrom.setCodec("UTF-8");
  46. streamTo.setCodec("UTF-8");
  47. streamOut.setCodec("UTF-8");
  48. while(!streamTo.atEnd())
  49. {
  50. // Read the next line in the target file
  51. QString line_to = streamTo.readLine();
  52. // If it contains ".lyric.", etc...
  53. if ( (line_to.contains(".lyric.") || line_to.contains(".song_name=") || line_to.contains(".songinfo.") || ( line_to.contains(".another_song.") && line_to.contains(".name=") ) ) )
  54. {
  55. bool found=0;
  56. while(!streamFrom.atEnd()&&found==0)
  57. {
  58. // Read the next line in the translation file
  59. QString line_from = streamFrom.readLine();
  60. // If the lines match
  61. if (line_from.split("=").at(0) == line_to.split("=").at(0))
  62. {
  63. // Copy the line from the translation file
  64. streamOut << line_from << endl;
  65. found=1;
  66. }
  67. // If nothing could be found, copy the line from the target file
  68. if (streamFrom.atEnd()&&found==0)
  69. streamOut << line_to << endl;
  70. }
  71. // Reset translation file
  72. copyFrom.seek(0);
  73. streamFrom.reset();
  74. streamFrom.resetStatus();
  75. }
  76. else streamOut << line_to << endl;
  77. }
  78. // Reset target file
  79. copyTo.seek(0);
  80. streamTo.reset();
  81. streamTo.resetStatus();
  82. copyFrom.close(); copyTo.close(); saveAs.close();
  83. QMessageBox::information(this, "Yay", "Done!");
  84. }
  85. void MainWindow::on_licence_clicked()
  86. {
  87. QMessageBox::information(this, "Licence", "PV_DB Translator\nCopyright (C) 2019 nastys\n\nThis program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.\n\nThis program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.\n\nYou should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.");
  88. }
  89. void MainWindow::on_pushButton_copyFrom_clicked()
  90. {
  91. QString copyFrom_string = QFileDialog::getOpenFileName(this, "Open old, translated pv_db");
  92. if (copyFrom_string != "")
  93. ui->lineEdit_copyFrom->setText(copyFrom_string);
  94. }
  95. void MainWindow::on_pushButton_copyTo_clicked()
  96. {
  97. QString copyTo_string = QFileDialog::getOpenFileName(this, "Open new, untranslated pv_db");
  98. if (copyTo_string != "")
  99. ui->lineEdit_copyTo->setText(copyTo_string);
  100. }
  101. void MainWindow::on_pushButton_saveAs_clicked()
  102. {
  103. QString saveAs_string = QFileDialog::getSaveFileName(this, "Save new, partially translated pv_db");
  104. if (saveAs_string != "")
  105. ui->lineEdit_saveAs->setText(saveAs_string);
  106. }