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