]> git.sesse.net Git - kdenlive/blob - src/titlewidget.cpp
Use QImage instead of QPixmap where possible, rework the clip loading stuff
[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 <cmath>
22
23 #include <KDebug>
24 #include <KGlobalSettings>
25 #include <KFileDialog>
26 #include <KStandardDirs>
27 #include <KMessageBox>
28 #include <kio/netaccess.h>
29
30 #include <QDomDocument>
31 #include <QGraphicsItem>
32 #include <QGraphicsSvgItem>
33 #include <QTimer>
34 #include <QToolBar>
35 #include <QMenu>
36 #include <QSignalMapper>
37 #include <QTextBlockFormat>
38 #include <QTextCursor>
39
40 int settingUp = false;
41
42 const int IMAGEITEM = 7;
43 const int RECTITEM = 3;
44 const int TEXTITEM = 8;
45 static bool insertingValues = false;
46
47 TitleWidget::TitleWidget(KUrl url, Timecode tc, QString projectTitlePath, Render *render, QWidget *parent) :
48         QDialog(parent),
49         Ui::TitleWidget_UI(),
50         m_startViewport(NULL),
51         m_endViewport(NULL),
52         m_render(render),
53         m_count(0),
54         m_unicodeDialog(new UnicodeDialog(UnicodeDialog::InputHex)),
55         m_projectTitlePath(projectTitlePath),
56         m_tc(tc)
57 {
58     setupUi(this);
59     setFont(KGlobalSettings::toolBarFont());
60     //toolBox->setFont(KGlobalSettings::toolBarFont());
61     frame_properties->setEnabled(false);
62     rect_properties->setFixedHeight(frame_properties->height() + 4);
63     no_properties->setFixedHeight(frame_properties->height() + 4);
64     image_properties->setFixedHeight(frame_properties->height() + 4);
65     text_properties->setFixedHeight(frame_properties->height() + 4);
66     frame_properties->setFixedHeight(frame_toolbar->height());
67
68     itemzoom->setSuffix(i18n("%"));
69     m_frameWidth = render->renderWidth();
70     m_frameHeight = render->renderHeight();
71     showToolbars(TITLE_NONE);
72
73     //TODO: get default title duration instead of hardcoded one
74     title_duration->setText(m_tc.getTimecode(GenTime(5000 / 1000.0)));
75
76     connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
77     connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
78
79     connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
80     connect(font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(slotUpdateText())) ;
81     connect(font_size, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText())) ;
82     connect(textAlpha, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText()));
83     connect(font_weight_box, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateText()));
84
85     connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
86     connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
87     connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
88     connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
89     connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
90
91     /*connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
92     connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
93     connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
94     connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
95     connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
96     connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));*/
97
98     connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
99     connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
100     connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
101     connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
102     connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
103     connect(itemtop, SIGNAL(clicked()), this, SLOT(itemTop()));
104     connect(itembottom, SIGNAL(clicked()), this, SLOT(itemBottom()));
105     connect(itemleft, SIGNAL(clicked()), this, SLOT(itemLeft()));
106     connect(itemright, SIGNAL(clicked()), this, SLOT(itemRight()));
107
108     connect(origin_x_left, SIGNAL(clicked()), this, SLOT(slotOriginXClicked()));
109     connect(origin_y_top, SIGNAL(clicked()), this, SLOT(slotOriginYClicked()));
110
111     m_signalMapper = new QSignalMapper(this);
112     m_signalMapper->setMapping(value_w, ValueWidth);
113     m_signalMapper->setMapping(value_h, ValueHeight);
114     connect(value_w, SIGNAL(valueChanged(int)), m_signalMapper, SLOT(map()));
115     connect(value_h, SIGNAL(valueChanged(int)), m_signalMapper, SLOT(map()));
116     connect(m_signalMapper, SIGNAL(mapped(int)), this, SLOT(slotValueChanged(int)));
117
118     connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
119     connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
120     connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
121     connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
122     connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
123     connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
124     connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
125     connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
126     connect(buttonAlignLeft, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
127     connect(buttonAlignRight, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
128     connect(buttonAlignCenter, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
129     connect(buttonAlignNone, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
130     //connect(buttonInsertUnicode, SIGNAL(clicked()), this, SLOT(slotInsertUnicode()));
131     connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
132
133     connect(m_unicodeDialog, SIGNAL(charSelected(QString)), this, SLOT(slotInsertUnicodeString(QString)));
134
135     // mbd
136     connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
137
138     font_weight_box->blockSignals(true);
139     font_weight_box->addItem(i18nc("Font style", "Light"), QFont::Light);
140     font_weight_box->addItem(i18nc("Font style", "Normal"), QFont::Normal);
141     font_weight_box->addItem(i18nc("Font style", "Demi-Bold"), QFont::DemiBold);
142     font_weight_box->addItem(i18nc("Font style", "Bold"), QFont::Bold);
143     font_weight_box->addItem(i18nc("Font style", "Black"), QFont::Black);
144     font_weight_box->setToolTip(i18n("Font weight"));
145     font_weight_box->setCurrentIndex(1);
146     font_weight_box->blockSignals(false);
147
148     buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
149     buttonRealSize->setIcon(KIcon("zoom-original"));
150     buttonItalic->setIcon(KIcon("format-text-italic"));
151     buttonUnder->setIcon(KIcon("format-text-underline"));
152     buttonAlignCenter->setIcon(KIcon("format-justify-center"));
153     buttonAlignLeft->setIcon(KIcon("format-justify-left"));
154     buttonAlignRight->setIcon(KIcon("format-justify-right"));
155     buttonAlignNone->setIcon(KIcon("kdenlive-align-none"));
156
157     buttonAlignNone->setToolTip(i18n("No alignment"));
158     buttonAlignRight->setToolTip(i18n("Align right"));
159     buttonAlignLeft->setToolTip(i18n("Align left"));
160     buttonAlignCenter->setToolTip(i18n("Align center"));
161
162     m_unicodeAction = new QAction(KIcon("kdenlive-insert-unicode"), QString(), this);
163     m_unicodeAction->setShortcut(Qt::SHIFT + Qt::CTRL + Qt::Key_U);
164     m_unicodeAction->setToolTip(i18n("Insert Unicode character") + ' ' + m_unicodeAction->shortcut().toString());
165     connect(m_unicodeAction, SIGNAL(triggered()), this, SLOT(slotInsertUnicode()));
166     buttonInsertUnicode->setDefaultAction(m_unicodeAction);
167
168     origin_x_left->setToolTip(i18n("Invert x axis and change 0 point"));
169     origin_y_top->setToolTip(i18n("Invert y axis and change 0 point"));
170     rectBColor->setToolTip(i18n("Select fill color"));
171     rectFColor->setToolTip(i18n("Select border color"));
172     rectBAlpha->setToolTip(i18n("Fill transparency"));
173     rectFAlpha->setToolTip(i18n("Border transparency"));
174     zoom_slider->setToolTip(i18n("Zoom"));
175     buttonRealSize->setToolTip(i18n("Original size (1:1)"));
176     buttonFitZoom->setToolTip(i18n("Fit zoom"));
177     kcolorbutton->setToolTip(i18n("Select background color"));
178     horizontalSlider->setToolTip(i18n("Background Transparency"));
179
180     itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
181     itemhcenter->setToolTip(i18n("Align item horizontally"));
182     itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
183     itemvcenter->setToolTip(i18n("Align item vertically"));
184     itemtop->setIcon(KIcon("kdenlive-align-top"));
185     itemtop->setToolTip(i18n("Align item to top"));
186     itembottom->setIcon(KIcon("kdenlive-align-bottom"));
187     itembottom->setToolTip(i18n("Align item to bottom"));
188     itemright->setIcon(KIcon("kdenlive-align-right"));
189     itemright->setToolTip(i18n("Align item to right"));
190     itemleft->setIcon(KIcon("kdenlive-align-left"));
191     itemleft->setToolTip(i18n("Align item to left"));
192
193
194     QHBoxLayout *layout = new QHBoxLayout;
195     frame_toolbar->setLayout(layout);
196     layout->setContentsMargins(2, 2, 2, 2);
197     QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
198
199     m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), QString());
200     m_buttonCursor->setCheckable(true);
201     m_buttonCursor->setShortcut(Qt::ALT + Qt::Key_S);
202     m_buttonCursor->setToolTip(i18n("Selection Tool") + ' ' + m_buttonCursor->shortcut().toString());
203     connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
204
205     m_buttonText = m_toolbar->addAction(KIcon("insert-text"), QString());
206     m_buttonText->setCheckable(true);
207     m_buttonText->setShortcut(Qt::ALT + Qt::Key_T);
208     m_buttonText->setToolTip(i18n("Add Text") + ' ' + m_buttonText->shortcut().toString());
209     connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
210
211     m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), QString());
212     m_buttonRect->setCheckable(true);
213     m_buttonRect->setShortcut(Qt::ALT + Qt::Key_R);
214     m_buttonRect->setToolTip(i18n("Add Rectangle") + ' ' + m_buttonRect->shortcut().toString());
215     connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
216
217     m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), QString());
218     m_buttonImage->setCheckable(false);
219     m_buttonImage->setShortcut(Qt::ALT + Qt::Key_I);
220     m_buttonImage->setToolTip(i18n("Add Image") + ' ' + m_buttonImage->shortcut().toString());
221     connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
222
223     m_toolbar->addSeparator();
224
225     m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
226     m_buttonLoad->setCheckable(false);
227     m_buttonLoad->setShortcut(Qt::CTRL + Qt::Key_O);
228     connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
229
230     m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
231     m_buttonSave->setCheckable(false);
232     m_buttonSave->setShortcut(Qt::CTRL + Qt::Key_S);
233     connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
234
235     layout->addWidget(m_toolbar);
236
237     // initialize graphic scene
238     m_scene = new GraphicsSceneRectMove(this);
239     graphicsView->setScene(m_scene);
240     m_titledocument.setScene(m_scene, m_frameWidth, m_frameHeight);
241     connect(m_scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(slotChanged()));
242
243     // a gradient background
244     /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
245     gradient->setSpread(QGradient::ReflectSpread);
246     scene->setBackgroundBrush(*gradient);*/
247
248     m_frameImage = new QGraphicsPixmapItem();
249     QTransform qtrans;
250     qtrans.scale(2.0, 2.0);
251     m_frameImage->setTransform(qtrans);
252     m_frameImage->setZValue(-1200);
253     m_frameImage->setFlags(0);
254     displayBackgroundFrame();
255     graphicsView->scene()->addItem(m_frameImage);
256
257     connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
258     connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
259     connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
260     connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
261     //connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
262     connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
263     connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
264     connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
265
266     QPen framepen(Qt::DotLine);
267     framepen.setColor(Qt::red);
268
269     m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
270     m_frameBorder->setPen(framepen);
271     m_frameBorder->setZValue(-1100);
272     m_frameBorder->setBrush(Qt::transparent);
273     m_frameBorder->setFlags(0);
274     graphicsView->scene()->addItem(m_frameBorder);
275
276     // mbd: load saved settings
277     readChoices();
278
279     graphicsView->show();
280     //graphicsView->setRenderHint(QPainter::Antialiasing);
281     graphicsView->setInteractive(true);
282     //graphicsView->resize(400, 300);
283     kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
284     //toolBox->setItemEnabled(2, false);
285     m_startViewport = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
286     m_endViewport = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
287     m_startViewport->setData(0, m_frameWidth);
288     m_startViewport->setData(1, m_frameHeight);
289     m_endViewport->setData(0, m_frameWidth);
290     m_endViewport->setData(1, m_frameHeight);
291
292     if (!url.isEmpty()) loadTitle(url);
293     else {
294         slotTextTool();
295         QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
296     }
297     initAnimation();
298     connect(anim_start, SIGNAL(toggled(bool)), this, SLOT(slotAnimStart(bool)));
299     connect(anim_end, SIGNAL(toggled(bool)), this, SLOT(slotAnimEnd(bool)));
300 }
301
302 TitleWidget::~TitleWidget()
303 {
304     delete m_buttonRect;
305     delete m_buttonText;
306     delete m_buttonImage;
307     delete m_buttonCursor;
308     delete m_buttonSave;
309     delete m_buttonLoad;
310     delete m_unicodeAction;
311
312     delete m_unicodeDialog;
313     delete m_frameBorder;
314     delete m_frameImage;
315     delete m_startViewport;
316     delete m_endViewport;
317     delete m_scene;
318     delete m_signalMapper;
319 }
320
321 //static
322 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl, bool isClone)
323 {
324     QStringList result;
325     QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
326     KStandardDirs::makeDir(titlePath);
327     titlePath.append((isClone == false) ? "title" : "clone");
328     int counter = 0;
329     QString path;
330     while (path.isEmpty() || QFile::exists(path)) {
331         counter++;
332         path = titlePath + QString::number(counter).rightJustified(3, '0', false) + ".png";
333     }
334     result.append(((isClone == false) ? i18n("Title") : i18n("Clone")) + ' ' + QString::number(counter).rightJustified(3, '0', false));
335     result.append(path);
336     return result;
337 }
338
339 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
340 {
341     QStringList result;
342     QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
343     KStandardDirs::makeDir(titlePath);
344     return titlePath + titleName + ".png";
345 }
346
347 //virtual
348 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
349 {
350     //slotAdjustZoom();
351 }
352
353 void TitleWidget::slotTextTool()
354 {
355     m_scene->setTool(TITLE_TEXT);
356     showToolbars(TITLE_TEXT);
357     checkButton(TITLE_TEXT);
358 }
359
360 void TitleWidget::slotRectTool()
361 {
362     m_scene->setTool(TITLE_RECTANGLE);
363     showToolbars(TITLE_RECTANGLE);
364     checkButton(TITLE_RECTANGLE);
365 }
366
367 void TitleWidget::slotSelectTool()
368 {
369     m_scene->setTool(TITLE_SELECT);
370
371     // Find out which toolbars need to be shown, depending on selected item
372     TITLETOOL t = TITLE_SELECT;
373     QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
374     if (l.size() > 0) {
375         switch (l.at(0)->type()) {
376         case TEXTITEM:
377             t = TITLE_TEXT;
378             break;
379         case RECTITEM:
380             t = TITLE_RECTANGLE;
381             break;
382         case IMAGEITEM:
383             t = TITLE_IMAGE;
384             break;
385         }
386     }
387
388     enableToolbars(t);
389     if (t == TITLE_RECTANGLE && (l.at(0) == m_endViewport || l.at(0) == m_startViewport)) {
390         //graphicsView->centerOn(l.at(0));
391         t = TITLE_NONE;
392     }
393     showToolbars(t);
394
395     if (l.size() > 0) {
396         updateCoordinates(l.at(0));
397         updateDimension(l.at(0));
398         updateRotZoom(l.at(0));
399     }
400
401     checkButton(TITLE_SELECT);
402 }
403
404 void TitleWidget::slotImageTool()
405 {
406     KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.svg *.png *.jpg *.jpeg *.gif *.raw", this, i18n("Load Image"));
407     if (!url.isEmpty()) {
408         if (url.path().endsWith(".svg")) {
409             QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
410             svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
411             svg->setZValue(m_count++);
412             svg->setData(Qt::UserRole, url.path());
413             graphicsView->scene()->addItem(svg);
414         } else {
415             QPixmap pix(url.path());
416             QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
417             image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
418             image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
419             image->setData(Qt::UserRole, url.path());
420             image->setZValue(m_count++);
421             graphicsView->scene()->addItem(image);
422         }
423     }
424     m_scene->setTool(TITLE_SELECT);
425     showToolbars(TITLE_SELECT);
426     checkButton(TITLE_NONE);
427 }
428
429 void TitleWidget::showToolbars(TITLETOOL toolType)
430 {
431     switch (toolType) {
432     case TITLE_TEXT:
433         rect_properties->setHidden(true);
434         image_properties->setHidden(true);
435         no_properties->setHidden(true);
436         text_properties->setHidden(false);
437         break;
438     case TITLE_RECTANGLE:
439         image_properties->setHidden(true);
440         no_properties->setHidden(true);
441         text_properties->setHidden(true);
442         rect_properties->setHidden(false);
443         break;
444     case TITLE_IMAGE:
445         no_properties->setHidden(true);
446         text_properties->setHidden(true);
447         rect_properties->setHidden(true);
448         image_properties->setHidden(false);
449         break;
450     default:
451         text_properties->setHidden(true);
452         rect_properties->setHidden(true);
453         image_properties->setHidden(true);
454         no_properties->setHidden(false);
455         break;
456     }
457 }
458
459 void TitleWidget::enableToolbars(TITLETOOL toolType)
460 {
461     // TITLETOOL is defined in graphicsscenerectmove.h
462     bool bFrame = false;
463     bool bText = false;
464     bool bRect = false;
465     bool bImage = false;
466     bool bValue_w = false;
467     bool bValue_h = false;
468
469     switch (toolType) {
470     case TITLE_SELECT:
471         break;
472     case TITLE_TEXT:
473         bFrame = true;
474         bText = true;
475         break;
476     case TITLE_RECTANGLE:
477         bFrame = true;
478         bRect = true;
479         bValue_w = true;
480         bValue_h = true;
481         break;
482     case TITLE_IMAGE:
483         bFrame = true;
484         bValue_w = true;
485         bValue_h = true;
486         bImage = true;
487         break;
488     default:
489         break;
490     }
491     frame_properties->setEnabled(bFrame);
492     text_properties->setEnabled(bText);
493     rect_properties->setEnabled(bRect);
494     image_properties->setEnabled(bImage);
495     value_w->setEnabled(bValue_w);
496     value_h->setEnabled(bValue_h);
497 }
498
499 void TitleWidget::checkButton(TITLETOOL toolType)
500 {
501     bool bSelect = false;
502     bool bText = false;
503     bool bRect = false;
504     bool bImage = false;
505
506     switch (toolType) {
507     case TITLE_SELECT:
508         bSelect = true;
509         break;
510     case TITLE_TEXT:
511         bText = true;
512         break;
513     case TITLE_RECTANGLE:
514         bRect = true;
515         break;
516     case TITLE_IMAGE:
517         bImage = true;
518         break;
519     case TITLE_NONE:
520         break;
521     }
522
523     m_buttonCursor->setChecked(bSelect);
524     m_buttonText->setChecked(bText);
525     m_buttonRect->setChecked(bRect);
526     m_buttonImage->setChecked(bImage);
527 }
528
529 void TitleWidget::displayBackgroundFrame()
530 {
531     if (!displayBg->isChecked()) {
532         QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
533         QPixmap pattern(20, 20);
534         pattern.fill();
535         QColor bgcolor(210, 210, 210);
536         QPainter p;
537         p.begin(&pattern);
538         p.fillRect(QRect(0, 0, 10, 10), bgcolor);
539         p.fillRect(QRect(10, 10, 20, 20), bgcolor);
540         p.end();
541         QBrush br(pattern);
542
543         p.begin(&bg);
544         p.fillRect(bg.rect(), br);
545         p.end();
546         m_frameImage->setPixmap(bg);
547     } else {
548         m_frameImage->setPixmap(QPixmap::fromImage(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2)));
549     }
550 }
551
552 void TitleWidget::initAnimation()
553 {
554     align_box->setEnabled(false);
555     QPen startpen(Qt::DotLine);
556     QPen endpen(Qt::DashDotLine);
557     startpen.setColor(QColor(100, 200, 100, 140));
558     endpen.setColor(QColor(200, 100, 100, 140));
559
560     m_startViewport->setPen(startpen);
561     m_endViewport->setPen(endpen);
562
563     m_startViewport->setZValue(-1000);
564     m_endViewport->setZValue(-1000);
565
566     m_startViewport->setFlags(0);
567     m_endViewport->setFlags(0);
568
569     graphicsView->scene()->addItem(m_startViewport);
570     graphicsView->scene()->addItem(m_endViewport);
571
572     connect(keep_aspect, SIGNAL(toggled(bool)), this, SLOT(slotKeepAspect(bool)));
573     connect(resize50, SIGNAL(clicked()), this, SLOT(slotResize50()));
574     connect(resize100, SIGNAL(clicked()), this, SLOT(slotResize100()));
575     connect(resize200, SIGNAL(clicked()), this, SLOT(slotResize200()));
576 }
577
578 void TitleWidget::slotUpdateZoom(int pos)
579 {
580     m_scene->setZoom((double) pos / 100);
581     zoom_label->setText(QString::number(pos) + '%');
582 }
583
584 void TitleWidget::slotZoom(bool up)
585 {
586     int pos = zoom_slider->value();
587     if (up) pos++;
588     else pos--;
589     zoom_slider->setValue(pos);
590 }
591
592 void TitleWidget::slotAdjustZoom()
593 {
594     /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
595     double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
596     if (scalex > scaley) scalex = scaley;
597     int zoompos = (int)(scalex * 7 + 0.5);*/
598     graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
599     int zoompos = graphicsView->matrix().m11() * 100;
600     zoom_slider->setValue(zoompos);
601     graphicsView->centerOn(m_frameBorder);
602 }
603
604 void TitleWidget::slotZoomOneToOne()
605 {
606     zoom_slider->setValue(100);
607     graphicsView->centerOn(m_frameBorder);
608 }
609
610 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
611 {
612     updateAxisButtons(rect); // back to default
613
614     QColor f = rectFColor->color();
615     f.setAlpha(rectFAlpha->value());
616     QPen penf(f);
617     penf.setWidth(rectLineWidth->value());
618     rect->setPen(penf);
619     QColor b = rectBColor->color();
620     b.setAlpha(rectBAlpha->value());
621     rect->setBrush(QBrush(b));
622     rect->setZValue(m_count++);
623     //setCurrentItem(rect);
624     //graphicsView->setFocus();
625 }
626
627 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
628 {
629     updateAxisButtons(tt); // back to default
630
631     QFont font = font_family->currentFont();
632     font.setPixelSize(font_size->value());
633     // mbd: issue 551:
634     font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
635     font.setItalic(buttonItalic->isChecked());
636     font.setUnderline(buttonUnder->isChecked());
637
638     tt->setFont(font);
639     QColor color = fontColorButton->color();
640     color.setAlpha(textAlpha->value());
641     tt->setDefaultTextColor(color);
642     tt->setZValue(m_count++);
643     setCurrentItem(tt);
644 }
645
646 void TitleWidget::setFontBoxWeight(int weight)
647 {
648     int index = font_weight_box->findData(weight);
649     if (index < 0) {
650         index = font_weight_box->findData(QFont::Normal);
651     }
652     font_weight_box->setCurrentIndex(index);
653 }
654
655 void TitleWidget::setCurrentItem(QGraphicsItem *item)
656 {
657     m_scene->setSelectedItem(item);
658 }
659
660 void TitleWidget::zIndexChanged(int v)
661 {
662     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
663     if (l.size() >= 1) {
664         l[0]->setZValue(v);
665     }
666 }
667
668 void TitleWidget::selectionChanged()
669 {
670     if (m_scene->tool() != TITLE_SELECT) return;
671     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
672     //toolBox->setItemEnabled(2, false);
673     //toolBox->setItemEnabled(3, false);
674     value_x->blockSignals(true);
675     value_y->blockSignals(true);
676     value_w->blockSignals(true);
677     value_h->blockSignals(true);
678     itemzoom->blockSignals(true);
679     itemrotate->blockSignals(true);
680     if (l.size() == 0) {
681         bool blockX = !origin_x_left->signalsBlocked();
682         bool blockY = !origin_y_top->signalsBlocked();
683         if (blockX) origin_x_left->blockSignals(true);
684         if (blockY) origin_y_top->blockSignals(true);
685         origin_x_left->setChecked(false);
686         origin_y_top->setChecked(false);
687         updateTextOriginX();
688         updateTextOriginY();
689         enableToolbars(TITLE_NONE);
690         if (blockX) origin_x_left->blockSignals(false);
691         if (blockY) origin_y_top->blockSignals(false);
692         itemzoom->setEnabled(false);
693         itemrotate->setEnabled(false);
694     } else if (l.size() == 1) {
695         if (l.at(0) != m_startViewport && l.at(0) != m_endViewport) {
696             itemzoom->setEnabled(true);
697             itemrotate->setEnabled(true);
698         } else {
699             itemzoom->setEnabled(false);
700             itemrotate->setEnabled(false);
701         }
702         if (l.at(0)->type() == TEXTITEM) {
703             showToolbars(TITLE_TEXT);
704             QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
705             //if (l[0]->hasFocus())
706             //toolBox->setCurrentIndex(0);
707             //toolBox->setItemEnabled(2, true);
708             font_size->blockSignals(true);
709             font_family->blockSignals(true);
710             font_weight_box->blockSignals(true);
711             buttonItalic->blockSignals(true);
712             buttonUnder->blockSignals(true);
713             fontColorButton->blockSignals(true);
714             textAlpha->blockSignals(true);
715             buttonAlignLeft->blockSignals(true);
716             buttonAlignRight->blockSignals(true);
717             buttonAlignNone->blockSignals(true);
718             buttonAlignCenter->blockSignals(true);
719
720             QFont font = i->font();
721             font_family->setCurrentFont(font);
722             font_size->setValue(font.pixelSize());
723             buttonItalic->setChecked(font.italic());
724             buttonUnder->setChecked(font.underline());
725             setFontBoxWeight(font.weight());
726
727             QColor color = i->defaultTextColor();
728             fontColorButton->setColor(color);
729             textAlpha->setValue(color.alpha());
730
731             QTextCursor cur = i->textCursor();
732             QTextBlockFormat format = cur.blockFormat();
733             if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
734             else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
735             else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
736             else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
737
738             font_size->blockSignals(false);
739             font_family->blockSignals(false);
740             font_weight_box->blockSignals(false);
741             buttonItalic->blockSignals(false);
742             buttonUnder->blockSignals(false);
743             fontColorButton->blockSignals(false);
744             textAlpha->blockSignals(false);
745             buttonAlignLeft->blockSignals(false);
746             buttonAlignRight->blockSignals(false);
747             buttonAlignNone->blockSignals(false);
748             buttonAlignCenter->blockSignals(false);
749
750             updateAxisButtons(i);
751             updateCoordinates(i);
752             updateDimension(i);
753             enableToolbars(TITLE_TEXT);
754
755         } else if ((l.at(0))->type() == RECTITEM) {
756             showToolbars(TITLE_RECTANGLE);
757             settingUp = true;
758             QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
759             if (rec == m_startViewport || rec == m_endViewport) {
760                 /*toolBox->setCurrentIndex(3);
761                 toolBox->widget(0)->setEnabled(false);
762                 toolBox->widget(1)->setEnabled(false);*/
763                 enableToolbars(TITLE_NONE);
764             } else {
765                 /*toolBox->widget(0)->setEnabled(true);
766                 toolBox->widget(1)->setEnabled(true);
767                 toolBox->setCurrentIndex(0);*/
768                 //toolBox->setItemEnabled(3, true);
769                 rectFAlpha->setValue(rec->pen().color().alpha());
770                 rectBAlpha->setValue(rec->brush().color().alpha());
771                 //kDebug() << rec->brush().color().alpha();
772                 QColor fcol = rec->pen().color();
773                 QColor bcol = rec->brush().color();
774                 //fcol.setAlpha(255);
775                 //bcol.setAlpha(255);
776                 rectFColor->setColor(fcol);
777                 rectBColor->setColor(bcol);
778                 settingUp = false;
779                 rectLineWidth->setValue(rec->pen().width());
780                 enableToolbars(TITLE_RECTANGLE);
781             }
782
783             updateAxisButtons(l.at(0));
784             updateCoordinates(rec);
785             updateDimension(rec);
786
787         } else if (l.at(0)->type() == IMAGEITEM) {
788             showToolbars(TITLE_IMAGE);
789
790             updateCoordinates(l.at(0));
791             updateDimension(l.at(0));
792
793             enableToolbars(TITLE_IMAGE);
794
795         } else {
796             //toolBox->setCurrentIndex(0);
797             showToolbars(TITLE_NONE);
798             enableToolbars(TITLE_NONE);
799             /*frame_properties->setEnabled(false);
800             text_properties->setEnabled(false);
801             rect_properties->setEnabled(false);*/
802         }
803         zValue->setValue((int)l.at(0)->zValue());
804         itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
805         itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
806         value_x->blockSignals(false);
807         value_y->blockSignals(false);
808         value_w->blockSignals(false);
809         value_h->blockSignals(false);
810         itemzoom->blockSignals(false);
811         itemrotate->blockSignals(false);
812     }
813 }
814
815 void TitleWidget::slotValueChanged(int type)
816 {
817     QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
818     if (l.size() > 0 && l.at(0)->type() == IMAGEITEM) {
819
820         int val = 0;
821         switch (type) {
822         case ValueWidth:
823             val = value_w->value();
824             break;
825         case ValueHeight:
826             val = value_h->value();
827             break;
828         }
829
830         QGraphicsItem *i = l.at(0);
831         Transform t = m_transformations.value(i);
832
833         // Ratio width:height
834         double phi = (double) i->boundingRect().width() / i->boundingRect().height();
835         double alpha = (double) t.rotate / 180.0 * M_PI;
836
837         // New length
838         double length = val;
839
840         // Scaling factor
841         double scale = 1;
842
843         switch (type) {
844         case ValueWidth:
845             // Add 0.5 because otherwise incrementing by 1 might have no effect
846             length = val / (cos(alpha) + 1 / phi * sin(alpha)) + 0.5;
847             scale = length / i->boundingRect().width();
848             break;
849         case ValueHeight:
850             length = val / (phi * sin(alpha) + cos(alpha)) + 0.5;
851             scale = length / i->boundingRect().height();
852             break;
853         }
854
855         t.scalex = scale;
856         t.scaley = scale;
857         QTransform qtrans;
858         qtrans.scale(scale, scale);
859         qtrans.rotate(t.rotate);
860         i->setTransform(qtrans);
861         m_transformations[i] = t;
862
863         updateDimension(i);
864         updateRotZoom(i);
865     }
866 }
867
868 /** \brief Updates position/size of the selected item when a value
869  * of an item (coordinates, size) has changed */
870 void TitleWidget::slotAdjustSelectedItem()
871 {
872     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
873     if (l.size() >= 1) {
874         if (l.at(0)->type() == RECTITEM) {
875             //rect item
876             QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
877             updatePosition(rec);
878             rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
879         } else if (l.at(0)->type() == TEXTITEM) {
880             //text item
881             updatePosition(l.at(0));
882         } else if (l.at(0)->type() == IMAGEITEM) {
883             //image item
884             updatePosition(l.at(0));
885         }
886     }
887 }
888
889 /** \brief Updates width/height int the text fields, regarding transformation matrix */
890 void TitleWidget::updateDimension(QGraphicsItem *i)
891 {
892     value_w->blockSignals(true);
893     value_h->blockSignals(true);
894
895
896     if (i->type() == IMAGEITEM) {
897         // Get multipliers for rotation/scaling
898
899         /*Transform t = m_transformations.value(i);
900         QRectF r = i->boundingRect();
901         int width = (int) ( abs(r.width()*t.scalex * cos(t.rotate/180.0*M_PI))
902                     + abs(r.height()*t.scaley * sin(t.rotate/180.0*M_PI)) );
903         int height = (int) ( abs(r.height()*t.scaley * cos(t.rotate/180*M_PI))
904                     + abs(r.width()*t.scalex * sin(t.rotate/180*M_PI)) );*/
905
906         value_w->setValue(i->sceneBoundingRect().width());
907         value_h->setValue(i->sceneBoundingRect().height());
908     } else if (i->type() == RECTITEM) {
909         QGraphicsRectItem *r = static_cast <QGraphicsRectItem *>(i);
910         value_w->setValue((int) r->rect().width());
911         value_h->setValue((int) r->rect().height());
912     } else if (i->type() == TEXTITEM) {
913         QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(i);
914         value_w->setValue((int) t->boundingRect().width());
915         value_h->setValue((int) t->boundingRect().height());
916     }
917
918     value_w->blockSignals(false);
919     value_h->blockSignals(false);
920 }
921
922 /** \brief Updates the coordinates in the text fields from the item */
923 void TitleWidget::updateCoordinates(QGraphicsItem *i)
924 {
925     // Block signals emitted by this method
926     value_x->blockSignals(true);
927     value_y->blockSignals(true);
928
929     if (i->type() == TEXTITEM) {
930
931         QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
932
933         // Set the correct x coordinate value
934         if (origin_x_left->isChecked()) {
935             // Origin (0 point) is at m_frameWidth, coordinate axis is inverted
936             value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->boundingRect().width()));
937         } else {
938             // Origin is at 0 (default)
939             value_x->setValue((int) rec->pos().x());
940         }
941
942         // Same for y
943         if (origin_y_top->isChecked()) {
944             value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->boundingRect().height()));
945         } else {
946             value_y->setValue((int) rec->pos().y());
947         }
948
949     } else if (i->type() == RECTITEM) {
950
951         QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
952
953         if (origin_x_left->isChecked()) {
954             // Origin (0 point) is at m_frameWidth
955             value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->rect().width()));
956         } else {
957             // Origin is at 0 (default)
958             value_x->setValue((int) rec->pos().x());
959         }
960
961         if (origin_y_top->isChecked()) {
962             value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->rect().height()));
963         } else {
964             value_y->setValue((int) rec->pos().y());
965         }
966
967     } else if (i->type() == IMAGEITEM) {
968
969         if (origin_x_left->isChecked()) {
970             value_x->setValue((int)(m_frameWidth - i->pos().x() - i->sceneBoundingRect().width()));
971         } else {
972             value_x->setValue((int) i->pos().x());
973         }
974
975         if (origin_y_top->isChecked()) {
976             value_y->setValue((int)(m_frameHeight - i->pos().y() - i->sceneBoundingRect().height()));
977         } else {
978             value_y->setValue((int) i->pos().y());
979         }
980
981     }
982
983     // Stop blocking signals now
984     value_x->blockSignals(false);
985     value_y->blockSignals(false);
986 }
987
988 void TitleWidget::updateRotZoom(QGraphicsItem *i)
989 {
990     itemzoom->blockSignals(true);
991     itemrotate->blockSignals(false);
992
993     Transform t = m_transformations.value(i);
994     itemzoom->setValue((int)(t.scalex * 100.0 + 0.5));
995     itemrotate->setValue((int)(t.rotate));
996
997     itemzoom->blockSignals(false);
998     itemrotate->blockSignals(false);
999 }
1000
1001 /** \brief Updates the position of an item by reading coordinates from the text fields */
1002 void TitleWidget::updatePosition(QGraphicsItem *i)
1003 {
1004     if (i->type() == TEXTITEM) {
1005         QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
1006
1007         int posX;
1008         if (origin_x_left->isChecked()) {
1009             /* Origin of the x axis is at m_frameWidth,
1010              * and distance from right border of the item to the right
1011              * border of the frame is taken.
1012              * See comment to slotOriginXClicked().
1013              */
1014             posX = m_frameWidth - value_x->value() - rec->boundingRect().width();
1015         } else {
1016             posX = value_x->value();
1017         }
1018
1019         int posY;
1020         if (origin_y_top->isChecked()) {
1021             /* Same for y axis */
1022             posY = m_frameHeight - value_y->value() - rec->boundingRect().height();
1023         } else {
1024             posY = value_y->value();
1025         }
1026
1027         rec->setPos(posX, posY);
1028
1029     } else if (i->type() == RECTITEM) {
1030
1031         QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
1032
1033         int posX;
1034         if (origin_x_left->isChecked()) {
1035             posX = m_frameWidth - value_x->value() - rec->rect().width();
1036         } else {
1037             posX = value_x->value();
1038         }
1039
1040         int posY;
1041         if (origin_y_top->isChecked()) {
1042             posY = m_frameHeight - value_y->value() - rec->rect().height();
1043         } else {
1044             posY = value_y->value();
1045         }
1046
1047         rec->setPos(posX, posY);
1048
1049     } else if (i->type() == IMAGEITEM) {
1050         int posX;
1051         if (origin_x_left->isChecked()) {
1052             // Use the sceneBoundingRect because this also regards transformations like zoom
1053             posX = m_frameWidth - value_x->value() - i->sceneBoundingRect().width();
1054         } else {
1055             posX = value_x->value();
1056         }
1057
1058         int posY;
1059         if (origin_y_top->isChecked()) {
1060             posY = m_frameHeight - value_y->value() - i->sceneBoundingRect().height();
1061         } else {
1062             posY = value_y->value();
1063         }
1064
1065         i->setPos(posX, posY);
1066
1067     }
1068
1069 }
1070
1071 void TitleWidget::updateTextOriginX()
1072 {
1073     if (origin_x_left->isChecked()) {
1074         origin_x_left->setText(i18n("\u2212X"));
1075     } else {
1076         origin_x_left->setText(i18n("+X"));
1077     }
1078 }
1079
1080 void TitleWidget::slotOriginXClicked()
1081 {
1082     // Update the text displayed on the button.
1083     updateTextOriginX();
1084
1085     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1086     if (l.size() >= 1) {
1087         updateCoordinates(l.at(0));
1088
1089         // Remember x axis setting
1090         l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked() ?
1091                          TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1092     }
1093     graphicsView->setFocus();
1094 }
1095
1096 void TitleWidget::updateTextOriginY()
1097 {
1098     if (origin_y_top->isChecked()) {
1099         origin_y_top->setText(i18n("\u2212Y"));
1100     } else {
1101         origin_y_top->setText(i18n("+Y"));
1102     }
1103 }
1104
1105 void TitleWidget::slotOriginYClicked()
1106 {
1107     // Update the text displayed on the button.
1108     updateTextOriginY();
1109
1110     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1111     if (l.size() >= 1) {
1112         updateCoordinates(l.at(0));
1113
1114         l.at(0)->setData(TitleDocument::OriginYTop, origin_y_top->isChecked() ?
1115                          TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1116
1117     }
1118     graphicsView->setFocus();
1119 }
1120
1121 void TitleWidget::updateAxisButtons(QGraphicsItem *i)
1122 {
1123     int xAxis = i->data(TitleDocument::OriginXLeft).toInt();
1124     int yAxis = i->data(TitleDocument::OriginYTop).toInt();
1125     origin_x_left->blockSignals(true);
1126     origin_y_top->blockSignals(true);
1127
1128     if (xAxis == TitleDocument::AxisInverted) {
1129         origin_x_left->setChecked(true);
1130     } else {
1131         origin_x_left->setChecked(false);
1132     }
1133     updateTextOriginX();
1134
1135     if (yAxis == TitleDocument::AxisInverted) {
1136         origin_y_top->setChecked(true);
1137     } else {
1138         origin_y_top->setChecked(false);
1139     }
1140     updateTextOriginY();
1141
1142     origin_x_left->blockSignals(false);
1143     origin_y_top->blockSignals(false);
1144 }
1145
1146 void TitleWidget::slotChangeBackground()
1147 {
1148     QColor color = kcolorbutton->color();
1149     color.setAlpha(horizontalSlider->value());
1150     m_frameBorder->setBrush(QBrush(color));
1151 }
1152
1153 /**
1154  * Something (yeah) has changed in our QGraphicsScene.
1155  */
1156 void TitleWidget::slotChanged()
1157 {
1158     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1159     if (l.size() >= 1 && l.at(0)->type() == TEXTITEM) {
1160         textChanged(static_cast <QGraphicsTextItem *>(l.at(0)));
1161     }
1162 }
1163
1164 /**
1165  * If the user has set origin_x_left (everything also for y),
1166  * we need to look whether a text element has been selected. If yes,
1167  * we need to ensure that the right border of the text field
1168  * remains fixed also when some text has been entered.
1169  *
1170  * This is also known as right-justified, with the difference that
1171  * it is not valid for text but for its boundingRect. Text may still
1172  * be left-justified.
1173  */
1174 void TitleWidget::textChanged(QGraphicsTextItem *i)
1175 {
1176
1177     updateDimension(i);
1178
1179     if (origin_x_left->isChecked() || origin_y_top->isChecked()) {
1180
1181         if (!i->toPlainText().isEmpty()) {
1182             updatePosition(i);
1183         } else {
1184             /*
1185              * Don't do anything if the string is empty. If the position
1186              * would be updated here, a newly created text field would
1187              * be set to the position of the last selected text field.
1188              */
1189         }
1190     }
1191 }
1192
1193 void TitleWidget::slotInsertUnicode()
1194 {
1195     m_unicodeDialog->exec();
1196 }
1197
1198 void TitleWidget::slotInsertUnicodeString(QString text)
1199 {
1200     QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
1201     if (l.size() > 0) {
1202         if (l.at(0)->type() == TEXTITEM) {
1203             QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(l.at(0));
1204             t->textCursor().insertText(text);
1205         }
1206     }
1207 }
1208
1209 void TitleWidget::slotUpdateText()
1210 {
1211     QFont font = font_family->currentFont();
1212     font.setPixelSize(font_size->value());
1213     font.setItalic(buttonItalic->isChecked());
1214     font.setUnderline(buttonUnder->isChecked());
1215     font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1216     QColor color = fontColorButton->color();
1217     color.setAlpha(textAlpha->value());
1218
1219     QGraphicsTextItem* item = NULL;
1220     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1221     if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
1222         item = static_cast <QGraphicsTextItem *>(l.at(0));
1223     }
1224     if (!item) return;
1225     //if (item->textCursor().selection ().isEmpty())
1226     QTextCursor cur = item->textCursor();
1227     QTextBlockFormat format = cur.blockFormat();
1228     if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
1229         item->setTextWidth(item->boundingRect().width());
1230         if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
1231         else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
1232         else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
1233     } else {
1234         format.setAlignment(Qt::AlignLeft);
1235         item->setTextWidth(-1);
1236     }
1237
1238     {
1239         item->setFont(font);
1240         item->setDefaultTextColor(color);
1241         cur.select(QTextCursor::Document);
1242         cur.setBlockFormat(format);
1243         item->setTextCursor(cur);
1244         cur.clearSelection();
1245         item->setTextCursor(cur);
1246
1247     }
1248 }
1249
1250 void TitleWidget::rectChanged()
1251 {
1252     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1253     if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
1254         QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
1255         QColor f = rectFColor->color();
1256         f.setAlpha(rectFAlpha->value());
1257         QPen penf(f);
1258         penf.setWidth(rectLineWidth->value());
1259         rec->setPen(penf);
1260         QColor b = rectBColor->color();
1261         b.setAlpha(rectBAlpha->value());
1262         rec->setBrush(QBrush(b));
1263     }
1264 }
1265
1266 void TitleWidget::itemScaled(int val)
1267 {
1268     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1269     if (l.size() == 1) {
1270         Transform x = m_transformations.value(l.at(0));
1271         x.scalex = (double)val / 100.0;
1272         x.scaley = (double)val / 100.0;
1273         QTransform qtrans;
1274         qtrans.scale(x.scalex, x.scaley);
1275         qtrans.rotate(x.rotate);
1276         l[0]->setTransform(qtrans);
1277         m_transformations[l.at(0)] = x;
1278         updateDimension(l.at(0));
1279     }
1280 }
1281
1282 void TitleWidget::itemRotate(int val)
1283 {
1284     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1285     if (l.size() == 1) {
1286         Transform x = m_transformations[l.at(0)];
1287         x.rotate = (double)val;
1288         QTransform qtrans;
1289         qtrans.scale(x.scalex, x.scaley);
1290         qtrans.rotate(x.rotate);
1291         l[0]->setTransform(qtrans);
1292         m_transformations[l.at(0)] = x;
1293         updateDimension(l.at(0));
1294     }
1295 }
1296
1297 void TitleWidget::itemHCenter()
1298 {
1299     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1300     if (l.size() == 1) {
1301         QGraphicsItem *item = l.at(0);
1302         QRectF br = item->sceneBoundingRect();
1303         int width = (int)br.width();
1304         int newPos = (int)((m_frameWidth - width) / 2);
1305         newPos += item->pos().x() - br.left(); // Check item transformation
1306         item->setPos(newPos, item->pos().y());
1307         updateCoordinates(item);
1308     }
1309 }
1310
1311 void TitleWidget::itemVCenter()
1312 {
1313     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1314     if (l.size() == 1) {
1315         QGraphicsItem *item = l.at(0);
1316         QRectF br = item->sceneBoundingRect();
1317         int height = (int)br.height();
1318         int newPos = (int)((m_frameHeight - height) / 2);
1319         newPos += item->pos().y() - br.top(); // Check item transformation
1320         item->setPos(item->pos().x(), newPos);
1321         updateCoordinates(item);
1322     }
1323 }
1324
1325 void TitleWidget::itemTop()
1326 {
1327     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1328     if (l.size() == 1) {
1329         QGraphicsItem *item = l.at(0);
1330         QRectF br = item->sceneBoundingRect();
1331         double diff;
1332         if (br.top() > 0) diff = -br.top();
1333         else diff = -br.bottom();
1334         item->moveBy(0, diff);
1335         updateCoordinates(item);
1336     }
1337 }
1338
1339 void TitleWidget::itemBottom()
1340 {
1341     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1342     if (l.size() == 1) {
1343         QGraphicsItem *item = l.at(0);
1344         QRectF br = item->sceneBoundingRect();
1345         double diff;
1346         if (br.bottom() > m_frameHeight) diff = m_frameHeight - br.top();
1347         else diff = m_frameHeight - br.bottom();
1348         item->moveBy(0, diff);
1349         updateCoordinates(item);
1350     }
1351 }
1352
1353 void TitleWidget::itemLeft()
1354 {
1355     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1356     if (l.size() == 1) {
1357         QGraphicsItem *item = l.at(0);
1358         QRectF br = item->sceneBoundingRect();
1359         double diff;
1360         if (br.left() > 0) diff = -br.left();
1361         else diff = -br.right();
1362         item->moveBy(diff, 0);
1363         updateCoordinates(item);
1364     }
1365 }
1366
1367 void TitleWidget::itemRight()
1368 {
1369     QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1370     if (l.size() == 1) {
1371         QGraphicsItem *item = l.at(0);
1372         QRectF br = item->sceneBoundingRect();
1373         double diff;
1374         if (br.right() < m_frameWidth) diff = m_frameWidth - br.right();
1375         else diff = m_frameWidth - br.left();
1376         item->moveBy(diff, 0);
1377         updateCoordinates(item);
1378     }
1379 }
1380
1381 void TitleWidget::setupViewports()
1382 {
1383     //double aspect_ratio = 4.0 / 3.0;//read from project
1384     //better zoom centered, but render uses only the created rect, so no problem to change the zoom function
1385     /*QRectF sp(0, 0, startViewportSize->value() * m_frameWidth / 100.0 , startViewportSize->value()* m_frameHeight / 100.0);
1386     QRectF ep(0, 0, endViewportSize->value() * m_frameWidth / 100.0, endViewportSize->value() * m_frameHeight / 100.0);
1387     // use a polygon thiat uses 16:9 and 4:3 rects forpreview the size in all aspect ratios ?
1388     QPolygonF spoly(sp);
1389     QPolygonF epoly(ep);
1390     spoly.translate(startViewportX->value(), startViewportY->value());
1391     epoly.translate(endViewportX->value(), endViewportY->value());
1392     m_startViewport->setPolygon(spoly);
1393     m_endViewport->setPolygon(epoly);
1394     if (! insertingValues) {
1395         m_startViewport->setData(0, startViewportX->value());
1396         m_startViewport->setData(1, startViewportY->value());
1397         m_startViewport->setData(2, startViewportSize->value());
1398
1399         m_endViewport->setData(0, endViewportX->value());
1400         m_endViewport->setData(1, endViewportY->value());
1401         m_endViewport->setData(2, endViewportSize->value());
1402     }*/
1403 }
1404
1405 void TitleWidget::loadTitle(KUrl url)
1406 {
1407     if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this, i18n("Load Title"));
1408     if (!url.isEmpty()) {
1409         QList<QGraphicsItem *> items = m_scene->items();
1410         for (int i = 0; i < items.size(); i++) {
1411             if (items.at(i)->zValue() > -1000) delete items.at(i);
1412         }
1413         m_scene->clearTextSelection();
1414         QDomDocument doc;
1415         QString tmpfile;
1416
1417         if (KIO::NetAccess::download(url, tmpfile, 0)) {
1418             QFile file(tmpfile);
1419             if (file.open(QIODevice::ReadOnly)) {
1420                 doc.setContent(&file, false);
1421                 file.close();
1422             } else return;
1423             KIO::NetAccess::removeTempFile(tmpfile);
1424         }
1425         setXml(doc);
1426
1427         /*int out;
1428         m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport, &out) + 1;
1429         adjustFrameSize();
1430         title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1431         insertingValues = true;
1432         startViewportX->setValue(m_startViewport->data(0).toInt());
1433         startViewportY->setValue(m_startViewport->data(1).toInt());
1434         startViewportSize->setValue(m_startViewport->data(2).toInt());
1435         endViewportX->setValue(m_endViewport->data(0).toInt());
1436         endViewportY->setValue(m_endViewport->data(1).toInt());
1437         endViewportSize->setValue(m_endViewport->data(2).toInt());
1438
1439         insertingValues = false;
1440         slotSelectTool();
1441         slotAdjustZoom();*/
1442     }
1443 }
1444
1445 void TitleWidget::saveTitle(KUrl url)
1446 {
1447     if (anim_start->isChecked()) slotAnimStart(false);
1448     if (anim_end->isChecked()) slotAnimEnd(false);
1449     if (url.isEmpty()) {
1450         KFileDialog *fs = new KFileDialog(KUrl(m_projectTitlePath), "application/x-kdenlivetitle",this);
1451         fs->setOperationMode(KFileDialog::Saving);
1452         fs->setMode(KFile::File);
1453         fs->setConfirmOverwrite(true);
1454         fs->setKeepLocation(true);
1455         fs->exec();
1456         url = fs->selectedUrl();
1457         delete fs;
1458     }
1459     if (!url.isEmpty()) {
1460         if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport, m_tc.getFrameCount(title_duration->text())) == false)
1461             KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
1462     }
1463 }
1464
1465 QDomDocument TitleWidget::xml()
1466 {
1467     QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
1468     doc.documentElement().setAttribute("out", m_tc.getFrameCount(title_duration->text()));
1469     return doc;
1470 }
1471
1472 int TitleWidget::duration() const
1473 {
1474     return m_tc.getFrameCount(title_duration->text());
1475 }
1476
1477 void TitleWidget::setXml(QDomDocument doc)
1478 {
1479     int out;
1480     m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport, &out);
1481     adjustFrameSize();
1482     title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1483     /*if (doc.documentElement().hasAttribute("out")) {
1484     GenTime duration = GenTime(doc.documentElement().attribute("out").toDouble() / 1000.0);
1485     title_duration->setText(m_tc.getTimecode(duration));
1486     }
1487     else title_duration->setText(m_tc.getTimecode(GenTime(5000)));*/
1488
1489     QDomElement e = doc.documentElement();
1490     m_transformations.clear();
1491     QList <QGraphicsItem *> items = graphicsView->scene()->items();
1492     const double PI = 4.0 * atan(1.0);
1493     for (int i = 0; i < items.count(); i++) {
1494         QTransform t = items.at(i)->transform();
1495         Transform x;
1496         x.scalex = t.m11();
1497         x.scaley = t.m22();
1498         x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
1499         m_transformations[items.at(i)] = x;
1500     }
1501     // mbd: Update the GUI color selectors to match the stuff from the loaded document
1502     QColor background_color = m_titledocument.getBackgroundColor();
1503     horizontalSlider->blockSignals(true);
1504     kcolorbutton->blockSignals(true);
1505     horizontalSlider->setValue(background_color.alpha());
1506     background_color.setAlpha(255);
1507     kcolorbutton->setColor(background_color);
1508     horizontalSlider->blockSignals(false);
1509     kcolorbutton->blockSignals(false);
1510
1511     /*startViewportX->setValue(m_startViewport->data(0).toInt());
1512     startViewportY->setValue(m_startViewport->data(1).toInt());
1513     startViewportSize->setValue(m_startViewport->data(2).toInt());
1514     endViewportX->setValue(m_endViewport->data(0).toInt());
1515     endViewportY->setValue(m_endViewport->data(1).toInt());
1516     endViewportSize->setValue(m_endViewport->data(2).toInt());*/
1517
1518     QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
1519     slotSelectTool();
1520 }
1521
1522 /** \brief Connected to the accepted signal - calls writeChoices */
1523 void TitleWidget::slotAccepted()
1524 {
1525     if (anim_start->isChecked()) slotAnimStart(false);
1526     if (anim_end->isChecked()) slotAnimEnd(false);
1527     writeChoices();
1528 }
1529
1530 /** \brief Store the current choices of font, background and rect values */
1531 void TitleWidget::writeChoices()
1532 {
1533     // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1534     KSharedConfigPtr config = KGlobal::config();
1535     KConfigGroup titleConfig(config, "TitleWidget");
1536     // Write the entries
1537     titleConfig.writeEntry("font_family", font_family->currentFont());
1538     //titleConfig.writeEntry("font_size", font_size->value());
1539     titleConfig.writeEntry("font_pixel_size", font_size->value());
1540     titleConfig.writeEntry("font_color", fontColorButton->color());
1541     titleConfig.writeEntry("font_alpha", textAlpha->value());
1542     titleConfig.writeEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1543     titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
1544     titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
1545
1546     titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
1547     titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
1548     titleConfig.writeEntry("rect_background_color", rectBColor->color());
1549     titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
1550     titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
1551
1552     titleConfig.writeEntry("background_color", kcolorbutton->color());
1553     titleConfig.writeEntry("background_alpha", horizontalSlider->value());
1554
1555     //! \todo Not sure if I should sync - it is probably safe to do it
1556     config->sync();
1557
1558 }
1559
1560 /** \brief Read the last stored choices into the dialog */
1561 void TitleWidget::readChoices()
1562 {
1563     // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1564     KSharedConfigPtr config = KGlobal::config();
1565     KConfigGroup titleConfig(config, "TitleWidget");
1566     // read the entries
1567     font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
1568     font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
1569     fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
1570     textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
1571     int weight;
1572     if (titleConfig.readEntry("font_bold", false)) weight = QFont::Bold;
1573     else weight = titleConfig.readEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1574     setFontBoxWeight(weight);
1575     buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
1576     buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
1577
1578     rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
1579     rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
1580     rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
1581     rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
1582     rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
1583
1584     kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
1585     horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
1586 }
1587
1588 void TitleWidget::adjustFrameSize()
1589 {
1590     m_frameWidth = m_titledocument.frameWidth();
1591     m_frameHeight = m_titledocument.frameHeight();
1592     m_frameBorder->setRect(0, 0, m_frameWidth, m_frameHeight);
1593     displayBackgroundFrame();
1594 }
1595
1596 void TitleWidget::slotAnimStart(bool anim)
1597 {
1598     if (anim && anim_end->isChecked()) {
1599         anim_end->setChecked(false);
1600         m_endViewport->setZValue(-1000);
1601         m_endViewport->setBrush(QBrush());
1602     }
1603     slotSelectTool();
1604     QList<QGraphicsItem *> list = m_scene->items();
1605     for (int i = 0; i < list.count(); i++) {
1606         if (list.at(i)->zValue() > -1000) {
1607             list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1608             list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1609         }
1610     }
1611     align_box->setEnabled(anim);
1612     itemzoom->setEnabled(!anim);
1613     itemrotate->setEnabled(!anim);
1614     frame_toolbar->setEnabled(!anim);
1615     rect_properties->setEnabled(!anim);
1616     if (anim) {
1617         keep_aspect->setChecked(!m_startViewport->data(0).isNull());
1618         m_startViewport->setZValue(1100);
1619         QColor col = m_startViewport->pen().color();
1620         col.setAlpha(100);
1621         m_startViewport->setBrush(col);
1622         m_startViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1623         m_startViewport->setSelected(true);
1624         selectionChanged();
1625         slotSelectTool();
1626     } else {
1627         m_startViewport->setZValue(-1000);
1628         m_startViewport->setBrush(QBrush());
1629         m_startViewport->setFlags(0);
1630     }
1631
1632 }
1633
1634 void TitleWidget::slotAnimEnd(bool anim)
1635 {
1636     if (anim && anim_start->isChecked()) {
1637         anim_start->setChecked(false);
1638         m_startViewport->setZValue(-1000);
1639         m_startViewport->setBrush(QBrush());
1640     }
1641     slotSelectTool();
1642     QList<QGraphicsItem *> list = m_scene->items();
1643     for (int i = 0; i < list.count(); i++) {
1644         if (list.at(i)->zValue() > -1000) {
1645             list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1646             list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1647         }
1648     }
1649     align_box->setEnabled(anim);
1650     itemzoom->setEnabled(!anim);
1651     itemrotate->setEnabled(!anim);
1652     frame_toolbar->setEnabled(!anim);
1653     rect_properties->setEnabled(!anim);
1654     if (anim) {
1655         keep_aspect->setChecked(!m_endViewport->data(0).isNull());
1656         m_endViewport->setZValue(1100);
1657         QColor col = m_endViewport->pen().color();
1658         col.setAlpha(100);
1659         m_endViewport->setBrush(col);
1660         m_endViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1661         m_endViewport->setSelected(true);
1662         selectionChanged();
1663         slotSelectTool();
1664     } else {
1665         m_endViewport->setZValue(-1000);
1666         m_endViewport->setBrush(QBrush());
1667         m_endViewport->setFlags(0);
1668     }
1669 }
1670
1671 void TitleWidget::slotKeepAspect(bool keep)
1672 {
1673     if (m_endViewport->zValue() == 1100) {
1674         m_endViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1675         m_endViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1676     } else {
1677         m_startViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1678         m_startViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1679     }
1680 }
1681
1682 void TitleWidget::slotResize50()
1683 {
1684     if (m_endViewport->zValue() == 1100) {
1685         m_endViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1686     } else m_startViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1687 }
1688
1689 void TitleWidget::slotResize100()
1690 {
1691     if (m_endViewport->zValue() == 1100) {
1692         m_endViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1693     } else m_startViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1694 }
1695
1696 void TitleWidget::slotResize200()
1697 {
1698     if (m_endViewport->zValue() == 1100) {
1699         m_endViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);
1700     } else m_startViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);
1701 }
1702
1703
1704