/src/config/configurestyle.cpp

https://github.com/lbellonda/qxmledit · C++ · 141 lines · 108 code · 11 blank · 22 comment · 16 complexity · 2b8fbc94095f3f8b576af31aac7823de MD5 · raw file

  1. /**************************************************************************
  2. * This file is part of QXmlEdit *
  3. * Copyright (C) 2011-2018 by Luca Bellonda and individual contributors *
  4. * as indicated in the AUTHORS file *
  5. * lbellonda _at_ gmail.com *
  6. * *
  7. * This library is free software; you can redistribute it and/or *
  8. * modify it under the terms of the GNU Library General Public *
  9. * License as published by the Free Software Foundation; either *
  10. * version 2 of the License, or (at your option) any later version. *
  11. * *
  12. * This library is distributed in the hope that it will be useful, *
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  15. * Library General Public License for more details. *
  16. * *
  17. * You should have received a copy of the GNU Library General Public *
  18. * License along with this library; if not, write to the *
  19. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
  20. * Boston, MA 02110-1301 USA *
  21. **************************************************************************/
  22. #include "configurestyle.h"
  23. #include "ui_configurestyle.h"
  24. #include "utils.h"
  25. #include "qxmleditconfig.h"
  26. #include <QDir>
  27. #include <QUrl>
  28. #include <QDesktopServices>
  29. ConfigureStyle::ConfigureStyle(QWidget *parent) :
  30. QWidget(parent),
  31. _data(NULL),
  32. ui(new Ui::ConfigureStyle)
  33. {
  34. ui->setupUi(this);
  35. _group.addButton(ui->pgmLocation);
  36. _group.addButton(ui->pgmThisDirectory);
  37. _group.addButton(ui->pgmDefault);
  38. //connect( &_group, SIGNAL(buttonClicked ( QAbstractButton * button )), this, SLOT(buttonClicked ( QAbstractButton * button )) );
  39. }
  40. ConfigureStyle::~ConfigureStyle()
  41. {
  42. delete ui;
  43. }
  44. void ConfigureStyle::changeEvent(QEvent *e)
  45. {
  46. QWidget::changeEvent(e);
  47. switch(e->type()) {
  48. case QEvent::LanguageChange:
  49. ui->retranslateUi(this);
  50. break;
  51. default:
  52. break;
  53. }
  54. }
  55. void ConfigureStyle::init(ApplicationData* data)
  56. {
  57. _data = data ;
  58. QString stylesDirectory = _data->getStylesDirSpecific();
  59. ui->aDirectory->setText(stylesDirectory);
  60. stylesDirectory = _data->getStylesDirStandard();
  61. ui->stdDirValue->setText(stylesDirectory);
  62. stylesDirectory = _data->getStylesDirApplication();
  63. ui->pgmDirValue->setText(stylesDirectory);
  64. EStylesDir type = _data->getStylesDirLocation();
  65. switch(type) {
  66. case ESTYLE_DIR_USEAPPL:
  67. ui->pgmLocation->setChecked(true);
  68. break;
  69. case ESTYLE_DIR_USEDIR:
  70. ui->pgmThisDirectory->setChecked(true);
  71. break;
  72. default:
  73. ui->pgmDefault->setChecked(true);
  74. break;
  75. }
  76. }
  77. void ConfigureStyle::save()
  78. {
  79. if(ui->pgmLocation->isChecked()) {
  80. _data->setStylesDirLocation(ESTYLE_DIR_USEAPPL);
  81. } else if(ui->pgmThisDirectory->isChecked()) {
  82. QString dir = ui->aDirectory->text();
  83. if(dir.isEmpty()) {
  84. Utils::error(tr("Data directory not specified"));
  85. return ;
  86. }
  87. _data->setStylesDirLocation(ESTYLE_DIR_USEDIR);
  88. Config::saveString(Config::KEY_STYLE_DATADIR, dir);
  89. } else {
  90. _data->setStylesDirLocation(ESTYLE_DIR_USESTD);
  91. }
  92. }
  93. void ConfigureStyle::saveIfChanged()
  94. {
  95. save();
  96. }
  97. void ConfigureStyle::on_openLocation_clicked()
  98. {
  99. bool isOk = false;
  100. if(ui->pgmLocation->isChecked()) {
  101. QDir dir(_data->getStylesDirApplication());
  102. if(!dir.exists()) {
  103. dir.mkpath(_data->getStylesDirApplication());
  104. }
  105. isOk = QDesktopServices::openUrl(QUrl::fromLocalFile(_data->getStylesDirApplication()));
  106. } else if(ui->pgmThisDirectory->isChecked()) {
  107. if(ui->aDirectory->text().isEmpty()) {
  108. Utils::error(this, tr("The directory name is empty."));
  109. return ;
  110. }
  111. isOk = QDesktopServices::openUrl(QUrl::fromLocalFile(ui->aDirectory->text()));
  112. } else {
  113. QDir dir(_data->getStylesDirStandard());
  114. if(!dir.exists()) {
  115. dir.mkpath(_data->getStylesDirStandard());
  116. }
  117. isOk = QDesktopServices::openUrl(QUrl::fromLocalFile(_data->getStylesDirStandard()));
  118. }
  119. if(!isOk) {
  120. Utils::error(this, tr("An error occurred while opening location."));
  121. }
  122. }
  123. void ConfigureStyle::on_browseFolderCmd_clicked()
  124. {
  125. QString dirPath = QFileDialog::getExistingDirectory(this, tr("Choose the directory"),
  126. ui->aDirectory->text(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
  127. if(!dirPath.isEmpty()) {
  128. ui->aDirectory->setText(dirPath);
  129. }
  130. }