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