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 "api_imageitem.h"
8 #include "utils.h"
9 #include "selection.h"
10 #include "undomanager.h"
11 #include "sctextstream.h"
12 #include "scribusview.h"
13 #include "ui/contentpalette.h"
14 
ImageAPI(PageItem_ImageFrame * im)15 ImageAPI::ImageAPI(PageItem_ImageFrame* im) : ItemAPI(im)
16 {
17 	qDebug() << "ImageitemAPI loaded";
18     setObjectName("ImageItemAPI");
19 	item = im;
20 }
21 
22 
xScale()23 double ImageAPI::xScale()
24 {
25 	return item->imageXScale();
26 }
27 
setXScale(double value)28 void ImageAPI::setXScale(double value)
29 {
30 	item->setImageXScale(value);
31 }
32 
33 
yScale()34 double ImageAPI::yScale()
35 {
36 	return item->imageYScale();
37 }
38 
setYScale(double value)39 void ImageAPI::setYScale(double value)
40 {
41 	item->setImageYScale(value);
42 }
43 
44 
xOffset()45 double ImageAPI::xOffset()
46 {
47 	return item->imageXOffset();
48 }
49 
setXOffset(double value)50 void ImageAPI::setXOffset(double value)
51 {
52 	item->setImageXOffset(value);
53 }
54 
55 
yOffset()56 double ImageAPI::yOffset()
57 {
58 	return item->imageYOffset();
59 }
60 
setYOffset(double value)61 void ImageAPI::setYOffset(double value)
62 {
63 	item->setImageYOffset(value);
64 }
65 
setGrayscale()66 void ImageAPI::setGrayscale()
67 {
68     if (!checkHaveDocument())
69         RAISE("No document open");
70 	if (item == nullptr)
71         return;
72 	if (! item->isImageFrame())
73     {
74         RAISE("Specified item not an image frame.");
75         return;
76     }
77 
78     ImageEffect ef;
79     ef.effectCode = ImageEffect::EF_GRAYSCALE;
80 
81     item->effectsInUse.append(ef);
82     item->pixm.applyEffect(item->effectsInUse, ScCore->primaryMainWindow()->doc->PageColors, false);
83 
84     ScCore->primaryMainWindow()->doc->updatePic();
85 }
86 
load(QString filename)87 void ImageAPI::load(QString filename)
88 {
89 	if (!checkHaveDocument())
90 		RAISE("No document open");
91 	if (item == nullptr)
92 		return;
93 	if (!item->isImageFrame())
94 	{
95 		RAISE("Target is not an image frame.");
96 		return;
97 	}
98 	ScCore->primaryMainWindow()->doc->loadPict(filename, item);
99 }
100 
scale(double x,double y)101 void ImageAPI::scale(double x, double y)
102 {
103 	if (!checkHaveDocument())
104 		RAISE("No document open");
105 	if (item == nullptr)
106 		return;
107 	if (! item->isImageFrame())
108 	{
109 		RAISE("Specified item not an image frame.");
110 		return;
111 	}
112 
113     Selection tempSelection(*ScCore->primaryMainWindow()->doc->m_Selection);
114 	bool hadOrigSelection = (tempSelection.count() != 0);
115 
116 	ScCore->primaryMainWindow()->doc->m_Selection->clear();
117     ScCore->primaryMainWindow()->view->Deselect();
118     ScCore->primaryMainWindow()->view->SelectItem(item);
119 
120     ScCore->primaryMainWindow()->doc->itemSelection_SetImageScale(x, y);
121 	ScCore->primaryMainWindow()->doc->updatePic();
122 
123 	// Now restore the selection.
124 	ScCore->primaryMainWindow()->view->Deselect();
125 	if (hadOrigSelection)
126 		*ScCore->primaryMainWindow()->doc->m_Selection=tempSelection;
127 }
128 
offset(double x,double y)129 void ImageAPI::offset(double x, double y)
130 {
131 	if (!checkHaveDocument())
132 		RAISE("No document open");
133 	if (item == nullptr)
134 		return;
135 	if (! item->isImageFrame())
136 	{
137 		RAISE("Specified item not an image frame.");
138 		return;
139 	}
140 
141 	// Grab the old selection - but use it only where is there any
142 	Selection tempSelection(*ScCore->primaryMainWindow()->doc->m_Selection);
143 	bool hadOrigSelection = (tempSelection.count() != 0);
144 
145 	ScCore->primaryMainWindow()->doc->m_Selection->clear();
146 	// Clear the selection
147 	ScCore->primaryMainWindow()->view->Deselect();
148 	// Select the item, which will also select its group if
149 	// there is one.
150 	ScCore->primaryMainWindow()->view->SelectItem(item);
151 
152 	// offset
153 	double newOffsetX = x / ((item->imageXScale() != 0.0) ? item->imageXScale() : 1);
154 	double newOffsetY = y / ((item->imageYScale() != 0.0) ? item->imageYScale() : 1);
155 	ScCore->primaryMainWindow()->doc->itemSelection_SetImageOffset(newOffsetX, newOffsetY);    //CB why when this is done above?
156 	ScCore->primaryMainWindow()->doc->updatePic();
157 
158 	// Now restore the selection.
159 	ScCore->primaryMainWindow()->view->Deselect();
160 	if (hadOrigSelection)
161 		*ScCore->primaryMainWindow()->doc->m_Selection=tempSelection;
162 }
163 
setBrightness(double n)164 void ImageAPI::setBrightness(double n)
165 {
166 	if (!checkHaveDocument())
167 		RAISE("No document open");
168 	if (item == nullptr)
169 		return ;
170 	if (! item->isImageFrame())
171 	{
172 		RAISE("Specified item not an image frame.");
173 		return;
174 	}
175 
176 	ImageEffect ef;
177 	ef.effectCode = ImageEffect::EF_BRIGHTNESS;
178 	ScTextStream fp(&ef.effectParameters, QIODevice::WriteOnly);
179 	fp << n;
180 
181 	item->effectsInUse.append(ef);
182 	item->pixm.applyEffect(item->effectsInUse, ScCore->primaryMainWindow()->doc->PageColors, false);
183 
184 	ScCore->primaryMainWindow()->doc->updatePic();
185 }
186 
scaleToFrame(bool scaleToFrame,bool proportional)187 void ImageAPI::scaleToFrame(bool scaleToFrame, bool proportional)
188 {
189 	if (!checkHaveDocument())
190 		RAISE("No document open.");
191 	if (item == nullptr)
192 		return;
193 	if (! item->isImageFrame())
194 	{
195 		RAISE("Specified item not an image frame.");
196 		return;
197 	}
198 	// Set the item to scale if appropriate. ScaleType 1 is free
199 	// scale, 0 is scale to frame.
200 	item->ScaleType = !scaleToFrame;
201 	// Now, if the user has chosen to set the proportional mode,
202 	// set it. 1 is proportional, 0 is free aspect.
203 	item->AspectRatio = proportional;
204 	// Force the braindead app to notice the changes
205 
206 	//FIXME emit or something so we dont need this
207 	ScCore->primaryMainWindow()->contentPalette->update(item->asImageFrame());
208 	//ScCore->primaryMainWindow()->view->adjustPictScale(item);
209 
210 	item->update();
211 }
212 
~ImageAPI()213 ImageAPI::~ImageAPI()
214 {
215 	qDebug() << "ImageitemAPI deleted";
216 }
217 
218 
219