From ec410e00a38ac004c7ac60821609e204f49efd99 Mon Sep 17 00:00:00 2001 From: Mohammed Date: Sat, 8 Apr 2023 14:02:47 +0200 Subject: [PATCH] Updating the Qt example to build in CMake --- .gitignore | 3 +- CMakeLists.txt | 7 +- .../WinToastExample/WinToastExample.pro | 27 --- examples/CMakeLists.txt | 3 + .../console-example/README.MD | 0 .../WinToast Console Example.sln | 0 .../WinToast Console Example.vcxproj | 0 .../WinToast Console Example.vcxproj.filters | 0 .../WinToast Console Example.vcxproj.user | 0 .../console-example/if_terminal_298878.png | Bin .../console-example/main.cpp | 0 examples/qt-gui-example/CMakeLists.txt | 9 + .../qt-gui-example}/main.cpp | 3 +- .../qt-gui-example}/mainwindow.cpp | 77 ++++--- .../qt-gui-example}/mainwindow.h | 9 +- .../qt-gui-example}/mainwindow.ui | 188 ++++++++++-------- src/wintoastlib.cpp | 7 +- 17 files changed, 182 insertions(+), 151 deletions(-) delete mode 100644 example/qt-gui-example/WinToastExample/WinToastExample.pro create mode 100644 examples/CMakeLists.txt rename {example => examples}/console-example/README.MD (100%) rename {example => examples}/console-example/WinToast Console Example.sln (100%) rename {example => examples}/console-example/WinToast Console Example.vcxproj (100%) rename {example => examples}/console-example/WinToast Console Example.vcxproj.filters (100%) rename {example => examples}/console-example/WinToast Console Example.vcxproj.user (100%) rename {example => examples}/console-example/if_terminal_298878.png (100%) rename {example => examples}/console-example/main.cpp (100%) create mode 100644 examples/qt-gui-example/CMakeLists.txt rename {example/qt-gui-example/WinToastExample => examples/qt-gui-example}/main.cpp (79%) rename {example/qt-gui-example/WinToastExample => examples/qt-gui-example}/mainwindow.cpp (51%) rename {example/qt-gui-example/WinToastExample => examples/qt-gui-example}/mainwindow.h (65%) rename {example/qt-gui-example/WinToastExample => examples/qt-gui-example}/mainwindow.ui (84%) diff --git a/.gitignore b/.gitignore index d2c011a..eb5e316 100644 --- a/.gitignore +++ b/.gitignore @@ -40,4 +40,5 @@ Release/ enc_temp_folder/ /example/qt-gui-example/build-WinToastExample-Desktop_Qt_5_9_2_MSVC2015_32bit-Debug build/ -cmake-build-* \ No newline at end of file +cmake-build-* +CMakeLists.txt.user \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 835e2fb..d1183b5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,8 @@ set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) -option(WINTOASTLIB_BUILD_EXAMPLES "If enabled, the examples will be built" OFF) +option(WINTOASTLIB_BUILD_EXAMPLES "Compile the examples" ON) +option(WINTOASTLIB_QT_ENABLED "Enable Qt support to build the GUI examples" ON) set(WINTOASTLIB_LIBNAME WinToast) set(WINTOASTLIB_HEADERS ${CMAKE_CURRENT_LIST_DIR}/include/wintoastlib.h) @@ -13,6 +14,6 @@ add_library(${WINTOASTLIB_LIBNAME} STATIC ${WINTOASTLIB_HEADERS} ${WINTOASTLIB_S target_include_directories(${WINTOASTLIB_LIBNAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include) target_link_libraries(${WINTOASTLIB_LIBNAME} psapi) -if (WINTOASTLIB_BUILD_EXAMPLES) +if (${WINTOASTLIB_BUILD_EXAMPLES}) add_subdirectory(examples) -endif() \ No newline at end of file +endif() diff --git a/example/qt-gui-example/WinToastExample/WinToastExample.pro b/example/qt-gui-example/WinToastExample/WinToastExample.pro deleted file mode 100644 index e5d4385..0000000 --- a/example/qt-gui-example/WinToastExample/WinToastExample.pro +++ /dev/null @@ -1,27 +0,0 @@ -#------------------------------------------------- -# -# Project created by QtCreator 2016-10-01T17:29:36 -# -#------------------------------------------------- - -QT += core gui -CONFIG += c++11 - -greaterThan(QT_MAJOR_VERSION, 4): QT += widgets - -TARGET = WinToastExample -TEMPLATE = app - - -SOURCES += main.cpp\ - mainwindow.cpp \ - ../../../src/wintoastlib.cpp - -HEADERS += mainwindow.h \ - ../../../src/wintoastlib.h - -FORMS += mainwindow.ui - -#win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../lib/Release/ -lWinToastLib -#else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../lib/Debug/ -lWinToastLib - diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..72447bf --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,3 @@ +if (${WINTOASTLIB_QT_ENABLED}) + add_subdirectory(qt-gui-example) +endif() \ No newline at end of file diff --git a/example/console-example/README.MD b/examples/console-example/README.MD similarity index 100% rename from example/console-example/README.MD rename to examples/console-example/README.MD diff --git a/example/console-example/WinToast Console Example.sln b/examples/console-example/WinToast Console Example.sln similarity index 100% rename from example/console-example/WinToast Console Example.sln rename to examples/console-example/WinToast Console Example.sln diff --git a/example/console-example/WinToast Console Example.vcxproj b/examples/console-example/WinToast Console Example.vcxproj similarity index 100% rename from example/console-example/WinToast Console Example.vcxproj rename to examples/console-example/WinToast Console Example.vcxproj diff --git a/example/console-example/WinToast Console Example.vcxproj.filters b/examples/console-example/WinToast Console Example.vcxproj.filters similarity index 100% rename from example/console-example/WinToast Console Example.vcxproj.filters rename to examples/console-example/WinToast Console Example.vcxproj.filters diff --git a/example/console-example/WinToast Console Example.vcxproj.user b/examples/console-example/WinToast Console Example.vcxproj.user similarity index 100% rename from example/console-example/WinToast Console Example.vcxproj.user rename to examples/console-example/WinToast Console Example.vcxproj.user diff --git a/example/console-example/if_terminal_298878.png b/examples/console-example/if_terminal_298878.png similarity index 100% rename from example/console-example/if_terminal_298878.png rename to examples/console-example/if_terminal_298878.png diff --git a/example/console-example/main.cpp b/examples/console-example/main.cpp similarity index 100% rename from example/console-example/main.cpp rename to examples/console-example/main.cpp diff --git a/examples/qt-gui-example/CMakeLists.txt b/examples/qt-gui-example/CMakeLists.txt new file mode 100644 index 0000000..c57a275 --- /dev/null +++ b/examples/qt-gui-example/CMakeLists.txt @@ -0,0 +1,9 @@ +find_package(Qt6 REQUIRED COMPONENTS Widgets) +qt_standard_project_setup() + +add_executable(WinToastGuiExample mainwindow.ui mainwindow.cpp main.cpp) +target_link_libraries(WinToastGuiExample PRIVATE WinToast Qt6::Widgets) +set_target_properties(WinToastGuiExample PROPERTIES + WIN32_EXECUTABLE ON + MACOSX_BUNDLE ON +) \ No newline at end of file diff --git a/example/qt-gui-example/WinToastExample/main.cpp b/examples/qt-gui-example/main.cpp similarity index 79% rename from example/qt-gui-example/WinToastExample/main.cpp rename to examples/qt-gui-example/main.cpp index b48f94e..309dc2b 100644 --- a/example/qt-gui-example/WinToastExample/main.cpp +++ b/examples/qt-gui-example/main.cpp @@ -1,8 +1,7 @@ #include "mainwindow.h" #include -int main(int argc, char *argv[]) -{ +int main(int argc, char* argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); diff --git a/example/qt-gui-example/WinToastExample/mainwindow.cpp b/examples/qt-gui-example/mainwindow.cpp similarity index 51% rename from example/qt-gui-example/WinToastExample/mainwindow.cpp rename to examples/qt-gui-example/mainwindow.cpp index 6508d6b..8854187 100644 --- a/example/qt-gui-example/WinToastExample/mainwindow.cpp +++ b/examples/qt-gui-example/mainwindow.cpp @@ -4,14 +4,22 @@ #include #include -#include "../../../src/wintoastlib.h" +#include "wintoastlib.h" + using namespace WinToastLib; -MainWindow::MainWindow(QWidget *parent) : - QMainWindow(parent), - ui(new Ui::MainWindow) -{ + +MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); + ui->toastType->addItem("ImageAndText01", WinToastTemplate::ImageAndText01); + ui->toastType->addItem("ImageAndText02", WinToastTemplate::ImageAndText02); + ui->toastType->addItem("ImageAndText03", WinToastTemplate::ImageAndText03); + ui->toastType->addItem("ImageAndText04", WinToastTemplate::ImageAndText04); + ui->toastType->addItem("Text01", WinToastTemplate::Text01); + ui->toastType->addItem("Text02", WinToastTemplate::Text02); + ui->toastType->addItem("Text03", WinToastTemplate::Text03); + ui->toastType->addItem("Text04", WinToastTemplate::Text04); + ui->audioMode->addItem("Default", WinToastTemplate::AudioOption::Default); ui->audioMode->addItem("Loop", WinToastTemplate::AudioOption::Loop); ui->audioMode->addItem("Silence", WinToastTemplate::AudioOption::Silent); @@ -21,26 +29,26 @@ MainWindow::MainWindow(QWidget *parent) : ui->audioSystemFile->addItem("SMS", WinToastTemplate::AudioSystemFile::SMS); ui->audioSystemFile->addItem("Alarm", WinToastTemplate::AudioSystemFile::Alarm); + ui->cropHint->addItem("Square", WinToastTemplate::CropHint::Square); + ui->cropHint->addItem("Circle", WinToastTemplate::CropHint::Circle); + WinToast::instance()->setAppName(L"WinToastExample"); - WinToast::instance()->setAppUserModelId( - WinToast::configureAUMI(L"mohabouje", L"wintoast", L"wintoastexample", L"20161006")); + WinToast::instance()->setAppUserModelId(WinToast::configureAUMI(L"mohabouje", L"wintoast", L"wintoastexample", L"20161006")); if (!WinToast::instance()->initialize()) { qDebug() << "Error, your system in not compatible!"; } } -MainWindow::~MainWindow() -{ +MainWindow::~MainWindow() { delete ui; } -void MainWindow::on_imagePathSelector_clicked() -{ +void MainWindow::on_imagePathSelector_clicked() { const QString fileName = QFileDialog::getOpenFileName(this, "Select an image", QDir::currentPath(), "*.png"); - if (fileName.isEmpty()) + if (fileName.isEmpty()) { return; + } ui->imagePath->setText(QDir::toNativeSeparators(fileName)); - } class CustomHandler : public IWinToastHandler { @@ -58,35 +66,40 @@ class CustomHandler : public IWinToastHandler { } void toastDismissed(WinToastDismissalReason state) const { switch (state) { - case UserCanceled: - std::wcout << L"The user dismissed this toast" << std::endl; - break; - case ApplicationHidden: - std::wcout << L"The application hid the toast using ToastNotifier.hide()" << std::endl; - break; - case TimedOut: - std::wcout << L"The toast has timed out" << std::endl; - break; - default: - std::wcout << L"Toast not activated" << std::endl; - break; + case UserCanceled: + std::wcout << L"The user dismissed this toast" << std::endl; + break; + case ApplicationHidden: + std::wcout << L"The application hid the toast using ToastNotifier.hide()" << std::endl; + break; + case TimedOut: + std::wcout << L"The toast has timed out" << std::endl; + break; + default: + std::wcout << L"Toast not activated" << std::endl; + break; } } }; -void MainWindow::on_showToast_clicked() -{ - WinToastTemplate templ = WinToastTemplate(WinToastTemplate::ImageAndText04); - templ.setImagePath(ui->imagePath->text().toStdWString()); +void MainWindow::on_showToast_clicked() { + auto const type = static_cast(ui->toastType->currentData().toInt()); + WinToastTemplate templ = WinToastTemplate(type); + templ.setImagePath(ui->imagePath->text().toStdWString(), static_cast(ui->cropHint->currentData().toInt())); templ.setTextField(ui->firstLine->text().toStdWString(), WinToastTemplate::FirstLine); templ.setTextField(ui->secondLine->text().toStdWString(), WinToastTemplate::SecondLine); - templ.setTextField(ui->secondLine->text().toStdWString(), WinToastTemplate::ThirdLine); + templ.setTextField(ui->thirdLine->text().toStdWString(), WinToastTemplate::ThirdLine); templ.setExpiration(ui->spinBox->value() * 1000); templ.setAudioPath(static_cast(ui->audioSystemFile->currentData().toInt())); templ.setAudioOption(static_cast(ui->audioMode->currentData().toInt())); - if (ui->addYes->isChecked()) templ.addAction(L"Yes"); - if (ui->addNo->isChecked()) templ.addAction(L"No"); + if (ui->addYes->isChecked()) { + templ.addAction(L"Yes"); + } + + if (ui->addNo->isChecked()) { + templ.addAction(L"No"); + } if (WinToast::instance()->showToast(templ, new CustomHandler()) < 0) { QMessageBox::warning(this, "Error", "Could not launch your toast notification!"); diff --git a/example/qt-gui-example/WinToastExample/mainwindow.h b/examples/qt-gui-example/mainwindow.h similarity index 65% rename from example/qt-gui-example/WinToastExample/mainwindow.h rename to examples/qt-gui-example/mainwindow.h index 0b648f4..1d3d51b 100644 --- a/example/qt-gui-example/WinToastExample/mainwindow.h +++ b/examples/qt-gui-example/mainwindow.h @@ -4,15 +4,14 @@ #include namespace Ui { -class MainWindow; + class MainWindow; } -class MainWindow : public QMainWindow -{ +class MainWindow : public QMainWindow { Q_OBJECT public: - explicit MainWindow(QWidget *parent = 0); + explicit MainWindow(QWidget* parent = 0); ~MainWindow(); private slots: @@ -21,7 +20,7 @@ private slots: void on_showToast_clicked(); private: - Ui::MainWindow *ui; + Ui::MainWindow* ui; }; #endif // MAINWINDOW_H diff --git a/example/qt-gui-example/WinToastExample/mainwindow.ui b/examples/qt-gui-example/mainwindow.ui similarity index 84% rename from example/qt-gui-example/WinToastExample/mainwindow.ui rename to examples/qt-gui-example/mainwindow.ui index 8b9b62c..fc2b53a 100644 --- a/example/qt-gui-example/WinToastExample/mainwindow.ui +++ b/examples/qt-gui-example/mainwindow.ui @@ -7,7 +7,7 @@ 0 0 670 - 220 + 228 @@ -15,38 +15,95 @@ - - + + + + 10 + + + 2 + + - + First Line - - - - Second Line + + + + + + + + + + + 0 + 0 + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 0 + 0 + + + + + + + + + + + ... + + + + - + - Add Yes Action + Audio Mode - - + + - Audio System File + Add No Action + + + + + + + Expiration time (secs) - + @@ -90,97 +147,70 @@ + + + - + - Image Path + Second Line - - + + + + + - Add No Action + Add Yes Action - - - - - + + - Expiration time (secs) + Audio System File - + - - - - 10 - - - 2 + + + + Image Path - - - - - 0 - 0 - + + + + + + + Third Line - - - 0 - - - 0 - - - 0 - - - 0 - - - - - - 0 - 0 - - - - - - - - - - - ... - - - - - - + + + + Crop Hint + + - - + + - Audio Mode + Toast Type + + + diff --git a/src/wintoastlib.cpp b/src/wintoastlib.cpp index 9956240..5aedc2d 100644 --- a/src/wintoastlib.cpp +++ b/src/wintoastlib.cpp @@ -1159,7 +1159,7 @@ void WinToast::setError(_Out_opt_ WinToastError* error, _In_ WinToastError value } WinToastTemplate::WinToastTemplate(_In_ WinToastTemplateType type) : _type(type) { - constexpr static std::size_t TextFieldsCount[] = {1, 2, 2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2, 2, 3}; + constexpr static std::size_t TextFieldsCount[] = {1, 2, 2, 3, 1, 2, 2, 3}; _textFields = std::vector(TextFieldsCount[type], L""); } @@ -1169,7 +1169,10 @@ WinToastTemplate::~WinToastTemplate() { void WinToastTemplate::setTextField(_In_ std::wstring const& txt, _In_ WinToastTemplate::TextField pos) { auto const position = static_cast(pos); - assert(position < _textFields.size()); + if (position >= _textFields.size()) { + DEBUG_MSG("The selected template type supports only " << _textFields.size() << " text lines"); + return; + } _textFields[position] = txt; }