From 1d95344358ea2abd9610cdeb8adb9031fc10af56 Mon Sep 17 00:00:00 2001 From: Andrei Popa Date: Tue, 11 Jun 2024 17:20:35 +0300 Subject: [PATCH] a big mess --- core/include/core/stylerepository.h | 35 +++++++++++++++++ core/src/stylerepository.cpp | 59 +++++++++++++++++++++++++++++ main.cpp | 7 +++- pyScripts/json.py | 32 +++++++++++++++- pyScripts/main.py | 2 +- style/json/globals/dimensions.json | 3 ++ style/qss/label/big.qss | 37 +++++++++--------- 7 files changed, 153 insertions(+), 22 deletions(-) create mode 100644 core/include/core/stylerepository.h create mode 100644 core/src/stylerepository.cpp diff --git a/core/include/core/stylerepository.h b/core/include/core/stylerepository.h new file mode 100644 index 0000000000..e6f2120297 --- /dev/null +++ b/core/include/core/stylerepository.h @@ -0,0 +1,35 @@ +#ifndef STYLEREPOSITORY_H +#define STYLEREPOSITORY_H + +#include +#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 diff --git a/core/src/stylerepository.cpp b/core/src/stylerepository.cpp new file mode 100644 index 0000000000..1f8761c1a0 --- /dev/null +++ b/core/src/stylerepository.cpp @@ -0,0 +1,59 @@ +#include "stylerepository.h" +#include "qcolor.h" +#include "qjsonobject.h" + +#include +#include +#include +#include + +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; +} diff --git a/main.cpp b/main.cpp index e7d2d7fecc..b9731f4cb6 100644 --- a/main.cpp +++ b/main.cpp @@ -9,6 +9,7 @@ #include #include #include +#include using namespace scopy; @@ -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(); diff --git a/pyScripts/json.py b/pyScripts/json.py index 1ca55aff5d..bc7494a96a 100644 --- a/pyScripts/json.py +++ b/pyScripts/json.py @@ -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() @@ -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 @@ -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 @@ -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() + diff --git a/pyScripts/main.py b/pyScripts/main.py index 2ff9d11df5..a41cfa4fdb 100644 --- a/pyScripts/main.py +++ b/pyScripts/main.py @@ -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): diff --git a/style/json/globals/dimensions.json b/style/json/globals/dimensions.json index e69de29bb2..6294772a7a 100644 --- a/style/json/globals/dimensions.json +++ b/style/json/globals/dimensions.json @@ -0,0 +1,3 @@ +{ + "unit_pixel_1": "12" +} diff --git a/style/qss/label/big.qss b/style/qss/label/big.qss index bbe0a01e34..1f91aca932 100644 --- a/style/qss/label/big.qss +++ b/style/qss/label/big.qss @@ -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&; }