Skip to content

Commit

Permalink
a big mess
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei47w committed Jun 11, 2024
1 parent 4aac6cc commit 1d95344
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 22 deletions.
35 changes: 35 additions & 0 deletions core/include/core/stylerepository.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef STYLEREPOSITORY_H
#define STYLEREPOSITORY_H

#include <QObject>
#include "scopy-core_export.h"

namespace scopy {
class SCOPY_CORE_EXPORT StyleRepository : public QObject
{
Q_OBJECT
protected:
StyleRepository(QObject *parent = nullptr);
~StyleRepository();

public:
StyleRepository(StyleRepository &other) = delete;
void operator=(const StyleRepository &) = delete;
static StyleRepository *GetInstance();

QString getAttribute(char *key);
QColor getColor(char *key);
int getDimension(char *key);

protected:
void applyStyle();

private:
static StyleRepository *pinstance_;
QJsonDocument *m_json;
QString m_jsonPath;
QString m_qssPath;
};
} // namespace scopy

#endif // STYLEREPOSITORY_H
59 changes: 59 additions & 0 deletions core/src/stylerepository.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "stylerepository.h"
#include "qcolor.h"
#include "qjsonobject.h"

#include <QApplication>
#include <QFile>
#include <QJsonDocument>
#include <iostream>

using namespace scopy;

StyleRepository *StyleRepository::pinstance_{nullptr};

StyleRepository::StyleRepository(QObject *parent)
: QObject(parent),
m_jsonPath("style_variables.json"),
m_qssPath("style.qss")

{
QFile file(m_jsonPath);
file.open(QIODevice::ReadOnly);
QByteArray data = file.readAll();
file.close();
m_json = new QJsonDocument(QJsonDocument::fromJson(data));

applyStyle();
}

StyleRepository::~StyleRepository() {}

StyleRepository *StyleRepository::GetInstance()
{
if(pinstance_ == nullptr) {
pinstance_ = new StyleRepository(QApplication::instance()); // singleton has the app as parent
}
return pinstance_;
}

QString StyleRepository::getAttribute(char *key) { return m_json->object().value(key).toString(); }

QColor StyleRepository::getColor(char *key) { return QColor(getAttribute(key)); }

int StyleRepository::getDimension(char *key) { return getAttribute(key).toInt(); }

void StyleRepository::applyStyle()
{
QFile file(m_qssPath);
file.open(QIODevice::ReadOnly);
QString style = QString(file.readAll());
file.close();

for(const QString &key: m_json->object().keys()) {
QJsonValue value = m_json->object().value(key);
style.replace("&" + key + "&", value.toString());
}

qApp->setStyleSheet(style);
std::cout << style.toStdString() << std::endl;
}
7 changes: 5 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <core/scopymainwindow_api.h>
#include <gui/utils.h>
#include <core/crashreport.h>
#include <core/stylerepository.h>

using namespace scopy;

Expand Down Expand Up @@ -101,8 +102,10 @@ int main(int argc, char *argv[])
printRuntimeEnvironmentInfo();
ApplicationRestarter restarter(QString::fromLocal8Bit(argv[0]));
a.setWindowIcon(QIcon(":/gui/icon.ico"));
a.setStyle("Fusion");
a.setStyleSheet(Util::loadStylesheetFromFile(":/gui/stylesheets/default.qss"));
// a.setStyle("Fusion");
// a.setStyleSheet(Util::loadStylesheetFromFile(":/gui/stylesheets/default.qss"));
StyleRepository::GetInstance();

ScopyMainWindow w;
w.show();

Expand Down
32 changes: 30 additions & 2 deletions pyScripts/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
import main


def append_variable(key, value):
with open(json_path, 'a') as file:
file.write("\t\"" + key + "\": " + value + ",\n")


def parse_json_file(filepath):
with open(filepath, 'r') as file:
content = file.read()
Expand All @@ -17,8 +22,10 @@ def parse_json_file(filepath):
for item in items:
key, value = item.split(':')
key = key.strip().strip('"')
value = value.strip().strip('"')
json_content[key] = os.path.relpath(filepath, style_folder).replace("/", "_").replace(file_extension, "") + "_" + key
formatted_key = os.path.relpath(filepath, style_folder).replace("/", "_").replace(file_extension,
"") + "_" + key
json_content[key] = formatted_key
append_variable(formatted_key, value)

return json_content

Expand All @@ -37,6 +44,23 @@ def generate_variable(filepath, indent):
return variable[:-1]


def open_variables_file():
with open(json_path, 'w') as file:
file.write("{\n")


def close_variables_file():
with open(json_path, 'r+') as file:
content = file.read()
last_comma_index = content.rfind(',')
if last_comma_index != -1:
content = content[:last_comma_index]
file.seek(0)
file.write(content)
file.truncate()
file.write("\n}\n")


if __name__ == "__main__":
import sys

Expand All @@ -50,7 +74,11 @@ def generate_variable(filepath, indent):
header_path = source_folder + "/gui/include/gui/style_variables.h"
file_extension = '.json'
style_folder = source_folder + "/style"
json_path = build_folder + "/style_variables.json"

open_variables_file()
namespace_structure = main.create_namespace_structure(style_folder, file_extension)
namespace_code = main.generate_namespace_code(namespace_structure, generate_variable)
main.write_header_file(header_path, namespace_code, header_name)
close_variables_file()

2 changes: 1 addition & 1 deletion pyScripts/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def generate_variable(filepath, indent):
value = os.path.relpath(filepath, qss_folder).replace("/", "_").replace(file_extension, "")
replace_property(filepath, value)

return f'{indent}const char* ' + os.path.basename(filepath).replace(file_extension, '') + f' = "{value}";'
return f'{indent}const char *const ' + os.path.basename(filepath).replace(file_extension, '') + f' = "{value}";'


def generate_namespace_code(namespace_structure, variable_func):
Expand Down
3 changes: 3 additions & 0 deletions style/json/globals/dimensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"unit_pixel_1": "12"
}
37 changes: 20 additions & 17 deletions style/qss/label/big.qss
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
QPushButton[&&property&&="true"] {
width: &unit_pixel_1&; // 48px
height: &unit_pixel_1&; // 48px
QPushButton[&&property&&=true] {
width: &json_globals_dimensions_unit_pixel_1&;
height: &json_globals_dimensions_unit_pixel_1&;

&add_border_small&
// border-radius: 0px 2px 2px 0px;
// border-style: outset;
border-radius: 0px 2px 2px 0px;
border-style: outset;

qproperty-iconSize: &1pixel_unit&; // 48px
background-color: &color_highlight_1&; // #272730
qproperty-iconSize: &json_globals_dimensions_unit_pixel_1&;
background-color: &theme_dark_colors_highlight&;

&add_font_small&
// color: white;
// font-weight: 700;
// font-size: 14px;

color: &theme_dark_colors_highlight&;
font-weight: 700;
font-size: 14px;

height: &unit_pixel_1&;
height: &json_globals_dimensions_unit_pixel_1&;
}

QPushButton[&&property&&=true]:hover {
background-color: &theme_dark_colors_background&;
}

QPushButton[&&property&&="true"]:checked {
background-color: &color_highlight_2&; // #4A64FF
QPushButton[&&property&&=true]:pressed {
background-color: &theme_dark_colors_background&;
}

QPushButton[&&property&&="true"]:pressed {
background-color: &color_highlight_2&; // #4A64FF
QPushButton{
color: &theme_dark_colors_highlight&;
}

0 comments on commit 1d95344

Please sign in to comment.