1 /*
2  For general Scribus (>=1.3.2) copyright and licensing information please refer
3  to the COPYING file provided with the program. Following this notice may exist
4  a copyright and/or license notice that predates the release of Scribus 1.3.2
5  for which a new license (GPL+exception) is in place.
6  */
7 /***************************************************************************
8 *                                                                         *
9 *   This program is free software; you can redistribute it and/or modify  *
10 *   it under the terms of the GNU General Public License as published by  *
11 *   the Free Software Foundation; either version 2 of the License, or     *
12 *   (at your option) any later version.                                   *
13 *                                                                         *
14 ***************************************************************************/
15 
16 #ifndef UPDATEMANAGER_H
17 #define UPDATEMANAGER_H
18 
19 #include "scribusapi.h"
20 
21 #include <QSet>
22 
23 
24 
25 /**
26  Clients of UpdateManager use this class to encapsulate their argument
27  to updateNow().
28  */
29 struct SCRIBUS_API UpdateMemento
30 {
31 	virtual ~UpdateMemento();
32 };
33 
34 
35 
36 /**
37   Implement this interface in Observable.
38 */
39 
40 
41 class SCRIBUS_API UpdateManaged
42 {
43 public:
44 	virtual void updateNow(UpdateMemento* what) = 0;
~UpdateManaged()45 	virtual ~UpdateManaged() {}
46 };
47 
48 
49 /**
50   An UpdateManager controls if and when updates will be propagated to the Observers.
51   Usually each document has one UpdateManager.
52   They are used this way:
53 
54   When many changes are about to happen, the application calls "setUpdatesDisabled()"
55   before it begins the changes and calls "setUpdatesEnabled()" when it's done.
56 
57   An Observable calls "requestUpdate(this)". If the result is positive, the Observable
58   calls "updateNow()" directly. Otherwise the UpdateManager stores a reference to the
59   Observable and will call its "updateNow()" method once updates are enabled again.
60 
61   If "requestUpdate()" is called multiple times before updates are enabled again, each
62   observable will only get one notification with "updateNow()"
63   when the updates are enabled again.
64  */
65 
66 class SCRIBUS_API UpdateManager
67 {
68 	int m_updatesDisabled {0};
69 	QSet<QPair<UpdateManaged*, UpdateMemento*> > m_pending;
70 
71 public:
UpdateManager()72 	UpdateManager() {}
73 	virtual ~UpdateManager();
74 
75 	void setUpdatesEnabled(bool val = true);
setUpdatesDisabled()76 	void setUpdatesDisabled() { setUpdatesEnabled(false); };
updatesEnabled()77 	bool updatesEnabled() { return m_updatesDisabled == 0; };
78 	/**
79 		Returns true if updates are enabled, otherwise stores 'observable' for notification when updates get enabled again.
80 	 */
81 	bool requestUpdate(UpdateManaged* observable, UpdateMemento* what);
82 };
83 
84 
85 
86 #endif
87