Compare commits
8 Commits
d1fb33c58e
...
old-graph-
| Author | SHA1 | Date | |
|---|---|---|---|
| 26839b82b7 | |||
| 6cd7d7db29 | |||
| 17c559a31f | |||
| e6ba60da89 | |||
| 8bf174d256 | |||
| 5f9f508581 | |||
| 3b6ecaa5e9 | |||
| de652bad32 |
2
.gitignore
vendored
@@ -10,3 +10,5 @@
|
|||||||
**/Makefile
|
**/Makefile
|
||||||
**/*.cbp
|
**/*.cbp
|
||||||
**/node-modules/
|
**/node-modules/
|
||||||
|
**/CMakeLists.txt.user
|
||||||
|
**/catch2/bin/
|
||||||
|
|||||||
11
README.md
@@ -2,15 +2,16 @@
|
|||||||
|
|
||||||
This repository is a collection of useful code snippets and configurations.
|
This repository is a collection of useful code snippets and configurations.
|
||||||
|
|
||||||
```
|
```bash
|
||||||
github.com/shaunrd0/klips/
|
shaunrd0/klips/
|
||||||
├── ansible # Ansible roles, playbooks, and examples
|
├── ansible # Ansible roles, playbooks, and examples
|
||||||
├── blockchain # Blockchain related project templates and examples
|
├── blockchain # Blockchain related project templates and examples
|
||||||
├── cpp # C++ programs, datastructures, and other examples
|
├── cpp # C++ programs, datastructures, and other examples
|
||||||
├── dotnet # .NET projects and examples
|
├── dotnet # .NET projects and examples
|
||||||
|
├── esp # ESP32 projects and examples
|
||||||
├── figlet # Figlet fonts I like :)
|
├── figlet # Figlet fonts I like :)
|
||||||
├── javascript # Javascript projects and examples
|
├── javascript # Javascript projects and examples
|
||||||
├── python # Python scripts or tools I've made
|
├── python # Python scripts and tools I've made
|
||||||
├── README.md
|
├── scripts # Bash scripts
|
||||||
└── scripts # Bash scripts
|
└── README.md
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
# Ansible
|
# ansible
|
||||||
|
|
||||||
A few simple roles / plays I've put together in learning how to use Ansible can be found under their corresponding directories.
|
A few simple roles / plays I've put together in learning how to use Ansible can be found under their corresponding directories.
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
# blockchain
|
||||||
|
|
||||||
A template project for getting started working on the Ethereum blockchain.
|
A template project for getting started working on the Ethereum blockchain.
|
||||||
This project comes with basic packages for compiling and deploying Solidity contracts with Truffle.
|
This project comes with basic packages for compiling and deploying Solidity contracts with Truffle.
|
||||||
|
|||||||
@@ -21,9 +21,11 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
|||||||
add_compile_options("-Wall")
|
add_compile_options("-Wall")
|
||||||
|
|
||||||
add_subdirectory(algorithms)
|
add_subdirectory(algorithms)
|
||||||
|
add_subdirectory(catch2)
|
||||||
add_subdirectory(cmake-example)
|
add_subdirectory(cmake-example)
|
||||||
add_subdirectory(cryptography)
|
add_subdirectory(cryptography)
|
||||||
add_subdirectory(datastructs)
|
add_subdirectory(datastructs)
|
||||||
add_subdirectory(graphics)
|
add_subdirectory(graphics)
|
||||||
add_subdirectory(multithreading)
|
add_subdirectory(multithreading)
|
||||||
add_subdirectory(patterns)
|
add_subdirectory(patterns)
|
||||||
|
add_subdirectory(qt)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
# Cpp
|
# cpp
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
shaunrd0/klips/cpp/
|
shaunrd0/klips/cpp/
|
||||||
@@ -9,6 +9,7 @@ shaunrd0/klips/cpp/
|
|||||||
├── graphics # Examples of graphics projects written in C++
|
├── graphics # Examples of graphics projects written in C++
|
||||||
├── multithreading # Basic multithreading examples in C++
|
├── multithreading # Basic multithreading examples in C++
|
||||||
├── patterns # Examples of various design patterns written in C++
|
├── patterns # Examples of various design patterns written in C++
|
||||||
|
├── qt # Qt project examples using C++
|
||||||
└── README.md
|
└── README.md
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
31
cpp/catch2/CMakeLists.txt
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
################################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Practice project for testing with catch2 framework ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.15)
|
||||||
|
|
||||||
|
project(
|
||||||
|
#[[NAME]] Catch2
|
||||||
|
VERSION 1.0
|
||||||
|
DESCRIPTION "Practice project for learning Catch2"
|
||||||
|
LANGUAGES CXX
|
||||||
|
)
|
||||||
|
|
||||||
|
add_compile_options(-Wall)
|
||||||
|
|
||||||
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
|
||||||
|
|
||||||
|
Include(FetchContent)
|
||||||
|
FetchContent_Declare(
|
||||||
|
Catch2
|
||||||
|
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
|
||||||
|
GIT_TAG v3.0.1
|
||||||
|
)
|
||||||
|
FetchContent_MakeAvailable(Catch2)
|
||||||
|
|
||||||
|
add_subdirectory(src)
|
||||||
|
add_subdirectory(test)
|
||||||
10
cpp/catch2/include/klips.hpp
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
|
||||||
|
#ifndef KLIPS_KLIPS_H
|
||||||
|
#define KLIPS_KLIPS_H
|
||||||
|
|
||||||
|
|
||||||
|
class klips { };
|
||||||
|
|
||||||
|
unsigned int factorial(unsigned int);
|
||||||
|
|
||||||
|
#endif // KLIPS_KLIPS_H
|
||||||
29
cpp/catch2/include/type_name.hpp
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
// Authored by 康桓瑋 on SO: https://stackoverflow.com/a/56766138
|
||||||
|
#ifndef CATCH2_TYPE_NAME_HPP
|
||||||
|
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
constexpr auto type_name() {
|
||||||
|
std::string_view name, prefix, suffix;
|
||||||
|
#ifdef __clang__
|
||||||
|
name = __PRETTY_FUNCTION__;
|
||||||
|
prefix = "auto type_name() [T = ";
|
||||||
|
suffix = "]";
|
||||||
|
#elif defined(__GNUC__)
|
||||||
|
name = __PRETTY_FUNCTION__;
|
||||||
|
prefix = "constexpr auto type_name() [with T = ";
|
||||||
|
suffix = "]";
|
||||||
|
#elif defined(_MSC_VER)
|
||||||
|
name = __FUNCSIG__;
|
||||||
|
prefix = "auto __cdecl type_name<";
|
||||||
|
suffix = ">(void)";
|
||||||
|
#endif
|
||||||
|
name.remove_prefix(prefix.size());
|
||||||
|
name.remove_suffix(suffix.size());
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define CATCH2_TYPE_NAME_HPP
|
||||||
|
|
||||||
|
#endif // CATCH2_TYPE_NAME_HPP
|
||||||
22
cpp/catch2/src/CMakeLists.txt
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
################################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Practice project for testing with catch2 framework ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.15)
|
||||||
|
|
||||||
|
project(
|
||||||
|
#[[NAME]] Catch2
|
||||||
|
VERSION 1.0
|
||||||
|
DESCRIPTION "Practice project for learning Catch2"
|
||||||
|
LANGUAGES CXX
|
||||||
|
)
|
||||||
|
|
||||||
|
add_compile_options(-Wall)
|
||||||
|
add_definitions("-std=c++17")
|
||||||
|
|
||||||
|
add_library(klips SHARED klips.cpp)
|
||||||
|
target_include_directories(klips PRIVATE ${CMAKE_SOURCE_DIR}/include)
|
||||||
4
cpp/catch2/src/klips.cpp
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
unsigned int factorial( unsigned int number ) {
|
||||||
|
return number <= 1 ? number : factorial(number-1)*number;
|
||||||
|
}
|
||||||
22
cpp/catch2/test/CMakeLists.txt
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
################################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Practice project for testing with catch2 framework ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.15)
|
||||||
|
|
||||||
|
project(
|
||||||
|
#[[NAME]] Catch2
|
||||||
|
VERSION 1.0
|
||||||
|
DESCRIPTION "Practice project for learning Catch2"
|
||||||
|
LANGUAGES CXX
|
||||||
|
)
|
||||||
|
|
||||||
|
add_compile_options(-Wall)
|
||||||
|
|
||||||
|
add_executable(test_klips test_klips.cpp)
|
||||||
|
target_link_libraries(test_klips PRIVATE Catch2::Catch2WithMain klips)
|
||||||
|
target_include_directories(test_klips PRIVATE ${CMAKE_SOURCE_DIR}/include)
|
||||||
147
cpp/catch2/test/test_klips.cpp
Normal file
@@ -0,0 +1,147 @@
|
|||||||
|
#define CATCH_CONFIG_MAIN
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "../bin/catch.hpp"
|
||||||
|
#include "klips.hpp"
|
||||||
|
#include "type_name.hpp"
|
||||||
|
|
||||||
|
#define TT() std::cout << "T = " << type_name<TestType>() << std::endl;
|
||||||
|
#define TD(x) \
|
||||||
|
std::cout << type_name<decltype(x)>() << " " << #x << " = " << x << std::endl;
|
||||||
|
#define T(x) std::cout << "T = " << type_name<x>() << std::endl;
|
||||||
|
|
||||||
|
TEST_CASE("factorials are computed", "[factorial]") {
|
||||||
|
REQUIRE(factorial(1) == 1);
|
||||||
|
REQUIRE(factorial(2) == 2);
|
||||||
|
REQUIRE(factorial(3) == 6);
|
||||||
|
REQUIRE(factorial(10) == 3628800);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Generators") {
|
||||||
|
auto i = GENERATE(1, 3, 5);
|
||||||
|
TD(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Generators 2") {
|
||||||
|
auto i = GENERATE(1, 2);
|
||||||
|
SECTION("one") {
|
||||||
|
auto j = GENERATE(-3, -2);
|
||||||
|
REQUIRE(j < i);
|
||||||
|
std::cout << "i = " << i << "; j = " << j << std::endl;
|
||||||
|
}
|
||||||
|
SECTION("two") {
|
||||||
|
auto k = GENERATE(4, 5, 6);
|
||||||
|
REQUIRE(i != k);
|
||||||
|
std::cout << "i = " << i << "; k = " << k << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Complex mix of sections and generates") {
|
||||||
|
auto i = GENERATE(1, 2);
|
||||||
|
SECTION("A") {
|
||||||
|
std::cout << "i = " << i << "; A passed" << std::endl;
|
||||||
|
SUCCEED("A");
|
||||||
|
}
|
||||||
|
std::cout << "left A\n";
|
||||||
|
auto j = GENERATE(3, 4);
|
||||||
|
std::cout << "i = " << i << "; j = " << j << std::endl;
|
||||||
|
SECTION("B") {
|
||||||
|
std::cout << "i = " << i << "; j = " << j << "; B passed;" << std::endl;
|
||||||
|
SUCCEED("B");
|
||||||
|
}
|
||||||
|
auto k = GENERATE(5, 6);
|
||||||
|
std::cout << "i = " << i << "; k = " << k << std::endl;
|
||||||
|
SUCCEED();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Test generaators 3", "[test]") { GENERATE(values({1, 2})); }
|
||||||
|
|
||||||
|
TEMPLATE_TEST_CASE("Testing template tests", "[test][template]", int8_t,
|
||||||
|
int16_t, int32_t, int64_t) {
|
||||||
|
TT();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T> struct Foo {
|
||||||
|
size_t size() { return 0; }
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T> struct Test {
|
||||||
|
T test() {
|
||||||
|
T x;
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
TEMPLATE_PRODUCT_TEST_CASE("A Template product test case",
|
||||||
|
"[template][product]", (std::vector, Test),
|
||||||
|
(int, float)) {
|
||||||
|
TT();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEMPLATE_PRODUCT_TEST_CASE("Product with differing arities",
|
||||||
|
"[template][product]", std::tuple,
|
||||||
|
(int, (int, double), (int, double, float))) {
|
||||||
|
TT();
|
||||||
|
}
|
||||||
|
|
||||||
|
using types = std::tuple<int8_t, int16_t, int32_t, int64_t>;
|
||||||
|
|
||||||
|
TEMPLATE_LIST_TEST_CASE("Testing template list tests", "[test][template][list]",
|
||||||
|
types) {
|
||||||
|
TT();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEMPLATE_TEST_CASE_SIG(
|
||||||
|
"TemplateTestSig: arrays can be created from NTTP arguments",
|
||||||
|
"[vector][template][nttp]", ((typename T, int V), T, V), (int, 5),
|
||||||
|
(float, 4), (std::string, 15), ((std::tuple<int, float>), 6)) {
|
||||||
|
T(T);
|
||||||
|
std::cout << "V = " << V;
|
||||||
|
std::array<T, V> v;
|
||||||
|
REQUIRE(v.size() > 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, size_t S> struct Bar {
|
||||||
|
size_t size() { return S; }
|
||||||
|
};
|
||||||
|
|
||||||
|
TEMPLATE_PRODUCT_TEST_CASE_SIG(
|
||||||
|
"A Template product test case with array signature",
|
||||||
|
"[template][product][nttp]", ((typename T, size_t S), T, S),
|
||||||
|
(std::array, Bar), ((int, 9), (float, 42))) {
|
||||||
|
TT();
|
||||||
|
TestType x;
|
||||||
|
REQUIRE(x.size() > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T> struct test_config_get {
|
||||||
|
template <bool must_find> void run() {
|
||||||
|
// Config c{};
|
||||||
|
// std::string key{"the_key"};
|
||||||
|
// std::string value{"the_value"};
|
||||||
|
// c.set(key, value);
|
||||||
|
if constexpr (must_find) {
|
||||||
|
std::cout << "Test 1 ran";
|
||||||
|
} else {
|
||||||
|
std::cout << "Test 2 ran";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
template <> template <bool must_find> void test_config_get<std::string>::run() {
|
||||||
|
if constexpr (must_find) {
|
||||||
|
std::cout << "Test 1 ran for strings";
|
||||||
|
} else {
|
||||||
|
std::cout << "Test 2 ran for strings";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEMPLATE_PRODUCT_TEST_CASE("Test", "[test]", test_config_get,
|
||||||
|
(int, std::string)) {
|
||||||
|
TT();
|
||||||
|
TestType t;
|
||||||
|
test_config_get<int> s;
|
||||||
|
s.template run<true>();
|
||||||
|
// TestType t;
|
||||||
|
// t.run<true>();
|
||||||
|
}
|
||||||
21
cpp/problems/CMakeLists.txt
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
################################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: A root project for C++ practice problems and solutions ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.16)
|
||||||
|
|
||||||
|
project(
|
||||||
|
#[[NAME]] Problems
|
||||||
|
VERSION 1.0
|
||||||
|
DESCRIPTION "Practice problems and solutions written in C++"
|
||||||
|
LANGUAGES CXX
|
||||||
|
)
|
||||||
|
|
||||||
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY, ${CMAKE_BINARY_DIR}/bin)
|
||||||
|
add_compile_options("-Wall")
|
||||||
|
|
||||||
|
add_subdirectory(graphs)
|
||||||
24
cpp/problems/README.md
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
# Problems
|
||||||
|
|
||||||
|
A collection of some example problems and solutions written in C++. Mostly these
|
||||||
|
are based off questions I found on [hackerrank](https://www.hackerrank.com),
|
||||||
|
[leetcode](https://leetcode.com/), [codility](https://www.codility.com/), or
|
||||||
|
similar programming practice platforms.
|
||||||
|
|
||||||
|
```
|
||||||
|
klips/cpp/problems
|
||||||
|
.
|
||||||
|
├── graphs # Graph implementations with related problems and solutions
|
||||||
|
└── README.md
|
||||||
|
```
|
||||||
|
|
||||||
|
We can build the examples with the following commands.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd /path/to/klips/cpp/problems/
|
||||||
|
mkdir build && cd build
|
||||||
|
cmake .. && cmake --build .
|
||||||
|
ls bin/
|
||||||
|
|
||||||
|
problems-graphs
|
||||||
|
```
|
||||||
21
cpp/problems/graphs/CMakeLists.txt
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
################################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Collection of problems and solutions to graph problems in C++ ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.16)
|
||||||
|
|
||||||
|
project(
|
||||||
|
#[[NAME]] ProblemsGraphs
|
||||||
|
VERSION 1.0
|
||||||
|
DESCRIPTION "Problems and solutions using graphs in C++"
|
||||||
|
LANGUAGES CXX
|
||||||
|
)
|
||||||
|
|
||||||
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||||
|
add_compile_options("-Wall")
|
||||||
|
|
||||||
|
add_executable(problems-graphs driver.cpp lib-graph.cpp lib-graph.hpp)
|
||||||
32
cpp/problems/graphs/driver.cpp
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Driver program solving various C++ graph problems ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "lib-graph.hpp"
|
||||||
|
|
||||||
|
int main(const int argc, const char * argv[]) {
|
||||||
|
Simple::Graph g({ {1,2,3}, {2,3,4} });
|
||||||
|
g.Print();
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
std::vector<int> graphA = {6,0,1,2,3,4,5};
|
||||||
|
std::vector<std::pair<int, int>> graphB = { {9, 2}, {2, 3}, {3, 1} };
|
||||||
|
std::vector<std::vector<int>> graphC = {{1}, {2, 3}, {3, 1, 0}};
|
||||||
|
g.ReadEdges(graphA);
|
||||||
|
g.Print();
|
||||||
|
std::cout << std::endl;
|
||||||
|
g.ReadEdges(graphB);
|
||||||
|
g.Print();
|
||||||
|
std::cout << std::endl;
|
||||||
|
g.ReadEdges(graphC);
|
||||||
|
g.Print();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
12
cpp/problems/graphs/lib-graph.cpp
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Graph implementations to solve various problems in C++ ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "lib-graph.hpp"
|
||||||
|
|
||||||
|
|
||||||
335
cpp/problems/graphs/lib-graph.hpp
Normal file
@@ -0,0 +1,335 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Graph implementations to solve various problems in C++ ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <map>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
#ifndef GRAPHS_LIB_GRAPH_HPP
|
||||||
|
#define GRAPHS_LIB_GRAPH_HPP
|
||||||
|
|
||||||
|
|
||||||
|
namespace Simple {
|
||||||
|
typedef int32_t Node;
|
||||||
|
typedef std::vector<Node> Nodes;
|
||||||
|
typedef std::vector<Nodes> Edges;
|
||||||
|
|
||||||
|
class Graph {
|
||||||
|
public:
|
||||||
|
Graph() = default;
|
||||||
|
explicit Graph(Edges e) : edges(std::move(e)) { }
|
||||||
|
|
||||||
|
void Print()
|
||||||
|
{
|
||||||
|
for (size_t node = 0; node < edges.size(); node++) {
|
||||||
|
for (const auto & to : edges[node]) {
|
||||||
|
std::cout << "(" << node << ")-----(" << to << ")" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Where graph[i] represents the connection between node i and graph[i]
|
||||||
|
// {1, 1, 2, 2}
|
||||||
|
void ReadEdges(const std::vector<int> & graph)
|
||||||
|
{
|
||||||
|
edges.clear();
|
||||||
|
edges.assign(graph.size(), {});
|
||||||
|
for (int i = 0; i < graph.size(); i++) {
|
||||||
|
if (i == graph[i]) continue;
|
||||||
|
edges[graph[i]].push_back(i);
|
||||||
|
edges[i].push_back(graph[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Where each graph[i] represents a single edge between two nodes
|
||||||
|
// { {1, 2}, {2, 3}, {3, 1} }
|
||||||
|
void ReadEdges(const std::vector<std::pair<int, int>> & graph)
|
||||||
|
{
|
||||||
|
edges.clear();
|
||||||
|
for (const auto & edge : graph) {
|
||||||
|
while (edges.size() <= edge.first || edges.size() <= edge.second) {
|
||||||
|
edges.emplace_back();
|
||||||
|
}
|
||||||
|
edges[edge.first].push_back(edge.second);
|
||||||
|
edges[edge.second].push_back(edge.first);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Where graph[node] holds all connected adjacent nodes
|
||||||
|
// {{1}, {2, 3}, {2, 1, 0}}
|
||||||
|
void ReadEdges(const std::vector<std::vector<int>> & graph)
|
||||||
|
{
|
||||||
|
edges.clear();
|
||||||
|
edges.assign(graph.size(), {});
|
||||||
|
for (size_t i = 0; i < graph.size(); i++) {
|
||||||
|
for (const auto & adj : graph[i]) {
|
||||||
|
if (adj == i) continue;
|
||||||
|
edges[i].push_back(adj);
|
||||||
|
edges[adj].push_back(int32_t(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Edges edges;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
namespace Object {
|
||||||
|
struct Node {
|
||||||
|
Node() : val(INT32_MIN), adj() { }
|
||||||
|
explicit Node(int32_t v) : val(v), adj() { }
|
||||||
|
Node(int32_t v, std::vector<int32_t> a) : val(v), adj(std::move(a)) { }
|
||||||
|
|
||||||
|
int32_t val;
|
||||||
|
std::vector<int32_t> adj;
|
||||||
|
|
||||||
|
// Define operator== for std::find; And comparisons between nodes
|
||||||
|
bool operator==(const Node & b) const { return this->val == b.val;}
|
||||||
|
bool operator!=(const Node & b) const { return this->val != b.val;}
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef std::vector<Node> Edges;
|
||||||
|
|
||||||
|
class Graph {
|
||||||
|
public:
|
||||||
|
Graph() = default;
|
||||||
|
explicit Graph(Edges e) : edges(std::move(e)) { }
|
||||||
|
|
||||||
|
void Print()
|
||||||
|
{
|
||||||
|
for (int32_t node = 0; node < edges.size(); node++) {
|
||||||
|
for (const auto & to : GetNode(node)->adj) {
|
||||||
|
std::cout << "(" << node << ")-----(" << to << ")" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Node * GetNode(const int32_t & nodeVal)
|
||||||
|
{
|
||||||
|
auto foundNode = std::find(edges.begin(), edges.end(), Node(nodeVal));
|
||||||
|
// [nodeVal](const Node & a)->bool { return a.val == nodeVal;});
|
||||||
|
if (foundNode == edges.end()) return nullptr; // Node does not exist
|
||||||
|
return &*foundNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node * CreateNode(const int32_t & nodeVal)
|
||||||
|
{
|
||||||
|
auto newNode = GetNode(nodeVal);
|
||||||
|
if (newNode != nullptr) return newNode;
|
||||||
|
// Create node if not found
|
||||||
|
edges.emplace_back(nodeVal); // Calls Node(int32_t) ctor
|
||||||
|
return &edges.back(); // Get ptr to our new node; Don't copy it
|
||||||
|
}
|
||||||
|
|
||||||
|
// Where graph[i] represents the connection between node i and graph[i]
|
||||||
|
// {1, 1, 2, 2}
|
||||||
|
void ReadEdges(const std::vector<int> & graph)
|
||||||
|
{
|
||||||
|
edges.clear();
|
||||||
|
for (int i = 0; i < graph.size(); i++) {
|
||||||
|
if (i == graph[i]) continue;
|
||||||
|
// Check if nodes already exist; Create them if not found
|
||||||
|
auto nodeFrom = CreateNode(graph[i]);
|
||||||
|
auto nodeTo = CreateNode(i);
|
||||||
|
// Push node ptr to adjacent list
|
||||||
|
nodeFrom->adj.push_back(nodeTo->val);
|
||||||
|
nodeTo->adj.push_back(nodeFrom->val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Where each graph[i] represents a single edge between two nodes
|
||||||
|
// { {1, 2}, {2, 3}, {3, 1} }
|
||||||
|
void ReadEdges(const std::vector<std::pair<int, int>> & graph)
|
||||||
|
{
|
||||||
|
edges.clear();
|
||||||
|
for (const auto & edge : graph) {
|
||||||
|
auto nodeFrom = CreateNode(edge.first);
|
||||||
|
auto nodeTo = CreateNode(edge.second);
|
||||||
|
nodeFrom->adj.push_back(nodeTo->val);
|
||||||
|
nodeTo->adj.push_back(nodeFrom->val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Where graph[node] holds all connected adjacent nodes
|
||||||
|
// {{1}, {2, 3}, {2, 1, 0}}
|
||||||
|
void ReadEdges(const std::vector<std::vector<int>> & graph)
|
||||||
|
{
|
||||||
|
edges.clear();
|
||||||
|
edges.assign(graph.size(), {});
|
||||||
|
for (size_t i = 0; i < graph.size(); i++) {
|
||||||
|
for (const auto & adj : graph[i]) {
|
||||||
|
if (adj == i) continue;
|
||||||
|
auto nodeFrom = CreateNode(int32_t(i));
|
||||||
|
auto nodeTo = CreateNode(adj);
|
||||||
|
nodeFrom->adj.push_back(nodeTo->val);
|
||||||
|
nodeTo->adj.push_back(nodeFrom->val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Edges edges;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace Weighted {
|
||||||
|
using Weight = int32_t;
|
||||||
|
using Adjacent = std::multimap<Weight, int32_t>;
|
||||||
|
struct Node {
|
||||||
|
Node() : val(INT32_MIN), adj() { }
|
||||||
|
explicit Node(int32_t v) : val(v), adj() { }
|
||||||
|
Node(int32_t v, Adjacent a) : val(v), adj(std::move(a)) { }
|
||||||
|
|
||||||
|
int32_t val;
|
||||||
|
Adjacent adj;
|
||||||
|
};
|
||||||
|
using Edge = std::pair<int, int>;
|
||||||
|
|
||||||
|
class Graph {
|
||||||
|
Graph() = default;
|
||||||
|
explicit Graph(Node n) : root(std::move(n)) { }
|
||||||
|
void ReadGraph(std::vector<std::vector<int>> nodeList)
|
||||||
|
{
|
||||||
|
// Read a 2D vector of nodes into a
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
Node root;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//namespace Object {
|
||||||
|
//struct Edge {
|
||||||
|
// friend struct Node;
|
||||||
|
// friend class Graph;
|
||||||
|
// Edge() : from(INT32_MIN), to(INT32_MIN) { }
|
||||||
|
// Edge(const int32_t & f, const int32_t & t) : from(f), to(t) { }
|
||||||
|
//
|
||||||
|
// private:
|
||||||
|
// int32_t from, to;
|
||||||
|
// };
|
||||||
|
// using Edges = std::vector<Edge>;
|
||||||
|
//
|
||||||
|
//// template <typename T, typename S>
|
||||||
|
//// struct Subscriptor {
|
||||||
|
//// T<int32_t, S> data;
|
||||||
|
//// };
|
||||||
|
//
|
||||||
|
// struct Node {
|
||||||
|
// using Adjacent = std::vector<Node *>;
|
||||||
|
// using NodeMap = std::unordered_map<int32_t, Node *>;
|
||||||
|
// friend class Graph; // Allow Graph to access protected / private members
|
||||||
|
// friend struct GraphData;
|
||||||
|
//
|
||||||
|
// // Struct is public by default
|
||||||
|
// Node () : val(0), adj() { }
|
||||||
|
// explicit Node(int32_t v) : val(v), adj() { }
|
||||||
|
// Node(int32_t v, Adjacent a) : val(v), adj(std::move(a)) { }
|
||||||
|
// inline void SetAdjacent(Adjacent a) { adj = std::move(a);}
|
||||||
|
// inline void SetVal(int32_t v) { val = v;}
|
||||||
|
// NodeMap GetNodeMap() {
|
||||||
|
// NodeMap result;
|
||||||
|
// BuildNodeMap(&result);
|
||||||
|
// return result;
|
||||||
|
// }
|
||||||
|
// void BuildNodeMap(NodeMap & nodeMap, Node * startNode=nullptr) {
|
||||||
|
// auto list = startNode == nullptr ? adj : startNode->adj;
|
||||||
|
// for (const auto & node : list) {
|
||||||
|
// if (!nodeMap.count(node->val)) {
|
||||||
|
// nodeMap[node->val] = node;
|
||||||
|
// BuildNodeMap(nodeMap, startNode);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// protected:
|
||||||
|
// int32_t val;
|
||||||
|
// Adjacent adj;
|
||||||
|
// Edges edges;
|
||||||
|
// };
|
||||||
|
//
|
||||||
|
// struct GraphData {
|
||||||
|
// GraphData() = default;
|
||||||
|
// explicit GraphData(const Node & n)
|
||||||
|
// {
|
||||||
|
// graphEdges n.edges;
|
||||||
|
// for (const auto & edge : n.edges) {
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // Implement subscript operators for unordered_multimap
|
||||||
|
// struct GraphEdges {
|
||||||
|
// Edges * operator[](int32_t nodeVal) {
|
||||||
|
// auto found = graphEdges.find(nodeVal):
|
||||||
|
// if (found != graphEdges.end()) {
|
||||||
|
// return &*found->second;
|
||||||
|
// }
|
||||||
|
// else {
|
||||||
|
// return nullptr;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// std::unordered_multimap<int32_t, Edges *> graphEdges;
|
||||||
|
// };
|
||||||
|
//
|
||||||
|
// // Implement subscript operators for unordered_map
|
||||||
|
// struct GraphNodes {
|
||||||
|
// Node * operator[](int32_t nodeVal) {
|
||||||
|
// auto found = graphNodes.find(nodeVal):
|
||||||
|
// if (found != graphNodes.end()) {
|
||||||
|
// return &*found->second;
|
||||||
|
// }
|
||||||
|
// else {
|
||||||
|
// return nullptr;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// std::unordered_map<int32_t, Node *> graphNodes;
|
||||||
|
// };
|
||||||
|
// // unordered_* provides O(1) access and search
|
||||||
|
// GraphEdges graphEdges;
|
||||||
|
// GraphNodes graphNodes;
|
||||||
|
// };
|
||||||
|
//
|
||||||
|
// class Graph {
|
||||||
|
// // Class is private by default
|
||||||
|
// Node root;
|
||||||
|
// std::unordered_map<int32_t, Node *> graphNodes;
|
||||||
|
// std::multimap<int32_t, Edges> graphEdges;
|
||||||
|
//// std::unordered_map<int32_t, Node *> graphNodes;
|
||||||
|
//// GraphData data; // Struct containing all graph edges / nodes
|
||||||
|
//
|
||||||
|
// public:
|
||||||
|
// Graph() = default;
|
||||||
|
// explicit Graph(Node r) : root(std::move(r)) { }
|
||||||
|
//
|
||||||
|
// inline const Node & GetRoot() const { return root;}
|
||||||
|
//// inline Node * GetNode(int32_t nodeVal) { return data.graphNodes[nodeVal];}
|
||||||
|
//// inline const Node * GetConstNode(int32_t nodeVal)
|
||||||
|
//// { return data.graphNodes[nodeVal];}
|
||||||
|
//
|
||||||
|
// const Node * DFS(int32_t nodeVal, int32_t startNode=INT32_MIN)
|
||||||
|
// {
|
||||||
|
// // If startNode was not set, begin search from root node
|
||||||
|
// startNode = startNode == INT32_MIN ? root.val : startNode;
|
||||||
|
// if (startNode == nodeVal) {
|
||||||
|
// return graphNodes[nodeVal];
|
||||||
|
// }
|
||||||
|
// for (const auto & edge : root.edges) {
|
||||||
|
// return DFS(nodeVal, edge.to);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// };
|
||||||
|
//}
|
||||||
|
|
||||||
|
#endif // GRAPHS_LIB_GRAPH_HPP
|
||||||
24
cpp/qt/CMakeLists.txt
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
################################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: A root project for practicing Qt 6 projects in C++ ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.15)
|
||||||
|
|
||||||
|
project(
|
||||||
|
#[[NAME]] Klips
|
||||||
|
VERSION 1.0
|
||||||
|
DESCRIPTION "A root project for several small Qt6 practice projects"
|
||||||
|
LANGUAGES CXX
|
||||||
|
)
|
||||||
|
|
||||||
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||||
|
add_compile_options("-Wall")
|
||||||
|
|
||||||
|
add_subdirectory(designer)
|
||||||
|
add_subdirectory(designer-plugin)
|
||||||
|
add_subdirectory(designer-plugin-collection)
|
||||||
|
add_subdirectory(slots)
|
||||||
32
cpp/qt/README.md
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
# Cpp
|
||||||
|
|
||||||
|
```bash
|
||||||
|
shaunrd0/klips/cpp/qt/
|
||||||
|
├── designer # Using Qt Designer to create application GUI
|
||||||
|
├── designer-plugin # Adding custom widgets as Qt Designer plugins
|
||||||
|
├── designer-plugin-collection # Adding a collection of widget plugins to Qt Designer
|
||||||
|
└── README.md
|
||||||
|
```
|
||||||
|
|
||||||
|
This directory contains a `CMakeLists.txt`, which can be selected to open as a
|
||||||
|
project within your preferred IDE. From there, all nested examples can be built,
|
||||||
|
debugged, and ran.
|
||||||
|
|
||||||
|
The plugin examples will need to be installed for Qt Designer integration to work.
|
||||||
|
On Linux, Qt Designer looks under `/some/path/to/Qt/Tools/QtCreator/lib/Qt/plugins/designer/`.
|
||||||
|
On windows or Mac, this path may differ. Unfortunately I don't have these machines to test for myself.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd klips/cpp/qt/designer-plugin-collection
|
||||||
|
mkdir build && cd build
|
||||||
|
cmake .. && cmake --build . --target install
|
||||||
|
```
|
||||||
|
|
||||||
|
After installing the plugin collection example above, we can open Qt Creator and navigate to the Designer.
|
||||||
|
We should see the custom collection is available within the Designer, and the contents of the widgets render correctly in the application view.
|
||||||
|
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|

|
||||||
110
cpp/qt/designer-plugin-collection/CMakeLists.txt
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
################################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Example of making a collection of widget plugins for Qt Designer ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.15)
|
||||||
|
|
||||||
|
project(
|
||||||
|
#[[NAME]] DesignerPluginCollection
|
||||||
|
VERSION 1.0
|
||||||
|
DESCRIPTION "Example of a widget plugin collection for Qt Designer"
|
||||||
|
LANGUAGES CXX
|
||||||
|
)
|
||||||
|
|
||||||
|
include(GenerateExportHeader)
|
||||||
|
|
||||||
|
add_compile_options(-Wall)
|
||||||
|
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||||
|
set(CMAKE_AUTOUIC ON)
|
||||||
|
set(CMAKE_AUTOMOC ON)
|
||||||
|
set(CMAKE_AUTORCC ON)
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
set(CMAKE_SHARED_MODULE_PREFIX "")
|
||||||
|
|
||||||
|
set(QT_DIR "$ENV{HOME}/Code/Clones/Qt/" CACHE PATH "Path to Qt6")
|
||||||
|
# Qt Designer will look in different locations if WIN / Unix.
|
||||||
|
# These paths are for using Qt Designer integrated within Qt Creator.
|
||||||
|
# Standalone Qt Designer may use different paths.
|
||||||
|
if (WIN32)
|
||||||
|
set(QT_PLUGIN_INSTALL_DIR
|
||||||
|
"${QT_DIR}/Tools/QtCreator/bin/plugins/designer"
|
||||||
|
)
|
||||||
|
# This path may be different on windows. I have not tested this.
|
||||||
|
set(QT_PLUGIN_LIBRARY_DIR
|
||||||
|
"${QT_DIR}/Tools/QtCreator/lib/Qt/lib"
|
||||||
|
)
|
||||||
|
else()
|
||||||
|
set(QT_PLUGIN_INSTALL_DIR
|
||||||
|
"${QT_DIR}/Tools/QtCreator/lib/Qt/plugins/designer"
|
||||||
|
)
|
||||||
|
set(QT_PLUGIN_LIBRARY_DIR
|
||||||
|
"${QT_DIR}/Tools/QtCreator/lib/Qt/lib"
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
# This should be set to your Qt6 installation directory.
|
||||||
|
set(QT_INSTALL_DIR "${QT_DIR}/6.3.1/gcc_64/" CACHE PATH "Path to Qt6 install")
|
||||||
|
list(APPEND CMAKE_PREFIX_PATH "${QT_INSTALL_DIR}")
|
||||||
|
|
||||||
|
find_package(Qt6 REQUIRED COMPONENTS UiPlugin Core Gui Widgets)
|
||||||
|
|
||||||
|
# Creating a library with two plugins for the collection.
|
||||||
|
qt_add_library(widget-plugin-library
|
||||||
|
textview.cpp textview.h
|
||||||
|
widgetplugin.cpp widgetplugin.h
|
||||||
|
)
|
||||||
|
target_sources(widget-plugin-library PRIVATE
|
||||||
|
textview.cpp textview.h
|
||||||
|
treeview.cpp treeview.h
|
||||||
|
widgetplugin.cpp widgetplugin.h
|
||||||
|
)
|
||||||
|
set_target_properties(widget-plugin-library PROPERTIES
|
||||||
|
WIN32_EXECUTABLE TRUE
|
||||||
|
MACOSX_BUNDLE TRUE
|
||||||
|
)
|
||||||
|
target_link_libraries(widget-plugin-library
|
||||||
|
PUBLIC Qt::UiPlugin Qt::Core Qt::Gui Qt::Widgets
|
||||||
|
)
|
||||||
|
|
||||||
|
install(TARGETS widget-plugin-library
|
||||||
|
RUNTIME DESTINATION "${QT_PLUGIN_LIBRARY_DIR}"
|
||||||
|
BUNDLE DESTINATION "${QT_PLUGIN_LIBRARY_DIR}"
|
||||||
|
LIBRARY DESTINATION "${QT_PLUGIN_LIBRARY_DIR}"
|
||||||
|
)
|
||||||
|
|
||||||
|
generate_export_header(widget-plugin-library)
|
||||||
|
|
||||||
|
# Creating the collection
|
||||||
|
|
||||||
|
qt_add_library(widget-plugin-collection
|
||||||
|
widgetplugincollection.cpp widgetplugincollection.h
|
||||||
|
)
|
||||||
|
target_link_libraries(widget-plugin-collection
|
||||||
|
Qt6::Widgets Qt6::UiPlugin widget-plugin-library
|
||||||
|
)
|
||||||
|
install(TARGETS widget-plugin-collection
|
||||||
|
RUNTIME DESTINATION "${QT_PLUGIN_INSTALL_DIR}"
|
||||||
|
BUNDLE DESTINATION "${QT_PLUGIN_INSTALL_DIR}"
|
||||||
|
LIBRARY DESTINATION "${QT_PLUGIN_INSTALL_DIR}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Application that will use the widget plugin
|
||||||
|
|
||||||
|
set(APP_DIR ${CMAKE_CURRENT_SOURCE_DIR})
|
||||||
|
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/app-dir.h.in"
|
||||||
|
"${CMAKE_CURRENT_SOURCE_DIR}/app-dir.h"
|
||||||
|
@ONLY
|
||||||
|
)
|
||||||
|
|
||||||
|
qt_add_executable(widget-app
|
||||||
|
widgetapp.cpp widgetapp.h widgetapp.ui
|
||||||
|
main.cpp
|
||||||
|
)
|
||||||
|
target_link_libraries(widget-app
|
||||||
|
PRIVATE Qt::Widgets widget-plugin-library
|
||||||
|
)
|
||||||
6
cpp/qt/designer-plugin-collection/app-dir.h
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#ifndef APPDIR_H_IN
|
||||||
|
#define APPDIR_H_IN
|
||||||
|
|
||||||
|
#define APP_DIR "/home/kapper/Code/klips/cpp/qt/designer-plugin-collection"
|
||||||
|
|
||||||
|
#endif // APPDIR_H_IN
|
||||||
6
cpp/qt/designer-plugin-collection/app-dir.h.in
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#ifndef APPDIR_H_IN
|
||||||
|
#define APPDIR_H_IN
|
||||||
|
|
||||||
|
#define APP_DIR "@APP_DIR@"
|
||||||
|
|
||||||
|
#endif // APPDIR_H_IN
|
||||||
18
cpp/qt/designer-plugin-collection/main.cpp
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Main driver fprogram for practice using signals and slots in Qt ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "widgetapp.h"
|
||||||
|
#include <QApplication>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
QApplication app(argc, argv);
|
||||||
|
WidgetApp widgetApp;
|
||||||
|
widgetApp.show();
|
||||||
|
return app.exec();
|
||||||
|
}
|
||||||
10
cpp/qt/designer-plugin-collection/textview.cpp
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Text viewer for signals and slots examples ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "textview.h"
|
||||||
44
cpp/qt/designer-plugin-collection/textview.h
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Text viewer for signals and slots examples ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef KLIPS_TEXTVIEW_H
|
||||||
|
#define KLIPS_TEXTVIEW_H
|
||||||
|
|
||||||
|
#include "widget-plugin-library_export.h"
|
||||||
|
#include <QPlainTextEdit>
|
||||||
|
|
||||||
|
class WIDGET_PLUGIN_LIBRARY_EXPORT TextView : public QPlainTextEdit {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit TextView(QWidget *parent = nullptr) : QPlainTextEdit(parent) {
|
||||||
|
appendPlainText("This is an example of a custom QTextView widget.");
|
||||||
|
}
|
||||||
|
|
||||||
|
~TextView() = default;
|
||||||
|
|
||||||
|
QString includeFile() const { return QStringLiteral("text-view.h"); };
|
||||||
|
public:
|
||||||
|
signals:
|
||||||
|
void sendTest();
|
||||||
|
|
||||||
|
private:
|
||||||
|
signals:
|
||||||
|
void sentTestPrivate();
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void test() { appendPlainText("Test signal received by TextView."); }
|
||||||
|
|
||||||
|
void testArgs(const QString &message) { appendPlainText(message); }
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void testPrivate() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // KLIPS_TEXTVIEW_H
|
||||||
10
cpp/qt/designer-plugin-collection/treeview.cpp
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Tree viewer ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "treeview.h"
|
||||||
36
cpp/qt/designer-plugin-collection/treeview.h
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Tree viewer ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef KLIPS_TREEVIEW_H
|
||||||
|
#define KLIPS_TREEVIEW_H
|
||||||
|
|
||||||
|
#include "widget-plugin-library_export.h"
|
||||||
|
#include <app-dir.h>
|
||||||
|
|
||||||
|
#include <QFileSystemModel>
|
||||||
|
#include <QSortFilterProxyModel>
|
||||||
|
#include <QTreeView>
|
||||||
|
|
||||||
|
class WIDGET_PLUGIN_LIBRARY_EXPORT TreeView : public QTreeView {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit TreeView(QWidget *parent = nullptr) : QTreeView(parent) {
|
||||||
|
QFileSystemModel *model = new QFileSystemModel;
|
||||||
|
QSortFilterProxyModel *proxy = new QSortFilterProxyModel(this);
|
||||||
|
QModelIndex rootModelIndex = model->setRootPath(APP_DIR);
|
||||||
|
proxy->setSourceModel(model);
|
||||||
|
setModel(proxy);
|
||||||
|
setRootIndex(proxy->mapFromSource(rootModelIndex));
|
||||||
|
}
|
||||||
|
|
||||||
|
~TreeView() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // KLIPS_TREEVIEW_H
|
||||||
15
cpp/qt/designer-plugin-collection/widgetapp.cpp
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Application that uses widget from the collection ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "widgetapp.h"
|
||||||
|
|
||||||
|
WidgetApp::WidgetApp(QWidget *parent) : QMainWindow(parent) {
|
||||||
|
m_widgetApp = new Ui::MainWindow;
|
||||||
|
m_widgetApp->setupUi(this);
|
||||||
|
}
|
||||||
37
cpp/qt/designer-plugin-collection/widgetapp.h
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Application that uses a custom Qt Designer widget plugin ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef KLIPS_WIDGETAPP_H
|
||||||
|
#define KLIPS_WIDGETAPP_H
|
||||||
|
|
||||||
|
#include <QDockWidget>
|
||||||
|
#include <QMainWindow>
|
||||||
|
#include <QPlainTextEdit>
|
||||||
|
#include <QVBoxLayout>
|
||||||
|
#include <QWidget>
|
||||||
|
|
||||||
|
#include "ui_widgetapp.h"
|
||||||
|
|
||||||
|
class WidgetApp : public QMainWindow {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit WidgetApp(QWidget *parent = nullptr);
|
||||||
|
~WidgetApp() = default;
|
||||||
|
|
||||||
|
Ui::MainWindow * m_widgetApp;
|
||||||
|
public:
|
||||||
|
signals:
|
||||||
|
void sendTest();
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void test(){};
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // KLIPS_WIDGETAPP_H
|
||||||
107
cpp/qt/designer-plugin-collection/widgetapp.ui
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<ui version="4.0">
|
||||||
|
<class>MainWindow</class>
|
||||||
|
<widget class="QMainWindow" name="MainWindow">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>800</width>
|
||||||
|
<height>600</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="windowTitle">
|
||||||
|
<string>MainWindow</string>
|
||||||
|
</property>
|
||||||
|
<widget class="QWidget" name="centralwidget">
|
||||||
|
<property name="enabled">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="sizePolicy">
|
||||||
|
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
|
||||||
|
<horstretch>0</horstretch>
|
||||||
|
<verstretch>0</verstretch>
|
||||||
|
</sizepolicy>
|
||||||
|
</property>
|
||||||
|
<layout class="QHBoxLayout" name="horizontalLayout_9">
|
||||||
|
<item>
|
||||||
|
<widget class="TextView" name="text-view_4" native="true"/>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
<widget class="QMenuBar" name="menubar">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>800</width>
|
||||||
|
<height>22</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<widget class="QMenu" name="menuFile">
|
||||||
|
<property name="title">
|
||||||
|
<string>File</string>
|
||||||
|
</property>
|
||||||
|
<addaction name="actionOption1"/>
|
||||||
|
<addaction name="actionOption2"/>
|
||||||
|
<addaction name="separator"/>
|
||||||
|
<addaction name="actionCategory_2"/>
|
||||||
|
</widget>
|
||||||
|
<widget class="QMenu" name="menuEdit">
|
||||||
|
<property name="title">
|
||||||
|
<string>Edit</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
<widget class="QMenu" name="menuHelp">
|
||||||
|
<property name="title">
|
||||||
|
<string>Help</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
<addaction name="menuFile"/>
|
||||||
|
<addaction name="menuEdit"/>
|
||||||
|
<addaction name="menuHelp"/>
|
||||||
|
</widget>
|
||||||
|
<widget class="QStatusBar" name="statusbar"/>
|
||||||
|
<widget class="QDockWidget" name="dockWidget_5">
|
||||||
|
<attribute name="dockWidgetArea">
|
||||||
|
<number>1</number>
|
||||||
|
</attribute>
|
||||||
|
<widget class="QWidget" name="dockWidgetContents_8">
|
||||||
|
<layout class="QHBoxLayout" name="horizontalLayout_10">
|
||||||
|
<item>
|
||||||
|
<widget class="TreeView" name="tree-view"/>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
</widget>
|
||||||
|
<action name="actionOption1">
|
||||||
|
<property name="text">
|
||||||
|
<string>Option1</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
|
<action name="actionOption2">
|
||||||
|
<property name="text">
|
||||||
|
<string>Option2</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
|
<action name="actionCategory_2">
|
||||||
|
<property name="text">
|
||||||
|
<string>Section 2</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
|
</widget>
|
||||||
|
<customwidgets>
|
||||||
|
<customwidget>
|
||||||
|
<class>TreeView</class>
|
||||||
|
<extends>QWidget</extends>
|
||||||
|
<header>treeview.h</header>
|
||||||
|
</customwidget>
|
||||||
|
<customwidget>
|
||||||
|
<class>TextView</class>
|
||||||
|
<extends>QWidget</extends>
|
||||||
|
<header>textview.h</header>
|
||||||
|
</customwidget>
|
||||||
|
</customwidgets>
|
||||||
|
<resources/>
|
||||||
|
<connections/>
|
||||||
|
</ui>
|
||||||
52
cpp/qt/designer-plugin-collection/widgetplugin.cpp
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Example of a generic Qt Designer widget plugin ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "widgetplugin.h"
|
||||||
|
#include "textview.h"
|
||||||
|
|
||||||
|
#include <QtPlugin>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
WidgetPlugin::WidgetPlugin(QString group, QString name,
|
||||||
|
WidgetPlugin::Factory factory)
|
||||||
|
: m_group(std::move(group)), m_name(std::move(name)),
|
||||||
|
m_includeFile(name + ".h"), m_factory(std::move(factory)) {}
|
||||||
|
|
||||||
|
WidgetPlugin::WidgetPlugin(QString group, QString name, QString include,
|
||||||
|
WidgetPlugin::Factory factory)
|
||||||
|
: m_group(std::move(group)), m_name(std::move(name)),
|
||||||
|
m_includeFile(std::move(include)), m_factory(std::move(factory)) {}
|
||||||
|
|
||||||
|
QString WidgetPlugin::toolTip() const { return {}; }
|
||||||
|
|
||||||
|
QString WidgetPlugin::whatsThis() const { return {}; }
|
||||||
|
|
||||||
|
QIcon WidgetPlugin::icon() const { return {}; }
|
||||||
|
|
||||||
|
bool WidgetPlugin::isContainer() const { return false; }
|
||||||
|
|
||||||
|
QString WidgetPlugin::group() const { return m_group; }
|
||||||
|
|
||||||
|
QString WidgetPlugin::name() const { return m_name; }
|
||||||
|
|
||||||
|
// TODO: The generated UI headers do not use this member appropriately.
|
||||||
|
QString WidgetPlugin::includeFile() const { return m_includeFile; }
|
||||||
|
|
||||||
|
QWidget *WidgetPlugin::createWidget(QWidget *parent) {
|
||||||
|
return m_factory(parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WidgetPlugin::isInitialized() const { return m_initialized; }
|
||||||
|
|
||||||
|
void WidgetPlugin::initialize(QDesignerFormEditorInterface *) {
|
||||||
|
if (m_initialized)
|
||||||
|
return;
|
||||||
|
|
||||||
|
m_initialized = true;
|
||||||
|
}
|
||||||
52
cpp/qt/designer-plugin-collection/widgetplugin.h
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Example Qt Designer widget plugin ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef KLIPS_WIDGETPLUGIN_H
|
||||||
|
#define KLIPS_WIDGETPLUGIN_H
|
||||||
|
|
||||||
|
#include <QDesignerCustomWidgetInterface>
|
||||||
|
|
||||||
|
class WidgetPlugin : public QObject, public QDesignerCustomWidgetInterface {
|
||||||
|
Q_OBJECT
|
||||||
|
Q_PLUGIN_METADATA(IID "com.Klips.WidgetPlugin")
|
||||||
|
Q_INTERFACES(QDesignerCustomWidgetInterface)
|
||||||
|
|
||||||
|
using Factory = std::function<QWidget *(QWidget *)>;
|
||||||
|
|
||||||
|
public:
|
||||||
|
WidgetPlugin(QString group, QString name, Factory factory);
|
||||||
|
WidgetPlugin(QString group, QString name, QString include, Factory factory);
|
||||||
|
|
||||||
|
explicit WidgetPlugin(QObject *parent = nullptr) : QObject(parent) {}
|
||||||
|
|
||||||
|
~WidgetPlugin() = default;
|
||||||
|
|
||||||
|
public:
|
||||||
|
[[nodiscard]] QString group() const override;
|
||||||
|
[[nodiscard]] QString name() const override;
|
||||||
|
[[nodiscard]] QString includeFile() const override;
|
||||||
|
QWidget *createWidget(QWidget *parent) override;
|
||||||
|
|
||||||
|
[[nodiscard]] QString toolTip() const override;
|
||||||
|
[[nodiscard]] QString whatsThis() const override;
|
||||||
|
[[nodiscard]] QIcon icon() const override;
|
||||||
|
[[nodiscard]] bool isContainer() const override;
|
||||||
|
[[nodiscard]] bool isInitialized() const override;
|
||||||
|
void initialize(QDesignerFormEditorInterface *core) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool m_initialized = false;
|
||||||
|
|
||||||
|
QString m_group;
|
||||||
|
QString m_name;
|
||||||
|
QString m_includeFile;
|
||||||
|
Factory m_factory;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // KLIPS_WIDGETPLUGIN_H
|
||||||
28
cpp/qt/designer-plugin-collection/widgetplugincollection.cpp
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Collection of widget plugins for Qt Designer ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "widgetplugincollection.h"
|
||||||
|
#include "textview.h"
|
||||||
|
#include "treeview.h"
|
||||||
|
#include "widgetplugin.h"
|
||||||
|
|
||||||
|
WidgetPluginCollection::WidgetPluginCollection(QObject *parent)
|
||||||
|
: QObject(parent), m_collectionName("Klips Widget Plugin Collection") {
|
||||||
|
m_collection = {
|
||||||
|
new WidgetPlugin(m_collectionName, "Text View Widget", "text-view.h",
|
||||||
|
[](QWidget *parent) { return new TextView(parent); }),
|
||||||
|
new WidgetPlugin(m_collectionName, "tree-view",
|
||||||
|
[](QWidget *parent) { return new TreeView(parent); }),
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
QList<QDesignerCustomWidgetInterface *>
|
||||||
|
WidgetPluginCollection::customWidgets() const {
|
||||||
|
return m_collection;
|
||||||
|
}
|
||||||
22
cpp/qt/designer-plugin-collection/widgetplugincollection.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
|
||||||
|
#ifndef DESIGNERPLUGINCOLLECTION_WIDGETPLUGINCOLLECTION_H
|
||||||
|
#define DESIGNERPLUGINCOLLECTION_WIDGETPLUGINCOLLECTION_H
|
||||||
|
|
||||||
|
#include <QDesignerCustomWidgetCollectionInterface>
|
||||||
|
|
||||||
|
class WidgetPluginCollection : public QObject,
|
||||||
|
public QDesignerCustomWidgetCollectionInterface {
|
||||||
|
Q_OBJECT
|
||||||
|
Q_PLUGIN_METADATA(IID "com.Klips.WidgetPluginCollection")
|
||||||
|
Q_INTERFACES(QDesignerCustomWidgetCollectionInterface)
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit WidgetPluginCollection(QObject *parent = nullptr);
|
||||||
|
[[nodiscard]] QList<QDesignerCustomWidgetInterface *> customWidgets() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
QList<QDesignerCustomWidgetInterface *> m_collection;
|
||||||
|
QString m_collectionName;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // DESIGNERPLUGINCOLLECTION_WIDGETPLUGINCOLLECTION_H
|
||||||
76
cpp/qt/designer-plugin/CMakeLists.txt
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
################################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Example of making widget plugins for Qt Designer ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.15)
|
||||||
|
|
||||||
|
project(
|
||||||
|
#[[NAME]] DesignerPlugin
|
||||||
|
VERSION 1.0
|
||||||
|
DESCRIPTION "Example of a widget plugin for Qt Designer"
|
||||||
|
LANGUAGES CXX
|
||||||
|
)
|
||||||
|
|
||||||
|
include(GenerateExportHeader)
|
||||||
|
|
||||||
|
add_compile_options(-Wall)
|
||||||
|
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||||
|
set(CMAKE_AUTOUIC ON)
|
||||||
|
set(CMAKE_AUTOMOC ON)
|
||||||
|
set(CMAKE_AUTORCC ON)
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
set(CMAKE_SHARED_MODULE_PREFIX "")
|
||||||
|
|
||||||
|
set(QT_DIR "$ENV{HOME}/Code/Clones/Qt/" CACHE PATH "Path to Qt6")
|
||||||
|
# Qt Designer will look in different locations if WIN / Unix.
|
||||||
|
if (WIN32)
|
||||||
|
set(QT_PLUGIN_INSTALL_DIR
|
||||||
|
"${QT_DIR}/Tools/QtCreator/bin/plugins/designer"
|
||||||
|
)
|
||||||
|
else()
|
||||||
|
set(QT_PLUGIN_INSTALL_DIR
|
||||||
|
"${QT_DIR}/Tools/QtCreator/lib/Qt/plugins/designer"
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
# This should be set to your Qt6 installation directory.
|
||||||
|
set(QT_INSTALL_DIR "${QT_DIR}/6.3.1/gcc_64/" CACHE PATH "Path to Qt6 install")
|
||||||
|
list(APPEND CMAKE_PREFIX_PATH "${QT_INSTALL_DIR}")
|
||||||
|
|
||||||
|
find_package(Qt6 REQUIRED COMPONENTS UiPlugin Core Gui Widgets)
|
||||||
|
|
||||||
|
# Creating the plugin
|
||||||
|
|
||||||
|
qt_add_library(widget-plugin)
|
||||||
|
target_sources(widget-plugin PRIVATE
|
||||||
|
text-view.cpp text-view.h
|
||||||
|
widget-plugin.cpp widget-plugin.h
|
||||||
|
)
|
||||||
|
set_target_properties(widget-plugin PROPERTIES
|
||||||
|
WIN32_EXECUTABLE TRUE
|
||||||
|
MACOSX_BUNDLE TRUE
|
||||||
|
)
|
||||||
|
target_link_libraries(widget-plugin PUBLIC
|
||||||
|
Qt::UiPlugin Qt::Core Qt::Gui Qt::Widgets
|
||||||
|
)
|
||||||
|
|
||||||
|
install(TARGETS widget-plugin
|
||||||
|
RUNTIME DESTINATION "${QT_PLUGIN_INSTALL_DIR}"
|
||||||
|
BUNDLE DESTINATION "${QT_PLUGIN_INSTALL_DIR}"
|
||||||
|
LIBRARY DESTINATION "${QT_PLUGIN_INSTALL_DIR}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Application that will use the widget plugin
|
||||||
|
|
||||||
|
qt_add_executable(widget-app
|
||||||
|
widget-app.cpp widget-app.h widget-app.ui
|
||||||
|
main.cpp
|
||||||
|
)
|
||||||
|
target_link_libraries(widget-app PRIVATE
|
||||||
|
Qt::Widgets widget-plugin
|
||||||
|
)
|
||||||
18
cpp/qt/designer-plugin/main.cpp
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Main driver fprogram for practice using signals and slots in Qt ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "widget-app.h"
|
||||||
|
#include <QApplication>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
QApplication app(argc, argv);
|
||||||
|
WidgetApp widgetApp;
|
||||||
|
widgetApp.show();
|
||||||
|
return app.exec();
|
||||||
|
}
|
||||||
10
cpp/qt/designer-plugin/text-view.cpp
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Text viewer for signals and slots examples ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "text-view.h"
|
||||||
40
cpp/qt/designer-plugin/text-view.h
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Text viewer for signals and slots examples ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef KLIPS_TEXTVIEW_H
|
||||||
|
#define KLIPS_TEXTVIEW_H
|
||||||
|
|
||||||
|
#include <QPlainTextEdit>
|
||||||
|
|
||||||
|
class TextView : public QPlainTextEdit {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit TextView(QWidget *parent = nullptr) : QPlainTextEdit(parent) { }
|
||||||
|
|
||||||
|
~TextView() = default;
|
||||||
|
|
||||||
|
public:
|
||||||
|
signals:
|
||||||
|
void sendTest();
|
||||||
|
|
||||||
|
private:
|
||||||
|
signals:
|
||||||
|
void sentTestPrivate();
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void test() { appendPlainText("Test signal received by TextView."); }
|
||||||
|
|
||||||
|
void testArgs(const QString &message) { appendPlainText(message); }
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void testPrivate() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // KLIPS_TEXTVIEW_H
|
||||||
16
cpp/qt/designer-plugin/widget-app.cpp
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Application that uses a custom Qt Designer widget plugin ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "widget-app.h"
|
||||||
|
#include "widget-plugin.h"
|
||||||
|
|
||||||
|
WidgetApp::WidgetApp(QWidget *parent) : QMainWindow(parent) {
|
||||||
|
m_ui = new Ui::MainWindow;
|
||||||
|
m_ui->setupUi(this);
|
||||||
|
}
|
||||||
38
cpp/qt/designer-plugin/widget-app.h
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Application that uses a custom Qt Designer widget plugin ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef KLIPS_WIDGETAPP_H
|
||||||
|
#define KLIPS_WIDGETAPP_H
|
||||||
|
|
||||||
|
#include <QDockWidget>
|
||||||
|
#include <QMainWindow>
|
||||||
|
#include <QPlainTextEdit>
|
||||||
|
#include <QVBoxLayout>
|
||||||
|
#include <QWidget>
|
||||||
|
|
||||||
|
#include "ui_widget-app.h"
|
||||||
|
|
||||||
|
class WidgetApp : public QMainWindow {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
WidgetApp(QWidget *parent = nullptr);
|
||||||
|
~WidgetApp() = default;
|
||||||
|
|
||||||
|
Ui::MainWindow *m_ui;
|
||||||
|
|
||||||
|
public:
|
||||||
|
signals:
|
||||||
|
void sendTest();
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void test(){};
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // KLIPS_WIDGETAPP_H
|
||||||
33
cpp/qt/designer-plugin/widget-app.ui
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<ui version="4.0">
|
||||||
|
<class>MainWindow</class>
|
||||||
|
<widget class="QMainWindow" name="MainWindow">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>800</width>
|
||||||
|
<height>600</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="windowTitle">
|
||||||
|
<string>MainWindow</string>
|
||||||
|
</property>
|
||||||
|
<widget class="QWidget" name="centralwidget">
|
||||||
|
<layout class="QHBoxLayout" name="horizontalLayout"/>
|
||||||
|
</widget>
|
||||||
|
<widget class="QMenuBar" name="menubar">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>800</width>
|
||||||
|
<height>22</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
<widget class="QStatusBar" name="statusbar"/>
|
||||||
|
</widget>
|
||||||
|
<resources/>
|
||||||
|
<connections/>
|
||||||
|
</ui>
|
||||||
67
cpp/qt/designer-plugin/widget-plugin.cpp
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Example Qt Designer widget plugin ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "widget-plugin.h"
|
||||||
|
#include "text-view.h"
|
||||||
|
|
||||||
|
#include <QtPlugin>
|
||||||
|
|
||||||
|
QString WidgetPlugin::toolTip() const
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
QString WidgetPlugin::whatsThis() const
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
QIcon WidgetPlugin::icon() const
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WidgetPlugin::isContainer() const
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString WidgetPlugin::group() const
|
||||||
|
{
|
||||||
|
return m_group;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString WidgetPlugin::name() const
|
||||||
|
{
|
||||||
|
return QStringLiteral("KlipsWidgetPlugin");
|
||||||
|
}
|
||||||
|
|
||||||
|
QString WidgetPlugin::includeFile() const
|
||||||
|
{
|
||||||
|
return QStringLiteral("widget-plugin.h");
|
||||||
|
}
|
||||||
|
|
||||||
|
QWidget *WidgetPlugin::createWidget(QWidget *parent)
|
||||||
|
{
|
||||||
|
return new TextView(parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WidgetPlugin::isInitialized() const
|
||||||
|
{
|
||||||
|
return m_initialized;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WidgetPlugin::initialize(QDesignerFormEditorInterface *)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (m_initialized)
|
||||||
|
return;
|
||||||
|
|
||||||
|
m_initialized = true;
|
||||||
|
}
|
||||||
45
cpp/qt/designer-plugin/widget-plugin.h
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Example Qt Designer widget plugin ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef KLIPS_WIDGETPLUGIN_H
|
||||||
|
#define KLIPS_WIDGETPLUGIN_H
|
||||||
|
|
||||||
|
#include <QtUiPlugin/QDesignerCustomWidgetInterface>
|
||||||
|
|
||||||
|
class WidgetPlugin : public QObject, public QDesignerCustomWidgetInterface {
|
||||||
|
Q_OBJECT
|
||||||
|
Q_PLUGIN_METADATA(IID "com.Klips.WidgetPlugin")
|
||||||
|
Q_INTERFACES(QDesignerCustomWidgetInterface)
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit WidgetPlugin(QObject *parent = nullptr) : QObject(parent) {}
|
||||||
|
|
||||||
|
~WidgetPlugin() = default;
|
||||||
|
public:
|
||||||
|
QString group() const override;
|
||||||
|
QString name() const override;
|
||||||
|
QString includeFile() const override;
|
||||||
|
QWidget *createWidget(QWidget *parent) override;
|
||||||
|
|
||||||
|
QString toolTip() const override;
|
||||||
|
QString whatsThis() const override;
|
||||||
|
QIcon icon() const override;
|
||||||
|
bool isContainer() const override;
|
||||||
|
bool isInitialized() const override;
|
||||||
|
void initialize(QDesignerFormEditorInterface *core) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool m_initialized = false;
|
||||||
|
|
||||||
|
QString m_group;
|
||||||
|
QString m_name;
|
||||||
|
QString m_includeFile;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // KLIPS_WIDGETPLUGIN_H
|
||||||
52
cpp/qt/designer/CMakeLists.txt
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
################################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Practice project for using Qt Designer with custom C++ widgets ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.15)
|
||||||
|
|
||||||
|
project(
|
||||||
|
#[[NAME]] Designer
|
||||||
|
VERSION 0.1
|
||||||
|
DESCRIPTION "Practice using Qt designer for desktop applications"
|
||||||
|
LANGUAGES CXX
|
||||||
|
)
|
||||||
|
|
||||||
|
add_compile_options(-Wall)
|
||||||
|
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||||
|
set(CMAKE_AUTOUIC ON)
|
||||||
|
set(CMAKE_AUTOMOC ON)
|
||||||
|
set(CMAKE_AUTORCC ON)
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
|
||||||
|
set(QT_DIR "$ENV{HOME}/Code/Clones/Qt/6.3.1/gcc_64/" CACHE PATH "Path to Qt6")
|
||||||
|
|
||||||
|
list(APPEND CMAKE_PREFIX_PATH "${QT_DIR}")
|
||||||
|
|
||||||
|
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
|
||||||
|
|
||||||
|
qt_add_executable(designer
|
||||||
|
designer.cpp designer.h designer.ui
|
||||||
|
debugconsole.h debugconsole.cpp debugconsole.ui
|
||||||
|
texteditor.h texteditor.cpp texteditor.ui
|
||||||
|
treeview.h treeview.cpp treeview.ui
|
||||||
|
main.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
set_target_properties(designer PROPERTIES
|
||||||
|
WIN32_EXECUTABLE TRUE
|
||||||
|
MACOSX_BUNDLE TRUE
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(designer PUBLIC Qt::Core Qt::Gui Qt::Widgets)
|
||||||
|
|
||||||
|
install(TARGETS designer
|
||||||
|
RUNTIME DESTINATION "install/designer"
|
||||||
|
BUNDLE DESTINATION "install/designer"
|
||||||
|
LIBRARY DESTINATION "install/designer"
|
||||||
|
)
|
||||||
18
cpp/qt/designer/debugconsole.cpp
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Debug console widget made in Qt Designer with C++ ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "debugconsole.h"
|
||||||
|
#include "ui_debugconsole.h"
|
||||||
|
|
||||||
|
DebugConsole::DebugConsole(QWidget *parent)
|
||||||
|
: QDockWidget(parent), ui(new Ui::DebugConsole) {
|
||||||
|
ui->setupUi(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
DebugConsole::~DebugConsole() { delete ui; }
|
||||||
29
cpp/qt/designer/debugconsole.h
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Debug console widget made in Qt Designer with C++ ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef DEBUGCONSOLE_H
|
||||||
|
#define DEBUGCONSOLE_H
|
||||||
|
|
||||||
|
#include "ui_debugconsole.h"
|
||||||
|
#include <QDockWidget>
|
||||||
|
|
||||||
|
class DebugConsole : public QDockWidget {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit DebugConsole(QWidget *parent = nullptr);
|
||||||
|
~DebugConsole();
|
||||||
|
|
||||||
|
inline QPlainTextEdit *getConsole() { return ui->plainTextEdit; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
Ui::DebugConsole *ui;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // DEBUGCONSOLE_H
|
||||||
26
cpp/qt/designer/debugconsole.ui
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<ui version="4.0">
|
||||||
|
<class>DebugConsole</class>
|
||||||
|
<widget class="QDockWidget" name="DebugConsole">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>400</width>
|
||||||
|
<height>300</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="windowTitle">
|
||||||
|
<string>DockWidget</string>
|
||||||
|
</property>
|
||||||
|
<widget class="QWidget" name="dockWidgetContents">
|
||||||
|
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||||
|
<item>
|
||||||
|
<widget class="QPlainTextEdit" name="plainTextEdit"/>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
</widget>
|
||||||
|
<resources/>
|
||||||
|
<connections/>
|
||||||
|
</ui>
|
||||||
30
cpp/qt/designer/designer.cpp
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: MainWindow for Qt Designer desktop application ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <QtWidgets>
|
||||||
|
|
||||||
|
#include "debugconsole.h"
|
||||||
|
#include "designer.h"
|
||||||
|
#include "texteditor.h"
|
||||||
|
#include "treeview.h"
|
||||||
|
|
||||||
|
Designer::Designer(QWidget *parent)
|
||||||
|
: QMainWindow(parent), designer_(new Ui::Designer) {
|
||||||
|
designer_->setupUi(this);
|
||||||
|
setCentralWidget(new TextEditor);
|
||||||
|
auto debugConsole = new DebugConsole;
|
||||||
|
debugConsole->getConsole()->appendPlainText("Test 1");
|
||||||
|
debugConsole->getConsole()->appendPlainText("Test 2");
|
||||||
|
|
||||||
|
auto treeView = new TreeView;
|
||||||
|
|
||||||
|
addDockWidget(Qt::DockWidgetArea::BottomDockWidgetArea, debugConsole);
|
||||||
|
addDockWidget(Qt::DockWidgetArea::BottomDockWidgetArea, treeView);
|
||||||
|
sendTest();
|
||||||
|
}
|
||||||
39
cpp/qt/designer/designer.h
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: MainWindow for Qt Designer desktop application ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef KLIPS_SLOTSAPP_H
|
||||||
|
#define KLIPS_SLOTSAPP_H
|
||||||
|
|
||||||
|
#include <QDockWidget>
|
||||||
|
#include <QMainWindow>
|
||||||
|
#include <QPlainTextEdit>
|
||||||
|
#include <QVBoxLayout>
|
||||||
|
#include <QWidget>
|
||||||
|
|
||||||
|
#include "ui_designer.h"
|
||||||
|
|
||||||
|
class Designer : public QMainWindow {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
Designer(QWidget *parent = nullptr);
|
||||||
|
~Designer() = default;
|
||||||
|
|
||||||
|
public:
|
||||||
|
signals:
|
||||||
|
void sendTest();
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void test(){};
|
||||||
|
|
||||||
|
private:
|
||||||
|
Ui::Designer *designer_;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // KLIPS_SLOTSAPP_H
|
||||||
37
cpp/qt/designer/designer.ui
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<ui version="4.0">
|
||||||
|
<class>Designer</class>
|
||||||
|
<widget class="QMainWindow" name="Designer">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>800</width>
|
||||||
|
<height>600</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="windowTitle">
|
||||||
|
<string>MainWindow</string>
|
||||||
|
</property>
|
||||||
|
<widget class="QWidget" name="centralwidget"/>
|
||||||
|
<widget class="QMenuBar" name="menubar">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>800</width>
|
||||||
|
<height>22</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<widget class="QMenu" name="menuFile">
|
||||||
|
<property name="title">
|
||||||
|
<string>File</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
<addaction name="menuFile"/>
|
||||||
|
</widget>
|
||||||
|
<widget class="QStatusBar" name="statusbar"/>
|
||||||
|
</widget>
|
||||||
|
<resources/>
|
||||||
|
<connections/>
|
||||||
|
</ui>
|
||||||
19
cpp/qt/designer/main.cpp
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Main driver program for Qt Designer desktop application.. ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "designer.h"
|
||||||
|
#include <QApplication>
|
||||||
|
|
||||||
|
int main(int argc, char * argv[]) {
|
||||||
|
QApplication app(argc, argv);
|
||||||
|
Designer qtk;
|
||||||
|
qtk.show();
|
||||||
|
// Show widget.
|
||||||
|
return app.exec();
|
||||||
|
}
|
||||||
23
cpp/qt/designer/texteditor.cpp
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Dockable text editor widget made in Qt Designer with C++ ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "texteditor.h"
|
||||||
|
#include "ui_texteditor.h"
|
||||||
|
|
||||||
|
TextEditor::TextEditor(QWidget *parent) :
|
||||||
|
QDockWidget(parent),
|
||||||
|
ui(new Ui::TextEditor)
|
||||||
|
{
|
||||||
|
ui->setupUi(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
TextEditor::~TextEditor()
|
||||||
|
{
|
||||||
|
delete ui;
|
||||||
|
}
|
||||||
31
cpp/qt/designer/texteditor.h
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Dockable text editor widget made in Qt Designer with C++ ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef TEXTEDITOR_H
|
||||||
|
#define TEXTEDITOR_H
|
||||||
|
|
||||||
|
#include <QDockWidget>
|
||||||
|
|
||||||
|
namespace Ui {
|
||||||
|
class TextEditor;
|
||||||
|
}
|
||||||
|
|
||||||
|
class TextEditor : public QDockWidget
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit TextEditor(QWidget *parent = nullptr);
|
||||||
|
~TextEditor();
|
||||||
|
|
||||||
|
private:
|
||||||
|
Ui::TextEditor *ui;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // TEXTEDITOR_H
|
||||||
17
cpp/qt/designer/texteditor.ui
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
<ui version="4.0">
|
||||||
|
<class>TextEditor</class>
|
||||||
|
<widget class="QDockWidget" name="TextEditor">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>400</width>
|
||||||
|
<height>300</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="windowTitle">
|
||||||
|
<string>DockWidget</string>
|
||||||
|
</property>
|
||||||
|
<widget class="QWidget" name="dockWidgetContents"/>
|
||||||
|
</widget>
|
||||||
|
</ui>
|
||||||
23
cpp/qt/designer/treeview.cpp
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Dockable tree view widget made in Qt Designer with C++ ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "treeview.h"
|
||||||
|
#include "ui_treeview.h"
|
||||||
|
|
||||||
|
TreeView::TreeView(QWidget *parent) :
|
||||||
|
QDockWidget(parent),
|
||||||
|
ui(new Ui::TreeView)
|
||||||
|
{
|
||||||
|
ui->setupUi(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
TreeView::~TreeView()
|
||||||
|
{
|
||||||
|
delete ui;
|
||||||
|
}
|
||||||
31
cpp/qt/designer/treeview.h
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Dockable tree view widget made in Qt Designer with C++ ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef TREEVIEW_H
|
||||||
|
#define TREEVIEW_H
|
||||||
|
|
||||||
|
#include <QDockWidget>
|
||||||
|
|
||||||
|
namespace Ui {
|
||||||
|
class TreeView;
|
||||||
|
}
|
||||||
|
|
||||||
|
class TreeView : public QDockWidget
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit TreeView(QWidget *parent = nullptr);
|
||||||
|
~TreeView();
|
||||||
|
|
||||||
|
private:
|
||||||
|
Ui::TreeView *ui;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // TREEVIEW_H
|
||||||
26
cpp/qt/designer/treeview.ui
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<ui version="4.0">
|
||||||
|
<class>TreeView</class>
|
||||||
|
<widget class="QDockWidget" name="TreeView">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>400</width>
|
||||||
|
<height>300</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="windowTitle">
|
||||||
|
<string>DockWidget</string>
|
||||||
|
</property>
|
||||||
|
<widget class="QWidget" name="dockWidgetContents">
|
||||||
|
<layout class="QVBoxLayout" name="verticalLayout">
|
||||||
|
<item>
|
||||||
|
<widget class="QTreeView" name="treeView"/>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
</widget>
|
||||||
|
<resources/>
|
||||||
|
<connections/>
|
||||||
|
</ui>
|
||||||
BIN
cpp/qt/plugin-render-view.png
Normal file
|
After Width: | Height: | Size: 83 KiB |
BIN
cpp/qt/side-panel-view.png
Normal file
|
After Width: | Height: | Size: 30 KiB |
50
cpp/qt/slots/CMakeLists.txt
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
################################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Practice project for using signals and slots in Qt ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.15)
|
||||||
|
|
||||||
|
project(
|
||||||
|
#[[NAME]] Slots
|
||||||
|
VERSION 1.0
|
||||||
|
DESCRIPTION "Practice using signals and slots in Qt 6"
|
||||||
|
LANGUAGES CXX
|
||||||
|
)
|
||||||
|
|
||||||
|
add_compile_options(-Wall)
|
||||||
|
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||||
|
set(CMAKE_AUTOUIC ON)
|
||||||
|
set(CMAKE_AUTOMOC ON)
|
||||||
|
set(CMAKE_AUTORCC ON)
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
|
||||||
|
set(QT_DIR "$ENV{HOME}/Code/Clones/Qt/6.3.1/gcc_64/" CACHE PATH "Path to Qt6")
|
||||||
|
|
||||||
|
list(APPEND CMAKE_PREFIX_PATH "${QT_DIR}")
|
||||||
|
|
||||||
|
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
|
||||||
|
|
||||||
|
qt_add_executable(slots
|
||||||
|
text-view.cpp text-view.h
|
||||||
|
slots-app.cpp slots-app.h
|
||||||
|
main.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
set_target_properties(slots PROPERTIES
|
||||||
|
WIN32_EXECUTABLE TRUE
|
||||||
|
MACOSX_BUNDLE TRUE
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(slots PUBLIC Qt::Core Qt::Gui Qt::Widgets)
|
||||||
|
|
||||||
|
install(TARGETS slots
|
||||||
|
RUNTIME DESTINATION "install/slots"
|
||||||
|
BUNDLE DESTINATION "install/slots"
|
||||||
|
LIBRARY DESTINATION "install/slots"
|
||||||
|
)
|
||||||
18
cpp/qt/slots/main.cpp
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Main driver fprogram for practice using signals and slots in Qt ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "slots-app.h"
|
||||||
|
#include <QApplication>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
QApplication app(argc, argv);
|
||||||
|
SlotsApp slotsApp;
|
||||||
|
slotsApp.show();
|
||||||
|
return app.exec();
|
||||||
|
}
|
||||||
42
cpp/qt/slots/slots-app.cpp
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: MainWindow application for practice using signals and slots in Qt ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "slots-app.h"
|
||||||
|
#include "text-view.h"
|
||||||
|
|
||||||
|
SlotsApp::SlotsApp(QWidget *parent) : QMainWindow(parent) {
|
||||||
|
|
||||||
|
auto textBox = new QPlainTextEdit;
|
||||||
|
auto textView = new TextView;
|
||||||
|
auto frame = new QFrame;
|
||||||
|
|
||||||
|
auto dock = new QDockWidget(this);
|
||||||
|
auto dockWidget = new QWidget;
|
||||||
|
auto dockWidgetLayout = new QVBoxLayout;
|
||||||
|
dockWidgetLayout->addWidget(frame);
|
||||||
|
dockWidgetLayout->addWidget(textBox);
|
||||||
|
dockWidget->setLayout(dockWidgetLayout);
|
||||||
|
dock->setWidget(dockWidget);
|
||||||
|
|
||||||
|
auto dock2 = new QDockWidget(this);
|
||||||
|
auto dockWidget2 = new QWidget;
|
||||||
|
auto dockWidgetLayout2 = new QVBoxLayout;
|
||||||
|
dockWidgetLayout2->addWidget(textView);
|
||||||
|
dockWidget2->setLayout(dockWidgetLayout2);
|
||||||
|
dock2->setWidget(dockWidget2);
|
||||||
|
|
||||||
|
textBox->setReadOnly(true);
|
||||||
|
textBox->appendPlainText("Test 1");
|
||||||
|
textBox->appendPlainText("Test 2");
|
||||||
|
|
||||||
|
addDockWidget(Qt::DockWidgetArea::BottomDockWidgetArea, dock);
|
||||||
|
addDockWidget(Qt::DockWidgetArea::BottomDockWidgetArea, dock2);
|
||||||
|
connect(this, &SlotsApp::sendTest, textView, &TextView::test);
|
||||||
|
sendTest();
|
||||||
|
}
|
||||||
34
cpp/qt/slots/slots-app.h
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: MainWindow application for practice using signals and slots in Qt ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef KLIPS_SLOTSAPP_H
|
||||||
|
#define KLIPS_SLOTSAPP_H
|
||||||
|
|
||||||
|
#include <QDockWidget>
|
||||||
|
#include <QMainWindow>
|
||||||
|
#include <QPlainTextEdit>
|
||||||
|
#include <QVBoxLayout>
|
||||||
|
#include <QWidget>
|
||||||
|
|
||||||
|
class SlotsApp : public QMainWindow {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
SlotsApp(QWidget *parent = nullptr);
|
||||||
|
~SlotsApp() = default;
|
||||||
|
|
||||||
|
public:
|
||||||
|
signals:
|
||||||
|
void sendTest();
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void test(){};
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // KLIPS_SLOTSAPP_H
|
||||||
10
cpp/qt/slots/text-view.cpp
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Text viewer for signals and slots examples ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "text-view.h"
|
||||||
40
cpp/qt/slots/text-view.h
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
/*##############################################################################
|
||||||
|
## Author: Shaun Reed ##
|
||||||
|
## Legal: All Content (c) 2022 Shaun Reed, all rights reserved ##
|
||||||
|
## About: Text viewer for signals and slots examples ##
|
||||||
|
## ##
|
||||||
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
|
################################################################################
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef KLIPS_TEXTVIEW_H
|
||||||
|
#define KLIPS_TEXTVIEW_H
|
||||||
|
|
||||||
|
#include <QPlainTextEdit>
|
||||||
|
|
||||||
|
class TextView : public QPlainTextEdit {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
TextView(QWidget *parent = nullptr) {}
|
||||||
|
|
||||||
|
~TextView() = default;
|
||||||
|
|
||||||
|
public:
|
||||||
|
signals:
|
||||||
|
void sendTest()QWidget;
|
||||||
|
|
||||||
|
private:
|
||||||
|
signals:
|
||||||
|
void sentTestPrivate();
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void test() { appendPlainText("Test signal received by TextView."); }
|
||||||
|
|
||||||
|
void testArgs(const QString &message) { appendPlainText(message); }
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void testPrivate() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // KLIPS_TEXTVIEW_H
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
# Dotnet
|
# dotnet
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
shaunrd0/klips/dotnet/
|
shaunrd0/klips/dotnet/
|
||||||
|
|||||||
38
esp/cpp/01_led-button/01_led-button.ino
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
// set pin numbers
|
||||||
|
const int buttonPin = 4; // the number of the pushbutton pin
|
||||||
|
const int ledPin = 5; // the number of the LED pin
|
||||||
|
|
||||||
|
// variable for storing the pushbutton status
|
||||||
|
int buttonState = 0;
|
||||||
|
|
||||||
|
bool light_on = false;
|
||||||
|
bool handled = false;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(115200);
|
||||||
|
// initialize the pushbutton pin as an input
|
||||||
|
pinMode(buttonPin, INPUT);
|
||||||
|
// initialize the LED pin as an output
|
||||||
|
pinMode(ledPin, OUTPUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
// read the state of the pushbutton value
|
||||||
|
buttonState = digitalRead(buttonPin);
|
||||||
|
// check if the pushbutton is pressed.
|
||||||
|
if (buttonState == HIGH) {
|
||||||
|
if (!handled) {
|
||||||
|
Serial.println("Handling button pressed.");
|
||||||
|
light_on = !light_on;
|
||||||
|
handled = true;
|
||||||
|
digitalWrite(ledPin, light_on ? HIGH : LOW);
|
||||||
|
}
|
||||||
|
// turn LED on
|
||||||
|
} else {
|
||||||
|
if (handled) {
|
||||||
|
Serial.println("Reset button handled state.");
|
||||||
|
handled = false;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
9
esp/cpp/01_led-button/README.md
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# 01_led-button
|
||||||
|
|
||||||
|
This example is largely adapted from those in [ESP32-basic-starter-kit.pdf](./ESP32-basic-starter-kit.pdf).
|
||||||
|
|
||||||
|
The APIs in the original examples paired with this PDF have changed, and I decided to do some different things with the code and/or circuits, but the original code can be [found here](https://www.dropbox.com/scl/fo/6znlij3eb23ih4jxcpv2w/AKvB1t9CCUgoVRVtGen8Yrw?rlkey=z84anl0hs940qf9fpl7l8q8q2&e=1&dl=0).
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
Simple LED controlled by an on-board button.
|
||||||
BIN
esp/cpp/01_led-button/schematic.png
Normal file
|
After Width: | Height: | Size: 304 KiB |
183
esp/cpp/02_led-button-web/02_led-button-web.ino
Normal file
@@ -0,0 +1,183 @@
|
|||||||
|
#include <WiFi.h>
|
||||||
|
|
||||||
|
//
|
||||||
|
// Web server project globals
|
||||||
|
|
||||||
|
// Replace with your network credentials
|
||||||
|
const char* ssid = "network-name";
|
||||||
|
const char* password = "password";
|
||||||
|
|
||||||
|
WiFiServer server(80);
|
||||||
|
|
||||||
|
// Variable to store the HTTP request
|
||||||
|
String header;
|
||||||
|
|
||||||
|
// Current time
|
||||||
|
unsigned long currentTime = millis();
|
||||||
|
// Previous time
|
||||||
|
unsigned long previousTime = 0;
|
||||||
|
// Define timeout time in milliseconds (example: 2000ms = 2s)
|
||||||
|
const long timeoutTime = 2000;
|
||||||
|
|
||||||
|
//
|
||||||
|
// Button / light project globals
|
||||||
|
|
||||||
|
// set pin numbers
|
||||||
|
const int buttonPin = 4; // the number of the pushbutton pin
|
||||||
|
const int ledPin = 5; // the number of the LED pin
|
||||||
|
|
||||||
|
// variable for storing the pushbutton status
|
||||||
|
int buttonState = 0;
|
||||||
|
|
||||||
|
bool light_on = false;
|
||||||
|
bool handled = false;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(115200);
|
||||||
|
// initialize the pushbutton pin as an input
|
||||||
|
pinMode(buttonPin, INPUT);
|
||||||
|
// initialize the LED pin as an output
|
||||||
|
pinMode(ledPin, OUTPUT);
|
||||||
|
|
||||||
|
// wait for the Serial Monitor to be open
|
||||||
|
while (!Serial) {
|
||||||
|
delay(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Connect to Wi-Fi network with SSID and password
|
||||||
|
Serial.print("Connecting to ");
|
||||||
|
Serial.println(ssid);
|
||||||
|
WiFi.begin(ssid, password);
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(500);
|
||||||
|
Serial.print(".");
|
||||||
|
}
|
||||||
|
// Print local IP address and start web server
|
||||||
|
Serial.println("");
|
||||||
|
Serial.println("WiFi connected.");
|
||||||
|
Serial.println("Web Server IP address: ");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
server.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handles the on-board button press.
|
||||||
|
void handleButtonPress() {
|
||||||
|
// read the state of the pushbutton value
|
||||||
|
buttonState = digitalRead(buttonPin);
|
||||||
|
// check if the pushbutton is pressed.
|
||||||
|
if (buttonState == HIGH) {
|
||||||
|
if (!handled) {
|
||||||
|
Serial.println("Handling button pressed.");
|
||||||
|
light_on = !light_on;
|
||||||
|
handled = true;
|
||||||
|
digitalWrite(ledPin, light_on ? HIGH : LOW);
|
||||||
|
}
|
||||||
|
// turn LED on
|
||||||
|
} else {
|
||||||
|
if (handled) {
|
||||||
|
Serial.println("Reset button handled state.");
|
||||||
|
handled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void displayPinState(WiFiClient client, int gpio_pin, bool state) {
|
||||||
|
// Display current state, and ON/OFF buttons for GPIO <gpio_pin>
|
||||||
|
client.println(String("<p>GPIO ") + String(gpio_pin) + String(" - Current State ") + String(state ? "ON" : "OFF") + String("</p>"));
|
||||||
|
// If the output state is off, it displays the ON button
|
||||||
|
if (state) {
|
||||||
|
client.println(String("<p><a href=\"/") + String(gpio_pin) + String("/off\"><button class=\"button button2\">OFF</button></a></p>"));
|
||||||
|
} else {
|
||||||
|
client.println(String("<p><a href=\"/") + String(gpio_pin) + String("/on\"><button class=\"button\">ON</button></a></p>"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleStateChange(WiFiClient client, int gpio_pin) {
|
||||||
|
if (header.indexOf(String("GET /") + String(gpio_pin) + String("/on")) >= 0) {
|
||||||
|
Serial.println(String("GPIO ") + String(gpio_pin) + String(" on"));
|
||||||
|
light_on = true;
|
||||||
|
digitalWrite(gpio_pin, HIGH);
|
||||||
|
} else if (header.indexOf(String("GET /") + String(gpio_pin) + String("/off")) >= 0) {
|
||||||
|
Serial.println(String("GPIO ") + String(gpio_pin) + String(" off"));
|
||||||
|
light_on = false;
|
||||||
|
digitalWrite(gpio_pin, LOW);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
WiFiClient client = server.available(); // Listen for incoming clients
|
||||||
|
|
||||||
|
handleButtonPress();
|
||||||
|
|
||||||
|
if (client) { // If a new client connects,
|
||||||
|
currentTime = millis();
|
||||||
|
previousTime = currentTime;
|
||||||
|
Serial.println("New Client."); // print a message out in the serial port
|
||||||
|
Serial.println("Web Server IP address: ");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
|
String currentLine = ""; // make a String to hold incoming data from the client
|
||||||
|
while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
|
||||||
|
// Also handle the button press in this loop to not miss events.
|
||||||
|
handleButtonPress();
|
||||||
|
|
||||||
|
currentTime = millis();
|
||||||
|
if (client.available()) { // if there's bytes to read from the client,
|
||||||
|
char c = client.read(); // read a byte, then
|
||||||
|
Serial.write(c); // print it out the serial monitor
|
||||||
|
header += c;
|
||||||
|
if (c == '\n') { // if the byte is a newline character
|
||||||
|
// if the current line is blank, you got two newline characters in a row.
|
||||||
|
// that's the end of the client HTTP request, so send a response:
|
||||||
|
if (currentLine.length() == 0) {
|
||||||
|
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
|
||||||
|
// and a content-type so the client knows what's coming, then a blank line:
|
||||||
|
client.println("HTTP/1.1 200 OK");
|
||||||
|
client.println("Content-type:text/html");
|
||||||
|
client.println("Connection: close");
|
||||||
|
client.println();
|
||||||
|
|
||||||
|
// turns the GPIOs on and off
|
||||||
|
// Display the HTML web page
|
||||||
|
client.println("<!DOCTYPE html><html>");
|
||||||
|
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
|
||||||
|
client.println("<link rel=\"icon\" href=\"data:,\">");
|
||||||
|
// CSS to style the on/off buttons
|
||||||
|
// Feel free to change the background-color and font-size attributes to fit your preferences
|
||||||
|
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
|
||||||
|
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
|
||||||
|
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
|
||||||
|
client.println(".button2 {background-color: #555555;}</style></head>");
|
||||||
|
|
||||||
|
//
|
||||||
|
// Web Page Heading
|
||||||
|
client.println("<body><h1>ESP32 Web Server</h1>");
|
||||||
|
//
|
||||||
|
// Web Page Body Contents
|
||||||
|
|
||||||
|
// Button Controls
|
||||||
|
displayPinState(client, ledPin, light_on);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Close Web Page Body / Heading
|
||||||
|
client.println("</body></html>");
|
||||||
|
// The HTTP response ends with another blank line
|
||||||
|
client.println();
|
||||||
|
// Break out of the while loop
|
||||||
|
break;
|
||||||
|
} else { // if you got a newline, then clear currentLine
|
||||||
|
currentLine = "";
|
||||||
|
}
|
||||||
|
} else if (c != '\r') { // if you got anything else but a carriage return character,
|
||||||
|
currentLine += c; // add it to the end of the currentLine
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Clear the header variable
|
||||||
|
header = "";
|
||||||
|
// Close the connection
|
||||||
|
client.stop();
|
||||||
|
Serial.println("Client disconnected.");
|
||||||
|
Serial.println("");
|
||||||
|
}
|
||||||
|
}
|
||||||
15
esp/cpp/02_led-button-web/README.md
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
# 02_led-button-web
|
||||||
|
|
||||||
|
This example is largely adapted from those in [ESP32-basic-starter-kit.pdf](./ESP32-basic-starter-kit.pdf).
|
||||||
|
|
||||||
|
The APIs in the original examples paired with this PDF have changed, and I decided to do some different things with the code and/or circuits, but the original code can be [found here](https://www.dropbox.com/scl/fo/6znlij3eb23ih4jxcpv2w/AKvB1t9CCUgoVRVtGen8Yrw?rlkey=z84anl0hs940qf9fpl7l8q8q2&e=1&dl=0).
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
This example uses the same schematic as [01_led-button](../01_led-button/).
|
||||||
|
|
||||||
|
The only difference is that you can also control the button through a web browser from any device connected to your local network.
|
||||||
|
|
||||||
|
This example was adapted from the example code in the [Arduino ESP32 API reference](https://docs.espressif.com/projects/arduino-esp32/en/latest/api/wifi.html#wi-fi-ap-example).
|
||||||
|
|
||||||
|

|
||||||
BIN
esp/cpp/02_led-button-web/schematic.png
Normal file
|
After Width: | Height: | Size: 304 KiB |
BIN
esp/cpp/02_led-button-web/screenshot.png
Normal file
|
After Width: | Height: | Size: 18 KiB |
161
esp/cpp/03_temp-humidity-web/03_temp-humidity-web.ino
Normal file
@@ -0,0 +1,161 @@
|
|||||||
|
// Import required libraries
|
||||||
|
|
||||||
|
#include "WiFi.h"
|
||||||
|
#include "ESPAsyncWebServer.h"
|
||||||
|
#include <Adafruit_Sensor.h>
|
||||||
|
#include <DHT.h>
|
||||||
|
|
||||||
|
// Replace with your network credentials
|
||||||
|
const char* ssid = "network-name";
|
||||||
|
const char* password = "password";
|
||||||
|
|
||||||
|
#define DHTPIN 4 // Digital pin connected to the DHT sensor
|
||||||
|
|
||||||
|
// Uncomment the type of sensor in use:
|
||||||
|
#define DHTTYPE DHT11 // DHT 11
|
||||||
|
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
|
||||||
|
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
|
||||||
|
|
||||||
|
DHT dht(DHTPIN, DHTTYPE);
|
||||||
|
|
||||||
|
// Create AsyncWebServer object on port 80
|
||||||
|
AsyncWebServer server(80);
|
||||||
|
|
||||||
|
String readDHTTemperature() {
|
||||||
|
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
|
||||||
|
// Read temperature as Celsius (the default)
|
||||||
|
float t = dht.readTemperature();
|
||||||
|
// Read temperature as Fahrenheit (isFahrenheit = true)
|
||||||
|
//float t = dht.readTemperature(true);
|
||||||
|
// Check if any reads failed and exit early (to try again).
|
||||||
|
if (isnan(t)) {
|
||||||
|
Serial.println("Failed to read from DHT sensor!");
|
||||||
|
return "--";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Serial.println(t);
|
||||||
|
return String(t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String readDHTHumidity() {
|
||||||
|
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
|
||||||
|
float h = dht.readHumidity();
|
||||||
|
if (isnan(h)) {
|
||||||
|
Serial.println("Failed to read from DHT sensor!");
|
||||||
|
return "--";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Serial.println(h);
|
||||||
|
return String(h);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const char index_html[] PROGMEM = R"rawliteral(
|
||||||
|
<!DOCTYPE HTML><html>
|
||||||
|
<head>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
|
||||||
|
<style>
|
||||||
|
html {
|
||||||
|
font-family: Arial;
|
||||||
|
display: inline-block;
|
||||||
|
margin: 0px auto;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
h2 { font-size: 3.0rem; }
|
||||||
|
p { font-size: 3.0rem; }
|
||||||
|
.units { font-size: 1.2rem; }
|
||||||
|
.dht-labels{
|
||||||
|
font-size: 1.5rem;
|
||||||
|
vertical-align:middle;
|
||||||
|
padding-bottom: 15px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h2>ESP32 DHT Server</h2>
|
||||||
|
<p>
|
||||||
|
<i class="fas fa-thermometer-half" style="color:#059e8a;"></i>
|
||||||
|
<span class="dht-labels">Temperature</span>
|
||||||
|
<span id="temperature">%TEMPERATURE%</span>
|
||||||
|
<sup class="units">°C</sup>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<i class="fas fa-tint" style="color:#00add6;"></i>
|
||||||
|
<span class="dht-labels">Humidity</span>
|
||||||
|
<span id="humidity">%HUMIDITY%</span>
|
||||||
|
<sup class="units">%</sup>
|
||||||
|
</p>
|
||||||
|
</body>
|
||||||
|
<script>
|
||||||
|
setInterval(function ( ) {
|
||||||
|
var xhttp = new XMLHttpRequest();
|
||||||
|
xhttp.onreadystatechange = function() {
|
||||||
|
if (this.readyState == 4 && this.status == 200) {
|
||||||
|
document.getElementById("temperature").innerHTML = this.responseText;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
xhttp.open("GET", "/temperature", true);
|
||||||
|
xhttp.send();
|
||||||
|
}, 10000 ) ;
|
||||||
|
|
||||||
|
setInterval(function ( ) {
|
||||||
|
var xhttp = new XMLHttpRequest();
|
||||||
|
xhttp.onreadystatechange = function() {
|
||||||
|
if (this.readyState == 4 && this.status == 200) {
|
||||||
|
document.getElementById("humidity").innerHTML = this.responseText;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
xhttp.open("GET", "/humidity", true);
|
||||||
|
xhttp.send();
|
||||||
|
}, 10000 ) ;
|
||||||
|
</script>
|
||||||
|
</html>)rawliteral";
|
||||||
|
|
||||||
|
// Replaces placeholder with DHT values
|
||||||
|
String processor(const String& var){
|
||||||
|
//Serial.println(var);
|
||||||
|
if(var == "TEMPERATURE"){
|
||||||
|
return readDHTTemperature();
|
||||||
|
}
|
||||||
|
else if(var == "HUMIDITY"){
|
||||||
|
return readDHTHumidity();
|
||||||
|
}
|
||||||
|
return String();
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup(){
|
||||||
|
// Serial port for debugging purposes
|
||||||
|
Serial.begin(115200);
|
||||||
|
|
||||||
|
dht.begin();
|
||||||
|
|
||||||
|
// Connect to Wi-Fi
|
||||||
|
WiFi.begin(ssid, password);
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(1000);
|
||||||
|
Serial.println("Connecting to WiFi..");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print ESP32 Local IP Address
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
|
// Route for root / web page
|
||||||
|
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||||
|
request->send_P(200, "text/html", index_html, processor);
|
||||||
|
});
|
||||||
|
server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||||
|
request->send_P(200, "text/plain", readDHTTemperature().c_str());
|
||||||
|
});
|
||||||
|
server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||||
|
request->send_P(200, "text/plain", readDHTHumidity().c_str());
|
||||||
|
});
|
||||||
|
|
||||||
|
// Start server
|
||||||
|
server.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop(){
|
||||||
|
|
||||||
|
}
|
||||||
60
esp/cpp/03_temp-humidity-web/README.md
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
# 03_temp-humidity-web
|
||||||
|
|
||||||
|
This example is largely adapted from those in [ESP32-basic-starter-kit.pdf](./ESP32-basic-starter-kit.pdf).
|
||||||
|
|
||||||
|
The APIs in the original examples paired with this PDF have changed, and I decided to do some different things with the code and/or circuits, but the original code can be [found here](https://www.dropbox.com/scl/fo/6znlij3eb23ih4jxcpv2w/AKvB1t9CCUgoVRVtGen8Yrw?rlkey=z84anl0hs940qf9fpl7l8q8q2&e=1&dl=0).
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
Temperature and humidity sensor served on a web page within the local network.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## Dependencies
|
||||||
|
|
||||||
|
You need to install a couple of libraries for this project:
|
||||||
|
|
||||||
|
* The DHT and the Adafruit Unified Sensor Driver libraries to read from the DHT sensor.
|
||||||
|
* ESPAsyncWebServer and Async TCP libraries to build the asynchronous web server.
|
||||||
|
|
||||||
|
The default Arduino IDE installation will store libraries at `~/Arduino/ibraries` - if this directory doesn't exist, create it.
|
||||||
|
|
||||||
|
### Installing DHT Sensor Library
|
||||||
|
|
||||||
|
To read from the DHT sensor using Arduino IDE, you need to install the [DHT sensor library](https://github.com/adafruit/DHT-sensor-library).
|
||||||
|
|
||||||
|
```bash
|
||||||
|
wget -O DHT_sensor.zip https://github.com/adafruit/DHT-sensor-library/archive/master.zip
|
||||||
|
unzip DHT_sensor.zip
|
||||||
|
mv DHT-sensor-library-master/ ~/Arduino/libraries/DHT_sensor
|
||||||
|
```
|
||||||
|
|
||||||
|
## Install Adafruit Unified Sensor Driver Library
|
||||||
|
|
||||||
|
You also need to install the [Adafruit Unified Sensor Driver library](https://github.com/adafruit/Adafruit_Sensor) to work with the DHT sensor.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
wget -O Adafruit_sensor.zip https://github.com/adafruit/Adafruit_Sensor/archive/master.zip
|
||||||
|
unzip Adafruit_sensor.zip
|
||||||
|
mv Adafruit_Sensor-master/ ~/Arduino/libraries/Adafruit_sensor
|
||||||
|
```
|
||||||
|
|
||||||
|
## Installing ESPAsyncWebServer Library
|
||||||
|
|
||||||
|
Follow the next steps to install the [ESPAsyncWebServer library](https://github.com/me-no-dev/ESPAsyncWebServer):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
wget -O ESPAsyncWebServer.zip https://github.com/me-no-dev/ESPAsyncWebServer/archive/master.zip
|
||||||
|
unzip ESPAsyncWebServer.zip
|
||||||
|
mv ESPAsyncWebServer-master/ ~/Arduino/libraries/ESPAsyncWebServer
|
||||||
|
```
|
||||||
|
|
||||||
|
## Installing Async TCP Library
|
||||||
|
|
||||||
|
The ESPAsyncWebServer library requires the [AsyncTCP library](https://github.com/me-no-dev/AsyncTCP) to work. Follow the next steps to install that library:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
wget -O AsyncTCP.zip https://github.com/me-no-dev/AsyncTCP/archive/master.zip
|
||||||
|
unzip AsyncTCP.zip
|
||||||
|
mv AsyncTCP-master/ ~/Arduino/libraries/AsyncTCP
|
||||||
|
```
|
||||||
BIN
esp/cpp/03_temp-humidity-web/schematic.png
Normal file
|
After Width: | Height: | Size: 238 KiB |
BIN
esp/cpp/03_temp-humidity-web/screenshot.png
Normal file
|
After Width: | Height: | Size: 33 KiB |
4
esp/cpp/04_esp-idf-arduino/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
build
|
||||||
|
managed_components
|
||||||
|
dependencies.lock
|
||||||
|
sdkconfig.old
|
||||||
18
esp/cpp/04_esp-idf-arduino/CMakeLists.txt
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# For more information about build system see
|
||||||
|
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
|
||||||
|
# The following five lines of boilerplate have to be in your project's
|
||||||
|
# CMakeLists in this exact order for cmake to work correctly
|
||||||
|
cmake_minimum_required(VERSION 3.26)
|
||||||
|
|
||||||
|
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||||
|
|
||||||
|
project(
|
||||||
|
#[[NAME]] esp-idf-arduino
|
||||||
|
VERSION 0.1
|
||||||
|
DESCRIPTION "Example ESP-IDF cmake project"
|
||||||
|
LANGUAGES CXX
|
||||||
|
)
|
||||||
|
|
||||||
|
# For writing pure cmake components, see the documentation
|
||||||
|
# https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/build-system.html#writing-pure-cmake-components
|
||||||
|
idf_build_set_property(COMPILE_OPTIONS "-Wno-error" APPEND)
|
||||||
81
esp/cpp/04_esp-idf-arduino/README.md
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
# 04_esp-idf-arduino
|
||||||
|
|
||||||
|
There is no schematic for this example, it simply prints some output to the serial monitor at 115200.
|
||||||
|
This is more of a build system example for untethering yourself from the Arduino IDE.
|
||||||
|
|
||||||
|
To build this example you can run the following commands.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# See Dependencies section below for instructions.
|
||||||
|
source ~/path/to/esp-idf/export.sh
|
||||||
|
|
||||||
|
mkdir build
|
||||||
|
cd build
|
||||||
|
cmake ..
|
||||||
|
make -j $(nproc)
|
||||||
|
|
||||||
|
# Flash the example to the ESP.
|
||||||
|
make flash
|
||||||
|
|
||||||
|
# Check the serial monitor for 'Hello world!' output.
|
||||||
|
idf.py monitor -b 115200
|
||||||
|
```
|
||||||
|
|
||||||
|
To flash to your ESP or access the `idf.py menuconfig` menu from the ESP-IDF you can run the same commands with `make`.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
make flash
|
||||||
|
make menuconfig
|
||||||
|
```
|
||||||
|
|
||||||
|
If Ninja is preferred:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
mkdir build
|
||||||
|
cd build
|
||||||
|
cmake .. -G Nina
|
||||||
|
ninja
|
||||||
|
```
|
||||||
|
|
||||||
|
## Dependencies
|
||||||
|
|
||||||
|
Install the [ESP-IDF](https://github.com/espressif/esp-idf?tab=readme-ov-file#setup-build-environment)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# https://docs.espressif.com/projects/esp-idf/en/v5.3.2/esp32/get-started/linux-macos-setup.html#for-linux-users
|
||||||
|
sudo apt-get install -y git wget flex bison gperf python3 python3-pip python3-venv cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0
|
||||||
|
|
||||||
|
git clone -b v5.3.2 --recursive https://github.com/espressif/esp-idf
|
||||||
|
cd esp-idf
|
||||||
|
./install.sh
|
||||||
|
. ./export.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
In CLion there is an official [Serial Monitor](https://plugins.jetbrains.com/plugin/8031-serial-port-monitor) plugin, or use ESP-IDF -
|
||||||
|
|
||||||
|
```bash
|
||||||
|
idf.py monitor -b 115200
|
||||||
|
```
|
||||||
|
|
||||||
|
## Starting Over
|
||||||
|
|
||||||
|
To set up this project from scratch the following commands were used
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# My example project directory
|
||||||
|
cd ~/Code/klips/esp/cpp/04_esp-idf-arduino
|
||||||
|
idf.py set-target esp32
|
||||||
|
idf.py add-dependency "espressif/arduino-esp32^3.1.1"
|
||||||
|
|
||||||
|
# Autostart Arduino for use of `loop()` and `setup()` functions
|
||||||
|
# You can also use the esp-idf `app_main()` function if preferred
|
||||||
|
# https://docs.espressif.com/projects/arduino-esp32/en/latest/esp-idf_component.html#configuration
|
||||||
|
# You can alternatively do this in the GUI tool `idf.py menuconfig`
|
||||||
|
echo "CONFIG_AUTOSTART_ARDUINO=y" >> sdkconfig
|
||||||
|
sed -i -e 's/CONFIG_FREERTOS_HZ=100/CONFIG_FREERTOS_HZ=1000/' sdkconfig
|
||||||
|
|
||||||
|
# Build the project
|
||||||
|
idf.py build
|
||||||
|
```
|
||||||
|
|
||||||
|
To set this project up in CLion, see [JetBrains documentation](https://www.jetbrains.com/help/clion/esp-idf.html#env-vars).
|
||||||
4
esp/cpp/04_esp-idf-arduino/main/CMakeLists.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
idf_component_register(
|
||||||
|
SRCS "main.cpp"
|
||||||
|
INCLUDE_DIRS "."
|
||||||
|
)
|
||||||
17
esp/cpp/04_esp-idf-arduino/main/idf_component.yml
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
## IDF Component Manager Manifest File
|
||||||
|
dependencies:
|
||||||
|
## Required IDF version
|
||||||
|
idf:
|
||||||
|
version: '>=4.1.0'
|
||||||
|
# # Put list of dependencies here
|
||||||
|
# # For components maintained by Espressif:
|
||||||
|
# component: "~1.0.0"
|
||||||
|
# # For 3rd party components:
|
||||||
|
# username/component: ">=1.0.0,<2.0.0"
|
||||||
|
# username2/component2:
|
||||||
|
# version: "~1.0.0"
|
||||||
|
# # For transient dependencies `public` flag can be set.
|
||||||
|
# # `public` flag doesn't have an effect dependencies of the `main` component.
|
||||||
|
# # All dependencies of `main` are public by default.
|
||||||
|
# public: true
|
||||||
|
espressif/arduino-esp32: ^3.1.1
|
||||||
10
esp/cpp/04_esp-idf-arduino/main/main.cpp
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#include "Arduino.h"
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(115200);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
Serial.println("Hello world!");
|
||||||
|
delay(1000);
|
||||||
|
}
|
||||||
2405
esp/cpp/04_esp-idf-arduino/sdkconfig
Normal file
4
esp/cpp/05_temp-humidity-web/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
build
|
||||||
|
managed_components
|
||||||
|
dependencies.lock
|
||||||
|
sdkconfig.old
|
||||||
18
esp/cpp/05_temp-humidity-web/CMakeLists.txt
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# For more information about build system see
|
||||||
|
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
|
||||||
|
# The following five lines of boilerplate have to be in your project's
|
||||||
|
# CMakeLists in this exact order for cmake to work correctly
|
||||||
|
cmake_minimum_required(VERSION 3.26)
|
||||||
|
|
||||||
|
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||||
|
|
||||||
|
project(
|
||||||
|
#[[NAME]] temp-humidity-web
|
||||||
|
VERSION 0.1
|
||||||
|
DESCRIPTION "Temperature and humidity from DHT sensor served on a web page"
|
||||||
|
LANGUAGES CXX
|
||||||
|
)
|
||||||
|
|
||||||
|
# For writing pure cmake components, see the documentation
|
||||||
|
# https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-guides/build-system.html#writing-pure-cmake-components
|
||||||
|
idf_build_set_property(COMPILE_OPTIONS "-Wno-error" APPEND)
|
||||||
27
esp/cpp/05_temp-humidity-web/README.md
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
# 05_temp-humidity-web
|
||||||
|
|
||||||
|
This is the same example in [03_temp-humidity-web](./../03_temp-humidity-web), ported to the cmake ESP-IDF build system.
|
||||||
|
|
||||||
|
For instructions on setting up the ESP-IDF see [04_-esp-idf-arduino](./../04_esp-idf-arduino)
|
||||||
|
|
||||||
|
This example is largely adapted from those in [ESP32-basic-starter-kit.pdf](./ESP32-basic-starter-kit.pdf).
|
||||||
|
|
||||||
|
The APIs in the original examples paired with this PDF have changed, and I decided to do some different things with the code and/or circuits, but the original code can be [found here](https://www.dropbox.com/scl/fo/6znlij3eb23ih4jxcpv2w/AKvB1t9CCUgoVRVtGen8Yrw?rlkey=z84anl0hs940qf9fpl7l8q8q2&e=1&dl=0).
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
Temperature and humidity sensor served on a web page within the local network.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
To build this example run the following commands.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
source ~/path/to/esp-idf/export.sh
|
||||||
|
|
||||||
|
mkdir build
|
||||||
|
cd build
|
||||||
|
cmake ..
|
||||||
|
make -j $(nproc)
|
||||||
|
make flash
|
||||||
|
```
|
||||||
4
esp/cpp/05_temp-humidity-web/main/CMakeLists.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
idf_component_register(
|
||||||
|
SRCS "main.cpp"
|
||||||
|
INCLUDE_DIRS "."
|
||||||
|
)
|
||||||
19
esp/cpp/05_temp-humidity-web/main/idf_component.yml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
## IDF Component Manager Manifest File
|
||||||
|
dependencies:
|
||||||
|
## Required IDF version
|
||||||
|
idf:
|
||||||
|
version: '>=4.1.0'
|
||||||
|
# # Put list of dependencies here
|
||||||
|
# # For components maintained by Espressif:
|
||||||
|
# component: "~1.0.0"
|
||||||
|
# # For 3rd party components:
|
||||||
|
# username/component: ">=1.0.0,<2.0.0"
|
||||||
|
# username2/component2:
|
||||||
|
# version: "~1.0.0"
|
||||||
|
# # For transient dependencies `public` flag can be set.
|
||||||
|
# # `public` flag doesn't have an effect dependencies of the `main` component.
|
||||||
|
# # All dependencies of `main` are public by default.
|
||||||
|
# public: true
|
||||||
|
espressif/arduino-esp32: ^3.1.1
|
||||||
|
zorxx/dht: ^1.0.1
|
||||||
|
esp32async/espasyncwebserver: ^3.7.0~1
|
||||||
189
esp/cpp/05_temp-humidity-web/main/main.cpp
Normal file
@@ -0,0 +1,189 @@
|
|||||||
|
#include "WiFi.h"
|
||||||
|
#include "ESPAsyncWebServer.h"
|
||||||
|
#include "main.h"
|
||||||
|
|
||||||
|
// Replace with your network credentials
|
||||||
|
const char *ssid = "wifi";
|
||||||
|
const char *password = "password";
|
||||||
|
|
||||||
|
#define DHTPIN GPIO_NUM_4 // Digital pin connected to the DHT sensor
|
||||||
|
|
||||||
|
// Uncomment the type of sensor in use:
|
||||||
|
#define DHTTYPE DHT_TYPE_DHT11 // DHT 11
|
||||||
|
//#define DHTTYPE DHT_TYPE_DHT22 // DHT 22 (AM2302)
|
||||||
|
//#define DHTTYPE DHT_TYPE_DHT21 // DHT 21 (AM2301)
|
||||||
|
|
||||||
|
float DHT::readTemperature(bool f)
|
||||||
|
{
|
||||||
|
float humidity = 0;
|
||||||
|
float temperature = 0;
|
||||||
|
esp_err_t result = dht_read_float_data(type_, gpio_, &humidity,
|
||||||
|
&temperature);
|
||||||
|
if (result == ESP_OK) {
|
||||||
|
ESP_LOGI("[DHT::readTemperature]", "Humidity: %.1f%% Temperature: %.1f°C",
|
||||||
|
humidity,
|
||||||
|
temperature);
|
||||||
|
} else {
|
||||||
|
ESP_LOGE("[DHT::readTemperature]", "Failed to read sensor data: %s",
|
||||||
|
esp_err_to_name(result));
|
||||||
|
}
|
||||||
|
return f ? (temperature * 1.8f) + 32 : temperature;
|
||||||
|
}
|
||||||
|
|
||||||
|
float DHT::readHumidity()
|
||||||
|
{
|
||||||
|
float humidity = 0;
|
||||||
|
float temperature = 0;
|
||||||
|
esp_err_t result = dht_read_float_data(type_, gpio_, &humidity,
|
||||||
|
&temperature);
|
||||||
|
if (result == ESP_OK) {
|
||||||
|
ESP_LOGI("[DHT::readTemperature]", "Humidity: %.1f%% Temperature: %.1f°C",
|
||||||
|
humidity,
|
||||||
|
temperature);
|
||||||
|
} else {
|
||||||
|
ESP_LOGE("[DHT::readTemperature]", "Failed to read sensor data: %s",
|
||||||
|
esp_err_to_name(result));
|
||||||
|
}
|
||||||
|
return humidity;
|
||||||
|
}
|
||||||
|
|
||||||
|
DHT dht(DHTPIN, DHTTYPE);
|
||||||
|
|
||||||
|
// Create AsyncWebServer object on port 80
|
||||||
|
AsyncWebServer server(80);
|
||||||
|
|
||||||
|
String readDHTTemperature()
|
||||||
|
{
|
||||||
|
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
|
||||||
|
float t = dht.readTemperature();
|
||||||
|
// Check if any reads failed and exit early (to try again).
|
||||||
|
if (isnan(t)) {
|
||||||
|
Serial.println("Failed to read from DHT sensor!");
|
||||||
|
return "--";
|
||||||
|
} else {
|
||||||
|
Serial.println(t);
|
||||||
|
return String(t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String readDHTHumidity()
|
||||||
|
{
|
||||||
|
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
|
||||||
|
float h = dht.readHumidity();
|
||||||
|
if (isnan(h)) {
|
||||||
|
Serial.println("Failed to read from DHT sensor!");
|
||||||
|
return "--";
|
||||||
|
} else {
|
||||||
|
Serial.println(h);
|
||||||
|
return String(h);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const char index_html[] PROGMEM = R"rawliteral(
|
||||||
|
<!DOCTYPE HTML><html>
|
||||||
|
<head>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
|
||||||
|
<style>
|
||||||
|
html {
|
||||||
|
font-family: Arial;
|
||||||
|
display: inline-block;
|
||||||
|
margin: 0px auto;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
h2 { font-size: 3.0rem; }
|
||||||
|
p { font-size: 3.0rem; }
|
||||||
|
.units { font-size: 1.2rem; }
|
||||||
|
.dht-labels{
|
||||||
|
font-size: 1.5rem;
|
||||||
|
vertical-align:middle;
|
||||||
|
padding-bottom: 15px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h2>ESP32 DHT Server</h2>
|
||||||
|
<p>
|
||||||
|
<i class="fas fa-thermometer-half" style="color:#059e8a;"></i>
|
||||||
|
<span class="dht-labels">Temperature</span>
|
||||||
|
<span id="temperature">%TEMPERATURE%</span>
|
||||||
|
<sup class="units">°C</sup>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<i class="fas fa-tint" style="color:#00add6;"></i>
|
||||||
|
<span class="dht-labels">Humidity</span>
|
||||||
|
<span id="humidity">%HUMIDITY%</span>
|
||||||
|
<sup class="units">%</sup>
|
||||||
|
</p>
|
||||||
|
</body>
|
||||||
|
<script>
|
||||||
|
setInterval(function ( ) {
|
||||||
|
var xhttp = new XMLHttpRequest();
|
||||||
|
xhttp.onreadystatechange = function() {
|
||||||
|
if (this.readyState == 4 && this.status == 200) {
|
||||||
|
document.getElementById("temperature").innerHTML = this.responseText;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
xhttp.open("GET", "/temperature", true);
|
||||||
|
xhttp.send();
|
||||||
|
}, 10000 ) ;
|
||||||
|
|
||||||
|
setInterval(function ( ) {
|
||||||
|
var xhttp = new XMLHttpRequest();
|
||||||
|
xhttp.onreadystatechange = function() {
|
||||||
|
if (this.readyState == 4 && this.status == 200) {
|
||||||
|
document.getElementById("humidity").innerHTML = this.responseText;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
xhttp.open("GET", "/humidity", true);
|
||||||
|
xhttp.send();
|
||||||
|
}, 10000 ) ;
|
||||||
|
</script>
|
||||||
|
</html>)rawliteral";
|
||||||
|
|
||||||
|
// Replaces placeholder with DHT values
|
||||||
|
String processor(const String &var)
|
||||||
|
{
|
||||||
|
//Serial.println(var);
|
||||||
|
if (var == "TEMPERATURE") {
|
||||||
|
return readDHTTemperature();
|
||||||
|
} else if (var == "HUMIDITY") {
|
||||||
|
return readDHTHumidity();
|
||||||
|
}
|
||||||
|
return String();
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup()
|
||||||
|
{
|
||||||
|
// Serial port for debugging purposes
|
||||||
|
Serial.begin(115200);
|
||||||
|
|
||||||
|
// Connect to Wi-Fi
|
||||||
|
WiFi.begin(ssid, password);
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(1000);
|
||||||
|
Serial.println("Connecting to WiFi..");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print ESP32 Local IP Address
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
|
// Route for root / web page
|
||||||
|
server.on("/", HTTP_GET, [ ](AsyncWebServerRequest *request) {
|
||||||
|
request->send(200, "text/html", index_html, processor);
|
||||||
|
});
|
||||||
|
server.on("/temperature", HTTP_GET, [ ](AsyncWebServerRequest *request) {
|
||||||
|
request->send(200, "text/plain", readDHTTemperature().c_str());
|
||||||
|
});
|
||||||
|
server.on("/humidity", HTTP_GET, [ ](AsyncWebServerRequest *request) {
|
||||||
|
request->send(200, "text/plain", readDHTHumidity().c_str());
|
||||||
|
});
|
||||||
|
|
||||||
|
// Start server
|
||||||
|
server.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
33
esp/cpp/05_temp-humidity-web/main/main.h
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#ifndef MAIN_H
|
||||||
|
#define MAIN_H
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include "dht.h"
|
||||||
|
|
||||||
|
class DHT {
|
||||||
|
public:
|
||||||
|
DHT(gpio_num_t gpio, dht_sensor_type_t type) :
|
||||||
|
gpio_(gpio), type_(type) { }
|
||||||
|
|
||||||
|
~DHT() = default;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read temperature from DHT sensor
|
||||||
|
*
|
||||||
|
* @param f True to return in Fahrenheit, False for Celsius.
|
||||||
|
*/
|
||||||
|
float readTemperature(bool f = true);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read humidity from DHT sensor.
|
||||||
|
*/
|
||||||
|
float readHumidity();
|
||||||
|
|
||||||
|
private:
|
||||||
|
gpio_num_t gpio_;
|
||||||
|
dht_sensor_type_t type_;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif // MAIN_H
|
||||||
BIN
esp/cpp/05_temp-humidity-web/schematic.png
Normal file
|
After Width: | Height: | Size: 238 KiB |
BIN
esp/cpp/05_temp-humidity-web/screenshot.png
Normal file
|
After Width: | Height: | Size: 33 KiB |
2415
esp/cpp/05_temp-humidity-web/sdkconfig
Normal file
BIN
esp/cpp/ESP32-basic-starter-kit.pdf
Normal file
BIN
esp/cpp/ESP32-dev-module.png
Normal file
|
After Width: | Height: | Size: 66 KiB |
20
esp/cpp/README.md
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# esp
|
||||||
|
|
||||||
|
```bash
|
||||||
|
shaunrd0/klips/esp/
|
||||||
|
├── 01_led-button # Simple LED circuit controlled by an on board button.
|
||||||
|
├── 02_led-button-web # LED controlled by a button or within a web browser.
|
||||||
|
├── 03_temp-humidity-web # Temperature and humidity sensor within a web browser.
|
||||||
|
├── 04_esp-idf-arduino # CMake example instead of Arduino IDE for ESP development.
|
||||||
|
├── 05_temp-humidity-web # Temperature and humidity sensor within a web browser.
|
||||||
|
├── ESP32-basic-starter-kit.pdf # PDF for tutorials in ESP32 starter kit.
|
||||||
|
├── ESP32-dev-module.png
|
||||||
|
└── README.md
|
||||||
|
```
|
||||||
|
|
||||||
|
Examples 1-3 are built using the Arduino IDE.
|
||||||
|
|
||||||
|
All examples after `04_esp-idf-arduino` are built with cmake and the [ESP-IDF](https://github.com/espressif/esp-idf).
|
||||||
|
|
||||||
|
[Arduino ESP32 GitHub](https://github.com/espressif/arduino-esp32) \
|
||||||
|
[Arduino ESP32 API reference](https://docs.espressif.com/projects/arduino-esp32/en/latest/libraries.html)
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
# Javascript
|
# javascript
|
||||||
|
|
||||||
This directory contains Javascript projects and examples that I've made.
|
This directory contains Javascript projects and examples that I've made.
|
||||||
|
|
||||||
```
|
```bash
|
||||||
github.com/shaunrd0/klips/javascript
|
shaunrd0/klips/javascript
|
||||||
├── webgl # Examples of using WebGL within JS
|
├── webgl # Examples of using WebGL within JS
|
||||||
└── README.md
|
└── README.md
|
||||||
```
|
```
|
||||||
|
|||||||