Skip to content

Commit

Permalink
use QSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
Odizinne committed Oct 25, 2024
1 parent a8bab54 commit e547675
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 97 deletions.
62 changes: 16 additions & 46 deletions src/NoteWidget/notewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,10 @@
#include <QRandomGenerator>
#include <QFont>
#include <QTextBlock>
#include <QGraphicsBlurEffect>
#include <QSettings>

using namespace Utils;
const int resizeMargin = 7;
#ifdef _WIN32
const QString NoteWidget::settingsFile = QStandardPaths::writableLocation(
QStandardPaths::AppDataLocation)
+ "/Qwote/settings.json";
#elif __linux__
const QString NoteWidget::settingsFile = QDir::homePath() + "/.config/Qwote/settings.json";

#endif

QList<NoteWidget*> NoteWidget::existingNotes;

Expand Down Expand Up @@ -467,47 +459,25 @@ void NoteWidget::loadSettings()
#elif __linux__
defaultFontFamily = "Monospace";
#endif
QFont currentFont = ui->noteTextEdit->font();
int currentSize = currentFont.pointSize();
int titleSize = 11;

QDir settingsDir(QFileInfo(settingsFile).absolutePath());
if (!settingsDir.exists()) {
settingsDir.mkpath(settingsDir.absolutePath());
}
QSettings settings("Odizinne", "Qwote");

QFile file(settingsFile);
if (!file.exists()) {
QFont defaultFont;
defaultFont.setFamily(defaultFontFamily);
defaultFont.setPointSize(11);
ui->noteTextEdit->setFont(defaultFont);
defaultFont.setBold(true);
ui->noteTitleLineEdit->setFont(defaultFont);
QFont font;
font.setFamily(settings.value("font", defaultFontFamily).toString());
font.setPointSize(settings.value("fontSize", 11).toInt());
ui->noteTextEdit->setFont(font);

} else {
if (file.open(QIODevice::ReadOnly)) {
QJsonParseError parseError;
QJsonDocument doc = QJsonDocument::fromJson(file.readAll(), &parseError);
if (parseError.error == QJsonParseError::NoError) {
settings = doc.object();
QFont userFont;
userFont.setFamily(settings.value("font").toString());
opacity = settings.contains("opacity") ? settings.value("opacity").toInt() : 255;
roundedCorners = settings.contains("roundedCorners") ? settings.value("roundedCorners").toBool() : true;
userFont.setPointSize(currentSize);
ui->noteTextEdit->setFont(userFont);
userFont.setPointSize(titleSize);
userFont.setBold(true);
ui->noteTitleLineEdit->setFont(userFont);
setContentTransparency();
setTitleTransparency();
update();
}
file.close();
}
}
QFont titleFont = font;
titleFont.setPointSize(settings.value("titleFontSize", 11).toInt());
titleFont.setBold(true);
ui->noteTitleLineEdit->setFont(titleFont);

opacity = settings.value("opacity", 255).toInt();
roundedCorners = settings.value("roundedCorners", true).toBool();

setContentTransparency();
setTitleTransparency();
update();
}

void NoteWidget::updateFormat() {
Expand Down
2 changes: 0 additions & 2 deletions src/NoteWidget/notewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ private slots:
bool ctrlPressed;
Qt::Edges resizeDirection;
QPoint dragStartPosition;
QJsonObject settings;
static const QString settingsFile;
int opacity;
bool roundedCorners;

Expand Down
59 changes: 13 additions & 46 deletions src/SettingsPage/settingspage.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
#include "settingspage.h"
#include "ui_settingspage.h"
#include <QFile>
#include <QJsonDocument>
#include <QDir>
#include <QFontDatabase>
#include <QStandardPaths>

#ifdef _WIN32
const QString SettingsPage::settingsFile = QStandardPaths::writableLocation(
QStandardPaths::AppDataLocation)
+ "/Qwote/settings.json";
#elif __linux__
const QString SettingsPage::settingsFile = QDir::homePath() + "/.config/Qwote/settings.json";

#endif
#include <QSettings>

SettingsPage::SettingsPage(QWidget *parent)
: QMainWindow(parent)
Expand All @@ -36,10 +24,8 @@ SettingsPage::~SettingsPage()

void SettingsPage::populateFontComboBox() {
QStringList fonts = QFontDatabase::families();

ui->fontComboBox->clear();

foreach (const QString &font, fonts) {
for (const QString &font : fonts) {
ui->fontComboBox->addItem(font);
}
}
Expand All @@ -51,46 +37,27 @@ void SettingsPage::onFontComboBoxIndexChanged() {

void SettingsPage::loadSettings()
{
QDir settingsDir(QFileInfo(settingsFile).absolutePath());
if (!settingsDir.exists()) {
settingsDir.mkpath(settingsDir.absolutePath());
}
QSettings settings("Odizinne", "Qwote");

QFile file(settingsFile);
if (!file.exists()) {
QString defaultFont;
#ifdef _WIN32
ui->fontComboBox->setCurrentText("Consolas");
defaultFont = "Consolas";
#elif __linux__
ui->fontComboBox->setCurrentText("Monospace");
defaultFont = "Monospace";
#endif

} else {
if (file.open(QIODevice::ReadOnly)) {
QJsonParseError parseError;
QJsonDocument doc = QJsonDocument::fromJson(file.readAll(), &parseError);
if (parseError.error == QJsonParseError::NoError) {
settings = doc.object();
ui->fontComboBox->setCurrentText(settings.value("font").toString());
ui->opacitySlider->setValue(settings.value("opacity").toInt());
ui->roundedCornersCheckbox->setChecked(settings.value("roundedCorners").toBool());
}
file.close();
}
}
ui->fontComboBox->setCurrentText(settings.value("font", defaultFont).toString());
ui->opacitySlider->setValue(settings.value("opacity", 255).toInt());
ui->roundedCornersCheckbox->setChecked(settings.value("roundedCorners", true).toBool());
}

void SettingsPage::saveSettings()
{
settings["font"] = ui->fontComboBox->currentText();
settings["opacity"] = ui->opacitySlider->value();
settings["roundedCorners"] = ui->roundedCornersCheckbox->isChecked();
QSettings settings("Odizinne", "Qwote");

QFile file(settingsFile);
if (file.open(QIODevice::WriteOnly)) {
QJsonDocument doc(settings);
file.write(doc.toJson(QJsonDocument::Indented));
file.close();
}
settings.setValue("font", ui->fontComboBox->currentText());
settings.setValue("opacity", ui->opacitySlider->value());
settings.setValue("roundedCorners", ui->roundedCornersCheckbox->isChecked());
}

void SettingsPage::onOpacitySliderValueChanged() {
Expand Down
3 changes: 0 additions & 3 deletions src/SettingsPage/settingspage.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define SETTINGSPAGE_H

#include <QMainWindow>
#include <QJsonObject>

namespace Ui {
class SettingsPage;
Expand All @@ -20,8 +19,6 @@ class SettingsPage : public QMainWindow
Ui::SettingsPage *ui;
void loadSettings();
void saveSettings();
QJsonObject settings;
static const QString settingsFile;
void populateFontComboBox();

private slots:
Expand Down

0 comments on commit e547675

Please sign in to comment.