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>
33 #include <QTextBlockFormat>
34 #include <QTextCursor>
36 int settingUp = false;
38 const int IMAGEITEM = 7;
39 const int RECTITEM = 3;
40 const int TEXTITEM = 8;
42 TitleWidget::TitleWidget(KUrl url, QString projectPath, Render *render, QWidget *parent) :
45 m_startViewport(NULL),
49 m_projectPath(projectPath)
52 setFont(KGlobalSettings::toolBarFont());
53 //toolBox->setFont(KGlobalSettings::toolBarFont());
54 frame_properties->setEnabled(false);
55 rect_properties->setFixedHeight(frame_properties->height() + 4);
56 text_properties->setFixedHeight(frame_properties->height() + 4);
57 m_frameWidth = render->renderWidth();
58 m_frameHeight = render->renderHeight();
59 //connect(newTextButton, SIGNAL(clicked()), this, SLOT(slotNewText()));
60 //connect(newRectButton, SIGNAL(clicked()), this, SLOT(slotNewRect()));
61 // kcolorbutton == The color of the background
62 connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
63 // horizontalslider == The alpha of the background
64 connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
67 connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
68 connect(font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(slotUpdateText())) ;
69 connect(font_size, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText())) ;
70 connect(textAlpha, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText()));
72 connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
73 connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
74 connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
75 connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
76 connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
78 connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
79 connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
80 connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
81 connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
82 connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
83 connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
85 connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
86 connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
87 connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
88 connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
89 connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
91 connect(origin_x_left, SIGNAL(clicked()), this, SLOT(slotOriginXClicked()));
92 connect(origin_y_top, SIGNAL(clicked()), this, SLOT(slotOriginYClicked()));
94 connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
95 connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
96 connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
97 connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
98 connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
99 connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
100 connect(buttonBold, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
101 connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
102 connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
103 connect(buttonAlignLeft, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
104 connect(buttonAlignRight, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
105 connect(buttonAlignCenter, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
106 connect(buttonAlignNone, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
107 connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
110 connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
112 buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
113 buttonRealSize->setIcon(KIcon("zoom-original"));
114 buttonBold->setIcon(KIcon("format-text-bold"));
115 buttonItalic->setIcon(KIcon("format-text-italic"));
116 buttonUnder->setIcon(KIcon("format-text-underline"));
117 buttonAlignCenter->setIcon(KIcon("format-justify-center"));
118 buttonAlignLeft->setIcon(KIcon("format-justify-left"));
119 buttonAlignRight->setIcon(KIcon("format-justify-right"));
120 buttonAlignNone->setToolTip(i18n("No alignment"));
121 buttonAlignRight->setToolTip(i18n("Align right"));
122 buttonAlignLeft->setToolTip(i18n("Align left"));
123 buttonAlignCenter->setToolTip(i18n("Align center"));
124 origin_x_left->setToolTip(i18n("Invert x axis and change 0 point"));
125 origin_y_top->setToolTip(i18n("Invert y axis and change 0 point"));
126 rectBColor->setToolTip(i18n("Select fill color"));
127 rectFColor->setToolTip(i18n("Select border color"));
128 rectBAlpha->setToolTip(i18n("Fill transparency"));
129 rectFAlpha->setToolTip(i18n("Border transparency"));
130 zoom_slider->setToolTip(i18n("Zoom"));
131 buttonRealSize->setToolTip(i18n("Original size (1:1)"));
132 buttonFitZoom->setToolTip(i18n("Fit zoom"));
133 kcolorbutton->setToolTip(i18n("Select background color"));
134 horizontalSlider->setToolTip(i18n("Background Transparency"));
136 itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
137 itemhcenter->setToolTip(i18n("Align item horizontally"));
138 itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
139 itemvcenter->setToolTip(i18n("Align item vertically"));
141 QHBoxLayout *layout = new QHBoxLayout;
142 frame_toolbar->setLayout(layout);
143 layout->setContentsMargins(2, 2, 2, 2);
144 QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
146 m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), i18n("Add Rectangle (Alt+R)"));
147 m_buttonRect->setCheckable(true);
148 m_buttonRect->setShortcut(Qt::ALT + Qt::Key_R);
149 connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
151 m_buttonText = m_toolbar->addAction(KIcon("insert-text"), i18n("Add Text (Alt+T)"));
152 m_buttonText->setCheckable(true);
153 m_buttonText->setShortcut(Qt::ALT + Qt::Key_T);
154 connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
156 m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), i18n("Add Image (Alt+I)"));
157 m_buttonImage->setCheckable(false);
158 m_buttonImage->setShortcut(Qt::ALT + Qt::Key_I);
159 connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
161 m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), i18n("Selection Tool (Alt+S)"));
162 m_buttonCursor->setCheckable(true);
163 m_buttonCursor->setShortcut(Qt::ALT + Qt::Key_S);
164 connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
166 m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
167 m_buttonLoad->setCheckable(false);
168 m_buttonLoad->setShortcut(Qt::CTRL + Qt::Key_O);
169 connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
171 m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
172 m_buttonSave->setCheckable(false);
173 m_buttonSave->setShortcut(Qt::CTRL + Qt::Key_S);
174 connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
176 layout->addWidget(m_toolbar);
177 text_properties->setHidden(true);
179 // initialize graphic scene
180 m_scene = new GraphicsSceneRectMove(this);
181 graphicsView->setScene(m_scene);
182 m_titledocument.setScene(m_scene);
183 connect(m_scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(slotChanged()));
185 // a gradient background
186 /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
187 gradient->setSpread(QGradient::ReflectSpread);
188 scene->setBackgroundBrush(*gradient);*/
190 m_frameImage = new QGraphicsPixmapItem();
192 qtrans.scale(2.0, 2.0);
193 m_frameImage->setTransform(qtrans);
194 m_frameImage->setZValue(-1200);
195 m_frameImage->setFlags(QGraphicsItem::ItemClipsToShape);
196 displayBackgroundFrame();
197 graphicsView->scene()->addItem(m_frameImage);
199 connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
200 connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
201 connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
202 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
203 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
204 connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
205 connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
206 connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
208 QPen framepen(Qt::DotLine);
209 framepen.setColor(Qt::red);
211 m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
212 m_frameBorder->setPen(framepen);
213 m_frameBorder->setZValue(-1100);
214 m_frameBorder->setBrush(Qt::transparent);
215 m_frameBorder->setFlags(QGraphicsItem::ItemClipsToShape);
216 graphicsView->scene()->addItem(m_frameBorder);
218 // mbd: load saved settings
222 QTimer::singleShot(500, this, SLOT(slotAdjustZoom()));
223 graphicsView->show();
224 //graphicsView->setRenderHint(QPainter::Antialiasing);
225 graphicsView->setInteractive(true);
226 //graphicsView->resize(400, 300);
227 kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
228 toolBox->setItemEnabled(2, false);
229 if (!url.isEmpty()) {
230 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
237 TitleWidget::~TitleWidget()
241 delete m_buttonImage;
242 delete m_buttonCursor;
246 delete m_frameBorder;
248 delete m_startViewport;
249 delete m_endViewport;
254 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl)
257 QString titlePath = projectUrl.path() + "/titles/";
258 KStandardDirs::makeDir(titlePath);
259 QString titleName = "title";
261 QString path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
262 while (QFile::exists(path + ".png")) {
264 path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
266 result.append(titleName + QString::number(counter).rightJustified(3, '0', false));
267 result.append(path + ".png");
271 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
274 QString titlePath = projectUrl.path() + "/titles/";
275 KStandardDirs::makeDir(titlePath);
276 return titlePath + titleName + ".png";
280 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
285 void TitleWidget::slotTextTool()
287 rect_properties->setHidden(true);
288 text_properties->setHidden(false);
289 m_scene->setTool(TITLE_TEXT);
290 m_buttonRect->setChecked(false);
291 m_buttonCursor->setChecked(false);
294 void TitleWidget::slotRectTool()
296 text_properties->setHidden(true);
297 rect_properties->setHidden(false);
298 m_scene->setTool(TITLE_RECTANGLE);
299 m_buttonText->setChecked(false);
300 m_buttonCursor->setChecked(false);
301 m_buttonRect->setChecked(true);
304 void TitleWidget::slotSelectTool()
306 m_scene->setTool(TITLE_SELECT);
307 m_buttonCursor->setChecked(true);
308 m_buttonText->setChecked(false);
309 m_buttonRect->setChecked(false);
312 void TitleWidget::slotImageTool()
314 KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.svg *.png *.jpg *.jpeg *.gif *.raw", this, i18n("Load Image"));
315 if (!url.isEmpty()) {
316 if (url.path().endsWith(".svg")) {
317 QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
318 svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
319 svg->setZValue(m_count++);
320 svg->setData(Qt::UserRole, url.path());
321 graphicsView->scene()->addItem(svg);
323 QPixmap pix(url.path());
324 QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
325 image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
326 image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
327 image->setData(Qt::UserRole, url.path());
328 image->setZValue(m_count++);
329 graphicsView->scene()->addItem(image);
332 m_scene->setTool(TITLE_SELECT);
333 m_buttonRect->setChecked(false);
334 m_buttonCursor->setChecked(true);
335 m_buttonText->setChecked(false);
338 void TitleWidget::displayBackgroundFrame()
340 if (!displayBg->isChecked()) {
341 QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
342 QPixmap pattern(20, 20);
346 p.fillRect(QRect(0, 0, 10, 10), QColor(210, 210, 210));
347 p.fillRect(QRect(10, 10, 20, 20), QColor(210, 210, 210));
352 p.fillRect(bg.rect(), br);
354 m_frameImage->setPixmap(bg);
356 m_frameImage->setPixmap(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2));
360 void TitleWidget::initViewports()
362 m_startViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
363 m_endViewport = new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 0, 0)));
365 QPen startpen(Qt::DotLine);
366 QPen endpen(Qt::DashDotLine);
367 startpen.setColor(QColor(100, 200, 100, 140));
368 endpen.setColor(QColor(200, 100, 100, 140));
370 m_startViewport->setPen(startpen);
371 m_endViewport->setPen(endpen);
373 startViewportSize->setValue(40);
374 endViewportSize->setValue(40);
376 m_startViewport->setZValue(-1000);
377 m_endViewport->setZValue(-1000);
379 m_startViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
380 m_endViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
382 graphicsView->scene()->addItem(m_startViewport);
383 graphicsView->scene()->addItem(m_endViewport);
386 void TitleWidget::slotUpdateZoom(int pos)
388 m_scene->setZoom((double) pos / 100);
389 zoom_label->setText(QString::number(pos) + '%');
392 void TitleWidget::slotZoom(bool up)
394 int pos = zoom_slider->value();
397 zoom_slider->setValue(pos);
400 void TitleWidget::slotAdjustZoom()
402 /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
403 double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
404 if (scalex > scaley) scalex = scaley;
405 int zoompos = (int)(scalex * 7 + 0.5);*/
406 graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
407 int zoompos = graphicsView->matrix().m11() * 100;
408 zoom_slider->setValue(zoompos);
409 graphicsView->centerOn(m_frameBorder);
412 void TitleWidget::slotZoomOneToOne()
414 zoom_slider->setValue(100);
415 graphicsView->centerOn(m_frameBorder);
418 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
420 QColor f = rectFColor->color();
421 f.setAlpha(rectFAlpha->value());
423 penf.setWidth(rectLineWidth->value());
425 QColor b = rectBColor->color();
426 b.setAlpha(rectBAlpha->value());
427 rect->setBrush(QBrush(b));
428 rect->setZValue(m_count++);
429 //setCurrentItem(rect);
430 //graphicsView->setFocus();
433 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
435 QFont font = font_family->currentFont();
436 font.setPixelSize(font_size->value());
438 font.setBold(buttonBold->isChecked());
439 font.setItalic(buttonItalic->isChecked());
440 font.setUnderline(buttonUnder->isChecked());
443 QColor color = fontColorButton->color();
444 color.setAlpha(textAlpha->value());
445 tt->setDefaultTextColor(color);
446 tt->setZValue(m_count++);
450 void TitleWidget::setCurrentItem(QGraphicsItem *item)
452 m_scene->setSelectedItem(item);
455 void TitleWidget::zIndexChanged(int v)
457 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
463 void TitleWidget::selectionChanged()
465 if (m_scene->tool() != TITLE_SELECT) return;
466 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
467 //toolBox->setItemEnabled(2, false);
468 //toolBox->setItemEnabled(3, false);
469 value_x->blockSignals(true);
470 value_y->blockSignals(true);
471 value_w->blockSignals(true);
472 value_h->blockSignals(true);
473 itemzoom->blockSignals(true);
474 itemrotate->blockSignals(true);
476 bool blockX = !origin_x_left->signalsBlocked();
477 bool blockY = !origin_y_top->signalsBlocked();
478 if (blockX) origin_x_left->blockSignals(true);
479 if (blockY) origin_y_top->blockSignals(true);
480 origin_x_left->setChecked(false);
481 origin_y_top->setChecked(false);
484 frame_properties->setEnabled(false);
485 if (blockX) origin_x_left->blockSignals(false);
486 if (blockY) origin_y_top->blockSignals(false);
487 } else if (l.size() == 1) {
488 if (l.at(0)->type() == TEXTITEM) {
489 rect_properties->setHidden(true);
490 text_properties->setHidden(false);
491 QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
492 //if (l[0]->hasFocus())
493 toolBox->setCurrentIndex(0);
494 //toolBox->setItemEnabled(2, true);
495 font_size->blockSignals(true);
496 font_family->blockSignals(true);
497 buttonBold->blockSignals(true);
498 buttonItalic->blockSignals(true);
499 buttonUnder->blockSignals(true);
500 fontColorButton->blockSignals(true);
501 textAlpha->blockSignals(true);
502 buttonAlignLeft->blockSignals(true);
503 buttonAlignRight->blockSignals(true);
504 buttonAlignNone->blockSignals(true);
505 buttonAlignCenter->blockSignals(true);
507 QFont font = i->font();
508 font_family->setCurrentFont(font);
509 font_size->setValue(font.pixelSize());
510 buttonBold->setChecked(font.bold());
511 buttonItalic->setChecked(font.italic());
512 buttonUnder->setChecked(font.underline());
514 QColor color = i->defaultTextColor();
515 fontColorButton->setColor(color);
516 textAlpha->setValue(color.alpha());
518 QTextCursor cur = i->textCursor();
519 QTextBlockFormat format = cur.blockFormat();
520 if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
521 else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
522 else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
523 else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
525 font_size->blockSignals(false);
526 font_family->blockSignals(false);
527 buttonBold->blockSignals(false);
528 buttonItalic->blockSignals(false);
529 buttonUnder->blockSignals(false);
530 fontColorButton->blockSignals(false);
531 textAlpha->blockSignals(false);
532 buttonAlignLeft->blockSignals(false);
533 buttonAlignRight->blockSignals(false);
534 buttonAlignNone->blockSignals(false);
535 buttonAlignCenter->blockSignals(false);
537 updateAxisButtons(i);
538 updateCoordinates(i);
540 //value_w->setValue((int) i->boundingRect().width());
541 //value_h->setValue((int) i->boundingRect().height());
542 frame_properties->setEnabled(true);
543 value_w->setEnabled(false);
544 value_h->setEnabled(false);
546 } else if ((l.at(0))->type() == RECTITEM) {
547 rect_properties->setHidden(false);
548 text_properties->setHidden(true);
550 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
551 toolBox->setCurrentIndex(0);
552 //toolBox->setItemEnabled(3, true);
553 rectFAlpha->setValue(rec->pen().color().alpha());
554 rectBAlpha->setValue(rec->brush().color().alpha());
555 //kDebug() << rec->brush().color().alpha();
556 QColor fcol = rec->pen().color();
557 QColor bcol = rec->brush().color();
558 //fcol.setAlpha(255);
559 //bcol.setAlpha(255);
560 rectFColor->setColor(fcol);
561 rectBColor->setColor(bcol);
563 rectLineWidth->setValue(rec->pen().width());
565 updateAxisButtons(l.at(0));
566 updateCoordinates(rec);
567 updateDimension(rec);
568 //value_w->setValue((int) rec->rect().width());
569 //value_h->setValue((int) rec->rect().height());
570 frame_properties->setEnabled(true);
571 value_w->setEnabled(true);
572 value_h->setEnabled(true);
573 } else if (l.at(0)->type() == IMAGEITEM) {
576 updateCoordinates(l.at(0));
577 updateDimension(l.at(0));
579 frame_properties->setEnabled(true);
580 value_x->setEnabled(true);
581 value_w->setEnabled(false);
582 value_h->setEnabled(false);
584 //toolBox->setCurrentIndex(0);
585 frame_properties->setEnabled(false);
587 zValue->setValue((int)l.at(0)->zValue());
588 itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
589 itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
590 value_x->blockSignals(false);
591 value_y->blockSignals(false);
592 value_w->blockSignals(false);
593 value_h->blockSignals(false);
594 itemzoom->blockSignals(false);
595 itemrotate->blockSignals(false);
599 /** \brief Updates position/size of the selected item when a value
600 * of an item (coordinates, size) has changed */
601 void TitleWidget::slotAdjustSelectedItem()
603 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
605 if (l.at(0)->type() == RECTITEM) {
607 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
609 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
610 } else if (l.at(0)->type() == TEXTITEM) {
612 updatePosition(l.at(0));
613 } else if (l.at(0)->type() == IMAGEITEM) {
615 updatePosition(l.at(0));
620 /** \brief Updates width/height int the text fields, regarding transformation matrix */
621 void TitleWidget::updateDimension(QGraphicsItem *i)
623 bool blockW = !value_w->signalsBlocked();
624 bool blockH = !value_h->signalsBlocked();
626 if (blockW) value_w->blockSignals(true);
627 if (blockH) value_h->blockSignals(true);
630 if (i->type() == IMAGEITEM) {
631 // Get multipliers for rotation/scaling
633 /*Transform t = m_transformations.value(i);
634 QRectF r = i->boundingRect();
635 int width = (int) ( abs(r.width()*t.scalex * cos(t.rotate/180.0*M_PI))
636 + abs(r.height()*t.scaley * sin(t.rotate/180.0*M_PI)) );
637 int height = (int) ( abs(r.height()*t.scaley * cos(t.rotate/180*M_PI))
638 + abs(r.width()*t.scalex * sin(t.rotate/180*M_PI)) );*/
640 value_w->setValue(i->sceneBoundingRect().width());
641 value_h->setValue(i->sceneBoundingRect().height());
642 } else if (i->type() == RECTITEM) {
643 QGraphicsRectItem *r = static_cast <QGraphicsRectItem *> (i);
644 value_w->setValue((int) r->rect().width());
645 value_h->setValue((int) r->rect().height());
646 } else if (i->type() == TEXTITEM) {
647 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *> (i);
648 value_w->setValue((int) t->boundingRect().width());
649 value_h->setValue((int) t->boundingRect().height());
652 if (blockW) value_w->blockSignals(false);
653 if (blockH) value_h->blockSignals(false);
656 /** \brief Updates the coordinates in the text fields from the item */
657 void TitleWidget::updateCoordinates(QGraphicsItem *i)
660 bool blockX = !value_x->signalsBlocked();
661 bool blockY = !value_y->signalsBlocked();
663 // Block signals emitted by this method
664 if (blockX) value_x->blockSignals(true);
665 if (blockY) value_y->blockSignals(true);
667 if (i->type() == TEXTITEM) {
669 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *> (i);
671 // Set the correct x coordinate value
672 if (origin_x_left->isChecked()) {
673 // Origin (0 point) is at m_frameWidth, coordinate axis is inverted
674 value_x->setValue((int) (m_frameWidth - rec->pos().x() - rec->boundingRect().width()));
676 // Origin is at 0 (default)
677 value_x->setValue((int) rec->pos().x());
681 if (origin_y_top->isChecked()) {
682 value_y->setValue((int) (m_frameHeight - rec->pos().y() - rec->boundingRect().height()));
684 value_y->setValue((int) rec->pos().y());
687 } else if (i->type() == RECTITEM) {
689 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *> (i);
691 if (origin_x_left->isChecked()) {
692 // Origin (0 point) is at m_frameWidth
693 value_x->setValue((int) (m_frameWidth - rec->pos().x() - rec->rect().width()));
695 // Origin is at 0 (default)
696 value_x->setValue((int) rec->pos().x());
699 if (origin_y_top->isChecked()) {
700 value_y->setValue((int) (m_frameHeight - rec->pos().y() - rec->rect().height()));
702 value_y->setValue((int) rec->pos().y());
705 } else if (i->type() == IMAGEITEM) {
707 if (origin_x_left->isChecked()) {
708 value_x->setValue((int) (m_frameWidth - i->pos().x() - i->sceneBoundingRect().width()));
710 value_x->setValue((int) i->pos().x());
713 if (origin_y_top->isChecked()) {
714 value_y->setValue((int) (m_frameHeight - i->pos().y() - i->sceneBoundingRect().height()));
716 value_y->setValue((int) i->pos().y());
721 // Stop blocking signals now
722 if (!blockX) value_x->blockSignals(false);
723 if (!blockY) value_y->blockSignals(false);
726 /** \brief Updates the position of an item by reading coordinates from the text fields */
727 void TitleWidget::updatePosition(QGraphicsItem *i) {
729 if (i->type() == TEXTITEM) {
730 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
733 if (origin_x_left->isChecked()) {
734 /* Origin of the x axis is at m_frameWidth,
735 * and distance from right border of the item to the right
736 * border of the frame is taken.
737 * See comment to slotOriginXClicked().
739 posX = m_frameWidth - value_x->value() - rec->boundingRect().width();
741 posX = value_x->value();
745 if (origin_y_top->isChecked()) {
746 /* Same for y axis */
747 posY = m_frameHeight - value_y->value() - rec->boundingRect().height();
749 posY = value_y->value();
752 rec->setPos(posX, posY);
754 } else if (i->type() == RECTITEM) {
756 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *> (i);
759 if (origin_x_left->isChecked()) {
760 posX = m_frameWidth - value_x->value() - rec->rect().width();
762 posX = value_x->value();
766 if (origin_y_top->isChecked()) {
767 posY = m_frameHeight - value_y->value() - rec->rect().height();
769 posY = value_y->value();
772 rec->setPos(posX, posY);
774 } else if (i->type() == IMAGEITEM) {
777 if (origin_x_left->isChecked()) {
778 // Use the sceneBoundingRect because this also regards transformations like zoom
779 posX = m_frameWidth - value_x->value() - i->sceneBoundingRect().width();
781 posX = value_x->value();
785 if (origin_y_top->isChecked()) {
786 posY = m_frameHeight - value_y->value() - i->sceneBoundingRect().height();
788 posY = value_y->value();
791 i->setPos(posX, posY);
797 void TitleWidget::updateTextOriginX()
799 if (origin_x_left->isChecked()) {
800 origin_x_left->setText(i18n("\u2212X"));
802 origin_x_left->setText(i18n("+X"));
806 void TitleWidget::slotOriginXClicked()
808 // Update the text displayed on the button.
811 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
813 updateCoordinates(l.at(0));
815 // Remember x axis setting
816 l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked()?
817 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
821 void TitleWidget::updateTextOriginY()
823 if (origin_y_top->isChecked()) {
824 origin_y_top->setText(i18n("\u2212Y"));
826 origin_y_top->setText(i18n("+Y"));
830 void TitleWidget::slotOriginYClicked()
832 // Update the text displayed on the button.
835 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
837 updateCoordinates(l.at(0));
839 l.at(0)->setData(TitleDocument::OriginYTop, origin_y_top->isChecked()?
840 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
845 void TitleWidget::updateAxisButtons(QGraphicsItem *i)
847 int xAxis = i->data(TitleDocument::OriginXLeft).toInt();
848 int yAxis = i->data(TitleDocument::OriginYTop).toInt();
849 origin_x_left->blockSignals(true);
850 origin_y_top->blockSignals(true);
852 if (xAxis == TitleDocument::AxisInverted) {
853 origin_x_left->setChecked(true);
855 origin_x_left->setChecked(false);
859 if (yAxis == TitleDocument::AxisInverted) {
860 origin_y_top->setChecked(true);
862 origin_y_top->setChecked(false);
866 origin_x_left->blockSignals(false);
867 origin_y_top->blockSignals(false);
870 void TitleWidget::slotChangeBackground()
872 QColor color = kcolorbutton->color();
873 color.setAlpha(horizontalSlider->value());
874 m_frameBorder->setBrush(QBrush(color));
878 * Something (yeah) has changed in our QGraphicsScene.
880 void TitleWidget::slotChanged() {
881 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
882 if (l.size() >= 1 && l.at(0)->type() == TEXTITEM) {
883 textChanged(static_cast <QGraphicsTextItem *> (l.at(0)));
888 * If the user has set origin_x_left (everything also for y),
889 * we need to look whether a text element has been selected. If yes,
890 * we need to ensure that the right border of the text field
891 * remains fixed also when some text has been entered.
893 * This is also known as right-justified, with the difference that
894 * it is not valid for text but for its boundingRect. Text may still
897 void TitleWidget::textChanged(QGraphicsTextItem *i) {
901 if (origin_x_left->isChecked() || origin_y_top->isChecked()) {
903 if (!i->toPlainText().isEmpty()) {
907 * Don't do anything if the string is empty. If the position
908 * would be updated here, a newly created text field would
909 * be set to the position of the last selected text field.
915 void TitleWidget::slotUpdateText()
917 QFont font = font_family->currentFont();
918 font.setPixelSize(font_size->value());
919 font.setBold(buttonBold->isChecked());
920 font.setItalic(buttonItalic->isChecked());
921 font.setUnderline(buttonUnder->isChecked());
922 QColor color = fontColorButton->color();
923 color.setAlpha(textAlpha->value());
925 QGraphicsTextItem* item = NULL;
926 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
927 if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
928 item = static_cast <QGraphicsTextItem *>(l.at(0));
931 //if (item->textCursor().selection ().isEmpty())
932 QTextCursor cur = item->textCursor();
933 QTextBlockFormat format = cur.blockFormat();
934 if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
935 item->setTextWidth(item->boundingRect().width());
936 if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
937 else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
938 else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
940 format.setAlignment(Qt::AlignLeft);
941 item->setTextWidth(-1);
946 item->setDefaultTextColor(color);
947 cur.select(QTextCursor::Document);
948 cur.setBlockFormat(format);
949 item->setTextCursor(cur);
950 cur.clearSelection();
951 item->setTextCursor(cur);
956 void TitleWidget::rectChanged()
958 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
959 if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
960 QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
961 QColor f = rectFColor->color();
962 f.setAlpha(rectFAlpha->value());
964 penf.setWidth(rectLineWidth->value());
966 QColor b = rectBColor->color();
967 b.setAlpha(rectBAlpha->value());
968 rec->setBrush(QBrush(b));
972 void TitleWidget::itemScaled(int val)
974 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
976 Transform x = m_transformations.value(l.at(0));
977 x.scalex = (double)val / 100.0;
978 x.scaley = (double)val / 100.0;
980 qtrans.scale(x.scalex, x.scaley);
981 qtrans.rotate(x.rotate);
982 l[0]->setTransform(qtrans);
983 m_transformations[l.at(0)] = x;
984 updateDimension(l.at(0));
988 void TitleWidget::itemRotate(int val)
990 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
992 Transform x = m_transformations[l.at(0)];
993 x.rotate = (double)val;
995 qtrans.scale(x.scalex, x.scaley);
996 qtrans.rotate(x.rotate);
997 l[0]->setTransform(qtrans);
998 m_transformations[l.at(0)] = x;
999 updateDimension(l.at(0));
1003 void TitleWidget::itemHCenter()
1005 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1006 if (l.size() == 1) {
1007 QGraphicsItem *item = l.at(0);
1009 if (item->type() == RECTITEM) {
1010 br = ((QGraphicsRectItem*)item)->rect();
1011 } else br = item->sceneBoundingRect();
1012 int width = (int) br.width();
1013 int newPos = (int)((m_frameWidth - width) / 2);
1014 item->setPos(newPos, item->pos().y());
1015 updateCoordinates(item);
1019 void TitleWidget::itemVCenter()
1021 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1022 if (l.size() == 1) {
1023 QGraphicsItem *item = l.at(0);
1025 if (item->type() == RECTITEM) {
1026 br = ((QGraphicsRectItem*)item)->rect();
1027 } else br = item->sceneBoundingRect();
1028 int height = (int) br.height();
1029 int newPos = (int)((m_frameHeight - height) / 2);
1030 item->setPos(item->pos().x(), newPos);
1031 updateCoordinates(item);
1035 void TitleWidget::setupViewports()
1037 double aspect_ratio = 4.0 / 3.0;//read from project
1039 QRectF sp(0, 0, 0, 0);
1040 QRectF ep(0, 0, 0, 0);
1042 double sv_size = startViewportSize->value();
1043 double ev_size = endViewportSize->value();
1044 sp.adjust(-sv_size, -sv_size / aspect_ratio, sv_size, sv_size / aspect_ratio);
1045 ep.adjust(-ev_size, -ev_size / aspect_ratio, ev_size, ev_size / aspect_ratio);
1047 m_startViewport->setPos(startViewportX->value(), startViewportY->value());
1048 m_endViewport->setPos(endViewportX->value(), endViewportY->value());
1050 m_startViewport->setPolygon(QPolygonF(sp));
1051 m_endViewport->setPolygon(QPolygonF(ep));
1055 void TitleWidget::loadTitle()
1057 KUrl url = KFileDialog::getOpenUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Load Title"));
1058 if (!url.isEmpty()) {
1059 QList<QGraphicsItem *> items = m_scene->items();
1060 for (int i = 0; i < items.size(); i++) {
1061 if (items.at(i)->zValue() > -1000) delete items.at(i);
1063 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport) + 1;
1068 void TitleWidget::saveTitle(KUrl url)
1070 if (url.isEmpty()) url = KFileDialog::getSaveUrl(KUrl(m_projectPath), "*.kdenlivetitle", this, i18n("Save Title"));
1071 if (!url.isEmpty()) {
1072 if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport) == false)
1073 KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
1077 QDomDocument TitleWidget::xml()
1079 QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
1080 if (cropImage->isChecked()) {
1081 doc.documentElement().setAttribute("crop", 1);
1086 void TitleWidget::setXml(QDomDocument doc)
1088 m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport);
1089 QDomElement e = doc.documentElement();
1090 cropImage->setChecked(e.hasAttribute("crop"));
1091 m_transformations.clear();
1092 QList <QGraphicsItem *> items = graphicsView->scene()->items();
1093 const double PI = 4.0 * atan(1.0);
1094 for (int i = 0; i < items.count(); i++) {
1095 QTransform t = items.at(i)->transform();
1099 x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
1100 m_transformations[items.at(i)] = x;
1102 // mbd: Update the GUI color selectors to match the stuff from the loaded document
1103 QColor background_color = m_titledocument.getBackgroundColor();
1104 horizontalSlider->blockSignals(true);
1105 kcolorbutton->blockSignals(true);
1106 horizontalSlider->setValue(background_color.alpha());
1107 background_color.setAlpha(255);
1108 kcolorbutton->setColor(background_color);
1109 horizontalSlider->blockSignals(false);
1110 kcolorbutton->blockSignals(false);
1115 QImage TitleWidget::renderedPixmap()
1119 int maxX = m_frameWidth;
1120 int maxY = m_frameHeight;
1121 if (!cropImage->isChecked()) {
1122 m_scene->removeItem(m_startViewport);
1123 m_scene->removeItem(m_endViewport);
1124 QRect boundingRect = m_scene->itemsBoundingRect().toRect();
1125 if (boundingRect.left() < 0) minX = boundingRect.left();
1126 if (boundingRect.top() < 0) minY = boundingRect.top();
1127 if (boundingRect.right() > maxX) maxX = boundingRect.right();
1128 if (boundingRect.bottom() > maxY) maxY = boundingRect.bottom();
1137 QImage pix(maxX, maxY, QImage::Format_ARGB32);
1138 pix.fill(Qt::transparent);
1139 QPainter painter(&pix);
1140 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
1141 m_scene->clearTextSelection();
1142 QPen framepen = m_frameBorder->pen();
1143 m_frameBorder->setPen(Qt::NoPen);
1144 m_startViewport->setVisible(false);
1145 m_endViewport->setVisible(false);
1146 m_frameImage->setVisible(false);
1148 m_scene->render(&painter, QRectF(), QRectF(minX, minY, maxX - minX, maxY - minY));
1150 m_frameBorder->setPen(framepen);
1151 m_startViewport->setVisible(true);
1152 m_endViewport->setVisible(true);
1153 m_frameImage->setVisible(true);
1157 /** \brief Connected to the accepted signal - calls writeChoices */
1158 void TitleWidget::slotAccepted()
1163 /** \brief Store the current choices of font, background and rect values */
1164 void TitleWidget::writeChoices()
1166 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1167 KSharedConfigPtr config = KGlobal::config();
1168 KConfigGroup titleConfig(config, "TitleWidget");
1169 // Write the entries
1170 titleConfig.writeEntry("font_family", font_family->currentFont());
1171 //titleConfig.writeEntry("font_size", font_size->value());
1172 titleConfig.writeEntry("font_pixel_size", font_size->value());
1173 titleConfig.writeEntry("font_color", fontColorButton->color());
1174 titleConfig.writeEntry("font_alpha", textAlpha->value());
1175 titleConfig.writeEntry("font_bold", buttonBold->isChecked());
1176 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
1177 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
1179 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
1180 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
1181 titleConfig.writeEntry("rect_background_color", rectBColor->color());
1182 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
1183 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
1185 titleConfig.writeEntry("background_color", kcolorbutton->color());
1186 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
1188 titleConfig.writeEntry("crop_image", cropImage->isChecked());
1189 //! \todo Not sure if I should sync - it is probably safe to do it
1194 /** \brief Read the last stored choices into the dialog */
1195 void TitleWidget::readChoices()
1197 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1198 KSharedConfigPtr config = KGlobal::config();
1199 KConfigGroup titleConfig(config, "TitleWidget");
1201 font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
1202 font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
1203 fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
1204 textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
1205 buttonBold->setChecked(titleConfig.readEntry("font_bold", buttonBold->isChecked()));
1206 buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
1207 buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
1209 rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
1210 rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
1211 rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
1212 rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
1213 rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
1215 kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
1216 horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
1218 cropImage->setChecked(titleConfig.readEntry("crop_image", cropImage->isChecked()));