/src/importexportdialog.cpp

https://code.google.com/p/dwarftherapist/ · C++ · 382 lines · 325 code · 31 blank · 26 comment · 28 complexity · d6f7a6058ed7bd43eb39fdeabe3e06d8 MD5 · raw file

  1. /*
  2. Dwarf Therapist
  3. Copyright (c) 2009 Trey Stout (chmod)
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #include "importexportdialog.h"
  21. #include "ui_importexportdialog.h"
  22. #include "dwarftherapist.h"
  23. #include "customprofession.h"
  24. #include "version.h"
  25. #include "gamedatareader.h"
  26. #include "labor.h"
  27. #include "mainwindow.h"
  28. #include "viewmanager.h"
  29. #include "gridview.h"
  30. #include "viewcolumnset.h"
  31. #include "truncatingfilelogger.h"
  32. ImportExportDialog::ImportExportDialog(QWidget *parent)
  33. : QDialog(parent)
  34. , ui(new Ui::ImportExportDialog)
  35. {
  36. ui->setupUi(this);
  37. connect(ui->btn_select_none, SIGNAL(clicked()), SLOT(clear_selection()));
  38. connect(ui->btn_select_all, SIGNAL(clicked()), SLOT(select_all()));
  39. }
  40. void ImportExportDialog::setup_for_profession_export() {
  41. m_mode = MODE_EXPORT_PROFESSIONS;
  42. QString default_path = QString("%1/%2")
  43. .arg(QDesktopServices::storageLocation(QDesktopServices::DesktopLocation))
  44. .arg("custom_professions.dtp");
  45. m_path = QFileDialog::getSaveFileName(this, tr("Choose a file to export to"), default_path,
  46. "Dwarf Therapist Profession Exports (*.dtp);;All Files (*.*)");
  47. if (m_path.isEmpty())
  48. return; // they cancelled
  49. LOGD << "exporting custom professions to:" << m_path;
  50. setWindowTitle(tr("Export Custom Professions"));
  51. ui->buttonBox->addButton(tr("Export Selected"), QDialogButtonBox::YesRole);
  52. Version v;
  53. QDateTime t = QDateTime::currentDateTime();
  54. ui->lbl_file_path->setText(m_path);
  55. ui->lbl_version->setText(v.to_string());
  56. ui->lbl_export_time->setText(t.toString());
  57. ui->lbl_professions_count->setText(QString::number(0));
  58. foreach(CustomProfession *cp, DT->get_custom_professions()) {
  59. QString title = QString("%1 (%2)").arg(cp->get_name()).arg(cp->get_enabled_labors().size());
  60. QListWidgetItem *i = new QListWidgetItem(title, ui->list_professions);
  61. i->setData(Qt::UserRole, cp->get_name());
  62. i->setData(Qt::UserRole+1, false); // not conflicting as far as we know
  63. i->setCheckState(Qt::Checked);
  64. m_profs << cp;
  65. }
  66. }
  67. void ImportExportDialog::setup_for_profession_import() {
  68. m_mode = MODE_IMPORT_PROFESSIONS;
  69. QString default_path = QString("%1/%2").arg(QDesktopServices::storageLocation(QDesktopServices::DesktopLocation), "custom_professions.dtp");
  70. m_path = QFileDialog::getOpenFileName(this, tr("Choose a file to import"), default_path,
  71. "Dwarf Therapist Profession Exports (*.dtp);;All Files (*.*)");
  72. if (m_path.isEmpty())
  73. return; // they cancelled
  74. LOGD << "importing custom professions from:" << m_path;
  75. setWindowTitle(tr("Import Custom Professions"));
  76. ui->buttonBox->addButton(tr("Import Selected"), QDialogButtonBox::YesRole);
  77. QSettings s(m_path, QSettings::IniFormat);
  78. /* don't need to check versions yet, since everything will be compatible */
  79. Version file_version;
  80. file_version.major = s.value("info/DT_version/major", 0).toInt();
  81. file_version.minor = s.value("info/DT_version/minor", 0).toInt();
  82. file_version.patch = s.value("info/DT_version/patch", 0).toInt();
  83. QDateTime t = s.value("info/export_date").toDateTime();
  84. int cnt = s.beginReadArray("custom_professions");
  85. for(int i = 0; i < cnt; i++) {
  86. s.setArrayIndex(i);
  87. CustomProfession *cp = new CustomProfession(DT);
  88. cp->set_name(s.value("name", "UNKNOWN").toString());
  89. int labor_cnt = s.beginReadArray("labors");
  90. for(int j = 0; j < labor_cnt; ++j) {
  91. s.setArrayIndex(j);
  92. cp->add_labor(s.childKeys()[0].toInt());
  93. }
  94. s.endArray();
  95. m_profs << cp;
  96. }
  97. s.endArray();
  98. ui->lbl_file_path->setText(m_path);
  99. ui->lbl_version->setText(file_version.to_string());
  100. ui->lbl_export_time->setText(t.toString());
  101. ui->lbl_professions_count->setText(QString::number(m_profs.size()));
  102. foreach(CustomProfession *cp, m_profs) {
  103. QString title = QString("%1 (%2)").arg(cp->get_name())
  104. .arg(cp->get_enabled_labors().size());
  105. QListWidgetItem *i = new QListWidgetItem(title, ui->list_professions);
  106. i->setData(Qt::UserRole, cp->get_name());
  107. i->setData(Qt::UserRole+1, false); // not conflicting as far as we know
  108. i->setCheckState(Qt::Checked);
  109. QString tooltip = "<h3>Enabled Labors</h3><ul>";
  110. GameDataReader *gdr = GameDataReader::ptr();
  111. foreach(int labor_id, cp->get_enabled_labors()) {
  112. QString labor_name = "UNKNOWN";
  113. Labor *l = gdr->get_labor(labor_id);
  114. if (l) {
  115. labor_name = l->name;
  116. } else {
  117. LOGE << tr("custom profession lists labor_id %1, which is "
  118. "unrecognized!").arg(labor_id);
  119. }
  120. tooltip += QString("<li>%1</li>").arg(labor_name);
  121. }
  122. tooltip += "</ul>";
  123. i->setToolTip(tooltip);
  124. // watch out for conflicts!
  125. if (DT->get_custom_profession(cp->get_name())) {
  126. i->setTextColor(Qt::red);
  127. i->setData(Qt::UserRole+1, true); // conflicting flag
  128. i->setText(i->text() + " CONFLICT");
  129. i->setCheckState(Qt::Unchecked);
  130. i->setFlags(Qt::NoItemFlags);
  131. i->setToolTip(tr("You already have a custom profession with this name!"));
  132. }
  133. }
  134. }
  135. void ImportExportDialog::setup_for_gridview_export() {
  136. m_mode = MODE_EXPORT_GRIDVIEWS;
  137. QString default_path = QString("%1/%2")
  138. .arg(QDesktopServices::storageLocation(QDesktopServices::DesktopLocation))
  139. .arg("gridviews.dtg");
  140. m_path = QFileDialog::getSaveFileName(this, tr("Choose a file to export to"),
  141. default_path, "Dwarf Therapist Grid View Exports (*.dtg);;All Files (*.*)");
  142. if (m_path.isEmpty())
  143. return; // they cancelled
  144. LOGD << "exporting grid views to:" << m_path;
  145. setWindowTitle(tr("Export Grid Views"));
  146. ui->buttonBox->addButton(tr("Export Selected"), QDialogButtonBox::YesRole);
  147. Version v;
  148. QDateTime t = QDateTime::currentDateTime();
  149. ui->lbl_file_path->setText(m_path);
  150. ui->lbl_version->setText(v.to_string());
  151. ui->lbl_export_time->setText(t.toString());
  152. ui->lbl_professions_count->setText(QString::number(0));
  153. foreach(GridView *gv, DT->get_main_window()->get_view_manager()->views()) {
  154. QListWidgetItem *i = new QListWidgetItem(gv->name(), ui->list_professions);
  155. i->setData(Qt::UserRole, gv->name());
  156. i->setData(Qt::UserRole+1, false); // not conflicting as far as we know
  157. i->setCheckState(Qt::Checked);
  158. m_views << gv;
  159. }
  160. }
  161. void ImportExportDialog::setup_for_gridview_import() {
  162. m_mode = MODE_IMPORT_GRIDVIEWS;
  163. QString default_path = QString("%1/%2")
  164. .arg(QDesktopServices::storageLocation(QDesktopServices::DesktopLocation))
  165. .arg("gridviews.dtg");
  166. m_path = QFileDialog::getOpenFileName(this, tr("Choose a file to import"),
  167. default_path, "Dwarf Therapist Grid View Exports (*.dtg);;All Files (*.*)");
  168. LOGD << "importing grid views from:" << m_path;
  169. setWindowTitle(tr("Import Grid Views"));
  170. ui->buttonBox->addButton(tr("Import Selected"), QDialogButtonBox::YesRole);
  171. QSettings s(m_path, QSettings::IniFormat);
  172. /* don't need to check versions yet, since everything will be compatible */
  173. Version file_version;
  174. file_version.major = s.value("info/DT_version/major", 0).toInt();
  175. file_version.minor = s.value("info/DT_version/minor", 0).toInt();
  176. file_version.patch = s.value("info/DT_version/patch", 0).toInt();
  177. QDateTime t = s.value("info/export_date").toDateTime();
  178. ViewManager *view_mgr = DT->get_main_window()->get_view_manager();
  179. int cnt = s.beginReadArray("gridviews");
  180. for(int i = 0; i < cnt; i++) {
  181. s.setArrayIndex(i);
  182. m_views << GridView::read_from_ini(s, this);
  183. }
  184. s.endArray();
  185. ui->lbl_file_path->setText(m_path);
  186. ui->lbl_version->setText(file_version.to_string());
  187. ui->lbl_export_time->setText(t.toString());
  188. ui->lbl_professions_count->setText(QString::number(m_views.size()));
  189. foreach(GridView *gv, m_views) {
  190. QListWidgetItem *i = new QListWidgetItem(gv->name(), ui->list_professions);
  191. i->setData(Qt::UserRole, gv->name());
  192. i->setData(Qt::UserRole+1, false); // not conflicting as far as we know
  193. i->setCheckState(Qt::Checked);
  194. QString tooltip = "<h3>BreakDown</h3><ul>";
  195. foreach(ViewColumnSet *set, gv->sets()) {
  196. tooltip += QString("<li>SET: %1 (%2 cols)</li>")
  197. .arg(set->name())
  198. .arg(set->columns().size());
  199. }
  200. tooltip += "</ul>";
  201. i->setToolTip(tooltip);
  202. // watch out for conflicts!
  203. if (view_mgr->get_view(gv->name())) {
  204. i->setTextColor(Qt::red);
  205. i->setData(Qt::UserRole+1, true); // conflicting flag
  206. i->setText(i->text() + " CONFLICT");
  207. i->setCheckState(Qt::Unchecked);
  208. i->setFlags(Qt::NoItemFlags);
  209. i->setToolTip(tr("You already have a grid view with this name!"));
  210. }
  211. }
  212. }
  213. void ImportExportDialog::select_all() {
  214. for(int i = 0; i < ui->list_professions->count(); ++i) {
  215. QListWidgetItem *item = static_cast<QListWidgetItem*>(ui->list_professions->item(i));
  216. if (item->data(Qt::UserRole + 1).toBool())
  217. continue;
  218. ui->list_professions->item(i)->setCheckState(Qt::Checked);
  219. }
  220. }
  221. void ImportExportDialog::clear_selection() {
  222. for(int i = 0; i < ui->list_professions->count(); ++i) {
  223. ui->list_professions->item(i)->setCheckState(Qt::Unchecked);
  224. }
  225. }
  226. QVector<CustomProfession*> ImportExportDialog::get_profs() {
  227. QVector<CustomProfession*> out;
  228. for(int i = 0; i < ui->list_professions->count(); ++i) {
  229. QListWidgetItem *item = static_cast<QListWidgetItem*>(ui->list_professions->item(i));
  230. if (item->checkState() != Qt::Checked)
  231. continue;
  232. QString name = item->data(Qt::UserRole).toString();
  233. foreach(CustomProfession *cp, m_profs) {
  234. if (name == cp->get_name())
  235. out << cp;
  236. }
  237. }
  238. return out;
  239. }
  240. QVector<GridView*> ImportExportDialog::get_views() {
  241. QVector<GridView*> out;
  242. for(int i = 0; i < ui->list_professions->count(); ++i) {
  243. QListWidgetItem *item = static_cast<QListWidgetItem*>(ui->list_professions->item(i));
  244. if (item->checkState() != Qt::Checked)
  245. continue;
  246. QString name = item->data(Qt::UserRole).toString();
  247. foreach(GridView *gv, m_views) {
  248. if (name == gv->name())
  249. out << gv;
  250. }
  251. }
  252. return out;
  253. }
  254. void ImportExportDialog::accept() {
  255. switch(m_mode) {
  256. case MODE_EXPORT_PROFESSIONS:
  257. export_selected_professions();
  258. break;
  259. case MODE_IMPORT_PROFESSIONS:
  260. import_selected_professions();
  261. break;
  262. case MODE_EXPORT_GRIDVIEWS:
  263. export_selected_gridviews();
  264. break;
  265. case MODE_IMPORT_GRIDVIEWS:
  266. import_selected_gridviews();
  267. break;
  268. default:
  269. QMessageBox::warning(this, tr("Oh no!"), tr("Unknown import/export type!"));
  270. return;
  271. }
  272. return QDialog::accept();
  273. }
  274. void ImportExportDialog::export_selected_professions() {
  275. QSettings s(m_path, QSettings::IniFormat);
  276. s.remove(""); // clear out the file if there was anything there.
  277. Version v;
  278. s.setValue("info/DT_version/major", v.major);
  279. s.setValue("info/DT_version/minor", v.minor);
  280. s.setValue("info/DT_version/patch", v.patch);
  281. s.setValue("info/export_date", QDateTime::currentDateTime());
  282. int exported = 0;
  283. int i = 0;
  284. s.beginWriteArray("custom_professions");
  285. foreach(CustomProfession *cp, get_profs()) {
  286. s.setArrayIndex(i++);
  287. s.setValue("name", cp->get_name());
  288. s.beginWriteArray("labors");
  289. int j = 0;
  290. foreach(int labor_id, cp->get_enabled_labors()) {
  291. s.setArrayIndex(j++);
  292. s.setValue(QString::number(labor_id), true);
  293. }
  294. s.endArray();
  295. exported++;
  296. }
  297. s.endArray();
  298. s.sync();
  299. if (exported)
  300. QMessageBox::information(this, tr("Export Successful"),
  301. tr("Exported %n custom profession(s)", "", exported));
  302. }
  303. void ImportExportDialog::import_selected_professions() {
  304. int imported = 0;
  305. foreach(CustomProfession *cp, get_profs()) {
  306. DT->add_custom_profession(cp);
  307. imported++;
  308. }
  309. if (imported)
  310. QMessageBox::information(this, tr("Import Successful"),
  311. tr("Imported %n custom profession(s)", "", imported));
  312. }
  313. void ImportExportDialog::export_selected_gridviews() {
  314. QSettings s(m_path, QSettings::IniFormat);
  315. s.remove(""); // clear out the file if there was anything there.
  316. Version v;
  317. s.setValue("info/DT_version/major", v.major);
  318. s.setValue("info/DT_version/minor", v.minor);
  319. s.setValue("info/DT_version/patch", v.patch);
  320. s.setValue("info/export_date", QDateTime::currentDateTime());
  321. int exported = 0;
  322. int i = 0;
  323. s.beginWriteArray("gridviews");
  324. foreach(GridView *gv, get_views()) {
  325. s.setArrayIndex(i++);
  326. gv->write_to_ini(s);
  327. exported++;
  328. }
  329. s.endArray();
  330. s.sync();
  331. if (exported)
  332. QMessageBox::information(this, tr("Export Successful"),
  333. tr("Exported %n grid view(s)", "", exported));
  334. }
  335. void ImportExportDialog::import_selected_gridviews() {
  336. int imported = 0;
  337. ViewManager *view_mgr = DT->get_main_window()->get_view_manager();
  338. foreach(GridView *gv, get_views()) {
  339. view_mgr->add_view(gv);
  340. imported++;
  341. }
  342. if (imported) {
  343. QMessageBox::information(this, tr("Import Successful"),
  344. tr("Imported %n grid view(s)", "", imported));
  345. view_mgr->write_views();
  346. view_mgr->reload_views();
  347. }
  348. }