File: observable.h

package info (click to toggle)
scribus 1.4.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 244,532 kB
  • sloc: cpp: 274,439; xml: 12,534; python: 3,448; ansic: 3,438; makefile: 1,201; perl: 95; sh: 41
file content (320 lines) | stat: -rw-r--r-- 7,904 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 For general Scribus (>=1.3.2) copyright and licensing information please refer
 to the COPYING file provided with the program. Following this notice may exist
 a copyright and/or license notice that predates the release of Scribus 1.3.2
 for which a new license (GPL+exception) is in place.
 */
/***************************************************************************
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
***************************************************************************/

#ifndef OBSERVABLE_H
#define OBSERVABLE_H

#include "scribusapi.h"

#include <QObject>
#include <QSet>
#include <QVariant>

#include "updatemanager.h"


//#include "observable_private.h"
struct SCRIBUS_API Private_Signal : public QObject 
{
	Q_OBJECT;
	
public:
	void emitSignal(QObject* what)
{
		emit changedObject(what);
}

void emitSignal(QVariant what)
{
	emit changedData(what);
}

bool connectSignal(QObject*, QObject* o, const char* slot)
{
	return QObject::connect(this, SIGNAL(changedObject(QObject*)), o, slot);
}

bool disconnectSignal(QObject*, QObject* o, const char* slot)
{
	return QObject::disconnect(this, SIGNAL(changedObject(QObject*)), o, slot);
}

bool connectSignal(QVariant, QObject* o, const char* slot)
{
	return QObject::connect(this, SIGNAL(changedData(QVariant)), o, slot);
}

bool disconnectSignal(QVariant, QObject* o, const char* slot)
{
	return QObject::disconnect(this, SIGNAL(changedData(QVariant)), o, slot);
}

signals:
void changedObject(QObject* what);
void changedData(QVariant what);
};



template<class OBSERVED>
struct Private_Memento : public UpdateMemento
{
	Private_Memento(OBSERVED data) : m_data(data), m_layout(false) {};
	Private_Memento(OBSERVED data, bool layout) : m_data(data), m_layout(layout) {};
	
	OBSERVED m_data;
	bool     m_layout;
};



/**
  Implement this interface if you want to observe an observable but dont want to derive from QObject 
 */
template<class OBSERVED>
class SCRIBUS_API Observer {
public:
	virtual void changed(OBSERVED, bool doLayout) = 0;
	virtual ~Observer() {}
};




/**
 An MassObservable is basically just the source of a changed() signal.
 Observers can register via the Qt signal/slot mechanism or via the above interface.
 
 The difference to Observable (below) is that a MassObservable doesnt report changes to
 itself but to a bunch of SingleObservables.
 When you call update() on the SingleObservable, it will tell the associated
 MassObservable to notify all observers with the "changed" signal,
 providing a pointer to the single Observable. 
 
 The class parameter OBSERVED is usually a pointer to a subclass of SingleObservable. 
 
 MassObservable does not inherit QObject since that makes it difficult to use it as a mixin class.
 */
template<class OBSERVED>
class MassObservable : public UpdateManaged 
{	
	friend class UpdateManager;
	
public:
	MassObservable(UpdateManager* um = NULL);
	virtual ~MassObservable();
	/**
		Used if the UpdateManager is not available when the constructor is called
	*/
	void setUpdateManager(UpdateManager* um);
	
	/**
		This method will be called by the SingleObservable.
	    If updates are enabled, it calls updateNow() directly, otherwise the undomanager
	    will take care of that.
	 */
	virtual void update(OBSERVED what);

	/**
		Same as update, except layout will be update immediately
	 */
	virtual void updateLayout(OBSERVED what);
	
	void connectObserver(Observer<OBSERVED>* o);
	void disconnectObserver(Observer<OBSERVED>* o);
	
	bool connectObserver(QObject* o, const char* slot);
	bool disconnectObserver(QObject* o, const char* slot = 0);
	
protected:
	/**
		This method will be called by the updatemanager or by update()
	 */
	virtual void updateNow(UpdateMemento* what);

	QSet<Observer<OBSERVED>*> m_observers;
	Private_Signal* changedSignal;
	UpdateManager* m_um;
};




/**
  This mixin class just provides the update() method.
 */
template<class OBSERVED>
class SCRIBUS_API SingleObservable
{
public:
	/**
	  Connects this SingleObservale to the MassObservable
	 */
	SingleObservable(MassObservable<OBSERVED*> * massObservable) : m_massObservable(massObservable) 
	{};
	
	virtual ~SingleObservable() 
	{};
	
	void setMassObservable(MassObservable<OBSERVED*> * massObservable) 
	{
		m_massObservable = massObservable;
	}
	
	virtual void update() 
	{ 
		m_massObservable->update(dynamic_cast<OBSERVED*>(this)); 
	}

	virtual void updateLayout() 
	{ 
		m_massObservable->updateLayout(dynamic_cast<OBSERVED*>(this)); 
	}
	
private:
	MassObservable<OBSERVED*>* m_massObservable;
};





/**
  An Observable is basically just the source of a changed() signal.
  Observers can register via the Qt signal/slot mechanism or via the above interface.
 
  When you call update(), all observers will receive the "changed" signal with a pointer
  to the Observable (this).
 
  Observable is implemented as a MassObservable which.
 
  The class parameter OBSERVED should be the implementing class.
 */
template<class OBSERVED>
class SCRIBUS_API Observable : public MassObservable<OBSERVED*> 
{	
public:
	Observable(UpdateManager* um = NULL) : MassObservable<OBSERVED*>(um) 
	{};
	
	virtual void update() 
	{ 
		MassObservable<OBSERVED*>::update(dynamic_cast<OBSERVED*>(this)); 
	}
private:
	using MassObservable<OBSERVED*>::update;
};



// IMPLEMENTATION



template<class OBSERVED>
inline MassObservable<OBSERVED>::MassObservable(UpdateManager* um) : m_observers(), changedSignal(new Private_Signal()), m_um(um)
{
}

template<class OBSERVED>
inline MassObservable<OBSERVED>::~MassObservable()
{
	m_observers.clear();
	delete changedSignal;
}


template<class OBSERVED>
inline void MassObservable<OBSERVED>::setUpdateManager(UpdateManager* um)
{
	m_um = um;
}


template<class OBSERVED>
inline void MassObservable<OBSERVED>::update(OBSERVED what)
{
	Private_Memento<OBSERVED>* memento = new Private_Memento<OBSERVED>(what);
	if (m_um == NULL || m_um->requestUpdate(this, memento))
	{
		updateNow(memento);
	}
}

template<class OBSERVED>
inline void MassObservable<OBSERVED>::updateLayout(OBSERVED what)
{
	Private_Memento<OBSERVED>* memento = new Private_Memento<OBSERVED>(what, true);
	if (m_um == NULL || m_um->requestUpdate(this, memento))
	{
		updateNow(memento);
	}
}

template<class OBSERVED>
inline void MassObservable<OBSERVED>::updateNow(UpdateMemento* what)
{
	Private_Memento<OBSERVED>* memento = dynamic_cast<Private_Memento<OBSERVED>*>(what);
	foreach (Observer<OBSERVED>* obs, m_observers)
	{
		obs->changed(memento->m_data, memento->m_layout);
	}
	changedSignal->emitSignal(QVariant::fromValue(memento->m_data));
	delete memento;
}


template<class OBSERVED>
inline void MassObservable<OBSERVED>::connectObserver(Observer<OBSERVED>* o)
{
	m_observers.insert(o);
}

template<class OBSERVED>
inline void MassObservable<OBSERVED>::disconnectObserver(Observer<OBSERVED>* o)
{
	m_observers.remove(o);
}


template <typename T>
inline void Private_Init(T& dummy) {}

template <>
inline void Private_Init(QObject*& dummy) 
{ 
//	dummy->die_compiler_die(); 
	dummy = 0;
}


template<class OBSERVED>
inline bool MassObservable<OBSERVED>::connectObserver(QObject* o, const char* slot)
{
	OBSERVED dummy;
	Private_Init(dummy);
	return changedSignal->connectSignal(QVariant::fromValue(dummy), o, slot);
}

template<class OBSERVED>
inline bool MassObservable<OBSERVED>::disconnectObserver(QObject* o, const char* slot)
{
	OBSERVED dummy;
	Private_Init(dummy);
	return changedSignal->disconnectSignal(QVariant::fromValue(dummy), o, slot);
}


#endif