Skip to content

Commit

Permalink
Add Windows compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
minorsecond committed Oct 1, 2022
1 parent 1dda470 commit daa8ba5
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 12 deletions.
32 changes: 22 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ IF( NOT Qt6_FOUND )
find_package(Qt6 COMPONENTS Core Widgets PrintSupport Gui REQUIRED)
ENDIF()

find_package(SQLite3 REQUIRED)
find_package(Catch2 3 REQUIRED)
find_package(Catch2 REQUIRED)

IF(APPLE)
set(ICON_NAME "icon.icns")
Expand All @@ -47,16 +46,18 @@ IF(APPLE)
file(COPY ${ICON_PATH} DESTINATION "BuzzBot.app/Contents/Resources")
ENDIF()

add_executable(BuzzBot MACOSX_BUNDLE ${ICON_PATH} src/main.cpp src/mainwindow.cpp src/database.cpp src/mainwindow.h
src/database.h src/usersettings.cpp src/usersettings.h src/calculate.cpp src/calculate.h src/beer.cpp
src/liquor.cpp src/wine.cpp src/filters.cpp src/about.cpp src/about.h src/exporters.cpp src/exporters.h
src/standard_drink_calculator.cpp src/standard_drink_calculator.h src/confirm_dialog.cpp src/confirm_dialog.h
src/graphing.cpp src/graphing.h include/qcustomplot.h include/qcustomplot.cpp
src/table_manipulation.cpp src/stats_updaters.cpp src/utilities.cpp src/utilities.h src/drink.h
src/graphing_calculations.h src/graphing_calculations.cpp src/options.h src/drink.cpp src/drink_standards.h src/drink_standards.cpp src/options.cpp)
add_executable(BuzzBot MACOSX_BUNDLE ${ICON_PATH} src/main.cpp src/mainwindow.cpp src/database.cpp
src/usersettings.cpp src/calculate.cpp src/beer.cpp
src/liquor.cpp src/wine.cpp src/filters.cpp src/about.cpp src/exporters.cpp
src/standard_drink_calculator.cpp src/confirm_dialog.cpp
src/graphing.cpp include/qcustomplot.cpp
src/table_manipulation.cpp src/stats_updaters.cpp src/utilities.cpp
src/graphing_calculations.cpp src/drink.cpp
src/drink_standards.cpp src/options.cpp)
add_executable(functions_test src/database.cpp src/database.h src/calculate.cpp src/calculate.h
test/test_database_functions.cpp test/test_calculations.cpp test/test_graph_calculations.cpp
src/utilities.cpp src/utilities.h src/drink.h src/graphing_calculations.h src/graphing_calculations.cpp src/options.h src/drink.cpp src/drink_standards.h src/drink_standards.cpp src/options.cpp)
src/utilities.cpp src/utilities.h src/drink.h src/graphing_calculations.h src/graphing_calculations.cpp
src/options.h src/drink.cpp src/drink_standards.h src/drink_standards.cpp src/options.cpp)

if (CMAKE_BUILD_TYPE MATCHES Debug)
message("Building debug")
Expand Down Expand Up @@ -103,6 +104,17 @@ ELSEIF(UNIX AND NOT APPLE)
$ENV{HOME}/.local/share/icons/big_sur_icon.png)
add_custom_command(TARGET BuzzBot POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/res/buzzbot.desktop
$ENV{HOME}/.local/share/applications/buzzbot.desktop)

ELSEIF(WIN32)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/res)
add_custom_command(TARGET BuzzBot POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/res/previous.png
${CMAKE_CURRENT_BINARY_DIR}/res/previous.png)
add_custom_command(TARGET BuzzBot POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/res/next.png
${CMAKE_CURRENT_BINARY_DIR}/res/next.png)
add_custom_command(TARGET BuzzBot POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/res/mini-icon.png
${CMAKE_CURRENT_BINARY_DIR}/res/mini-icon.png)
add_custom_command(TARGET BuzzBot POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/res/big_sur_icon.png
${CMAKE_CURRENT_BINARY_DIR}/res/big_sur_icon.png)
ENDIF()

IF(APPLE)
Expand Down
5 changes: 4 additions & 1 deletion src/about.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <QDir>
#ifdef __APPLE__
#include <CoreFoundation/CFBundle.h>
#elif __linux
#else
#include "utilities.h"
#endif

Expand Down Expand Up @@ -38,6 +38,9 @@ About::About() {
const std::string icon_path = icon_path_qstring.toStdString() + "/Contents/Resources/mini-icon.png";
CFRelease(app_url_ref);
CFRelease(mac_path);
#elif _WIN32
std::string current_path = utilities::exe_path();
const std::string icon_path {current_path + "\\res\\mini-icon.png"};
#endif
// Set icon
QPixmap pixmap(QString::fromStdString(icon_path));
Expand Down
12 changes: 12 additions & 0 deletions src/graphing_calculations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include <sstream>
#include "graphing_calculations.h"

#ifdef __WIN32
#include "utilities.h" // For strptime
#endif

std::vector<double> GraphingCalculations::get_beer_ibus(const std::vector<Drink>& all_drinks) {
/*
* Create a vector containing IBU values of all beers.
Expand Down Expand Up @@ -111,7 +115,11 @@ std::string GraphingCalculations::week_number(const int date) {

constexpr int days_per_week {7};
struct tm tm{};
#ifdef __WIN32
utilities::strptime(std::to_string(date).c_str(), "%Y%m%d", &tm);
#else
strptime(std::to_string(date).c_str(), "%Y%m%d", &tm);
#endif

const int wday {tm.tm_wday};
const int delta {wday ? wday - 1 : days_per_week - 1};
Expand All @@ -130,7 +138,11 @@ int GraphingCalculations::date_from_week_num(const std::string& week_num) {

struct tm tm{};
const std::string week_num_tmp {week_num + "-1"};
#ifdef __WIN32
utilities::strptime(week_num_tmp.c_str(), "%Y-%W-%w", &tm);
#else
strptime(week_num_tmp.c_str(), "%Y-%W-%w", &tm);
#endif

std::string year {std::to_string(tm.tm_year +1900)};
std::string month {std::to_string(tm.tm_mon + 1)};
Expand Down
8 changes: 8 additions & 0 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#ifdef __APPLE__
#include <CoreFoundation/CFBundle.h>
#elif _WIN32
#include <unistd.h>
#endif

MainWindow::MainWindow(QWidget *parent)
Expand Down Expand Up @@ -149,6 +151,10 @@ void MainWindow::configure_calendar() {
const std::string home_path = utilities::get_home_path();
const std::string next_month_arrow = home_path + "/.local/share/icons/com.rwardrup.buzzbot/next.png";
const std::string previous_month_arrow = home_path + "/.local/share/icons/com.rwardrup.buzzbot/previous.png";
#elif _WIN32
const std::string current_path {utilities::exe_path()};
const std::string next_month_arrow {current_path + "/res/next.png"};
const std::string previous_month_arrow {current_path + "/res/previous.png"};
#endif

const std::string stylesheet_text = "QCalendarWidget QWidget#qt_calendar_navigationbar\n"
Expand All @@ -171,9 +177,11 @@ void MainWindow::configure_calendar() {
"QCalendarWidget QToolButton#qt_calendar_nextmonth \n"
"{\n"
"\tqproperty-icon: url(" + next_month_arrow + ");\n""}";
#if __linux__ || __APPLE__
ui->beerDateInput->setStyleSheet(QString::fromStdString(stylesheet_text));
ui->liquorDateInput->setStyleSheet(QString::fromStdString(stylesheet_text));
ui->wineDateInput->setStyleSheet(QString::fromStdString(stylesheet_text));
#endif
}

void MainWindow::configure_table() {
Expand Down
2 changes: 1 addition & 1 deletion src/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ std::string utilities::get_local_date() {
for (char i : query_date) {
output += std::string(1, i);
}
#elif __linux
#else
const int year{1900 + now_tm.tm_year};

// 0-pad month
Expand Down
34 changes: 34 additions & 0 deletions src/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
#define BUZZBOT_UTILITIES_H

#include <string>
#include <time.h>
#include <iomanip>
#include <sstream>

#ifdef __WIN32
#include <windows.h>
#endif

class utilities {
public:
Expand All @@ -26,6 +33,33 @@ class utilities {
static std::string get_db_path();

static std::string settings_path();

static char* strptime(const char* s,
const char* f,
struct tm* tm) {
// Isn't the C++ standard lib nice? std::get_time is defined such that its
// format parameters are the exact same as strptime. Of course, we have to
// create a string stream first, and imbue it with the current C locale, and
// we also have to make sure we return the right things if it fails, or
// if it succeeds, but this is still far simpler an implementation than any
// of the versions in any of the C standard libraries.
std::istringstream input(s);
input.imbue(std::locale(setlocale(LC_ALL, nullptr)));
input >> std::get_time(tm, f);
if (input.fail()) {
return nullptr;
}
return (char*)(s + input.tellg());
}

static std::string exe_path() {
TCHAR buffer[MAX_PATH] = { 0 };
GetModuleFileName( NULL, buffer, MAX_PATH );
std::wstring::size_type pos = std::wstring(buffer).find_last_of(L"\\/");
std::wstring ws(std::wstring(buffer).substr(0, pos));
std::string str(ws.begin(), ws.end());
return str;
}
};


Expand Down

0 comments on commit daa8ba5

Please sign in to comment.