Subversion Repositories Scribus

Rev

Rev 24732 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4430 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
*/
24731 jghali 7
 
8
#include <memory>
9
 
10
#include <QColor>
11
#include <QCursor>
12
#include <QPixmap>
13
#include <QString>
14
 
15
#include "commonstrings.h"
16
#include "pageitem_textframe.h"
17
#include "prefsmanager.h"
4070 subik 18
#include "sampleitem.h"
10601 mrdocs 19
#include "scpainter.h"
24731 jghali 20
#include "scribuscore.h"
4077 subik 21
#include "scribusdoc.h"
24731 jghali 22
#include "ui/loremipsum.h"
4150 subik 23
#include "undomanager.h"
4070 subik 24
 
22607 craig 25
SampleItem::SampleItem()
24731 jghali 26
          : m_Doc(new ScribusDoc())
4070 subik 27
{
14807 fschmid 28
        if (!m_Doc)
29
                return;
24731 jghali 30
 
14807 fschmid 31
        m_Doc->setup(0, 1, 1, 1, 1, "Custom", "Custom");
32
        m_Doc->setPage(1, 1, 0, 0, 0, 0, 0, 0, false, false);
33
        m_Doc->addPage(0);
22607 craig 34
        m_Doc->setGUI(false, ScCore->primaryMainWindow(), nullptr);
24731 jghali 35
        // tmp colors. to be removed in destructor
5781 cbradney 36
        m_Doc->PageColors.insert("__blackforpreview__", ScColor(0, 0, 0, 255));
37
        m_Doc->PageColors.insert("__whiteforpreview__", ScColor(0, 0, 0, 0));
38
        m_Doc->PageColors.insert("__whiteforpreviewbg__", ScColor(0, 0, 0, 0));
24731 jghali 39
 
20691 craig 40
        m_tmpStyle.setName("(preview temporary)");
41
        m_tmpStyle.setLineSpacingMode(ParagraphStyle::FixedLineSpacing);
42
        m_tmpStyle.setLineSpacing((m_Doc->itemToolPrefs().textSize / 10.0) * (static_cast<double>(m_Doc->typographicPrefs().autoLineSpacing) / 100));
23126 jghali 43
        m_tmpStyle.setAlignment(ParagraphStyle::LeftAligned);
20691 craig 44
        m_tmpStyle.setLeftMargin(0);
45
        m_tmpStyle.setFirstIndent(0);
46
        m_tmpStyle.setRightMargin(0);
47
        m_tmpStyle.setGapBefore(0);
48
        m_tmpStyle.setGapAfter(0);
23060 craig 49
        m_tmpStyle.charStyle().setFont(PrefsManager::instance().appPrefs.fontPrefs.AvailFonts[m_Doc->itemToolPrefs().textFont]);
20691 craig 50
        m_tmpStyle.charStyle().setFontSize(m_Doc->itemToolPrefs().textSize);
51
        m_tmpStyle.setHasDropCap(false);
52
        m_tmpStyle.setHasBullet(false);
53
        m_tmpStyle.setHasNum(false);
54
        m_tmpStyle.setDropCapLines(0);//2;
55
        m_tmpStyle.setParEffectOffset(0);
56
        m_tmpStyle.charStyle().setFeatures(QStringList(CharStyle::INHERIT));
57
        m_tmpStyle.charStyle().setFillColor("__blackforpreview__");
24731 jghali 58
        m_tmpStyle.charStyle().setFillShade(100);
20691 craig 59
        m_tmpStyle.charStyle().setStrokeColor("__whiteforpreview__");
24731 jghali 60
        m_tmpStyle.charStyle().setStrokeShade(100);
20691 craig 61
        m_tmpStyle.charStyle().setShadowXOffset(50);
62
        m_tmpStyle.charStyle().setShadowYOffset(-50);
63
        m_tmpStyle.charStyle().setOutlineWidth(10);
24731 jghali 64
        m_tmpStyle.charStyle().setUnderlineOffset(0);
65
        m_tmpStyle.charStyle().setUnderlineWidth(0);
66
        m_tmpStyle.charStyle().setStrikethruOffset(0);
67
        m_tmpStyle.charStyle().setStrikethruWidth(0);
20691 craig 68
        m_tmpStyle.charStyle().setScaleH(1000);
69
        m_tmpStyle.charStyle().setScaleV(1000);
70
        m_tmpStyle.charStyle().setBaselineOffset(0);
71
        m_tmpStyle.charStyle().setTracking(0);
4070 subik 72
}
73
 
74
SampleItem::~SampleItem()
75
{
14807 fschmid 76
        delete m_Doc;
4070 subik 77
}
78
 
22607 craig 79
void SampleItem::setText(const QString& aText)
4070 subik 80
{
20691 craig 81
        m_text = aText;
4070 subik 82
}
83
 
84
void SampleItem::setLoremIpsum(int para)
85
{
24733 jghali 86
        std::unique_ptr<LoremParser> loremParser(new LoremParser("loremipsum.xml"));
24731 jghali 87
        m_text = loremParser->createLorem(para);
4070 subik 88
}
89
 
5698 avox 90
void SampleItem::setStyle(const ParagraphStyle& aStyle)
4070 subik 91
{
20691 craig 92
        m_tmpStyle = aStyle;
4070 subik 93
}
94
 
22607 craig 95
void SampleItem::setBgColor(const QColor& c)
4070 subik 96
{
5781 cbradney 97
        m_Doc->PageColors["__whiteforpreviewbg__"].fromQColor(c);
4070 subik 98
}
99
 
4614 fschmid 100
void SampleItem::setBgShade(int c)
101
{
20691 craig 102
        m_bgShade = c;
4614 fschmid 103
}
104
 
14491 jghali 105
void SampleItem::setBgColorMgmt(bool enable)
7054 jghali 106
{
107
        m_Doc->PageColors["__whiteforpreviewbg__"].setSpotColor(!enable);
108
}
109
 
22607 craig 110
void SampleItem::setTxColor(const QColor& c)
4614 fschmid 111
{
5781 cbradney 112
        m_Doc->PageColors["__blackforpreview__"].fromQColor(c);
4614 fschmid 113
}
114
 
115
void SampleItem::setTxShade(int c)
116
{
20691 craig 117
        m_tmpStyle.charStyle().setFillShade(c);
4614 fschmid 118
}
119
 
24731 jghali 120
void SampleItem::setLineSpacingMode(int lineSpaMode)
4070 subik 121
{
20691 craig 122
        m_tmpStyle.setLineSpacingMode(static_cast<ParagraphStyle::LineSpacingMode>(lineSpaMode));
4070 subik 123
}
124
 
24731 jghali 125
void SampleItem::setLineSpacing(double lineSpa)
4070 subik 126
{
20691 craig 127
        m_tmpStyle.setLineSpacing(lineSpa);
4070 subik 128
}
129
 
130
void SampleItem::setTextAlignment(int textAlignment)
131
{
20691 craig 132
        m_tmpStyle.setAlignment(static_cast<ParagraphStyle::AlignmentType>(textAlignment));
4070 subik 133
}
134
 
24731 jghali 135
void SampleItem::setLeftMargin(double indent)
4070 subik 136
{
20691 craig 137
        m_tmpStyle.setLeftMargin(indent);
4070 subik 138
}
139
 
24731 jghali 140
void SampleItem::setRightMargin(double indent)
4070 subik 141
{
24731 jghali 142
        m_tmpStyle.setRightMargin(indent);
143
}
144
 
145
void SampleItem::setFirstIndent(double first)
146
{
20691 craig 147
        m_tmpStyle.setFirstIndent(first);
4070 subik 148
}
149
 
150
void SampleItem::setGapBefore(double gapBefore)
151
{
20691 craig 152
        m_tmpStyle.setGapBefore(gapBefore);
4070 subik 153
}
154
 
155
void SampleItem::setGapAfter(double gapAfter)
156
{
20691 craig 157
        m_tmpStyle.setGapAfter(gapAfter);
4070 subik 158
}
159
 
22607 craig 160
void SampleItem::setFont(const QString& font)
4070 subik 161
{
23060 craig 162
        m_tmpStyle.charStyle().setFont(PrefsManager::instance().appPrefs.fontPrefs.AvailFonts[font]);
4070 subik 163
}
164
 
4077 subik 165
void SampleItem::setFontSize(int fontSize, bool autoLineSpa)
4070 subik 166
{
20691 craig 167
        m_tmpStyle.charStyle().setFontSize(fontSize);
4077 subik 168
        if (autoLineSpa)
20691 craig 169
                m_tmpStyle.setLineSpacing((fontSize / 10)  * (m_Doc->typographicPrefs().autoLineSpacing / 100.0));
4070 subik 170
}
171
 
172
/*void SampleItem::setTabValues(QValueList<PageItem::TabRecord> tabValues)
173
{
174
        tmpStyle.TabValues = tabValues;
175
}*/
176
 
177
void SampleItem::setDrop(bool drop)
178
{
20691 craig 179
        m_tmpStyle.setHasDropCap(drop);
4070 subik 180
}
181
 
24731 jghali 182
void SampleItem::setDropCapLines(int dropLin)
4070 subik 183
{
20691 craig 184
        m_tmpStyle.setDropCapLines(dropLin);
4070 subik 185
}
186
 
18047 craig 187
void SampleItem::setParEffectDist(double dist)
4070 subik 188
{
20691 craig 189
        m_tmpStyle.setParEffectOffset(dist);
4070 subik 190
}
191
 
18047 craig 192
void SampleItem::setBullet(bool bul)
193
{
20691 craig 194
        m_tmpStyle.setHasBullet(bul);
18047 craig 195
}
196
 
197
void SampleItem::setNum(bool num)
198
{
20691 craig 199
        m_tmpStyle.setHasNum(num);
18047 craig 200
}
201
 
4070 subik 202
void SampleItem::setFontEffect(int fontEffect)
203
{
20691 craig 204
        m_tmpStyle.charStyle().setFeatures(static_cast<StyleFlag>(fontEffect).featureList());
4070 subik 205
}
206
 
24731 jghali 207
void SampleItem::setTxtFillColor(const QString& fColor)
4070 subik 208
{
20691 craig 209
        m_tmpStyle.charStyle().setFillColor(fColor);
4070 subik 210
}
211
 
24732 jghali 212
void SampleItem::setTxtFillShade(double fShade)
4070 subik 213
{
20691 craig 214
        m_tmpStyle.charStyle().setFillShade(fShade);
4070 subik 215
}
216
 
24731 jghali 217
void SampleItem::setTxtStrokeColor(const QString& sColor)
4070 subik 218
{
20691 craig 219
        m_tmpStyle.charStyle().setStrokeColor(sColor);
4070 subik 220
}
221
 
24732 jghali 222
void SampleItem::setTxtStrokeShade(double sShade)
4070 subik 223
{
20691 craig 224
        m_tmpStyle.charStyle().setStrokeShade(sShade);
4070 subik 225
}
226
 
24732 jghali 227
void SampleItem::setTxtShadowX(double txtShadowX)
4070 subik 228
{
20691 craig 229
        m_tmpStyle.charStyle().setShadowXOffset(txtShadowX);
4070 subik 230
}
231
 
24732 jghali 232
void SampleItem::setTxtShadowY(double txtShadowY)
4070 subik 233
{
20691 craig 234
        m_tmpStyle.charStyle().setShadowYOffset(txtShadowY);
4070 subik 235
}
236
 
24732 jghali 237
void SampleItem::setTxtOutlineWidth(double txtOutline)
4070 subik 238
{
20691 craig 239
        m_tmpStyle.charStyle().setOutlineWidth(txtOutline);
4070 subik 240
}
241
 
24732 jghali 242
void SampleItem::setTxtUnderlineOffset(double txtUnderPos)
4070 subik 243
{
20691 craig 244
        m_tmpStyle.charStyle().setUnderlineOffset(txtUnderPos);
4070 subik 245
}
246
 
24732 jghali 247
void SampleItem::setTxtUnderlineWidth(double txtUnderWidth)
4070 subik 248
{
20691 craig 249
        m_tmpStyle.charStyle().setUnderlineWidth(txtUnderWidth);
4070 subik 250
}
251
 
24732 jghali 252
void SampleItem::setTxtStrikethruOffset(double txtStrikePos)
4070 subik 253
{
20691 craig 254
        m_tmpStyle.charStyle().setStrikethruOffset(txtStrikePos);
4070 subik 255
}
256
 
24732 jghali 257
void SampleItem::setTxtStrikethruWidth(double txtStrikeWidth)
4070 subik 258
{
20691 craig 259
        m_tmpStyle.charStyle().setStrikethruWidth(txtStrikeWidth);
4070 subik 260
}
261
 
24732 jghali 262
void SampleItem::setScaleH(double scaleH)
4070 subik 263
{
20691 craig 264
        m_tmpStyle.charStyle().setScaleH(scaleH);
4070 subik 265
}
266
 
24732 jghali 267
void SampleItem::setScaleV(double scaleV)
4070 subik 268
{
20691 craig 269
        m_tmpStyle.charStyle().setScaleV(scaleV);
4070 subik 270
}
271
 
24732 jghali 272
void SampleItem::setBaselineOffset(double baseOff)
4070 subik 273
{
20691 craig 274
        m_tmpStyle.charStyle().setBaselineOffset(baseOff);
4070 subik 275
}
276
 
24732 jghali 277
void SampleItem::setKernVal(double kernVal)
4070 subik 278
{
20691 craig 279
        m_tmpStyle.charStyle().setTracking(kernVal);
4070 subik 280
}
281
 
24732 jghali 282
void SampleItem::setWordTracking(double val)
283
{
284
        m_tmpStyle.charStyle().setWordTracking(val);
285
}
286
 
4070 subik 287
QPixmap SampleItem::getSample(int width, int height)
288
{
5470 subik 289
        // if it's false => the used font will be removed from used fonts
290
        // after sample creating
291
        bool previouslyUsedFont = false;
292
 
20691 craig 293
        if (m_tmpStyle.charStyle().font().isNone())
5315 avox 294
                return QPixmap();
14724 cbradney 295
 
24731 jghali 296
        UndoBlocker undoBlocker; // disable undo
4150 subik 297
 
24308 jghali 298
        int pmWidth = qRound(width * m_devicePixelRatio);
299
        int pmHeight = qRound(height * m_devicePixelRatio);
23060 craig 300
        double frameWidth  = width / PrefsManager::instance().appPrefs.displayPrefs.displayScale;
301
        double frameHeight = height / PrefsManager::instance().appPrefs.displayPrefs.displayScale;
21860 jghali 302
 
24308 jghali 303
        QImage pm(pmWidth, pmHeight, QImage::Format_ARGB32);
304
        pm.setDevicePixelRatio(m_devicePixelRatio);
305
 
24733 jghali 306
        std::unique_ptr<ScPainter> painter(new ScPainter(&pm, pmWidth, pmHeight, 1.0, 0));
23060 craig 307
        painter->setZoomFactor(PrefsManager::instance().appPrefs.displayPrefs.displayScale);
4070 subik 308
 
20691 craig 309
        if (m_Doc->UsedFonts.contains(m_tmpStyle.charStyle().font().scName()))
5470 subik 310
                previouslyUsedFont = true;
311
 
20691 craig 312
        m_Doc->AddFont(m_tmpStyle.charStyle().font().scName(), qRound(m_Doc->itemToolPrefs().textSize / 10.0));
4070 subik 313
 
24733 jghali 314
        std::unique_ptr<PageItem_TextFrame> previewItem(new PageItem_TextFrame(m_Doc, 0, 0, frameWidth, frameHeight, 0, "__whiteforpreviewbg__", "__whiteforpreview__"));
24731 jghali 315
        m_text.replace(QChar(10), QChar(13)).replace(QChar(5), QChar(13));
316
 
4070 subik 317
        previewItem->FrameType = PageItem::TextFrame;
318
        previewItem->itemText.clear();
22832 craig 319
        previewItem->m_columns = 1;
20691 craig 320
        previewItem->itemText.insertChars(0, m_text);
321
        previewItem->itemText.setDefaultStyle(m_tmpStyle);
4614 fschmid 322
        previewItem->setFillColor("__whiteforpreviewbg__");
20691 craig 323
        previewItem->setFillShade(m_bgShade);
4614 fschmid 324
        previewItem->SetRectFrame();
18007 craig 325
        previewItem->setSampleItem(true);
24731 jghali 326
        previewItem->DrawObj(painter.get(), QRect(0, 0, frameWidth, frameHeight));
4070 subik 327
        painter->end();
4374 subik 328
 
24731 jghali 329
        painter.reset();
330
        previewItem.reset();
331
 
5470 subik 332
        // cleanups and resets
333
        if (!previouslyUsedFont)
12993 jghali 334
        {
20691 craig 335
                QString fontName = m_tmpStyle.charStyle().font().scName();
12993 jghali 336
                (*m_Doc->AllFonts)[fontName].decreaseUsage(); // was increased by AddFont()
337
                m_Doc->UsedFonts.remove(fontName);
338
        }
24308 jghali 339
 
340
        QPixmap pixmap = QPixmap::fromImage(pm);
341
        return pixmap;
4070 subik 342
}