1 /***************************************************************************
2 titlewidget.h - description
5 copyright : (C) 2008 by Marco Gittler
6 email : g.marco@freenet.de
7 ***************************************************************************/
9 /***************************************************************************
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. *
16 ***************************************************************************/
18 #include "titlewidget.h"
19 #include "kdenlivesettings.h"
22 #include <KGlobalSettings>
23 #include <KFileDialog>
24 #include <KStandardDirs>
25 #include <KMessageBox>
27 #include <QDomDocument>
28 #include <QGraphicsItem>
29 #include <QGraphicsSvgItem>
34 int settingUp = false;
36 TitleWidget::TitleWidget(KUrl url, QString projectPath, Render *render, QWidget *parent): QDialog(parent), m_render(render), m_count(0), m_projectPath(projectPath), startViewport(NULL), endViewport(NULL)
39 setFont(KGlobalSettings::toolBarFont());
40 //toolBox->setFont(KGlobalSettings::toolBarFont());
41 frame_properties->setEnabled(false);
42 rect_properties->setFixedHeight(frame_properties->height() + 4);
43 text_properties->setFixedHeight(frame_properties->height() + 4);
44 m_frameWidth = render->renderWidth();
45 m_frameHeight = render->renderHeight();
46 //connect(newTextButton, SIGNAL(clicked()), this, SLOT(slotNewText()));
47 //connect(newRectButton, SIGNAL(clicked()), this, SLOT(slotNewRect()));
48 // kcolorbutton == The color of the background
49 connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
50 // horizontalslider == The alpha of the background
51 connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
52 //connect(ktextedit, SIGNAL(textChanged()), this , SLOT(textChanged()));
53 //connect (fontBold, SIGNAL ( clicked()), this, SLOT( setBold()) ) ;
56 //ktextedit->setHidden(true);
57 connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
58 connect(font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(slotUpdateText())) ;
59 connect(font_size, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText())) ;
60 connect(textAlpha, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText()));
61 //connect (ktextedit, SIGNAL(selectionChanged()), this , SLOT (textChanged()));
63 connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
64 connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
65 connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
66 connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
67 connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
69 connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
70 connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
71 connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
72 connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
73 connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
74 connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
76 connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
77 connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
78 connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
79 connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
80 connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
82 connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
83 connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
84 connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
85 connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
86 connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
87 connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
88 connect(buttonBold, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
89 connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
90 connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
91 connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
94 connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
96 buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
97 buttonRealSize->setIcon(KIcon("zoom-original"));
98 buttonBold->setIcon(KIcon("format-text-bold"));
99 buttonItalic->setIcon(KIcon("format-text-italic"));
100 buttonUnder->setIcon(KIcon("format-text-underline"));
102 itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
103 itemhcenter->setToolTip(i18n("Align item horizontally"));
104 itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
105 itemvcenter->setToolTip(i18n("Align item vertically"));
107 QHBoxLayout *layout = new QHBoxLayout;
108 frame_toolbar->setLayout(layout);
109 layout->setContentsMargins(2, 2, 2, 2);
110 QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
112 m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), i18n("Add Rectangle"));
113 m_buttonRect->setCheckable(true);
114 connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
116 m_buttonText = m_toolbar->addAction(KIcon("insert-text"), i18n("Add Text"));
117 m_buttonText->setCheckable(true);
118 connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
120 m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), i18n("Add Image"));
121 m_buttonImage->setCheckable(false);
122 connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
124 m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), i18n("Selection Tool"));
125 m_buttonCursor->setCheckable(true);
126 connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
128 m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
129 m_buttonLoad->setCheckable(false);
130 connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
132 m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
133 m_buttonSave->setCheckable(false);
134 connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
136 layout->addWidget(m_toolbar);
137 text_properties->setHidden(true);
139 // initialize graphic scene
140 m_scene = new GraphicsSceneRectMove(this);
141 graphicsView->setScene(m_scene);
142 m_titledocument.setScene(m_scene);
144 // a gradient background
145 /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
146 gradient->setSpread(QGradient::ReflectSpread);
147 scene->setBackgroundBrush(*gradient);*/
149 m_frameImage = new QGraphicsPixmapItem();
151 qtrans.scale(2.0, 2.0);
152 m_frameImage->setTransform(qtrans);
153 m_frameImage->setZValue(-1200);
154 m_frameImage->setFlags(QGraphicsItem::ItemClipsToShape);
155 displayBackgroundFrame();
156 graphicsView->scene()->addItem(m_frameImage);
158 connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
159 connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
160 connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
161 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
162 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
163 connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
164 connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
165 connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
167 QPen framepen(Qt::DotLine);
168 framepen.setColor(Qt::red);
170 m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
171 m_frameBorder->setPen(framepen);
172 m_frameBorder->setZValue(-1100);
173 m_frameBorder->setBrush(Qt::transparent);
174 m_frameBorder->setFlags(QGraphicsItem::ItemClipsToShape);
175 graphicsView->scene()->addItem(m_frameBorder);
177 // mbd: load saved settings
181 QTimer::singleShot(500, this, SLOT(slotAdjustZoom()));
182 graphicsView->show();
183 //graphicsView->setRenderHint(QPainter::Antialiasing);
184 graphicsView->setInteractive(true);
185 //graphicsView->resize(400, 300);
186 kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
187 toolBox->setItemEnabled(2, false);
188 if (!url.isEmpty()) {
189 m_count = m_titledocument.loadDocument(url, startViewport, endViewport) + 1;
196 TitleWidget::~TitleWidget()
200 delete m_buttonImage;
201 delete m_buttonCursor;
205 delete m_frameBorder;
207 if (startViewport) delete startViewport;
208 if (endViewport) delete endViewport;
213 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl)
216 QString titlePath = projectUrl.path() + "/titles/";
217 KStandardDirs::makeDir(titlePath);
218 QString titleName = "title";
220 QString path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
221 while (QFile::exists(path + ".png")) {
223 path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
225 result.append(titleName + QString::number(counter).rightJustified(3, '0', false));
226 result.append(path + ".png");
230 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
233 QString titlePath = projectUrl.path() + "/titles/";
234 KStandardDirs::makeDir(titlePath);
235 return titlePath + titleName + ".png";
239 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
244 void TitleWidget::slotTextTool()
246 rect_properties->setHidden(true);
247 text_properties->setHidden(false);
248 m_scene->setTool(TITLE_TEXT);
249 m_buttonRect->setChecked(false);
250 m_buttonCursor->setChecked(false);
253 void TitleWidget::slotRectTool()
255 text_properties->setHidden(true);
256 rect_properties->setHidden(false);
257 m_scene->setTool(TITLE_RECTANGLE);
258 m_buttonText->setChecked(false);
259 m_buttonCursor->setChecked(false);
260 m_buttonRect->setChecked(true);
263 void TitleWidget::slotSelectTool()
265 m_scene->setTool(TITLE_SELECT);
266 m_buttonCursor->setChecked(true);
267 m_buttonText->setChecked(false);
268 m_buttonRect->setChecked(false);
271 void TitleWidget::slotImageTool()
273 KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.svg *.png *.jpg *.jpeg *.gif *.raw", this, i18n("Load Image"));
274 if (!url.isEmpty()) {
275 if (url.path().endsWith(".svg")) {
276 QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
277 svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
278 svg->setZValue(m_count++);
279 svg->setData(Qt::UserRole, url.path());
280 graphicsView->scene()->addItem(svg);
282 QPixmap pix(url.path());
283 QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
284 image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
285 image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
286 image->setData(Qt::UserRole, url.path());
287 image->setZValue(m_count++);
288 graphicsView->scene()->addItem(image);
291 m_scene->setTool(TITLE_SELECT);
292 m_buttonRect->setChecked(false);
293 m_buttonCursor->setChecked(true);
294 m_buttonText->setChecked(false);
297 void TitleWidget::displayBackgroundFrame()
299 if (!displayBg->isChecked()) {
300 QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
301 QPixmap pattern(20, 20);
305 p.fillRect(QRect(0, 0, 10, 10), QColor(210, 210, 210));
306 p.fillRect(QRect(10, 10, 20, 20), QColor(210, 210, 210));
311 p.fillRect(bg.rect(), br);
313 m_frameImage->setPixmap(bg);
315 m_frameImage->setPixmap(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2));
319 void TitleWidget::initViewports()
321 startViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
322 endViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
324 QPen startpen(Qt::DotLine);
325 QPen endpen(Qt::DashDotLine);
326 startpen.setColor(QColor(100, 200, 100, 140));
327 endpen.setColor(QColor(200, 100, 100, 140));
329 startViewport->setPen(startpen);
330 endViewport->setPen(endpen);
332 startViewportSize->setValue(40);
333 endViewportSize->setValue(40);
335 startViewport->setZValue(-1000);
336 endViewport->setZValue(-1000);
338 startViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
339 endViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
341 graphicsView->scene()->addItem(startViewport);
342 graphicsView->scene()->addItem(endViewport);
345 void TitleWidget::slotUpdateZoom(int pos)
347 m_scene->setZoom((double) pos / 100);
348 zoom_label->setText(QString::number(pos) + '%');
351 void TitleWidget::slotZoom(bool up)
353 int pos = zoom_slider->value();
356 zoom_slider->setValue(pos);
359 void TitleWidget::slotAdjustZoom()
361 /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
362 double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
363 if (scalex > scaley) scalex = scaley;
364 int zoompos = (int)(scalex * 7 + 0.5);*/
365 graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
366 int zoompos = graphicsView->matrix().m11() * 100;
367 zoom_slider->setValue(zoompos);
368 graphicsView->centerOn(m_frameBorder);
371 void TitleWidget::slotZoomOneToOne()
373 zoom_slider->setValue(100);
374 graphicsView->centerOn(m_frameBorder);
377 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
379 QColor f = rectFColor->color();
380 f.setAlpha(rectFAlpha->value());
382 penf.setWidth(rectLineWidth->value());
384 QColor b = rectBColor->color();
385 b.setAlpha(rectBAlpha->value());
386 rect->setBrush(QBrush(b));
387 rect->setZValue(m_count++);
388 //setCurrentItem(rect);
389 //graphicsView->setFocus();
392 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
394 QFont font = font_family->currentFont();
395 font.setPixelSize(font_size->value());
397 font.setBold(buttonBold->isChecked());
398 font.setItalic(buttonItalic->isChecked());
399 font.setUnderline(buttonUnder->isChecked());
402 QColor color = fontColorButton->color();
403 color.setAlpha(textAlpha->value());
404 tt->setDefaultTextColor(color);
405 tt->setZValue(m_count++);
409 void TitleWidget::setCurrentItem(QGraphicsItem *item)
411 m_scene->setSelectedItem(item);
414 void TitleWidget::zIndexChanged(int v)
416 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
422 void TitleWidget::selectionChanged()
424 if (m_scene->tool() != TITLE_SELECT) return;
425 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
426 //toolBox->setItemEnabled(2, false);
427 //toolBox->setItemEnabled(3, false);
428 value_x->blockSignals(true);
429 value_y->blockSignals(true);
430 value_w->blockSignals(true);
431 value_h->blockSignals(true);
432 //kDebug() << "//////// SELECTION CHANGED; ITEMS: " << l.size();
434 if ((l[0])->type() == 8) {
435 rect_properties->setHidden(true);
436 text_properties->setHidden(false);
437 QGraphicsTextItem* i = ((QGraphicsTextItem*)l[0]);
438 //if (l[0]->hasFocus())
439 //ktextedit->setHtml(i->toHtml());
440 toolBox->setCurrentIndex(0);
441 //toolBox->setItemEnabled(2, true);
442 font_size->blockSignals(true);
443 font_family->blockSignals(true);
444 buttonBold->blockSignals(true);
445 buttonItalic->blockSignals(true);
446 buttonUnder->blockSignals(true);
447 fontColorButton->blockSignals(true);
448 textAlpha->blockSignals(true);
450 QFont font = i->font();
451 font_family->setCurrentFont(font);
452 font_size->setValue(font.pixelSize());
453 buttonBold->setChecked(font.bold());
454 buttonItalic->setChecked(font.italic());
455 buttonUnder->setChecked(font.underline());
457 QColor color = i->defaultTextColor();
458 fontColorButton->setColor(color);
459 textAlpha->setValue(color.alpha());
461 font_size->blockSignals(false);
462 font_family->blockSignals(false);
463 buttonBold->blockSignals(false);
464 buttonItalic->blockSignals(false);
465 buttonUnder->blockSignals(false);
466 fontColorButton->blockSignals(false);
467 textAlpha->blockSignals(false);
469 value_x->setValue((int) i->pos().x());
470 value_y->setValue((int) i->pos().y());
471 value_w->setValue((int) i->boundingRect().width());
472 value_h->setValue((int) i->boundingRect().height());
473 frame_properties->setEnabled(true);
474 value_w->setEnabled(false);
475 value_h->setEnabled(false);
476 } else if ((l[0])->type() == 3) {
477 rect_properties->setHidden(false);
478 text_properties->setHidden(true);
480 QGraphicsRectItem *rec = ((QGraphicsRectItem*)l[0]);
481 toolBox->setCurrentIndex(0);
482 //toolBox->setItemEnabled(3, true);
483 rectFAlpha->setValue(rec->pen().color().alpha());
484 rectBAlpha->setValue(rec->brush().color().alpha());
485 //kDebug() << rec->brush().color().alpha();
486 QColor fcol = rec->pen().color();
487 QColor bcol = rec->brush().color();
488 //fcol.setAlpha(255);
489 //bcol.setAlpha(255);
490 rectFColor->setColor(fcol);
491 rectBColor->setColor(bcol);
493 rectLineWidth->setValue(rec->pen().width());
494 value_x->setValue((int) rec->pos().x());
495 value_y->setValue((int) rec->pos().y());
496 value_w->setValue((int) rec->rect().width());
497 value_h->setValue((int) rec->rect().height());
498 frame_properties->setEnabled(true);
499 value_w->setEnabled(true);
500 value_h->setEnabled(true);
502 //toolBox->setCurrentIndex(0);
503 frame_properties->setEnabled(false);
505 zValue->setValue((int)l[0]->zValue());
506 itemzoom->setValue((int)(transformations[l[0]].scalex * 100));
507 itemrotate->setValue((int)(transformations[l[0]].rotate));
508 value_x->blockSignals(false);
509 value_y->blockSignals(false);
510 value_w->blockSignals(false);
511 value_h->blockSignals(false);
512 } else frame_properties->setEnabled(false);
515 void TitleWidget::slotAdjustSelectedItem()
517 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
519 if (l[0]->type() == 3) {
521 QGraphicsRectItem *rec = ((QGraphicsRectItem*)l[0]);
522 rec->setPos(value_x->value(), value_y->value());
523 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
524 } else if (l[0]->type() == 8) {
526 QGraphicsTextItem *rec = ((QGraphicsTextItem*)l[0]);
527 rec->setPos(value_x->value(), value_y->value());
532 void TitleWidget::slotChangeBackground()
534 QColor color = kcolorbutton->color();
535 color.setAlpha(horizontalSlider->value());
536 m_frameBorder->setBrush(QBrush(color));
539 void TitleWidget::textChanged()
541 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
542 if (l.size() == 1 && (l[0])->type() == 8 && !l[0]->hasFocus()) {
543 //kDebug() << ktextedit->document()->toHtml();
544 //((QGraphicsTextItem*)l[0])->setHtml(ktextedit->toHtml());
548 void TitleWidget::slotUpdateText()
550 QFont font = font_family->currentFont();
551 font.setPixelSize(font_size->value());
552 font.setBold(buttonBold->isChecked());
553 font.setItalic(buttonItalic->isChecked());
554 font.setUnderline(buttonUnder->isChecked());
555 QColor color = fontColorButton->color();
556 color.setAlpha(textAlpha->value());
558 QGraphicsTextItem* item = NULL;
559 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
560 if (l.size() == 1 && (l[0])->type() == 8) {
561 item = (QGraphicsTextItem*)l[0];
564 //if (item->textCursor().selection ().isEmpty())
567 item->setDefaultTextColor(color);
570 QTextDocumentFragment selec = item->textCursor().selection ();
573 //if (ktextedit->textCursor().selectedText().isEmpty()) ktextedit->selectAll();
575 //ktextedit->setCurrentFont(font);
576 //ktextedit->setTextColor(color);
577 /*QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
578 if (l.size() == 1 && (l[0])->type() == 8 && l[0]->hasFocus()) {
579 QGraphicsTextItem* item = static_cast <QGraphicsTextItem*> (l[0]);
584 void TitleWidget::rectChanged()
586 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
587 if (l.size() == 1 && (l[0])->type() == 3 && !settingUp) {
588 QGraphicsRectItem *rec = (QGraphicsRectItem*)l[0];
589 QColor f = rectFColor->color();
590 f.setAlpha(rectFAlpha->value());
592 penf.setWidth(rectLineWidth->value());
594 QColor b = rectBColor->color();
595 b.setAlpha(rectBAlpha->value());
596 rec->setBrush(QBrush(b));
600 void TitleWidget::fontBold()
602 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
603 if (l.size() == 1 && (l[0])->type() == 8 && !l[0]->hasFocus()) {
604 //ktextedit->document()->setTextOption();
608 void TitleWidget::itemScaled(int val)
610 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
612 Transform x = transformations[l[0]];
613 x.scalex = (double)val / 100.0;
614 x.scaley = (double)val / 100.0;
616 qtrans.scale(x.scalex, x.scaley);
617 qtrans.rotate(x.rotate);
618 l[0]->setTransform(qtrans);
619 transformations[l[0]] = x;
623 void TitleWidget::itemRotate(int val)
625 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
627 Transform x = transformations[l[0]];
628 x.rotate = (double)val;
630 qtrans.scale(x.scalex, x.scaley);
631 qtrans.rotate(x.rotate);
632 l[0]->setTransform(qtrans);
633 transformations[l[0]] = x;
637 void TitleWidget::itemHCenter()
639 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
641 QGraphicsItem *item = l[0];
643 if (item->type() == 3) {
644 br = ((QGraphicsRectItem*)item)->rect();
645 } else br = item->boundingRect();
646 int width = (int) br.width();
647 int newPos = (int)((m_frameWidth - width) / 2);
648 item->setPos(newPos, item->pos().y());
652 void TitleWidget::itemVCenter()
654 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
656 QGraphicsItem *item = l[0];
658 if (item->type() == 3) {
659 br = ((QGraphicsRectItem*)item)->rect();
660 } else br = item->boundingRect();
661 int height = (int) br.height();
662 int newPos = (int)((m_frameHeight - height) / 2);
663 item->setPos(item->pos().x(), newPos);
667 void TitleWidget::setupViewports()
669 double aspect_ratio = 4.0 / 3.0;//read from project
671 QRectF sp(0, 0, 0, 0);
672 QRectF ep(0, 0, 0, 0);
674 double sv_size = startViewportSize->value();
675 double ev_size = endViewportSize->value();
676 sp.adjust(-sv_size, -sv_size / aspect_ratio, sv_size, sv_size / aspect_ratio);
677 ep.adjust(-ev_size, -ev_size / aspect_ratio, ev_size, ev_size / aspect_ratio);
679 startViewport->setPos(startViewportX->value(), startViewportY->value());
680 endViewport->setPos(endViewportX->value(), endViewportY->value());
682 startViewport->setPolygon(QPolygonF(sp));
683 endViewport->setPolygon(QPolygonF(ep));
687 void TitleWidget::loadTitle()
689 KUrl url = KFileDialog::getOpenUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Load Title"));
690 if (!url.isEmpty()) {
691 QList<QGraphicsItem *> items = m_scene->items();
692 for (int i = 0; i < items.size(); i++) {
693 if (items.at(i)->zValue() > -1000) delete items.at(i);
695 m_count = m_titledocument.loadDocument(url, startViewport, endViewport) + 1;
700 void TitleWidget::saveTitle(KUrl url)
702 if (url.isEmpty()) url = KFileDialog::getSaveUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Save Title"));
703 if (!url.isEmpty()) {
704 if (m_titledocument.saveDocument(url, startViewport, endViewport) == false)
705 KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
709 QDomDocument TitleWidget::xml()
711 return m_titledocument.xml(startViewport, endViewport);
714 void TitleWidget::setXml(QDomDocument doc)
716 m_count = m_titledocument.loadFromXml(doc, startViewport, endViewport);
717 // mbd: Update the GUI color selectors to match the stuff from the loaded document
718 QColor background_color = m_titledocument.getBackgroundColor();
719 horizontalSlider->blockSignals(true);
720 kcolorbutton->blockSignals(true);
721 horizontalSlider->setValue(background_color.alpha());
722 background_color.setAlpha(255);
723 kcolorbutton->setColor(background_color);
724 horizontalSlider->blockSignals(false);
725 kcolorbutton->blockSignals(false);
730 QImage TitleWidget::renderedPixmap()
732 QImage pix(m_frameWidth, m_frameHeight, QImage::Format_ARGB32);
733 pix.fill(Qt::transparent);
734 QPainter painter(&pix);
735 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
736 m_scene->clearTextSelection();
737 QPen framepen = m_frameBorder->pen();
738 m_frameBorder->setPen(Qt::NoPen);
739 startViewport->setVisible(false);
740 endViewport->setVisible(false);
741 m_frameImage->setVisible(false);
743 m_scene->render(&painter, QRectF(), QRectF(0, 0, m_frameWidth, m_frameHeight));
745 m_frameBorder->setPen(framepen);
746 startViewport->setVisible(true);
747 endViewport->setVisible(true);
748 m_frameImage->setVisible(true);
752 /** \brief Connected to the accepted signal - calls writeChoices */
753 void TitleWidget::slotAccepted()
758 /** \brief Store the current choices of font, background and rect values */
759 void TitleWidget::writeChoices()
761 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
762 KSharedConfigPtr config = KGlobal::config();
763 KConfigGroup titleConfig(config, "TitleWidget");
765 titleConfig.writeEntry("font_family", font_family->currentFont());
766 //titleConfig.writeEntry("font_size", font_size->value());
767 titleConfig.writeEntry("font_pixel_size", font_size->value());
768 titleConfig.writeEntry("font_color", fontColorButton->color());
769 titleConfig.writeEntry("font_alpha", textAlpha->value());
770 titleConfig.writeEntry("font_bold", buttonBold->isChecked());
771 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
772 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
774 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
775 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
776 titleConfig.writeEntry("rect_background_color", rectBColor->color());
777 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
778 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
780 titleConfig.writeEntry("background_color", kcolorbutton->color());
781 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
782 //! \todo Not sure if I should sync - it is probably safe to do it
787 /** \brief Read the last stored choices into the dialog */
788 void TitleWidget::readChoices()
790 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
791 KSharedConfigPtr config = KGlobal::config();
792 KConfigGroup titleConfig(config, "TitleWidget");
794 font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
795 font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
796 fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
797 textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
798 buttonBold->setChecked(titleConfig.readEntry("font_bold", buttonBold->isChecked()));
799 buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
800 buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
802 rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
803 rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
804 rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
805 rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
806 rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
808 kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
809 horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));