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