Skip to content

Commit cd790a1

Browse files
committed
Update githu
1 parent b543207 commit cd790a1

16 files changed

Lines changed: 410 additions & 3 deletions

.vscode/launch.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"name": "(gdb) Launch",
66
"type": "cppdbg",
77
"request": "launch",
8-
"program": "${workspaceFolder}/build/debug/bin/cpp_lab_project", // path to the executable
8+
// "program": "${workspaceFolder}/build/debug/bin/cpp_lab_project", // path to the executable
9+
"program": "${workspaceFolder}/build/debug/bin/mvc_ap", // path to the executable
910
"args": [],
1011
"stopAtEntry": true,
1112
"cwd": "${workspaceFolder}", // working directory

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_subdirectory(controller)
88
add_subdirectory(dp)
99
add_subdirectory(socket)
1010
add_subdirectory(excercise)
11+
add_subdirectory(ap)
1112

1213
# main application executable does NOT link to this library.
1314
add_subdirectory(leetcode)

src/ap/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Create the gtk4-setting interface library
2+
# Link against the libraries found for GTKMM
3+
add_library(gtk4-settings INTERFACE)
4+
target_link_libraries(gtk4-settings INTERFACE ${GTKMM_LIBRARIES})
5+
6+
# Include the directories where gtkmm.h resides
7+
target_include_directories(gtk4-settings INTERFACE ${GTKMM_INCLUDE_DIRS})
8+
9+
# Add compiler flags (defines, etc.) required by gtkmm
10+
target_compile_options(gtk4-settings INTERFACE ${GTKMM_CFLAGS_OTHER})
11+
12+
# ap executable
13+
add_executable(ap)
14+
target_sources(ap
15+
PRIVATE
16+
simple_ap.cpp
17+
)
18+
target_link_libraries(ap PRIVATE gtk4-settings)
19+
20+
add_subdirectory(mvc)
21+
# add_subdirectory(mvvm)

src/ap/README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ View ↔ ViewModel ↔ Model # view automatically update
2525
- **MVC (Model - View - Controller)** is an architectural pattern that separates the user interface (`View`) from the application logic and data (`Model`) using an intermediary component called the `Controller`.
2626
- **Components:**
2727
- **Model**: is responsible for managing and abstracting data sources (databases, APIs, etc.). The `Model` handles data retrieval, storage, and business logic.
28-
- **View**: displays the data provided by the `Model` and represents the user interface. The `View` is responsible only for presentation and does not contain business logic.
28+
- **View**: displays the data provided by the `Model` and represents the user interface. The `View` is responsible only for presentation and `does not contain business logic`.
2929
- **Controller**: acts as an intermediary between the `View` and the `Model`. It receives user input from the `View`, processes it, and interacts with the `Model` to update or retrieve data. The `Controller` then determines which `View` should display the result.
3030

3131
- **Workflow:**
@@ -41,4 +41,16 @@ View → Controller → Model
4141
4242
View # view update manually
4343
```
44-
### 3. Examples
44+
### 3. GTK4
45+
- [Refer](https://docs.gtk.org/gtk4/getting_started.html)
46+
47+
### 4. Examples
48+
### 4.1. simple_ap
49+
- Cos:
50+
- Quick, Simple
51+
- Pos:
52+
- Dependency: e.g. what happen when we delete Gtk::Label m_labelMonitorA;
53+
- Scalability:
54+
- Reusability:
55+
56+
### 4.2. mvc_ap

src/ap/mvc/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
add_executable(mvc_ap)
2+
3+
target_sources(mvc_ap
4+
PRIVATE
5+
mvc_ap.cpp
6+
model/SharedData.cpp
7+
view/EditorWidget.cpp
8+
view/DisplayWidget.cpp
9+
controller/Controller.cpp
10+
)
11+
12+
target_link_libraries(mvc_ap PRIVATE gtk4-settings)

src/ap/mvc/IObserver.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
class IObserver {
4+
public:
5+
virtual ~IObserver() = default;
6+
virtual void onDataChanged(const std::string& newData) = 0;
7+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "Controller.h"
2+
3+
Controller::Controller(std::shared_ptr<SharedData> model) : model_(model) {}
4+
5+
void Controller::updateRequest(const std::string& text) {
6+
if (text.empty())
7+
return;
8+
9+
// the Controller updates the Model when receiving new data from the View,
10+
// which then notifies all Views of the change
11+
model_->setData(text);
12+
}

src/ap/mvc/controller/Controller.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
#include <memory>
3+
#include "../model/SharedData.h"
4+
5+
class Controller {
6+
public:
7+
explicit Controller(std::shared_ptr<SharedData> model);
8+
9+
void updateRequest(const std::string& text);
10+
11+
private:
12+
std::shared_ptr<SharedData> model_;
13+
};

src/ap/mvc/model/SharedData.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "SharedData.h"
2+
3+
SharedData::SharedData() : data_{"Initial Data"} {}
4+
5+
void SharedData::setData(const std::string& data) {
6+
this->data_ = data;
7+
// Notify observers of the changed data
8+
notifyObservers();
9+
}
10+
11+
void SharedData::notifyObservers() {
12+
for (auto o : observers_) {
13+
o->onDataChanged(this->data_);
14+
}
15+
}
16+
void SharedData::addObserver(IObserver* obs) {
17+
if (obs != nullptr)
18+
observers_.push_back(obs);
19+
}
20+
21+
std::string SharedData::getData() const {
22+
return data_;
23+
}

src/ap/mvc/model/SharedData.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
#include <string>
3+
#include <vector>
4+
#include "../IObserver.h"
5+
6+
class SharedData {
7+
public:
8+
SharedData();
9+
10+
void setData(const std::string& data);
11+
std::string getData() const;
12+
13+
void addObserver(IObserver* obs);
14+
15+
private:
16+
void notifyObservers();
17+
18+
private:
19+
std::string data_;
20+
std::vector<IObserver*> observers_;
21+
};

0 commit comments

Comments
 (0)