1 /****************************************************************************************
2  * Copyright (c) 2008-2012 Soren Harward <stharward@gmail.com>                          *
3  *                                                                                      *
4  * This program is free software; you can redistribute it and/or modify it under        *
5  * the terms of the GNU General Public License as published by the Free Software        *
6  * Foundation; either version 2 of the License, or (at your option) any later           *
7  * version.                                                                             *
8  *                                                                                      *
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
11  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
12  *                                                                                      *
13  * You should have received a copy of the GNU General Public License along with         *
14  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
15  ****************************************************************************************/
16 
17 #ifndef APG_PRESET_MODEL
18 #define APG_PRESET_MODEL
19 
20 #include "Preset.h"
21 
22 #include <QAbstractItemModel>
23 #include <QFileDialog>
24 #include <QList>
25 #include <QString>
26 
27 class QPersistentModelIndex;
28 
29 namespace APG {
30     class PresetModel : public QAbstractListModel {
31         Q_OBJECT
32 
33         public:
34             static PresetModel* instance();
35             static void destroy();
36             static const QString presetExamples; // holds a hard-coded set of example presets
37                                                  // that are loaded the first time the
38                                                  // user opens the APG
39 
40             // Overloaded QAbstractListModel methods
41             QVariant data( const QModelIndex&, int role = Qt::DisplayRole ) const override;
42             QModelIndex index( int, int, const QModelIndex& parent = QModelIndex() ) const override;
parent(const QModelIndex &)43             QModelIndex parent( const QModelIndex& ) const override { return QModelIndex(); }
44             int rowCount( const QModelIndex& parent = QModelIndex() ) const override;
columnCount(const QModelIndex &)45             int columnCount( const QModelIndex& ) const override { return 1; }
46 
47             APG::PresetPtr activePreset() const;
48 
49         Q_SIGNALS:
50             void lock( bool ); // disable the edit widgets if the solver is running
51 
52         public Q_SLOTS:
53             void addNew();
54             void edit();
55             void editPreset( const QModelIndex& );
56             void exportActive();
57             void import();
58             void removeActive();
59             void runGenerator( int );
60             void setActivePreset( const QModelIndex& );
61             void savePresetsToXmlDefault() const; // force saving to default location
62 
63         private Q_SLOTS:
64             void savePresetsToXml( const QString&, const QList<APG::PresetPtr> & ) const;
65             void loadPresetsFromXml( const QString&, bool createDefaults = false );
66 
67         private:
68             PresetModel();
69             ~PresetModel() override;
70             static PresetModel* s_instance;
71 
72             class ExportDialog;
73 
74             void insertPreset(const APG::PresetPtr&);
75             void parseXmlToPresets( QDomDocument& );
76 
77             QPersistentModelIndex* m_activePresetIndex;
78             QList<APG::PresetPtr> m_presetList;
79     }; // class PresetModel
80 
81     class PresetModel::ExportDialog : public QFileDialog {
82         Q_OBJECT
83 
84         public:
85             ExportDialog( APG::PresetPtr );
86             ~ExportDialog() override;
87 
88         Q_SIGNALS:
89             void pleaseExport( const QString&, const QList<APG::PresetPtr> ) const;
90 
91         private Q_SLOTS:
92             void recvAccept() const;
93 
94         private:
95             QList<APG::PresetPtr> m_presetsToExportList;
96     };
97 } //namespace APG
98 
99 #endif // APG_PRESET_MODEL
100