1 /***************************************************************************
2 titlewidget.cpp - 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>
28 #include <kio/netaccess.h>
30 #include <QDomDocument>
31 #include <QGraphicsItem>
32 #include <QGraphicsSvgItem>
36 #include <QSignalMapper>
37 #include <QTextBlockFormat>
38 #include <QTextCursor>
40 int settingUp = false;
42 const int IMAGEITEM = 7;
43 const int RECTITEM = 3;
44 const int TEXTITEM = 8;
45 static bool insertingValues = false;
47 TitleWidget::TitleWidget(KUrl url, Timecode tc, QString projectTitlePath, Render *render, QWidget *parent) :
50 m_startViewport(NULL),
54 m_unicodeDialog(new UnicodeDialog(UnicodeDialog::InputHex)),
55 m_projectTitlePath(projectTitlePath),
59 setFont(KGlobalSettings::toolBarFont());
60 //toolBox->setFont(KGlobalSettings::toolBarFont());
61 frame_properties->setEnabled(false);
62 rect_properties->setFixedHeight(frame_properties->height() + 4);
63 no_properties->setFixedHeight(frame_properties->height() + 4);
64 image_properties->setFixedHeight(frame_properties->height() + 4);
65 text_properties->setFixedHeight(frame_properties->height() + 4);
66 frame_properties->setFixedHeight(frame_toolbar->height());
67 m_frameWidth = render->renderWidth();
68 m_frameHeight = render->renderHeight();
69 showToolbars(TITLE_NONE);
71 //TODO: get default title duration instead of hardcoded one
72 title_duration->setText(m_tc.getTimecode(GenTime(5000 / 1000.0)));
74 connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
75 connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
77 connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
78 connect(font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(slotUpdateText())) ;
79 connect(font_size, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText())) ;
80 connect(textAlpha, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText()));
81 connect(font_weight_box, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateText()));
83 connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
84 connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
85 connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
86 connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
87 connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
89 /*connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
90 connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
91 connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
92 connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
93 connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
94 connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));*/
96 connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
97 connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
98 connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
99 connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
100 connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
102 connect(origin_x_left, SIGNAL(clicked()), this, SLOT(slotOriginXClicked()));
103 connect(origin_y_top, SIGNAL(clicked()), this, SLOT(slotOriginYClicked()));
105 m_signalMapper = new QSignalMapper(this);
106 m_signalMapper->setMapping(value_w, ValueWidth);
107 m_signalMapper->setMapping(value_h, ValueHeight);
108 connect(value_w, SIGNAL(valueChanged(int)), m_signalMapper, SLOT(map()));
109 connect(value_h, SIGNAL(valueChanged(int)), m_signalMapper, SLOT(map()));
110 connect(m_signalMapper, SIGNAL(mapped(int)), this, SLOT(slotValueChanged(int)));
112 connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
113 connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
114 connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
115 connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
116 connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
117 connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
118 connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
119 connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
120 connect(buttonAlignLeft, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
121 connect(buttonAlignRight, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
122 connect(buttonAlignCenter, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
123 connect(buttonAlignNone, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
124 //connect(buttonInsertUnicode, SIGNAL(clicked()), this, SLOT(slotInsertUnicode()));
125 connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
127 connect(m_unicodeDialog, SIGNAL(charSelected(QString)), this, SLOT(slotInsertUnicodeString(QString)));
130 connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
132 font_weight_box->blockSignals(true);
133 font_weight_box->addItem(i18nc("Font style", "Light"), QFont::Light);
134 font_weight_box->addItem(i18nc("Font style", "Normal"), QFont::Normal);
135 font_weight_box->addItem(i18nc("Font style", "Demi-Bold"), QFont::DemiBold);
136 font_weight_box->addItem(i18nc("Font style", "Bold"), QFont::Bold);
137 font_weight_box->addItem(i18nc("Font style", "Black"), QFont::Black);
138 font_weight_box->setToolTip(i18n("Font weight"));
139 font_weight_box->setCurrentIndex(1);
140 font_weight_box->blockSignals(false);
142 buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
143 buttonRealSize->setIcon(KIcon("zoom-original"));
144 buttonItalic->setIcon(KIcon("format-text-italic"));
145 buttonUnder->setIcon(KIcon("format-text-underline"));
146 buttonAlignCenter->setIcon(KIcon("format-justify-center"));
147 buttonAlignLeft->setIcon(KIcon("format-justify-left"));
148 buttonAlignRight->setIcon(KIcon("format-justify-right"));
149 buttonAlignNone->setIcon(KIcon("kdenlive-align-none"));
151 buttonAlignNone->setToolTip(i18n("No alignment"));
152 buttonAlignRight->setToolTip(i18n("Align right"));
153 buttonAlignLeft->setToolTip(i18n("Align left"));
154 buttonAlignCenter->setToolTip(i18n("Align center"));
156 m_unicodeAction = new QAction(KIcon("kdenlive-insert-unicode"), QString(), this);
157 m_unicodeAction->setShortcut(Qt::SHIFT + Qt::CTRL + Qt::Key_U);
158 m_unicodeAction->setToolTip(i18n("Insert Unicode character") + ' ' + m_unicodeAction->shortcut().toString());
159 connect(m_unicodeAction, SIGNAL(triggered()), this, SLOT(slotInsertUnicode()));
160 buttonInsertUnicode->setDefaultAction(m_unicodeAction);
162 origin_x_left->setToolTip(i18n("Invert x axis and change 0 point"));
163 origin_y_top->setToolTip(i18n("Invert y axis and change 0 point"));
164 rectBColor->setToolTip(i18n("Select fill color"));
165 rectFColor->setToolTip(i18n("Select border color"));
166 rectBAlpha->setToolTip(i18n("Fill transparency"));
167 rectFAlpha->setToolTip(i18n("Border transparency"));
168 zoom_slider->setToolTip(i18n("Zoom"));
169 buttonRealSize->setToolTip(i18n("Original size (1:1)"));
170 buttonFitZoom->setToolTip(i18n("Fit zoom"));
171 kcolorbutton->setToolTip(i18n("Select background color"));
172 horizontalSlider->setToolTip(i18n("Background Transparency"));
174 itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
175 itemhcenter->setToolTip(i18n("Align item horizontally"));
176 itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
177 itemvcenter->setToolTip(i18n("Align item vertically"));
179 QHBoxLayout *layout = new QHBoxLayout;
180 frame_toolbar->setLayout(layout);
181 layout->setContentsMargins(2, 2, 2, 2);
182 QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
184 m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), QString());
185 m_buttonCursor->setCheckable(true);
186 m_buttonCursor->setShortcut(Qt::ALT + Qt::Key_S);
187 m_buttonCursor->setToolTip(i18n("Selection Tool") + ' ' + m_buttonCursor->shortcut().toString());
188 connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
190 m_buttonText = m_toolbar->addAction(KIcon("insert-text"), QString());
191 m_buttonText->setCheckable(true);
192 m_buttonText->setShortcut(Qt::ALT + Qt::Key_T);
193 m_buttonText->setToolTip(i18n("Add Text") + ' ' + m_buttonText->shortcut().toString());
194 connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
196 m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), QString());
197 m_buttonRect->setCheckable(true);
198 m_buttonRect->setShortcut(Qt::ALT + Qt::Key_R);
199 m_buttonRect->setToolTip(i18n("Add Rectangle") + ' ' + m_buttonRect->shortcut().toString());
200 connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
202 m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), QString());
203 m_buttonImage->setCheckable(false);
204 m_buttonImage->setShortcut(Qt::ALT + Qt::Key_I);
205 m_buttonImage->setToolTip(i18n("Add Image") + ' ' + m_buttonImage->shortcut().toString());
206 connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
208 m_toolbar->addSeparator();
210 m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
211 m_buttonLoad->setCheckable(false);
212 m_buttonLoad->setShortcut(Qt::CTRL + Qt::Key_O);
213 connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
215 m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
216 m_buttonSave->setCheckable(false);
217 m_buttonSave->setShortcut(Qt::CTRL + Qt::Key_S);
218 connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
220 layout->addWidget(m_toolbar);
222 // initialize graphic scene
223 m_scene = new GraphicsSceneRectMove(this);
224 graphicsView->setScene(m_scene);
225 m_titledocument.setScene(m_scene, m_frameWidth, m_frameHeight);
226 connect(m_scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(slotChanged()));
228 // a gradient background
229 /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
230 gradient->setSpread(QGradient::ReflectSpread);
231 scene->setBackgroundBrush(*gradient);*/
233 m_frameImage = new QGraphicsPixmapItem();
235 qtrans.scale(2.0, 2.0);
236 m_frameImage->setTransform(qtrans);
237 m_frameImage->setZValue(-1200);
238 m_frameImage->setFlags(QGraphicsItem::ItemClipsToShape);
239 displayBackgroundFrame();
240 graphicsView->scene()->addItem(m_frameImage);
242 connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
243 connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
244 connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
245 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
246 //connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
247 connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
248 connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
249 connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
251 QPen framepen(Qt::DotLine);
252 framepen.setColor(Qt::red);
254 m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
255 m_frameBorder->setPen(framepen);
256 m_frameBorder->setZValue(-1100);
257 m_frameBorder->setBrush(Qt::transparent);
258 m_frameBorder->setFlags(QGraphicsItem::ItemClipsToShape);
259 graphicsView->scene()->addItem(m_frameBorder);
261 // mbd: load saved settings
264 graphicsView->show();
265 //graphicsView->setRenderHint(QPainter::Antialiasing);
266 graphicsView->setInteractive(true);
267 //graphicsView->resize(400, 300);
268 kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
269 //toolBox->setItemEnabled(2, false);
270 m_startViewport = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
271 m_endViewport = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
272 if (!url.isEmpty()) loadTitle(url);
275 QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
278 connect(anim_start, SIGNAL(toggled(bool)), this, SLOT(slotAnimStart(bool)));
279 connect(anim_end, SIGNAL(toggled(bool)), this, SLOT(slotAnimEnd(bool)));
282 TitleWidget::~TitleWidget()
286 delete m_buttonImage;
287 delete m_buttonCursor;
290 delete m_unicodeAction;
292 delete m_unicodeDialog;
293 delete m_frameBorder;
295 delete m_startViewport;
296 delete m_endViewport;
298 delete m_signalMapper;
302 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl, bool isClone)
305 QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
306 KStandardDirs::makeDir(titlePath);
307 titlePath.append((isClone == false) ? "title" : "clone");
310 while (path.isEmpty() || QFile::exists(path)) {
312 path = titlePath + QString::number(counter).rightJustified(3, '0', false) + ".png";
314 result.append(((isClone == false) ? i18n("Title") : i18n("Clone")) + ' ' + QString::number(counter).rightJustified(3, '0', false));
319 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
322 QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
323 KStandardDirs::makeDir(titlePath);
324 return titlePath + titleName + ".png";
328 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
333 void TitleWidget::slotTextTool()
335 m_scene->setTool(TITLE_TEXT);
336 showToolbars(TITLE_TEXT);
337 checkButton(TITLE_TEXT);
340 void TitleWidget::slotRectTool()
342 m_scene->setTool(TITLE_RECTANGLE);
343 showToolbars(TITLE_RECTANGLE);
344 checkButton(TITLE_RECTANGLE);
347 void TitleWidget::slotSelectTool()
349 m_scene->setTool(TITLE_SELECT);
351 // Find out which toolbars need to be shown, depending on selected item
352 TITLETOOL t = TITLE_SELECT;
353 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
355 switch (l.at(0)->type()) {
369 if (t == TITLE_RECTANGLE && (l.at(0) == m_endViewport || l.at(0) == m_startViewport)) {
370 //graphicsView->centerOn(l.at(0));
376 updateCoordinates(l.at(0));
377 updateDimension(l.at(0));
378 updateRotZoom(l.at(0));
381 checkButton(TITLE_SELECT);
384 void TitleWidget::slotImageTool()
386 KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.svg *.png *.jpg *.jpeg *.gif *.raw", this, i18n("Load Image"));
387 if (!url.isEmpty()) {
388 if (url.path().endsWith(".svg")) {
389 QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
390 svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
391 svg->setZValue(m_count++);
392 svg->setData(Qt::UserRole, url.path());
393 graphicsView->scene()->addItem(svg);
395 QPixmap pix(url.path());
396 QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
397 image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
398 image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
399 image->setData(Qt::UserRole, url.path());
400 image->setZValue(m_count++);
401 graphicsView->scene()->addItem(image);
404 m_scene->setTool(TITLE_SELECT);
405 showToolbars(TITLE_SELECT);
406 checkButton(TITLE_NONE);
409 void TitleWidget::showToolbars(TITLETOOL toolType)
413 rect_properties->setHidden(true);
414 image_properties->setHidden(true);
415 no_properties->setHidden(true);
416 text_properties->setHidden(false);
418 case TITLE_RECTANGLE:
419 image_properties->setHidden(true);
420 no_properties->setHidden(true);
421 text_properties->setHidden(true);
422 rect_properties->setHidden(false);
425 no_properties->setHidden(true);
426 text_properties->setHidden(true);
427 rect_properties->setHidden(true);
428 image_properties->setHidden(false);
431 text_properties->setHidden(true);
432 rect_properties->setHidden(true);
433 image_properties->setHidden(true);
434 no_properties->setHidden(false);
439 void TitleWidget::enableToolbars(TITLETOOL toolType)
441 // TITLETOOL is defined in graphicsscenerectmove.h
446 bool bValue_w = false;
447 bool bValue_h = false;
456 case TITLE_RECTANGLE:
471 frame_properties->setEnabled(bFrame);
472 text_properties->setEnabled(bText);
473 rect_properties->setEnabled(bRect);
474 image_properties->setEnabled(bImage);
475 value_w->setEnabled(bValue_w);
476 value_h->setEnabled(bValue_h);
479 void TitleWidget::checkButton(TITLETOOL toolType)
481 bool bSelect = false;
493 case TITLE_RECTANGLE:
503 m_buttonCursor->setChecked(bSelect);
504 m_buttonText->setChecked(bText);
505 m_buttonRect->setChecked(bRect);
506 m_buttonImage->setChecked(bImage);
509 void TitleWidget::displayBackgroundFrame()
511 if (!displayBg->isChecked()) {
512 QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
513 QPixmap pattern(20, 20);
515 QColor bgcolor(210, 210, 210);
518 p.fillRect(QRect(0, 0, 10, 10), bgcolor);
519 p.fillRect(QRect(10, 10, 20, 20), bgcolor);
524 p.fillRect(bg.rect(), br);
526 m_frameImage->setPixmap(bg);
528 m_frameImage->setPixmap(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2));
532 void TitleWidget::initAnimation()
534 align_box->setEnabled(false);
535 align_box->setVisible(false);
536 QPen startpen(Qt::DotLine);
537 QPen endpen(Qt::DashDotLine);
538 startpen.setColor(QColor(100, 200, 100, 140));
539 endpen.setColor(QColor(200, 100, 100, 140));
541 m_startViewport->setPen(startpen);
542 m_endViewport->setPen(endpen);
544 m_startViewport->setZValue(-1000);
545 m_endViewport->setZValue(-1000);
547 m_startViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
548 m_endViewport->setFlags(/*QGraphicsItem::ItemIsMovable|*/QGraphicsItem::ItemIsSelectable);
550 graphicsView->scene()->addItem(m_startViewport);
551 graphicsView->scene()->addItem(m_endViewport);
554 void TitleWidget::slotUpdateZoom(int pos)
556 m_scene->setZoom((double) pos / 100);
557 zoom_label->setText(QString::number(pos) + '%');
560 void TitleWidget::slotZoom(bool up)
562 int pos = zoom_slider->value();
565 zoom_slider->setValue(pos);
568 void TitleWidget::slotAdjustZoom()
570 /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
571 double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
572 if (scalex > scaley) scalex = scaley;
573 int zoompos = (int)(scalex * 7 + 0.5);*/
574 graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
575 int zoompos = graphicsView->matrix().m11() * 100;
576 zoom_slider->setValue(zoompos);
577 graphicsView->centerOn(m_frameBorder);
580 void TitleWidget::slotZoomOneToOne()
582 zoom_slider->setValue(100);
583 graphicsView->centerOn(m_frameBorder);
586 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
588 updateAxisButtons(rect); // back to default
590 QColor f = rectFColor->color();
591 f.setAlpha(rectFAlpha->value());
593 penf.setWidth(rectLineWidth->value());
595 QColor b = rectBColor->color();
596 b.setAlpha(rectBAlpha->value());
597 rect->setBrush(QBrush(b));
598 rect->setZValue(m_count++);
599 //setCurrentItem(rect);
600 //graphicsView->setFocus();
603 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
605 updateAxisButtons(tt); // back to default
607 QFont font = font_family->currentFont();
608 font.setPixelSize(font_size->value());
610 font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
611 font.setItalic(buttonItalic->isChecked());
612 font.setUnderline(buttonUnder->isChecked());
615 QColor color = fontColorButton->color();
616 color.setAlpha(textAlpha->value());
617 tt->setDefaultTextColor(color);
618 tt->setZValue(m_count++);
622 void TitleWidget::setFontBoxWeight(int weight)
624 int index = font_weight_box->findData(weight);
626 index = font_weight_box->findData(QFont::Normal);
628 font_weight_box->setCurrentIndex(index);
631 void TitleWidget::setCurrentItem(QGraphicsItem *item)
633 m_scene->setSelectedItem(item);
636 void TitleWidget::zIndexChanged(int v)
638 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
644 void TitleWidget::selectionChanged()
646 if (m_scene->tool() != TITLE_SELECT) return;
647 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
648 //toolBox->setItemEnabled(2, false);
649 //toolBox->setItemEnabled(3, false);
650 value_x->blockSignals(true);
651 value_y->blockSignals(true);
652 value_w->blockSignals(true);
653 value_h->blockSignals(true);
654 itemzoom->blockSignals(true);
655 itemrotate->blockSignals(true);
657 bool blockX = !origin_x_left->signalsBlocked();
658 bool blockY = !origin_y_top->signalsBlocked();
659 if (blockX) origin_x_left->blockSignals(true);
660 if (blockY) origin_y_top->blockSignals(true);
661 origin_x_left->setChecked(false);
662 origin_y_top->setChecked(false);
665 enableToolbars(TITLE_NONE);
666 if (blockX) origin_x_left->blockSignals(false);
667 if (blockY) origin_y_top->blockSignals(false);
668 } else if (l.size() == 1) {
669 if (l.at(0)->type() == TEXTITEM) {
670 showToolbars(TITLE_TEXT);
671 QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
672 //if (l[0]->hasFocus())
673 toolBox->setCurrentIndex(0);
674 //toolBox->setItemEnabled(2, true);
675 font_size->blockSignals(true);
676 font_family->blockSignals(true);
677 font_weight_box->blockSignals(true);
678 buttonItalic->blockSignals(true);
679 buttonUnder->blockSignals(true);
680 fontColorButton->blockSignals(true);
681 textAlpha->blockSignals(true);
682 buttonAlignLeft->blockSignals(true);
683 buttonAlignRight->blockSignals(true);
684 buttonAlignNone->blockSignals(true);
685 buttonAlignCenter->blockSignals(true);
687 QFont font = i->font();
688 font_family->setCurrentFont(font);
689 font_size->setValue(font.pixelSize());
690 buttonItalic->setChecked(font.italic());
691 buttonUnder->setChecked(font.underline());
692 setFontBoxWeight(font.weight());
694 QColor color = i->defaultTextColor();
695 fontColorButton->setColor(color);
696 textAlpha->setValue(color.alpha());
698 QTextCursor cur = i->textCursor();
699 QTextBlockFormat format = cur.blockFormat();
700 if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
701 else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
702 else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
703 else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
705 font_size->blockSignals(false);
706 font_family->blockSignals(false);
707 font_weight_box->blockSignals(false);
708 buttonItalic->blockSignals(false);
709 buttonUnder->blockSignals(false);
710 fontColorButton->blockSignals(false);
711 textAlpha->blockSignals(false);
712 buttonAlignLeft->blockSignals(false);
713 buttonAlignRight->blockSignals(false);
714 buttonAlignNone->blockSignals(false);
715 buttonAlignCenter->blockSignals(false);
717 updateAxisButtons(i);
718 updateCoordinates(i);
720 enableToolbars(TITLE_TEXT);
722 } else if ((l.at(0))->type() == RECTITEM) {
723 showToolbars(TITLE_RECTANGLE);
725 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
726 if (rec == m_startViewport || rec == m_endViewport) {
727 toolBox->setCurrentIndex(3);
728 toolBox->widget(0)->setEnabled(false);
729 toolBox->widget(1)->setEnabled(false);
730 enableToolbars(TITLE_NONE);
733 toolBox->widget(0)->setEnabled(true);
734 toolBox->widget(1)->setEnabled(true);
735 toolBox->setCurrentIndex(0);
736 //toolBox->setItemEnabled(3, true);
737 rectFAlpha->setValue(rec->pen().color().alpha());
738 rectBAlpha->setValue(rec->brush().color().alpha());
739 //kDebug() << rec->brush().color().alpha();
740 QColor fcol = rec->pen().color();
741 QColor bcol = rec->brush().color();
742 //fcol.setAlpha(255);
743 //bcol.setAlpha(255);
744 rectFColor->setColor(fcol);
745 rectBColor->setColor(bcol);
747 rectLineWidth->setValue(rec->pen().width());
748 enableToolbars(TITLE_RECTANGLE);
751 updateAxisButtons(l.at(0));
752 updateCoordinates(rec);
753 updateDimension(rec);
755 } else if (l.at(0)->type() == IMAGEITEM) {
756 showToolbars(TITLE_IMAGE);
758 updateCoordinates(l.at(0));
759 updateDimension(l.at(0));
761 enableToolbars(TITLE_IMAGE);
764 //toolBox->setCurrentIndex(0);
765 showToolbars(TITLE_NONE);
766 enableToolbars(TITLE_NONE);
767 /*frame_properties->setEnabled(false);
768 text_properties->setEnabled(false);
769 rect_properties->setEnabled(false);*/
771 zValue->setValue((int)l.at(0)->zValue());
772 itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
773 itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
774 value_x->blockSignals(false);
775 value_y->blockSignals(false);
776 value_w->blockSignals(false);
777 value_h->blockSignals(false);
778 itemzoom->blockSignals(false);
779 itemrotate->blockSignals(false);
783 void TitleWidget::slotValueChanged(int type)
785 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
786 if (l.size() > 0 && l.at(0)->type() == IMAGEITEM) {
791 val = value_w->value();
794 val = value_h->value();
798 QGraphicsItem *i = l.at(0);
799 Transform t = m_transformations.value(i);
801 // Ratio width:height
802 double phi = (double) i->boundingRect().width() / i->boundingRect().height();
803 double alpha = (double) t.rotate / 180.0 * M_PI;
813 // Add 0.5 because otherwise incrementing by 1 might have no effect
814 length = val / (cos(alpha) + 1 / phi * sin(alpha)) + 0.5;
815 scale = length / i->boundingRect().width();
818 length = val / (phi * sin(alpha) + cos(alpha)) + 0.5;
819 scale = length / i->boundingRect().height();
826 qtrans.scale(scale, scale);
827 qtrans.rotate(t.rotate);
828 i->setTransform(qtrans);
829 m_transformations[i] = t;
836 /** \brief Updates position/size of the selected item when a value
837 * of an item (coordinates, size) has changed */
838 void TitleWidget::slotAdjustSelectedItem()
840 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
842 if (l.at(0)->type() == RECTITEM) {
844 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
846 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
847 } else if (l.at(0)->type() == TEXTITEM) {
849 updatePosition(l.at(0));
850 } else if (l.at(0)->type() == IMAGEITEM) {
852 updatePosition(l.at(0));
857 /** \brief Updates width/height int the text fields, regarding transformation matrix */
858 void TitleWidget::updateDimension(QGraphicsItem *i)
860 value_w->blockSignals(true);
861 value_h->blockSignals(true);
864 if (i->type() == IMAGEITEM) {
865 // Get multipliers for rotation/scaling
867 /*Transform t = m_transformations.value(i);
868 QRectF r = i->boundingRect();
869 int width = (int) ( abs(r.width()*t.scalex * cos(t.rotate/180.0*M_PI))
870 + abs(r.height()*t.scaley * sin(t.rotate/180.0*M_PI)) );
871 int height = (int) ( abs(r.height()*t.scaley * cos(t.rotate/180*M_PI))
872 + abs(r.width()*t.scalex * sin(t.rotate/180*M_PI)) );*/
874 value_w->setValue(i->sceneBoundingRect().width());
875 value_h->setValue(i->sceneBoundingRect().height());
876 } else if (i->type() == RECTITEM) {
877 QGraphicsRectItem *r = static_cast <QGraphicsRectItem *>(i);
878 value_w->setValue((int) r->rect().width());
879 value_h->setValue((int) r->rect().height());
880 } else if (i->type() == TEXTITEM) {
881 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(i);
882 value_w->setValue((int) t->boundingRect().width());
883 value_h->setValue((int) t->boundingRect().height());
886 value_w->blockSignals(false);
887 value_h->blockSignals(false);
890 /** \brief Updates the coordinates in the text fields from the item */
891 void TitleWidget::updateCoordinates(QGraphicsItem *i)
893 // Block signals emitted by this method
894 value_x->blockSignals(true);
895 value_y->blockSignals(true);
897 if (i->type() == TEXTITEM) {
899 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
901 // Set the correct x coordinate value
902 if (origin_x_left->isChecked()) {
903 // Origin (0 point) is at m_frameWidth, coordinate axis is inverted
904 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->boundingRect().width()));
906 // Origin is at 0 (default)
907 value_x->setValue((int) rec->pos().x());
911 if (origin_y_top->isChecked()) {
912 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->boundingRect().height()));
914 value_y->setValue((int) rec->pos().y());
917 } else if (i->type() == RECTITEM) {
919 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
921 if (origin_x_left->isChecked()) {
922 // Origin (0 point) is at m_frameWidth
923 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->rect().width()));
925 // Origin is at 0 (default)
926 value_x->setValue((int) rec->pos().x());
929 if (origin_y_top->isChecked()) {
930 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->rect().height()));
932 value_y->setValue((int) rec->pos().y());
935 } else if (i->type() == IMAGEITEM) {
937 if (origin_x_left->isChecked()) {
938 value_x->setValue((int)(m_frameWidth - i->pos().x() - i->sceneBoundingRect().width()));
940 value_x->setValue((int) i->pos().x());
943 if (origin_y_top->isChecked()) {
944 value_y->setValue((int)(m_frameHeight - i->pos().y() - i->sceneBoundingRect().height()));
946 value_y->setValue((int) i->pos().y());
951 // Stop blocking signals now
952 value_x->blockSignals(false);
953 value_y->blockSignals(false);
956 void TitleWidget::updateRotZoom(QGraphicsItem *i)
958 itemzoom->blockSignals(true);
959 itemrotate->blockSignals(false);
961 Transform t = m_transformations.value(i);
962 itemzoom->setValue((int)(t.scalex * 100.0 + 0.5));
963 itemrotate->setValue((int)(t.rotate));
965 itemzoom->blockSignals(false);
966 itemrotate->blockSignals(false);
969 /** \brief Updates the position of an item by reading coordinates from the text fields */
970 void TitleWidget::updatePosition(QGraphicsItem *i)
972 if (i->type() == TEXTITEM) {
973 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
976 if (origin_x_left->isChecked()) {
977 /* Origin of the x axis is at m_frameWidth,
978 * and distance from right border of the item to the right
979 * border of the frame is taken.
980 * See comment to slotOriginXClicked().
982 posX = m_frameWidth - value_x->value() - rec->boundingRect().width();
984 posX = value_x->value();
988 if (origin_y_top->isChecked()) {
989 /* Same for y axis */
990 posY = m_frameHeight - value_y->value() - rec->boundingRect().height();
992 posY = value_y->value();
995 rec->setPos(posX, posY);
997 } else if (i->type() == RECTITEM) {
999 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
1002 if (origin_x_left->isChecked()) {
1003 posX = m_frameWidth - value_x->value() - rec->rect().width();
1005 posX = value_x->value();
1009 if (origin_y_top->isChecked()) {
1010 posY = m_frameHeight - value_y->value() - rec->rect().height();
1012 posY = value_y->value();
1015 rec->setPos(posX, posY);
1017 } else if (i->type() == IMAGEITEM) {
1019 if (origin_x_left->isChecked()) {
1020 // Use the sceneBoundingRect because this also regards transformations like zoom
1021 posX = m_frameWidth - value_x->value() - i->sceneBoundingRect().width();
1023 posX = value_x->value();
1027 if (origin_y_top->isChecked()) {
1028 posY = m_frameHeight - value_y->value() - i->sceneBoundingRect().height();
1030 posY = value_y->value();
1033 i->setPos(posX, posY);
1039 void TitleWidget::updateTextOriginX()
1041 if (origin_x_left->isChecked()) {
1042 origin_x_left->setText(i18n("\u2212X"));
1044 origin_x_left->setText(i18n("+X"));
1048 void TitleWidget::slotOriginXClicked()
1050 // Update the text displayed on the button.
1051 updateTextOriginX();
1053 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1054 if (l.size() >= 1) {
1055 updateCoordinates(l.at(0));
1057 // Remember x axis setting
1058 l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked() ?
1059 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1061 graphicsView->setFocus();
1064 void TitleWidget::updateTextOriginY()
1066 if (origin_y_top->isChecked()) {
1067 origin_y_top->setText(i18n("\u2212Y"));
1069 origin_y_top->setText(i18n("+Y"));
1073 void TitleWidget::slotOriginYClicked()
1075 // Update the text displayed on the button.
1076 updateTextOriginY();
1078 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1079 if (l.size() >= 1) {
1080 updateCoordinates(l.at(0));
1082 l.at(0)->setData(TitleDocument::OriginYTop, origin_y_top->isChecked() ?
1083 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1086 graphicsView->setFocus();
1089 void TitleWidget::updateAxisButtons(QGraphicsItem *i)
1091 int xAxis = i->data(TitleDocument::OriginXLeft).toInt();
1092 int yAxis = i->data(TitleDocument::OriginYTop).toInt();
1093 origin_x_left->blockSignals(true);
1094 origin_y_top->blockSignals(true);
1096 if (xAxis == TitleDocument::AxisInverted) {
1097 origin_x_left->setChecked(true);
1099 origin_x_left->setChecked(false);
1101 updateTextOriginX();
1103 if (yAxis == TitleDocument::AxisInverted) {
1104 origin_y_top->setChecked(true);
1106 origin_y_top->setChecked(false);
1108 updateTextOriginY();
1110 origin_x_left->blockSignals(false);
1111 origin_y_top->blockSignals(false);
1114 void TitleWidget::slotChangeBackground()
1116 QColor color = kcolorbutton->color();
1117 color.setAlpha(horizontalSlider->value());
1118 m_frameBorder->setBrush(QBrush(color));
1122 * Something (yeah) has changed in our QGraphicsScene.
1124 void TitleWidget::slotChanged()
1126 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1127 if (l.size() >= 1 && l.at(0)->type() == TEXTITEM) {
1128 textChanged(static_cast <QGraphicsTextItem *>(l.at(0)));
1133 * If the user has set origin_x_left (everything also for y),
1134 * we need to look whether a text element has been selected. If yes,
1135 * we need to ensure that the right border of the text field
1136 * remains fixed also when some text has been entered.
1138 * This is also known as right-justified, with the difference that
1139 * it is not valid for text but for its boundingRect. Text may still
1140 * be left-justified.
1142 void TitleWidget::textChanged(QGraphicsTextItem *i)
1147 if (origin_x_left->isChecked() || origin_y_top->isChecked()) {
1149 if (!i->toPlainText().isEmpty()) {
1153 * Don't do anything if the string is empty. If the position
1154 * would be updated here, a newly created text field would
1155 * be set to the position of the last selected text field.
1161 void TitleWidget::slotInsertUnicode()
1163 m_unicodeDialog->exec();
1166 void TitleWidget::slotInsertUnicodeString(QString text)
1168 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
1170 if (l.at(0)->type() == TEXTITEM) {
1171 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(l.at(0));
1172 t->textCursor().insertText(text);
1177 void TitleWidget::slotUpdateText()
1179 QFont font = font_family->currentFont();
1180 font.setPixelSize(font_size->value());
1181 font.setItalic(buttonItalic->isChecked());
1182 font.setUnderline(buttonUnder->isChecked());
1183 font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1184 QColor color = fontColorButton->color();
1185 color.setAlpha(textAlpha->value());
1187 QGraphicsTextItem* item = NULL;
1188 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1189 if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
1190 item = static_cast <QGraphicsTextItem *>(l.at(0));
1193 //if (item->textCursor().selection ().isEmpty())
1194 QTextCursor cur = item->textCursor();
1195 QTextBlockFormat format = cur.blockFormat();
1196 if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
1197 item->setTextWidth(item->boundingRect().width());
1198 if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
1199 else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
1200 else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
1202 format.setAlignment(Qt::AlignLeft);
1203 item->setTextWidth(-1);
1207 item->setFont(font);
1208 item->setDefaultTextColor(color);
1209 cur.select(QTextCursor::Document);
1210 cur.setBlockFormat(format);
1211 item->setTextCursor(cur);
1212 cur.clearSelection();
1213 item->setTextCursor(cur);
1218 void TitleWidget::rectChanged()
1220 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1221 if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
1222 QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
1223 QColor f = rectFColor->color();
1224 f.setAlpha(rectFAlpha->value());
1226 penf.setWidth(rectLineWidth->value());
1228 QColor b = rectBColor->color();
1229 b.setAlpha(rectBAlpha->value());
1230 rec->setBrush(QBrush(b));
1234 void TitleWidget::itemScaled(int val)
1236 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1237 if (l.size() == 1) {
1238 Transform x = m_transformations.value(l.at(0));
1239 x.scalex = (double)val / 100.0;
1240 x.scaley = (double)val / 100.0;
1242 qtrans.scale(x.scalex, x.scaley);
1243 qtrans.rotate(x.rotate);
1244 l[0]->setTransform(qtrans);
1245 m_transformations[l.at(0)] = x;
1246 updateDimension(l.at(0));
1250 void TitleWidget::itemRotate(int val)
1252 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1253 if (l.size() == 1) {
1254 Transform x = m_transformations[l.at(0)];
1255 x.rotate = (double)val;
1257 qtrans.scale(x.scalex, x.scaley);
1258 qtrans.rotate(x.rotate);
1259 l[0]->setTransform(qtrans);
1260 m_transformations[l.at(0)] = x;
1261 updateDimension(l.at(0));
1265 void TitleWidget::itemHCenter()
1267 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1268 if (l.size() == 1) {
1269 QGraphicsItem *item = l.at(0);
1271 br = item->sceneBoundingRect();
1272 int width = (int)br.width();
1273 int newPos = (int)((m_frameWidth - width) / 2);
1274 newPos += item->pos().x() - br.left(); // Check item transformation
1275 item->setPos(newPos, item->pos().y());
1276 updateCoordinates(item);
1280 void TitleWidget::itemVCenter()
1282 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1283 if (l.size() == 1) {
1284 QGraphicsItem *item = l.at(0);
1286 br = item->sceneBoundingRect();
1287 int height = (int)br.height();
1288 int newPos = (int)((m_frameHeight - height) / 2);
1289 newPos += item->pos().y() - br.top(); // Check item transformation
1290 item->setPos(item->pos().x(), newPos);
1291 updateCoordinates(item);
1295 void TitleWidget::setupViewports()
1297 //double aspect_ratio = 4.0 / 3.0;//read from project
1298 //better zoom centered, but render uses only the created rect, so no problem to change the zoom function
1299 /*QRectF sp(0, 0, startViewportSize->value() * m_frameWidth / 100.0 , startViewportSize->value()* m_frameHeight / 100.0);
1300 QRectF ep(0, 0, endViewportSize->value() * m_frameWidth / 100.0, endViewportSize->value() * m_frameHeight / 100.0);
1301 // use a polygon thiat uses 16:9 and 4:3 rects forpreview the size in all aspect ratios ?
1302 QPolygonF spoly(sp);
1303 QPolygonF epoly(ep);
1304 spoly.translate(startViewportX->value(), startViewportY->value());
1305 epoly.translate(endViewportX->value(), endViewportY->value());
1306 m_startViewport->setPolygon(spoly);
1307 m_endViewport->setPolygon(epoly);
1308 if (! insertingValues) {
1309 m_startViewport->setData(0, startViewportX->value());
1310 m_startViewport->setData(1, startViewportY->value());
1311 m_startViewport->setData(2, startViewportSize->value());
1313 m_endViewport->setData(0, endViewportX->value());
1314 m_endViewport->setData(1, endViewportY->value());
1315 m_endViewport->setData(2, endViewportSize->value());
1319 void TitleWidget::loadTitle(KUrl url)
1321 if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this, i18n("Load Title"));
1322 if (!url.isEmpty()) {
1323 QList<QGraphicsItem *> items = m_scene->items();
1324 for (int i = 0; i < items.size(); i++) {
1325 if (items.at(i)->zValue() > -1000) delete items.at(i);
1327 m_scene->clearTextSelection();
1331 if (KIO::NetAccess::download(url, tmpfile, 0)) {
1332 QFile file(tmpfile);
1333 if (file.open(QIODevice::ReadOnly)) {
1334 doc.setContent(&file, false);
1337 KIO::NetAccess::removeTempFile(tmpfile);
1342 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport, &out) + 1;
1344 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1345 insertingValues = true;
1346 startViewportX->setValue(m_startViewport->data(0).toInt());
1347 startViewportY->setValue(m_startViewport->data(1).toInt());
1348 startViewportSize->setValue(m_startViewport->data(2).toInt());
1349 endViewportX->setValue(m_endViewport->data(0).toInt());
1350 endViewportY->setValue(m_endViewport->data(1).toInt());
1351 endViewportSize->setValue(m_endViewport->data(2).toInt());
1353 insertingValues = false;
1359 void TitleWidget::saveTitle(KUrl url)
1361 if (anim_start->isChecked()) slotAnimStart(false);
1362 if (anim_end->isChecked()) slotAnimEnd(false);
1363 if (url.isEmpty()) url = KFileDialog::getSaveUrl(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this, i18n("Save Title"));
1364 if (!url.isEmpty()) {
1365 if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport, m_tc.getFrameCount(title_duration->text())) == false)
1366 KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
1370 QDomDocument TitleWidget::xml()
1372 QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
1373 doc.documentElement().setAttribute("out", m_tc.getFrameCount(title_duration->text()));
1374 if (cropImage->isChecked()) {
1375 doc.documentElement().setAttribute("crop", 1);
1380 int TitleWidget::duration() const
1382 return m_tc.getFrameCount(title_duration->text());
1385 void TitleWidget::setXml(QDomDocument doc)
1388 m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport, &out);
1390 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1391 /*if (doc.documentElement().hasAttribute("out")) {
1392 GenTime duration = GenTime(doc.documentElement().attribute("out").toDouble() / 1000.0);
1393 title_duration->setText(m_tc.getTimecode(duration));
1395 else title_duration->setText(m_tc.getTimecode(GenTime(5000)));*/
1397 QDomElement e = doc.documentElement();
1398 cropImage->setChecked(e.hasAttribute("crop"));
1399 m_transformations.clear();
1400 QList <QGraphicsItem *> items = graphicsView->scene()->items();
1401 const double PI = 4.0 * atan(1.0);
1402 for (int i = 0; i < items.count(); i++) {
1403 QTransform t = items.at(i)->transform();
1407 x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
1408 m_transformations[items.at(i)] = x;
1410 // mbd: Update the GUI color selectors to match the stuff from the loaded document
1411 QColor background_color = m_titledocument.getBackgroundColor();
1412 horizontalSlider->blockSignals(true);
1413 kcolorbutton->blockSignals(true);
1414 horizontalSlider->setValue(background_color.alpha());
1415 background_color.setAlpha(255);
1416 kcolorbutton->setColor(background_color);
1417 horizontalSlider->blockSignals(false);
1418 kcolorbutton->blockSignals(false);
1420 /*startViewportX->setValue(m_startViewport->data(0).toInt());
1421 startViewportY->setValue(m_startViewport->data(1).toInt());
1422 startViewportSize->setValue(m_startViewport->data(2).toInt());
1423 endViewportX->setValue(m_endViewport->data(0).toInt());
1424 endViewportY->setValue(m_endViewport->data(1).toInt());
1425 endViewportSize->setValue(m_endViewport->data(2).toInt());*/
1427 QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
1431 const QRect TitleWidget::renderedRect()
1435 int maxX = m_frameWidth;
1436 int maxY = m_frameHeight;
1437 if (!cropImage->isChecked()) {
1438 m_scene->removeItem(m_startViewport);
1439 m_scene->removeItem(m_endViewport);
1440 QRect boundingRect = m_scene->itemsBoundingRect().toRect();
1441 if (boundingRect.left() < 0) minX = boundingRect.left();
1442 if (boundingRect.top() < 0) minY = boundingRect.top();
1443 if (boundingRect.right() > maxX) maxX = boundingRect.right();
1444 if (boundingRect.bottom() > maxY) maxY = boundingRect.bottom();
1452 QRect rect(minX, minY, maxX, maxY);
1456 QImage TitleWidget::renderedPixmap()
1458 QRect rect = renderedRect();
1459 QImage pix(rect.width(), rect.height(), QImage::Format_ARGB32);
1460 pix.fill(Qt::transparent);
1461 QPainter painter(&pix);
1462 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
1463 m_scene->clearTextSelection();
1464 QPen framepen = m_frameBorder->pen();
1465 m_frameBorder->setPen(Qt::NoPen);
1466 m_startViewport->setVisible(false);
1467 m_endViewport->setVisible(false);
1468 m_frameImage->setVisible(false);
1470 m_scene->render(&painter, QRectF(), rect); //QRectF(minX, minY, maxX - minX, maxY - minY));
1472 m_frameBorder->setPen(framepen);
1473 m_startViewport->setVisible(true);
1474 m_endViewport->setVisible(true);
1475 m_frameImage->setVisible(true);
1479 /** \brief Connected to the accepted signal - calls writeChoices */
1480 void TitleWidget::slotAccepted()
1482 if (anim_start->isChecked()) slotAnimStart(false);
1483 if (anim_end->isChecked()) slotAnimEnd(false);
1487 /** \brief Store the current choices of font, background and rect values */
1488 void TitleWidget::writeChoices()
1490 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1491 KSharedConfigPtr config = KGlobal::config();
1492 KConfigGroup titleConfig(config, "TitleWidget");
1493 // Write the entries
1494 titleConfig.writeEntry("font_family", font_family->currentFont());
1495 //titleConfig.writeEntry("font_size", font_size->value());
1496 titleConfig.writeEntry("font_pixel_size", font_size->value());
1497 titleConfig.writeEntry("font_color", fontColorButton->color());
1498 titleConfig.writeEntry("font_alpha", textAlpha->value());
1499 titleConfig.writeEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1500 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
1501 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
1503 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
1504 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
1505 titleConfig.writeEntry("rect_background_color", rectBColor->color());
1506 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
1507 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
1509 titleConfig.writeEntry("background_color", kcolorbutton->color());
1510 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
1512 titleConfig.writeEntry("crop_image", cropImage->isChecked());
1514 //! \todo Not sure if I should sync - it is probably safe to do it
1519 /** \brief Read the last stored choices into the dialog */
1520 void TitleWidget::readChoices()
1522 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1523 KSharedConfigPtr config = KGlobal::config();
1524 KConfigGroup titleConfig(config, "TitleWidget");
1526 font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
1527 font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
1528 fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
1529 textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
1531 if (titleConfig.readEntry("font_bold", false)) weight = QFont::Bold;
1532 else weight = titleConfig.readEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1533 setFontBoxWeight(weight);
1534 buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
1535 buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
1537 rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
1538 rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
1539 rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
1540 rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
1541 rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
1543 kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
1544 horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
1546 cropImage->setChecked(titleConfig.readEntry("crop_image", cropImage->isChecked()));
1549 void TitleWidget::adjustFrameSize()
1551 m_frameWidth = m_titledocument.frameWidth();
1552 m_frameHeight = m_titledocument.frameHeight();
1553 m_frameBorder->setRect(0, 0, m_frameWidth, m_frameHeight);
1554 displayBackgroundFrame();
1557 void TitleWidget::slotAnimStart(bool anim)
1559 if (anim && anim_end->isChecked()) {
1560 anim_end->setChecked(false);
1561 m_endViewport->setZValue(-1000);
1562 m_endViewport->setBrush(QBrush());
1565 QList<QGraphicsItem *> list = m_scene->items();
1566 for (int i = 0; i < list.count(); i++) {
1567 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1568 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1570 align_box->setEnabled(anim);
1571 frame_toolbar->setEnabled(!anim);
1572 rect_properties->setEnabled(!anim);
1574 m_startViewport->setZValue(1100);
1575 QColor col = m_startViewport->pen().color();
1577 m_startViewport->setBrush(col);
1578 m_startViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1581 m_startViewport->setZValue(-1000);
1582 m_startViewport->setBrush(QBrush());
1583 m_startViewport->setFlags(0);
1588 void TitleWidget::slotAnimEnd(bool anim)
1590 if (anim && anim_start->isChecked()) {
1591 anim_start->setChecked(false);
1592 m_startViewport->setZValue(-1000);
1593 m_startViewport->setBrush(QBrush());
1596 QList<QGraphicsItem *> list = m_scene->items();
1597 for (int i = 0; i < list.count(); i++) {
1598 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1599 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1601 align_box->setEnabled(anim);
1602 frame_toolbar->setEnabled(!anim);
1603 rect_properties->setEnabled(!anim);
1605 m_endViewport->setZValue(1100);
1606 QColor col = m_endViewport->pen().color();
1608 m_endViewport->setBrush(col);
1609 m_endViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1612 m_endViewport->setZValue(-1000);
1613 m_endViewport->setBrush(QBrush());
1614 m_endViewport->setFlags(0);