| 
									
										
										
										
											2015-09-17 17:16:40 -03:00
										 |  |  | #include "preferencesdialog.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "abstractpreferenceswidget.h"
 | 
					
						
							|  |  |  | #include "preferences_language.h"
 | 
					
						
							| 
									
										
										
										
											2015-09-17 19:22:19 -03:00
										 |  |  | #include "preferences_georeference.h"
 | 
					
						
							| 
									
										
										
										
											2015-09-25 16:15:37 -03:00
										 |  |  | #include "preferences_defaults.h"
 | 
					
						
							| 
									
										
										
										
											2015-09-25 17:07:04 -03:00
										 |  |  | #include "preferences_units.h"
 | 
					
						
							| 
									
										
										
										
											2015-10-01 18:59:53 -03:00
										 |  |  | #include "preferences_graph.h"
 | 
					
						
							| 
									
										
										
										
											2015-10-01 19:48:57 -03:00
										 |  |  | #include "preferences_network.h"
 | 
					
						
							| 
									
										
										
										
											2015-09-17 17:16:40 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-04 22:02:03 -07:00
										 |  |  | #include "core/qthelper.h"
 | 
					
						
							| 
									
										
										
										
											2015-11-01 12:14:40 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-17 17:16:40 -03:00
										 |  |  | #include <QVBoxLayout>
 | 
					
						
							|  |  |  | #include <QHBoxLayout>
 | 
					
						
							|  |  |  | #include <QListWidget>
 | 
					
						
							|  |  |  | #include <QStackedWidget>
 | 
					
						
							|  |  |  | #include <QDialogButtonBox>
 | 
					
						
							| 
									
										
										
										
											2015-09-17 17:53:39 -03:00
										 |  |  | #include <QAbstractButton>
 | 
					
						
							|  |  |  | #include <QDebug>
 | 
					
						
							| 
									
										
										
										
											2015-09-17 17:16:40 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-31 21:02:16 -02:00
										 |  |  | PreferencesDialog* PreferencesDialog::instance() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-10-31 21:54:59 -02:00
										 |  |  | 	static PreferencesDialog *self = new PreferencesDialog(); | 
					
						
							| 
									
										
										
										
											2015-10-31 21:02:16 -02:00
										 |  |  | 	return self; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void PreferencesDialog::emitSettingsChanged() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-11-10 13:45:13 -08:00
										 |  |  | 	emit settingsChanged(); | 
					
						
							| 
									
										
										
										
											2015-10-31 21:02:16 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PreferencesDialog::PreferencesDialog() | 
					
						
							| 
									
										
										
										
											2015-09-17 17:16:40 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-08-11 11:52:49 -03:00
										 |  |  | 	//FIXME: This looks wrong.
 | 
					
						
							|  |  |  | 	//QSettings s;
 | 
					
						
							|  |  |  | 	//s.beginGroup("GeneralSettings");
 | 
					
						
							|  |  |  | 	//s.setValue("default_directory", system_default_directory());
 | 
					
						
							|  |  |  | 	//s.endGroup();
 | 
					
						
							| 
									
										
										
										
											2015-11-03 19:03:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-17 17:16:40 -03:00
										 |  |  | 	pagesList = new QListWidget(); | 
					
						
							|  |  |  | 	pagesStack = new QStackedWidget(); | 
					
						
							| 
									
										
										
										
											2015-09-17 17:53:39 -03:00
										 |  |  | 	buttonBox = new QDialogButtonBox( | 
					
						
							|  |  |  | 		QDialogButtonBox::Save | | 
					
						
							| 
									
										
										
										
											2015-10-31 21:26:52 -02:00
										 |  |  | 		QDialogButtonBox::Apply | | 
					
						
							| 
									
										
										
										
											2015-09-17 17:53:39 -03:00
										 |  |  | 		QDialogButtonBox::Cancel); | 
					
						
							| 
									
										
										
										
											2015-09-17 17:16:40 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	pagesList->setMinimumWidth(120); | 
					
						
							|  |  |  | 	pagesList->setMaximumWidth(120); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	QHBoxLayout *h = new QHBoxLayout(); | 
					
						
							|  |  |  | 	h->addWidget(pagesList); | 
					
						
							|  |  |  | 	h->addWidget(pagesStack); | 
					
						
							|  |  |  | 	QVBoxLayout *v = new QVBoxLayout(); | 
					
						
							|  |  |  | 	v->addLayout(h); | 
					
						
							|  |  |  | 	v->addWidget(buttonBox); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	setLayout(v); | 
					
						
							| 
									
										
										
										
											2015-09-17 17:53:39 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-17 17:16:40 -03:00
										 |  |  | 	addPreferencePage(new PreferencesLanguage()); | 
					
						
							| 
									
										
										
										
											2015-09-17 19:22:19 -03:00
										 |  |  | 	addPreferencePage(new PreferencesGeoreference()); | 
					
						
							| 
									
										
										
										
											2015-09-25 16:15:37 -03:00
										 |  |  | 	addPreferencePage(new PreferencesDefaults()); | 
					
						
							| 
									
										
										
										
											2015-09-25 17:07:04 -03:00
										 |  |  | 	addPreferencePage(new PreferencesUnits()); | 
					
						
							| 
									
										
										
										
											2015-10-01 18:59:53 -03:00
										 |  |  | 	addPreferencePage(new PreferencesGraph()); | 
					
						
							| 
									
										
										
										
											2015-10-01 19:48:57 -03:00
										 |  |  | 	addPreferencePage(new PreferencesNetwork()); | 
					
						
							| 
									
										
										
										
											2015-09-17 17:16:40 -03:00
										 |  |  | 	refreshPages(); | 
					
						
							| 
									
										
										
										
											2015-09-17 17:53:39 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-17 17:16:40 -03:00
										 |  |  | 	connect(pagesList, &QListWidget::currentRowChanged, | 
					
						
							|  |  |  | 		pagesStack, &QStackedWidget::setCurrentIndex); | 
					
						
							| 
									
										
										
										
											2015-09-17 17:53:39 -03:00
										 |  |  | 	connect(buttonBox, &QDialogButtonBox::clicked, | 
					
						
							| 
									
										
										
										
											2015-10-31 21:02:16 -02:00
										 |  |  | 		this, &PreferencesDialog::buttonClicked); | 
					
						
							| 
									
										
										
										
											2015-09-17 17:16:40 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-31 21:02:16 -02:00
										 |  |  | PreferencesDialog::~PreferencesDialog() | 
					
						
							| 
									
										
										
										
											2015-09-17 17:16:40 -03:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-31 21:02:16 -02:00
										 |  |  | void PreferencesDialog::buttonClicked(QAbstractButton* btn) | 
					
						
							| 
									
										
										
										
											2015-09-17 17:53:39 -03:00
										 |  |  | { | 
					
						
							|  |  |  | 	QDialogButtonBox::ButtonRole role = buttonBox->buttonRole(btn); | 
					
						
							|  |  |  | 	switch(role) { | 
					
						
							| 
									
										
										
										
											2015-11-02 12:50:17 -08:00
										 |  |  | 	case QDialogButtonBox::ApplyRole : applyRequested(false); return; | 
					
						
							|  |  |  | 	case QDialogButtonBox::AcceptRole : applyRequested(true); return; | 
					
						
							|  |  |  | 	case QDialogButtonBox::RejectRole : cancelRequested(); return; | 
					
						
							|  |  |  | 	case QDialogButtonBox::ResetRole : defaultsRequested(); return; | 
					
						
							| 
									
										
										
										
											2015-11-07 23:17:24 +02:00
										 |  |  | 	default: return; | 
					
						
							| 
									
										
										
										
											2015-09-17 17:53:39 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-17 17:16:40 -03:00
										 |  |  | bool abstractpreferenceswidget_lessthan(AbstractPreferencesWidget *p1, AbstractPreferencesWidget *p2) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return p1->positionHeight() <= p2->positionHeight(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-31 21:02:16 -02:00
										 |  |  | void PreferencesDialog::addPreferencePage(AbstractPreferencesWidget *page) | 
					
						
							| 
									
										
										
										
											2015-09-17 17:16:40 -03:00
										 |  |  | { | 
					
						
							|  |  |  | 	pages.push_back(page); | 
					
						
							|  |  |  | 	qSort(pages.begin(), pages.end(), abstractpreferenceswidget_lessthan); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-31 21:02:16 -02:00
										 |  |  | void PreferencesDialog::refreshPages() | 
					
						
							| 
									
										
										
										
											2015-09-17 17:16:40 -03:00
										 |  |  | { | 
					
						
							|  |  |  | 	// Remove things
 | 
					
						
							|  |  |  | 	pagesList->clear(); | 
					
						
							|  |  |  | 	while(pagesStack->count()) { | 
					
						
							|  |  |  | 		QWidget *curr = pagesStack->widget(0); | 
					
						
							|  |  |  | 		pagesStack->removeWidget(curr); | 
					
						
							|  |  |  | 		curr->setParent(0); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Readd things.
 | 
					
						
							|  |  |  | 	Q_FOREACH(AbstractPreferencesWidget *page, pages) { | 
					
						
							|  |  |  | 		QListWidgetItem *item = new QListWidgetItem(page->icon(), page->name()); | 
					
						
							|  |  |  | 		pagesList->addItem(item); | 
					
						
							|  |  |  | 		pagesStack->addWidget(page); | 
					
						
							| 
									
										
										
										
											2015-09-17 18:10:32 -03:00
										 |  |  | 		page->refreshSettings(); | 
					
						
							| 
									
										
										
										
											2015-09-17 17:16:40 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-02 12:50:17 -08:00
										 |  |  | void PreferencesDialog::applyRequested(bool closeIt) | 
					
						
							| 
									
										
										
										
											2015-09-17 17:16:40 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-09-17 17:57:41 -03:00
										 |  |  | 	Q_FOREACH(AbstractPreferencesWidget *page, pages) { | 
					
						
							| 
									
										
										
										
											2015-11-10 13:45:13 -08:00
										 |  |  | 		connect(page, &AbstractPreferencesWidget::settingsChanged, this, &PreferencesDialog::settingsChanged, Qt::UniqueConnection); | 
					
						
							| 
									
										
										
										
											2015-09-17 17:57:41 -03:00
										 |  |  | 		page->syncSettings(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-10-02 11:09:13 -03:00
										 |  |  | 	emit settingsChanged(); | 
					
						
							| 
									
										
										
										
											2015-11-02 12:50:17 -08:00
										 |  |  | 	if (closeIt) | 
					
						
							|  |  |  | 		accept(); | 
					
						
							| 
									
										
										
										
											2015-09-17 17:16:40 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-31 21:02:16 -02:00
										 |  |  | void PreferencesDialog::cancelRequested() | 
					
						
							| 
									
										
										
										
											2015-09-17 17:16:40 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-09-17 17:57:41 -03:00
										 |  |  | 	Q_FOREACH(AbstractPreferencesWidget *page, pages) { | 
					
						
							|  |  |  | 		page->refreshSettings(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-10-02 11:09:13 -03:00
										 |  |  | 	reject(); | 
					
						
							| 
									
										
										
										
											2015-09-17 17:16:40 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-31 21:02:16 -02:00
										 |  |  | void PreferencesDialog::defaultsRequested() | 
					
						
							| 
									
										
										
										
											2015-09-17 17:16:40 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-09-17 17:57:41 -03:00
										 |  |  | 	prefs = default_prefs; | 
					
						
							|  |  |  | 	Q_FOREACH(AbstractPreferencesWidget *page, pages) { | 
					
						
							|  |  |  | 		page->refreshSettings(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-10-02 11:09:13 -03:00
										 |  |  | 	emit settingsChanged(); | 
					
						
							|  |  |  | 	accept(); | 
					
						
							| 
									
										
										
										
											2015-09-17 17:16:40 -03:00
										 |  |  | } |