1 // qpwgraph_config.cpp
2 //
3 /****************************************************************************
4    Copyright (C) 2021, rncbc aka Rui Nuno Capela. All rights reserved.
5 
6    This program is free software; you can redistribute it and/or
7    modify it under the terms of the GNU General Public License
8    as published by the Free Software Foundation; either version 2
9    of the License, or (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License along
17    with this program; if not, write to the Free Software Foundation, Inc.,
18    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 
20 *****************************************************************************/
21 
22 #include "qpwgraph_config.h"
23 
24 #include <QSettings>
25 
26 #include <QMainWindow>
27 
28 
29 // Local constants.
30 static const char *GeometryGroup    = "/GraphGeometry";
31 static const char *LayoutGroup      = "/GraphLayout";
32 static const char *ViewGroup        = "/GraphView";
33 static const char *ViewMenubarKey   = "/Menubar";
34 static const char *ViewToolbarKey   = "/Toolbar";
35 static const char *ViewStatusbarKey = "/Statusbar";
36 static const char *ViewTextBesideIconsKey = "/TextBesideIcons";
37 static const char *ViewZoomRangeKey = "/ZoomRange";
38 static const char *ViewSortTypeKey  = "/SortType";
39 static const char *ViewSortOrderKey = "/SortOrder";
40 
41 
42 //----------------------------------------------------------------------------
43 // qpwgraph_config --  Canvas state memento.
44 
45 // Constructors.
qpwgraph_config(QSettings * settings,bool owner)46 qpwgraph_config::qpwgraph_config ( QSettings *settings, bool owner )
47 	: m_settings(settings), m_owner(owner),
48 		m_menubar(false), m_toolbar(false), m_statusbar(false),
49 		m_texticons(false), m_zoomrange(false),
50 		m_sorttype(0), m_sortorder(0)
51 {
52 }
53 
54 
qpwgraph_config(const QString & org_name,const QString & app_name)55 qpwgraph_config::qpwgraph_config ( const QString& org_name, const QString& app_name )
56 	: qpwgraph_config(new QSettings(org_name, app_name), true)
57 {
58 }
59 
60 
61 // Destructor.
~qpwgraph_config(void)62 qpwgraph_config::~qpwgraph_config (void)
63 {
64 	setSettings(nullptr);
65 }
66 
67 
68 // Accessors.
setSettings(QSettings * settings,bool owner)69 void qpwgraph_config::setSettings ( QSettings *settings, bool owner )
70 {
71 	if (m_settings && m_owner)
72 		delete m_settings;
73 
74 	m_settings = settings;
75 	m_owner = owner;
76 }
77 
78 
settings(void) const79 QSettings *qpwgraph_config::settings (void) const
80 {
81 	return m_settings;
82 }
83 
84 
setMenubar(bool menubar)85 void qpwgraph_config::setMenubar ( bool menubar )
86 {
87 	m_menubar = menubar;
88 }
89 
isMenubar(void) const90 bool qpwgraph_config::isMenubar (void) const
91 {
92 	return m_menubar;
93 }
94 
95 
setToolbar(bool toolbar)96 void qpwgraph_config::setToolbar ( bool toolbar )
97 {
98 	m_toolbar = toolbar;
99 }
100 
isToolbar(void) const101 bool qpwgraph_config::isToolbar (void) const
102 {
103 	return m_toolbar;
104 }
105 
106 
setStatusbar(bool statusbar)107 void qpwgraph_config::setStatusbar ( bool statusbar )
108 {
109 	m_statusbar = statusbar;
110 }
111 
isStatusbar(void) const112 bool qpwgraph_config::isStatusbar (void) const
113 {
114 	return m_statusbar;
115 }
116 
117 
setTextBesideIcons(bool texticons)118 void qpwgraph_config::setTextBesideIcons ( bool texticons )
119 {
120 	m_texticons = texticons;
121 }
122 
isTextBesideIcons(void) const123 bool qpwgraph_config::isTextBesideIcons (void) const
124 {
125 	return m_texticons;
126 }
127 
128 
setZoomRange(bool zoomrange)129 void qpwgraph_config::setZoomRange ( bool zoomrange )
130 {
131 	m_zoomrange = zoomrange;
132 }
133 
isZoomRange(void) const134 bool qpwgraph_config::isZoomRange (void) const
135 {
136 	return m_zoomrange;
137 }
138 
139 
setSortType(int sorttype)140 void qpwgraph_config::setSortType ( int sorttype )
141 {
142 	m_sorttype = sorttype;
143 }
144 
sortType(void) const145 int qpwgraph_config::sortType (void) const
146 {
147 	return m_sorttype;
148 }
149 
150 
setSortOrder(int sortorder)151 void qpwgraph_config::setSortOrder ( int sortorder )
152 {
153 	m_sortorder = sortorder;
154 }
155 
sortOrder(void) const156 int qpwgraph_config::sortOrder (void) const
157 {
158 	return m_sortorder;
159 }
160 
161 
162 // Graph main-widget state methods.
restoreState(QMainWindow * widget)163 bool qpwgraph_config::restoreState ( QMainWindow *widget )
164 {
165 	if (m_settings == nullptr || widget == nullptr)
166 		return false;
167 
168 	m_settings->beginGroup(ViewGroup);
169 	m_menubar = m_settings->value(ViewMenubarKey, true).toBool();
170 	m_toolbar = m_settings->value(ViewToolbarKey, true).toBool();
171 	m_statusbar = m_settings->value(ViewStatusbarKey, true).toBool();
172 	m_texticons = m_settings->value(ViewTextBesideIconsKey, true).toBool();
173 	m_zoomrange = m_settings->value(ViewZoomRangeKey, false).toBool();
174 	m_sorttype  = m_settings->value(ViewSortTypeKey, 0).toInt();
175 	m_sortorder = m_settings->value(ViewSortOrderKey, 0).toInt();
176 	m_settings->endGroup();
177 
178 	m_settings->beginGroup(GeometryGroup);
179 	const QByteArray& geometry_state
180 		= m_settings->value('/' + widget->objectName()).toByteArray();
181 	m_settings->endGroup();
182 
183 	if (geometry_state.isEmpty() || geometry_state.isNull())
184 		return false;
185 
186 	widget->restoreGeometry(geometry_state);
187 
188 	m_settings->beginGroup(LayoutGroup);
189 	const QByteArray& layout_state
190 		= m_settings->value('/' + widget->objectName()).toByteArray();
191 	m_settings->endGroup();
192 
193 	if (layout_state.isEmpty() || layout_state.isNull())
194 		return false;
195 
196 	widget->restoreState(layout_state);
197 
198 	return true;
199 }
200 
201 
saveState(QMainWindow * widget) const202 bool qpwgraph_config::saveState ( QMainWindow *widget ) const
203 {
204 	if (m_settings == nullptr || widget == nullptr)
205 		return false;
206 
207 	m_settings->beginGroup(ViewGroup);
208 	m_settings->setValue(ViewMenubarKey, m_menubar);
209 	m_settings->setValue(ViewToolbarKey, m_toolbar);
210 	m_settings->setValue(ViewStatusbarKey, m_statusbar);
211 	m_settings->setValue(ViewTextBesideIconsKey, m_texticons);
212 	m_settings->setValue(ViewZoomRangeKey, m_zoomrange);
213 	m_settings->setValue(ViewSortTypeKey, m_sorttype);
214 	m_settings->setValue(ViewSortOrderKey, m_sortorder);
215 	m_settings->endGroup();
216 
217 	m_settings->beginGroup(GeometryGroup);
218 	const QByteArray& geometry_state = widget->saveGeometry();
219 	m_settings->setValue('/' + widget->objectName(), geometry_state);
220 	m_settings->endGroup();
221 
222 	m_settings->beginGroup(LayoutGroup);
223 	const QByteArray& layout_state = widget->saveState();
224 	m_settings->setValue('/' + widget->objectName(), layout_state);
225 	m_settings->endGroup();
226 
227 	return true;
228 }
229 
230 
231 // end of qpwgraph_config.cpp
232