Skip to content

Commit

Permalink
Fix #285 - "Save as .." keeps the default settings location unchanged
Browse files Browse the repository at this point in the history
Save current settings to the selected file (same for "Open settings ..")
but do not alter the storage for further automatic persistent storage.

Signed-off-by: Martin <Ho-Ro@users.noreply.github.com>
  • Loading branch information
Ho-Ro committed Apr 6, 2022
1 parent 94eae3e commit 8a7b4d3
Show file tree
Hide file tree
Showing 14 changed files with 348 additions and 216 deletions.
2 changes: 1 addition & 1 deletion openhantek/src/OH_BUILD.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// Do not edit, will be re-created at each commit!
#define OH_BUILD "20220406 - commit 985"
#define OH_BUILD "20220406 - commit 986"
61 changes: 47 additions & 14 deletions openhantek/src/dsosettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

#include <QApplication>
#include <QColor>
#include <QFileInfo>
#include <QSettings>

#include "dsosettings.h"
#include "dsowidget.h"

/// \brief Set the number of channels.
/// \param channels The new channel count, that will be applied to lists.
DsoSettings::DsoSettings( const ScopeDevice *scopeDevice, bool resetSettings )
DsoSettings::DsoSettings( const ScopeDevice *scopeDevice, int verboseLevel, bool resetSettings )
: deviceName( scopeDevice->getModel()->name ), deviceID( scopeDevice->getSerialNumber() ),
deviceFW( scopeDevice->getFwVersion() ), deviceSpecification( scopeDevice->getModel()->spec() ),
deviceFW( scopeDevice->getFwVersion() ), deviceSpecification( scopeDevice->getModel()->spec() ), verboseLevel( verboseLevel ),
resetSettings( resetSettings ) {
scope.verboseLevel = verboseLevel;
if ( verboseLevel > 1 )
qDebug() << " DsoSettings::DsoSettings()" << deviceName << deviceID << resetSettings;
// Add new channels to the list
int voltage_hue[] = { 60, 210, 0, 120 }; // yellow, lightblue, red, green
int spectrum_hue[] = { 30, 240, 330, 90 }; // orange, blue, purple, green
Expand Down Expand Up @@ -63,22 +67,45 @@ DsoSettings::DsoSettings( const ScopeDevice *scopeDevice, bool resetSettings )
}


bool DsoSettings::setFilename( const QString &filename ) {
// store the current settings to an explicitly named file
bool DsoSettings::saveToFile( const QString &filename ) {
if ( verboseLevel > 1 )
qDebug() << " DsoSettings::saveFilename()" << filename;
std::unique_ptr< QSettings > local = std::unique_ptr< QSettings >( new QSettings( filename, QSettings::IniFormat ) );
if ( local->status() != QSettings::NoError ) {
qWarning() << "Could not change the settings file to " << filename;
qWarning() << "Could not save to config file " << filename;
return false;
}
storeSettings.swap( local );
storeSettings.swap( local ); // switch to requested filename
save(); // store the settings
storeSettings.swap( local ); // and switch back to default persistent storage location (file, registry, ...)
return true;
}


// load settings from a config file
bool DsoSettings::loadFromFile( const QString &filename ) {
if ( verboseLevel > 1 )
qDebug() << " DsoSettings::loadFilename()" << filename;
if ( QFileInfo( filename ).isReadable() ) {
std::unique_ptr< QSettings > local = std::unique_ptr< QSettings >( new QSettings( filename, QSettings::IniFormat ) );
if ( local->status() == QSettings::NoError ) {
storeSettings.swap( local );
load();
storeSettings.swap( local );
return true;
}
}
qWarning() << "Could not load from config file " << filename;
return false;
}


// load the persistent scope settings
// called by "DsoSettings::DsoSettings" and explicitely by "ui->actionOpen"
// called by "DsoSettings::DsoSettings()" and "loadFromFile()"
void DsoSettings::load() {
if ( scope.verboseLevel > 1 )
qDebug() << " DsoSettings::load()";
if ( verboseLevel > 1 )
qDebug() << " DsoSettings::load()" << storeSettings->fileName();
// Start with default configuration?
if ( resetSettings || storeSettings->value( "configuration/version", 0 ).toUInt() < CONFIG_VERSION ) {
// incompatible change or config reset by user
Expand Down Expand Up @@ -297,23 +324,29 @@ void DsoSettings::load() {


// save the persistent scope settings
// called by "MainWindow::closeEvent" and explicitely by "ui->actionSave" and "ui->actionSave_as"
// called by "DsoSettings::saveToFile()", "MainWindow::closeEvent" and explicitely by "ui->actionSave"
void DsoSettings::save() {
// Use default configuration after restart?
if ( 0 == configVersion ) {
storeSettings->clear();
if ( scope.verboseLevel > 1 )
qDebug() << " DsoSettings::save() storeSettings->clear()";
if ( verboseLevel > 1 )
qDebug() << " DsoSettings::save() storeSettings->clear() << storeSettings->fileName()";
return;
} else { // save fontSize as global setting
QSettings().setValue( "view/fontSize", view.fontSize );
QSettings().setValue( "view/toolTipVisible", scope.toolTipVisible );
}
if ( scope.verboseLevel > 1 )
qDebug() << " DsoSettings::save()" << deviceName << deviceID;
if ( verboseLevel > 1 )
qDebug() << " DsoSettings::save()" << storeSettings->fileName();
// now store individual device values

// Device ID (helps to identify the connection of a "Save as" file with a specific device)
// Date and Time of last storage
storeSettings->beginGroup( "ConfigurationSaved" );
storeSettings->setValue( "Date", QDate::currentDate().toString( "yyyy-MM-dd" ) );
storeSettings->setValue( "Time", QTime::currentTime().toString( "HH:mm:ss" ) );
storeSettings->endGroup(); // ConfigurationSaved

// Device ID (helps to identify the connection of a "Save as" file with a specific device)
storeSettings->beginGroup( "DeviceID" );
storeSettings->setValue( "Model", deviceName );
storeSettings->setValue( "SerialNumber", deviceID );
Expand Down
6 changes: 4 additions & 2 deletions openhantek/src/dsosettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ class DsoSettings {
Q_DECLARE_TR_FUNCTIONS( DsoSettings )

public:
explicit DsoSettings( const ScopeDevice *scopeDevice, bool resetSettings = false );
bool setFilename( const QString &filename );
explicit DsoSettings( const ScopeDevice *scopeDevice, int verboseLevel = 0, bool resetSettings = false );
bool saveToFile( const QString &filename );
bool loadFromFile( const QString &filename );

DsoSettingsScope scope; ///< All oscilloscope related settings
DsoSettingsView view; ///< All view related settings
Expand All @@ -48,5 +49,6 @@ class DsoSettings {
std::unique_ptr< QSettings > storeSettings;
const Dso::ControlSpecification *deviceSpecification;
void setDefaultConfig();
int verboseLevel = 0;
bool resetSettings = false;
};
15 changes: 11 additions & 4 deletions openhantek/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ int main( int argc, char *argv[] ) {
int theme = 0; // set to "auto"
int toolTipVisible = 1; // start with tooltips
bool styleFusion = false; // use system style
QString configFileName = QString();

{ // do this early at program start ...
// get font size and other global program settings:
Expand Down Expand Up @@ -157,6 +158,9 @@ int main( int argc, char *argv[] ) {

p.addHelpOption();
p.addVersionOption();
QCommandLineOption configFileOption( { "c", "config" }, QCoreApplication::translate( "main", "Load config file" ),
QCoreApplication::translate( "main", "File" ) );
p.addOption( configFileOption );
QCommandLineOption demoModeOption( { "d", "demoMode" },
QCoreApplication::translate( "main", "Demo mode without scope HW" ) );
p.addOption( demoModeOption );
Expand All @@ -180,8 +184,7 @@ int main( int argc, char *argv[] ) {
QCoreApplication::translate( "main", "Size" ) );
p.addOption( sizeOption );
QCommandLineOption condensedOption(
{ "c", "condensed" },
QString( QCoreApplication::translate( "main", "Set the font condensed value (default = %1)" ) ).arg( condensed ),
"condensed", QCoreApplication::translate( "main", "Set the font condensed value (default = %1)" ).arg( condensed ),
QCoreApplication::translate( "main", "Condensed" ) );
p.addOption( condensedOption );
QCommandLineOption resetSettingsOption(
Expand All @@ -192,6 +195,8 @@ int main( int argc, char *argv[] ) {
QCoreApplication::translate( "main", "Level" ) );
p.addOption( verboseOption );
p.process( parserApp );
if ( p.isSet( configFileOption ) )
configFileName = p.value( "config" );
demoMode = p.isSet( demoModeOption );
if ( p.isSet( fontOption ) )
font = p.value( "font" );
Expand Down Expand Up @@ -385,8 +390,10 @@ int main( int argc, char *argv[] ) {
if ( verboseLevel )
qDebug() << startupTime.elapsed() << "ms:"
<< "create settings object";
DsoSettings settings( scopeDevice.get(), resetSettings );
settings.scope.verboseLevel = verboseLevel;
DsoSettings settings( scopeDevice.get(), verboseLevel, resetSettings );

if ( !configFileName.isEmpty() )
settings.loadFromFile( configFileName );

//////// Create exporters ////////
if ( verboseLevel )
Expand Down
26 changes: 13 additions & 13 deletions openhantek/src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,13 @@ MainWindow::MainWindow( HantekDsoControl *dsoControl, DsoSettings *settings, Exp
connect( this, &MainWindow::settingsLoaded, dsoWidget, &DsoWidget::updateSlidersSettings );

connect( ui->actionOpen, &QAction::triggered, [ this, spec ]() {
QString fileName = QFileDialog::getOpenFileName( this, tr( "Open file" ), "", tr( "Settings (*.conf)" ), nullptr,
QFileDialog::DontUseNativeDialog );
if ( !fileName.isEmpty() ) {
if ( dsoSettings->setFilename( fileName ) ) {
dsoSettings->load();
}
QString configFileName = QFileDialog::getOpenFileName( this, tr( "Open file" ), "", tr( "Settings (*.conf)" ), nullptr,
QFileDialog::DontUseNativeDialog );
if ( !configFileName.isEmpty() ) {
dsoSettings->loadFromFile( configFileName );
restoreGeometry( dsoSettings->mainWindowGeometry );
restoreState( dsoSettings->mainWindowState );

emit settingsLoaded( &dsoSettings->scope, spec );

dsoWidget->updateTimebase( dsoSettings->scope.horizontal.timebase );
Expand All @@ -404,16 +405,15 @@ MainWindow::MainWindow( HantekDsoControl *dsoControl, DsoSettings *settings, Exp
} );

connect( ui->actionSave_as, &QAction::triggered, [ this ]() {
QString fileName = QFileDialog::getSaveFileName( this, tr( "Save settings" ), "", tr( "Settings (*.conf)" ), nullptr,
QFileDialog::DontUseNativeDialog );
if ( fileName.isEmpty() )
QString configFileName = QFileDialog::getSaveFileName( this, tr( "Save settings" ), "", tr( "Settings (*.conf)" ), nullptr,
QFileDialog::DontUseNativeDialog );
if ( configFileName.isEmpty() )
return;
if ( !fileName.endsWith( ".conf" ) )
fileName.append( ".conf" );
if ( !configFileName.endsWith( ".conf" ) )
configFileName.append( ".conf" );
dsoSettings->mainWindowGeometry = saveGeometry();
dsoSettings->mainWindowState = saveState();
dsoSettings->setFilename( fileName );
dsoSettings->save();
dsoSettings->saveToFile( configFileName );
} );

connect( ui->actionExit, &QAction::triggered, this, &QWidget::close );
Expand Down
50 changes: 30 additions & 20 deletions openhantek/translations/openhantek_de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -609,22 +609,22 @@
<context>
<name>DsoSettings</name>
<message>
<location filename="../src/dsosettings.cpp" line="24"/>
<location filename="../src/dsosettings.cpp" line="28"/>
<source>SP%1</source>
<translation>SP%1</translation>
</message>
<message>
<location filename="../src/dsosettings.cpp" line="29"/>
<location filename="../src/dsosettings.cpp" line="33"/>
<source>CH%1</source>
<translation>CH%1</translation>
</message>
<message>
<location filename="../src/dsosettings.cpp" line="40"/>
<location filename="../src/dsosettings.cpp" line="44"/>
<source>SPM</source>
<translation>SPM</translation>
</message>
<message>
<location filename="../src/dsosettings.cpp" line="45"/>
<location filename="../src/dsosettings.cpp" line="49"/>
<source>MATH</source>
<translation>MATH</translation>
</message>
Expand Down Expand Up @@ -1319,7 +1319,7 @@
</message>
<message>
<location filename="../src/mainwindow.cpp" line="383"/>
<location filename="../src/mainwindow.cpp" line="407"/>
<location filename="../src/mainwindow.cpp" line="408"/>
<source>Settings (*.conf)</source>
<translation>Einstellungen (*.conf)</translation>
</message>
Expand Down Expand Up @@ -1417,7 +1417,7 @@
<translation>Erzeuge eine Hardcopy und öffne den Druckdialog</translation>
</message>
<message>
<location filename="../src/mainwindow.cpp" line="407"/>
<location filename="../src/mainwindow.cpp" line="408"/>
<source>Save settings</source>
<translation>Einstellungen speichern</translation>
</message>
Expand Down Expand Up @@ -2364,73 +2364,83 @@
<context>
<name>main</name>
<message>
<location filename="../src/main.cpp" line="136"/>
<location filename="../src/main.cpp" line="171"/>
<location filename="../src/main.cpp" line="137"/>
<location filename="../src/main.cpp" line="175"/>
<source>Show the international interface, do not translate</source>
<translation>Internationale Version ohne Übersetzung</translation>
</message>
<message>
<location filename="../src/main.cpp" line="161"/>
<source>Load config file</source>
<translation>Lade Konfigurationsdatei</translation>
</message>
<message>
<location filename="../src/main.cpp" line="162"/>
<source>File</source>
<translation>Datei</translation>
</message>
<message>
<location filename="../src/main.cpp" line="165"/>
<source>Demo mode without scope HW</source>
<translation>Demo-Modus ohne Hardware</translation>
</message>
<message>
<location filename="../src/main.cpp" line="164"/>
<location filename="../src/main.cpp" line="168"/>
<source>Use OpenGL ES instead of OpenGL</source>
<translation>Benutze OpenGL ES anstelle von OpenGL</translation>
</message>
<message>
<location filename="../src/main.cpp" line="166"/>
<location filename="../src/main.cpp" line="170"/>
<source>Force OpenGL SL version 1.20</source>
<translation>Erzwinge OpenGL SL Version 1.20</translation>
</message>
<message>
<location filename="../src/main.cpp" line="168"/>
<location filename="../src/main.cpp" line="172"/>
<source>Force OpenGL SL version 1.50</source>
<translation>Erzwinge OpenGL SL Version 1.50</translation>
</message>
<message>
<location filename="../src/main.cpp" line="173"/>
<location filename="../src/main.cpp" line="177"/>
<source>Define the system font</source>
<translation>Definiere die System-Schriftart</translation>
</message>
<message>
<location filename="../src/main.cpp" line="174"/>
<location filename="../src/main.cpp" line="178"/>
<source>Font</source>
<translation>Schriftart</translation>
</message>
<message>
<location filename="../src/main.cpp" line="178"/>
<location filename="../src/main.cpp" line="182"/>
<source>Set the font size (default = %1, 0: automatic from dpi)</source>
<translation>Wähle Schriftgröße (Standard = %1, 0: automatisch von Bildschirmauflösung)</translation>
</message>
<message>
<location filename="../src/main.cpp" line="180"/>
<location filename="../src/main.cpp" line="184"/>
<source>Size</source>
<translation>Größe</translation>
</message>
<message>
<location filename="../src/main.cpp" line="184"/>
<location filename="../src/main.cpp" line="187"/>
<source>Set the font condensed value (default = %1)</source>
<translation>Wähle schmalere Schrift (Standard : %1)</translation>
</message>
<message>
<location filename="../src/main.cpp" line="185"/>
<location filename="../src/main.cpp" line="188"/>
<source>Condensed</source>
<translation>Schmaler</translation>
</message>
<message>
<location filename="../src/main.cpp" line="188"/>
<location filename="../src/main.cpp" line="191"/>
<source>Reset persistent settings, start with default</source>
<translation>Zurücksetzen auf Standardwerte</translation>
</message>
<message>
<location filename="../src/main.cpp" line="191"/>
<location filename="../src/main.cpp" line="194"/>
<source>Verbose tracing of program startup, ui and processing steps</source>
<translation>Zeige Hinweise während des Programmstarts und der Verarbeitungsschritte</translation>
</message>
<message>
<location filename="../src/main.cpp" line="192"/>
<location filename="../src/main.cpp" line="195"/>
<source>Level</source>
<translation>Stufe</translation>
</message>
Expand Down
Loading

0 comments on commit 8a7b4d3

Please sign in to comment.