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