PageRenderTime 83ms CodeModel.GetById 10ms app.highlight 68ms RepoModel.GetById 1ms app.codeStats 1ms

/src/importexportdialog.cpp

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