cancel
Showing results for 
Search instead for 
Did you mean: 

How to know the active screen from model.cpp

COSEBE
Senior

I would like to check which screen is activated from model.cpp

I tried to use getCurrentScreen() in touchgfx::Application

https://www.touchgfx.com/documentation/html/classtouchgfx_1_1_application_af560e49a149b6d2d0ccffd4ca42e5991.html#af560e49a149b6d2d0ccffd4ca42e5991

But I am not able to use in model.cpp

Can you explain the philosophy and some code example ?

Regards,

Sébastien

6 REPLIES 6
Alexandre RENOUX
Principal

Hi Sébastien,

The best way to know the current screen is to implement a setter and a getter in the model. A presenter is created for each screen and everytime a screen is entered, its dedicated presenter is activated, meaning the activate() function is called.

A way to implement that would be as follows:

Model.cpp

#include <gui/model/Model.hpp>
#include <gui/model/ModelListener.hpp>
 
Model::Model() : modelListener(0), 
				currentScreenIndex (0)
{
}
 
void Model::tick()
{
}
 
void Model::saveCurrentScreenIndex(uint8_t cScreen)
{
	currentScreenIndex = sScreen;
}
 
uint8_t Model::getCurrentScreenIndex()
{
	return currentScreenIndex;
}

Declare the functions in Model.hpp file

ScreenPresenter.cpp

#include <gui/screen1_screen/Screen1View.hpp>
#include <gui/screen1_screen/Screen1Presenter.hpp>
 
Screen1Presenter::Screen1Presenter(Screen1View& v)
    : view(v)
{
}
 
void Screen1Presenter::activate()
{
  model ->saveCurrentScreenIndex(1);
}
 
void Screen1Presenter::deactivate()
{
 
}

COSEBE
Senior

Hi,

I did a similar thing, but I would use the functions proposed by touchgfx.

I think touchgfx is a very powerfull environnement and I would use the API proposed.

What about the activeScreen function ?

Alexandre RENOUX
Principal

Thank you for the compliment. I also think it is a very powerful environment.

However, it is meant to be implemented in a similar way as shown in my previous post in a Model Presenter View system. The Presenter tells the model (if necessary) that this presenter/screen is active, hence the generated activate() function in the Presenter.

getCurrentScreen() returns the pointer to a Screen object, not any string or int value to tell you which screen you are in.

The solution mentioned above is by far the easiest and it follows the philosophy behind the Model Presenter View system.

/Alexandre

Hello Alexandre, I tried this implementation but when the presenter goes inside:

 

void Screen1Presenter::activate()
{
  model ->saveCurrentScreenIndex(1);
}

 

The  ScreenIndex doesn't change.

I declared 

uint8_t currentScreenIndex ;
uint8_t sScreen;

as global variables just to see what was happening inside them and the values are always cero.

Any ideas?

Thanks in advance.

Hi @f.padoan ,

I think you have a typo.


Change sScreen to cScreen. and also your global variable.
void Model::saveCurrentScreenIndex(uint8_t cScreen)
{
currentScreenIndex = sScreen;
}

 

It should work then. 

How do you do this if you want to handle screen changing from a Widget?
I don't see how to get access to the model through the application() pointer (model is protected).