Skip to content

Commit

Permalink
fix stylesheet loading on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
kduske committed Jan 4, 2020
1 parent 2cc22ff commit 70b6f7f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
10 changes: 9 additions & 1 deletion app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ if(APPLE)
set_source_files_properties(${MACOSX_SHADER_FILES} PROPERTIES MACOSX_PACKAGE_LOCATION Resources/shader)
set(APP_SOURCE ${APP_SOURCE} ${MACOSX_SHADER_FILES})

# Configure Qt stylesheets
file (GLOB_RECURSE MACOSX_STYLESHEET_FILES
"${APP_RESOURCE_DIR}/stylesheets/*.qss")
set_source_files_properties(${MACOSX_STYLESHEET_FILES} PROPERTIES MACOSX_PACKAGE_LOCATION Resources/stylesheets)
set(APP_SOURCE ${APP_SOURCE} ${MACOSX_STYLESHEET_FILES})

# Configure manual files
set_source_files_properties(${INDEX_OUTPUT_PATH} PROPERTIES MACOSX_PACKAGE_LOCATION Resources/manual)
set_source_files_properties(${DOC_MANUAL_TARGET_FILES_ABSOLUTE} PROPERTIES MACOSX_PACKAGE_LOCATION Resources/manual)
Expand Down Expand Up @@ -174,7 +180,7 @@ macro(configure_app_target APP_TARGET_NAME)
add_custom_command(TARGET ${APP_TARGET_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory "${APP_RESOURCE_DIR}/shader" "${APP_RESOURCE_DEST_DIR}/shader")

# Copy stylesheet files to resources directory
# Copy Qt stylesheet files to resources directory
add_custom_command(TARGET ${APP_TARGET_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory "${APP_RESOURCE_DEST_DIR}/stylesheets/"
COMMAND ${CMAKE_COMMAND} -E copy_directory "${APP_RESOURCE_DIR}/stylesheets" "${APP_RESOURCE_DEST_DIR}/stylesheets")
Expand Down Expand Up @@ -278,6 +284,7 @@ if(WIN32)
"${APP_DIR}/resources/fonts"
"${APP_DIR}/resources/games"
"${APP_DIR}/resources/shader"
"${APP_DIR}/resources/stylesheets"
DESTINATION . COMPONENT TrenchBroom)
set(CPACK_GENERATOR "7Z")
set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY FALSE)
Expand Down Expand Up @@ -314,6 +321,7 @@ elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/manual" DESTINATION ${LINUX_RESOURCE_LOCATION} COMPONENT TrenchBroom)
install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/images" DESTINATION ${LINUX_RESOURCE_LOCATION} COMPONENT TrenchBroom)
install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/shader" DESTINATION ${LINUX_RESOURCE_LOCATION} COMPONENT TrenchBroom)
install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/stylesheets" DESTINATION ${LINUX_RESOURCE_LOCATION} COMPONENT TrenchBroom)
install(DIRECTORY "${APP_DIR}/resources/linux/icons" DESTINATION ${LINUX_RESOURCE_LOCATION} COMPONENT TrenchBroom FILES_MATCHING PATTERN "*.png")
install(FILES "${CMAKE_SOURCE_DIR}/LICENSE.txt" DESTINATION ${LINUX_RESOURCE_LOCATION} COMPONENT TrenchBroom)
install(FILES "${APP_DIR}/resources/linux/copyright" DESTINATION ${LINUX_RESOURCE_LOCATION} COMPONENT TrenchBroom)
Expand Down
27 changes: 19 additions & 8 deletions common/src/TrenchBroomApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ namespace TrenchBroom {
return;
}

loadStyleSheets();
const bool styleSheetsLoaded = loadStyleSheets();

// these must be initialized here and not earlier
m_frameManager = std::make_unique<FrameManager>(useSDI());
Expand Down Expand Up @@ -151,6 +151,10 @@ namespace TrenchBroom {
connect(this, &QCoreApplication::aboutToQuit, this, []() {
Model::GameFactory::instance().saveAllConfigs();
});

if (!styleSheetsLoaded) {
QMessageBox::critical(nullptr, "TrenchBroom", "Could not load Qt stylesheets", QMessageBox::Ok);
}
}

// must be implemented in cpp file in order to use std::unique_ptr with forward declared type as members
Expand All @@ -170,13 +174,20 @@ namespace TrenchBroom {
return m_frameManager.get();
}

void TrenchBroomApp::loadStyleSheets() {
QFile baseStyle = QFile(IO::pathAsQString(IO::SystemPaths::appDirectory() + IO::Path("stylesheets/base.qss")));
assert(baseStyle.exists());

baseStyle.open(QFile::ReadOnly | QFile::Text);
qApp->setStyleSheet(QTextStream(&baseStyle).readAll());
baseStyle.close();
bool TrenchBroomApp::loadStyleSheets() {
const auto path = IO::SystemPaths::findResourceFile(IO::Path("stylesheets/base.qss"));
auto file = QFile(IO::pathAsQString(path));
if (file.exists()) {
// closed automatically by destructor
file.open(QFile::ReadOnly | QFile::Text);

const auto text = QTextStream(&file).readAll();
qApp->setStyleSheet(text);

return true;
} else {
return false;
}
}

const std::vector<IO::Path>& TrenchBroomApp::recentDocuments() const {
Expand Down
2 changes: 1 addition & 1 deletion common/src/TrenchBroomApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace TrenchBroom {

FrameManager* frameManager();

void loadStyleSheets();
bool loadStyleSheets();

const std::vector<IO::Path>& recentDocuments() const;
void addRecentDocumentMenu(QMenu* menu);
Expand Down

0 comments on commit 70b6f7f

Please sign in to comment.