Introduction
This tutorial shows how to create a View for MIRA's Rich Client Platform. The view can e.g. be used within the miracenter. When we are finished, our view will show a message informing the user if the currently selected workbench part supports properties.
The full working example (source code, makefile) can be found in gui/examples/PropertyInfoView
.
Creating the View Class
First we will create our view class. We can do that using the mirawizard, or manually by deriving a class from mira::ViewPart:
#include <QtGui/QLabel>
using namespace mira;
class PropertyInfoView :
public ViewPart
{
("Name", "Properties")
("Description", "View for showing if a workbench part supports the property adapter"))
public:
PropertyInfoView() :
mPage(NULL) {}
protected:
virtual QWidget* createPartControl()
{
QLabel* label = new QLabel();
label->setAlignment(Qt::AlignCenter);
label->setText(tr("No properties available"));
mPage = label;
return mPage;
}
private:
QLabel* mPage;
};
Compiling and Using the View
In order to compile our new view, we need to provide a CMakeLists file.
# include our dependencies
# for base classes
MIRA_REQUIRE_PACKAGE(GUIRichClientPlatform)
# for additional headers (PropertyViewPage)
MIRA_REQUIRE_PACKAGE(GUIViews)
MIRA_ADD_LIBRARY(PropertyInfoView
SHARED
PACKAGE MyPackage
SOURCE
PropertyInfoView.C
LINK_LIBS
GUIRichClientPlatform
)
Afterwards, we can add it to the workbench of miracenter via "Windows->Add view". For many views, this will be sufficient and the rest of the development will be Qt-GUI related.
Reacting on the Activation of Other Windows
However, in this example we want to show our content depending on what other view or window is selected. Hence, we need to react whenever the user activates or deactivates another window within the workbench.
To get informed whenever the active/selected workbench part changes, we can implement the mira::IPartListener interface:
...
class PropertyInfoView : public ViewPart, public IPartListener
{
...
virtual ~PropertyInfoView()
{
getSite()->removePartListener(this);
}
virtual void editorActivated(IWorkbenchPart* part)
{
}
virtual void editorClosed(IWorkbenchPart* part)
{
mPage->setText(tr("No properties available"));
}
virtual void viewActivated(IWorkbenchPart* part)
{
}
virtual void viewClosed(IWorkbenchPart* part)
{
mPage->setText(tr("No properties available"));
}
protected:
virtual QWidget* createPartControl()
{
...
getSite()->addPartListener(this);
return mPage;
}
...
};
In the createPartControl() method, our class is registered as a part listener and will get informed whenever a view part or an editor part is activated or closed.
Adapters
What we need to do now is to check if the selected workbench part supports properties. The interaction of workbench parts is done via adapters. Parts supporting properties will be able to provide the property adapter in their getAdapter() method. The method takes the class of the desired adapter as parameter and will return a pointer to that adapter, if supported:
...
virtual void editorActivated(IWorkbenchPart* part)
{
viewActivated(part);
}
virtual void viewActivated(IWorkbenchPart* part)
{
if(part->getClass().isDerivedFrom(&WorkbenchPart::CLASS()))
{
Object* obj = part->getAdapter(PropertyViewPage::CLASS());
if(obj==NULL)
mPage->setText(tr("No properties available"));
else
mPage->setText(tr("Part supports property adapter"));
}
}
If we add our view to the MIRACenter workbench and click on a workbench part that has the property adapter implemented, we should see our "supports" message.