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"
24 #include <KGlobalSettings>
25 #include <KFileDialog>
26 #include <KStandardDirs>
27 #include <KMessageBox>
29 #include <QDomDocument>
30 #include <QGraphicsItem>
31 #include <QGraphicsSvgItem>
35 #include <QTextBlockFormat>
36 #include <QTextCursor>
38 int settingUp = false;
40 const int IMAGEITEM = 7;
41 const int RECTITEM = 3;
42 const int TEXTITEM = 8;
44 TitleWidget::TitleWidget(KUrl url, QString projectPath, Render *render, QWidget *parent) :
47 m_startViewport(NULL),
51 m_projectPath(projectPath)
54 setFont(KGlobalSettings::toolBarFont());
55 //toolBox->setFont(KGlobalSettings::toolBarFont());
56 frame_properties->setEnabled(false);
57 rect_properties->setFixedHeight(frame_properties->height() + 4);
58 text_properties->setFixedHeight(frame_properties->height() + 4);
59 m_frameWidth = render->renderWidth();
60 m_frameHeight = render->renderHeight();
61 //connect(newTextButton, SIGNAL(clicked()), this, SLOT(slotNewText()));
62 //connect(newRectButton, SIGNAL(clicked()), this, SLOT(slotNewRect()));
63 // kcolorbutton == The color of the background
64 connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
65 // horizontalslider == The alpha of the background
66 connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
69 connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
70 connect(font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(slotUpdateText())) ;
71 connect(font_size, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText())) ;
72 connect(textAlpha, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText()));
74 connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
75 connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
76 connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
77 connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
78 connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
80 connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
81 connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
82 connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
83 connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
84 connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
85 connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
87 connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
88 connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
89 connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
90 connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
91 connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
93 connect(origin_x_left, SIGNAL(clicked()), this, SLOT(slotOriginXClicked()));
94 connect(origin_y_top, SIGNAL(clicked()), this, SLOT(slotOriginYClicked()));
96 connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
97 connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
98 connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
99 connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
100 connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
101 connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
102 connect(buttonBold, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
103 connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
104 connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
105 connect(buttonAlignLeft, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
106 connect(buttonAlignRight, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
107 connect(buttonAlignCenter, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
108 connect(buttonAlignNone, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
109 connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
112 connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
114 buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
115 buttonRealSize->setIcon(KIcon("zoom-original"));
116 buttonBold->setIcon(KIcon("format-text-bold"));
117 buttonItalic->setIcon(KIcon("format-text-italic"));
118 buttonUnder->setIcon(KIcon("format-text-underline"));
119 buttonAlignCenter->setIcon(KIcon("format-justify-center"));
120 buttonAlignLeft->setIcon(KIcon("format-justify-left"));
121 buttonAlignRight->setIcon(KIcon("format-justify-right"));
122 buttonAlignNone->setToolTip(i18n("No alignment"));
123 buttonAlignRight->setToolTip(i18n("Align right"));
124 buttonAlignLeft->setToolTip(i18n("Align left"));
125 buttonAlignCenter->setToolTip(i18n("Align center"));
127 itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
128 itemhcenter->setToolTip(i18n("Align item horizontally"));
129 itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
130 itemvcenter->setToolTip(i18n("Align item vertically"));
132 QHBoxLayout *layout = new QHBoxLayout;
133 frame_toolbar->setLayout(layout);
134 layout->setContentsMargins(2, 2, 2, 2);
135 QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
137 m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), i18n("Add Rectangle"));
138 m_buttonRect->setCheckable(true);
139 connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
141 m_buttonText = m_toolbar->addAction(KIcon("insert-text"), i18n("Add Text"));
142 m_buttonText->setCheckable(true);
143 connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
145 m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), i18n("Add Image"));
146 m_buttonImage->setCheckable(false);
147 connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
149 m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), i18n("Selection Tool"));
150 m_buttonCursor->setCheckable(true);
151 connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
153 m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
154 m_buttonLoad->setCheckable(false);
155 connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
157 m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
158 m_buttonSave->setCheckable(false);
159 connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
161 layout->addWidget(m_toolbar);
162 text_properties->setHidden(true);
164 // initialize graphic scene
165 m_scene = new GraphicsSceneRectMove(this);
166 graphicsView->setScene(m_scene);
167 m_titledocument.setScene(m_scene);
168 connect(m_scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(slotChanged()));
170 // a gradient background
171 /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
172 gradient->setSpread(QGradient::ReflectSpread);
173 scene->setBackgroundBrush(*gradient);*/
175 m_frameImage = new QGraphicsPixmapItem();
177 qtrans.scale(2.0, 2.0);
178 m_frameImage->setTransform(qtrans);
179 m_frameImage->setZValue(-1200);
180 m_frameImage->setFlags(QGraphicsItem::ItemClipsToShape);
181 displayBackgroundFrame();
182 graphicsView->scene()->addItem(m_frameImage);
184 connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
185 connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
186 connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
187 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
188 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
189 connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
190 connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
191 connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
193 QPen framepen(Qt::DotLine);
194 framepen.setColor(Qt::red);
196 m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
197 m_frameBorder->setPen(framepen);
198 m_frameBorder->setZValue(-1100);
199 m_frameBorder->setBrush(Qt::transparent);
200 m_frameBorder->setFlags(QGraphicsItem::ItemClipsToShape);
201 graphicsView->scene()->addItem(m_frameBorder);
203 // mbd: load saved settings
207 QTimer::singleShot(500, this, SLOT(slotAdjustZoom()));
208 graphicsView->show();
209 //graphicsView->setRenderHint(QPainter::Antialiasing);
210 graphicsView->setInteractive(true);
211 //graphicsView->resize(400, 300);
212 kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
213 toolBox->setItemEnabled(2, false);
214 if (!url.isEmpty()) {
215 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
222 TitleWidget::~TitleWidget()
226 delete m_buttonImage;
227 delete m_buttonCursor;
231 delete m_frameBorder;
233 delete m_startViewport;
234 delete m_endViewport;
239 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl)
242 QString titlePath = projectUrl.path() + "/titles/";
243 KStandardDirs::makeDir(titlePath);
244 QString titleName = "title";
246 QString path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
247 while (QFile::exists(path + ".png")) {
249 path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
251 result.append(titleName + QString::number(counter).rightJustified(3, '0', false));
252 result.append(path + ".png");
256 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
259 QString titlePath = projectUrl.path() + "/titles/";
260 KStandardDirs::makeDir(titlePath);
261 return titlePath + titleName + ".png";
265 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
270 void TitleWidget::slotTextTool()
272 rect_properties->setHidden(true);
273 text_properties->setHidden(false);
274 m_scene->setTool(TITLE_TEXT);
275 m_buttonRect->setChecked(false);
276 m_buttonCursor->setChecked(false);
279 void TitleWidget::slotRectTool()
281 text_properties->setHidden(true);
282 rect_properties->setHidden(false);
283 m_scene->setTool(TITLE_RECTANGLE);
284 m_buttonText->setChecked(false);
285 m_buttonCursor->setChecked(false);
286 m_buttonRect->setChecked(true);
289 void TitleWidget::slotSelectTool()
291 m_scene->setTool(TITLE_SELECT);
292 m_buttonCursor->setChecked(true);
293 m_buttonText->setChecked(false);
294 m_buttonRect->setChecked(false);
297 void TitleWidget::slotImageTool()
299 KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.svg *.png *.jpg *.jpeg *.gif *.raw", this, i18n("Load Image"));
300 if (!url.isEmpty()) {
301 if (url.path().endsWith(".svg")) {
302 QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
303 svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
304 svg->setZValue(m_count++);
305 svg->setData(Qt::UserRole, url.path());
306 graphicsView->scene()->addItem(svg);
308 QPixmap pix(url.path());
309 QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
310 image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
311 image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
312 image->setData(Qt::UserRole, url.path());
313 image->setZValue(m_count++);
314 graphicsView->scene()->addItem(image);
317 m_scene->setTool(TITLE_SELECT);
318 m_buttonRect->setChecked(false);
319 m_buttonCursor->setChecked(true);
320 m_buttonText->setChecked(false);
323 void TitleWidget::displayBackgroundFrame()
325 if (!displayBg->isChecked()) {
326 QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
327 QPixmap pattern(20, 20);
331 p.fillRect(QRect(0, 0, 10, 10), QColor(210, 210, 210));
332 p.fillRect(QRect(10, 10, 20, 20), QColor(210, 210, 210));
337 p.fillRect(bg.rect(), br);
339 m_frameImage->setPixmap(bg);
341 m_frameImage->setPixmap(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2));
345 void TitleWidget::initViewports()
347 m_startViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
348 m_endViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
350 QPen startpen(Qt::DotLine);
351 QPen endpen(Qt::DashDotLine);
352 startpen.setColor(QColor(100, 200, 100, 140));
353 endpen.setColor(QColor(200, 100, 100, 140));
355 m_startViewport->setPen(startpen);
356 m_endViewport->setPen(endpen);
358 startViewportSize->setValue(40);
359 endViewportSize->setValue(40);
361 m_startViewport->setZValue(-1000);
362 m_endViewport->setZValue(-1000);
364 m_startViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
365 m_endViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
367 graphicsView->scene()->addItem(m_startViewport);
368 graphicsView->scene()->addItem(m_endViewport);
371 void TitleWidget::slotUpdateZoom(int pos)
373 m_scene->setZoom((double) pos / 100);
374 zoom_label->setText(QString::number(pos) + '%');
377 void TitleWidget::slotZoom(bool up)
379 int pos = zoom_slider->value();
382 zoom_slider->setValue(pos);
385 void TitleWidget::slotAdjustZoom()
387 /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
388 double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
389 if (scalex > scaley) scalex = scaley;
390 int zoompos = (int)(scalex * 7 + 0.5);*/
391 graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
392 int zoompos = graphicsView->matrix().m11() * 100;
393 zoom_slider->setValue(zoompos);
394 graphicsView->centerOn(m_frameBorder);
397 void TitleWidget::slotZoomOneToOne()
399 zoom_slider->setValue(100);
400 graphicsView->centerOn(m_frameBorder);
403 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
405 QColor f = rectFColor->color();
406 f.setAlpha(rectFAlpha->value());
408 penf.setWidth(rectLineWidth->value());
410 QColor b = rectBColor->color();
411 b.setAlpha(rectBAlpha->value());
412 rect->setBrush(QBrush(b));
413 rect->setZValue(m_count++);
414 //setCurrentItem(rect);
415 //graphicsView->setFocus();
418 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
420 QFont font = font_family->currentFont();
421 font.setPixelSize(font_size->value());
423 font.setBold(buttonBold->isChecked());
424 font.setItalic(buttonItalic->isChecked());
425 font.setUnderline(buttonUnder->isChecked());
428 QColor color = fontColorButton->color();
429 color.setAlpha(textAlpha->value());
430 tt->setDefaultTextColor(color);
431 tt->setZValue(m_count++);
435 void TitleWidget::setCurrentItem(QGraphicsItem *item)
437 m_scene->setSelectedItem(item);
440 void TitleWidget::zIndexChanged(int v)
442 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
448 void TitleWidget::selectionChanged()
450 if (m_scene->tool() != TITLE_SELECT) return;
451 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
452 //toolBox->setItemEnabled(2, false);
453 //toolBox->setItemEnabled(3, false);
454 value_x->blockSignals(true);
455 value_y->blockSignals(true);
456 value_w->blockSignals(true);
457 value_h->blockSignals(true);
458 itemzoom->blockSignals(true);
459 itemrotate->blockSignals(true);
461 if (l.at(0)->type() == TEXTITEM) {
462 rect_properties->setHidden(true);
463 text_properties->setHidden(false);
464 QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
465 //if (l[0]->hasFocus())
466 toolBox->setCurrentIndex(0);
467 //toolBox->setItemEnabled(2, true);
468 font_size->blockSignals(true);
469 font_family->blockSignals(true);
470 buttonBold->blockSignals(true);
471 buttonItalic->blockSignals(true);
472 buttonUnder->blockSignals(true);
473 fontColorButton->blockSignals(true);
474 textAlpha->blockSignals(true);
475 buttonAlignLeft->blockSignals(true);
476 buttonAlignRight->blockSignals(true);
477 buttonAlignNone->blockSignals(true);
478 buttonAlignCenter->blockSignals(true);
480 QFont font = i->font();
481 font_family->setCurrentFont(font);
482 font_size->setValue(font.pixelSize());
483 buttonBold->setChecked(font.bold());
484 buttonItalic->setChecked(font.italic());
485 buttonUnder->setChecked(font.underline());
487 QColor color = i->defaultTextColor();
488 fontColorButton->setColor(color);
489 textAlpha->setValue(color.alpha());
491 QTextCursor cur = i->textCursor();
492 QTextBlockFormat format = cur.blockFormat();
493 if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
494 else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
495 else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
496 else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
498 font_size->blockSignals(false);
499 font_family->blockSignals(false);
500 buttonBold->blockSignals(false);
501 buttonItalic->blockSignals(false);
502 buttonUnder->blockSignals(false);
503 fontColorButton->blockSignals(false);
504 textAlpha->blockSignals(false);
505 buttonAlignLeft->blockSignals(false);
506 buttonAlignRight->blockSignals(false);
507 buttonAlignNone->blockSignals(false);
508 buttonAlignCenter->blockSignals(false);
510 updateCoordinates(i);
511 value_w->setValue((int) i->boundingRect().width());
512 value_h->setValue((int) i->boundingRect().height());
513 frame_properties->setEnabled(true);
514 value_w->setEnabled(false);
515 value_h->setEnabled(false);
516 } else if ((l.at(0))->type() == RECTITEM) {
517 rect_properties->setHidden(false);
518 text_properties->setHidden(true);
520 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
521 toolBox->setCurrentIndex(0);
522 //toolBox->setItemEnabled(3, true);
523 rectFAlpha->setValue(rec->pen().color().alpha());
524 rectBAlpha->setValue(rec->brush().color().alpha());
525 //kDebug() << rec->brush().color().alpha();
526 QColor fcol = rec->pen().color();
527 QColor bcol = rec->brush().color();
528 //fcol.setAlpha(255);
529 //bcol.setAlpha(255);
530 rectFColor->setColor(fcol);
531 rectBColor->setColor(bcol);
533 rectLineWidth->setValue(rec->pen().width());
535 updateCoordinates(rec);
536 value_w->setValue((int) rec->rect().width());
537 value_h->setValue((int) rec->rect().height());
538 frame_properties->setEnabled(true);
539 value_w->setEnabled(true);
540 value_h->setEnabled(true);
542 //toolBox->setCurrentIndex(0);
543 frame_properties->setEnabled(false);
545 zValue->setValue((int)l.at(0)->zValue());
546 itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
547 itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
548 value_x->blockSignals(false);
549 value_y->blockSignals(false);
550 value_w->blockSignals(false);
551 value_h->blockSignals(false);
552 itemzoom->blockSignals(false);
553 itemrotate->blockSignals(false);
554 } else frame_properties->setEnabled(false);
557 /** \brief Updates position/size of the selected item when a value
558 * of a text field (coordinates, size) has changed */
559 void TitleWidget::slotAdjustSelectedItem()
561 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
563 if (l.at(0)->type() == RECTITEM) {
565 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
567 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
568 } else if (l.at(0)->type() == TEXTITEM) {
570 updatePosition(l.at(0));
575 /** \brief Updates the coordinates in the text fields from the item */
576 void TitleWidget::updateCoordinates(QGraphicsItem *i)
578 // Block signals emitted by this method
579 value_x->blockSignals(true);
580 value_y->blockSignals(true);
582 if (i->type() == TEXTITEM) {
584 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *> (i);
586 // Set the correct x coordinate value
587 if (origin_x_left->isChecked()) {
588 // Origin (0 point) is at m_frameWidth, coordinate axis is inverted
589 value_x->setValue((int) (m_frameWidth - rec->pos().x() - rec->boundingRect().width()));
591 // Origin is at 0 (default)
592 value_x->setValue((int) rec->pos().x());
596 if (origin_y_top->isChecked()) {
597 value_y->setValue((int) (m_frameHeight - rec->pos().y() - rec->boundingRect().height()));
599 value_y->setValue((int) rec->pos().y());
602 } else if (i->type() == RECTITEM) {
604 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *> (i);
606 if (origin_x_left->isChecked()) {
607 // Origin (0 point) is at m_frameWidth
608 value_x->setValue((int) (m_frameWidth - rec->pos().x() - rec->rect().width()));
610 // Origin is at 0 (default)
611 value_x->setValue((int) rec->pos().x());
614 if (origin_y_top->isChecked()) {
615 value_y->setValue((int) (m_frameHeight - rec->pos().y() - rec->rect().height()));
617 value_y->setValue((int) rec->pos().y());
623 // Stop blocking signals now
624 value_x->blockSignals(false);
625 value_y->blockSignals(false);
628 /** \brief Updates the position of an item reading coordinates from the text fields */
629 void TitleWidget::updatePosition(QGraphicsItem *i) {
631 if (i->type() == TEXTITEM) {
632 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
635 if (origin_x_left->isChecked()) {
636 /* Origin of the x axis is at m_frameWidth,
637 * and distance from right border of the item to the right
638 * border of the frame is taken.
639 * See comment to slotOriginXClicked().
641 posX = m_frameWidth - value_x->value() - rec->boundingRect().width();
643 posX = value_x->value();
647 if (origin_y_top->isChecked()) {
648 /* Same for y axis */
649 posY = m_frameHeight - value_y->value() - rec->boundingRect().height();
651 posY = value_y->value();
654 rec->setPos(posX, posY);
655 } else if (i->type() == RECTITEM) {
657 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *> (i);
660 if (origin_x_left->isChecked()) {
661 posX = m_frameWidth - value_x->value() - rec->rect().width();
663 posX = value_x->value();
667 if (origin_y_top->isChecked()) {
668 posY = m_frameHeight - value_y->value() - rec->rect().height();
670 posY = value_y->value();
673 rec->setPos(posX, posY);
678 void TitleWidget::slotOriginXClicked()
680 // Update the text displayed on the button.
681 if (origin_x_left->isChecked()) {
682 origin_x_left->setText(i18n("\u2212X"));
684 origin_x_left->setText(i18n("+X"));
687 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
689 updateCoordinates(l.at(0));
693 void TitleWidget::slotOriginYClicked()
695 // Update the text displayed on the button.
696 if (origin_y_top->isChecked()) {
697 origin_y_top->setText(i18n("\u2212Y"));
699 origin_y_top->setText(i18n("+Y"));
702 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
704 updateCoordinates(l.at(0));
708 void TitleWidget::slotChangeBackground()
710 QColor color = kcolorbutton->color();
711 color.setAlpha(horizontalSlider->value());
712 m_frameBorder->setBrush(QBrush(color));
716 * Something (yeah) has changed in our QGraphicsScene.
717 * If the user has activated origin_x_left (everything also for y),
718 * we need to look whether a text element has been selected. If yes,
719 * we need to ensure that the right border of the text field
720 * remains fixed also when some text has been entered.
722 * This is also known as right-justified, with the difference that
723 * it is not valid for text but for its boundingRect. Text may still
726 void TitleWidget::slotChanged() {
727 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
728 if (l.size() >= 1 && l.at(0)->type() == TEXTITEM) {
729 if (origin_x_left->isChecked() || origin_y_top->isChecked()) {
730 updatePosition(l.at(0));
735 void TitleWidget::slotUpdateText()
737 QFont font = font_family->currentFont();
738 font.setPixelSize(font_size->value());
739 font.setBold(buttonBold->isChecked());
740 font.setItalic(buttonItalic->isChecked());
741 font.setUnderline(buttonUnder->isChecked());
742 QColor color = fontColorButton->color();
743 color.setAlpha(textAlpha->value());
745 QGraphicsTextItem* item = NULL;
746 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
747 if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
748 item = static_cast <QGraphicsTextItem *>(l.at(0));
751 //if (item->textCursor().selection ().isEmpty())
752 QTextCursor cur = item->textCursor();
753 QTextBlockFormat format = cur.blockFormat();
754 if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
755 item->setTextWidth(item->boundingRect().width());
756 if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
757 else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
758 else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
760 format.setAlignment(Qt::AlignLeft);
761 item->setTextWidth(-1);
766 item->setDefaultTextColor(color);
767 cur.select(QTextCursor::Document);
768 cur.setBlockFormat(format);
769 item->setTextCursor(cur);
770 cur.clearSelection();
771 item->setTextCursor(cur);
776 void TitleWidget::rectChanged()
778 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
779 if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
780 QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
781 QColor f = rectFColor->color();
782 f.setAlpha(rectFAlpha->value());
784 penf.setWidth(rectLineWidth->value());
786 QColor b = rectBColor->color();
787 b.setAlpha(rectBAlpha->value());
788 rec->setBrush(QBrush(b));
792 void TitleWidget::itemScaled(int val)
794 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
796 Transform x = m_transformations.value(l.at(0));
797 x.scalex = (double)val / 100.0;
798 x.scaley = (double)val / 100.0;
800 qtrans.scale(x.scalex, x.scaley);
801 qtrans.rotate(x.rotate);
802 l[0]->setTransform(qtrans);
803 m_transformations[l.at(0)] = x;
807 void TitleWidget::itemRotate(int val)
809 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
811 Transform x = m_transformations[l.at(0)];
812 x.rotate = (double)val;
814 qtrans.scale(x.scalex, x.scaley);
815 qtrans.rotate(x.rotate);
816 l[0]->setTransform(qtrans);
817 m_transformations[l.at(0)] = x;
821 void TitleWidget::itemHCenter()
823 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
825 QGraphicsItem *item = l.at(0);
827 if (item->type() == RECTITEM) {
828 br = ((QGraphicsRectItem*)item)->rect();
829 } else br = item->sceneBoundingRect();
830 int width = (int) br.width();
831 int newPos = (int)((m_frameWidth - width) / 2);
832 item->setPos(newPos, item->pos().y());
836 void TitleWidget::itemVCenter()
838 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
840 QGraphicsItem *item = l.at(0);
842 if (item->type() == RECTITEM) {
843 br = ((QGraphicsRectItem*)item)->rect();
844 } else br = item->sceneBoundingRect();
845 int height = (int) br.height();
846 int newPos = (int)((m_frameHeight - height) / 2);
847 item->setPos(item->pos().x(), newPos);
851 void TitleWidget::setupViewports()
853 double aspect_ratio = 4.0 / 3.0;//read from project
855 QRectF sp(0, 0, 0, 0);
856 QRectF ep(0, 0, 0, 0);
858 double sv_size = startViewportSize->value();
859 double ev_size = endViewportSize->value();
860 sp.adjust(-sv_size, -sv_size / aspect_ratio, sv_size, sv_size / aspect_ratio);
861 ep.adjust(-ev_size, -ev_size / aspect_ratio, ev_size, ev_size / aspect_ratio);
863 m_startViewport->setPos(startViewportX->value(), startViewportY->value());
864 m_endViewport->setPos(endViewportX->value(), endViewportY->value());
866 m_startViewport->setPolygon(QPolygonF(sp));
867 m_endViewport->setPolygon(QPolygonF(ep));
871 void TitleWidget::loadTitle()
873 KUrl url = KFileDialog::getOpenUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Load Title"));
874 if (!url.isEmpty()) {
875 QList<QGraphicsItem *> items = m_scene->items();
876 for (int i = 0; i < items.size(); i++) {
877 if (items.at(i)->zValue() > -1000) delete items.at(i);
879 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
884 void TitleWidget::saveTitle(KUrl url)
886 if (url.isEmpty()) url = KFileDialog::getSaveUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Save Title"));
887 if (!url.isEmpty()) {
888 if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport) == false)
889 KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
893 QDomDocument TitleWidget::xml()
895 QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
896 if (cropImage->isChecked()) {
897 doc.documentElement().setAttribute("crop", 1);
902 void TitleWidget::setXml(QDomDocument doc)
904 m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport);
905 QDomElement e = doc.documentElement();
906 cropImage->setChecked(e.hasAttribute("crop"));
907 m_transformations.clear();
908 QList <QGraphicsItem *> items = graphicsView->scene()->items();
909 const double PI = 4.0 * atan(1.0);
910 for (int i = 0; i < items.count(); i++) {
911 QTransform t = items.at(i)->transform();
915 x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
916 m_transformations[items.at(i)] = x;
918 // mbd: Update the GUI color selectors to match the stuff from the loaded document
919 QColor background_color = m_titledocument.getBackgroundColor();
920 horizontalSlider->blockSignals(true);
921 kcolorbutton->blockSignals(true);
922 horizontalSlider->setValue(background_color.alpha());
923 background_color.setAlpha(255);
924 kcolorbutton->setColor(background_color);
925 horizontalSlider->blockSignals(false);
926 kcolorbutton->blockSignals(false);
931 QImage TitleWidget::renderedPixmap()
935 int maxX = m_frameWidth;
936 int maxY = m_frameHeight;
937 if (!cropImage->isChecked()) {
938 m_scene->removeItem(m_startViewport);
939 m_scene->removeItem(m_endViewport);
940 QRect boundingRect = m_scene->itemsBoundingRect().toRect();
941 if (boundingRect.left() < 0) minX = boundingRect.left();
942 if (boundingRect.top() < 0) minY = boundingRect.top();
943 if (boundingRect.right() > maxX) maxX = boundingRect.right();
944 if (boundingRect.bottom() > maxY) maxY = boundingRect.bottom();
953 QImage pix(maxX, maxY, QImage::Format_ARGB32);
954 pix.fill(Qt::transparent);
955 QPainter painter(&pix);
956 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
957 m_scene->clearTextSelection();
958 QPen framepen = m_frameBorder->pen();
959 m_frameBorder->setPen(Qt::NoPen);
960 m_startViewport->setVisible(false);
961 m_endViewport->setVisible(false);
962 m_frameImage->setVisible(false);
964 m_scene->render(&painter, QRectF(), QRectF(minX, minY, maxX - minX, maxY - minY));
966 m_frameBorder->setPen(framepen);
967 m_startViewport->setVisible(true);
968 m_endViewport->setVisible(true);
969 m_frameImage->setVisible(true);
973 /** \brief Connected to the accepted signal - calls writeChoices */
974 void TitleWidget::slotAccepted()
979 /** \brief Store the current choices of font, background and rect values */
980 void TitleWidget::writeChoices()
982 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
983 KSharedConfigPtr config = KGlobal::config();
984 KConfigGroup titleConfig(config, "TitleWidget");
986 titleConfig.writeEntry("font_family", font_family->currentFont());
987 //titleConfig.writeEntry("font_size", font_size->value());
988 titleConfig.writeEntry("font_pixel_size", font_size->value());
989 titleConfig.writeEntry("font_color", fontColorButton->color());
990 titleConfig.writeEntry("font_alpha", textAlpha->value());
991 titleConfig.writeEntry("font_bold", buttonBold->isChecked());
992 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
993 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
995 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
996 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
997 titleConfig.writeEntry("rect_background_color", rectBColor->color());
998 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
999 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
1001 titleConfig.writeEntry("background_color", kcolorbutton->color());
1002 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
1004 titleConfig.writeEntry("crop_image", cropImage->isChecked());
1005 //! \todo Not sure if I should sync - it is probably safe to do it
1010 /** \brief Read the last stored choices into the dialog */
1011 void TitleWidget::readChoices()
1013 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1014 KSharedConfigPtr config = KGlobal::config();
1015 KConfigGroup titleConfig(config, "TitleWidget");
1017 font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
1018 font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
1019 fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
1020 textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
1021 buttonBold->setChecked(titleConfig.readEntry("font_bold", buttonBold->isChecked()));
1022 buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
1023 buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
1025 rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
1026 rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
1027 rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
1028 rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
1029 rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
1031 kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
1032 horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
1034 cropImage->setChecked(titleConfig.readEntry("crop_image", cropImage->isChecked()));