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) :
40 m_projectPath(projectPath),
45 setFont(KGlobalSettings::toolBarFont());
46 //toolBox->setFont(KGlobalSettings::toolBarFont());
47 frame_properties->setEnabled(false);
48 rect_properties->setFixedHeight(frame_properties->height() + 4);
49 text_properties->setFixedHeight(frame_properties->height() + 4);
50 m_frameWidth = render->renderWidth();
51 m_frameHeight = render->renderHeight();
52 //connect(newTextButton, SIGNAL(clicked()), this, SLOT(slotNewText()));
53 //connect(newRectButton, SIGNAL(clicked()), this, SLOT(slotNewRect()));
54 // kcolorbutton == The color of the background
55 connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
56 // horizontalslider == The alpha of the background
57 connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
58 //connect(ktextedit, SIGNAL(textChanged()), this , SLOT(textChanged()));
59 //connect (fontBold, SIGNAL ( clicked()), this, SLOT( setBold()) ) ;
62 //ktextedit->setHidden(true);
63 connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
64 connect(font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(slotUpdateText())) ;
65 connect(font_size, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText())) ;
66 connect(textAlpha, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText()));
67 //connect (ktextedit, SIGNAL(selectionChanged()), this , SLOT (textChanged()));
69 connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
70 connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
71 connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
72 connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
73 connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
75 connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
76 connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
77 connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
78 connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
79 connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
80 connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
82 connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
83 connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
84 connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
85 connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
86 connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
88 connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
89 connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
90 connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
91 connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
92 connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
93 connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
94 connect(buttonBold, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
95 connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
96 connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
97 connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
100 connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
102 buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
103 buttonRealSize->setIcon(KIcon("zoom-original"));
104 buttonBold->setIcon(KIcon("format-text-bold"));
105 buttonItalic->setIcon(KIcon("format-text-italic"));
106 buttonUnder->setIcon(KIcon("format-text-underline"));
108 itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
109 itemhcenter->setToolTip(i18n("Align item horizontally"));
110 itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
111 itemvcenter->setToolTip(i18n("Align item vertically"));
113 QHBoxLayout *layout = new QHBoxLayout;
114 frame_toolbar->setLayout(layout);
115 layout->setContentsMargins(2, 2, 2, 2);
116 QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
118 m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), i18n("Add Rectangle"));
119 m_buttonRect->setCheckable(true);
120 connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
122 m_buttonText = m_toolbar->addAction(KIcon("insert-text"), i18n("Add Text"));
123 m_buttonText->setCheckable(true);
124 connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
126 m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), i18n("Add Image"));
127 m_buttonImage->setCheckable(false);
128 connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
130 m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), i18n("Selection Tool"));
131 m_buttonCursor->setCheckable(true);
132 connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
134 m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
135 m_buttonLoad->setCheckable(false);
136 connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
138 m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
139 m_buttonSave->setCheckable(false);
140 connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
142 layout->addWidget(m_toolbar);
143 text_properties->setHidden(true);
145 // initialize graphic scene
146 m_scene = new GraphicsSceneRectMove(this);
147 graphicsView->setScene(m_scene);
148 m_titledocument.setScene(m_scene);
150 // a gradient background
151 /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
152 gradient->setSpread(QGradient::ReflectSpread);
153 scene->setBackgroundBrush(*gradient);*/
155 m_frameImage = new QGraphicsPixmapItem();
157 qtrans.scale(2.0, 2.0);
158 m_frameImage->setTransform(qtrans);
159 m_frameImage->setZValue(-1200);
160 m_frameImage->setFlags(QGraphicsItem::ItemClipsToShape);
161 displayBackgroundFrame();
162 graphicsView->scene()->addItem(m_frameImage);
164 connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
165 connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
166 connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
167 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
168 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
169 connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
170 connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
171 connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
173 QPen framepen(Qt::DotLine);
174 framepen.setColor(Qt::red);
176 m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
177 m_frameBorder->setPen(framepen);
178 m_frameBorder->setZValue(-1100);
179 m_frameBorder->setBrush(Qt::transparent);
180 m_frameBorder->setFlags(QGraphicsItem::ItemClipsToShape);
181 graphicsView->scene()->addItem(m_frameBorder);
183 // mbd: load saved settings
187 QTimer::singleShot(500, this, SLOT(slotAdjustZoom()));
188 graphicsView->show();
189 //graphicsView->setRenderHint(QPainter::Antialiasing);
190 graphicsView->setInteractive(true);
191 //graphicsView->resize(400, 300);
192 kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
193 toolBox->setItemEnabled(2, false);
194 if (!url.isEmpty()) {
195 m_count = m_titledocument.loadDocument(url, startViewport, endViewport) + 1;
202 TitleWidget::~TitleWidget()
206 delete m_buttonImage;
207 delete m_buttonCursor;
211 delete m_frameBorder;
213 if (startViewport) delete startViewport;
214 if (endViewport) delete endViewport;
219 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl)
222 QString titlePath = projectUrl.path() + "/titles/";
223 KStandardDirs::makeDir(titlePath);
224 QString titleName = "title";
226 QString path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
227 while (QFile::exists(path + ".png")) {
229 path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
231 result.append(titleName + QString::number(counter).rightJustified(3, '0', false));
232 result.append(path + ".png");
236 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
239 QString titlePath = projectUrl.path() + "/titles/";
240 KStandardDirs::makeDir(titlePath);
241 return titlePath + titleName + ".png";
245 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
250 void TitleWidget::slotTextTool()
252 rect_properties->setHidden(true);
253 text_properties->setHidden(false);
254 m_scene->setTool(TITLE_TEXT);
255 m_buttonRect->setChecked(false);
256 m_buttonCursor->setChecked(false);
259 void TitleWidget::slotRectTool()
261 text_properties->setHidden(true);
262 rect_properties->setHidden(false);
263 m_scene->setTool(TITLE_RECTANGLE);
264 m_buttonText->setChecked(false);
265 m_buttonCursor->setChecked(false);
266 m_buttonRect->setChecked(true);
269 void TitleWidget::slotSelectTool()
271 m_scene->setTool(TITLE_SELECT);
272 m_buttonCursor->setChecked(true);
273 m_buttonText->setChecked(false);
274 m_buttonRect->setChecked(false);
277 void TitleWidget::slotImageTool()
279 KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.svg *.png *.jpg *.jpeg *.gif *.raw", this, i18n("Load Image"));
280 if (!url.isEmpty()) {
281 if (url.path().endsWith(".svg")) {
282 QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
283 svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
284 svg->setZValue(m_count++);
285 svg->setData(Qt::UserRole, url.path());
286 graphicsView->scene()->addItem(svg);
288 QPixmap pix(url.path());
289 QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
290 image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
291 image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
292 image->setData(Qt::UserRole, url.path());
293 image->setZValue(m_count++);
294 graphicsView->scene()->addItem(image);
297 m_scene->setTool(TITLE_SELECT);
298 m_buttonRect->setChecked(false);
299 m_buttonCursor->setChecked(true);
300 m_buttonText->setChecked(false);
303 void TitleWidget::displayBackgroundFrame()
305 if (!displayBg->isChecked()) {
306 QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
307 QPixmap pattern(20, 20);
311 p.fillRect(QRect(0, 0, 10, 10), QColor(210, 210, 210));
312 p.fillRect(QRect(10, 10, 20, 20), QColor(210, 210, 210));
317 p.fillRect(bg.rect(), br);
319 m_frameImage->setPixmap(bg);
321 m_frameImage->setPixmap(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2));
325 void TitleWidget::initViewports()
327 startViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
328 endViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
330 QPen startpen(Qt::DotLine);
331 QPen endpen(Qt::DashDotLine);
332 startpen.setColor(QColor(100, 200, 100, 140));
333 endpen.setColor(QColor(200, 100, 100, 140));
335 startViewport->setPen(startpen);
336 endViewport->setPen(endpen);
338 startViewportSize->setValue(40);
339 endViewportSize->setValue(40);
341 startViewport->setZValue(-1000);
342 endViewport->setZValue(-1000);
344 startViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
345 endViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
347 graphicsView->scene()->addItem(startViewport);
348 graphicsView->scene()->addItem(endViewport);
351 void TitleWidget::slotUpdateZoom(int pos)
353 m_scene->setZoom((double) pos / 100);
354 zoom_label->setText(QString::number(pos) + '%');
357 void TitleWidget::slotZoom(bool up)
359 int pos = zoom_slider->value();
362 zoom_slider->setValue(pos);
365 void TitleWidget::slotAdjustZoom()
367 /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
368 double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
369 if (scalex > scaley) scalex = scaley;
370 int zoompos = (int)(scalex * 7 + 0.5);*/
371 graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
372 int zoompos = graphicsView->matrix().m11() * 100;
373 zoom_slider->setValue(zoompos);
374 graphicsView->centerOn(m_frameBorder);
377 void TitleWidget::slotZoomOneToOne()
379 zoom_slider->setValue(100);
380 graphicsView->centerOn(m_frameBorder);
383 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
385 QColor f = rectFColor->color();
386 f.setAlpha(rectFAlpha->value());
388 penf.setWidth(rectLineWidth->value());
390 QColor b = rectBColor->color();
391 b.setAlpha(rectBAlpha->value());
392 rect->setBrush(QBrush(b));
393 rect->setZValue(m_count++);
394 //setCurrentItem(rect);
395 //graphicsView->setFocus();
398 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
400 QFont font = font_family->currentFont();
401 font.setPixelSize(font_size->value());
403 font.setBold(buttonBold->isChecked());
404 font.setItalic(buttonItalic->isChecked());
405 font.setUnderline(buttonUnder->isChecked());
408 QColor color = fontColorButton->color();
409 color.setAlpha(textAlpha->value());
410 tt->setDefaultTextColor(color);
411 tt->setZValue(m_count++);
415 void TitleWidget::setCurrentItem(QGraphicsItem *item)
417 m_scene->setSelectedItem(item);
420 void TitleWidget::zIndexChanged(int v)
422 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
428 void TitleWidget::selectionChanged()
430 if (m_scene->tool() != TITLE_SELECT) return;
431 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
432 //toolBox->setItemEnabled(2, false);
433 //toolBox->setItemEnabled(3, false);
434 value_x->blockSignals(true);
435 value_y->blockSignals(true);
436 value_w->blockSignals(true);
437 value_h->blockSignals(true);
438 //kDebug() << "//////// SELECTION CHANGED; ITEMS: " << l.size();
440 if ((l[0])->type() == 8) {
441 rect_properties->setHidden(true);
442 text_properties->setHidden(false);
443 QGraphicsTextItem* i = ((QGraphicsTextItem*)l[0]);
444 //if (l[0]->hasFocus())
445 //ktextedit->setHtml(i->toHtml());
446 toolBox->setCurrentIndex(0);
447 //toolBox->setItemEnabled(2, true);
448 font_size->blockSignals(true);
449 font_family->blockSignals(true);
450 buttonBold->blockSignals(true);
451 buttonItalic->blockSignals(true);
452 buttonUnder->blockSignals(true);
453 fontColorButton->blockSignals(true);
454 textAlpha->blockSignals(true);
456 QFont font = i->font();
457 font_family->setCurrentFont(font);
458 font_size->setValue(font.pixelSize());
459 buttonBold->setChecked(font.bold());
460 buttonItalic->setChecked(font.italic());
461 buttonUnder->setChecked(font.underline());
463 QColor color = i->defaultTextColor();
464 fontColorButton->setColor(color);
465 textAlpha->setValue(color.alpha());
467 font_size->blockSignals(false);
468 font_family->blockSignals(false);
469 buttonBold->blockSignals(false);
470 buttonItalic->blockSignals(false);
471 buttonUnder->blockSignals(false);
472 fontColorButton->blockSignals(false);
473 textAlpha->blockSignals(false);
475 value_x->setValue((int) i->pos().x());
476 value_y->setValue((int) i->pos().y());
477 value_w->setValue((int) i->boundingRect().width());
478 value_h->setValue((int) i->boundingRect().height());
479 frame_properties->setEnabled(true);
480 value_w->setEnabled(false);
481 value_h->setEnabled(false);
482 } else if ((l[0])->type() == 3) {
483 rect_properties->setHidden(false);
484 text_properties->setHidden(true);
486 QGraphicsRectItem *rec = ((QGraphicsRectItem*)l[0]);
487 toolBox->setCurrentIndex(0);
488 //toolBox->setItemEnabled(3, true);
489 rectFAlpha->setValue(rec->pen().color().alpha());
490 rectBAlpha->setValue(rec->brush().color().alpha());
491 //kDebug() << rec->brush().color().alpha();
492 QColor fcol = rec->pen().color();
493 QColor bcol = rec->brush().color();
494 //fcol.setAlpha(255);
495 //bcol.setAlpha(255);
496 rectFColor->setColor(fcol);
497 rectBColor->setColor(bcol);
499 rectLineWidth->setValue(rec->pen().width());
500 value_x->setValue((int) rec->pos().x());
501 value_y->setValue((int) rec->pos().y());
502 value_w->setValue((int) rec->rect().width());
503 value_h->setValue((int) rec->rect().height());
504 frame_properties->setEnabled(true);
505 value_w->setEnabled(true);
506 value_h->setEnabled(true);
508 //toolBox->setCurrentIndex(0);
509 frame_properties->setEnabled(false);
511 zValue->setValue((int)l[0]->zValue());
512 itemzoom->setValue((int)(transformations[l[0]].scalex * 100));
513 itemrotate->setValue((int)(transformations[l[0]].rotate));
514 value_x->blockSignals(false);
515 value_y->blockSignals(false);
516 value_w->blockSignals(false);
517 value_h->blockSignals(false);
518 } else frame_properties->setEnabled(false);
521 void TitleWidget::slotAdjustSelectedItem()
523 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
525 if (l[0]->type() == 3) {
527 QGraphicsRectItem *rec = ((QGraphicsRectItem*)l[0]);
528 rec->setPos(value_x->value(), value_y->value());
529 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
530 } else if (l[0]->type() == 8) {
532 QGraphicsTextItem *rec = ((QGraphicsTextItem*)l[0]);
533 rec->setPos(value_x->value(), value_y->value());
538 void TitleWidget::slotChangeBackground()
540 QColor color = kcolorbutton->color();
541 color.setAlpha(horizontalSlider->value());
542 m_frameBorder->setBrush(QBrush(color));
545 void TitleWidget::textChanged()
547 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
548 if (l.size() == 1 && (l[0])->type() == 8 && !l[0]->hasFocus()) {
549 //kDebug() << ktextedit->document()->toHtml();
550 //((QGraphicsTextItem*)l[0])->setHtml(ktextedit->toHtml());
554 void TitleWidget::slotUpdateText()
556 QFont font = font_family->currentFont();
557 font.setPixelSize(font_size->value());
558 font.setBold(buttonBold->isChecked());
559 font.setItalic(buttonItalic->isChecked());
560 font.setUnderline(buttonUnder->isChecked());
561 QColor color = fontColorButton->color();
562 color.setAlpha(textAlpha->value());
564 QGraphicsTextItem* item = NULL;
565 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
566 if (l.size() == 1 && (l[0])->type() == 8) {
567 item = (QGraphicsTextItem*)l[0];
570 //if (item->textCursor().selection ().isEmpty())
573 item->setDefaultTextColor(color);
576 QTextDocumentFragment selec = item->textCursor().selection ();
579 //if (ktextedit->textCursor().selectedText().isEmpty()) ktextedit->selectAll();
581 //ktextedit->setCurrentFont(font);
582 //ktextedit->setTextColor(color);
583 /*QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
584 if (l.size() == 1 && (l[0])->type() == 8 && l[0]->hasFocus()) {
585 QGraphicsTextItem* item = static_cast <QGraphicsTextItem*> (l[0]);
590 void TitleWidget::rectChanged()
592 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
593 if (l.size() == 1 && (l[0])->type() == 3 && !settingUp) {
594 QGraphicsRectItem *rec = (QGraphicsRectItem*)l[0];
595 QColor f = rectFColor->color();
596 f.setAlpha(rectFAlpha->value());
598 penf.setWidth(rectLineWidth->value());
600 QColor b = rectBColor->color();
601 b.setAlpha(rectBAlpha->value());
602 rec->setBrush(QBrush(b));
606 void TitleWidget::fontBold()
608 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
609 if (l.size() == 1 && (l[0])->type() == 8 && !l[0]->hasFocus()) {
610 //ktextedit->document()->setTextOption();
614 void TitleWidget::itemScaled(int val)
616 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
618 Transform x = transformations[l[0]];
619 x.scalex = (double)val / 100.0;
620 x.scaley = (double)val / 100.0;
622 qtrans.scale(x.scalex, x.scaley);
623 qtrans.rotate(x.rotate);
624 l[0]->setTransform(qtrans);
625 transformations[l[0]] = x;
629 void TitleWidget::itemRotate(int val)
631 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
633 Transform x = transformations[l[0]];
634 x.rotate = (double)val;
636 qtrans.scale(x.scalex, x.scaley);
637 qtrans.rotate(x.rotate);
638 l[0]->setTransform(qtrans);
639 transformations[l[0]] = x;
643 void TitleWidget::itemHCenter()
645 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
647 QGraphicsItem *item = l[0];
649 if (item->type() == 3) {
650 br = ((QGraphicsRectItem*)item)->rect();
651 } else br = item->boundingRect();
652 int width = (int) br.width();
653 int newPos = (int)((m_frameWidth - width) / 2);
654 item->setPos(newPos, item->pos().y());
658 void TitleWidget::itemVCenter()
660 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
662 QGraphicsItem *item = l[0];
664 if (item->type() == 3) {
665 br = ((QGraphicsRectItem*)item)->rect();
666 } else br = item->boundingRect();
667 int height = (int) br.height();
668 int newPos = (int)((m_frameHeight - height) / 2);
669 item->setPos(item->pos().x(), newPos);
673 void TitleWidget::setupViewports()
675 double aspect_ratio = 4.0 / 3.0;//read from project
677 QRectF sp(0, 0, 0, 0);
678 QRectF ep(0, 0, 0, 0);
680 double sv_size = startViewportSize->value();
681 double ev_size = endViewportSize->value();
682 sp.adjust(-sv_size, -sv_size / aspect_ratio, sv_size, sv_size / aspect_ratio);
683 ep.adjust(-ev_size, -ev_size / aspect_ratio, ev_size, ev_size / aspect_ratio);
685 startViewport->setPos(startViewportX->value(), startViewportY->value());
686 endViewport->setPos(endViewportX->value(), endViewportY->value());
688 startViewport->setPolygon(QPolygonF(sp));
689 endViewport->setPolygon(QPolygonF(ep));
693 void TitleWidget::loadTitle()
695 KUrl url = KFileDialog::getOpenUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Load Title"));
696 if (!url.isEmpty()) {
697 QList<QGraphicsItem *> items = m_scene->items();
698 for (int i = 0; i < items.size(); i++) {
699 if (items.at(i)->zValue() > -1000) delete items.at(i);
701 m_count = m_titledocument.loadDocument(url, startViewport, endViewport) + 1;
706 void TitleWidget::saveTitle(KUrl url)
708 if (url.isEmpty()) url = KFileDialog::getSaveUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Save Title"));
709 if (!url.isEmpty()) {
710 if (m_titledocument.saveDocument(url, startViewport, endViewport) == false)
711 KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
715 QDomDocument TitleWidget::xml()
717 return m_titledocument.xml(startViewport, endViewport);
720 void TitleWidget::setXml(QDomDocument doc)
722 m_count = m_titledocument.loadFromXml(doc, startViewport, endViewport);
723 // mbd: Update the GUI color selectors to match the stuff from the loaded document
724 QColor background_color = m_titledocument.getBackgroundColor();
725 horizontalSlider->blockSignals(true);
726 kcolorbutton->blockSignals(true);
727 horizontalSlider->setValue(background_color.alpha());
728 background_color.setAlpha(255);
729 kcolorbutton->setColor(background_color);
730 horizontalSlider->blockSignals(false);
731 kcolorbutton->blockSignals(false);
736 QImage TitleWidget::renderedPixmap()
738 QImage pix(m_frameWidth, m_frameHeight, QImage::Format_ARGB32);
739 pix.fill(Qt::transparent);
740 QPainter painter(&pix);
741 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
742 m_scene->clearTextSelection();
743 QPen framepen = m_frameBorder->pen();
744 m_frameBorder->setPen(Qt::NoPen);
745 startViewport->setVisible(false);
746 endViewport->setVisible(false);
747 m_frameImage->setVisible(false);
749 m_scene->render(&painter, QRectF(), QRectF(0, 0, m_frameWidth, m_frameHeight));
751 m_frameBorder->setPen(framepen);
752 startViewport->setVisible(true);
753 endViewport->setVisible(true);
754 m_frameImage->setVisible(true);
758 /** \brief Connected to the accepted signal - calls writeChoices */
759 void TitleWidget::slotAccepted()
764 /** \brief Store the current choices of font, background and rect values */
765 void TitleWidget::writeChoices()
767 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
768 KSharedConfigPtr config = KGlobal::config();
769 KConfigGroup titleConfig(config, "TitleWidget");
771 titleConfig.writeEntry("font_family", font_family->currentFont());
772 //titleConfig.writeEntry("font_size", font_size->value());
773 titleConfig.writeEntry("font_pixel_size", font_size->value());
774 titleConfig.writeEntry("font_color", fontColorButton->color());
775 titleConfig.writeEntry("font_alpha", textAlpha->value());
776 titleConfig.writeEntry("font_bold", buttonBold->isChecked());
777 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
778 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
780 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
781 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
782 titleConfig.writeEntry("rect_background_color", rectBColor->color());
783 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
784 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
786 titleConfig.writeEntry("background_color", kcolorbutton->color());
787 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
788 //! \todo Not sure if I should sync - it is probably safe to do it
793 /** \brief Read the last stored choices into the dialog */
794 void TitleWidget::readChoices()
796 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
797 KSharedConfigPtr config = KGlobal::config();
798 KConfigGroup titleConfig(config, "TitleWidget");
800 font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
801 font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
802 fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
803 textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
804 buttonBold->setChecked(titleConfig.readEntry("font_bold", buttonBold->isChecked()));
805 buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
806 buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
808 rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
809 rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
810 rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
811 rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
812 rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
814 kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
815 horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));