1 /*
2  * Copyright (c) 2015-2020, The University of Oxford
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * 1. Redistributions of source code must retain the above copyright notice,
8  *    this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  *    this list of conditions and the following disclaimer in the documentation
11  *    and/or other materials provided with the distribution.
12  * 3. Neither the name of the University of Oxford nor the names of its
13  *    contributors may be used to endorse or promote products derived from this
14  *    software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef OSKAR_SETTINGS_MODEL_H_
30 #define OSKAR_SETTINGS_MODEL_H_
31 
32 #include <QAbstractItemModel>
33 #include <QDateTime>
34 #include <QString>
35 #include <QSortFilterProxyModel>
36 #include <QIcon>
37 
38 class QModelIndex;
39 class QStringList;
40 class QVariant;
41 
42 namespace oskar {
43 
44 class SettingsTree;
45 class SettingsNode;
46 
47 class SettingsModel : public QAbstractItemModel
48 {
49     Q_OBJECT
50 
51 public:
52     enum ModelRole
53     {
54         KeyRole = Qt::UserRole,
55         ValueRole,
56         DefaultRole,
57         TypeRole,
58         OptionsRole,
59         CheckExternalChangesRole,
60         DisplayKeysRole,
61         ItemTypeRole,
62         RangeRole,
63         ExtRangeRole,
64         ResetGroupRole
65     };
66 
67 public:
68     SettingsModel(SettingsTree* settings, QObject* parent = 0);
69     virtual ~SettingsModel();
70     void beginReset();
71     void endReset();
72     int columnCount(const QModelIndex& parent = QModelIndex()) const;
73     QVariant data(const QModelIndex& index, int role) const;
74     Qt::ItemFlags flags(const QModelIndex& index) const;
75     QVariant headerData(int section, Qt::Orientation orientation,
76             int role = Qt::DisplayRole) const;
77     QModelIndex index(int row, int column,
78                       const QModelIndex& parent = QModelIndex()) const;
79     void load_settings_file(const QString& filename = QString());
80     void save_settings_file(const QString& filename = QString());
81     QModelIndex parent(const QModelIndex& index) const;
82     void refresh();
83     int rowCount(const QModelIndex& parent = QModelIndex()) const;
84     bool setData(const QModelIndex& index, const QVariant& value,
85             int role = Qt::EditRole);
86 
87 signals:
88     void fileReloaded();
89 
90 private:
91     const SettingsNode* get_node(const QModelIndex& index) const;
92     void refresh(const QModelIndex& parent);
93     void reset_group_(const SettingsNode* node);
94 
95     SettingsTree* settings_;
96     QIcon iconOpen_, iconSave_;
97     QString filename_;
98     QDateTime lastModified_;
99     bool displayKey_;
100 };
101 
102 
103 class SettingsModelFilter : public QSortFilterProxyModel
104 {
105     Q_OBJECT
106 
107 public:
108     SettingsModelFilter(QObject* parent = 0);
109     ~SettingsModelFilter();
110     QVariant data(const QModelIndex& index, int role) const;
111 
112 public slots:
113     void setFilterRegExp(const QString& pattern);
114 
115 protected:
116     bool filterAcceptsChildren(int sourceRow,
117             const QModelIndex& sourceParent) const;
118     bool filterAcceptsCurrentRow(const QModelIndex& idx) const;
119     bool filterAcceptsCurrentRow(int sourceRow,
120             const QModelIndex& sourceParent) const;
121     virtual bool filterAcceptsRow(int sourceRow,
122             const QModelIndex& sourceParent) const;
123 };
124 
125 } /* namespace oskar */
126 
127 #endif /* OSKAR_SETTINGS_MODEL_H_ */
128