Subversion Repositories Scribus

Rev

Rev 21587 | Rev 23282 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
14647 cbradney 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_page.h"
8
#include "pageitem.h"
9
#include "selection.h"
10
#include "util_math.h"
11
#include "commonstrings.h"
12
#include "../formatidlist.h"
13
#include "loadsaveplugin.h"
14
#include "ui/propertiespalette.h"
16555 craig 15
#include "ui/propertiespalette_text.h"
16
#include "ui/propertiespalette_line.h"
21587 craig 17
#include "ui/textpalette.h"
17560 jainbasil 18
#include "scribuscore.h"
19310 craig 19
#include "scribusview.h"
17560 jainbasil 20
#include "api_textitem.h"
21
#include "api_imageitem.h"
14647 cbradney 22
 
23
 
24
 
17935 jainbasil 25
PageAPI::PageAPI(ScPage *thisPage) : QObject(COLLECTOR)
14647 cbradney 26
{
17560 jainbasil 27
        qDebug() << "PageAPI loaded";
28
        setObjectName("page");
17935 jainbasil 29
        page = thisPage;
14647 cbradney 30
}
31
 
32
 
33
 
17638 jainbasil 34
PageAPI::~PageAPI()
14647 cbradney 35
{
17560 jainbasil 36
        qDebug() << "PageAPI deleted";
14647 cbradney 37
}
38
 
39
 
40
 
41
/**
42
 * Scripter.activeDocument.activePage.remove()
43
 *
44
 * Removes the active page from the active document.
45
 */
17638 jainbasil 46
void PageAPI::remove()
14647 cbradney 47
{
17560 jainbasil 48
        if (ScCore->primaryMainWindow()->doc->Pages->count() > 1)
49
                ScCore->primaryMainWindow()->deletePage2(number());
50
        else
51
                RAISE("No pages left to remove");
14647 cbradney 52
}
53
 
54
 
55
 
56
/**
57
 * Scripter.activeDocument.activePage.position
58
 * Property
59
 * location of active page as int value (read-only)
60
 */
17638 jainbasil 61
int PageAPI::position()
14647 cbradney 62
{
17560 jainbasil 63
        return ScCore->primaryMainWindow()->doc->locationOfPage(number());
14647 cbradney 64
}
65
 
66
/**
67
 * Scripter.activeDocument.activePage.position
68
 * Property
69
 * int value of active page number >= 1
70
 */
17638 jainbasil 71
int PageAPI::number()
14647 cbradney 72
{
17560 jainbasil 73
        return ScCore->primaryMainWindow()->doc->currentPageNumber() + 1;
14647 cbradney 74
}
75
 
76
 
77
/**
78
 * Scripter.activeDocument.activePage.items
79
 * Property
80
 * List of Item objects of active page
81
 */
17638 jainbasil 82
QList<QVariant> PageAPI::items()
14647 cbradney 83
{
17560 jainbasil 84
        QList<QVariant> l;
85
        int num = number() - 1;
86
        for (int i = 0; i<ScCore->primaryMainWindow()->doc->Items->count(); ++i)
87
        {
88
                if (num == ScCore->primaryMainWindow()->doc->Items->at(i)->OwnPage)
89
                {
90
                        PageItem *item = ScCore->primaryMainWindow()->doc->Items->at(i);
91
                        /**
92
                         * Checking whether it is a textframe. If yes, we are trying to cast
93
                         * it onto TextWrapper class, which can effectively perform all
94
                         * the text operations
95
                         */
96
                        if (item->asTextFrame())
97
                        {
17638 jainbasil 98
                                l.append(qVariantFromValue((QObject *)(new TextAPI(item->asTextFrame()))));
17560 jainbasil 99
                        }
100
                        else
101
                        {
102
                                l.append(qVariantFromValue(
17638 jainbasil 103
                                             (QObject *)(new ItemAPI(item))
17560 jainbasil 104
                                         ));
105
                        }
106
                }
107
        }
108
        return l;
14647 cbradney 109
}
110
 
111
 
112
/**
113
 * Scripter.activeDocument.activePage.selection
114
 * Property
115
 * List of selected Item objects on active page of active document
116
 */
17638 jainbasil 117
QList<QVariant> PageAPI::selection()
14647 cbradney 118
{
17560 jainbasil 119
        QList<QVariant> l;
120
        int num = number() - 1;
121
        Selection *sel = ScCore->primaryMainWindow()->doc->m_Selection;
122
        for (int i=0; i < sel->count(); i++)
123
        {
124
                PageItem *item = sel->itemAt(i);
125
                if (num == item->OwnPage)
126
                {
127
                        if (item->asTextFrame())
128
                        {
17638 jainbasil 129
                                l.append(qVariantFromValue((QObject*)(new TextAPI(item->asTextFrame()))));
17560 jainbasil 130
                        }
131
                        else
132
                        {
17638 jainbasil 133
                                l.append(qVariantFromValue((QObject*)(new ItemAPI(item))));
17560 jainbasil 134
                        }
135
                }
136
        }
137
        return l;
14647 cbradney 138
}
139
 
140
 
141
// copied from scripter/cmdutil.cpp
142
/// Convert an X co-ordinate part in page units to a document co-ordinate
143
/// in system units.
17638 jainbasil 144
double PageAPI::pageXtoDocX(double x)
14647 cbradney 145
{
17560 jainbasil 146
        return x + ScCore->primaryMainWindow()->doc->currentPage()->xOffset();
14647 cbradney 147
}
148
 
149
 
150
// copied from scripter/cmdutil.cpp
151
/// Convert a Y co-ordinate part in page units to a document co-ordinate
152
/// in system units. The document co-ordinates have their origin somewere
153
/// up and left of the first page, where page co-ordinates have their
154
/// origin on the top left of the current page.
17638 jainbasil 155
double PageAPI::pageYtoDocY(double y)
14647 cbradney 156
{
17560 jainbasil 157
        return y + ScCore->primaryMainWindow()->doc->currentPage()->yOffset();
14647 cbradney 158
}
159
 
17638 jainbasil 160
PageItem *PageAPI::newItem(const PageItem::ItemType itemType,
14647 cbradney 161
                          const PageItem::ItemFrameType frameType,
162
                          const double x, const double y, const double width,
163
                          const double height, const double w,
164
                          const QString& fill, const QString& outline)
165
{
17560 jainbasil 166
        int i = ScCore->primaryMainWindow()->doc->itemAdd(
167
                    itemType, frameType,
168
                    pageXtoDocX(x), pageYtoDocY(y),
169
                    width, height,
22333 jghali 170
                    w, fill, outline, PageItem::StandardItem);
17560 jainbasil 171
        return ScCore->primaryMainWindow()->doc->Items->at(i);
14647 cbradney 172
}
173
 
174
 
175
 
176
/**
177
 * Scripter.activeDocument.activePage.newRectangle(x, y, width, height)
178
 * x is a double
179
 * y is a double
180
 * width is a double
181
 * height is a double
182
 * returns Item object
183
 */
17638 jainbasil 184
QObject *PageAPI::newRectangle(double x, double y, double width, double height)
14647 cbradney 185
{
17560 jainbasil 186
        PageItem *item = newItem(
187
                   PageItem::Polygon, PageItem::Rectangle, x, y, width, height,
188
                   ScCore->primaryMainWindow()->doc->itemToolPrefs().shapeLineWidth,
189
                   ScCore->primaryMainWindow()->doc->itemToolPrefs().shapeFillColor,
190
                   ScCore->primaryMainWindow()->doc->itemToolPrefs().shapeLineColor);
17638 jainbasil 191
        return new ItemAPI(item);
14647 cbradney 192
}
193
 
194
 
195
 
196
/**
197
 * Scripter.activeDocument.activePage.newEllipse(x, y, width, height)
198
 * x is a double
199
 * y is a double
200
 * width is a double
201
 * height is a double
202
 * returns Item object
203
 */
17638 jainbasil 204
QObject *PageAPI::newEllipse(double x, double y, double width, double height)
14647 cbradney 205
{
17560 jainbasil 206
        PageItem *item = newItem(
207
                   PageItem::Polygon, PageItem::Ellipse, x, y, width, height,
208
                   ScCore->primaryMainWindow()->doc->itemToolPrefs().shapeLineWidth,
209
                   ScCore->primaryMainWindow()->doc->itemToolPrefs().shapeFillColor,
210
                   ScCore->primaryMainWindow()->doc->itemToolPrefs().shapeLineColor);
17638 jainbasil 211
        return new ItemAPI(item);
14647 cbradney 212
}
213
 
214
 
215
 
216
/**
217
 * Scripter.activeDocument.activePage.newImage(x, y, width, height)
218
 * x is a double
219
 * y is a double
220
 * width is a double
221
 * height is a double
222
 * returns Item object
223
 */
17638 jainbasil 224
QObject *PageAPI::newImage(double x, double y, double width, double height)
14647 cbradney 225
{
17560 jainbasil 226
        PageItem *item = newItem(
227
                   PageItem::ImageFrame, PageItem::Unspecified, x, y, width, height,
228
                   1,
229
                   // @xxx: ale
230
                   // ScCore->primaryMainWindow()->doc->toolSettings.dBrushPict,
231
                   ScCore->primaryMainWindow()->doc->itemToolPrefs().imageFillColor,
232
                   CommonStrings::None);
17638 jainbasil 233
        return new ImageAPI(item->asImageFrame());
14647 cbradney 234
}
235
 
236
 
237
 
238
/**
239
 * Scripter.activeDocument.activePage.newText(x, y, width, height)
240
 * x is a double
241
 * y is a double
242
 * width is a double
243
 * height is a double
244
 * returns Item object
245
 */
17638 jainbasil 246
QObject *PageAPI::newText(double x, double y, double width, double height)
14647 cbradney 247
{
17560 jainbasil 248
        PageItem *item = newItem(
249
                   PageItem::TextFrame, PageItem::Unspecified, x, y, width, height,
250
                   ScCore->primaryMainWindow()->doc->itemToolPrefs().shapeLineWidth,
251
                   CommonStrings::None,
252
                   ScCore->primaryMainWindow()->doc->itemToolPrefs().textFont);
17638 jainbasil 253
        return new TextAPI(item->asTextFrame());
14647 cbradney 254
}
255
 
256
 
257
 
258
/**
259
 * Scripter.activeDocument.activePage.newLine(x, y, width, height)
260
 * x is a double
261
 * y is a double
262
 * width is a double
263
 * height is a double
264
 * returns Item object
265
 */
17638 jainbasil 266
QObject *PageAPI::newLine(double x, double y, double width, double height)
14647 cbradney 267
{
17560 jainbasil 268
        PageItem *item = (PageItem*)newItem(
269
                             PageItem::Line, PageItem::Unspecified, x, y, width, height,
270
                             ScCore->primaryMainWindow()->doc->itemToolPrefs().shapeLineWidth,
271
                             ScCore->primaryMainWindow()->doc->itemToolPrefs().shapeFillColor,
272
                             ScCore->primaryMainWindow()->doc->itemToolPrefs().shapeLineColor);
273
        item->setRotation(xy2Deg(width-x, height-y));
274
        item->setWidthHeight(sqrt(pow(x-width, 2.0) + pow(y-height, 2.0)), 1.0);
275
        item->Sizing = false;
276
        item->updateClip();
17638 jainbasil 277
        return new ItemAPI(item);
14647 cbradney 278
}
279
 
280
// XXX: newPolyLine, bezierline
281
 
282
 
18300 jghali 283
void PageAPI::placeImage(const QString formatExt, const QString & filename, const double x, const double y)
14647 cbradney 284
{
18300 jghali 285
        const FileFormat * fmt = LoadSavePlugin::getFormatByExt(formatExt);
17560 jainbasil 286
        if (fmt)
287
        {
288
                fmt->loadFile(filename,
289
                              LoadSavePlugin::lfUseCurrentPage|LoadSavePlugin::lfInteractive|LoadSavePlugin::lfScripted);
290
                if (ScCore->primaryMainWindow()->doc->m_Selection->count() > 1)
291
                {
292
                        double x2, y2, w, h;
293
                        ScCore->primaryMainWindow()->doc->m_Selection->getGroupRect(
294
                            &x2, &y2, &w, &h);
295
                        ScCore->primaryMainWindow()->view->startGroupTransaction();
296
                        ScCore->primaryMainWindow()->doc->moveGroup(
297
                            pageXtoDocX(x) - x2, pageYtoDocY(y) - y2);
298
                        ScCore->primaryMainWindow()->view->endGroupTransaction();
299
                        ScCore->primaryMainWindow()->propertiesPalette->updateColorList();
21587 craig 300
                        ScCore->primaryMainWindow()->textPalette->updateColorList();
301
                        ScCore->primaryMainWindow()->textPalette->textPal->paraStyleCombo->updateFormatList();
302
                        ScCore->primaryMainWindow()->textPalette->textPal->charStyleCombo->updateFormatList();
17560 jainbasil 303
                        ScCore->primaryMainWindow()->propertiesPalette->linePal->updateLineStyles();
304
                }
305
        }
306
        else RAISE("Format plug-in not loaded.");
14647 cbradney 307
}
308
 
309
 
310
// XXX: join place* into one function and set formatid according to extension
311
 
312
 
313
/**
314
 * Scripter.activeDocument.activePage.placeSVG(filename x, y)
315
 * filename is string
316
 * x is double
317
 * y is double
318
 * import vector graphics at given position on active page
319
 */
17638 jainbasil 320
void PageAPI::placeSVG(const QString & filename, const double x, const double y)
14647 cbradney 321
{
18300 jghali 322
        placeImage("svg", filename, x, y);
14647 cbradney 323
}
324
 
325
 
326
 
327
/**
328
 * Scripter.activeDocument.activePage.placeODG(filename x, y)
329
 * filename is string
330
 * x is double
331
 * y is double
332
 * import vector graphics at given position on active page
333
 */
17638 jainbasil 334
void PageAPI::placeODG(const QString & filename, const double x, const double y)
14647 cbradney 335
{
18300 jghali 336
        placeImage("odg", filename, x, y);
14647 cbradney 337
}
338
 
339
 
340
 
341
/**
342
 * Scripter.activeDocument.activePage.placeEPS(filename x, y)
343
 * filename is string
344
 * x is double
345
 * y is double
346
 * import vector graphics at given position on active page
347
 */
17638 jainbasil 348
void PageAPI::placeEPS(const QString & filename, const double x, const double y)
14647 cbradney 349
{
18300 jghali 350
        placeImage("ps", filename, x, y);
14647 cbradney 351
}
352
 
353
 
354
 
355
/**
356
 * Scripter.activeDocument.activePage.placeSXD(filename x, y)
357
 * filename is string
358
 * x is double
359
 * y is double
360
 * import vector graphics at given position on active page
361
 */
17638 jainbasil 362
void PageAPI::placeSXD(const QString & filename, const double x, const double y)
14647 cbradney 363
{
18300 jghali 364
        placeImage("sxd", filename, x, y);
14647 cbradney 365
}
17560 jainbasil 366
 
367
 
368
/**
369
 *  Scripter.activeDocument.activePage.savePageAsEPS(filename)
370
 * filename is QString
371
 */
17638 jainbasil 372
void PageAPI::savePageAsEPS(const QString &filename)
17560 jainbasil 373
{
374
        QString epsError;
375
        bool ret = ScCore->primaryMainWindow()->DoSaveAsEps(filename, epsError);
376
        if (!ret)
377
                RAISE("Failed to save as EPS");
378
}
379
 
380
/**
381
 * Scripter.activeDocument.activePage.type
382
 * Returns 0 if LeftPage, 1 if Middle Page, 2 for Right Page
383
 */
17638 jainbasil 384
int PageAPI::type()
17560 jainbasil 385
{
386
        return ScCore->primaryMainWindow()->doc->locationOfPage(number());
387
}