helper.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. #include "app.h"
  2. #include "filesystem.h"
  3. Helper::Helper()
  4. {
  5. }
  6. int Helper::getVersion(QString string) {
  7. std::string str(string.toLatin1().data());
  8. int version = 0;
  9. for (unsigned i = str.find("_v") + 2; i < str.find("_v") + 7; i += 2) {
  10. version = version * 10 + (str[i] - '0');
  11. }
  12. return version;
  13. }
  14. void Helper::checkTab(QString tabname){
  15. App *app = &App::getInstance();
  16. QList<QGroupBox *> boxes = app->window->findChildren<QGroupBox *> ();
  17. foreach (QGroupBox * box, boxes){
  18. if(box->parent()->objectName() == "centralWidget"){
  19. QLabel* boximage = app->window->findChild<QLabel*>(box->objectName() + "Image");
  20. if(box->objectName() == tabname){
  21. box->show();
  22. if(boximage != nullptr) boximage->show();
  23. } else {
  24. box->hide();
  25. if(boximage != nullptr) boximage->hide();
  26. }
  27. }
  28. }
  29. app->window->ui->serversbox->show();
  30. }
  31. void Helper::loadNews(){
  32. App *app = &App::getInstance();
  33. app->logSectionStart("Загружаем скин лаунчера");
  34. qInfo("%s:%i: %s", __FILE__, __LINE__, "Загрузка новостей с сайта");
  35. QString response = app->network->query(app->config->getValue("Network", "news"));
  36. if(response == "error"){
  37. qInfo("%s:%i: %s", __FILE__, __LINE__, "Отсутствует связь с сервером. Прервано.");
  38. app->logSectionEnd();
  39. return;
  40. }
  41. QStringList data = response.split(":::");
  42. qInfo("%s:%i: %s%s", __FILE__, __LINE__, "Получено новостей - ", app->helper->stringToChar(QString::number(data.count())));
  43. int counter = 1;
  44. foreach(QString news, data){
  45. qInfo("%s:%i: %s%s", __FILE__, __LINE__, "Начинаем размещение новости №", app->helper->stringToChar(QString::number(counter)));
  46. QStringList item = news.split("|");
  47. QLabel* image = app->window->ui->newsbox->findChild<QLabel*>("news" + QString::number(counter) + "Image");
  48. QLabel* title = app->window->ui->newsbox->findChild<QLabel*>("news" + QString::number(counter) + "Title");
  49. QLabel* teaser = app->window->ui->newsbox->findChild<QLabel*>("news" + QString::number(counter) + "Teaser");
  50. qInfo("%s:%i: %s", __FILE__, __LINE__, "Устанавливаем миниатюру новости");
  51. QByteArray bytes = app->network->query_binary(item[0]);
  52. QImage img(36, 36, QImage::Format_Indexed8);
  53. img.loadFromData(bytes);
  54. QString skin = app->helper->getSkinName();
  55. if(img.bits() == nullptr){
  56. qInfo("%s:%i: %s", __FILE__, __LINE__, "Миниатюра не найдена, устанавливаем заглушку");
  57. img.load(":/res/styles/" + skin + "/nophoto.jpg");
  58. }
  59. image->setPixmap(QPixmap::fromImage(img));
  60. qInfo("%s:%i: %s", __FILE__, __LINE__, "Применяем HTML-цвета");
  61. QString hrefcolor = app->helper->htmlColors.contains("a") ? app->helper->htmlColors.lowerBound("a").value() : "blue";
  62. QString html = "<a href=\"" + item[3] +"\" style=\"color:" + hrefcolor + "; text-decoration:none;\">" + item[1] + "</a>";
  63. qInfo("%s:%i: %s", __FILE__, __LINE__, "Размещаем тексты");
  64. title->setText(html);
  65. teaser->setText(item[2]);
  66. counter = counter+1;
  67. }
  68. qInfo("%s:%i: %s", __FILE__, __LINE__, "Выполнено.");
  69. app->logSectionEnd();
  70. }
  71. void Helper::loadHelp(){
  72. App *app = &App::getInstance();
  73. app->logSectionStart("Создание справки");
  74. QFile file(QApplication::applicationDirPath() + "/readme.html");
  75. if(file.open(QFile::ReadOnly | QFile::Text)){
  76. qInfo("%s:%i: %s", __FILE__, __LINE__, "Справка загружена. Устанавливаем HTML-цвета и выводим.");
  77. app->window->ui->textBrowser->setHtml("");
  78. QString content = file.readAll();
  79. app->stylehtml = "<style>";
  80. for(auto e : app->helper->htmlColors.keys()){
  81. app->stylehtml = app->stylehtml + e + "{color:" + app->helper->htmlColors.value(e) + ";}";
  82. }
  83. QFile styleF;
  84. QString skin = app->helper->getSkinName();
  85. styleF.setFileName(app->app_folder + "styles/" + skin + "/readme.css");
  86. if(styleF.open(QFile::ReadOnly)){
  87. app->stylehtml = "<html><head><style type=\"text/css\">" + app->stylehtml + styleF.readAll();
  88. }
  89. app->stylehtml = app->stylehtml + "</style></head>";
  90. app->window->ui->textBrowser->setHtml(app->stylehtml + content);
  91. app->window->ui->textBrowser->document()->setDefaultStyleSheet(styleF.readAll());
  92. qInfo("%s:%i: %s", __FILE__, __LINE__, "Выполнено.");
  93. app->logSectionEnd();
  94. } else {
  95. qWarning("%s:%i: %s", __FILE__, __LINE__, "Не удалось получить информацию с сервера");
  96. qInfo("%s:%i: %s", __FILE__, __LINE__, "Выполнено.");
  97. app->logSectionEnd();
  98. }
  99. }
  100. void Helper::hideAll(){
  101. qInfo("%s:%i: %s", __FILE__, __LINE__, "Прячем ненужные элементы.");
  102. App *app = &App::getInstance();
  103. app->window->ui->optbox->hide();
  104. app->window->ui->optboxImage->hide();
  105. app->window->ui->newsbox->hide();
  106. app->window->ui->newsboxImage->hide();
  107. app->window->ui->helpbox->hide();
  108. app->window->ui->helpboxImage->hide();
  109. app->window->ui->aboutbox->hide();
  110. app->window->ui->aboutboxImage->hide();
  111. app->window->ui->dialogbox->hide();
  112. app->window->ui->loader->hide();
  113. app->window->ui->dialogList->hide();
  114. app->window->ui->startbox->hide();
  115. qInfo("%s:%i: %s", __FILE__, __LINE__, "Устанавливаем всплывающие подсказки.");
  116. app->window->ui->goOriginal->setToolTip("Запуск оригинальной версии игры");
  117. app->window->ui->goLocal->setToolTip("Запуск русской версии игры");
  118. app->window->ui->goCancel->setToolTip("Вернуться обратно");
  119. app->window->ui->goProgress->hide();
  120. app->window->ui->goHint->hide();
  121. app->window->ui->footRed->hide();
  122. app->window->ui->footYellow->hide();
  123. }
  124. void Helper::setAbout(){
  125. App *app = &App::getInstance();
  126. app->logSectionStart("О нас");
  127. qInfo("%s:%i: %s", __FILE__, __LINE__, "Размещаем информацию о хороших людях.");
  128. app->window->ui->programmerslist->setText("Gi1dor, coder");
  129. app->window->ui->designerslist->setText("Чирик, valyot, Quilanar, Eodoro");
  130. app->window->ui->translatorslist->setText("<b>Переводчики:</b> Pioneer, kitkatik134, Anadomiel, Progylkin, Drazgar, Rhymer, Влад Блюденов, "
  131. "Buddy, Quilanar, Ferdik, Alcassar, Чирик, Tukbard<br><br>"
  132. "<b>Модераторы:</b> Eodoro, unnamedartist, Marty, valyot, Invizet");
  133. app->window->ui->helperslist->setText("Menelhad, Мурзилка, Pastor, Alvees, Mirta, иван петров, vensys, "
  134. "Arkelas, Чеширская Улыбка, Shanishka70");
  135. }
  136. void Helper::findSkins(){
  137. App *app = &App::getInstance();
  138. app->logSectionStart("Темы оформления");
  139. qInfo("%s:%i: %s", __FILE__, __LINE__, "Производим поиск доступных тем.");
  140. QDir dir(QApplication::applicationDirPath() + "/styles");
  141. QStringList skins = dir.entryList(QDir::Dirs | QDir::AllDirs | QDir::NoDotAndDotDot);
  142. app->window->ui->skinSelect->addItems(skins);
  143. QString skinname = app->config->getValue("Global", "current_theme");
  144. app->window->ui->skinSelect->setCurrentText(skinname);
  145. qInfo("%s:%i: %s", __FILE__, __LINE__, "Устанавливаем отметку о файле локализации.");
  146. QString file = app->config->getValue("Local", "file");
  147. if(file == "client_local_English.dat") app->window->ui->enButton->setEnabled(false);
  148. if(file == "client_local_DE.dat") app->window->ui->deButton->setEnabled(false);
  149. if(file == "client_local_FR.dat") app->window->ui->frButton->setEnabled(false);
  150. qInfo("%s:%i: %s", __FILE__, __LINE__, "Выполнено.");
  151. app->logSectionEnd();
  152. }
  153. QString Helper::getSkinName() {
  154. App *app = &App::getInstance();
  155. QString skin = app->config->getValue("Global", "current_theme");
  156. if(skin == "-1") skin = app->config->getValue("Global", "default_theme");
  157. qInfo("%s:%i: %s%s", __FILE__, __LINE__, "Текущая тема оформления: ", app->helper->stringToChar(skin));
  158. return skin;
  159. }
  160. void Helper::applyShadow(QWidget * obj, QString color, int blur, int offset_x, int offset_y){
  161. QGraphicsDropShadowEffect *pLabelTextShadowEffect = new QGraphicsDropShadowEffect(obj);
  162. pLabelTextShadowEffect->setColor(QColor(color));
  163. pLabelTextShadowEffect->setBlurRadius(blur);
  164. pLabelTextShadowEffect->setOffset(offset_x, offset_y);
  165. obj->setGraphicsEffect(pLabelTextShadowEffect );
  166. }
  167. void Helper::myDialogBox(QString text, QString info, QString okText, QString cancelText, QString image, QString okAnswer, QString cancelAnswer,
  168. int w, int h, bool, bool is_cancel, bool prevent_dublicates){
  169. if (last_message_msg == info && prevent_dublicates)
  170. return;
  171. last_message_msg = info;
  172. App *app = &App::getInstance();
  173. if(okAnswer == "") okAnswer = "close";
  174. app->helper->dialog_ok_answer = okAnswer;
  175. if(cancelAnswer == "") cancelAnswer = "close";
  176. app->helper->dialog_cancel_answer = cancelAnswer;
  177. app->window->ui->okButton->setText(okText);
  178. app->window->ui->cancelButton->setText(cancelText);
  179. app->window->ui->dialogbox->setGeometry(0, 0, app->window->width(), app->window->height());
  180. QSettings settings( app->config->getSkinFile(), QSettings::IniFormat );
  181. settings.beginGroup("mainWindow");
  182. int left = (settings.value("width").toInt() - w)/2;
  183. int top = (settings.value("height").toInt() - h)/2;
  184. app->window->ui->dialog->setGeometry(QRect(left, top, w, h));
  185. settings.endGroup();
  186. int image_height = app->window->ui->dialog->rect().height() - app->window->ui->dialogHeader->rect().height();
  187. app->window->ui->dialogImage->setGeometry(0,
  188. app->window->ui->dialogHeader->rect().bottom(),
  189. image_height/2 ,image_height
  190. );
  191. QString skin = app->helper->getSkinName();
  192. QString iconpath = "";
  193. iconpath = FileSystem::fileExists("styles/" + skin + "/" + image) ? "styles/" + skin + "/" + image : ":/res/styles/" + skin + "/" + image;
  194. QPixmap icon(iconpath);
  195. app->window->ui->dialogImage->setPixmap(icon.scaled(app->window->ui->dialogImage->width(), app->window->ui->dialogImage->height()));
  196. if(icon.isNull()){
  197. app->window->ui->dialogImage->setGeometry(QRect(10, 10, 10, 10));
  198. }
  199. app->window->ui->dialogHeader->setGeometry(
  200. app->window->ui->dialogHeader->rect().left(),
  201. app->window->ui->dialogHeader->rect().top(),
  202. app->window->ui->dialog->rect().width(),
  203. app->window->ui->dialogHeader->rect().height()
  204. );
  205. app->window->ui->dialogInfo->setGeometry(app->window->ui->dialogImage->rect().right(),
  206. app->window->ui->dialogHeader->rect().bottom(),
  207. app->window->ui->dialog->rect().width() - app->window->ui->dialogImage->rect().width()-7,
  208. image_height - app->window->ui->cancelButton->height()
  209. );
  210. if(is_cancel){
  211. app->window->ui->cancelButton->setGeometry(
  212. app->window->ui->dialog->rect().left() + app->window->ui->dialog->width() - app->window->ui->cancelButton->width() - 20,
  213. app->window->ui->dialog->rect().bottom() - app->window->ui->cancelButton->height() - 10,
  214. app->window->ui->cancelButton->width(),
  215. app->window->ui->cancelButton->height()
  216. );
  217. app->window->ui->okButton->setGeometry(
  218. app->window->ui->dialog->rect().left() + app->window->ui->dialog->width() - app->window->ui->cancelButton->width() - app->window->ui->cancelButton->width() - 40,
  219. app->window->ui->dialog->rect().bottom() - app->window->ui->cancelButton->height() - 10,
  220. app->window->ui->cancelButton->width(),
  221. app->window->ui->cancelButton->height()
  222. );
  223. } else {
  224. app->window->ui->cancelButton->setGeometry(-1000,-1000, app->window->ui->cancelButton->width(), app->window->ui->cancelButton->height());
  225. app->window->ui->okButton->setGeometry(
  226. app->window->ui->dialog->rect().left() + app->window->ui->dialog->width() - app->window->ui->cancelButton->width() - 20,
  227. app->window->ui->dialog->rect().bottom() - app->window->ui->cancelButton->height() - 10,
  228. app->window->ui->cancelButton->width(),
  229. app->window->ui->cancelButton->height()
  230. );
  231. }
  232. app->window->ui->dialogHeader->setText(text);
  233. app->window->ui->dialogInfo->setText(info);
  234. app->window->ui->dialogImage->setText("");
  235. app->window->ui->dialogbox->show();
  236. }
  237. void Helper::setState(QString state){
  238. App *app = &App::getInstance();
  239. if(state == "busy"){
  240. app->window->ui->closeButton->setEnabled(false);
  241. app->window->ui->enterButton->setEnabled(false);
  242. app->window->ui->lotropathButton->setEnabled(false);
  243. app->window->ui->repairButton->setEnabled(false);
  244. app->state = "busy";
  245. app->window->ui->loader->show();
  246. }
  247. if(state == "free"){
  248. app->window->ui->closeButton->setEnabled(true);
  249. app->window->ui->enterButton->setEnabled(true);
  250. app->window->ui->lotropathButton->setEnabled(true);
  251. app->window->ui->repairButton->setEnabled(true);
  252. app->state = "free";
  253. app->window->ui->loader->hide();
  254. }
  255. if(state == "nofolder"){
  256. app->window->ui->enterButton->setEnabled(false);
  257. app->state = "nofolder";
  258. app->window->ui->loader->hide();
  259. }
  260. if(state == "runfirst"){
  261. app->window->ui->enterButton->setEnabled(false);
  262. app->window->ui->repairButton->setEnabled(false);
  263. app->state = "runfirst";
  264. app->window->ui->loader->hide();
  265. QString info = "Вы запускаете программу русификации LotRO впервые. Мастер установки поможет вам настроить программу для ее дальнейшего использования. Сейчас будет осуществлена попытка автоматически определить каталог с игрой. Для продолжения нажмите «Далее».";
  266. app->helper->myDialogBox("Добро пожаловать!", info, "Далее", "Отмена", "gandalf.png", "close gamefind", "", 400, 200, true, false);
  267. }
  268. }
  269. int Helper::dateToTimestamp(QString customDateString, QString format){
  270. int timestamp = QDateTime::fromString(customDateString, format).toTime_t();
  271. return timestamp;
  272. }
  273. void Helper::addComboItem(QComboBox* combo, QString itemName, QIcon* icon){
  274. combo->insertItem(0, itemName);
  275. combo->setItemIcon(0, *icon);
  276. }
  277. const char * Helper::stringToChar(QString val){
  278. return val.toLocal8Bit().toStdString().c_str();
  279. }
  280. void Helper::replaceSkin(QString name){
  281. App *app = &App::getInstance();
  282. QString text = "Смена темы оформления";
  283. QString info = "Выбрана тема " + name + ". Изменения вступят в силу при следующем запуске лаунчера.";
  284. //app->helper->myDialogBox(text, info, "OK", "Отмена", "gandalf.png", "", "", 400, 120, true, false);
  285. qInfo("%s:%i: %s", __FILE__, __LINE__, "Оформляем главное окно.");
  286. QFile styleF;
  287. QString skin = app->helper->getSkinName();
  288. styleF.setFileName(app->app_folder + "styles/" + name + "/style.css");
  289. styleF.open(QFile::ReadOnly);
  290. QString qssStr = styleF.readAll();
  291. qApp->setStyleSheet(qssStr);
  292. QPixmap maskPix(":/res/styles/" + name + "/bg.png");
  293. QPixmap maskPixLocal(app->app_folder + "styles/" + name + "/bg.png");
  294. maskPix = maskPix.isNull() ? maskPixLocal : maskPix;
  295. app->window->setMask(maskPix.mask());
  296. QString color = app->helper->htmlColors.contains("a") ? app->helper->htmlColors.lowerBound("a").value() : "blue";
  297. app->config->loadSkin();
  298. QString hrefcolor = app->helper->htmlColors.contains("a") ? app->helper->htmlColors.lowerBound("a").value() : "blue";
  299. QString newtext = app->window->ui->news1Title->text().replace(color, hrefcolor);
  300. app->window->ui->news1Title->setText(newtext);
  301. newtext = app->window->ui->news2Title->text().replace(color, hrefcolor);
  302. app->window->ui->news2Title->setText(newtext);
  303. newtext = app->window->ui->news3Title->text().replace(color, hrefcolor);
  304. app->window->ui->news3Title->setText(newtext);
  305. newtext = app->window->ui->news4Title->text().replace(color, hrefcolor);
  306. app->window->ui->news4Title->setText(newtext);
  307. app->helper->loadHelp();
  308. QMovie * movie = new QMovie(":/res/styles/" + skin + "/loader.gif");
  309. if(!movie->isValid()) movie = new QMovie(app->app_folder + "styles/" + skin + "/loader.gif");
  310. app->window->ui->loader->setMovie(movie);
  311. movie->setScaledSize(QSize(app->window->ui->loader->width(), app->window->ui->loader->height()));
  312. movie->start();
  313. }