Back to index

scribus-ng  1.3.4.dfsg+svn20071115
sampleitem.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 #include "scribuscore.h"
00008 #include "sampleitem.h"
00009 #include "sampleitem.moc"
00010 #include "loremipsum.h"
00011 #include "scribusdoc.h"
00012 #include "undomanager.h"
00013 #include "commonstrings.h"
00014 #include "prefsmanager.h"
00015 #include <qcolor.h>
00016 #include <qstring.h>
00017 #include <qcursor.h>
00018 #include "text/nlsconfig.h"
00019 
00020 SampleItem::SampleItem(ScribusDoc* doc) :
00021        QObject()
00022 {
00023        used = true;
00024        m_Doc=doc;
00025 
00026        if (doc==0)
00027        {
00028               qApp->setOverrideCursor(QCursor(Qt::WaitCursor));
00029               // FIXME: main preformance issue here! PV
00030               m_Doc=ScCore->primaryMainWindow()->doFileNew(//pageWidth, pageHeight,
00031                                                                0,0,
00032                                                                //topMargin, leftMargin, rightMargin, bottomMargin,
00033                                                                1, 1, 1, 1,
00034                                                                // autoframes. It's disabled in python
00035                                                                // columnDistance, numberCols, autoframes,
00036                                                                0, 1, false,
00037                                                                //pagesType, unit, firstPageOrder,
00038                                                                1, 1, 1,
00039                                                                //orientation, firstPageNr, "Custom", requires gui, page count, showview);
00040                                                                1, 1, "custom", false, 1, false);
00041               //m_Doc = new ScribusDoc();
00042               Q_ASSERT(m_Doc!=0);
00043               if (!m_Doc)
00044                      return;
00045               m_Doc->pageSets[1/*pagesType*/].FirstPage = 1;//firstPageOrder;
00046               used = false;
00047               qApp->restoreOverrideCursor();
00048        }
00049        // tmp colors. to be removed in descrictor
00050        m_Doc->PageColors.insert("__blackforpreview__", ScColor(0, 0, 0, 255));
00051        m_Doc->PageColors.insert("__whiteforpreview__", ScColor(0, 0, 0, 0));
00052        m_Doc->PageColors.insert("__whiteforpreviewbg__", ScColor(0, 0, 0, 0));
00053        bgShade = 100;
00054        tmpStyle.setName("(preview temporary)");
00055        tmpStyle.setLineSpacingMode(ParagraphStyle::FixedLineSpacing);
00056        tmpStyle.setLineSpacing((m_Doc->toolSettings.defSize / 10.0)
00057               * static_cast<double>(m_Doc->typographicSettings.autoLineSpacing) / 100
00058               + (m_Doc->toolSettings.defSize / 10.0));
00059        tmpStyle.setAlignment(ParagraphStyle::Leftaligned);
00060        tmpStyle.setLeftMargin(0);
00061        tmpStyle.setFirstIndent(0);
00062        tmpStyle.setRightMargin(0);
00063        tmpStyle.setGapBefore(0);
00064        tmpStyle.setGapAfter(0);
00065        tmpStyle.charStyle().setFont(PrefsManager::instance()->appPrefs.AvailFonts[m_Doc->toolSettings.defFont]);
00066        tmpStyle.charStyle().setFontSize(m_Doc->toolSettings.defSize);
00067 //     tmpStyle.tabValues().clear();
00068        tmpStyle.setHasDropCap(false);
00069        tmpStyle.setDropCapLines(0);//2;
00070        tmpStyle.setDropCapOffset(0);
00071        tmpStyle.charStyle().setFeatures(QStringList(CharStyle::INHERIT));
00072        tmpStyle.charStyle().setFillColor("__blackforpreview__");
00073        tmpStyle.charStyle().setFillShade(100); //m_Doc->toolSettings.dShade;
00074        tmpStyle.charStyle().setStrokeColor("__whiteforpreview__");
00075        tmpStyle.charStyle().setStrokeShade(100); //m_Doc->toolSettings.dShade2;
00076        tmpStyle.charStyle().setShadowXOffset(50);
00077        tmpStyle.charStyle().setShadowYOffset(-50);
00078        tmpStyle.charStyle().setOutlineWidth(10);
00079        tmpStyle.charStyle().setUnderlineOffset(0); //m_Doc->typographicSettings.valueUnderlinePos;
00080        tmpStyle.charStyle().setUnderlineWidth(0); //m_Doc->typographicSettings.valueUnderlineWidth;
00081        tmpStyle.charStyle().setStrikethruOffset(0); //m_Doc->typographicSettings.valueStrikeThruPos;
00082        tmpStyle.charStyle().setStrikethruWidth(0); //m_Doc->typographicSettings.valueStrikeThruPos;
00083        tmpStyle.charStyle().setScaleH(1000);
00084        tmpStyle.charStyle().setScaleV(1000);
00085        tmpStyle.charStyle().setBaselineOffset(0);
00086        tmpStyle.charStyle().setTracking(0);
00087 }
00088 
00089 SampleItem::~SampleItem()
00090 {
00091        cleanupTemporary();
00092        // clean tmp document
00093        if (used == false)
00094        {
00095               m_Doc->setModified(false);
00096               //Do this manually as the sample item's doc was never put into the main windows workspace and doc list etc
00097               //m_Doc->scMW()->slotFileClose();
00098               m_Doc->view()->disconnect();
00099               m_Doc->WinHan->disconnect();
00100               m_Doc->disconnect();
00101               delete m_Doc->view();
00102               delete m_Doc->WinHan;
00103               delete m_Doc;
00104        }
00105 }
00106 
00107 void SampleItem::setText(QString aText)
00108 {
00109        text = aText;
00110 }
00111 
00112 void SampleItem::setLoremIpsum(int para)
00113 {
00114        LoremParser *m = new LoremParser("loremipsum.xml");
00115        text = m->createLorem(para);
00116        text = QString::fromUtf8(text);
00117        delete m;
00118 }
00119 
00120 void SampleItem::setStyle(const ParagraphStyle& aStyle)
00121 {
00122        tmpStyle = aStyle;
00123 }
00124 
00125 void SampleItem::setBgColor(QColor c)
00126 {
00127        m_Doc->PageColors["__whiteforpreviewbg__"].fromQColor(c);
00128 }
00129 
00130 void SampleItem::setBgShade(int c)
00131 {
00132        bgShade = c;
00133 }
00134 
00135 void SampleItem::setBgColorMngt(bool enable)
00136 {
00137        m_Doc->PageColors["__whiteforpreviewbg__"].setSpotColor(!enable);
00138 }
00139 
00140 void SampleItem::setTxColor(QColor c)
00141 {
00142        m_Doc->PageColors["__blackforpreview__"].fromQColor(c);
00143 }
00144 
00145 void SampleItem::setTxShade(int c)
00146 {
00147        tmpStyle.charStyle().setFillShade(c);
00148 }
00149 
00150 void SampleItem::setLineSpaMode(int lineSpaMode)
00151 {
00152        tmpStyle.setLineSpacingMode(static_cast<ParagraphStyle::LineSpacingMode>(lineSpaMode));
00153 }
00154 
00155 void SampleItem::setLineSpa(double lineSpa)
00156 {
00157        tmpStyle.setLineSpacing(lineSpa);
00158 }
00159 
00160 void SampleItem::setTextAlignment(int textAlignment)
00161 {
00162        tmpStyle.setAlignment(static_cast<ParagraphStyle::AlignmentType>(textAlignment));
00163 }
00164 
00165 void SampleItem::setIndent(double indent)
00166 {
00167        tmpStyle.setLeftMargin(indent);
00168 }
00169 
00170 void SampleItem::setFirst(double first)
00171 {
00172        tmpStyle.setFirstIndent(first);
00173 }
00174 
00175 void SampleItem::setGapBefore(double gapBefore)
00176 {
00177        tmpStyle.setGapBefore(gapBefore);
00178 }
00179 
00180 void SampleItem::setGapAfter(double gapAfter)
00181 {
00182        tmpStyle.setGapAfter(gapAfter);
00183 }
00184 
00185 void SampleItem::setFont(QString font)
00186 {
00187        tmpStyle.charStyle().setFont(PrefsManager::instance()->appPrefs.AvailFonts[font]);
00188 }
00189 
00190 void SampleItem::setFontSize(int fontSize, bool autoLineSpa)
00191 {
00192        tmpStyle.charStyle().setFontSize(fontSize);
00193        if (autoLineSpa)
00194               tmpStyle.setLineSpacing(((fontSize / 10)  * (m_Doc->typographicSettings.autoLineSpacing / 100) + (fontSize / 10)));
00195 }
00196 
00197 /*void SampleItem::setTabValues(QValueList<PageItem::TabRecord> tabValues)
00198 {
00199        tmpStyle.TabValues = tabValues;
00200 }*/
00201 
00202 void SampleItem::setDrop(bool drop)
00203 {
00204        tmpStyle.setHasDropCap(drop);
00205 }
00206 
00207 void SampleItem::setDropLin(int dropLin)
00208 {
00209        tmpStyle.setDropCapLines(dropLin);
00210 }
00211 
00212 void SampleItem::setDropDist(double dropDist)
00213 {
00214        tmpStyle.setDropCapOffset(dropDist);
00215 }
00216 
00217 void SampleItem::setFontEffect(int fontEffect)
00218 {
00219        tmpStyle.charStyle().setFeatures(static_cast<StyleFlag>(fontEffect).featureList());
00220 }
00221 
00222 void SampleItem::setFColor(QString fColor)
00223 {
00224        tmpStyle.charStyle().setFillColor(fColor);
00225 }
00226 
00227 void SampleItem::setFShade(int fShade)
00228 {
00229        tmpStyle.charStyle().setFillShade(fShade);
00230 }
00231 
00232 void SampleItem::setSColor(QString sColor)
00233 {
00234        tmpStyle.charStyle().setStrokeColor(sColor);
00235 }
00236 
00237 void SampleItem::setSShade(int sShade)
00238 {
00239        tmpStyle.charStyle().setStrokeShade(sShade);
00240 }
00241 
00242 void SampleItem::setBaseAdj(bool baseAdj)
00243 {
00244        tmpStyle.setLineSpacingMode(baseAdj? ParagraphStyle::BaselineGridLineSpacing : ParagraphStyle::FixedLineSpacing);
00245 }
00246 
00247 void SampleItem::setTxtShadowX(int txtShadowX)
00248 {
00249        tmpStyle.charStyle().setShadowXOffset(txtShadowX);
00250 }
00251 
00252 void SampleItem::setTxtShadowY(int txtShadowY)
00253 {
00254        tmpStyle.charStyle().setShadowYOffset(txtShadowY);
00255 }
00256 
00257 void SampleItem::setTxtOutline(int txtOutline)
00258 {
00259        tmpStyle.charStyle().setOutlineWidth(txtOutline);
00260 }
00261 
00262 void SampleItem::setTxtUnderPos(int txtUnderPos)
00263 {
00264        tmpStyle.charStyle().setUnderlineOffset(txtUnderPos);
00265 }
00266 
00267 void SampleItem::setTxtUnderWidth(int txtUnderWidth)
00268 {
00269        tmpStyle.charStyle().setUnderlineWidth(txtUnderWidth);
00270 }
00271 
00272 void SampleItem::setTxtStrikePos(int txtStrikePos)
00273 {
00274        tmpStyle.charStyle().setStrikethruOffset(txtStrikePos);
00275 }
00276 
00277 void SampleItem::setTxtStrikeWidth(int txtStrikeWidth)
00278 {
00279        tmpStyle.charStyle().setStrikethruWidth(txtStrikeWidth);
00280 }
00281 
00282 void SampleItem::setScaleH(int scaleH)
00283 {
00284        tmpStyle.charStyle().setScaleH(scaleH);
00285 }
00286 
00287 void SampleItem::setScaleV(int scaleV)
00288 {
00289        tmpStyle.charStyle().setScaleV(scaleV);
00290 }
00291 
00292 void SampleItem::setBaseOff(int baseOff)
00293 {
00294        tmpStyle.charStyle().setBaselineOffset(baseOff);
00295 }
00296 
00297 void SampleItem::setKernVal(int kernVal)
00298 {
00299        tmpStyle.charStyle().setTracking(kernVal);
00300 }
00301 
00302 QPixmap SampleItem::getSample(int width, int height)
00303 {
00304        // if it's false => the used font will be removed from used fonts
00305        // after sample creating
00306        bool previouslyUsedFont = false;
00307 
00308        if (tmpStyle.charStyle().font().isNone())
00309               return QPixmap();
00310 
00311        UndoManager::instance()->setUndoEnabled(false); // disable undo
00312 
00313        PageItem_TextFrame *previewItem = new PageItem_TextFrame(m_Doc, 0, 0, width, height, 0, "__whiteforpreviewbg__", "__whiteforpreview__");
00314        QPixmap pm(width, height);
00315        ScPainter *painter = new ScPainter(&pm, width, height, 0, 0);
00316        double sca = 1.0; // original scale to set back at the end...
00317        int userAppMode = m_Doc->appMode; // We need to be in normal when creating/repainting items
00318        m_Doc->appMode = modeNormal;
00319 
00320        if (m_Doc->view() != NULL)
00321        {
00322               sca = m_Doc->view()->scale();
00323               m_Doc->view()->setScale(1.0 * PrefsManager::instance()->appPrefs.DisScale);
00324        }
00325 #ifdef HAVE_CAIRO
00326        painter->setZoomFactor(m_Doc->view()->scale());
00327 #endif
00328 
00329        if (m_Doc->UsedFonts.contains(tmpStyle.charStyle().font().scName()))
00330               previouslyUsedFont = true;
00331 
00332        m_Doc->AddFont(tmpStyle.charStyle().font().scName(), qRound(m_Doc->toolSettings.defSize / 10.0));
00333 //     m_Doc->docParagraphStyles.create(tmpStyle);
00334 //     int tmpIndex = m_Doc->docParagraphStyles.count() - 1;
00335 
00336        previewItem->FrameType = PageItem::TextFrame;
00337        previewItem->itemText.clear();
00338 //     previewItem->setFont(tmpStyle.charStyle().font()->scName());
00339        previewItem->Cols = 1;
00340        text.replace(QChar(10),QChar(13)).replace(QChar(5),QChar(13));
00341        previewItem->itemText.insertChars(0, text);
00342        previewItem->itemText.setDefaultStyle(tmpStyle);
00343        previewItem->setFillColor("__whiteforpreviewbg__");
00344        previewItem->setFillShade(bgShade);
00345        previewItem->SetRectFrame();
00346        previewItem->Frame = false;
00347        previewItem->DrawObj(painter, QRect(0, 0, width, height));
00348        painter->end();
00349        delete(painter);
00350        delete previewItem;
00351 
00352        // cleanups and resets
00353        if (!previouslyUsedFont)
00354               m_Doc->UsedFonts.remove(tmpStyle.charStyle().font().scName());
00355        if (m_Doc->view() != NULL)
00356               m_Doc->view()->setScale(sca);
00357        m_Doc->appMode = userAppMode;
00358 //     m_Doc->docParagraphStyles.remove(tmpIndex);
00359        UndoManager::instance()->setUndoEnabled(true);
00360        return pm;
00361 }
00362 
00363 void SampleItem::cleanupTemporary()
00364 {
00365        // clear tmp colors
00366        m_Doc->PageColors.remove("__blackforpreview__");
00367        m_Doc->PageColors.remove("__whiteforpreview__");
00368        m_Doc->PageColors.remove("__whiteforpreviewbg__");
00369 }