]> git.sesse.net Git - kdenlive/blob - src/titlewidget.cpp
Changes on titler:
[kdenlive] / src / titlewidget.cpp
1 /***************************************************************************
2                           titlewidget.cpp  -  description
3                              -------------------
4     begin                : Feb 28 2008
5     copyright            : (C) 2008 by Marco Gittler
6     email                : g.marco@freenet.de
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18 #include "titlewidget.h"
19 #include "kdenlivesettings.h"
20
21 #include <KDebug>
22 #include <KGlobalSettings>
23 #include <KFileDialog>
24 #include <KStandardDirs>
25 #include <KMessageBox>
26
27 #include <QDomDocument>
28 #include <QGraphicsItem>
29 #include <QGraphicsSvgItem>
30 #include <QTimer>
31 #include <QToolBar>
32 #include <QMenu>
33 #include <QTextBlockFormat>
34 #include <QTextCursor>
35
36 int settingUp = false;
37
38 const int IMAGEITEM = 7;
39 const int RECTITEM = 3;
40 const int TEXTITEM = 8;
41
42 TitleWidget::TitleWidget(KUrl url, QString projectTitlePath, Render *render, QWidget *parent) :
43         QDialog(parent),
44         Ui::TitleWidget_UI(),
45         m_startViewport(NULL),
46         m_endViewport(NULL),
47         m_render(render),
48         m_count(0),
49         m_unicodeDialog(new UnicodeDialog(UnicodeDialog::InputHex)),
50         m_projectTitlePath(projectTitlePath)
51 {
52     setupUi(this);
53     setFont(KGlobalSettings::toolBarFont());
54     //toolBox->setFont(KGlobalSettings::toolBarFont());
55     frame_properties->setEnabled(false);
56     rect_properties->setFixedHeight(frame_properties->height() + 4);
57     text_properties->setFixedHeight(frame_properties->height() + 4);
58     m_frameWidth = render->renderWidth();
59     m_frameHeight = render->renderHeight();
60     connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
61     connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
62
63
64     connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
65     connect(font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(slotUpdateText())) ;
66     connect(font_size, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText())) ;
67     connect(textAlpha, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText()));
68
69     connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
70     connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
71     connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
72     connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
73     connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
74
75     connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
76     connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
77     connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
78     connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
79     connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
80     connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
81
82     connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
83     connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
84     connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
85     connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
86     connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
87
88     connect(origin_x_left, SIGNAL(clicked()), this, SLOT(slotOriginXClicked()));
89     connect(origin_y_top, SIGNAL(clicked()), this, SLOT(slotOriginYClicked()));
90
91     connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
92     connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
93     connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
94     connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
95     connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
96     connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
97     connect(buttonBold, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
98     connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
99     connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
100     connect(buttonAlignLeft, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
101     connect(buttonAlignRight, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
102     connect(buttonAlignCenter, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
103     connect(buttonAlignNone, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
104     connect(buttonInsertUnicode, SIGNAL(clicked()), this, SLOT(slotInsertUnicode()));
105     connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
106
107     connect(m_unicodeDialog, SIGNAL(charSelected(QString)), this, SLOT(slotInsertUnicodeString(QString)));
108
109     // mbd
110     connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
111
112     buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
113     buttonRealSize->setIcon(KIcon("zoom-original"));
114     buttonBold->setIcon(KIcon("format-text-bold"));
115     buttonItalic->setIcon(KIcon("format-text-italic"));
116     buttonUnder->setIcon(KIcon("format-text-underline"));
117     buttonAlignCenter->setIcon(KIcon("format-justify-center"));
118     buttonAlignLeft->setIcon(KIcon("format-justify-left"));
119     buttonAlignRight->setIcon(KIcon("format-justify-right"));
120     buttonAlignNone->setIcon(KIcon("kdenlive-align-none"));
121     buttonInsertUnicode->setIcon(KIcon("kdenlive-insert-unicode"));
122
123     buttonAlignNone->setToolTip(i18n("No alignment"));
124     buttonAlignRight->setToolTip(i18n("Align right"));
125     buttonAlignLeft->setToolTip(i18n("Align left"));
126     buttonAlignCenter->setToolTip(i18n("Align center"));
127     buttonInsertUnicode->setToolTip(i18n("Insert Unicode character (Shift+Ctrl+U)"));
128     buttonInsertUnicode->setShortcut(Qt::SHIFT + Qt::CTRL + Qt::Key_U);
129     origin_x_left->setToolTip(i18n("Invert x axis and change 0 point"));
130     origin_y_top->setToolTip(i18n("Invert y axis and change 0 point"));
131     rectBColor->setToolTip(i18n("Select fill color"));
132     rectFColor->setToolTip(i18n("Select border color"));
133     rectBAlpha->setToolTip(i18n("Fill transparency"));
134     rectFAlpha->setToolTip(i18n("Border transparency"));
135     zoom_slider->setToolTip(i18n("Zoom"));
136     buttonRealSize->setToolTip(i18n("Original size (1:1)"));
137     buttonFitZoom->setToolTip(i18n("Fit zoom"));
138     kcolorbutton->setToolTip(i18n("Select background color"));
139     horizontalSlider->setToolTip(i18n("Background Transparency"));
140
141     itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
142     itemhcenter->setToolTip(i18n("Align item horizontally"));
143     itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
144     itemvcenter->setToolTip(i18n("Align item vertically"));
145
146     QHBoxLayout *layout = new QHBoxLayout;
147     frame_toolbar->setLayout(layout);
148     layout->setContentsMargins(2, 2, 2, 2);
149     QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
150
151     m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), i18n("Add Rectangle (Alt+R)"));
152     m_buttonRect->setCheckable(true);
153     m_buttonRect->setShortcut(Qt::ALT + Qt::Key_R);
154     connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
155
156     m_buttonText = m_toolbar->addAction(KIcon("insert-text"), i18n("Add Text (Alt+T)"));
157     m_buttonText->setCheckable(true);
158     m_buttonText->setShortcut(Qt::ALT + Qt::Key_T);
159     connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
160
161     m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), i18n("Add Image (Alt+I)"));
162     m_buttonImage->setCheckable(false);
163     m_buttonImage->setShortcut(Qt::ALT + Qt::Key_I);
164     connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
165
166     m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), i18n("Selection Tool (Alt+S)"));
167     m_buttonCursor->setCheckable(true);
168     m_buttonCursor->setShortcut(Qt::ALT + Qt::Key_S);
169     connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
170         
171         m_toolbar->addSeparator();
172
173     m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
174     m_buttonLoad->setCheckable(false);
175     m_buttonLoad->setShortcut(Qt::CTRL + Qt::Key_O);
176     connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
177
178     m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
179     m_buttonSave->setCheckable(false);
180     m_buttonSave->setShortcut(Qt::CTRL + Qt::Key_S);
181     connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
182
183     layout->addWidget(m_toolbar);
184
185     // initialize graphic scene
186     m_scene = new GraphicsSceneRectMove(this);
187     graphicsView->setScene(m_scene);
188     m_titledocument.setScene(m_scene);
189     connect(m_scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(slotChanged()));
190
191     // a gradient background
192     /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
193     gradient->setSpread(QGradient::ReflectSpread);
194     scene->setBackgroundBrush(*gradient);*/
195
196     m_frameImage = new QGraphicsPixmapItem();
197     QTransform qtrans;
198     qtrans.scale(2.0, 2.0);
199     m_frameImage->setTransform(qtrans);
200     m_frameImage->setZValue(-1200);
201     m_frameImage->setFlags(QGraphicsItem::ItemClipsToShape);
202     displayBackgroundFrame();
203     graphicsView->scene()->addItem(m_frameImage);
204
205     connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
206     connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
207     connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
208     connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
209     //connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
210     connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
211     connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
212     connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
213
214     QPen framepen(Qt::DotLine);
215     framepen.setColor(Qt::red);
216
217     m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
218     m_frameBorder->setPen(framepen);
219     m_frameBorder->setZValue(-1100);
220     m_frameBorder->setBrush(Qt::transparent);
221     m_frameBorder->setFlags(QGraphicsItem::ItemClipsToShape);
222     graphicsView->scene()->addItem(m_frameBorder);
223
224     // mbd: load saved settings
225     readChoices();
226
227     initViewports();
228     QTimer::singleShot(500, this, SLOT(slotAdjustZoom()));
229     graphicsView->show();
230     //graphicsView->setRenderHint(QPainter::Antialiasing);
231     graphicsView->setInteractive(true);
232     //graphicsView->resize(400, 300);
233     kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
234     toolBox->setItemEnabled(2, false);
235     if (!url.isEmpty()) {
236         m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
237         slotSelectTool();
238     } else {
239         slotRectTool();
240     }
241         
242         showToolbars(TITLE_NONE);
243 }
244
245 TitleWidget::~TitleWidget()
246 {
247     delete m_buttonRect;
248     delete m_buttonText;
249     delete m_buttonImage;
250     delete m_buttonCursor;
251     delete m_buttonSave;
252     delete m_buttonLoad;
253
254     delete m_unicodeDialog;
255     delete m_frameBorder;
256     delete m_frameImage;
257     delete m_startViewport;
258     delete m_endViewport;
259     delete m_scene;
260 }
261
262 //static
263 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl, bool isClone)
264 {
265     QStringList result;
266     QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
267     KStandardDirs::makeDir(titlePath);
268     titlePath.append((isClone == false) ? "title" : "clone");
269     int counter = 0;
270     QString path;
271     while (path.isEmpty() || QFile::exists(path)) {
272         counter++;
273         path = titlePath + QString::number(counter).rightJustified(3, '0', false) + ".png";
274     }
275     result.append(((isClone == false) ? i18n("Title") : i18n("Clone")) + ' ' + QString::number(counter).rightJustified(3, '0', false));
276     result.append(path);
277     return result;
278 }
279
280 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
281 {
282     QStringList result;
283     QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
284     KStandardDirs::makeDir(titlePath);
285     return titlePath + titleName + ".png";
286 }
287
288 //virtual
289 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
290 {
291     //slotAdjustZoom();
292 }
293
294 void TitleWidget::slotTextTool()
295 {
296     m_scene->setTool(TITLE_TEXT);
297         showToolbars(TITLE_TEXT);
298     m_buttonRect->setChecked(false);
299     m_buttonCursor->setChecked(false);
300 }
301
302 void TitleWidget::slotRectTool()
303 {
304     m_scene->setTool(TITLE_RECTANGLE);
305         showToolbars(TITLE_RECTANGLE);
306     m_buttonText->setChecked(false);
307     m_buttonCursor->setChecked(false);
308     m_buttonRect->setChecked(true);
309 }
310
311 void TitleWidget::slotSelectTool()
312 {
313     m_scene->setTool(TITLE_SELECT);
314         
315         // Find out which toolbars need to be shown, depending on selected item
316         TITLETOOL t = TITLE_SELECT;
317         QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
318         if (l.size() > 0) {
319                 switch (l.at(0)->type()) {
320                         case TEXTITEM:
321                                 t = TITLE_TEXT;
322                                 break;
323                         case RECTITEM:
324                                 t = TITLE_RECTANGLE;
325                                 break;
326                         case IMAGEITEM:
327                                 t = TITLE_IMAGE;
328                                 break;
329                 }
330         }
331         enableToolbars(t);
332         showToolbars(t);
333         
334     m_buttonCursor->setChecked(true);
335     m_buttonText->setChecked(false);
336     m_buttonRect->setChecked(false);
337 }
338
339 void TitleWidget::slotImageTool()
340 {
341     KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.svg *.png *.jpg *.jpeg *.gif *.raw", this, i18n("Load Image"));
342     if (!url.isEmpty()) {
343         if (url.path().endsWith(".svg")) {
344             QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
345             svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
346             svg->setZValue(m_count++);
347             svg->setData(Qt::UserRole, url.path());
348             graphicsView->scene()->addItem(svg);
349         } else {
350             QPixmap pix(url.path());
351             QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
352             image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
353             image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
354             image->setData(Qt::UserRole, url.path());
355             image->setZValue(m_count++);
356             graphicsView->scene()->addItem(image);
357         }
358     }
359     m_scene->setTool(TITLE_SELECT);
360         showToolbars(TITLE_SELECT);
361     m_buttonRect->setChecked(false);
362     m_buttonCursor->setChecked(true);
363     m_buttonText->setChecked(false);
364 }
365
366 void TitleWidget::showToolbars(TITLETOOL toolType)
367 {
368         bool bText = false;
369         bool bRect = false;
370         bool bNone = false;
371         
372         switch (toolType) {
373                 case TITLE_TEXT:
374                         bText = true;
375                         break;
376                 case TITLE_RECTANGLE:
377                         bRect = true;
378                         break;
379                 case TITLE_IMAGE: //fall through
380                 default:
381                         bNone = true;
382                         break;
383         }
384         text_properties->setHidden(!bText);
385         rect_properties->setHidden(!bRect);
386         no_properties->setHidden(!bNone);
387 }
388
389 void TitleWidget::enableToolbars(TITLETOOL toolType)
390 {
391         // TITLETOOL is defined in graphicsscenerectmove.h
392         bool bFrame = false;
393         bool bText = false;
394         bool bRect = false;
395         bool bValue_w = false;
396         bool bValue_h = false;
397         
398         switch (toolType) {
399                 case TITLE_SELECT:
400                         break;
401                 case TITLE_TEXT:
402                         bFrame = true;
403                         bText = true;
404                         break;
405                 case TITLE_RECTANGLE:
406                         bFrame = true;
407                         bRect = true;
408                         bValue_w = true;
409                         bValue_h = true;
410                         break;
411                 case TITLE_IMAGE:
412                         bFrame = true;
413                         break;
414                 default:
415                         break;
416         }
417         frame_properties->setEnabled(bFrame);
418         text_properties->setEnabled(bText);
419         rect_properties->setEnabled(bRect);
420         value_w->setEnabled(bValue_w);
421         value_h->setEnabled(bValue_h);
422 }
423
424 void TitleWidget::displayBackgroundFrame()
425 {
426     if (!displayBg->isChecked()) {
427         QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
428         QPixmap pattern(20, 20);
429         pattern.fill();
430         QPainter p;
431         p.begin(&pattern);
432         p.fillRect(QRect(0, 0, 10, 10), QColor(210, 210, 210));
433         p.fillRect(QRect(10, 10, 20, 20), QColor(210, 210, 210));
434         p.end();
435         QBrush br(pattern);
436
437         p.begin(&bg);
438         p.fillRect(bg.rect(), br);
439         p.end();
440         m_frameImage->setPixmap(bg);
441     } else {
442         m_frameImage->setPixmap(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2));
443     }
444 }
445
446 void TitleWidget::initViewports()
447 {
448     m_startViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
449     m_endViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
450
451     QPen startpen(Qt::DotLine);
452     QPen endpen(Qt::DashDotLine);
453     startpen.setColor(QColor(100, 200, 100, 140));
454     endpen.setColor(QColor(200, 100, 100, 140));
455
456     m_startViewport->setPen(startpen);
457     m_endViewport->setPen(endpen);
458
459     startViewportSize->setValue(40);
460     endViewportSize->setValue(40);
461
462     m_startViewport->setZValue(-1000);
463     m_endViewport->setZValue(-1000);
464
465     m_startViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
466     m_endViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
467
468     graphicsView->scene()->addItem(m_startViewport);
469     graphicsView->scene()->addItem(m_endViewport);
470 }
471
472 void TitleWidget::slotUpdateZoom(int pos)
473 {
474     m_scene->setZoom((double) pos / 100);
475     zoom_label->setText(QString::number(pos) + '%');
476 }
477
478 void TitleWidget::slotZoom(bool up)
479 {
480     int pos = zoom_slider->value();
481     if (up) pos++;
482     else pos--;
483     zoom_slider->setValue(pos);
484 }
485
486 void TitleWidget::slotAdjustZoom()
487 {
488     /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
489     double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
490     if (scalex > scaley) scalex = scaley;
491     int zoompos = (int)(scalex * 7 + 0.5);*/
492     graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
493     int zoompos = graphicsView->matrix().m11() * 100;
494     zoom_slider->setValue(zoompos);
495     graphicsView->centerOn(m_frameBorder);
496 }
497
498 void TitleWidget::slotZoomOneToOne()
499 {
500     zoom_slider->setValue(100);
501     graphicsView->centerOn(m_frameBorder);
502 }
503
504 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
505 {
506     QColor f = rectFColor->color();
507     f.setAlpha(rectFAlpha->value());
508     QPen penf(f);
509     penf.setWidth(rectLineWidth->value());
510     rect->setPen(penf);
511     QColor b = rectBColor->color();
512     b.setAlpha(rectBAlpha->value());
513     rect->setBrush(QBrush(b));
514     rect->setZValue(m_count++);
515     //setCurrentItem(rect);
516     //graphicsView->setFocus();
517 }
518
519 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
520 {
521     QFont font = font_family->currentFont();
522     font.setPixelSize(font_size->value());
523     // mbd: issue 551:
524     font.setBold(buttonBold->isChecked());
525     font.setItalic(buttonItalic->isChecked());
526     font.setUnderline(buttonUnder->isChecked());
527
528     tt->setFont(font);
529     QColor color = fontColorButton->color();
530     color.setAlpha(textAlpha->value());
531     tt->setDefaultTextColor(color);
532     tt->setZValue(m_count++);
533     setCurrentItem(tt);
534 }
535
536 void TitleWidget::setCurrentItem(QGraphicsItem *item)
537 {
538     m_scene->setSelectedItem(item);
539 }
540
541 void TitleWidget::zIndexChanged(int v)
542 {
543     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
544     if (l.size() >= 1) {
545         l[0]->setZValue(v);
546     }
547 }
548
549 void TitleWidget::selectionChanged()
550 {
551     if (m_scene->tool() != TITLE_SELECT) return;
552     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
553     //toolBox->setItemEnabled(2, false);
554     //toolBox->setItemEnabled(3, false);
555     value_x->blockSignals(true);
556     value_y->blockSignals(true);
557     value_w->blockSignals(true);
558     value_h->blockSignals(true);
559     itemzoom->blockSignals(true);
560     itemrotate->blockSignals(true);
561     if (l.size() == 0) {
562         bool blockX = !origin_x_left->signalsBlocked();
563         bool blockY = !origin_y_top->signalsBlocked();
564         if (blockX) origin_x_left->blockSignals(true);
565         if (blockY) origin_y_top->blockSignals(true);
566         origin_x_left->setChecked(false);
567         origin_y_top->setChecked(false);
568         updateTextOriginX();
569         updateTextOriginY();
570                 enableToolbars(TITLE_NONE);
571         if (blockX) origin_x_left->blockSignals(false);
572         if (blockY) origin_y_top->blockSignals(false);
573     } else if (l.size() == 1) {
574         if (l.at(0)->type() == TEXTITEM) {
575                         showToolbars(TITLE_TEXT);
576             QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
577             //if (l[0]->hasFocus())
578             toolBox->setCurrentIndex(0);
579             //toolBox->setItemEnabled(2, true);
580             font_size->blockSignals(true);
581             font_family->blockSignals(true);
582             buttonBold->blockSignals(true);
583             buttonItalic->blockSignals(true);
584             buttonUnder->blockSignals(true);
585             fontColorButton->blockSignals(true);
586             textAlpha->blockSignals(true);
587             buttonAlignLeft->blockSignals(true);
588             buttonAlignRight->blockSignals(true);
589             buttonAlignNone->blockSignals(true);
590             buttonAlignCenter->blockSignals(true);
591
592             QFont font = i->font();
593             font_family->setCurrentFont(font);
594             font_size->setValue(font.pixelSize());
595             buttonBold->setChecked(font.bold());
596             buttonItalic->setChecked(font.italic());
597             buttonUnder->setChecked(font.underline());
598
599             QColor color = i->defaultTextColor();
600             fontColorButton->setColor(color);
601             textAlpha->setValue(color.alpha());
602
603             QTextCursor cur = i->textCursor();
604             QTextBlockFormat format = cur.blockFormat();
605             if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
606             else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
607             else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
608             else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
609
610             font_size->blockSignals(false);
611             font_family->blockSignals(false);
612             buttonBold->blockSignals(false);
613             buttonItalic->blockSignals(false);
614             buttonUnder->blockSignals(false);
615             fontColorButton->blockSignals(false);
616             textAlpha->blockSignals(false);
617             buttonAlignLeft->blockSignals(false);
618             buttonAlignRight->blockSignals(false);
619             buttonAlignNone->blockSignals(false);
620             buttonAlignCenter->blockSignals(false);
621
622             updateAxisButtons(i);
623             updateCoordinates(i);
624             updateDimension(i);
625                         enableToolbars(TITLE_TEXT);
626
627         } else if ((l.at(0))->type() == RECTITEM) {
628                         showToolbars(TITLE_RECTANGLE);
629             settingUp = true;
630             QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
631             toolBox->setCurrentIndex(0);
632             //toolBox->setItemEnabled(3, true);
633             rectFAlpha->setValue(rec->pen().color().alpha());
634             rectBAlpha->setValue(rec->brush().color().alpha());
635             //kDebug() << rec->brush().color().alpha();
636             QColor fcol = rec->pen().color();
637             QColor bcol = rec->brush().color();
638             //fcol.setAlpha(255);
639             //bcol.setAlpha(255);
640             rectFColor->setColor(fcol);
641             rectBColor->setColor(bcol);
642             settingUp = false;
643             rectLineWidth->setValue(rec->pen().width());
644
645             updateAxisButtons(l.at(0));
646             updateCoordinates(rec);
647             updateDimension(rec);
648                         enableToolbars(TITLE_RECTANGLE);
649
650         } else if (l.at(0)->type() == IMAGEITEM) {
651             updateCoordinates(l.at(0));
652             updateDimension(l.at(0));
653
654                         enableToolbars(TITLE_IMAGE);
655
656         } else {
657             //toolBox->setCurrentIndex(0);
658                         enableToolbars(TITLE_NONE);
659             /*frame_properties->setEnabled(false);
660             text_properties->setEnabled(false);
661             rect_properties->setEnabled(false);*/
662         }
663         zValue->setValue((int)l.at(0)->zValue());
664         itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
665         itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
666         value_x->blockSignals(false);
667         value_y->blockSignals(false);
668         value_w->blockSignals(false);
669         value_h->blockSignals(false);
670         itemzoom->blockSignals(false);
671         itemrotate->blockSignals(false);
672     }
673 }
674
675 /** \brief Updates position/size of the selected item when a value
676  * of an item (coordinates, size) has changed */
677 void TitleWidget::slotAdjustSelectedItem()
678 {
679     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
680     if (l.size() >= 1) {
681         if (l.at(0)->type() == RECTITEM) {
682             //rect item
683             QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
684             updatePosition(rec);
685             rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
686         } else if (l.at(0)->type() == TEXTITEM) {
687             //text item
688             updatePosition(l.at(0));
689         } else if (l.at(0)->type() == IMAGEITEM) {
690             //image item
691             updatePosition(l.at(0));
692         }
693     }
694 }
695
696 /** \brief Updates width/height int the text fields, regarding transformation matrix */
697 void TitleWidget::updateDimension(QGraphicsItem *i)
698 {
699     bool blockW = !value_w->signalsBlocked();
700     bool blockH = !value_h->signalsBlocked();
701
702     if (blockW) value_w->blockSignals(true);
703     if (blockH) value_h->blockSignals(true);
704
705
706     if (i->type() == IMAGEITEM) {
707         // Get multipliers for rotation/scaling
708
709         /*Transform t = m_transformations.value(i);
710         QRectF r = i->boundingRect();
711         int width = (int) ( abs(r.width()*t.scalex * cos(t.rotate/180.0*M_PI))
712                     + abs(r.height()*t.scaley * sin(t.rotate/180.0*M_PI)) );
713         int height = (int) ( abs(r.height()*t.scaley * cos(t.rotate/180*M_PI))
714                     + abs(r.width()*t.scalex * sin(t.rotate/180*M_PI)) );*/
715
716         value_w->setValue(i->sceneBoundingRect().width());
717         value_h->setValue(i->sceneBoundingRect().height());
718     } else if (i->type() == RECTITEM) {
719         QGraphicsRectItem *r = static_cast <QGraphicsRectItem *>(i);
720         value_w->setValue((int) r->rect().width());
721         value_h->setValue((int) r->rect().height());
722     } else if (i->type() == TEXTITEM) {
723         QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(i);
724         value_w->setValue((int) t->boundingRect().width());
725         value_h->setValue((int) t->boundingRect().height());
726     }
727
728     if (blockW) value_w->blockSignals(false);
729     if (blockH) value_h->blockSignals(false);
730 }
731
732 /** \brief Updates the coordinates in the text fields from the item */
733 void TitleWidget::updateCoordinates(QGraphicsItem *i)
734 {
735     // Block signals emitted by this method
736     value_x->blockSignals(true);
737     value_y->blockSignals(true);
738
739     if (i->type() == TEXTITEM) {
740
741         QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
742
743         // Set the correct x coordinate value
744         if (origin_x_left->isChecked()) {
745             // Origin (0 point) is at m_frameWidth, coordinate axis is inverted
746             value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->boundingRect().width()));
747         } else {
748             // Origin is at 0 (default)
749             value_x->setValue((int) rec->pos().x());
750         }
751
752         // Same for y
753         if (origin_y_top->isChecked()) {
754             value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->boundingRect().height()));
755         } else {
756             value_y->setValue((int) rec->pos().y());
757         }
758
759     } else if (i->type() == RECTITEM) {
760
761         QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
762
763         if (origin_x_left->isChecked()) {
764             // Origin (0 point) is at m_frameWidth
765             value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->rect().width()));
766         } else {
767             // Origin is at 0 (default)
768             value_x->setValue((int) rec->pos().x());
769         }
770
771         if (origin_y_top->isChecked()) {
772             value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->rect().height()));
773         } else {
774             value_y->setValue((int) rec->pos().y());
775         }
776
777     } else if (i->type() == IMAGEITEM) {
778
779         if (origin_x_left->isChecked()) {
780             value_x->setValue((int)(m_frameWidth - i->pos().x() - i->sceneBoundingRect().width()));
781         } else {
782             value_x->setValue((int) i->pos().x());
783         }
784
785         if (origin_y_top->isChecked()) {
786             value_y->setValue((int)(m_frameHeight - i->pos().y() - i->sceneBoundingRect().height()));
787         } else {
788             value_y->setValue((int) i->pos().y());
789         }
790
791     }
792
793     // Stop blocking signals now
794     value_x->blockSignals(false);
795     value_y->blockSignals(false);
796 }
797
798 /** \brief Updates the position of an item by reading coordinates from the text fields */
799 void TitleWidget::updatePosition(QGraphicsItem *i)
800 {
801     if (i->type() == TEXTITEM) {
802         QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
803
804         int posX;
805         if (origin_x_left->isChecked()) {
806             /* Origin of the x axis is at m_frameWidth,
807              * and distance from right border of the item to the right
808              * border of the frame is taken.
809              * See comment to slotOriginXClicked().
810              */
811             posX = m_frameWidth - value_x->value() - rec->boundingRect().width();
812         } else {
813             posX = value_x->value();
814         }
815
816         int posY;
817         if (origin_y_top->isChecked()) {
818             /* Same for y axis */
819             posY = m_frameHeight - value_y->value() - rec->boundingRect().height();
820         } else {
821             posY = value_y->value();
822         }
823
824         rec->setPos(posX, posY);
825
826     } else if (i->type() == RECTITEM) {
827
828         QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
829
830         int posX;
831         if (origin_x_left->isChecked()) {
832             posX = m_frameWidth - value_x->value() - rec->rect().width();
833         } else {
834             posX = value_x->value();
835         }
836
837         int posY;
838         if (origin_y_top->isChecked()) {
839             posY = m_frameHeight - value_y->value() - rec->rect().height();
840         } else {
841             posY = value_y->value();
842         }
843
844         rec->setPos(posX, posY);
845
846     } else if (i->type() == IMAGEITEM) {
847         int posX;
848         if (origin_x_left->isChecked()) {
849             // Use the sceneBoundingRect because this also regards transformations like zoom
850             posX = m_frameWidth - value_x->value() - i->sceneBoundingRect().width();
851         } else {
852             posX = value_x->value();
853         }
854
855         int posY;
856         if (origin_y_top->isChecked()) {
857             posY = m_frameHeight - value_y->value() - i->sceneBoundingRect().height();
858         } else {
859             posY = value_y->value();
860         }
861
862         i->setPos(posX, posY);
863
864     }
865
866 }
867
868 void TitleWidget::updateTextOriginX()
869 {
870     if (origin_x_left->isChecked()) {
871         origin_x_left->setText(i18n("\u2212X"));
872     } else {
873         origin_x_left->setText(i18n("+X"));
874     }
875 }
876
877 void TitleWidget::slotOriginXClicked()
878 {
879     // Update the text displayed on the button.
880     updateTextOriginX();
881
882     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
883     if (l.size() >= 1) {
884         updateCoordinates(l.at(0));
885
886         // Remember x axis setting
887         l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked() ?
888                          TitleDocument::AxisInverted : TitleDocument::AxisDefault);
889     }
890     graphicsView->setFocus();
891 }
892
893 void TitleWidget::updateTextOriginY()
894 {
895     if (origin_y_top->isChecked()) {
896         origin_y_top->setText(i18n("\u2212Y"));
897     } else {
898         origin_y_top->setText(i18n("+Y"));
899     }
900 }
901
902 void TitleWidget::slotOriginYClicked()
903 {
904     // Update the text displayed on the button.
905     updateTextOriginY();
906
907     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
908     if (l.size() >= 1) {
909         updateCoordinates(l.at(0));
910
911         l.at(0)->setData(TitleDocument::OriginYTop, origin_y_top->isChecked() ?
912                          TitleDocument::AxisInverted : TitleDocument::AxisDefault);
913
914     }
915     graphicsView->setFocus();
916 }
917
918 void TitleWidget::updateAxisButtons(QGraphicsItem *i)
919 {
920     int xAxis = i->data(TitleDocument::OriginXLeft).toInt();
921     int yAxis = i->data(TitleDocument::OriginYTop).toInt();
922     origin_x_left->blockSignals(true);
923     origin_y_top->blockSignals(true);
924
925     if (xAxis == TitleDocument::AxisInverted) {
926         origin_x_left->setChecked(true);
927     } else {
928         origin_x_left->setChecked(false);
929     }
930     updateTextOriginX();
931
932     if (yAxis == TitleDocument::AxisInverted) {
933         origin_y_top->setChecked(true);
934     } else {
935         origin_y_top->setChecked(false);
936     }
937     updateTextOriginY();
938
939     origin_x_left->blockSignals(false);
940     origin_y_top->blockSignals(false);
941 }
942
943 void TitleWidget::slotChangeBackground()
944 {
945     QColor color = kcolorbutton->color();
946     color.setAlpha(horizontalSlider->value());
947     m_frameBorder->setBrush(QBrush(color));
948 }
949
950 /**
951  * Something (yeah) has changed in our QGraphicsScene.
952  */
953 void TitleWidget::slotChanged()
954 {
955     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
956     if (l.size() >= 1 && l.at(0)->type() == TEXTITEM) {
957         textChanged(static_cast <QGraphicsTextItem *>(l.at(0)));
958     }
959 }
960
961 /**
962  * If the user has set origin_x_left (everything also for y),
963  * we need to look whether a text element has been selected. If yes,
964  * we need to ensure that the right border of the text field
965  * remains fixed also when some text has been entered.
966  *
967  * This is also known as right-justified, with the difference that
968  * it is not valid for text but for its boundingRect. Text may still
969  * be left-justified.
970  */
971 void TitleWidget::textChanged(QGraphicsTextItem *i)
972 {
973
974     updateDimension(i);
975
976     if (origin_x_left->isChecked() || origin_y_top->isChecked()) {
977
978         if (!i->toPlainText().isEmpty()) {
979             updatePosition(i);
980         } else {
981             /*
982              * Don't do anything if the string is empty. If the position
983              * would be updated here, a newly created text field would
984              * be set to the position of the last selected text field.
985              */
986         }
987     }
988 }
989
990 void TitleWidget::slotInsertUnicode()
991 {
992     m_unicodeDialog->exec();
993 }
994
995 void TitleWidget::slotInsertUnicodeString(QString text)
996 {
997     QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
998     if (l.size() > 0) {
999         if (l.at(0)->type() == TEXTITEM) {
1000             QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(l.at(0));
1001             t->textCursor().insertText(text);
1002         }
1003     }
1004 }
1005
1006 void TitleWidget::slotUpdateText()
1007 {
1008     QFont font = font_family->currentFont();
1009     font.setPixelSize(font_size->value());
1010     font.setBold(buttonBold->isChecked());
1011     font.setItalic(buttonItalic->isChecked());
1012     font.setUnderline(buttonUnder->isChecked());
1013     QColor color = fontColorButton->color();
1014     color.setAlpha(textAlpha->value());
1015
1016     QGraphicsTextItem* item = NULL;
1017     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1018     if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
1019         item = static_cast <QGraphicsTextItem *>(l.at(0));
1020     }
1021     if (!item) return;
1022     //if (item->textCursor().selection ().isEmpty())
1023     QTextCursor cur = item->textCursor();
1024     QTextBlockFormat format = cur.blockFormat();
1025     if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
1026         item->setTextWidth(item->boundingRect().width());
1027         if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
1028         else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
1029         else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
1030     } else {
1031         format.setAlignment(Qt::AlignLeft);
1032         item->setTextWidth(-1);
1033     }
1034
1035     {
1036         item->setFont(font);
1037         item->setDefaultTextColor(color);
1038         cur.select(QTextCursor::Document);
1039         cur.setBlockFormat(format);
1040         item->setTextCursor(cur);
1041         cur.clearSelection();
1042         item->setTextCursor(cur);
1043
1044     }
1045 }
1046
1047 void TitleWidget::rectChanged()
1048 {
1049     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1050     if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
1051         QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
1052         QColor f = rectFColor->color();
1053         f.setAlpha(rectFAlpha->value());
1054         QPen penf(f);
1055         penf.setWidth(rectLineWidth->value());
1056         rec->setPen(penf);
1057         QColor b = rectBColor->color();
1058         b.setAlpha(rectBAlpha->value());
1059         rec->setBrush(QBrush(b));
1060     }
1061 }
1062
1063 void TitleWidget::itemScaled(int val)
1064 {
1065     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1066     if (l.size() == 1) {
1067         Transform x = m_transformations.value(l.at(0));
1068         x.scalex = (double)val / 100.0;
1069         x.scaley = (double)val / 100.0;
1070         QTransform qtrans;
1071         qtrans.scale(x.scalex, x.scaley);
1072         qtrans.rotate(x.rotate);
1073         l[0]->setTransform(qtrans);
1074         m_transformations[l.at(0)] = x;
1075         updateDimension(l.at(0));
1076     }
1077 }
1078
1079 void TitleWidget::itemRotate(int val)
1080 {
1081     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1082     if (l.size() == 1) {
1083         Transform x = m_transformations[l.at(0)];
1084         x.rotate = (double)val;
1085         QTransform qtrans;
1086         qtrans.scale(x.scalex, x.scaley);
1087         qtrans.rotate(x.rotate);
1088         l[0]->setTransform(qtrans);
1089         m_transformations[l.at(0)] = x;
1090         updateDimension(l.at(0));
1091     }
1092 }
1093
1094 void TitleWidget::itemHCenter()
1095 {
1096     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1097     if (l.size() == 1) {
1098         QGraphicsItem *item = l.at(0);
1099         QRectF br;
1100         br = item->sceneBoundingRect();
1101         int width = (int)br.width();
1102         int newPos = (int)((m_frameWidth - width) / 2);
1103         newPos += item->pos().x() - br.left(); // Check item transformation
1104         item->setPos(newPos, item->pos().y());
1105         updateCoordinates(item);
1106     }
1107 }
1108
1109 void TitleWidget::itemVCenter()
1110 {
1111     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1112     if (l.size() == 1) {
1113         QGraphicsItem *item = l.at(0);
1114         QRectF br;
1115         br = item->sceneBoundingRect();
1116         int height = (int)br.height();
1117         int newPos = (int)((m_frameHeight - height) / 2);
1118         newPos += item->pos().y() - br.top(); // Check item transformation
1119         item->setPos(item->pos().x(), newPos);
1120         updateCoordinates(item);
1121     }
1122 }
1123
1124 void TitleWidget::setupViewports()
1125 {
1126     double aspect_ratio = 4.0 / 3.0;//read from project
1127
1128     QRectF sp(0, 0, 0, 0);
1129     QRectF ep(0, 0, 0, 0);
1130
1131     double sv_size = startViewportSize->value();
1132     double ev_size = endViewportSize->value();
1133     sp.adjust(-sv_size, -sv_size / aspect_ratio, sv_size, sv_size / aspect_ratio);
1134     ep.adjust(-ev_size, -ev_size / aspect_ratio, ev_size, ev_size / aspect_ratio);
1135
1136     m_startViewport->setPos(startViewportX->value(), startViewportY->value());
1137     m_endViewport->setPos(endViewportX->value(), endViewportY->value());
1138
1139     m_startViewport->setPolygon(QPolygonF(sp));
1140     m_endViewport->setPolygon(QPolygonF(ep));
1141
1142 }
1143
1144 void TitleWidget::loadTitle()
1145 {
1146     KUrl url = KFileDialog::getOpenUrl(KUrl(m_projectTitlePath), "*.kdenlivetitle", this, i18n("Load Title"));
1147     if (!url.isEmpty()) {
1148         QList<QGraphicsItem *> items = m_scene->items();
1149         for (int i = 0; i < items.size(); i++) {
1150             if (items.at(i)->zValue() > -1000) delete items.at(i);
1151         }
1152         m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
1153         slotSelectTool();
1154     }
1155 }
1156
1157 void TitleWidget::saveTitle(KUrl url)
1158 {
1159     if (url.isEmpty()) url = KFileDialog::getSaveUrl(KUrl(m_projectTitlePath), "*.kdenlivetitle", this, i18n("Save Title"));
1160     if (!url.isEmpty()) {
1161         if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport) == false)
1162             KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
1163     }
1164 }
1165
1166 QDomDocument TitleWidget::xml()
1167 {
1168     QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
1169     if (cropImage->isChecked()) {
1170         doc.documentElement().setAttribute("crop", 1);
1171     }
1172     return doc;
1173 }
1174
1175 void TitleWidget::setXml(QDomDocument doc)
1176 {
1177     m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport);
1178     QDomElement e = doc.documentElement();
1179     cropImage->setChecked(e.hasAttribute("crop"));
1180     m_transformations.clear();
1181     QList <QGraphicsItem *> items = graphicsView->scene()->items();
1182     const double PI = 4.0 * atan(1.0);
1183     for (int i = 0; i < items.count(); i++) {
1184         QTransform t = items.at(i)->transform();
1185         Transform x;
1186         x.scalex = t.m11();
1187         x.scaley = t.m22();
1188         x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
1189         m_transformations[items.at(i)] = x;
1190     }
1191     // mbd: Update the GUI color selectors to match the stuff from the loaded document
1192     QColor background_color = m_titledocument.getBackgroundColor();
1193     horizontalSlider->blockSignals(true);
1194     kcolorbutton->blockSignals(true);
1195     horizontalSlider->setValue(background_color.alpha());
1196     background_color.setAlpha(255);
1197     kcolorbutton->setColor(background_color);
1198     horizontalSlider->blockSignals(false);
1199     kcolorbutton->blockSignals(false);
1200
1201     slotSelectTool();
1202 }
1203
1204 const QRect TitleWidget::renderedRect()
1205 {
1206     int minX = 0;
1207     int minY = 0;
1208     int maxX = m_frameWidth;
1209     int maxY = m_frameHeight;
1210     if (!cropImage->isChecked()) {
1211         m_scene->removeItem(m_startViewport);
1212         m_scene->removeItem(m_endViewport);
1213         QRect boundingRect = m_scene->itemsBoundingRect().toRect();
1214         if (boundingRect.left() < 0) minX = boundingRect.left();
1215         if (boundingRect.top() < 0) minY = boundingRect.top();
1216         if (boundingRect.right() > maxX) maxX = boundingRect.right();
1217         if (boundingRect.bottom() > maxY) maxY = boundingRect.bottom();
1218         if (minX < 0) {
1219             maxX = maxX - minX;
1220         }
1221         if (minY < 0) {
1222             maxY = maxY - minY;
1223         }
1224     }
1225     QRect rect(minX, minY, maxX, maxY);
1226     return rect;
1227 }
1228
1229 QImage TitleWidget::renderedPixmap()
1230 {
1231     QRect rect = renderedRect();
1232     QImage pix(rect.width(), rect.height(), QImage::Format_ARGB32);
1233     pix.fill(Qt::transparent);
1234     QPainter painter(&pix);
1235     painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
1236     m_scene->clearTextSelection();
1237     QPen framepen = m_frameBorder->pen();
1238     m_frameBorder->setPen(Qt::NoPen);
1239     m_startViewport->setVisible(false);
1240     m_endViewport->setVisible(false);
1241     m_frameImage->setVisible(false);
1242
1243     m_scene->render(&painter, QRectF(), rect); //QRectF(minX, minY, maxX - minX, maxY - minY));
1244     painter.end();
1245     m_frameBorder->setPen(framepen);
1246     m_startViewport->setVisible(true);
1247     m_endViewport->setVisible(true);
1248     m_frameImage->setVisible(true);
1249     return pix;
1250 }
1251
1252 /** \brief Connected to the accepted signal - calls writeChoices */
1253 void TitleWidget::slotAccepted()
1254 {
1255     writeChoices();
1256 }
1257
1258 /** \brief Store the current choices of font, background and rect values */
1259 void TitleWidget::writeChoices()
1260 {
1261     // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1262     KSharedConfigPtr config = KGlobal::config();
1263     KConfigGroup titleConfig(config, "TitleWidget");
1264     // Write the entries
1265     titleConfig.writeEntry("font_family", font_family->currentFont());
1266     //titleConfig.writeEntry("font_size", font_size->value());
1267     titleConfig.writeEntry("font_pixel_size", font_size->value());
1268     titleConfig.writeEntry("font_color", fontColorButton->color());
1269     titleConfig.writeEntry("font_alpha", textAlpha->value());
1270     titleConfig.writeEntry("font_bold", buttonBold->isChecked());
1271     titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
1272     titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
1273
1274     titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
1275     titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
1276     titleConfig.writeEntry("rect_background_color", rectBColor->color());
1277     titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
1278     titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
1279
1280     titleConfig.writeEntry("background_color", kcolorbutton->color());
1281     titleConfig.writeEntry("background_alpha", horizontalSlider->value());
1282
1283     titleConfig.writeEntry("crop_image", cropImage->isChecked());
1284
1285     //! \todo Not sure if I should sync - it is probably safe to do it
1286     config->sync();
1287
1288 }
1289
1290 /** \brief Read the last stored choices into the dialog */
1291 void TitleWidget::readChoices()
1292 {
1293     // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1294     KSharedConfigPtr config = KGlobal::config();
1295     KConfigGroup titleConfig(config, "TitleWidget");
1296     // read the entries
1297     font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
1298     font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
1299     fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
1300     textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
1301     buttonBold->setChecked(titleConfig.readEntry("font_bold", buttonBold->isChecked()));
1302     buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
1303     buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
1304
1305     rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
1306     rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
1307     rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
1308     rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
1309     rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
1310
1311     kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
1312     horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
1313
1314     cropImage->setChecked(titleConfig.readEntry("crop_image", cropImage->isChecked()));
1315 }
1316