]> git.sesse.net Git - kdenlive/blob - src/titlewidget.cpp
[PATCH 2/2] Ensure that all member variables have an m_ prefix
[kdenlive] / src / titlewidget.cpp
1 /***************************************************************************
2                           titlewidget.h  -  description
3                              -------------------
4     begin                : Feb 28 2008
5     copyright            : (C) 2008 by Marco Gittler
6     email                : g.marco@freenet.de
7  ***************************************************************************/
8
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17
18 #include "titlewidget.h"
19 #include "kdenlivesettings.h"
20
21 #include <KDebug>
22 #include <KGlobalSettings>
23 #include <KFileDialog>
24 #include <KStandardDirs>
25 #include <KMessageBox>
26
27 #include <QDomDocument>
28 #include <QGraphicsItem>
29 #include <QGraphicsSvgItem>
30 #include <QTimer>
31 #include <QToolBar>
32 #include <QMenu>
33
34 int settingUp = false;
35
36 TitleWidget::TitleWidget(KUrl url, QString projectPath, Render *render, QWidget *parent) :
37         QDialog(parent),
38         Ui::TitleWidget_UI(),
39         m_startViewport(NULL),
40         m_endViewport(NULL),
41         m_render(render),
42         m_count(0),
43         m_projectPath(projectPath)
44 {
45     setupUi(this);
46     setFont(KGlobalSettings::toolBarFont());
47     //toolBox->setFont(KGlobalSettings::toolBarFont());
48     frame_properties->setEnabled(false);
49     rect_properties->setFixedHeight(frame_properties->height() + 4);
50     text_properties->setFixedHeight(frame_properties->height() + 4);
51     m_frameWidth = render->renderWidth();
52     m_frameHeight = render->renderHeight();
53     //connect(newTextButton, SIGNAL(clicked()), this, SLOT(slotNewText()));
54     //connect(newRectButton, SIGNAL(clicked()), this, SLOT(slotNewRect()));
55     // kcolorbutton == The color of the background
56     connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
57     // horizontalslider == The alpha of the background
58     connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
59     //connect(ktextedit, SIGNAL(textChanged()), this , SLOT(textChanged()));
60     //connect (fontBold, SIGNAL ( clicked()), this, SLOT( setBold()) ) ;
61
62
63     //ktextedit->setHidden(true);
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     //connect (ktextedit, SIGNAL(selectionChanged()), this , SLOT (textChanged()));
69
70     connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
71     connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
72     connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
73     connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
74     connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
75
76     connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
77     connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
78     connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
79     connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
80     connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
81     connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
82
83     connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
84     connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
85     connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
86     connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
87     connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
88
89     connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
90     connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
91     connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
92     connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
93     connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
94     connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
95     connect(buttonBold, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
96     connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
97     connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
98     connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
99
100     // mbd
101     connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
102
103     buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
104     buttonRealSize->setIcon(KIcon("zoom-original"));
105     buttonBold->setIcon(KIcon("format-text-bold"));
106     buttonItalic->setIcon(KIcon("format-text-italic"));
107     buttonUnder->setIcon(KIcon("format-text-underline"));
108
109     itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
110     itemhcenter->setToolTip(i18n("Align item horizontally"));
111     itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
112     itemvcenter->setToolTip(i18n("Align item vertically"));
113
114     QHBoxLayout *layout = new QHBoxLayout;
115     frame_toolbar->setLayout(layout);
116     layout->setContentsMargins(2, 2, 2, 2);
117     QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
118
119     m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), i18n("Add Rectangle"));
120     m_buttonRect->setCheckable(true);
121     connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
122
123     m_buttonText = m_toolbar->addAction(KIcon("insert-text"), i18n("Add Text"));
124     m_buttonText->setCheckable(true);
125     connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
126
127     m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), i18n("Add Image"));
128     m_buttonImage->setCheckable(false);
129     connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
130
131     m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), i18n("Selection Tool"));
132     m_buttonCursor->setCheckable(true);
133     connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
134
135     m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
136     m_buttonLoad->setCheckable(false);
137     connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
138
139     m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
140     m_buttonSave->setCheckable(false);
141     connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
142
143     layout->addWidget(m_toolbar);
144     text_properties->setHidden(true);
145
146     // initialize graphic scene
147     m_scene = new GraphicsSceneRectMove(this);
148     graphicsView->setScene(m_scene);
149     m_titledocument.setScene(m_scene);
150
151     // a gradient background
152     /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
153     gradient->setSpread(QGradient::ReflectSpread);
154     scene->setBackgroundBrush(*gradient);*/
155
156     m_frameImage = new QGraphicsPixmapItem();
157     QTransform qtrans;
158     qtrans.scale(2.0, 2.0);
159     m_frameImage->setTransform(qtrans);
160     m_frameImage->setZValue(-1200);
161     m_frameImage->setFlags(QGraphicsItem::ItemClipsToShape);
162     displayBackgroundFrame();
163     graphicsView->scene()->addItem(m_frameImage);
164
165     connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
166     connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
167     connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
168     connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
169     connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
170     connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
171     connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
172     connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
173
174     QPen framepen(Qt::DotLine);
175     framepen.setColor(Qt::red);
176
177     m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
178     m_frameBorder->setPen(framepen);
179     m_frameBorder->setZValue(-1100);
180     m_frameBorder->setBrush(Qt::transparent);
181     m_frameBorder->setFlags(QGraphicsItem::ItemClipsToShape);
182     graphicsView->scene()->addItem(m_frameBorder);
183
184     // mbd: load saved settings
185     readChoices();
186
187     initViewports();
188     QTimer::singleShot(500, this, SLOT(slotAdjustZoom()));
189     graphicsView->show();
190     //graphicsView->setRenderHint(QPainter::Antialiasing);
191     graphicsView->setInteractive(true);
192     //graphicsView->resize(400, 300);
193     kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
194     toolBox->setItemEnabled(2, false);
195     if (!url.isEmpty()) {
196         m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
197         slotSelectTool();
198     } else {
199         slotRectTool();
200     }
201 }
202
203 TitleWidget::~TitleWidget()
204 {
205     delete m_buttonRect;
206     delete m_buttonText;
207     delete m_buttonImage;
208     delete m_buttonCursor;
209     delete m_buttonSave;
210     delete m_buttonLoad;
211
212     delete m_frameBorder;
213     delete m_frameImage;
214     delete m_startViewport;
215     delete m_endViewport;
216     delete m_scene;
217 }
218
219 //static
220 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl)
221 {
222     QStringList result;
223     QString titlePath = projectUrl.path() + "/titles/";
224     KStandardDirs::makeDir(titlePath);
225     QString titleName = "title";
226     int counter = 0;
227     QString path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
228     while (QFile::exists(path + ".png")) {
229         counter++;
230         path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
231     }
232     result.append(titleName + QString::number(counter).rightJustified(3, '0', false));
233     result.append(path + ".png");
234     return result;
235 }
236
237 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
238 {
239     QStringList result;
240     QString titlePath = projectUrl.path() + "/titles/";
241     KStandardDirs::makeDir(titlePath);
242     return titlePath + titleName + ".png";
243 }
244
245 //virtual
246 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
247 {
248     //slotAdjustZoom();
249 }
250
251 void TitleWidget::slotTextTool()
252 {
253     rect_properties->setHidden(true);
254     text_properties->setHidden(false);
255     m_scene->setTool(TITLE_TEXT);
256     m_buttonRect->setChecked(false);
257     m_buttonCursor->setChecked(false);
258 }
259
260 void TitleWidget::slotRectTool()
261 {
262     text_properties->setHidden(true);
263     rect_properties->setHidden(false);
264     m_scene->setTool(TITLE_RECTANGLE);
265     m_buttonText->setChecked(false);
266     m_buttonCursor->setChecked(false);
267     m_buttonRect->setChecked(true);
268 }
269
270 void TitleWidget::slotSelectTool()
271 {
272     m_scene->setTool(TITLE_SELECT);
273     m_buttonCursor->setChecked(true);
274     m_buttonText->setChecked(false);
275     m_buttonRect->setChecked(false);
276 }
277
278 void TitleWidget::slotImageTool()
279 {
280     KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.svg *.png *.jpg *.jpeg *.gif *.raw", this, i18n("Load Image"));
281     if (!url.isEmpty()) {
282         if (url.path().endsWith(".svg")) {
283             QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
284             svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
285             svg->setZValue(m_count++);
286             svg->setData(Qt::UserRole, url.path());
287             graphicsView->scene()->addItem(svg);
288         } else {
289             QPixmap pix(url.path());
290             QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
291             image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
292             image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
293             image->setData(Qt::UserRole, url.path());
294             image->setZValue(m_count++);
295             graphicsView->scene()->addItem(image);
296         }
297     }
298     m_scene->setTool(TITLE_SELECT);
299     m_buttonRect->setChecked(false);
300     m_buttonCursor->setChecked(true);
301     m_buttonText->setChecked(false);
302 }
303
304 void TitleWidget::displayBackgroundFrame()
305 {
306     if (!displayBg->isChecked()) {
307         QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
308         QPixmap pattern(20, 20);
309         pattern.fill();
310         QPainter p;
311         p.begin(&pattern);
312         p.fillRect(QRect(0, 0, 10, 10), QColor(210, 210, 210));
313         p.fillRect(QRect(10, 10, 20, 20), QColor(210, 210, 210));
314         p.end();
315         QBrush br(pattern);
316
317         p.begin(&bg);
318         p.fillRect(bg.rect(), br);
319         p.end();
320         m_frameImage->setPixmap(bg);
321     } else {
322         m_frameImage->setPixmap(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2));
323     }
324 }
325
326 void TitleWidget::initViewports()
327 {
328     m_startViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
329     m_endViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
330
331     QPen startpen(Qt::DotLine);
332     QPen endpen(Qt::DashDotLine);
333     startpen.setColor(QColor(100, 200, 100, 140));
334     endpen.setColor(QColor(200, 100, 100, 140));
335
336     m_startViewport->setPen(startpen);
337     m_endViewport->setPen(endpen);
338
339     startViewportSize->setValue(40);
340     endViewportSize->setValue(40);
341
342     m_startViewport->setZValue(-1000);
343     m_endViewport->setZValue(-1000);
344
345     m_startViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
346     m_endViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
347
348     graphicsView->scene()->addItem(m_startViewport);
349     graphicsView->scene()->addItem(m_endViewport);
350 }
351
352 void TitleWidget::slotUpdateZoom(int pos)
353 {
354     m_scene->setZoom((double) pos / 100);
355     zoom_label->setText(QString::number(pos) + '%');
356 }
357
358 void TitleWidget::slotZoom(bool up)
359 {
360     int pos = zoom_slider->value();
361     if (up) pos++;
362     else pos--;
363     zoom_slider->setValue(pos);
364 }
365
366 void TitleWidget::slotAdjustZoom()
367 {
368     /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
369     double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
370     if (scalex > scaley) scalex = scaley;
371     int zoompos = (int)(scalex * 7 + 0.5);*/
372     graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
373     int zoompos = graphicsView->matrix().m11() * 100;
374     zoom_slider->setValue(zoompos);
375     graphicsView->centerOn(m_frameBorder);
376 }
377
378 void TitleWidget::slotZoomOneToOne()
379 {
380     zoom_slider->setValue(100);
381     graphicsView->centerOn(m_frameBorder);
382 }
383
384 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
385 {
386     QColor f = rectFColor->color();
387     f.setAlpha(rectFAlpha->value());
388     QPen penf(f);
389     penf.setWidth(rectLineWidth->value());
390     rect->setPen(penf);
391     QColor b = rectBColor->color();
392     b.setAlpha(rectBAlpha->value());
393     rect->setBrush(QBrush(b));
394     rect->setZValue(m_count++);
395     //setCurrentItem(rect);
396     //graphicsView->setFocus();
397 }
398
399 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
400 {
401     QFont font = font_family->currentFont();
402     font.setPixelSize(font_size->value());
403     // mbd: issue 551:
404     font.setBold(buttonBold->isChecked());
405     font.setItalic(buttonItalic->isChecked());
406     font.setUnderline(buttonUnder->isChecked());
407
408     tt->setFont(font);
409     QColor color = fontColorButton->color();
410     color.setAlpha(textAlpha->value());
411     tt->setDefaultTextColor(color);
412     tt->setZValue(m_count++);
413     setCurrentItem(tt);
414 }
415
416 void TitleWidget::setCurrentItem(QGraphicsItem *item)
417 {
418     m_scene->setSelectedItem(item);
419 }
420
421 void TitleWidget::zIndexChanged(int v)
422 {
423     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
424     if (l.size() >= 1) {
425         l[0]->setZValue(v);
426     }
427 }
428
429 void TitleWidget::selectionChanged()
430 {
431     if (m_scene->tool() != TITLE_SELECT) return;
432     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
433     //toolBox->setItemEnabled(2, false);
434     //toolBox->setItemEnabled(3, false);
435     value_x->blockSignals(true);
436     value_y->blockSignals(true);
437     value_w->blockSignals(true);
438     value_h->blockSignals(true);
439     //kDebug() << "////////  SELECTION CHANGED; ITEMS: " << l.size();
440     if (l.size() == 1) {
441         if ((l[0])->type() == 8) {
442             rect_properties->setHidden(true);
443             text_properties->setHidden(false);
444             QGraphicsTextItem* i = ((QGraphicsTextItem*)l[0]);
445             //if (l[0]->hasFocus())
446             //ktextedit->setHtml(i->toHtml());
447             toolBox->setCurrentIndex(0);
448             //toolBox->setItemEnabled(2, true);
449             font_size->blockSignals(true);
450             font_family->blockSignals(true);
451             buttonBold->blockSignals(true);
452             buttonItalic->blockSignals(true);
453             buttonUnder->blockSignals(true);
454             fontColorButton->blockSignals(true);
455             textAlpha->blockSignals(true);
456
457             QFont font = i->font();
458             font_family->setCurrentFont(font);
459             font_size->setValue(font.pixelSize());
460             buttonBold->setChecked(font.bold());
461             buttonItalic->setChecked(font.italic());
462             buttonUnder->setChecked(font.underline());
463
464             QColor color = i->defaultTextColor();
465             fontColorButton->setColor(color);
466             textAlpha->setValue(color.alpha());
467
468             font_size->blockSignals(false);
469             font_family->blockSignals(false);
470             buttonBold->blockSignals(false);
471             buttonItalic->blockSignals(false);
472             buttonUnder->blockSignals(false);
473             fontColorButton->blockSignals(false);
474             textAlpha->blockSignals(false);
475
476             value_x->setValue((int) i->pos().x());
477             value_y->setValue((int) i->pos().y());
478             value_w->setValue((int) i->boundingRect().width());
479             value_h->setValue((int) i->boundingRect().height());
480             frame_properties->setEnabled(true);
481             value_w->setEnabled(false);
482             value_h->setEnabled(false);
483         } else if ((l[0])->type() == 3) {
484             rect_properties->setHidden(false);
485             text_properties->setHidden(true);
486             settingUp = true;
487             QGraphicsRectItem *rec = ((QGraphicsRectItem*)l[0]);
488             toolBox->setCurrentIndex(0);
489             //toolBox->setItemEnabled(3, true);
490             rectFAlpha->setValue(rec->pen().color().alpha());
491             rectBAlpha->setValue(rec->brush().color().alpha());
492             //kDebug() << rec->brush().color().alpha();
493             QColor fcol = rec->pen().color();
494             QColor bcol = rec->brush().color();
495             //fcol.setAlpha(255);
496             //bcol.setAlpha(255);
497             rectFColor->setColor(fcol);
498             rectBColor->setColor(bcol);
499             settingUp = false;
500             rectLineWidth->setValue(rec->pen().width());
501             value_x->setValue((int) rec->pos().x());
502             value_y->setValue((int) rec->pos().y());
503             value_w->setValue((int) rec->rect().width());
504             value_h->setValue((int) rec->rect().height());
505             frame_properties->setEnabled(true);
506             value_w->setEnabled(true);
507             value_h->setEnabled(true);
508         } else {
509             //toolBox->setCurrentIndex(0);
510             frame_properties->setEnabled(false);
511         }
512         zValue->setValue((int)l[0]->zValue());
513         itemzoom->setValue((int)(m_transformations[l[0]].scalex * 100));
514         itemrotate->setValue((int)(m_transformations[l[0]].rotate));
515         value_x->blockSignals(false);
516         value_y->blockSignals(false);
517         value_w->blockSignals(false);
518         value_h->blockSignals(false);
519     } else frame_properties->setEnabled(false);
520 }
521
522 void TitleWidget::slotAdjustSelectedItem()
523 {
524     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
525     if (l.size() >= 1) {
526         if (l[0]->type() == 3) {
527             //rect item
528             QGraphicsRectItem *rec = ((QGraphicsRectItem*)l[0]);
529             rec->setPos(value_x->value(), value_y->value());
530             rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
531         } else if (l[0]->type() == 8) {
532             //text item
533             QGraphicsTextItem *rec = ((QGraphicsTextItem*)l[0]);
534             rec->setPos(value_x->value(), value_y->value());
535         }
536     }
537 }
538
539 void TitleWidget::slotChangeBackground()
540 {
541     QColor color = kcolorbutton->color();
542     color.setAlpha(horizontalSlider->value());
543     m_frameBorder->setBrush(QBrush(color));
544 }
545
546 void TitleWidget::textChanged()
547 {
548     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
549     if (l.size() == 1 && (l[0])->type() == 8 && !l[0]->hasFocus()) {
550         //kDebug() << ktextedit->document()->toHtml();
551         //((QGraphicsTextItem*)l[0])->setHtml(ktextedit->toHtml());
552     }
553 }
554
555 void TitleWidget::slotUpdateText()
556 {
557     QFont font = font_family->currentFont();
558     font.setPixelSize(font_size->value());
559     font.setBold(buttonBold->isChecked());
560     font.setItalic(buttonItalic->isChecked());
561     font.setUnderline(buttonUnder->isChecked());
562     QColor color = fontColorButton->color();
563     color.setAlpha(textAlpha->value());
564
565     QGraphicsTextItem* item = NULL;
566     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
567     if (l.size() == 1 && (l[0])->type() == 8) {
568         item = (QGraphicsTextItem*)l[0];
569     }
570     if (!item) return;
571     //if (item->textCursor().selection ().isEmpty())
572     {
573         item->setFont(font);
574         item->setDefaultTextColor(color);
575     }
576     /*else {
577     QTextDocumentFragment selec = item->textCursor().selection ();
578     selec.set
579     }*/
580     //if (ktextedit->textCursor().selectedText().isEmpty()) ktextedit->selectAll();
581
582     //ktextedit->setCurrentFont(font);
583     //ktextedit->setTextColor(color);
584     /*QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
585     if (l.size() == 1 && (l[0])->type() == 8 && l[0]->hasFocus()) {
586     QGraphicsTextItem* item = static_cast <QGraphicsTextItem*> (l[0]);
587     //item-
588     }*/
589 }
590
591 void TitleWidget::rectChanged()
592 {
593     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
594     if (l.size() == 1 && (l[0])->type() == 3 && !settingUp) {
595         QGraphicsRectItem *rec = (QGraphicsRectItem*)l[0];
596         QColor f = rectFColor->color();
597         f.setAlpha(rectFAlpha->value());
598         QPen penf(f);
599         penf.setWidth(rectLineWidth->value());
600         rec->setPen(penf);
601         QColor b = rectBColor->color();
602         b.setAlpha(rectBAlpha->value());
603         rec->setBrush(QBrush(b));
604     }
605 }
606
607 void TitleWidget::fontBold()
608 {
609     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
610     if (l.size() == 1 && (l[0])->type() == 8 && !l[0]->hasFocus()) {
611         //ktextedit->document()->setTextOption();
612     }
613 }
614
615 void TitleWidget::itemScaled(int val)
616 {
617     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
618     if (l.size() == 1) {
619         Transform x = m_transformations[l[0]];
620         x.scalex = (double)val / 100.0;
621         x.scaley = (double)val / 100.0;
622         QTransform qtrans;
623         qtrans.scale(x.scalex, x.scaley);
624         qtrans.rotate(x.rotate);
625         l[0]->setTransform(qtrans);
626         m_transformations[l[0]] = x;
627     }
628 }
629
630 void TitleWidget::itemRotate(int val)
631 {
632     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
633     if (l.size() == 1) {
634         Transform x = m_transformations[l[0]];
635         x.rotate = (double)val;
636         QTransform qtrans;
637         qtrans.scale(x.scalex, x.scaley);
638         qtrans.rotate(x.rotate);
639         l[0]->setTransform(qtrans);
640         m_transformations[l[0]] = x;
641     }
642 }
643
644 void TitleWidget::itemHCenter()
645 {
646     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
647     if (l.size() == 1) {
648         QGraphicsItem *item = l[0];
649         QRectF br;
650         if (item->type() == 3) {
651             br = ((QGraphicsRectItem*)item)->rect();
652         } else br = item->boundingRect();
653         int width = (int) br.width();
654         int newPos = (int)((m_frameWidth - width) / 2);
655         item->setPos(newPos, item->pos().y());
656     }
657 }
658
659 void TitleWidget::itemVCenter()
660 {
661     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
662     if (l.size() == 1) {
663         QGraphicsItem *item = l[0];
664         QRectF br;
665         if (item->type() == 3) {
666             br = ((QGraphicsRectItem*)item)->rect();
667         } else br = item->boundingRect();
668         int height = (int) br.height();
669         int newPos = (int)((m_frameHeight - height) / 2);
670         item->setPos(item->pos().x(), newPos);
671     }
672 }
673
674 void TitleWidget::setupViewports()
675 {
676     double aspect_ratio = 4.0 / 3.0;//read from project
677
678     QRectF sp(0, 0, 0, 0);
679     QRectF ep(0, 0, 0, 0);
680
681     double sv_size = startViewportSize->value();
682     double ev_size = endViewportSize->value();
683     sp.adjust(-sv_size, -sv_size / aspect_ratio, sv_size, sv_size / aspect_ratio);
684     ep.adjust(-ev_size, -ev_size / aspect_ratio, ev_size, ev_size / aspect_ratio);
685
686     m_startViewport->setPos(startViewportX->value(), startViewportY->value());
687     m_endViewport->setPos(endViewportX->value(), endViewportY->value());
688
689     m_startViewport->setPolygon(QPolygonF(sp));
690     m_endViewport->setPolygon(QPolygonF(ep));
691
692 }
693
694 void TitleWidget::loadTitle()
695 {
696     KUrl url = KFileDialog::getOpenUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Load Title"));
697     if (!url.isEmpty()) {
698         QList<QGraphicsItem *> items = m_scene->items();
699         for (int i = 0; i < items.size(); i++) {
700             if (items.at(i)->zValue() > -1000) delete items.at(i);
701         }
702         m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
703         slotSelectTool();
704     }
705 }
706
707 void TitleWidget::saveTitle(KUrl url)
708 {
709     if (url.isEmpty()) url = KFileDialog::getSaveUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Save Title"));
710     if (!url.isEmpty()) {
711         if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport) == false)
712             KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
713     }
714 }
715
716 QDomDocument TitleWidget::xml()
717 {
718     return m_titledocument.xml(m_startViewport, m_endViewport);
719 }
720
721 void TitleWidget::setXml(QDomDocument doc)
722 {
723     m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport);
724     // mbd: Update the GUI color selectors to match the stuff from the loaded document
725     QColor background_color = m_titledocument.getBackgroundColor();
726     horizontalSlider->blockSignals(true);
727     kcolorbutton->blockSignals(true);
728     horizontalSlider->setValue(background_color.alpha());
729     background_color.setAlpha(255);
730     kcolorbutton->setColor(background_color);
731     horizontalSlider->blockSignals(false);
732     kcolorbutton->blockSignals(false);
733
734     slotSelectTool();
735 }
736
737 QImage TitleWidget::renderedPixmap()
738 {
739     QImage pix(m_frameWidth, m_frameHeight, QImage::Format_ARGB32);
740     pix.fill(Qt::transparent);
741     QPainter painter(&pix);
742     painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
743     m_scene->clearTextSelection();
744     QPen framepen = m_frameBorder->pen();
745     m_frameBorder->setPen(Qt::NoPen);
746     m_startViewport->setVisible(false);
747     m_endViewport->setVisible(false);
748     m_frameImage->setVisible(false);
749
750     m_scene->render(&painter, QRectF(), QRectF(0, 0, m_frameWidth, m_frameHeight));
751     painter.end();
752     m_frameBorder->setPen(framepen);
753     m_startViewport->setVisible(true);
754     m_endViewport->setVisible(true);
755     m_frameImage->setVisible(true);
756     return pix;
757 }
758
759 /** \brief Connected to the accepted signal - calls writeChoices */
760 void TitleWidget::slotAccepted()
761 {
762     writeChoices();
763 }
764
765 /** \brief Store the current choices of font, background and rect values */
766 void TitleWidget::writeChoices()
767 {
768     // Get a pointer to a shared configuration instance, then get the TitleWidget group.
769     KSharedConfigPtr config = KGlobal::config();
770     KConfigGroup titleConfig(config, "TitleWidget");
771     // Write the entries
772     titleConfig.writeEntry("font_family", font_family->currentFont());
773     //titleConfig.writeEntry("font_size", font_size->value());
774     titleConfig.writeEntry("font_pixel_size", font_size->value());
775     titleConfig.writeEntry("font_color", fontColorButton->color());
776     titleConfig.writeEntry("font_alpha", textAlpha->value());
777     titleConfig.writeEntry("font_bold", buttonBold->isChecked());
778     titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
779     titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
780
781     titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
782     titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
783     titleConfig.writeEntry("rect_background_color", rectBColor->color());
784     titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
785     titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
786
787     titleConfig.writeEntry("background_color", kcolorbutton->color());
788     titleConfig.writeEntry("background_alpha", horizontalSlider->value());
789     //! \todo Not sure if I should sync - it is probably safe to do it
790     config->sync();
791
792 }
793
794 /** \brief Read the last stored choices into the dialog */
795 void TitleWidget::readChoices()
796 {
797     // Get a pointer to a shared configuration instance, then get the TitleWidget group.
798     KSharedConfigPtr config = KGlobal::config();
799     KConfigGroup titleConfig(config, "TitleWidget");
800     // read the entries
801     font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
802     font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
803     fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
804     textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
805     buttonBold->setChecked(titleConfig.readEntry("font_bold", buttonBold->isChecked()));
806     buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
807     buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
808
809     rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
810     rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
811     rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
812     rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
813     rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
814
815     kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
816     horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
817 }
818