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>
29 #include <kdeversion.h>
31 #include <QDomDocument>
32 #include <QGraphicsItem>
33 #include <QGraphicsSvgItem>
37 #include <QSignalMapper>
38 #include <QTextBlockFormat>
39 #include <QTextCursor>
41 #if QT_VERSION >= 0x040600
42 #include <QGraphicsEffect>
43 #include <QGraphicsBlurEffect>
44 #include <QGraphicsDropShadowEffect>
47 int settingUp = false;
49 const int IMAGEITEM = 7;
50 const int RECTITEM = 3;
51 const int TEXTITEM = 8;
53 const int NOEFFECT = 0;
54 const int BLUREFFECT = 1;
55 const int SHADOWEFFECT = 2;
56 const int TYPEWRITEREFFECT = 3;
58 TitleWidget::TitleWidget(KUrl url, Timecode tc, QString projectTitlePath, Render *render, QWidget *parent) :
61 m_startViewport(NULL),
65 m_unicodeDialog(new UnicodeDialog(UnicodeDialog::InputHex)),
66 m_projectTitlePath(projectTitlePath),
70 setFont(KGlobalSettings::toolBarFont());
71 frame_properties->setEnabled(false);
72 frame_properties->setFixedHeight(frame_toolbar->height());
74 itemzoom->setSuffix(i18n("%"));
75 m_frameWidth = render->renderWidth();
76 m_frameHeight = render->renderHeight();
77 showToolbars(TITLE_SELECT);
79 //TODO: get default title duration instead of hardcoded one
80 title_duration->setText(m_tc.getTimecode(GenTime(5000 / 1000.0)));
82 connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
83 connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
85 connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
86 connect(font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(slotUpdateText())) ;
87 connect(font_size, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText())) ;
88 connect(textAlpha, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText()));
89 connect(font_weight_box, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateText()));
91 connect(font_family, SIGNAL(editTextChanged(const QString &)), this, SLOT(slotFontText(const QString&)));
93 connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
94 connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
95 connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
96 connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
97 connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
99 /*connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
100 connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
101 connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
102 connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
103 connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
104 connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));*/
107 effect_list->addItem(i18n("None"), NOEFFECT);
108 effect_list->addItem(i18n("Typewriter"), TYPEWRITEREFFECT);
110 connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
111 connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
112 connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
113 connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
114 connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
115 connect(itemtop, SIGNAL(clicked()), this, SLOT(itemTop()));
116 connect(itembottom, SIGNAL(clicked()), this, SLOT(itemBottom()));
117 connect(itemleft, SIGNAL(clicked()), this, SLOT(itemLeft()));
118 connect(itemright, SIGNAL(clicked()), this, SLOT(itemRight()));
119 connect(effect_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotAddEffect(int)));
120 connect(typewriter_delay, SIGNAL(valueChanged(int)), this, SLOT(slotEditTypewriter(int)));
121 connect(blur_radius, SIGNAL(valueChanged(int)), this, SLOT(slotEditBlur(int)));
122 connect(shadow_radius, SIGNAL(valueChanged(int)), this, SLOT(slotEditShadow()));
123 connect(shadow_x, SIGNAL(valueChanged(int)), this, SLOT(slotEditShadow()));
124 connect(shadow_y, SIGNAL(valueChanged(int)), this, SLOT(slotEditShadow()));
125 effect_stack->setHidden(true);
126 effect_frame->setEnabled(false);
128 connect(origin_x_left, SIGNAL(clicked()), this, SLOT(slotOriginXClicked()));
129 connect(origin_y_top, SIGNAL(clicked()), this, SLOT(slotOriginYClicked()));
131 m_signalMapper = new QSignalMapper(this);
132 m_signalMapper->setMapping(value_w, ValueWidth);
133 m_signalMapper->setMapping(value_h, ValueHeight);
134 connect(value_w, SIGNAL(valueChanged(int)), m_signalMapper, SLOT(map()));
135 connect(value_h, SIGNAL(valueChanged(int)), m_signalMapper, SLOT(map()));
136 connect(m_signalMapper, SIGNAL(mapped(int)), this, SLOT(slotValueChanged(int)));
138 connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
139 connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
140 connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
141 connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
142 connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
143 connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
144 connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
145 connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
146 connect(buttonAlignLeft, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
147 connect(buttonAlignRight, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
148 connect(buttonAlignCenter, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
149 connect(buttonAlignNone, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
150 //connect(buttonInsertUnicode, SIGNAL(clicked()), this, SLOT(slotInsertUnicode()));
151 connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
153 connect(m_unicodeDialog, SIGNAL(charSelected(QString)), this, SLOT(slotInsertUnicodeString(QString)));
156 connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
158 font_weight_box->blockSignals(true);
159 font_weight_box->addItem(i18nc("Font style", "Light"), QFont::Light);
160 font_weight_box->addItem(i18nc("Font style", "Normal"), QFont::Normal);
161 font_weight_box->addItem(i18nc("Font style", "Demi-Bold"), QFont::DemiBold);
162 font_weight_box->addItem(i18nc("Font style", "Bold"), QFont::Bold);
163 font_weight_box->addItem(i18nc("Font style", "Black"), QFont::Black);
164 font_weight_box->setToolTip(i18n("Font weight"));
165 font_weight_box->setCurrentIndex(1);
166 font_weight_box->blockSignals(false);
168 buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
169 buttonRealSize->setIcon(KIcon("zoom-original"));
170 buttonItalic->setIcon(KIcon("format-text-italic"));
171 buttonUnder->setIcon(KIcon("format-text-underline"));
172 buttonAlignCenter->setIcon(KIcon("format-justify-center"));
173 buttonAlignLeft->setIcon(KIcon("format-justify-left"));
174 buttonAlignRight->setIcon(KIcon("format-justify-right"));
175 buttonAlignNone->setIcon(KIcon("kdenlive-align-none"));
177 buttonAlignNone->setToolTip(i18n("No alignment"));
178 buttonAlignRight->setToolTip(i18n("Align right"));
179 buttonAlignLeft->setToolTip(i18n("Align left"));
180 buttonAlignCenter->setToolTip(i18n("Align center"));
182 m_unicodeAction = new QAction(KIcon("kdenlive-insert-unicode"), QString(), this);
183 m_unicodeAction->setShortcut(Qt::SHIFT + Qt::CTRL + Qt::Key_U);
184 m_unicodeAction->setToolTip(i18n("Insert Unicode character") + ' ' + m_unicodeAction->shortcut().toString());
185 connect(m_unicodeAction, SIGNAL(triggered()), this, SLOT(slotInsertUnicode()));
186 buttonInsertUnicode->setDefaultAction(m_unicodeAction);
188 origin_x_left->setToolTip(i18n("Invert x axis and change 0 point"));
189 origin_y_top->setToolTip(i18n("Invert y axis and change 0 point"));
190 rectBColor->setToolTip(i18n("Select fill color"));
191 rectFColor->setToolTip(i18n("Select border color"));
192 rectBAlpha->setToolTip(i18n("Fill transparency"));
193 rectFAlpha->setToolTip(i18n("Border transparency"));
194 zoom_slider->setToolTip(i18n("Zoom"));
195 buttonRealSize->setToolTip(i18n("Original size (1:1)"));
196 buttonFitZoom->setToolTip(i18n("Fit zoom"));
197 kcolorbutton->setToolTip(i18n("Select background color"));
198 horizontalSlider->setToolTip(i18n("Background Transparency"));
200 itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
201 itemhcenter->setToolTip(i18n("Align item horizontally"));
202 itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
203 itemvcenter->setToolTip(i18n("Align item vertically"));
204 itemtop->setIcon(KIcon("kdenlive-align-top"));
205 itemtop->setToolTip(i18n("Align item to top"));
206 itembottom->setIcon(KIcon("kdenlive-align-bottom"));
207 itembottom->setToolTip(i18n("Align item to bottom"));
208 itemright->setIcon(KIcon("kdenlive-align-right"));
209 itemright->setToolTip(i18n("Align item to right"));
210 itemleft->setIcon(KIcon("kdenlive-align-left"));
211 itemleft->setToolTip(i18n("Align item to left"));
214 QHBoxLayout *layout = new QHBoxLayout;
215 frame_toolbar->setLayout(layout);
216 layout->setContentsMargins(0, 0, 0, 0);
217 QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
218 int s = style()->pixelMetric(QStyle::PM_SmallIconSize);
219 m_toolbar->setIconSize(QSize(s, s));
221 m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), QString());
222 m_buttonCursor->setCheckable(true);
223 m_buttonCursor->setShortcut(Qt::ALT + Qt::Key_S);
224 m_buttonCursor->setToolTip(i18n("Selection Tool") + ' ' + m_buttonCursor->shortcut().toString());
225 connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
227 m_buttonText = m_toolbar->addAction(KIcon("insert-text"), QString());
228 m_buttonText->setCheckable(true);
229 m_buttonText->setShortcut(Qt::ALT + Qt::Key_T);
230 m_buttonText->setToolTip(i18n("Add Text") + ' ' + m_buttonText->shortcut().toString());
231 connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
233 m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), QString());
234 m_buttonRect->setCheckable(true);
235 m_buttonRect->setShortcut(Qt::ALT + Qt::Key_R);
236 m_buttonRect->setToolTip(i18n("Add Rectangle") + ' ' + m_buttonRect->shortcut().toString());
237 connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
239 m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), QString());
240 m_buttonImage->setCheckable(false);
241 m_buttonImage->setShortcut(Qt::ALT + Qt::Key_I);
242 m_buttonImage->setToolTip(i18n("Add Image") + ' ' + m_buttonImage->shortcut().toString());
243 connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
245 m_toolbar->addSeparator();
247 m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
248 m_buttonLoad->setCheckable(false);
249 m_buttonLoad->setShortcut(Qt::CTRL + Qt::Key_O);
250 connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
252 m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
253 m_buttonSave->setCheckable(false);
254 m_buttonSave->setShortcut(Qt::CTRL + Qt::Key_S);
255 connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
257 layout->addWidget(m_toolbar);
259 // initialize graphic scene
260 m_scene = new GraphicsSceneRectMove(this);
261 graphicsView->setScene(m_scene);
262 m_titledocument.setScene(m_scene, m_frameWidth, m_frameHeight);
263 connect(m_scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(slotChanged()));
265 // a gradient background
266 /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
267 gradient->setSpread(QGradient::ReflectSpread);
268 scene->setBackgroundBrush(*gradient);*/
270 m_frameImage = new QGraphicsPixmapItem();
272 qtrans.scale(2.0, 2.0);
273 m_frameImage->setTransform(qtrans);
274 m_frameImage->setZValue(-1200);
275 m_frameImage->setFlags(0);
276 displayBackgroundFrame();
277 graphicsView->scene()->addItem(m_frameImage);
279 connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
280 connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
281 connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
282 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
283 //connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
284 connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
285 connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
286 connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
288 QPen framepen(Qt::DotLine);
289 framepen.setColor(Qt::red);
291 m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
292 m_frameBorder->setPen(framepen);
293 m_frameBorder->setZValue(-1100);
294 m_frameBorder->setBrush(Qt::transparent);
295 m_frameBorder->setFlags(0);
296 graphicsView->scene()->addItem(m_frameBorder);
298 // mbd: load saved settings
301 graphicsView->show();
302 //graphicsView->setRenderHint(QPainter::Antialiasing);
303 graphicsView->setInteractive(true);
304 //graphicsView->resize(400, 300);
305 kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
306 //toolBox->setItemEnabled(2, false);
307 m_startViewport = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
308 m_endViewport = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
309 m_startViewport->setData(0, m_frameWidth);
310 m_startViewport->setData(1, m_frameHeight);
311 m_endViewport->setData(0, m_frameWidth);
312 m_endViewport->setData(1, m_frameHeight);
314 if (!url.isEmpty()) loadTitle(url);
317 QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
320 connect(anim_start, SIGNAL(toggled(bool)), this, SLOT(slotAnimStart(bool)));
321 connect(anim_end, SIGNAL(toggled(bool)), this, SLOT(slotAnimEnd(bool)));
323 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(KdenliveSettings::hastitleproducer());
326 TitleWidget::~TitleWidget()
330 delete m_buttonImage;
331 delete m_buttonCursor;
334 delete m_unicodeAction;
336 delete m_unicodeDialog;
337 delete m_frameBorder;
339 delete m_startViewport;
340 delete m_endViewport;
342 delete m_signalMapper;
346 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl, bool isClone)
349 QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
350 KStandardDirs::makeDir(titlePath);
351 titlePath.append((isClone == false) ? "title" : "clone");
354 while (path.isEmpty() || QFile::exists(path)) {
356 path = titlePath + QString::number(counter).rightJustified(3, '0', false) + ".png";
358 result.append(((isClone == false) ? i18n("Title") : i18n("Clone")) + ' ' + QString::number(counter).rightJustified(3, '0', false));
364 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
367 QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
368 KStandardDirs::makeDir(titlePath);
369 return titlePath + titleName + ".png";
373 QStringList TitleWidget::extractImageList(QString xml)
376 if (xml.isEmpty()) return result;
379 QDomNodeList images = doc.elementsByTagName("content");
380 for (int i = 0; i < images.count(); i++) {
381 if (images.at(i).toElement().hasAttribute("url"))
382 result.append(images.at(i).toElement().attribute("url"));
389 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
394 void TitleWidget::slotTextTool()
396 m_scene->setTool(TITLE_TEXT);
397 showToolbars(TITLE_TEXT);
398 checkButton(TITLE_TEXT);
401 void TitleWidget::slotRectTool()
403 m_scene->setTool(TITLE_RECTANGLE);
404 showToolbars(TITLE_RECTANGLE);
405 checkButton(TITLE_RECTANGLE);
408 void TitleWidget::slotSelectTool()
410 m_scene->setTool(TITLE_SELECT);
412 // Find out which toolbars need to be shown, depending on selected item
413 TITLETOOL t = TITLE_SELECT;
414 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
416 switch (l.at(0)->type()) {
430 if (t == TITLE_RECTANGLE && (l.at(0) == m_endViewport || l.at(0) == m_startViewport)) {
431 //graphicsView->centerOn(l.at(0));
437 updateCoordinates(l.at(0));
438 updateDimension(l.at(0));
439 updateRotZoom(l.at(0));
442 checkButton(TITLE_SELECT);
445 void TitleWidget::slotImageTool()
447 // TODO: find a way to get a list of all supported image types...
448 QString allExtensions = "image/gif image/jpeg image/png image/x-tga image/x-bmp image/svg+xml image/tiff image/x-xcf-gimp image/x-vnd.adobe.photoshop image/x-pcx image/x-exr";
449 KUrl url = KFileDialog::getOpenUrl(KUrl(), allExtensions, this, i18n("Load Image")); //"*.svg *.png *.jpg *.jpeg *.gif *.raw"
450 if (!url.isEmpty()) {
451 if (url.path().endsWith(".svg")) {
452 QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
453 svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
454 svg->setZValue(m_count++);
455 svg->setData(Qt::UserRole, url.path());
456 graphicsView->scene()->addItem(svg);
458 QPixmap pix(url.path());
459 QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
460 image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
461 image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
462 image->setData(Qt::UserRole, url.path());
463 image->setZValue(m_count++);
464 graphicsView->scene()->addItem(image);
467 m_scene->setTool(TITLE_SELECT);
468 showToolbars(TITLE_SELECT);
469 checkButton(TITLE_SELECT);
472 void TitleWidget::showToolbars(TITLETOOL toolType)
474 toolbar_stack->setCurrentIndex((int) toolType);
477 void TitleWidget::enableToolbars(TITLETOOL toolType)
479 // TITLETOOL is defined in graphicsscenerectmove.h
481 if (toolType == TITLE_RECTANGLE || toolType == TITLE_IMAGE) enable = true;
482 value_w->setEnabled(enable);
483 value_h->setEnabled(enable);
486 void TitleWidget::checkButton(TITLETOOL toolType)
488 bool bSelect = false;
500 case TITLE_RECTANGLE:
510 m_buttonCursor->setChecked(bSelect);
511 m_buttonText->setChecked(bText);
512 m_buttonRect->setChecked(bRect);
513 m_buttonImage->setChecked(bImage);
516 void TitleWidget::displayBackgroundFrame()
518 if (!displayBg->isChecked()) {
519 QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
520 QPixmap pattern(20, 20);
522 QColor bgcolor(210, 210, 210);
525 p.fillRect(QRect(0, 0, 10, 10), bgcolor);
526 p.fillRect(QRect(10, 10, 20, 20), bgcolor);
531 p.fillRect(bg.rect(), br);
533 m_frameImage->setPixmap(bg);
535 m_frameImage->setPixmap(QPixmap::fromImage(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2)));
539 void TitleWidget::initAnimation()
541 align_box->setEnabled(false);
542 QPen startpen(Qt::DotLine);
543 QPen endpen(Qt::DashDotLine);
544 startpen.setColor(QColor(100, 200, 100, 140));
545 endpen.setColor(QColor(200, 100, 100, 140));
547 m_startViewport->setPen(startpen);
548 m_endViewport->setPen(endpen);
550 m_startViewport->setZValue(-1000);
551 m_endViewport->setZValue(-1000);
553 m_startViewport->setFlags(0);
554 m_endViewport->setFlags(0);
556 graphicsView->scene()->addItem(m_startViewport);
557 graphicsView->scene()->addItem(m_endViewport);
559 connect(keep_aspect, SIGNAL(toggled(bool)), this, SLOT(slotKeepAspect(bool)));
560 connect(resize50, SIGNAL(clicked()), this, SLOT(slotResize50()));
561 connect(resize100, SIGNAL(clicked()), this, SLOT(slotResize100()));
562 connect(resize200, SIGNAL(clicked()), this, SLOT(slotResize200()));
565 void TitleWidget::slotUpdateZoom(int pos)
567 m_scene->setZoom((double) pos / 100);
568 zoom_label->setText(QString::number(pos) + '%');
571 void TitleWidget::slotZoom(bool up)
573 int pos = zoom_slider->value();
576 zoom_slider->setValue(pos);
579 void TitleWidget::slotAdjustZoom()
581 /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
582 double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
583 if (scalex > scaley) scalex = scaley;
584 int zoompos = (int)(scalex * 7 + 0.5);*/
585 graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
586 int zoompos = graphicsView->matrix().m11() * 100;
587 zoom_slider->setValue(zoompos);
588 graphicsView->centerOn(m_frameBorder);
591 void TitleWidget::slotZoomOneToOne()
593 zoom_slider->setValue(100);
594 graphicsView->centerOn(m_frameBorder);
597 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
599 updateAxisButtons(rect); // back to default
601 QColor f = rectFColor->color();
602 f.setAlpha(rectFAlpha->value());
604 penf.setWidth(rectLineWidth->value());
606 QColor b = rectBColor->color();
607 b.setAlpha(rectBAlpha->value());
608 rect->setBrush(QBrush(b));
609 rect->setZValue(m_count++);
610 //setCurrentItem(rect);
611 //graphicsView->setFocus();
614 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
616 updateAxisButtons(tt); // back to default
618 QFont font = font_family->currentFont();
619 font.setPixelSize(font_size->value());
621 font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
622 font.setItalic(buttonItalic->isChecked());
623 font.setUnderline(buttonUnder->isChecked());
626 QColor color = fontColorButton->color();
627 color.setAlpha(textAlpha->value());
628 tt->setDefaultTextColor(color);
629 tt->setZValue(m_count++);
633 void TitleWidget::setFontBoxWeight(int weight)
635 int index = font_weight_box->findData(weight);
637 index = font_weight_box->findData(QFont::Normal);
639 font_weight_box->setCurrentIndex(index);
642 void TitleWidget::setCurrentItem(QGraphicsItem *item)
644 m_scene->setSelectedItem(item);
647 void TitleWidget::zIndexChanged(int v)
649 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
655 void TitleWidget::selectionChanged()
657 if (m_scene->tool() != TITLE_SELECT) return;
658 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
659 //toolBox->setItemEnabled(2, false);
660 //toolBox->setItemEnabled(3, false);
661 value_x->blockSignals(true);
662 value_y->blockSignals(true);
663 value_w->blockSignals(true);
664 value_h->blockSignals(true);
665 itemzoom->blockSignals(true);
666 itemrotate->blockSignals(true);
668 effect_stack->setHidden(true);
669 effect_frame->setEnabled(false);
670 effect_list->setCurrentIndex(0);
671 bool blockX = !origin_x_left->signalsBlocked();
672 bool blockY = !origin_y_top->signalsBlocked();
673 if (blockX) origin_x_left->blockSignals(true);
674 if (blockY) origin_y_top->blockSignals(true);
675 origin_x_left->setChecked(false);
676 origin_y_top->setChecked(false);
679 enableToolbars(TITLE_SELECT);
680 if (blockX) origin_x_left->blockSignals(false);
681 if (blockY) origin_y_top->blockSignals(false);
682 itemzoom->setEnabled(false);
683 itemrotate->setEnabled(false);
684 frame_properties->setEnabled(false);
685 } else if (l.size() == 1) {
686 effect_frame->setEnabled(true);
687 frame_properties->setEnabled(true);
688 if (l.at(0) != m_startViewport && l.at(0) != m_endViewport) {
689 itemzoom->setEnabled(true);
690 itemrotate->setEnabled(true);
692 itemzoom->setEnabled(false);
693 itemrotate->setEnabled(false);
696 if (l.at(0)->type() == TEXTITEM) {
697 showToolbars(TITLE_TEXT);
698 QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
699 if (!i->data(100).isNull()) {
700 // Item has an effect
701 QStringList effdata = i->data(100).toStringList();
702 if (effdata.at(0) == "typewriter") {
703 typewriter_delay->setValue(effdata.at(1).toInt());
704 effect_list->setCurrentIndex(effect_list->findData((int) TYPEWRITEREFFECT));
705 effect_stack->setHidden(false);
708 if (i->graphicsEffect()) {
709 QGraphicsBlurEffect *blur = static_cast <QGraphicsBlurEffect *>(i->graphicsEffect());
711 effect_list->setCurrentIndex(effect_list->findData((int) BLUREFFECT));
712 int rad = (int) blur->blurRadius();
713 blur_radius->setValue(rad);
714 effect_stack->setHidden(false);
716 QGraphicsDropShadowEffect *shad = static_cast <QGraphicsDropShadowEffect *>(i->graphicsEffect());
718 effect_list->setCurrentIndex(effect_list->findData((int) SHADOWEFFECT));
719 shadow_radius->setValue(shad->blurRadius());
720 shadow_x->setValue(shad->xOffset());
721 shadow_y->setValue(shad->yOffset());
722 effect_stack->setHidden(false);
725 } else effect_stack->setHidden(true);
727 //if (l[0]->hasFocus())
728 //toolBox->setCurrentIndex(0);
729 //toolBox->setItemEnabled(2, true);
730 font_size->blockSignals(true);
731 font_family->blockSignals(true);
732 font_weight_box->blockSignals(true);
733 buttonItalic->blockSignals(true);
734 buttonUnder->blockSignals(true);
735 fontColorButton->blockSignals(true);
736 textAlpha->blockSignals(true);
737 buttonAlignLeft->blockSignals(true);
738 buttonAlignRight->blockSignals(true);
739 buttonAlignNone->blockSignals(true);
740 buttonAlignCenter->blockSignals(true);
742 QFont font = i->font();
743 font_family->setCurrentFont(font);
744 font_size->setValue(font.pixelSize());
745 buttonItalic->setChecked(font.italic());
746 buttonUnder->setChecked(font.underline());
747 setFontBoxWeight(font.weight());
749 QColor color = i->defaultTextColor();
750 fontColorButton->setColor(color);
751 textAlpha->setValue(color.alpha());
753 QTextCursor cur = i->textCursor();
754 QTextBlockFormat format = cur.blockFormat();
755 if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
756 else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
757 else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
758 else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
760 font_size->blockSignals(false);
761 font_family->blockSignals(false);
762 font_weight_box->blockSignals(false);
763 buttonItalic->blockSignals(false);
764 buttonUnder->blockSignals(false);
765 fontColorButton->blockSignals(false);
766 textAlpha->blockSignals(false);
767 buttonAlignLeft->blockSignals(false);
768 buttonAlignRight->blockSignals(false);
769 buttonAlignNone->blockSignals(false);
770 buttonAlignCenter->blockSignals(false);
772 updateAxisButtons(i);
773 updateCoordinates(i);
775 enableToolbars(TITLE_TEXT);
777 } else if ((l.at(0))->type() == RECTITEM) {
778 showToolbars(TITLE_RECTANGLE);
780 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
781 if (rec == m_startViewport || rec == m_endViewport) {
782 /*toolBox->setCurrentIndex(3);
783 toolBox->widget(0)->setEnabled(false);
784 toolBox->widget(1)->setEnabled(false);*/
785 enableToolbars(TITLE_SELECT);
787 /*toolBox->widget(0)->setEnabled(true);
788 toolBox->widget(1)->setEnabled(true);
789 toolBox->setCurrentIndex(0);*/
790 //toolBox->setItemEnabled(3, true);
791 rectFAlpha->setValue(rec->pen().color().alpha());
792 rectBAlpha->setValue(rec->brush().color().alpha());
793 //kDebug() << rec->brush().color().alpha();
794 QColor fcol = rec->pen().color();
795 QColor bcol = rec->brush().color();
796 //fcol.setAlpha(255);
797 //bcol.setAlpha(255);
798 rectFColor->setColor(fcol);
799 rectBColor->setColor(bcol);
801 rectLineWidth->setValue(rec->pen().width());
802 enableToolbars(TITLE_RECTANGLE);
805 updateAxisButtons(l.at(0));
806 updateCoordinates(rec);
807 updateDimension(rec);
809 } else if (l.at(0)->type() == IMAGEITEM) {
810 showToolbars(TITLE_IMAGE);
812 updateCoordinates(l.at(0));
813 updateDimension(l.at(0));
815 enableToolbars(TITLE_IMAGE);
818 //toolBox->setCurrentIndex(0);
819 showToolbars(TITLE_SELECT);
820 enableToolbars(TITLE_SELECT);
821 frame_properties->setEnabled(false);
823 zValue->setValue((int)l.at(0)->zValue());
824 itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
825 itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
826 value_x->blockSignals(false);
827 value_y->blockSignals(false);
828 value_w->blockSignals(false);
829 value_h->blockSignals(false);
830 itemzoom->blockSignals(false);
831 itemrotate->blockSignals(false);
835 void TitleWidget::slotValueChanged(int type)
837 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
838 if (l.size() > 0 && l.at(0)->type() == IMAGEITEM) {
843 val = value_w->value();
846 val = value_h->value();
850 QGraphicsItem *i = l.at(0);
851 Transform t = m_transformations.value(i);
853 // Ratio width:height
854 double phi = (double) i->boundingRect().width() / i->boundingRect().height();
855 double alpha = (double) t.rotate / 180.0 * M_PI;
865 // Add 0.5 because otherwise incrementing by 1 might have no effect
866 length = val / (cos(alpha) + 1 / phi * sin(alpha)) + 0.5;
867 scale = length / i->boundingRect().width();
870 length = val / (phi * sin(alpha) + cos(alpha)) + 0.5;
871 scale = length / i->boundingRect().height();
878 qtrans.scale(scale, scale);
879 qtrans.rotate(t.rotate);
880 i->setTransform(qtrans);
881 m_transformations[i] = t;
888 /** \brief Updates position/size of the selected item when a value
889 * of an item (coordinates, size) has changed */
890 void TitleWidget::slotAdjustSelectedItem()
892 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
894 if (l.at(0)->type() == RECTITEM) {
896 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
898 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
899 } else if (l.at(0)->type() == TEXTITEM) {
901 updatePosition(l.at(0));
902 } else if (l.at(0)->type() == IMAGEITEM) {
904 updatePosition(l.at(0));
909 /** \brief Updates width/height int the text fields, regarding transformation matrix */
910 void TitleWidget::updateDimension(QGraphicsItem *i)
912 value_w->blockSignals(true);
913 value_h->blockSignals(true);
916 if (i->type() == IMAGEITEM) {
917 // Get multipliers for rotation/scaling
919 /*Transform t = m_transformations.value(i);
920 QRectF r = i->boundingRect();
921 int width = (int) ( abs(r.width()*t.scalex * cos(t.rotate/180.0*M_PI))
922 + abs(r.height()*t.scaley * sin(t.rotate/180.0*M_PI)) );
923 int height = (int) ( abs(r.height()*t.scaley * cos(t.rotate/180*M_PI))
924 + abs(r.width()*t.scalex * sin(t.rotate/180*M_PI)) );*/
926 value_w->setValue(i->sceneBoundingRect().width());
927 value_h->setValue(i->sceneBoundingRect().height());
928 } else if (i->type() == RECTITEM) {
929 QGraphicsRectItem *r = static_cast <QGraphicsRectItem *>(i);
930 value_w->setValue((int) r->rect().width());
931 value_h->setValue((int) r->rect().height());
932 } else if (i->type() == TEXTITEM) {
933 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(i);
934 value_w->setValue((int) t->boundingRect().width());
935 value_h->setValue((int) t->boundingRect().height());
938 value_w->blockSignals(false);
939 value_h->blockSignals(false);
942 /** \brief Updates the coordinates in the text fields from the item */
943 void TitleWidget::updateCoordinates(QGraphicsItem *i)
945 // Block signals emitted by this method
946 value_x->blockSignals(true);
947 value_y->blockSignals(true);
949 if (i->type() == TEXTITEM) {
951 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
953 // Set the correct x coordinate value
954 if (origin_x_left->isChecked()) {
955 // Origin (0 point) is at m_frameWidth, coordinate axis is inverted
956 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->boundingRect().width()));
958 // Origin is at 0 (default)
959 value_x->setValue((int) rec->pos().x());
963 if (origin_y_top->isChecked()) {
964 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->boundingRect().height()));
966 value_y->setValue((int) rec->pos().y());
969 } else if (i->type() == RECTITEM) {
971 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
973 if (origin_x_left->isChecked()) {
974 // Origin (0 point) is at m_frameWidth
975 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->rect().width()));
977 // Origin is at 0 (default)
978 value_x->setValue((int) rec->pos().x());
981 if (origin_y_top->isChecked()) {
982 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->rect().height()));
984 value_y->setValue((int) rec->pos().y());
987 } else if (i->type() == IMAGEITEM) {
989 if (origin_x_left->isChecked()) {
990 value_x->setValue((int)(m_frameWidth - i->pos().x() - i->sceneBoundingRect().width()));
992 value_x->setValue((int) i->pos().x());
995 if (origin_y_top->isChecked()) {
996 value_y->setValue((int)(m_frameHeight - i->pos().y() - i->sceneBoundingRect().height()));
998 value_y->setValue((int) i->pos().y());
1003 // Stop blocking signals now
1004 value_x->blockSignals(false);
1005 value_y->blockSignals(false);
1008 void TitleWidget::updateRotZoom(QGraphicsItem *i)
1010 itemzoom->blockSignals(true);
1011 itemrotate->blockSignals(false);
1013 Transform t = m_transformations.value(i);
1014 itemzoom->setValue((int)(t.scalex * 100.0 + 0.5));
1015 itemrotate->setValue((int)(t.rotate));
1017 itemzoom->blockSignals(false);
1018 itemrotate->blockSignals(false);
1021 /** \brief Updates the position of an item by reading coordinates from the text fields */
1022 void TitleWidget::updatePosition(QGraphicsItem *i)
1024 if (i->type() == TEXTITEM) {
1025 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
1028 if (origin_x_left->isChecked()) {
1029 /* Origin of the x axis is at m_frameWidth,
1030 * and distance from right border of the item to the right
1031 * border of the frame is taken.
1032 * See comment to slotOriginXClicked().
1034 posX = m_frameWidth - value_x->value() - rec->boundingRect().width();
1036 posX = value_x->value();
1040 if (origin_y_top->isChecked()) {
1041 /* Same for y axis */
1042 posY = m_frameHeight - value_y->value() - rec->boundingRect().height();
1044 posY = value_y->value();
1047 rec->setPos(posX, posY);
1049 } else if (i->type() == RECTITEM) {
1051 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
1054 if (origin_x_left->isChecked()) {
1055 posX = m_frameWidth - value_x->value() - rec->rect().width();
1057 posX = value_x->value();
1061 if (origin_y_top->isChecked()) {
1062 posY = m_frameHeight - value_y->value() - rec->rect().height();
1064 posY = value_y->value();
1067 rec->setPos(posX, posY);
1069 } else if (i->type() == IMAGEITEM) {
1071 if (origin_x_left->isChecked()) {
1072 // Use the sceneBoundingRect because this also regards transformations like zoom
1073 posX = m_frameWidth - value_x->value() - i->sceneBoundingRect().width();
1075 posX = value_x->value();
1079 if (origin_y_top->isChecked()) {
1080 posY = m_frameHeight - value_y->value() - i->sceneBoundingRect().height();
1082 posY = value_y->value();
1085 i->setPos(posX, posY);
1091 void TitleWidget::updateTextOriginX()
1093 if (origin_x_left->isChecked()) {
1094 origin_x_left->setText(i18n("\u2212X"));
1096 origin_x_left->setText(i18n("+X"));
1100 void TitleWidget::slotOriginXClicked()
1102 // Update the text displayed on the button.
1103 updateTextOriginX();
1105 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1106 if (l.size() >= 1) {
1107 updateCoordinates(l.at(0));
1109 // Remember x axis setting
1110 l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked() ?
1111 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1113 graphicsView->setFocus();
1116 void TitleWidget::updateTextOriginY()
1118 if (origin_y_top->isChecked()) {
1119 origin_y_top->setText(i18n("\u2212Y"));
1121 origin_y_top->setText(i18n("+Y"));
1125 void TitleWidget::slotOriginYClicked()
1127 // Update the text displayed on the button.
1128 updateTextOriginY();
1130 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1131 if (l.size() >= 1) {
1132 updateCoordinates(l.at(0));
1134 l.at(0)->setData(TitleDocument::OriginYTop, origin_y_top->isChecked() ?
1135 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1138 graphicsView->setFocus();
1141 void TitleWidget::updateAxisButtons(QGraphicsItem *i)
1143 int xAxis = i->data(TitleDocument::OriginXLeft).toInt();
1144 int yAxis = i->data(TitleDocument::OriginYTop).toInt();
1145 origin_x_left->blockSignals(true);
1146 origin_y_top->blockSignals(true);
1148 if (xAxis == TitleDocument::AxisInverted) {
1149 origin_x_left->setChecked(true);
1151 origin_x_left->setChecked(false);
1153 updateTextOriginX();
1155 if (yAxis == TitleDocument::AxisInverted) {
1156 origin_y_top->setChecked(true);
1158 origin_y_top->setChecked(false);
1160 updateTextOriginY();
1162 origin_x_left->blockSignals(false);
1163 origin_y_top->blockSignals(false);
1166 void TitleWidget::slotChangeBackground()
1168 QColor color = kcolorbutton->color();
1169 color.setAlpha(horizontalSlider->value());
1170 m_frameBorder->setBrush(QBrush(color));
1174 * Something (yeah) has changed in our QGraphicsScene.
1176 void TitleWidget::slotChanged()
1178 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1179 if (l.size() >= 1 && l.at(0)->type() == TEXTITEM) {
1180 textChanged(static_cast <QGraphicsTextItem *>(l.at(0)));
1185 * If the user has set origin_x_left (everything also for y),
1186 * we need to look whether a text element has been selected. If yes,
1187 * we need to ensure that the right border of the text field
1188 * remains fixed also when some text has been entered.
1190 * This is also known as right-justified, with the difference that
1191 * it is not valid for text but for its boundingRect. Text may still
1192 * be left-justified.
1194 void TitleWidget::textChanged(QGraphicsTextItem *i)
1199 if (origin_x_left->isChecked() || origin_y_top->isChecked()) {
1201 if (!i->toPlainText().isEmpty()) {
1205 * Don't do anything if the string is empty. If the position
1206 * would be updated here, a newly created text field would
1207 * be set to the position of the last selected text field.
1213 void TitleWidget::slotInsertUnicode()
1215 m_unicodeDialog->exec();
1218 void TitleWidget::slotInsertUnicodeString(QString text)
1220 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
1222 if (l.at(0)->type() == TEXTITEM) {
1223 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(l.at(0));
1224 t->textCursor().insertText(text);
1229 void TitleWidget::slotUpdateText()
1231 QFont font = font_family->currentFont();
1232 font.setPixelSize(font_size->value());
1233 font.setItalic(buttonItalic->isChecked());
1234 font.setUnderline(buttonUnder->isChecked());
1235 font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1236 QColor color = fontColorButton->color();
1237 color.setAlpha(textAlpha->value());
1239 QGraphicsTextItem* item = NULL;
1240 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1241 if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
1242 item = static_cast <QGraphicsTextItem *>(l.at(0));
1245 //if (item->textCursor().selection ().isEmpty())
1246 QTextCursor cur = item->textCursor();
1247 QTextBlockFormat format = cur.blockFormat();
1248 if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
1249 item->setTextWidth(item->boundingRect().width());
1250 if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
1251 else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
1252 else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
1254 format.setAlignment(Qt::AlignLeft);
1255 item->setTextWidth(-1);
1259 item->setFont(font);
1260 item->setDefaultTextColor(color);
1261 cur.select(QTextCursor::Document);
1262 cur.setBlockFormat(format);
1263 item->setTextCursor(cur);
1264 cur.clearSelection();
1265 item->setTextCursor(cur);
1270 void TitleWidget::rectChanged()
1272 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1273 if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
1274 QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
1275 QColor f = rectFColor->color();
1276 f.setAlpha(rectFAlpha->value());
1278 penf.setWidth(rectLineWidth->value());
1280 QColor b = rectBColor->color();
1281 b.setAlpha(rectBAlpha->value());
1282 rec->setBrush(QBrush(b));
1286 void TitleWidget::itemScaled(int val)
1288 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1289 if (l.size() == 1) {
1290 Transform x = m_transformations.value(l.at(0));
1291 x.scalex = (double)val / 100.0;
1292 x.scaley = (double)val / 100.0;
1294 qtrans.scale(x.scalex, x.scaley);
1295 qtrans.rotate(x.rotate);
1296 l[0]->setTransform(qtrans);
1297 m_transformations[l.at(0)] = x;
1298 updateDimension(l.at(0));
1302 void TitleWidget::itemRotate(int val)
1304 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1305 if (l.size() == 1) {
1306 Transform x = m_transformations[l.at(0)];
1307 x.rotate = (double)val;
1309 qtrans.scale(x.scalex, x.scaley);
1310 qtrans.rotate(x.rotate);
1311 l[0]->setTransform(qtrans);
1312 m_transformations[l.at(0)] = x;
1313 updateDimension(l.at(0));
1317 void TitleWidget::itemHCenter()
1319 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1320 if (l.size() == 1) {
1321 QGraphicsItem *item = l.at(0);
1322 QRectF br = item->sceneBoundingRect();
1323 int width = (int)br.width();
1324 int newPos = (int)((m_frameWidth - width) / 2);
1325 newPos += item->pos().x() - br.left(); // Check item transformation
1326 item->setPos(newPos, item->pos().y());
1327 updateCoordinates(item);
1331 void TitleWidget::itemVCenter()
1333 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1334 if (l.size() == 1) {
1335 QGraphicsItem *item = l.at(0);
1336 QRectF br = item->sceneBoundingRect();
1337 int height = (int)br.height();
1338 int newPos = (int)((m_frameHeight - height) / 2);
1339 newPos += item->pos().y() - br.top(); // Check item transformation
1340 item->setPos(item->pos().x(), newPos);
1341 updateCoordinates(item);
1345 void TitleWidget::itemTop()
1347 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1348 if (l.size() == 1) {
1349 QGraphicsItem *item = l.at(0);
1350 QRectF br = item->sceneBoundingRect();
1352 if (br.top() > 0) diff = -br.top();
1353 else diff = -br.bottom();
1354 item->moveBy(0, diff);
1355 updateCoordinates(item);
1359 void TitleWidget::itemBottom()
1361 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1362 if (l.size() == 1) {
1363 QGraphicsItem *item = l.at(0);
1364 QRectF br = item->sceneBoundingRect();
1366 if (br.bottom() > m_frameHeight) diff = m_frameHeight - br.top();
1367 else diff = m_frameHeight - br.bottom();
1368 item->moveBy(0, diff);
1369 updateCoordinates(item);
1373 void TitleWidget::itemLeft()
1375 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1376 if (l.size() == 1) {
1377 QGraphicsItem *item = l.at(0);
1378 QRectF br = item->sceneBoundingRect();
1380 if (br.left() > 0) diff = -br.left();
1381 else diff = -br.right();
1382 item->moveBy(diff, 0);
1383 updateCoordinates(item);
1387 void TitleWidget::itemRight()
1389 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1390 if (l.size() == 1) {
1391 QGraphicsItem *item = l.at(0);
1392 QRectF br = item->sceneBoundingRect();
1394 if (br.right() < m_frameWidth) diff = m_frameWidth - br.right();
1395 else diff = m_frameWidth - br.left();
1396 item->moveBy(diff, 0);
1397 updateCoordinates(item);
1401 void TitleWidget::setupViewports()
1403 //double aspect_ratio = 4.0 / 3.0;//read from project
1404 //better zoom centered, but render uses only the created rect, so no problem to change the zoom function
1405 /*QRectF sp(0, 0, startViewportSize->value() * m_frameWidth / 100.0 , startViewportSize->value()* m_frameHeight / 100.0);
1406 QRectF ep(0, 0, endViewportSize->value() * m_frameWidth / 100.0, endViewportSize->value() * m_frameHeight / 100.0);
1407 // use a polygon thiat uses 16:9 and 4:3 rects forpreview the size in all aspect ratios ?
1408 QPolygonF spoly(sp);
1409 QPolygonF epoly(ep);
1410 spoly.translate(startViewportX->value(), startViewportY->value());
1411 epoly.translate(endViewportX->value(), endViewportY->value());
1412 m_startViewport->setPolygon(spoly);
1413 m_endViewport->setPolygon(epoly);
1414 if (! insertingValues) {
1415 m_startViewport->setData(0, startViewportX->value());
1416 m_startViewport->setData(1, startViewportY->value());
1417 m_startViewport->setData(2, startViewportSize->value());
1419 m_endViewport->setData(0, endViewportX->value());
1420 m_endViewport->setData(1, endViewportY->value());
1421 m_endViewport->setData(2, endViewportSize->value());
1425 void TitleWidget::loadTitle(KUrl url)
1427 if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this, i18n("Load Title"));
1428 if (!url.isEmpty()) {
1429 QList<QGraphicsItem *> items = m_scene->items();
1430 for (int i = 0; i < items.size(); i++) {
1431 if (items.at(i)->zValue() > -1000) delete items.at(i);
1433 m_scene->clearTextSelection();
1437 if (KIO::NetAccess::download(url, tmpfile, 0)) {
1438 QFile file(tmpfile);
1439 if (file.open(QIODevice::ReadOnly)) {
1440 doc.setContent(&file, false);
1443 KIO::NetAccess::removeTempFile(tmpfile);
1448 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport, &out) + 1;
1450 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1451 insertingValues = true;
1452 startViewportX->setValue(m_startViewport->data(0).toInt());
1453 startViewportY->setValue(m_startViewport->data(1).toInt());
1454 startViewportSize->setValue(m_startViewport->data(2).toInt());
1455 endViewportX->setValue(m_endViewport->data(0).toInt());
1456 endViewportY->setValue(m_endViewport->data(1).toInt());
1457 endViewportSize->setValue(m_endViewport->data(2).toInt());
1459 insertingValues = false;
1465 void TitleWidget::saveTitle(KUrl url)
1467 if (anim_start->isChecked()) slotAnimStart(false);
1468 if (anim_end->isChecked()) slotAnimEnd(false);
1469 if (url.isEmpty()) {
1470 KFileDialog *fs = new KFileDialog(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this);
1471 fs->setOperationMode(KFileDialog::Saving);
1472 fs->setMode(KFile::File);
1473 #if KDE_IS_VERSION(4,2,0)
1474 fs->setConfirmOverwrite(true);
1476 fs->setKeepLocation(true);
1478 url = fs->selectedUrl();
1481 if (!url.isEmpty()) {
1482 if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport, m_tc.getFrameCount(title_duration->text())) == false)
1483 KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
1487 QDomDocument TitleWidget::xml()
1489 QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
1490 doc.documentElement().setAttribute("out", m_tc.getFrameCount(title_duration->text()));
1494 int TitleWidget::duration() const
1496 return m_tc.getFrameCount(title_duration->text());
1499 void TitleWidget::setXml(QDomDocument doc)
1502 m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport, &out);
1504 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1505 /*if (doc.documentElement().hasAttribute("out")) {
1506 GenTime duration = GenTime(doc.documentElement().attribute("out").toDouble() / 1000.0);
1507 title_duration->setText(m_tc.getTimecode(duration));
1509 else title_duration->setText(m_tc.getTimecode(GenTime(5000)));*/
1511 QDomElement e = doc.documentElement();
1512 m_transformations.clear();
1513 QList <QGraphicsItem *> items = graphicsView->scene()->items();
1514 const double PI = 4.0 * atan(1.0);
1515 for (int i = 0; i < items.count(); i++) {
1516 QTransform t = items.at(i)->transform();
1520 x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
1521 m_transformations[items.at(i)] = x;
1523 // mbd: Update the GUI color selectors to match the stuff from the loaded document
1524 QColor background_color = m_titledocument.getBackgroundColor();
1525 horizontalSlider->blockSignals(true);
1526 kcolorbutton->blockSignals(true);
1527 horizontalSlider->setValue(background_color.alpha());
1528 background_color.setAlpha(255);
1529 kcolorbutton->setColor(background_color);
1530 horizontalSlider->blockSignals(false);
1531 kcolorbutton->blockSignals(false);
1533 /*startViewportX->setValue(m_startViewport->data(0).toInt());
1534 startViewportY->setValue(m_startViewport->data(1).toInt());
1535 startViewportSize->setValue(m_startViewport->data(2).toInt());
1536 endViewportX->setValue(m_endViewport->data(0).toInt());
1537 endViewportY->setValue(m_endViewport->data(1).toInt());
1538 endViewportSize->setValue(m_endViewport->data(2).toInt());*/
1540 QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
1544 /** \brief Connected to the accepted signal - calls writeChoices */
1545 void TitleWidget::slotAccepted()
1547 if (anim_start->isChecked()) slotAnimStart(false);
1548 if (anim_end->isChecked()) slotAnimEnd(false);
1552 /** \brief Store the current choices of font, background and rect values */
1553 void TitleWidget::writeChoices()
1555 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1556 KSharedConfigPtr config = KGlobal::config();
1557 KConfigGroup titleConfig(config, "TitleWidget");
1558 // Write the entries
1559 titleConfig.writeEntry("font_family", font_family->currentFont());
1560 //titleConfig.writeEntry("font_size", font_size->value());
1561 titleConfig.writeEntry("font_pixel_size", font_size->value());
1562 titleConfig.writeEntry("font_color", fontColorButton->color());
1563 titleConfig.writeEntry("font_alpha", textAlpha->value());
1564 titleConfig.writeEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1565 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
1566 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
1568 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
1569 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
1570 titleConfig.writeEntry("rect_background_color", rectBColor->color());
1571 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
1572 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
1574 titleConfig.writeEntry("background_color", kcolorbutton->color());
1575 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
1577 //! \todo Not sure if I should sync - it is probably safe to do it
1582 /** \brief Read the last stored choices into the dialog */
1583 void TitleWidget::readChoices()
1585 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1586 KSharedConfigPtr config = KGlobal::config();
1587 KConfigGroup titleConfig(config, "TitleWidget");
1589 font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
1590 font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
1591 fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
1592 textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
1594 if (titleConfig.readEntry("font_bold", false)) weight = QFont::Bold;
1595 else weight = titleConfig.readEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1596 setFontBoxWeight(weight);
1597 buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
1598 buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
1600 rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
1601 rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
1602 rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
1603 rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
1604 rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
1606 kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
1607 horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
1610 void TitleWidget::adjustFrameSize()
1612 m_frameWidth = m_titledocument.frameWidth();
1613 m_frameHeight = m_titledocument.frameHeight();
1614 m_frameBorder->setRect(0, 0, m_frameWidth, m_frameHeight);
1615 displayBackgroundFrame();
1618 void TitleWidget::slotAnimStart(bool anim)
1620 if (anim && anim_end->isChecked()) {
1621 anim_end->setChecked(false);
1622 m_endViewport->setZValue(-1000);
1623 m_endViewport->setBrush(QBrush());
1626 QList<QGraphicsItem *> list = m_scene->items();
1627 for (int i = 0; i < list.count(); i++) {
1628 if (list.at(i)->zValue() > -1000) {
1629 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1630 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1633 align_box->setEnabled(anim);
1634 itemzoom->setEnabled(!anim);
1635 itemrotate->setEnabled(!anim);
1636 frame_toolbar->setEnabled(!anim);
1637 toolbar_stack->setEnabled(!anim);
1639 keep_aspect->setChecked(!m_startViewport->data(0).isNull());
1640 m_startViewport->setZValue(1100);
1641 QColor col = m_startViewport->pen().color();
1643 m_startViewport->setBrush(col);
1644 m_startViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1645 m_startViewport->setSelected(true);
1648 if (m_startViewport->childItems().isEmpty()) addAnimInfoText();
1650 m_startViewport->setZValue(-1000);
1651 m_startViewport->setBrush(QBrush());
1652 m_startViewport->setFlags(0);
1653 if (!anim_end->isChecked()) deleteAnimInfoText();
1658 void TitleWidget::slotAnimEnd(bool anim)
1660 if (anim && anim_start->isChecked()) {
1661 anim_start->setChecked(false);
1662 m_startViewport->setZValue(-1000);
1663 m_startViewport->setBrush(QBrush());
1666 QList<QGraphicsItem *> list = m_scene->items();
1667 for (int i = 0; i < list.count(); i++) {
1668 if (list.at(i)->zValue() > -1000) {
1669 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1670 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1673 align_box->setEnabled(anim);
1674 itemzoom->setEnabled(!anim);
1675 itemrotate->setEnabled(!anim);
1676 frame_toolbar->setEnabled(!anim);
1677 toolbar_stack->setEnabled(!anim);
1679 keep_aspect->setChecked(!m_endViewport->data(0).isNull());
1680 m_endViewport->setZValue(1100);
1681 QColor col = m_endViewport->pen().color();
1683 m_endViewport->setBrush(col);
1684 m_endViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1685 m_endViewport->setSelected(true);
1688 if (m_endViewport->childItems().isEmpty()) addAnimInfoText();
1690 m_endViewport->setZValue(-1000);
1691 m_endViewport->setBrush(QBrush());
1692 m_endViewport->setFlags(0);
1693 if (!anim_start->isChecked()) deleteAnimInfoText();
1697 void TitleWidget::addAnimInfoText()
1699 // add text to anim viewport
1700 QGraphicsTextItem *t = new QGraphicsTextItem(i18n("Start"), m_startViewport);
1701 QGraphicsTextItem *t2 = new QGraphicsTextItem(i18n("End"), m_endViewport);
1702 QFont font = t->font();
1703 font.setPixelSize(m_startViewport->rect().width() / 10);
1704 QColor col = m_startViewport->pen().color();
1706 t->setDefaultTextColor(col);
1708 font.setPixelSize(m_endViewport->rect().width() / 10);
1709 col = m_endViewport->pen().color();
1711 t2->setDefaultTextColor(col);
1715 void TitleWidget::updateInfoText()
1717 // update info text font
1718 if (!m_startViewport->childItems().isEmpty()) {
1719 QGraphicsTextItem *item = static_cast <QGraphicsTextItem *>(m_startViewport->childItems().at(0));
1721 QFont font = item->font();
1722 font.setPixelSize(m_startViewport->rect().width() / 10);
1723 item->setFont(font);
1726 if (!m_endViewport->childItems().isEmpty()) {
1727 QGraphicsTextItem *item = static_cast <QGraphicsTextItem *>(m_endViewport->childItems().at(0));
1729 QFont font = item->font();
1730 font.setPixelSize(m_endViewport->rect().width() / 10);
1731 item->setFont(font);
1736 void TitleWidget::deleteAnimInfoText()
1738 // end animation editing, remove info text
1739 while (!m_startViewport->childItems().isEmpty()) {
1740 QGraphicsItem *item = m_startViewport->childItems().at(0);
1741 m_scene->removeItem(item);
1744 while (!m_endViewport->childItems().isEmpty()) {
1745 QGraphicsItem *item = m_endViewport->childItems().at(0);
1746 m_scene->removeItem(item);
1752 void TitleWidget::slotKeepAspect(bool keep)
1754 if (m_endViewport->zValue() == 1100) {
1755 m_endViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1756 m_endViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1758 m_startViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1759 m_startViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1763 void TitleWidget::slotResize50()
1765 if (m_endViewport->zValue() == 1100) {
1766 m_endViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1767 } else m_startViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1770 void TitleWidget::slotResize100()
1772 if (m_endViewport->zValue() == 1100) {
1773 m_endViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1774 } else m_startViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1777 void TitleWidget::slotResize200()
1779 if (m_endViewport->zValue() == 1100) {
1780 m_endViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);
1781 } else m_startViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);
1784 void TitleWidget::slotAddEffect(int ix)
1786 int effect = effect_list->itemData(ix).toInt();
1788 effect_stack->setHidden(true);
1791 effect_stack->setCurrentIndex(effect - 1);
1792 effect_stack->setHidden(false);
1793 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1794 if (effect == TYPEWRITEREFFECT) {
1795 if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
1796 QStringList effdata = QStringList() << "typewriter" << QString::number(typewriter_delay->value());
1797 l[0]->setData(100, effdata);
1800 #if QT_VERSION < 0x040600
1803 if (effect == BLUREFFECT) {
1805 if (l.size() == 1) {
1806 QGraphicsEffect *eff = new QGraphicsBlurEffect();
1807 l[0]->setGraphicsEffect(eff);
1809 } else if (effect == SHADOWEFFECT) {
1810 if (l.size() == 1) {
1811 QGraphicsEffect *eff = new QGraphicsDropShadowEffect();
1812 l[0]->setGraphicsEffect(eff);
1820 void TitleWidget::slotFontText(const QString& s)
1823 if (f.exactMatch()) {
1824 // Font really exists (could also just be a «d» if the user
1825 // starts typing «dejavu» for example).
1826 font_family->setCurrentFont(f);
1828 // TODO: typing dejavu serif does not recognize the font (takes sans).
1829 // upper/lowercase problem?
1832 void TitleWidget::slotEditTypewriter(int ix)
1834 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1835 if (l.size() == 1) {
1836 QStringList effdata = QStringList() << "typewriter" << QString::number(typewriter_delay->value());
1837 l[0]->setData(100, effdata);
1841 void TitleWidget::slotEditBlur(int ix)
1843 #if QT_VERSION < 0x040600
1846 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1847 if (l.size() == 1) {
1848 QGraphicsEffect *eff = l[0]->graphicsEffect();
1849 QGraphicsBlurEffect *blur = static_cast <QGraphicsBlurEffect *>(eff);
1850 if (blur) blur->setBlurRadius(ix);
1855 void TitleWidget::slotEditShadow()
1857 #if QT_VERSION < 0x040600
1860 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1861 if (l.size() == 1) {
1862 QGraphicsEffect *eff = l[0]->graphicsEffect();
1863 QGraphicsDropShadowEffect *shadow = static_cast <QGraphicsDropShadowEffect *>(eff);
1865 shadow->setBlurRadius(shadow_radius->value());
1866 shadow->setOffset(shadow_x->value(), shadow_y->value());