Added Data Folder selection ability, and fixed Language selection bug on Mac

This commit is contained in:
Mark Watkins 2013-10-19 12:59:52 +10:00
parent 013c2bc878
commit e739b7dd6f
7 changed files with 172 additions and 196 deletions

View File

@ -17,6 +17,8 @@
#include <QDir>
#include <QComboBox>
#include <QPushButton>
#include <QSettings>
#include <QFileDialog>
#include "SleepLib/schema.h"
#include "mainwindow.h"
@ -134,6 +136,15 @@ void build_notes()
relnotes.exec();
}
void sDelay(int s)
{
// QThread::msleep() is exposed in Qt5
#ifdef Q_OS_WIN32
Sleep(s*1000);
#else
sleep(s);
#endif
}
int main(int argc, char *argv[])
{
@ -146,18 +157,141 @@ int main(int argc, char *argv[])
#endif
bool force_login_screen=false;
bool force_data_dir=false;
QApplication a(argc, argv);
QStringList args=QCoreApplication::arguments();
for (int i=1;i<args.size();i++) {
if (args[i]=="-l") force_login_screen=true;
if (args[i]=="-p") {
#ifdef Q_OS_WIN32
Sleep(1000);
else if (args[i]=="-d") force_data_dir=true;
else if (args[i]=="-p") {
sDelay(1);
}
}
QSettings settings(getDeveloperName(),getAppName());
////////////////////////////////////////////////////////////////////////////////////////////
// Language Selection
////////////////////////////////////////////////////////////////////////////////////////////
QDialog langsel(NULL,Qt::CustomizeWindowHint|Qt::WindowTitleHint);
langsel.setWindowTitle(QObject::tr("Language"));
QHBoxLayout lang_layout(&langsel);
QComboBox lang_combo(&langsel);
QPushButton lang_okbtn("->",&langsel);
lang_layout.addWidget(&lang_combo,1);
lang_layout.addWidget(&lang_okbtn);
#ifdef Q_WS_MAC
QString transdir=QDir::cleanPath(QCoreApplication::applicationDirPath()+"/../Resources/Translations/");
#else
sleep(1);
const QString transdir=QCoreApplication::applicationDirPath()+"/Translations/";
#endif
QDir dir(transdir);
qDebug() << "Scanning \"" << transdir << "\" for translations";
dir.setFilter(QDir::Files);
dir.setNameFilters(QStringList("*.qm"));
qDebug() << "Available Translations";
QFileInfoList list=dir.entryInfoList();
QString language=settings.value("Settings/Language").toString();
language="";
QString langfile,langname;
// Fake english for now..
lang_combo.addItem("English","English.en_US.qm");
// Scan translation directory
for (int i=0;i<list.size();++i) {
QFileInfo fi=list.at(i);
langname=fi.fileName().section('.',0,0);
lang_combo.addItem(langname,fi.fileName());
qDebug() << "Found Translation" << QDir::toNativeSeparators(fi.fileName());
}
for (int i=0;i<lang_combo.count();i++) {
langname=lang_combo.itemText(i);
if (langname==language) {
langfile=lang_combo.itemData(i).toString();
break;
}
}
if (language.isEmpty()) {
langsel.connect(&lang_okbtn,SIGNAL(clicked()),&langsel, SLOT(close()));
langsel.exec();
langsel.disconnect(&lang_okbtn,SIGNAL(clicked()),&langsel, SLOT(close()));
langname=lang_combo.currentText();
langfile=lang_combo.itemData(lang_combo.currentIndex()).toString();
settings.setValue("Settings/Language",langname);
}
qDebug() << "Loading " << langname << " Translation" << langfile << "from" << transdir;
QTranslator translator;
translator.load(langfile,transdir);
a.installTranslator(&translator);
a.setApplicationName(STR_TR_SleepyHead);
////////////////////////////////////////////////////////////////////////////////////////////
// Datafolder location Selection
////////////////////////////////////////////////////////////////////////////////////////////
bool change_data_dir=force_data_dir;
bool havefolder=false;
if (!settings.contains("Settings/AppRoot")) {
change_data_dir=true;
} else {
QDir dir(GetAppRoot());
if (!dir.exists())
change_data_dir=true;
else havefolder=true;
}
if (!havefolder && !force_data_dir) {
if (QMessageBox::question(NULL,QObject::tr("Question"),QObject::tr("No SleepyHead data folder was found.\n\nWould you like SleepyHead to use the default location for storing it's data?\n\n")+GetAppRoot(),QMessageBox::Yes,QMessageBox::No)==QMessageBox::Yes) {
settings.setValue("Settings/AppRoot",GetAppRoot());
change_data_dir=false;
}
}
retry_directory:
if (change_data_dir) {
QString datadir=QFileDialog::getExistingDirectory(NULL,QObject::tr("Choose or create new folder for Sleepyhead data"),GetAppRoot(),QFileDialog::ShowDirsOnly);
if (datadir.isEmpty()) {
if (!havefolder) {
QMessageBox::information(NULL,QObject::tr("Exiting"),QObject::tr("As you did not select a data folder, SleepyHead will exit.\n\nNext time you run, you will be asked again."));
return 0;
} else {
QMessageBox::information(NULL,QObject::tr("No Directory"),QObject::tr("You did not select a directory.\n\nSleepyHead will now start with your old one.\n\n")+GetAppRoot(),QMessageBox::Ok);
}
} else {
QDir dir(datadir);
QFile file(datadir+"/Preferences.xml");
if (!file.exists()) {
if (dir.count() > 2) {
// Not a new directory.. nag the user.
if (QMessageBox::question(NULL,QObject::tr("Warning"),QObject::tr("The folder you chose is not empty, nor does it already contain valid SleepyHead data.\n\nAre you sure you want to use this folder?\n\n")+datadir,QMessageBox::Yes,QMessageBox::No)==QMessageBox::No) {
goto retry_directory;
}
}
}
settings.setValue("Settings/AppRoot",datadir);
qDebug() << "Changing data folder to" << datadir;
}
}
@ -192,73 +326,6 @@ int main(int argc, char *argv[])
}
////////////////////////////////////////////////////////////////////////////////////////////
// Language Selection
////////////////////////////////////////////////////////////////////////////////////////////
QDialog langsel(NULL,Qt::CustomizeWindowHint|Qt::WindowTitleHint);
langsel.setWindowTitle(QObject::tr("Language"));
QHBoxLayout lang_layout(&langsel);
QComboBox lang_combo(&langsel);
QPushButton lang_okbtn("->",&langsel);
lang_layout.addWidget(&lang_combo,1);
lang_layout.addWidget(&lang_okbtn);
#ifdef Q_WS_MAC
QString transdir=QCoreApplication::applicationDirPath()+"/Contents/Resources/Translations/";
#else
QString transdir=QCoreApplication::applicationDirPath()+"/Translations/";
#endif
QDir dir(transdir);
qDebug() << "Scanning \"" << transdir << "\" for translations";
dir.setFilter(QDir::Files);
dir.setNameFilters(QStringList("*.qm"));
qDebug() << "Available Translations";
QFileInfoList list=dir.entryInfoList();
QString language=PREF[STR_PREF_Language].toString();
QString langfile,langname;
// Fake english for now..
lang_combo.addItem("English","English.en_US.qm");
// Scan translation directory
for (int i=0;i<list.size();++i) {
QFileInfo fi=list.at(i);
langname=fi.fileName().section('.',0,0);
lang_combo.addItem(langname,fi.fileName());
qDebug() << "Found Translation" << QDir::toNativeSeparators(fi.fileName());
}
for (int i=0;i<lang_combo.count();i++) {
langname=lang_combo.itemText(i);
if (langname==language) {
langfile=lang_combo.itemData(i).toString();
break;
}
}
if (language.isEmpty()) {
langsel.connect(&lang_okbtn,SIGNAL(clicked()),&langsel, SLOT(close()));
langsel.exec();
langsel.disconnect(&lang_okbtn,SIGNAL(clicked()),&langsel, SLOT(close()));
langname=lang_combo.currentText();
langfile=lang_combo.itemData(lang_combo.currentIndex()).toString();
PREF[STR_PREF_Language]=langname;
}
qDebug() << "Loading " << langname << " Translation" << langfile;
QTranslator translator;
translator.load(langfile,QCoreApplication::applicationDirPath()+"/Translations");
a.installTranslator(&translator);
a.setApplicationName(STR_TR_SleepyHead);

View File

@ -1054,7 +1054,7 @@ void MainWindow::on_action_Rebuild_Oximetry_Index_triggered()
getOverview()->ReloadGraphs();
}
void MainWindow::RestartApplication(bool force_login)
void MainWindow::RestartApplication(bool force_login,bool change_datafolder)
{
QString apppath;
#ifdef Q_OS_MAC
@ -1073,6 +1073,7 @@ void MainWindow::RestartApplication(bool force_login)
if (force_login) args << "-l";
if (change_datafolder) args << "-d";
if (QProcess::startDetached("/usr/bin/open",args)) {
QApplication::instance()->exit();
@ -1090,6 +1091,7 @@ void MainWindow::RestartApplication(bool force_login)
QStringList args;
args << "-p";
if (force_login) args << "-l";
if (change_datafolder) args << "-d";
if (QProcess::startDetached(apppath,args)) {
::exit(0);
//QApplication::instance()->exit();
@ -1455,9 +1457,18 @@ void MainWindow::on_actionHelp_Support_Sleepyhead_Development_triggered()
void MainWindow::on_actionChange_Language_triggered()
{
PREF.Erase(STR_PREF_Language);
QSettings *settings=new QSettings(getDeveloperName(),getAppName());
settings->remove("Settings/Language");
delete settings;
PROFILE.Save();
PREF.Save();
RestartApplication(true);
}
void MainWindow::on_actionChange_Data_Folder_triggered()
{
PROFILE.Save();
PREF.Save();
RestartApplication(false,true);
}

View File

@ -121,7 +121,7 @@ public:
If force_login is set, it will return to the login menu even if it's set to skip
*/
void RestartApplication(bool force_login=false);
void RestartApplication(bool force_login=false,bool change_datafolder=false);
//! \brief Self explainitory, selects the Oximetry Tab
void selectOximetryTab();
@ -298,6 +298,8 @@ private slots:
void on_actionChange_Language_triggered();
void on_actionChange_Data_Folder_triggered();
private:
void FreeSessions();

View File

@ -448,7 +448,7 @@ QToolBox::tab:selected {
<property name="geometry">
<rect>
<x>0</x>
<y>-16</y>
<y>0</y>
<width>165</width>
<height>514</height>
</rect>
@ -2075,6 +2075,7 @@ border-radius: 10px;
<addaction name="actionExp_ort"/>
<addaction name="separator"/>
<addaction name="actionChange_User"/>
<addaction name="actionChange_Data_Folder"/>
<addaction name="separator"/>
<addaction name="actionE_xit"/>
</widget>
@ -2356,6 +2357,11 @@ border-radius: 10px;
<string>Change &amp;Language</string>
</property>
</action>
<action name="actionChange_Data_Folder">
<property name="text">
<string>Change &amp;Data Folder</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>

View File

@ -76,10 +76,6 @@ NewProfile::NewProfile(QWidget *parent) :
ui->AppTitle->setText("SleepyHead v"+VersionString);
ui->releaseStatus->setText(ReleaseStatus);
ui->dataFolderPath->setReadOnly(true);
ui->dataFolderPath->setText(GetAppRoot());
ui->textBrowser->setSource(QUrl("qrc:/docs/intro.html"));
}
@ -99,12 +95,6 @@ void NewProfile::on_nextButton_clicked()
case 0:
if (!ui->agreeCheckbox->isChecked())
return;
settings.setValue("Settings/AppRoot", ui->dataFolderPath->text());
p_pref->setFilename(ui->dataFolderPath->text()+"/"+p_pref->name()+xmlext);
p_pref->setPath(ui->dataFolderPath->text());
p_layout->setFilename(ui->dataFolderPath->text()+"/"+p_layout->name()+xmlext);
p_layout->setPath(ui->dataFolderPath->text());
// Reload Preferences object
break;
case 1:
@ -338,7 +328,6 @@ void NewProfile::on_passwordEdit2_editingFinished()
m_passwordHashed=false;
}
void NewProfile::on_heightCombo_currentIndexChanged(int index)
{
if (index==0) {
@ -362,18 +351,3 @@ void NewProfile::on_heightCombo_currentIndexChanged(int index)
ui->heightEdit2->setValue(inches);
}
}
void NewProfile::on_dataFolderButton_clicked()
{
QFileDialog fd(this);
QString filename;
fd.setFileMode(QFileDialog::Directory);
fd.setOption(QFileDialog::ShowDirsOnly,true);
fd.setAcceptMode(QFileDialog::AcceptSave);
if (fd.exec()==QFileDialog::Accepted) {
filename=fd.selectedFiles()[0];
ui->dataFolderPath->setText(filename);
//ui->dataFolderPath->setEnabled(true);
}
}

View File

@ -48,8 +48,6 @@ private slots:
void on_heightCombo_currentIndexChanged(int index);
void on_dataFolderButton_clicked();
private:
Ui::NewProfile *ui;
bool m_editMode;

View File

@ -18,6 +18,9 @@
<normaloff>:/icons/bob-v3.0.png</normaloff>:/icons/bob-v3.0.png</iconset>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="margin">
<number>8</number>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
@ -32,37 +35,6 @@
</property>
<widget class="QWidget" name="welcomePage">
<layout class="QVBoxLayout" name="verticalLayout_8">
<item>
<layout class="QGridLayout" name="gridLayout_3">
<property name="spacing">
<number>4</number>
</property>
<item row="0" column="2">
<widget class="QToolButton" name="dataFolderButton">
<property name="toolTip">
<string>Click here to choose where to store SleepyHead data.</string>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Data Folder</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="dataFolderPath">
<property name="toolTip">
<string>Shows the directory where SleepyHead data will be stored. </string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QTextBrowser" name="textBrowser">
<property name="autoFormatting">
@ -109,16 +81,7 @@
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>4</number>
</property>
<property name="topMargin">
<number>4</number>
</property>
<property name="rightMargin">
<number>4</number>
</property>
<property name="bottomMargin">
<property name="margin">
<number>4</number>
</property>
<item>
@ -228,24 +191,15 @@
<string>Locale Settings</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<property name="horizontalSpacing">
<number>0</number>
</property>
<property name="verticalSpacing">
<number>4</number>
</property>
<property name="margin">
<number>0</number>
</property>
<item row="1" column="0">
<widget class="QLabel" name="label_25">
<property name="sizePolicy">
@ -327,16 +281,7 @@
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>4</number>
</property>
<property name="topMargin">
<number>4</number>
</property>
<property name="rightMargin">
<number>4</number>
</property>
<property name="bottomMargin">
<property name="margin">
<number>4</number>
</property>
<item>
@ -445,16 +390,7 @@
</item>
<item row="3" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_6">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<property name="margin">
<number>0</number>
</property>
<item>
@ -589,16 +525,7 @@
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>4</number>
</property>
<property name="topMargin">
<number>4</number>
</property>
<property name="rightMargin">
<number>4</number>
</property>
<property name="bottomMargin">
<property name="margin">
<number>4</number>
</property>
<item>
@ -728,16 +655,7 @@
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>4</number>
</property>
<property name="topMargin">
<number>4</number>
</property>
<property name="rightMargin">
<number>4</number>
</property>
<property name="bottomMargin">
<property name="margin">
<number>4</number>
</property>
<item>