File indexing completed on 2024-05-05 04:48:48

0001 /****************************************************************************************
0002  * Copyright (c) 2008-2012 Soren Harward <stharward@gmail.com>                          *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #ifndef APG_PRESET_MODEL
0018 #define APG_PRESET_MODEL
0019 
0020 #include "Preset.h"
0021 
0022 #include <QAbstractItemModel>
0023 #include <QFileDialog>
0024 #include <QList>
0025 #include <QString>
0026 
0027 class QPersistentModelIndex;
0028 
0029 namespace APG {
0030     class PresetModel : public QAbstractListModel {
0031         Q_OBJECT
0032 
0033         public:
0034             static PresetModel* instance();
0035             static void destroy();
0036             static const QString presetExamples; // holds a hard-coded set of example presets
0037                                                  // that are loaded the first time the
0038                                                  // user opens the APG
0039 
0040             // Overloaded QAbstractListModel methods
0041             QVariant data( const QModelIndex&, int role = Qt::DisplayRole ) const override;
0042             QModelIndex index( int, int, const QModelIndex& parent = QModelIndex() ) const override;
0043             QModelIndex parent( const QModelIndex& ) const override { return QModelIndex(); }
0044             int rowCount( const QModelIndex& parent = QModelIndex() ) const override;
0045             int columnCount( const QModelIndex& ) const override { return 1; }
0046 
0047             APG::PresetPtr activePreset() const;
0048 
0049         Q_SIGNALS:
0050             void lock( bool ); // disable the edit widgets if the solver is running
0051 
0052         public Q_SLOTS:
0053             void addNew();
0054             void edit();
0055             void editPreset( const QModelIndex& );
0056             void exportActive();
0057             void import();
0058             void removeActive();
0059             void runGenerator( int );
0060             void setActivePreset( const QModelIndex& );
0061             void savePresetsToXmlDefault() const; // force saving to default location
0062 
0063         private Q_SLOTS:
0064             void savePresetsToXml( const QString&, const QList<APG::PresetPtr> & ) const;
0065             void loadPresetsFromXml( const QString&, bool createDefaults = false );
0066 
0067         private:
0068             PresetModel();
0069             ~PresetModel() override;
0070             static PresetModel* s_instance;
0071 
0072             class ExportDialog;
0073 
0074             void insertPreset(const APG::PresetPtr&);
0075             void parseXmlToPresets( QDomDocument& );
0076 
0077             QPersistentModelIndex* m_activePresetIndex;
0078             QList<APG::PresetPtr> m_presetList;
0079     }; // class PresetModel
0080 
0081     class PresetModel::ExportDialog : public QFileDialog {
0082         Q_OBJECT
0083 
0084         public:
0085             ExportDialog( APG::PresetPtr );
0086             ~ExportDialog() override;
0087 
0088         Q_SIGNALS:
0089             void pleaseExport( const QString&, const QList<APG::PresetPtr> ) const;
0090 
0091         private Q_SLOTS:
0092             void recvAccept() const;
0093 
0094         private:
0095             QList<APG::PresetPtr> m_presetsToExportList;
0096     };
0097 } //namespace APG
0098 
0099 #endif // APG_PRESET_MODEL