Skip to content

Commit 5e823e8

Browse files
committed
ENH: Add qSlicerSingletonViewFactory
The new view factory can be used by both C++ and Python classes to register any singleton QWidget by calling factory->setWidget(QWidget*) and factory->setTagName(std::string). Since the widget is a singleton, a factory can only be used to add a single view to a given layout. git-svn-id: http://svn.slicer.org/Slicer4/trunk@27945 3bd1e089-480b-0410-8dfb-8563597acbee
1 parent 781fcfe commit 5e823e8

File tree

4 files changed

+206
-1
lines changed

4 files changed

+206
-1
lines changed

Base/QTGUI/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ set(KIT_SRCS
102102
qSlicerWebWidget_p.h
103103
qSlicerWidget.cxx
104104
qSlicerWidget.h
105+
106+
qSlicerSingletonViewFactory.cxx
107+
qSlicerSingletonViewFactory.h
105108
)
106109

107110
if(Slicer_BUILD_EXTENSIONMANAGER_SUPPORT)
@@ -208,6 +211,8 @@ set(KIT_MOC_SRCS
208211
qSlicerWebWidget.h
209212
qSlicerWebWidget_p.h
210213
qSlicerWidget.h
214+
215+
qSlicerSingletonViewFactory.h
211216
)
212217

213218
if(Slicer_BUILD_EXTENSIONMANAGER_SUPPORT)
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*==============================================================================
2+
3+
Copyright (c) Laboratory for Percutaneous Surgery (PerkLab)
4+
Queen's University, Kingston, ON, Canada. All Rights Reserved.
5+
6+
See COPYRIGHT.txt
7+
or http://www.slicer.org/copyright/copyright.txt for details.
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
15+
This file was originally developed by Kyle Sunderland, PerkLab, Queen's University
16+
and was supported through CANARIE's Research Software Program, and Cancer
17+
Care Ontario.
18+
19+
==============================================================================*/
20+
21+
// QtGUI includes
22+
#include "qSlicerSingletonViewFactory.h"
23+
24+
#include <QDebug>
25+
#include <QMap>
26+
#include <QSharedPointer>
27+
#include <QWidget>
28+
29+
//-----------------------------------------------------------------------------
30+
class qSlicerSingletonViewFactoryPrivate
31+
{
32+
Q_DECLARE_PUBLIC(qSlicerSingletonViewFactory);
33+
public:
34+
qSlicerSingletonViewFactoryPrivate(qSlicerSingletonViewFactory& object);
35+
virtual ~qSlicerSingletonViewFactoryPrivate();
36+
37+
virtual void init();
38+
39+
QSharedPointer<QWidget> Widget;
40+
QString TagName;
41+
42+
protected:
43+
qSlicerSingletonViewFactory* q_ptr;
44+
};
45+
46+
//-----------------------------------------------------------------------------
47+
// qSlicerSingletonViewFactoryPrivate methods
48+
49+
qSlicerSingletonViewFactoryPrivate
50+
::qSlicerSingletonViewFactoryPrivate(qSlicerSingletonViewFactory& object)
51+
: q_ptr(&object)
52+
, Widget(NULL)
53+
{
54+
}
55+
56+
//-----------------------------------------------------------------------------
57+
qSlicerSingletonViewFactoryPrivate::~qSlicerSingletonViewFactoryPrivate()
58+
{
59+
}
60+
61+
//-----------------------------------------------------------------------------
62+
void qSlicerSingletonViewFactoryPrivate::init()
63+
{
64+
}
65+
66+
//-----------------------------------------------------------------------------
67+
// qSlicerSingletonViewFactory methods
68+
69+
//-----------------------------------------------------------------------------
70+
qSlicerSingletonViewFactory::qSlicerSingletonViewFactory(QObject* parent)
71+
: Superclass(parent)
72+
, d_ptr(new qSlicerSingletonViewFactoryPrivate(*this))
73+
{
74+
Q_D(qSlicerSingletonViewFactory);
75+
d->init();
76+
this->setUseCachedViews(false);
77+
}
78+
79+
//-----------------------------------------------------------------------------
80+
qSlicerSingletonViewFactory::~qSlicerSingletonViewFactory()
81+
{
82+
}
83+
84+
85+
//-----------------------------------------------------------------------------
86+
QWidget* qSlicerSingletonViewFactory::widget()
87+
{
88+
Q_D(qSlicerSingletonViewFactory);
89+
return d->Widget.data();
90+
}
91+
92+
//-----------------------------------------------------------------------------
93+
void qSlicerSingletonViewFactory::setWidget(QWidget* widget)
94+
{
95+
Q_D(qSlicerSingletonViewFactory);
96+
d->Widget = QSharedPointer<QWidget>(widget);
97+
}
98+
99+
//-----------------------------------------------------------------------------
100+
QString qSlicerSingletonViewFactory::tagName()
101+
{
102+
Q_D(qSlicerSingletonViewFactory);
103+
return d->TagName;
104+
}
105+
106+
//-----------------------------------------------------------------------------
107+
void qSlicerSingletonViewFactory::setTagName(QString tagName)
108+
{
109+
Q_D(qSlicerSingletonViewFactory);
110+
d->TagName = tagName;
111+
}
112+
113+
//-----------------------------------------------------------------------------
114+
QStringList qSlicerSingletonViewFactory::supportedElementNames() const
115+
{
116+
Q_D(const qSlicerSingletonViewFactory);
117+
return QStringList() << d->TagName;
118+
}
119+
120+
//---------------------------------------------------------------------------
121+
QWidget* qSlicerSingletonViewFactory::createViewFromXML(QDomElement layoutElement)
122+
{
123+
Q_D(qSlicerSingletonViewFactory);
124+
if (this->widget()->isVisible())
125+
{
126+
qCritical() << "qSlicerSingletonViewFactory::createViewFromXML - Widget for view \"" << d->TagName << "\" is already in use within the current layout!";
127+
}
128+
129+
return this->widget();
130+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*==============================================================================
2+
3+
Copyright (c) Laboratory for Percutaneous Surgery (PerkLab)
4+
Queen's University, Kingston, ON, Canada. All Rights Reserved.
5+
6+
See COPYRIGHT.txt
7+
or http://www.slicer.org/copyright/copyright.txt for details.
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
15+
This file was originally developed by Kyle Sunderland, PerkLab, Queen's University
16+
and was supported through CANARIE's Research Software Program, and Cancer
17+
Care Ontario.
18+
19+
==============================================================================*/
20+
21+
#ifndef qSlicerSingletonViewFactory_h
22+
#define qSlicerSingletonViewFactory_h
23+
24+
// CTK includes
25+
#include "ctkLayoutViewFactory.h"
26+
27+
// QtGUI includes
28+
#include "qSlicerBaseQTGUIExport.h"
29+
30+
class ctkDICOMBrowser;
31+
class qSlicerSingletonViewFactoryPrivate;
32+
33+
/// This class provides an interface for C++ and Python classes to register a singleton view replacement widget.
34+
/// New view widgets can be registered by registering a new qSlicerSingletonViewFactory, and then setting the widget and tag using setWidget(QWidget*) and
35+
/// setTagName(QString).
36+
/// The factory will be responsible for deleting the widget.
37+
/// This factory contains a single pointer to an instance of the widget, so only one view can be created within a given layout.
38+
class Q_SLICER_BASE_QTGUI_EXPORT qSlicerSingletonViewFactory : public ctkLayoutViewFactory
39+
{
40+
Q_OBJECT
41+
public:
42+
typedef ctkLayoutViewFactory Superclass;
43+
qSlicerSingletonViewFactory(QObject* parent=0);
44+
virtual ~qSlicerSingletonViewFactory();
45+
46+
/// Reimplemented to support custom element names
47+
virtual QStringList supportedElementNames()const;
48+
49+
/// Set the singleton widget instance that will be used to create the view
50+
/// The factory will become responsible for deleting the widget
51+
Q_INVOKABLE virtual void setWidget(QWidget* widget);
52+
/// Get the singleton widget instance that will be used to create the view
53+
Q_INVOKABLE virtual QWidget* widget();
54+
55+
/// Set the XML tag that identifies the view where the widget should be placed
56+
Q_INVOKABLE virtual void setTagName(QString tagName);
57+
/// Get the XML tag that identifies the view where the widget should be placed
58+
Q_INVOKABLE QString tagName();
59+
60+
protected:
61+
QScopedPointer<qSlicerSingletonViewFactoryPrivate> d_ptr;
62+
63+
/// Reimplemented to instantiate desired singleton widget from the element.
64+
Q_INVOKABLE virtual QWidget* createViewFromXML(QDomElement layoutElement);
65+
66+
private:
67+
Q_DECLARE_PRIVATE(qSlicerSingletonViewFactory)
68+
};
69+
70+
#endif

Libs/MRML/Widgets/qMRMLLayoutManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class QMRML_WIDGETS_EXPORT qMRMLLayoutManager : public ctkLayoutFactory
107107
/// If the registered view factory is a qMRMLLayoutViewFactory, then set
108108
/// its layoutManager and its mrmlScene.
109109
/// \sa ctkLayoutFactory::registerViewFactory(), unregisterViewFactory()
110-
virtual void registerViewFactory(ctkLayoutViewFactory* viewFactory);
110+
Q_INVOKABLE virtual void registerViewFactory(ctkLayoutViewFactory* viewFactory);
111111

112112
/// Return the list of registered MRML view factories.
113113
/// \sa registeredViewFactories(), registerViewFactory(),

0 commit comments

Comments
 (0)