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 #include "scplugin.h"
8
9 #include "deferredtask.h"
10
11 #include "scribusdoc.h"
12 #include "scribusapp.h"
13 #include "selection.h"
14 #include "ui/prefs_pane.h"
15
16 //=====================================================//
17 // ScPlugin //
18 //=====================================================//
19
ScPlugin()20 ScPlugin::ScPlugin()
21 : QObject(nullptr)
22 {
23 }
24
~ScPlugin()25 ScPlugin::~ScPlugin()
26 {
27 }
28
newPrefsPanelWidget(QWidget *,Prefs_Pane * &)29 bool ScPlugin::newPrefsPanelWidget( QWidget* /* parent */, Prefs_Pane*& /* panel */)
30 {
31 return false;
32 }
33
lastError() const34 const QString & ScPlugin::lastError() const
35 {
36 return m_lastError;
37 }
38
hasLastError() const39 bool ScPlugin::hasLastError() const
40 {
41 return (!m_lastError.isEmpty());
42 }
43
clearLastError()44 void ScPlugin::clearLastError()
45 {
46 m_lastError.clear();
47 }
48
pluginTypeName() const49 const QString ScPlugin::pluginTypeName() const
50 {
51 // These tests must be in reverse order of inheritance,
52 // ie most specific to least specific.
53 if (inherits("LoadSavePlugin"))
54 return tr("Load/Save/Import/Export");
55 if (inherits("ScPersistentPlugin"))
56 return tr("Persistent", "plugin manager plugin type");
57 if (inherits("ScActionPlugin"))
58 return tr("Action", "plugin manager plugin type");
59 qDebug("Unknown plugin type: %s", metaObject()->className());
60 return tr("Unknown");
61 }
62
setDoc(ScribusDoc *)63 void ScPlugin::setDoc(ScribusDoc* )
64 {
65 }
66
unsetDoc()67 void ScPlugin::unsetDoc()
68 {
69 }
70
changedDoc(ScribusDoc *)71 void ScPlugin::changedDoc(ScribusDoc* )
72 {
73 }
74
75 //=====================================================//
76 // ScActionPlugin //
77 //=====================================================//
78
ScActionPlugin()79 ScActionPlugin::ScActionPlugin() : ScPlugin()
80 {
81 }
82
~ScActionPlugin()83 ScActionPlugin::~ScActionPlugin()
84 {
85 }
86
87 // Stub for plugins that don't implement this method to inherit
run(ScribusDoc *,QIODevice *)88 bool ScActionPlugin::run(ScribusDoc* /*doc*/, QIODevice* /* target */)
89 {
90 return false;
91 }
92
run(QWidget *,ScribusDoc *,const QString &)93 bool ScActionPlugin::run(QWidget *, ScribusDoc* /*doc*/, const QString& /* target */)
94 {
95 return false;
96 }
97
98 // Stub for plugins that don't implement this method to inherit
runAsync(const QString &)99 DeferredTask* ScActionPlugin::runAsync(const QString& /* target */)
100 {
101 return nullptr;
102 }
103
104
105 // Stub for plugins that don't implement this method to inherit
runAsync(QIODevice *)106 DeferredTask* ScActionPlugin::runAsync(QIODevice* /* target */)
107 {
108 return nullptr;
109 }
110
111 // Legacy code support; avoid relying on in new code.
runResult() const112 const QString & ScActionPlugin::runResult() const
113 {
114 return m_runResult;
115 }
116
actionInfo() const117 const ScActionPlugin::ActionInfo & ScActionPlugin::actionInfo() const
118 {
119 Q_ASSERT(!m_actionInfo.text.isNull());
120 return m_actionInfo;
121 }
122
handleSelection(ScribusDoc * doc,int SelectedType)123 bool ScActionPlugin::handleSelection(ScribusDoc* doc, int SelectedType)
124 {
125 const int docSelectionCount = doc->m_Selection->count();
126 ActionInfo ai(actionInfo());
127 if (SelectedType != -1)
128 {
129 bool correctAppMode = false;
130 if (ai.forAppMode.count() == 0)
131 correctAppMode = true;
132 else if (ai.forAppMode.contains(doc->appMode))
133 correctAppMode = true;
134
135 if (!correctAppMode)
136 return false;
137
138 if (ai.needsNumObjects == -1)
139 return true;
140
141 if (ai.needsNumObjects > 2)
142 {
143 bool setter = true;
144 for (int bx = 0; bx < docSelectionCount; ++bx)
145 {
146 if (ai.notSuitableFor.contains(doc->m_Selection->itemAt(bx)->itemType()))
147 setter = false;
148 }
149 return setter;
150 }
151
152 if (docSelectionCount != ai.needsNumObjects)
153 return false;
154
155 if (ai.needsNumObjects == 2)
156 {
157 int sel1 = doc->m_Selection->itemAt(0)->itemType();
158 int sel2 = doc->m_Selection->itemAt(1)->itemType();
159 if (ai.notSuitableFor.contains(sel1))
160 return false;
161 if (ai.notSuitableFor.contains(sel2))
162 return false;
163 if ((ai.firstObjectType.count() == 0) && (ai.secondObjectType.count() == 0))
164 return true;
165 if ((ai.firstObjectType.count() == 0) && (ai.secondObjectType.count() != 0))
166 {
167 if ((ai.secondObjectType.contains(sel2)) || (ai.secondObjectType.contains(sel1)))
168 return true;
169 }
170 else if ((ai.firstObjectType.count() != 0) && (ai.secondObjectType.count() == 0))
171 {
172 if ((ai.firstObjectType.contains(sel2)) || (ai.firstObjectType.contains(sel1)))
173 return true;
174 }
175 if (((ai.firstObjectType.contains(sel1)) && (ai.secondObjectType.contains(sel2))) || ((ai.firstObjectType.contains(sel2)) && (ai.secondObjectType.contains(sel1))))
176 return true;
177 }
178 else if (!ai.notSuitableFor.contains(SelectedType))
179 return true;
180 else
181 return false;
182 }
183 else
184 {
185 bool correctAppMode = false;
186 if (ai.forAppMode.count() == 0)
187 correctAppMode = true;
188 else if (ai.forAppMode.contains(doc->appMode))
189 correctAppMode = true;
190 if (correctAppMode)
191 {
192 if (ai.needsNumObjects == -1)
193 return true;
194 if ((ai.needsNumObjects > 2) && (docSelectionCount > 0))
195 return true;
196 return false;
197 }
198 }
199 return false;
200 }
201
202 //=====================================================//
203 // ScActionPlugin //
204 //=====================================================//
205
ScPersistentPlugin()206 ScPersistentPlugin::ScPersistentPlugin()
207 : ScPlugin()
208 {
209 }
210
~ScPersistentPlugin()211 ScPersistentPlugin::~ScPersistentPlugin()
212 {
213 }
214