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