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 ***************************************************************************/
20 #include <QGraphicsView>
21 #include <QDomDocument>
22 #include <QGraphicsItem>
23 #include <QGraphicsSvgItem>
30 #include <KGlobalSettings>
31 #include <KFileDialog>
32 #include <KStandardDirs>
34 #include "titlewidget.h"
35 #include "kdenlivesettings.h"
37 int settingUp = false;
39 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) {
41 setFont(KGlobalSettings::toolBarFont());
42 //toolBox->setFont(KGlobalSettings::toolBarFont());
43 frame_properties->setEnabled(false);
44 rect_properties->setFixedHeight(frame_properties->height() + 4);
45 text_properties->setFixedHeight(frame_properties->height() + 4);
46 m_frameWidth = render->renderWidth();
47 m_frameHeight = render->renderHeight();
48 //connect(newTextButton, SIGNAL(clicked()), this, SLOT(slotNewText()));
49 //connect(newRectButton, SIGNAL(clicked()), this, SLOT(slotNewRect()));
50 // kcolorbutton == The color of the background
51 connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
52 // horizontalslider == The alpha of the background
53 connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
54 //connect(ktextedit, SIGNAL(textChanged()), this , SLOT(textChanged()));
55 //connect (fontBold, SIGNAL ( clicked()), this, SLOT( setBold()) ) ;
58 //ktextedit->setHidden(true);
59 connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
60 connect(font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(slotUpdateText())) ;
61 connect(font_size, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText())) ;
62 connect(textAlpha, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText()));
63 //connect (ktextedit, SIGNAL(selectionChanged()), this , SLOT (textChanged()));
65 connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
66 connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
67 connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
68 connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
69 connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
71 connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
72 connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
73 connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
74 connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
75 connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
76 connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
78 connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
79 connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
80 connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
81 connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
82 connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
84 connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
85 connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
86 connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
87 connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
88 connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
89 connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
90 connect(buttonBold, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
91 connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
92 connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
93 connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
96 connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
98 buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
99 buttonRealSize->setIcon(KIcon("zoom-original"));
100 buttonBold->setIcon(KIcon("format-text-bold"));
101 buttonItalic->setIcon(KIcon("format-text-italic"));
102 buttonUnder->setIcon(KIcon("format-text-underline"));
104 itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
105 itemhcenter->setToolTip(i18n("Align item horizontally"));
106 itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
107 itemvcenter->setToolTip(i18n("Align item vertically"));
109 QHBoxLayout *layout = new QHBoxLayout;
110 frame_toolbar->setLayout(layout);
111 layout->setContentsMargins(2, 2, 2, 2);
112 QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
114 m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), i18n("Add Rectangle"));
115 m_buttonRect->setCheckable(true);
116 connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
118 m_buttonText = m_toolbar->addAction(KIcon("insert-text"), i18n("Add Text"));
119 m_buttonText->setCheckable(true);
120 connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
122 m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), i18n("Add Image"));
123 m_buttonImage->setCheckable(false);
124 connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
126 m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), i18n("Selection Tool"));
127 m_buttonCursor->setCheckable(true);
128 connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
130 m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
131 m_buttonLoad->setCheckable(false);
132 connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
134 m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
135 m_buttonSave->setCheckable(false);
136 connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
138 layout->addWidget(m_toolbar);
139 text_properties->setHidden(true);
141 // initialize graphic scene
142 m_scene = new GraphicsSceneRectMove(this);
143 graphicsView->setScene(m_scene);
144 m_titledocument.setScene(m_scene);
146 // a gradient background
147 /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
148 gradient->setSpread(QGradient::ReflectSpread);
149 scene->setBackgroundBrush(*gradient);*/
151 m_frameImage = new QGraphicsPixmapItem();
153 qtrans.scale(2.0, 2.0);
154 m_frameImage->setTransform(qtrans);
155 m_frameImage->setZValue(-1200);
156 m_frameImage->setFlags(QGraphicsItem::ItemClipsToShape);
157 displayBackgroundFrame();
158 graphicsView->scene()->addItem(m_frameImage);
160 connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
161 connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
162 connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
163 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
164 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
165 connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
166 connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
167 connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
169 QPen framepen(Qt::DotLine);
170 framepen.setColor(Qt::red);
172 m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
173 m_frameBorder->setPen(framepen);
174 m_frameBorder->setZValue(-1100);
175 m_frameBorder->setBrush(Qt::transparent);
176 m_frameBorder->setFlags(QGraphicsItem::ItemClipsToShape);
177 graphicsView->scene()->addItem(m_frameBorder);
179 // mbd: load saved settings
183 QTimer::singleShot(500, this, SLOT(slotAdjustZoom()));
184 graphicsView->show();
185 //graphicsView->setRenderHint(QPainter::Antialiasing);
186 graphicsView->setInteractive(true);
187 //graphicsView->resize(400, 300);
188 kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
189 toolBox->setItemEnabled(2, false);
190 if (!url.isEmpty()) {
191 m_count = m_titledocument.loadDocument(url, startViewport, endViewport) + 1;
198 TitleWidget::~TitleWidget() {
201 delete m_buttonImage;
202 delete m_buttonCursor;
206 delete m_frameBorder;
208 if (startViewport) delete startViewport;
209 if (endViewport) delete endViewport;
214 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) {
232 QString titlePath = projectUrl.path() + "/titles/";
233 KStandardDirs::makeDir(titlePath);
234 return titlePath + titleName + ".png";
238 void TitleWidget::resizeEvent(QResizeEvent * event) {
242 void TitleWidget::slotTextTool() {
243 rect_properties->setHidden(true);
244 text_properties->setHidden(false);
245 m_scene->setTool(TITLE_TEXT);
246 m_buttonRect->setChecked(false);
247 m_buttonCursor->setChecked(false);
250 void TitleWidget::slotRectTool() {
251 text_properties->setHidden(true);
252 rect_properties->setHidden(false);
253 m_scene->setTool(TITLE_RECTANGLE);
254 m_buttonText->setChecked(false);
255 m_buttonCursor->setChecked(false);
256 m_buttonRect->setChecked(true);
259 void TitleWidget::slotSelectTool() {
260 m_scene->setTool(TITLE_SELECT);
261 m_buttonCursor->setChecked(true);
262 m_buttonText->setChecked(false);
263 m_buttonRect->setChecked(false);
266 void TitleWidget::slotImageTool() {
267 KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.svg *.png *.jpg *.jpeg *.gif *.raw", this, i18n("Load Image"));
268 if (!url.isEmpty()) {
269 if (url.path().endsWith(".svg")) {
270 QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
271 svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
272 svg->setZValue(m_count++);
273 svg->setData(Qt::UserRole, url.path());
274 graphicsView->scene()->addItem(svg);
276 QPixmap pix(url.path());
277 QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
278 image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
279 image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
280 image->setData(Qt::UserRole, url.path());
281 image->setZValue(m_count++);
282 graphicsView->scene()->addItem(image);
285 m_scene->setTool(TITLE_SELECT);
286 m_buttonRect->setChecked(false);
287 m_buttonCursor->setChecked(true);
288 m_buttonText->setChecked(false);
291 void TitleWidget::displayBackgroundFrame() {
292 if (!displayBg->isChecked()) {
293 QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
294 QPixmap pattern(20, 20);
298 p.fillRect(QRect(0, 0, 10, 10), QColor(210, 210, 210));
299 p.fillRect(QRect(10, 10, 20, 20), QColor(210, 210, 210));
304 p.fillRect(bg.rect(), br);
306 m_frameImage->setPixmap(bg);
308 m_frameImage->setPixmap(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2));
312 void TitleWidget::initViewports() {
313 startViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
314 endViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
316 QPen startpen(Qt::DotLine);
317 QPen endpen(Qt::DashDotLine);
318 startpen.setColor(QColor(100, 200, 100, 140));
319 endpen.setColor(QColor(200, 100, 100, 140));
321 startViewport->setPen(startpen);
322 endViewport->setPen(endpen);
324 startViewportSize->setValue(40);
325 endViewportSize->setValue(40);
327 startViewport->setZValue(-1000);
328 endViewport->setZValue(-1000);
330 startViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
331 endViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
333 graphicsView->scene()->addItem(startViewport);
334 graphicsView->scene()->addItem(endViewport);
337 void TitleWidget::slotUpdateZoom(int pos) {
338 m_scene->setZoom((double) pos / 100);
339 zoom_label->setText(QString::number(pos) + "%");
342 void TitleWidget::slotZoom(bool up) {
343 int pos = zoom_slider->value();
346 zoom_slider->setValue(pos);
349 void TitleWidget::slotAdjustZoom() {
350 /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
351 double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
352 if (scalex > scaley) scalex = scaley;
353 int zoompos = (int)(scalex * 7 + 0.5);*/
354 graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
355 int zoompos = graphicsView->matrix().m11() * 100;
356 zoom_slider->setValue(zoompos);
357 graphicsView->centerOn(m_frameBorder);
360 void TitleWidget::slotZoomOneToOne() {
361 zoom_slider->setValue(100);
362 graphicsView->centerOn(m_frameBorder);
365 void TitleWidget::slotNewRect(QGraphicsRectItem * rect) {
366 QColor f = rectFColor->color();
367 f.setAlpha(rectFAlpha->value());
369 penf.setWidth(rectLineWidth->value());
371 QColor b = rectBColor->color();
372 b.setAlpha(rectBAlpha->value());
373 rect->setBrush(QBrush(b));
374 rect->setZValue(m_count++);
375 //setCurrentItem(rect);
376 //graphicsView->setFocus();
379 void TitleWidget::slotNewText(QGraphicsTextItem *tt) {
380 QFont font = font_family->currentFont();
381 font.setPointSize(font_size->value());
383 font.setBold(buttonBold->isChecked());
384 font.setItalic(buttonItalic->isChecked());
385 font.setUnderline(buttonUnder->isChecked());
388 QColor color = fontColorButton->color();
389 color.setAlpha(textAlpha->value());
390 tt->setDefaultTextColor(color);
391 tt->setZValue(m_count++);
395 void TitleWidget::setCurrentItem(QGraphicsItem *item) {
396 m_scene->setSelectedItem(item);
399 void TitleWidget::zIndexChanged(int v) {
400 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
406 void TitleWidget::selectionChanged() {
407 if (m_scene->tool() != TITLE_SELECT) return;
408 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
409 //toolBox->setItemEnabled(2, false);
410 //toolBox->setItemEnabled(3, false);
411 value_x->blockSignals(true);
412 value_y->blockSignals(true);
413 value_w->blockSignals(true);
414 value_h->blockSignals(true);
415 //kDebug() << "//////// SELECTION CHANGED; ITEMS: " << l.size();
417 if ((l[0])->type() == 8) {
418 rect_properties->setHidden(true);
419 text_properties->setHidden(false);
420 QGraphicsTextItem* i = ((QGraphicsTextItem*)l[0]);
421 //if (l[0]->hasFocus())
422 //ktextedit->setHtml(i->toHtml());
423 toolBox->setCurrentIndex(0);
424 //toolBox->setItemEnabled(2, true);
425 font_size->blockSignals(true);
426 font_family->blockSignals(true);
427 buttonBold->blockSignals(true);
428 buttonItalic->blockSignals(true);
429 buttonUnder->blockSignals(true);
430 fontColorButton->blockSignals(true);
431 textAlpha->blockSignals(true);
433 QFont font = i->font();
434 font_family->setCurrentFont(font);
435 font_size->setValue(font.pointSize());
436 buttonBold->setChecked(font.bold());
437 buttonItalic->setChecked(font.italic());
438 buttonUnder->setChecked(font.underline());
440 QColor color = i->defaultTextColor();
441 fontColorButton->setColor(color);
442 textAlpha->setValue(color.alpha());
444 font_size->blockSignals(false);
445 font_family->blockSignals(false);
446 buttonBold->blockSignals(false);
447 buttonItalic->blockSignals(false);
448 buttonUnder->blockSignals(false);
449 fontColorButton->blockSignals(false);
450 textAlpha->blockSignals(false);
452 value_x->setValue((int) i->pos().x());
453 value_y->setValue((int) i->pos().y());
454 value_w->setValue((int) i->boundingRect().width());
455 value_h->setValue((int) i->boundingRect().height());
456 frame_properties->setEnabled(true);
457 value_w->setEnabled(false);
458 value_h->setEnabled(false);
459 } else if ((l[0])->type() == 3) {
460 rect_properties->setHidden(false);
461 text_properties->setHidden(true);
463 QGraphicsRectItem *rec = ((QGraphicsRectItem*)l[0]);
464 toolBox->setCurrentIndex(0);
465 //toolBox->setItemEnabled(3, true);
466 rectFAlpha->setValue(rec->pen().color().alpha());
467 rectBAlpha->setValue(rec->brush().color().alpha());
468 //kDebug() << rec->brush().color().alpha();
469 QColor fcol = rec->pen().color();
470 QColor bcol = rec->brush().color();
471 //fcol.setAlpha(255);
472 //bcol.setAlpha(255);
473 rectFColor->setColor(fcol);
474 rectBColor->setColor(bcol);
476 rectLineWidth->setValue(rec->pen().width());
477 value_x->setValue((int) rec->pos().x());
478 value_y->setValue((int) rec->pos().y());
479 value_w->setValue((int) rec->rect().width());
480 value_h->setValue((int) rec->rect().height());
481 frame_properties->setEnabled(true);
482 value_w->setEnabled(true);
483 value_h->setEnabled(true);
485 //toolBox->setCurrentIndex(0);
486 frame_properties->setEnabled(false);
488 zValue->setValue((int)l[0]->zValue());
489 itemzoom->setValue((int)(transformations[l[0]].scalex * 100));
490 itemrotate->setValue((int)(transformations[l[0]].rotate));
491 value_x->blockSignals(false);
492 value_y->blockSignals(false);
493 value_w->blockSignals(false);
494 value_h->blockSignals(false);
495 } else frame_properties->setEnabled(false);
498 void TitleWidget::slotAdjustSelectedItem() {
499 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
501 if (l[0]->type() == 3) {
503 QGraphicsRectItem *rec = ((QGraphicsRectItem*)l[0]);
504 rec->setPos(value_x->value(), value_y->value());
505 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
506 } else if (l[0]->type() == 8) {
508 QGraphicsTextItem *rec = ((QGraphicsTextItem*)l[0]);
509 rec->setPos(value_x->value(), value_y->value());
514 void TitleWidget::slotChangeBackground() {
515 QColor color = kcolorbutton->color();
516 color.setAlpha(horizontalSlider->value());
517 m_frameBorder->setBrush(QBrush(color));
520 void TitleWidget::textChanged() {
521 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
522 if (l.size() == 1 && (l[0])->type() == 8 && !l[0]->hasFocus()) {
523 //kDebug() << ktextedit->document()->toHtml();
524 //((QGraphicsTextItem*)l[0])->setHtml(ktextedit->toHtml());
528 void TitleWidget::slotUpdateText() {
529 QFont font = font_family->currentFont();
530 font.setPointSize(font_size->value());
531 font.setBold(buttonBold->isChecked());
532 font.setItalic(buttonItalic->isChecked());
533 font.setUnderline(buttonUnder->isChecked());
534 QColor color = fontColorButton->color();
535 color.setAlpha(textAlpha->value());
537 QGraphicsTextItem* item = NULL;
538 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
539 if (l.size() == 1 && (l[0])->type() == 8) {
540 item = (QGraphicsTextItem*)l[0];
543 //if (item->textCursor().selection ().isEmpty())
546 item->setDefaultTextColor(color);
549 QTextDocumentFragment selec = item->textCursor().selection ();
552 //if (ktextedit->textCursor().selectedText().isEmpty()) ktextedit->selectAll();
554 //ktextedit->setCurrentFont(font);
555 //ktextedit->setTextColor(color);
556 /*QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
557 if (l.size() == 1 && (l[0])->type() == 8 && l[0]->hasFocus()) {
558 QGraphicsTextItem* item = static_cast <QGraphicsTextItem*> (l[0]);
563 void TitleWidget::rectChanged() {
564 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
565 if (l.size() == 1 && (l[0])->type() == 3 && !settingUp) {
566 QGraphicsRectItem *rec = (QGraphicsRectItem*)l[0];
567 QColor f = rectFColor->color();
568 f.setAlpha(rectFAlpha->value());
570 penf.setWidth(rectLineWidth->value());
572 QColor b = rectBColor->color();
573 b.setAlpha(rectBAlpha->value());
574 rec->setBrush(QBrush(b));
578 void TitleWidget::fontBold() {
579 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
580 if (l.size() == 1 && (l[0])->type() == 8 && !l[0]->hasFocus()) {
581 //ktextedit->document()->setTextOption();
585 void TitleWidget::itemScaled(int val) {
586 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
588 Transform x = transformations[l[0]];
589 x.scalex = (double)val / 100.0;
590 x.scaley = (double)val / 100.0;
592 qtrans.scale(x.scalex, x.scaley);
593 qtrans.rotate(x.rotate);
594 l[0]->setTransform(qtrans);
595 transformations[l[0]] = x;
599 void TitleWidget::itemRotate(int val) {
600 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
602 Transform x = transformations[l[0]];
603 x.rotate = (double)val;
605 qtrans.scale(x.scalex, x.scaley);
606 qtrans.rotate(x.rotate);
607 l[0]->setTransform(qtrans);
608 transformations[l[0]] = x;
612 void TitleWidget::itemHCenter() {
613 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
615 QGraphicsItem *item = l[0];
617 if (item->type() == 3) {
618 br = ((QGraphicsRectItem*)item)->rect();
619 } else br = item->boundingRect();
620 int width = (int) br.width();
621 int newPos = (int)((m_frameWidth - width) / 2);
622 item->setPos(newPos, item->pos().y());
626 void TitleWidget::itemVCenter() {
627 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
629 QGraphicsItem *item = l[0];
631 if (item->type() == 3) {
632 br = ((QGraphicsRectItem*)item)->rect();
633 } else br = item->boundingRect();
634 int height = (int) br.height();
635 int newPos = (int)((m_frameHeight - height) / 2);
636 item->setPos(item->pos().x(), newPos);
640 void TitleWidget::setupViewports() {
641 double aspect_ratio = 4.0 / 3.0;//read from project
643 QRectF sp(0, 0, 0, 0);
644 QRectF ep(0, 0, 0, 0);
646 double sv_size = startViewportSize->value();
647 double ev_size = endViewportSize->value();
648 sp.adjust(-sv_size, -sv_size / aspect_ratio, sv_size, sv_size / aspect_ratio);
649 ep.adjust(-ev_size, -ev_size / aspect_ratio, ev_size, ev_size / aspect_ratio);
651 startViewport->setPos(startViewportX->value(), startViewportY->value());
652 endViewport->setPos(endViewportX->value(), endViewportY->value());
654 startViewport->setPolygon(QPolygonF(sp));
655 endViewport->setPolygon(QPolygonF(ep));
659 void TitleWidget::loadTitle() {
660 KUrl url = KFileDialog::getOpenUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Load Title"));
661 if (!url.isEmpty()) {
662 QList<QGraphicsItem *> items = m_scene->items();
663 for (int i = 0; i < items.size(); i++) {
664 if (items.at(i)->zValue() > -1000) delete items.at(i);
666 m_count = m_titledocument.loadDocument(url, startViewport, endViewport) + 1;
671 void TitleWidget::saveTitle(KUrl url) {
672 if (url.isEmpty()) url = KFileDialog::getSaveUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Save Title"));
673 if (!url.isEmpty()) m_titledocument.saveDocument(url, startViewport, endViewport);
676 QDomDocument TitleWidget::xml() {
677 return m_titledocument.xml(startViewport, endViewport);
680 void TitleWidget::setXml(QDomDocument doc) {
681 m_count = m_titledocument.loadFromXml(doc, startViewport, endViewport);
682 // mbd: Update the GUI color selectors to match the stuff from the loaded document
683 QColor background_color = m_titledocument.getBackgroundColor();
684 horizontalSlider->blockSignals(true);
685 kcolorbutton->blockSignals(true);
686 horizontalSlider->setValue(background_color.alpha());
687 background_color.setAlpha(255);
688 kcolorbutton->setColor(background_color);
689 horizontalSlider->blockSignals(false);
690 kcolorbutton->blockSignals(false);
695 QImage TitleWidget::renderedPixmap() {
696 QImage pix(m_frameWidth, m_frameHeight, QImage::Format_ARGB32);
697 pix.fill(Qt::transparent);
698 QPainter painter(&pix);
699 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
700 m_scene->clearTextSelection();
701 QPen framepen = m_frameBorder->pen();
702 m_frameBorder->setPen(Qt::NoPen);
703 startViewport->setVisible(false);
704 endViewport->setVisible(false);
705 m_frameImage->setVisible(false);
707 m_scene->render(&painter, QRectF(), QRectF(0, 0, m_frameWidth, m_frameHeight));
709 m_frameBorder->setPen(framepen);
710 startViewport->setVisible(true);
711 endViewport->setVisible(true);
712 m_frameImage->setVisible(true);
716 /** \brief Connected to the accepted signal - calls writeChoices */
717 void TitleWidget::slotAccepted() {
721 /** \brief Store the current choices of font, background and rect values */
722 void TitleWidget::writeChoices() {
723 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
724 KSharedConfigPtr config = KGlobal::config();
725 KConfigGroup titleConfig(config, "TitleWidget");
727 titleConfig.writeEntry("font_family", font_family->currentFont());
728 titleConfig.writeEntry("font_size", font_size->value());
729 titleConfig.writeEntry("font_color", fontColorButton->color());
730 titleConfig.writeEntry("font_alpha", textAlpha->value());
731 titleConfig.writeEntry("font_bold", buttonBold->isChecked());
732 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
733 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
735 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
736 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
737 titleConfig.writeEntry("rect_background_color", rectBColor->color());
738 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
739 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
741 titleConfig.writeEntry("background_color", kcolorbutton->color());
742 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
743 //! \todo Not sure if I should sync - it is probably safe to do it
748 /** \brief Read the last stored choices into the dialog */
749 void TitleWidget::readChoices() {
750 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
751 KSharedConfigPtr config = KGlobal::config();
752 KConfigGroup titleConfig(config, "TitleWidget");
754 font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
755 font_size->setValue(titleConfig.readEntry("font_size", font_size->value()));
756 fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
757 textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
758 buttonBold->setChecked(titleConfig.readEntry("font_bold", buttonBold->isChecked()));
759 buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
760 buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
762 rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
763 rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
764 rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
765 rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
766 rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
768 kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
769 horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));