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