2013-05-08 01:33:42 +00:00
|
|
|
#include "usersettingsdialog.hpp"
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QDesktopWidget>
|
|
|
|
#include <QWidget>
|
|
|
|
#include <QTabWidget>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QTextCodec>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QDockWidget>
|
|
|
|
|
|
|
|
#include "blankpage.hpp"
|
|
|
|
#include "editorpage.hpp"
|
2013-05-10 15:00:58 +00:00
|
|
|
#include "../../model/settings/support.hpp"
|
2013-05-08 01:33:42 +00:00
|
|
|
|
|
|
|
#include "settingwidget.hpp"
|
|
|
|
#include <QDebug>
|
|
|
|
|
2013-05-11 10:55:46 +00:00
|
|
|
CSVSettings::UserSettingsDialog::UserSettingsDialog(QMainWindow *parent) :
|
2013-05-08 01:33:42 +00:00
|
|
|
QMainWindow (parent), mUserSettings (mCfgMgr), mStackedWidget (0)
|
|
|
|
{
|
|
|
|
setWindowTitle(QString::fromUtf8 ("User Settings"));
|
|
|
|
buildPages();
|
|
|
|
setWidgetStates (loadSettings());
|
|
|
|
positionWindow ();
|
|
|
|
|
|
|
|
connect (mListWidget,
|
|
|
|
SIGNAL (currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
|
|
|
|
this,
|
|
|
|
SLOT (slotChangePage (QListWidgetItem*, QListWidgetItem*)));
|
|
|
|
}
|
|
|
|
|
2013-05-11 10:55:46 +00:00
|
|
|
CSVSettings::UserSettingsDialog::~UserSettingsDialog()
|
2013-05-08 01:33:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-05-11 10:55:46 +00:00
|
|
|
void CSVSettings::UserSettingsDialog::closeEvent (QCloseEvent *event)
|
2013-05-08 01:33:42 +00:00
|
|
|
{
|
|
|
|
writeSettings();
|
|
|
|
}
|
|
|
|
|
2013-05-11 10:55:46 +00:00
|
|
|
void CSVSettings::UserSettingsDialog::setWidgetStates (CSMSettings::SectionMap settingsMap)
|
2013-05-08 01:33:42 +00:00
|
|
|
{
|
|
|
|
//iterate the tabWidget's pages (sections)
|
|
|
|
for (int i = 0; i < mStackedWidget->count(); i++)
|
|
|
|
{
|
|
|
|
//get the settings defined for the entire section
|
2013-05-11 10:55:46 +00:00
|
|
|
CSMSettings::SettingMap *settings = settingsMap [mStackedWidget->widget(i)->objectName()];
|
2013-05-08 01:33:42 +00:00
|
|
|
|
|
|
|
//if found, initialize the page's widgets
|
|
|
|
if (settings)
|
|
|
|
{
|
|
|
|
AbstractPage *page = getAbstractPage (i);
|
|
|
|
page->initializeWidgets(*settings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-11 10:55:46 +00:00
|
|
|
void CSVSettings::UserSettingsDialog::buildPages()
|
2013-05-08 01:33:42 +00:00
|
|
|
{
|
|
|
|
//craete central widget with it's layout and immediate children
|
|
|
|
QWidget *centralWidget = new QWidget (this);
|
|
|
|
|
|
|
|
mListWidget = new QListWidget (centralWidget);
|
|
|
|
mStackedWidget = new QStackedWidget (centralWidget);
|
|
|
|
|
|
|
|
QLayout* dialogLayout = new QHBoxLayout();
|
|
|
|
|
|
|
|
dialogLayout->addWidget (mListWidget);
|
|
|
|
dialogLayout->addWidget (mStackedWidget);
|
|
|
|
|
|
|
|
centralWidget->setLayout (dialogLayout);
|
|
|
|
|
|
|
|
setCentralWidget (centralWidget);
|
|
|
|
setDockOptions (QMainWindow::AllowNestedDocks);
|
|
|
|
//uncomment to test with sample editor page.
|
2013-05-11 10:55:46 +00:00
|
|
|
createSamplePage();
|
2013-05-08 01:33:42 +00:00
|
|
|
createPage<BlankPage>("Page1");
|
|
|
|
createPage<BlankPage>("Page2");
|
|
|
|
createPage<BlankPage>("Page3");
|
|
|
|
}
|
|
|
|
|
2013-05-11 10:55:46 +00:00
|
|
|
void CSVSettings::UserSettingsDialog::createSamplePage()
|
2013-05-08 01:33:42 +00:00
|
|
|
{
|
|
|
|
//add pages to stackedwidget and items to listwidget
|
2013-05-11 10:55:46 +00:00
|
|
|
CSVSettings::AbstractPage *page
|
|
|
|
= new CSVSettings::EditorPage(this);
|
2013-05-08 01:33:42 +00:00
|
|
|
|
|
|
|
mStackedWidget->addWidget (page);
|
|
|
|
|
|
|
|
new QListWidgetItem (page->objectName(), mListWidget);
|
|
|
|
|
|
|
|
connect ( page, SIGNAL ( signalUpdateEditorSetting (const QString &, const QString &)),
|
|
|
|
this, SIGNAL ( signalUpdateEditorSetting (const QString &, const QString &)));
|
|
|
|
}
|
|
|
|
|
2013-05-11 10:55:46 +00:00
|
|
|
void CSVSettings::UserSettingsDialog::positionWindow ()
|
2013-05-08 01:33:42 +00:00
|
|
|
{
|
|
|
|
QRect scr = QApplication::desktop()->screenGeometry();
|
|
|
|
|
|
|
|
move(scr.center().x() - (width() / 2), scr.center().y() - (height() / 2));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-11 10:55:46 +00:00
|
|
|
CSMSettings::SectionMap CSVSettings::UserSettingsDialog::loadSettings ()
|
2013-05-08 01:33:42 +00:00
|
|
|
{
|
|
|
|
QString userPath = QString::fromStdString(mCfgMgr.getUserPath().string());
|
|
|
|
|
|
|
|
mPaths.append(QString("opencs.cfg"));
|
|
|
|
mPaths.append(userPath + QString("opencs.cfg"));
|
|
|
|
|
2013-05-11 10:55:46 +00:00
|
|
|
CSMSettings::SectionMap settingsMap;
|
2013-05-08 01:33:42 +00:00
|
|
|
|
|
|
|
foreach (const QString &path, mPaths)
|
|
|
|
{
|
|
|
|
qDebug() << "Loading config file:" << qPrintable(path);
|
|
|
|
QFile file(path);
|
|
|
|
|
|
|
|
if (file.exists())
|
|
|
|
{
|
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
|
|
|
|
{
|
|
|
|
QMessageBox msgBox;
|
|
|
|
msgBox.setWindowTitle(tr("Error opening OpenCS configuration file"));
|
|
|
|
msgBox.setIcon(QMessageBox::Critical);
|
|
|
|
msgBox.setStandardButtons(QMessageBox::Ok);
|
|
|
|
msgBox.setText(QObject::tr("<br><b>Could not open %0 for reading</b><br><br> \
|
|
|
|
Please make sure you have the right permissions \
|
|
|
|
and try again.<br>").arg(file.fileName()));
|
|
|
|
msgBox.exec();
|
|
|
|
return settingsMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextStream stream(&file);
|
|
|
|
stream.setCodec(QTextCodec::codecForName("UTF-8"));
|
|
|
|
|
|
|
|
mUserSettings.getSettings(stream, settingsMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return settingsMap;
|
|
|
|
}
|
|
|
|
|
2013-05-11 10:55:46 +00:00
|
|
|
void CSVSettings::UserSettingsDialog::writeSettings()
|
2013-05-08 01:33:42 +00:00
|
|
|
{
|
2013-05-11 10:55:46 +00:00
|
|
|
QMap<QString, CSMSettings::SettingList *> settings;
|
2013-05-08 01:33:42 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < mStackedWidget->count(); ++i)
|
|
|
|
{
|
|
|
|
AbstractPage *page = getAbstractPage (i);
|
|
|
|
settings [page->objectName()] = page->getSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
mUserSettings.writeFile(mUserSettings.openFile(mPaths.back()), settings);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-11 10:55:46 +00:00
|
|
|
CSVSettings::AbstractPage *CSVSettings::UserSettingsDialog::getAbstractPage (int index)
|
2013-05-08 01:33:42 +00:00
|
|
|
{
|
|
|
|
return dynamic_cast<AbstractPage *>(mStackedWidget->widget(index));
|
|
|
|
}
|
|
|
|
|
2013-05-11 10:55:46 +00:00
|
|
|
void CSVSettings::UserSettingsDialog::slotChangePage(QListWidgetItem *current, QListWidgetItem *previous)
|
2013-05-08 01:33:42 +00:00
|
|
|
{
|
|
|
|
if (!current)
|
|
|
|
current = previous;
|
|
|
|
|
|
|
|
if (!(current == previous))
|
|
|
|
mStackedWidget->setCurrentIndex (mListWidget->row(current));
|
|
|
|
}
|