Skip to content

Commit

Permalink
added signalmapper and settingmanager
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzhaninan committed Mar 5, 2018
1 parent 1936a0c commit 4cfbe11
Show file tree
Hide file tree
Showing 20 changed files with 276 additions and 220 deletions.
2 changes: 1 addition & 1 deletion stacer-core/Tools/apt_source_tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ QList<APTSourcePtr> AptSourceTool::getSourceList()
aptSource->components = sourceColumns.mid(3).join(' ');

aptSource->source = line.trimmed().replace("#", "").trimmed();
qDebug() << aptSource->source;
// qDebug() << aptSource->source;

aptSourceList.append(aptSource);
}
Expand Down
140 changes: 22 additions & 118 deletions stacer/Managers/app_manager.cpp
Original file line number Diff line number Diff line change
@@ -1,91 +1,67 @@
#include "app_manager.h"
#include <QDebug>

AppManager *AppManager::_instance = nullptr;
AppManager *AppManager::instance = nullptr;

AppManager *AppManager::ins()
{
if (! _instance) {
_instance = new AppManager;
if (! instance) {
instance = new AppManager;
}

return _instance;
return instance;
}

AppManager::AppManager(QObject *parent) : QObject(parent)
AppManager::AppManager()
{
trayIcon = new QSystemTrayIcon(QIcon(":/static/logo.png"), this);
mSettingManager = SettingManager::ins();

// font settings
QFontDatabase::addApplicationFont(":/static/font/Ubuntu-R.ttf");

configPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);

settings = new QSettings(QString("%1/settings.conf").arg(configPath), QSettings::IniFormat);
mTrayIcon = new QSystemTrayIcon(QIcon(":/static/logo.png"));

loadLanguageList();

loadThemeList();

themeName = settings->value(THEME_PROP, "default").toString();

if (translator.load(QString("stacer_%1").arg(getLanguageCode()), qApp->applicationDirPath() + "/translations")) {
if (translator.load(QString("stacer_%1").arg(mSettingManager->getLanguage()), qApp->applicationDirPath() + "/translations")) {
qApp->installTranslator(&translator);
(getLanguageCode() == "ar") ? qApp->setLayoutDirection(Qt::RightToLeft) : qApp->setLayoutDirection(Qt::LeftToRight);
(mSettingManager->getLanguage() == "ar") ? qApp->setLayoutDirection(Qt::RightToLeft) : qApp->setLayoutDirection(Qt::LeftToRight);
} else {
qCritical() << "Translator could not load.";
}

styleValues = new QSettings(QString(":/static/themes/%1/style/values.ini").arg(themeName), QSettings::IniFormat);
}

QSystemTrayIcon *AppManager::getTrayIcon()
{
return trayIcon;
return mTrayIcon;
}

QSettings *AppManager::getStyleValues() const
{
return styleValues;
}

/************
* LANGUAGE
***********/
void AppManager::loadLanguageList()
{
QJsonArray langs = QJsonDocument::fromJson(FileUtil::readStringFromFile(":/static/languages.json").toUtf8())
.array();
QByteArray lanuagesJson = FileUtil::readStringFromFile(":/static/languages.json").toUtf8();
QJsonArray lanuages = QJsonDocument::fromJson(lanuagesJson).array();

for (int i = 0; i < langs.count(); ++i) {
for (int i = 0; i < lanuages.count(); ++i) {

QJsonObject ob = langs.at(i).toObject();
QJsonObject ob = lanuages.at(i).toObject();

languageList.insert(ob["value"].toString(), ob["text"].toString());
}
}

void AppManager::setLanguage(const QString &value)
{
settings->setValue(LANG_PROP, value);
}

QString AppManager::getLanguageCode() const
{
return settings->value(LANG_PROP, "en").toString();
}

QMap<QString, QString> AppManager::getLanguageList() const
{
return languageList;
}

/************
* THEME
***********/
void AppManager::loadThemeList()
{
QJsonArray themes = QJsonDocument::fromJson(FileUtil::readStringFromFile(":/static/themes.json").toUtf8())
.array();
QByteArray themesJson = FileUtil::readStringFromFile(":/static/themes.json").toUtf8();
QJsonArray themes = QJsonDocument::fromJson(themesJson).array();

for (int i = 0; i < themes.count(); ++i) {

Expand All @@ -100,96 +76,24 @@ QMap<QString, QString> AppManager::getThemeList() const
return themeList;
}

QString AppManager::getThemeName() const
{
return themeName;
}

void AppManager::updateStylesheet()
{
styleValues = new QSettings(QString(":/static/themes/%1/style/values.ini").arg(themeName), QSettings::NativeFormat);
QString appThemePath = QString(":/static/themes/%1/style").arg(mSettingManager->getThemeName());
styleValues = new QSettings(QString("%1/values.ini").arg(appThemePath), QSettings::IniFormat);

stylesheetFileContent = FileUtil::readStringFromFile(QString(":/static/themes/%1/style/style.qss").arg(themeName));
stylesheetFileContent = FileUtil::readStringFromFile(QString("%1/style.qss").arg(appThemePath));

// set values
// set values example: @color01 => #fff
for (const QString &key : styleValues->allKeys()) {
stylesheetFileContent.replace(key, styleValues->value(key).toString());
}

qApp->setStyleSheet(stylesheetFileContent);

emit changedTheme();
}

void AppManager::setThemeName(const QString &value)
{
themeName = value;

settings->setValue(THEME_PROP, value);
emit SignalMapper::ins()->changedAppTheme();
}

QString AppManager::getStylesheetFileContent() const
{
return stylesheetFileContent;
}

/************
* DISKS
***********/
void AppManager::setDiskName(const QString &value)
{
settings->setValue(DISK_PROP, value);
emit changedDisk();
}

QString AppManager::getDiskName()
{
return settings->value(DISK_PROP, "").toString();
}

/************
* HOMEPAGE
***********/

void AppManager::setHomePage(const QString &value)
{
settings->setValue(HOMEPAGE_PROP, value);
}

QString AppManager::getHomePage()
{
return settings->value(HOMEPAGE_PROP, "Dashboard").toString();
}

/************
* ALERT MESSAGES
***********/
void AppManager::setCpuPercent(const int value)
{
settings->setValue(CPU_PERCENT, value);
}

int AppManager::getCpuPercent()
{
return settings->value(CPU_PERCENT, 0).toInt();
}

void AppManager::setMemoryPercent(const int value)
{
settings->setValue(MEMORY_PERCENT, value);
}

int AppManager::getMemoryPercent()
{
return settings->value(MEMORY_PERCENT, 0).toInt();
}

void AppManager::setDiskPercent(const int value)
{
settings->setValue(DISK_PERCENT, value);
}

int AppManager::getDiskPercent()
{
return settings->value(DISK_PERCENT, 0).toInt();
}
59 changes: 12 additions & 47 deletions stacer/Managers/app_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,53 @@
#define APP_MANAGER_H

#include <QApplication>
#include <QObject>
#include <QMap>
#include <QStandardPaths>
#include <QSettings>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QTranslator>
#include <QFontDatabase>
#include <QEasingCurve>
#include <QSystemTrayIcon>

#include <Utils/file_util.h>
#include "Utils/file_util.h"
#include "Managers/setting_manager.h"
#include "signal_mapper.h"

#include <QDebug>

#define THEME_PROP "ThemeName"
#define LANG_PROP "Language"
#define DISK_PROP "DiskName"
#define HOMEPAGE_PROP "HomePage"
#define CPU_PERCENT "CPUPercent"
#define MEMORY_PERCENT "MemoryPercent"
#define DISK_PERCENT "DiskPercent"

class AppManager : public QObject
class AppManager
{
Q_OBJECT

public:
static AppManager *ins();

QString getLanguageCode() const;
void setLanguage(const QString &value);
QMap<QString, QString> getLanguageList() const;
void loadLanguageList();
QString getStylesheetFileContent() const;

void updateStylesheet();
void setThemeName(const QString &value);
QString getThemeName() const;
QMap<QString, QString> getThemeList() const;
void loadThemeList();
QSettings *getStyleValues() const;

void setDiskName(const QString &value);
QString getDiskName();
void updateStylesheet();
QString getStylesheetFileContent() const;

void setHomePage(const QString &value);
QString getHomePage();
QSettings *getStyleValues() const;

QSystemTrayIcon *getTrayIcon();

void setCpuPercent(const int value);
int getCpuPercent();

void setMemoryPercent(const int value);
int getMemoryPercent();

void setDiskPercent(const int value);
int getDiskPercent();

signals:
void changedTheme();
void changedDisk();

private:
static AppManager *_instance;
explicit AppManager(QObject *parent = nullptr);
static AppManager *instance;
AppManager();

private:
QTranslator translator;
QString configPath;
QString themeName;

QSystemTrayIcon *trayIcon;
QSystemTrayIcon *mTrayIcon;

QSettings *settings;
QSettings *styleValues;

QMap<QString, QString> languageList;
QMap<QString, QString> themeList;
QString stylesheetFileContent;

SettingManager *mSettingManager;
};

#endif // APP_MANAGER_H
Loading

0 comments on commit 4cfbe11

Please sign in to comment.