PageRenderTime 48ms CodeModel.GetById 1ms RepoModel.GetById 4ms app.codeStats 0ms

/src/UIUtils.cc

http://github.com/openscad/openscad
C++ | 182 lines | 128 code | 24 blank | 30 comment | 12 complexity | 4ec9c5be09e376f88567188930f7aba9 MD5 | raw file
Possible License(s): MIT, GPL-2.0
  1. /*
  2. * OpenSCAD (www.openscad.org)
  3. * Copyright (C) 2009-2011 Clifford Wolf <clifford@clifford.at> and
  4. * Marius Kintel <marius@kintel.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * As a special exception, you have permission to link this program
  12. * with the CGAL library and distribute executables, as long as you
  13. * follow the requirements of the GNU GPL in regard to all of the
  14. * software in the executable aside from CGAL.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. */
  26. #include <QDir>
  27. #include <QFileInfo>
  28. #include <QUrl>
  29. #include <QFileDialog>
  30. #include <QDesktopServices>
  31. #include "version.h"
  32. #include "UIUtils.h"
  33. #include "qtgettext.h"
  34. #include "PlatformUtils.h"
  35. #include "QSettingsCached.h"
  36. #include <boost/property_tree/ptree.hpp>
  37. #include <boost/property_tree/json_parser.hpp>
  38. QFileInfo UIUtils::openFile(QWidget *parent)
  39. {
  40. QSettingsCached settings;
  41. QString last_dirname = settings.value("lastOpenDirName").toString();
  42. QString new_filename = QFileDialog::getOpenFileName(parent, "Open File",
  43. last_dirname, "OpenSCAD Designs (*.scad *.csg)");
  44. if (new_filename.isEmpty()) {
  45. return QFileInfo();
  46. }
  47. QFileInfo fileInfo(new_filename);
  48. QDir last_dir = fileInfo.dir();
  49. last_dirname = last_dir.path();
  50. settings.setValue("lastOpenDirName", last_dirname);
  51. return fileInfo;
  52. }
  53. QFileInfoList UIUtils::openFiles(QWidget *parent)
  54. {
  55. QSettingsCached settings;
  56. QString last_dirname = settings.value("lastOpenDirName").toString();
  57. QStringList new_filenames = QFileDialog::getOpenFileNames(parent, "Open File",
  58. last_dirname, "OpenSCAD Designs (*.scad *.csg)");
  59. QFileInfoList fileInfoList;
  60. for(QString filename: new_filenames)
  61. {
  62. if(filename.isEmpty()) {
  63. continue;
  64. }
  65. fileInfoList.append(QFileInfo(filename));
  66. }
  67. if(!fileInfoList.isEmpty())
  68. {
  69. QDir last_dir = fileInfoList[fileInfoList.size() - 1].dir(); // last_dir is set to directory of last choosen valid file
  70. last_dirname = last_dir.path();
  71. settings.setValue("lastOpenDirName", last_dirname);
  72. }
  73. return fileInfoList;
  74. }
  75. QStringList UIUtils::recentFiles()
  76. {
  77. QSettingsCached settings; // set up project and program properly in main.cpp
  78. QStringList files = settings.value("recentFileList").toStringList();
  79. // Remove any duplicate or empty entries from the list
  80. #if (QT_VERSION >= QT_VERSION_CHECK(4, 5, 0))
  81. files.removeDuplicates();
  82. #endif
  83. files.removeAll(QString());
  84. // Now remove any entries which do not exist
  85. for (int i = files.size() - 1; i >= 0; --i) {
  86. QFileInfo fileInfo(files[i]);
  87. if (!QFile(fileInfo.absoluteFilePath()).exists())
  88. files.removeAt(i);
  89. }
  90. while (files.size() > UIUtils::maxRecentFiles) {
  91. files.removeAt(files.size() - 1);
  92. }
  93. settings.setValue("recentFileList", files);
  94. return files;
  95. }
  96. using namespace boost::property_tree;
  97. static ptree *examples_tree = nullptr;
  98. static ptree *examplesTree()
  99. {
  100. if (!examples_tree) {
  101. std::string path = (PlatformUtils::resourcePath("examples") / "examples.json").string();
  102. try {
  103. examples_tree = new ptree;
  104. read_json(path, *examples_tree);
  105. } catch (const std::exception & e) {
  106. PRINTB("Error reading examples.json: %s", e.what());
  107. delete examples_tree;
  108. examples_tree = nullptr;
  109. }
  110. }
  111. return examples_tree;
  112. }
  113. QStringList UIUtils::exampleCategories()
  114. {
  115. // categories in File menu item - Examples
  116. QStringList categories;
  117. ptree *pt = examplesTree();
  118. if (pt) {
  119. for(const auto &v : *pt) {
  120. // v.first is the name of the child.
  121. // v.second is the child tree.
  122. categories << QString::fromStdString(v.first);
  123. }
  124. }
  125. return categories;
  126. }
  127. QFileInfoList UIUtils::exampleFiles(const QString &category)
  128. {
  129. QFileInfoList examples;
  130. ptree *pt = examplesTree();
  131. if (pt) {
  132. fs::path examplesPath = PlatformUtils::resourcePath("examples") / category.toStdString();
  133. for(const auto &v : pt->get_child(category.toStdString())) {
  134. examples << QFileInfo(QString::fromStdString((examplesPath / v.second.data()).string()));
  135. }
  136. }
  137. return examples;
  138. }
  139. void UIUtils::openHomepageURL()
  140. {
  141. QDesktopServices::openUrl(QUrl("https://www.openscad.org/"));
  142. }
  143. static void openVersionedURL(QString url)
  144. {
  145. QDesktopServices::openUrl(QUrl(url.arg(openscad_shortversionnumber.c_str())));
  146. }
  147. void UIUtils::openUserManualURL()
  148. {
  149. openVersionedURL("https://www.openscad.org/documentation.html?version=%1");
  150. }
  151. void UIUtils::openCheatSheetURL()
  152. {
  153. #ifdef OPENSCAD_SNAPSHOT
  154. openVersionedURL("https://www.openscad.org/cheatsheet/snapshot.html?version=%1");
  155. #else
  156. openVersionedURL("https://www.openscad.org/cheatsheet/index.html?version=%1");
  157. #endif
  158. }