mainwindow.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. MainWindow::MainWindow(QWidget *parent) :
  4. QMainWindow(parent),
  5. ui(new Ui::MainWindow)
  6. {
  7. ui->setupUi(this);
  8. //Initialize classes
  9. m_textColorTab = new textColorTab;
  10. m_themeInfoTab = new themeInfoTab;
  11. //Connect elements for Menu Bar
  12. QObject::connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(openFile()));
  13. QObject::connect(ui->actionSave, SIGNAL(triggered()), this, SLOT(saveFile()));
  14. QObject::connect(ui->actionAbout_Qt, SIGNAL(triggered()), this, SLOT(aboutQt()));
  15. QObject::connect(ui->actionClose, SIGNAL(triggered()), this, SLOT(close()));
  16. QObject::connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(aboutMyself()));
  17. //Connect elements for Tab#1 (Color text)
  18. QObject::connect(this, SIGNAL(jsonDataUpdated(QString)), m_textColorTab, SLOT(updateLabels(QString)));
  19. QObject::connect(m_textColorTab, SIGNAL(setLabelsColor(QColor,QColor,QColor,QColor,QColor)), this, SLOT(updateLabelColor(QColor,QColor,QColor,QColor,QColor)));
  20. QObject::connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(updatePrimaryColor()));
  21. QObject::connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(updateSecondaryColor()));
  22. QObject::connect(ui->pushButton_3, SIGNAL(clicked()), this, SLOT(updateIconSetColor()));
  23. QObject::connect(ui->pushButton_4, SIGNAL(clicked()), this, SLOT(updateIconSetActiveColor()));
  24. QObject::connect(ui->pushButton_5, SIGNAL(clicked()), this, SLOT(updateTransitionScreenColor()));
  25. //Connect elements for Tab#2 (Theme informations)
  26. QObject::connect(this, SIGNAL(jsonDataUpdated(QString)), m_themeInfoTab, SLOT(updateData(QString)));
  27. QObject::connect(m_themeInfoTab, SIGNAL(setTextBox(QString,QString,QString)), this, SLOT(updateThemeInfos(QString,QString,QString)));
  28. QObject::connect(ui->lineEdit, SIGNAL(editingFinished()), this, SLOT(updateThemeName()));
  29. QObject::connect(ui->lineEdit_2, SIGNAL(editingFinished()), this, SLOT(updateThemeVersion()));
  30. QObject::connect(ui->textEdit, SIGNAL(textChanged()), this, SLOT(updateThemeDescription()));
  31. }
  32. MainWindow::~MainWindow()
  33. {
  34. delete ui;
  35. }
  36. void MainWindow::openFile()
  37. {
  38. //Open the file dialog, showing only json files
  39. QString fileName = QFileDialog::getOpenFileName(this, tr("Open Config File"),
  40. QDir::homePath(),
  41. tr("JSON Files (*.json)"));
  42. //Open the file in a text stream
  43. QFile configFile(fileName);
  44. if (!configFile.open(QFile::ReadOnly | QFile::Text)) return;
  45. QTextStream in(&configFile);
  46. m_jsonData = in.readAll();
  47. //Convert json content to Json Object
  48. QJsonDocument jsonDataDocument = QJsonDocument::fromJson(m_jsonData.toUtf8());
  49. m_jsonObject = jsonDataDocument.object();
  50. //Emit the update event
  51. qDebug() << m_jsonObject;
  52. emit jsonDataUpdated(m_jsonData);
  53. }
  54. void MainWindow::saveFile()
  55. {
  56. //Open the save file dialog, showing only json files
  57. QString fileName = QFileDialog::getSaveFileName(this, tr("Save Config File"),
  58. QDir::homePath(),
  59. tr("JSON Files (*.json)"));
  60. //Remove the file to clear it's content
  61. QFile::remove(fileName);
  62. //Open the file in a text stream
  63. QFile configFile(fileName);
  64. if (!configFile.open(QFile::ReadWrite | QFile::Text)) return;
  65. QTextStream in(&configFile);
  66. //Write the QJsonObject to the file
  67. QJsonDocument doc(m_jsonObject);
  68. in << doc.toJson(QJsonDocument::Compact);
  69. }
  70. void MainWindow::updateLabelColor(QColor labelPrimaryColor, QColor labelSecondaryColor, QColor labelIconSetColor, QColor labelSetActiveColor, QColor labelTransitionScreen)
  71. {
  72. //Update the preview labels color for Tab#1
  73. ui->label->setStyleSheet("QLabel {color : " + labelPrimaryColor.name() + "}");
  74. ui->label_2->setStyleSheet("QLabel {color : " + labelSecondaryColor.name() + "}");
  75. ui->label_3->setStyleSheet("QLabel {color : " + labelIconSetColor.name() + "}");
  76. ui->label_4->setStyleSheet("QLabel {color : " + labelSetActiveColor.name() + "}");
  77. ui->label_5->setStyleSheet("QLabel {color : " + labelTransitionScreen.name() + "}");
  78. }
  79. void MainWindow::updatePrimaryColor()
  80. {
  81. //Get color from user
  82. QString colorName = m_textColorTab->getColorFromUser().name();
  83. //Update Primary Text Label color + write new value to the JSON Object
  84. ui->label->setStyleSheet("QLabel {color : " + colorName + "}");
  85. m_jsonObject["primaryText"] = QJsonValue(QString(colorName.mid(1, 6)));
  86. }
  87. void MainWindow::updateSecondaryColor()
  88. {
  89. //Get color from user
  90. QString colorName = m_textColorTab->getColorFromUser().name();
  91. //Update Secondary Text Label color + write new value to the JSON Object
  92. ui->label_2->setStyleSheet("QLabel {color : " + colorName + "}");
  93. m_jsonObject["secondaryText"] = QJsonValue(QString(colorName.mid(1, 6)));
  94. }
  95. void MainWindow::updateIconSetColor()
  96. {
  97. //Get color from user
  98. QString colorName = m_textColorTab->getColorFromUser().name();
  99. //Update IconSet Text Label color + write new value to the JSON Object
  100. ui->label_3->setStyleSheet("QLabel {color : " + colorName + "}");
  101. m_jsonObject["iconSet"] = QJsonValue(QString(colorName.mid(1, 6)));
  102. }
  103. void MainWindow::updateIconSetActiveColor()
  104. {
  105. //Get color from user
  106. QString colorName = m_textColorTab->getColorFromUser().name();
  107. //Update IconSet Text Label color + write new value to the JSON Object
  108. ui->label_4->setStyleSheet("QLabel {color : " + colorName + "}");
  109. m_jsonObject["iconSetActive"] = QJsonValue(QString(colorName.mid(1, 6)));
  110. }
  111. void MainWindow::updateTransitionScreenColor()
  112. {
  113. //Get color from user
  114. QString colorName = m_textColorTab->getColorFromUser().name();
  115. //Update IconSet Text Label color + write new value to the JSON Object
  116. ui->label_5->setStyleSheet("QLabel {color : " + colorName + "}");
  117. m_jsonObject["transitionScreen"] = QJsonValue(QString(colorName.mid(1, 6)));
  118. }
  119. void MainWindow::aboutQt()
  120. {
  121. //Just to open the "About Qt" dialog
  122. QMessageBox::aboutQt(this);
  123. }
  124. void MainWindow::aboutMyself()
  125. {
  126. //Open the "About Timmy Companion" dialog
  127. QMessageBox::about(this, "About Timmy Companion", "Timmy Companion\n\nMake freeShop themer's life easier (Sometimes)\nv. " APP_VERSION);
  128. }
  129. void MainWindow::updateThemeInfos(QString themeName, QString themeVer, QString themeDesc)
  130. {
  131. ui->lineEdit->setText(themeName);
  132. ui->lineEdit_2->setText(themeVer);
  133. ui->textEdit->setText(themeDesc);
  134. }
  135. void MainWindow::updateThemeName()
  136. {
  137. m_jsonObject["themeName"] = QJsonValue(ui->lineEdit->text());
  138. }
  139. void MainWindow::updateThemeVersion()
  140. {
  141. m_jsonObject["themeVer"] = QJsonValue(ui->lineEdit_2->text());
  142. }
  143. void MainWindow::updateThemeDescription()
  144. {
  145. m_jsonObject["themeDesc"] = QJsonValue(ui->textEdit->toPlainText());
  146. }