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);
109 effect_list->addItem(i18n("Blur"), BLUREFFECT);
112 connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
113 connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
114 connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
115 connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
116 connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
117 connect(itemtop, SIGNAL(clicked()), this, SLOT(itemTop()));
118 connect(itembottom, SIGNAL(clicked()), this, SLOT(itemBottom()));
119 connect(itemleft, SIGNAL(clicked()), this, SLOT(itemLeft()));
120 connect(itemright, SIGNAL(clicked()), this, SLOT(itemRight()));
121 connect(effect_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotAddEffect(int)));
122 connect(typewriter_delay, SIGNAL(valueChanged(int)), this, SLOT(slotEditTypewriter(int)));
123 connect(typewriter_start, SIGNAL(valueChanged(int)), this, SLOT(slotEditTypewriter(int)));
124 connect(blur_radius, SIGNAL(valueChanged(int)), this, SLOT(slotEditBlur(int)));
125 connect(shadow_radius, SIGNAL(valueChanged(int)), this, SLOT(slotEditShadow()));
126 connect(shadow_x, SIGNAL(valueChanged(int)), this, SLOT(slotEditShadow()));
127 connect(shadow_y, SIGNAL(valueChanged(int)), this, SLOT(slotEditShadow()));
128 effect_stack->setHidden(true);
129 effect_frame->setEnabled(false);
131 connect(origin_x_left, SIGNAL(clicked()), this, SLOT(slotOriginXClicked()));
132 connect(origin_y_top, SIGNAL(clicked()), this, SLOT(slotOriginYClicked()));
134 m_signalMapper = new QSignalMapper(this);
135 m_signalMapper->setMapping(value_w, ValueWidth);
136 m_signalMapper->setMapping(value_h, ValueHeight);
137 connect(value_w, SIGNAL(valueChanged(int)), m_signalMapper, SLOT(map()));
138 connect(value_h, SIGNAL(valueChanged(int)), m_signalMapper, SLOT(map()));
139 connect(m_signalMapper, SIGNAL(mapped(int)), this, SLOT(slotValueChanged(int)));
141 connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
142 connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
143 connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
144 connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
145 connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
146 connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
147 connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
148 connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
149 connect(buttonAlignLeft, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
150 connect(buttonAlignRight, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
151 connect(buttonAlignCenter, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
152 connect(buttonAlignNone, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
153 //connect(buttonInsertUnicode, SIGNAL(clicked()), this, SLOT(slotInsertUnicode()));
154 connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
156 connect(m_unicodeDialog, SIGNAL(charSelected(QString)), this, SLOT(slotInsertUnicodeString(QString)));
159 connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
161 font_weight_box->blockSignals(true);
162 font_weight_box->addItem(i18nc("Font style", "Light"), QFont::Light);
163 font_weight_box->addItem(i18nc("Font style", "Normal"), QFont::Normal);
164 font_weight_box->addItem(i18nc("Font style", "Demi-Bold"), QFont::DemiBold);
165 font_weight_box->addItem(i18nc("Font style", "Bold"), QFont::Bold);
166 font_weight_box->addItem(i18nc("Font style", "Black"), QFont::Black);
167 font_weight_box->setToolTip(i18n("Font weight"));
168 font_weight_box->setCurrentIndex(1);
169 font_weight_box->blockSignals(false);
171 buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
172 buttonRealSize->setIcon(KIcon("zoom-original"));
173 buttonItalic->setIcon(KIcon("format-text-italic"));
174 buttonUnder->setIcon(KIcon("format-text-underline"));
175 buttonAlignCenter->setIcon(KIcon("format-justify-center"));
176 buttonAlignLeft->setIcon(KIcon("format-justify-left"));
177 buttonAlignRight->setIcon(KIcon("format-justify-right"));
178 buttonAlignNone->setIcon(KIcon("kdenlive-align-none"));
180 buttonAlignNone->setToolTip(i18n("No alignment"));
181 buttonAlignRight->setToolTip(i18n("Align right"));
182 buttonAlignLeft->setToolTip(i18n("Align left"));
183 buttonAlignCenter->setToolTip(i18n("Align center"));
185 m_unicodeAction = new QAction(KIcon("kdenlive-insert-unicode"), QString(), this);
186 m_unicodeAction->setShortcut(Qt::SHIFT + Qt::CTRL + Qt::Key_U);
187 m_unicodeAction->setToolTip(i18n("Insert Unicode character") + ' ' + m_unicodeAction->shortcut().toString());
188 connect(m_unicodeAction, SIGNAL(triggered()), this, SLOT(slotInsertUnicode()));
189 buttonInsertUnicode->setDefaultAction(m_unicodeAction);
191 origin_x_left->setToolTip(i18n("Invert x axis and change 0 point"));
192 origin_y_top->setToolTip(i18n("Invert y axis and change 0 point"));
193 rectBColor->setToolTip(i18n("Select fill color"));
194 rectFColor->setToolTip(i18n("Select border color"));
195 rectBAlpha->setToolTip(i18n("Fill transparency"));
196 rectFAlpha->setToolTip(i18n("Border transparency"));
197 zoom_slider->setToolTip(i18n("Zoom"));
198 buttonRealSize->setToolTip(i18n("Original size (1:1)"));
199 buttonFitZoom->setToolTip(i18n("Fit zoom"));
200 kcolorbutton->setToolTip(i18n("Select background color"));
201 horizontalSlider->setToolTip(i18n("Background Transparency"));
203 itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
204 itemhcenter->setToolTip(i18n("Align item horizontally"));
205 itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
206 itemvcenter->setToolTip(i18n("Align item vertically"));
207 itemtop->setIcon(KIcon("kdenlive-align-top"));
208 itemtop->setToolTip(i18n("Align item to top"));
209 itembottom->setIcon(KIcon("kdenlive-align-bottom"));
210 itembottom->setToolTip(i18n("Align item to bottom"));
211 itemright->setIcon(KIcon("kdenlive-align-right"));
212 itemright->setToolTip(i18n("Align item to right"));
213 itemleft->setIcon(KIcon("kdenlive-align-left"));
214 itemleft->setToolTip(i18n("Align item to left"));
217 QHBoxLayout *layout = new QHBoxLayout;
218 frame_toolbar->setLayout(layout);
219 layout->setContentsMargins(0, 0, 0, 0);
220 QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
221 int s = style()->pixelMetric(QStyle::PM_SmallIconSize);
222 m_toolbar->setIconSize(QSize(s, s));
224 m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), QString());
225 m_buttonCursor->setCheckable(true);
226 m_buttonCursor->setShortcut(Qt::ALT + Qt::Key_S);
227 m_buttonCursor->setToolTip(i18n("Selection Tool") + ' ' + m_buttonCursor->shortcut().toString());
228 connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
230 m_buttonText = m_toolbar->addAction(KIcon("insert-text"), QString());
231 m_buttonText->setCheckable(true);
232 m_buttonText->setShortcut(Qt::ALT + Qt::Key_T);
233 m_buttonText->setToolTip(i18n("Add Text") + ' ' + m_buttonText->shortcut().toString());
234 connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
236 m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), QString());
237 m_buttonRect->setCheckable(true);
238 m_buttonRect->setShortcut(Qt::ALT + Qt::Key_R);
239 m_buttonRect->setToolTip(i18n("Add Rectangle") + ' ' + m_buttonRect->shortcut().toString());
240 connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
242 m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), QString());
243 m_buttonImage->setCheckable(false);
244 m_buttonImage->setShortcut(Qt::ALT + Qt::Key_I);
245 m_buttonImage->setToolTip(i18n("Add Image") + ' ' + m_buttonImage->shortcut().toString());
246 connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
248 m_toolbar->addSeparator();
250 m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
251 m_buttonLoad->setCheckable(false);
252 m_buttonLoad->setShortcut(Qt::CTRL + Qt::Key_O);
253 connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
255 m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
256 m_buttonSave->setCheckable(false);
257 m_buttonSave->setShortcut(Qt::CTRL + Qt::Key_S);
258 connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
260 layout->addWidget(m_toolbar);
262 // initialize graphic scene
263 m_scene = new GraphicsSceneRectMove(this);
264 graphicsView->setScene(m_scene);
265 m_titledocument.setScene(m_scene, m_frameWidth, m_frameHeight);
266 connect(m_scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(slotChanged()));
268 // a gradient background
269 /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
270 gradient->setSpread(QGradient::ReflectSpread);
271 scene->setBackgroundBrush(*gradient);*/
273 m_frameImage = new QGraphicsPixmapItem();
275 qtrans.scale(2.0, 2.0);
276 m_frameImage->setTransform(qtrans);
277 m_frameImage->setZValue(-1200);
278 m_frameImage->setFlags(0);
279 displayBackgroundFrame();
280 graphicsView->scene()->addItem(m_frameImage);
282 connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
283 connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
284 connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
285 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
286 //connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
287 connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
288 connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
289 connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
291 QPen framepen(Qt::DotLine);
292 framepen.setColor(Qt::red);
294 m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
295 m_frameBorder->setPen(framepen);
296 m_frameBorder->setZValue(-1100);
297 m_frameBorder->setBrush(Qt::transparent);
298 m_frameBorder->setFlags(0);
299 graphicsView->scene()->addItem(m_frameBorder);
301 // mbd: load saved settings
304 graphicsView->show();
305 //graphicsView->setRenderHint(QPainter::Antialiasing);
306 graphicsView->setInteractive(true);
307 //graphicsView->resize(400, 300);
308 kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
309 //toolBox->setItemEnabled(2, false);
310 m_startViewport = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
311 m_endViewport = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
312 m_startViewport->setData(0, m_frameWidth);
313 m_startViewport->setData(1, m_frameHeight);
314 m_endViewport->setData(0, m_frameWidth);
315 m_endViewport->setData(1, m_frameHeight);
317 if (!url.isEmpty()) loadTitle(url);
320 QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
323 connect(anim_start, SIGNAL(toggled(bool)), this, SLOT(slotAnimStart(bool)));
324 connect(anim_end, SIGNAL(toggled(bool)), this, SLOT(slotAnimEnd(bool)));
326 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(KdenliveSettings::hastitleproducer());
329 TitleWidget::~TitleWidget()
333 delete m_buttonImage;
334 delete m_buttonCursor;
337 delete m_unicodeAction;
339 delete m_unicodeDialog;
340 delete m_frameBorder;
342 delete m_startViewport;
343 delete m_endViewport;
345 delete m_signalMapper;
349 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl, bool isClone)
352 QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
353 KStandardDirs::makeDir(titlePath);
354 titlePath.append((isClone == false) ? "title" : "clone");
357 while (path.isEmpty() || QFile::exists(path)) {
359 path = titlePath + QString::number(counter).rightJustified(3, '0', false) + ".png";
361 result.append(((isClone == false) ? i18n("Title") : i18n("Clone")) + ' ' + QString::number(counter).rightJustified(3, '0', false));
367 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
370 QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
371 KStandardDirs::makeDir(titlePath);
372 return titlePath + titleName + ".png";
376 QStringList TitleWidget::extractImageList(QString xml)
379 if (xml.isEmpty()) return result;
382 QDomNodeList images = doc.elementsByTagName("content");
383 for (int i = 0; i < images.count(); i++) {
384 if (images.at(i).toElement().hasAttribute("url"))
385 result.append(images.at(i).toElement().attribute("url"));
392 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
397 void TitleWidget::slotTextTool()
399 m_scene->setTool(TITLE_TEXT);
400 showToolbars(TITLE_TEXT);
401 checkButton(TITLE_TEXT);
404 void TitleWidget::slotRectTool()
406 m_scene->setTool(TITLE_RECTANGLE);
407 showToolbars(TITLE_RECTANGLE);
408 checkButton(TITLE_RECTANGLE);
411 void TitleWidget::slotSelectTool()
413 m_scene->setTool(TITLE_SELECT);
415 // Find out which toolbars need to be shown, depending on selected item
416 TITLETOOL t = TITLE_SELECT;
417 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
419 switch (l.at(0)->type()) {
433 if (t == TITLE_RECTANGLE && (l.at(0) == m_endViewport || l.at(0) == m_startViewport)) {
434 //graphicsView->centerOn(l.at(0));
440 updateCoordinates(l.at(0));
441 updateDimension(l.at(0));
442 updateRotZoom(l.at(0));
445 checkButton(TITLE_SELECT);
448 void TitleWidget::slotImageTool()
450 // TODO: find a way to get a list of all supported image types...
451 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";
452 KUrl url = KFileDialog::getOpenUrl(KUrl(), allExtensions, this, i18n("Load Image")); //"*.svg *.png *.jpg *.jpeg *.gif *.raw"
453 if (!url.isEmpty()) {
454 if (url.path().endsWith(".svg")) {
455 QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
456 svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
457 svg->setZValue(m_count++);
458 svg->setData(Qt::UserRole, url.path());
459 graphicsView->scene()->addItem(svg);
461 QPixmap pix(url.path());
462 QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
463 image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
464 image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
465 image->setData(Qt::UserRole, url.path());
466 image->setZValue(m_count++);
467 graphicsView->scene()->addItem(image);
470 m_scene->setTool(TITLE_SELECT);
471 showToolbars(TITLE_SELECT);
472 checkButton(TITLE_SELECT);
475 void TitleWidget::showToolbars(TITLETOOL toolType)
477 toolbar_stack->setCurrentIndex((int) toolType);
480 void TitleWidget::enableToolbars(TITLETOOL toolType)
482 // TITLETOOL is defined in graphicsscenerectmove.h
484 if (toolType == TITLE_RECTANGLE || toolType == TITLE_IMAGE) enable = true;
485 value_w->setEnabled(enable);
486 value_h->setEnabled(enable);
489 void TitleWidget::checkButton(TITLETOOL toolType)
491 bool bSelect = false;
503 case TITLE_RECTANGLE:
513 m_buttonCursor->setChecked(bSelect);
514 m_buttonText->setChecked(bText);
515 m_buttonRect->setChecked(bRect);
516 m_buttonImage->setChecked(bImage);
519 void TitleWidget::displayBackgroundFrame()
521 if (!displayBg->isChecked()) {
522 QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
523 QPixmap pattern(20, 20);
525 QColor bgcolor(210, 210, 210);
528 p.fillRect(QRect(0, 0, 10, 10), bgcolor);
529 p.fillRect(QRect(10, 10, 20, 20), bgcolor);
534 p.fillRect(bg.rect(), br);
536 m_frameImage->setPixmap(bg);
538 m_frameImage->setPixmap(QPixmap::fromImage(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2)));
542 void TitleWidget::initAnimation()
544 align_box->setEnabled(false);
545 QPen startpen(Qt::DotLine);
546 QPen endpen(Qt::DashDotLine);
547 startpen.setColor(QColor(100, 200, 100, 140));
548 endpen.setColor(QColor(200, 100, 100, 140));
550 m_startViewport->setPen(startpen);
551 m_endViewport->setPen(endpen);
553 m_startViewport->setZValue(-1000);
554 m_endViewport->setZValue(-1000);
556 m_startViewport->setFlags(0);
557 m_endViewport->setFlags(0);
559 graphicsView->scene()->addItem(m_startViewport);
560 graphicsView->scene()->addItem(m_endViewport);
562 connect(keep_aspect, SIGNAL(toggled(bool)), this, SLOT(slotKeepAspect(bool)));
563 connect(resize50, SIGNAL(clicked()), this, SLOT(slotResize50()));
564 connect(resize100, SIGNAL(clicked()), this, SLOT(slotResize100()));
565 connect(resize200, SIGNAL(clicked()), this, SLOT(slotResize200()));
568 void TitleWidget::slotUpdateZoom(int pos)
570 m_scene->setZoom((double) pos / 100);
571 zoom_label->setText(QString::number(pos) + '%');
574 void TitleWidget::slotZoom(bool up)
576 int pos = zoom_slider->value();
579 zoom_slider->setValue(pos);
582 void TitleWidget::slotAdjustZoom()
584 /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
585 double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
586 if (scalex > scaley) scalex = scaley;
587 int zoompos = (int)(scalex * 7 + 0.5);*/
588 graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
589 int zoompos = graphicsView->matrix().m11() * 100;
590 zoom_slider->setValue(zoompos);
591 graphicsView->centerOn(m_frameBorder);
594 void TitleWidget::slotZoomOneToOne()
596 zoom_slider->setValue(100);
597 graphicsView->centerOn(m_frameBorder);
600 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
602 updateAxisButtons(rect); // back to default
604 QColor f = rectFColor->color();
605 f.setAlpha(rectFAlpha->value());
607 penf.setWidth(rectLineWidth->value());
609 QColor b = rectBColor->color();
610 b.setAlpha(rectBAlpha->value());
611 rect->setBrush(QBrush(b));
612 rect->setZValue(m_count++);
613 //setCurrentItem(rect);
614 //graphicsView->setFocus();
617 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
619 updateAxisButtons(tt); // back to default
621 QFont font = font_family->currentFont();
622 font.setPixelSize(font_size->value());
624 font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
625 font.setItalic(buttonItalic->isChecked());
626 font.setUnderline(buttonUnder->isChecked());
629 QColor color = fontColorButton->color();
630 color.setAlpha(textAlpha->value());
631 tt->setDefaultTextColor(color);
632 tt->setZValue(m_count++);
636 void TitleWidget::setFontBoxWeight(int weight)
638 int index = font_weight_box->findData(weight);
640 index = font_weight_box->findData(QFont::Normal);
642 font_weight_box->setCurrentIndex(index);
645 void TitleWidget::setCurrentItem(QGraphicsItem *item)
647 m_scene->setSelectedItem(item);
650 void TitleWidget::zIndexChanged(int v)
652 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
658 void TitleWidget::selectionChanged()
660 if (m_scene->tool() != TITLE_SELECT) return;
661 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
662 //toolBox->setItemEnabled(2, false);
663 //toolBox->setItemEnabled(3, false);
664 value_x->blockSignals(true);
665 value_y->blockSignals(true);
666 value_w->blockSignals(true);
667 value_h->blockSignals(true);
668 itemzoom->blockSignals(true);
669 itemrotate->blockSignals(true);
671 effect_stack->setHidden(true);
672 effect_frame->setEnabled(false);
673 effect_list->setCurrentIndex(0);
674 bool blockX = !origin_x_left->signalsBlocked();
675 bool blockY = !origin_y_top->signalsBlocked();
676 if (blockX) origin_x_left->blockSignals(true);
677 if (blockY) origin_y_top->blockSignals(true);
678 origin_x_left->setChecked(false);
679 origin_y_top->setChecked(false);
682 enableToolbars(TITLE_SELECT);
683 if (blockX) origin_x_left->blockSignals(false);
684 if (blockY) origin_y_top->blockSignals(false);
685 itemzoom->setEnabled(false);
686 itemrotate->setEnabled(false);
687 frame_properties->setEnabled(false);
688 } else if (l.size() == 1) {
689 effect_frame->setEnabled(true);
690 frame_properties->setEnabled(true);
691 if (l.at(0) != m_startViewport && l.at(0) != m_endViewport) {
692 itemzoom->setEnabled(true);
693 itemrotate->setEnabled(true);
695 itemzoom->setEnabled(false);
696 itemrotate->setEnabled(false);
699 if (l.at(0)->type() == TEXTITEM) {
700 showToolbars(TITLE_TEXT);
701 QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
702 if (!i->data(100).isNull()) {
703 // Item has an effect
704 QStringList effdata = i->data(100).toStringList();
705 QString effectName = effdata.takeFirst();
706 if (effectName == "typewriter") {
707 QStringList params = effdata.at(0).split(';');
708 typewriter_delay->setValue(params.at(0).toInt());
709 typewriter_start->setValue(params.at(1).toInt());
710 effect_list->setCurrentIndex(effect_list->findData((int) TYPEWRITEREFFECT));
711 effect_stack->setHidden(false);
714 if (i->graphicsEffect()) {
715 QGraphicsBlurEffect *blur = static_cast <QGraphicsBlurEffect *>(i->graphicsEffect());
717 effect_list->setCurrentIndex(effect_list->findData((int) BLUREFFECT));
718 int rad = (int) blur->blurRadius();
719 blur_radius->setValue(rad);
720 effect_stack->setHidden(false);
722 QGraphicsDropShadowEffect *shad = static_cast <QGraphicsDropShadowEffect *>(i->graphicsEffect());
724 effect_list->setCurrentIndex(effect_list->findData((int) SHADOWEFFECT));
725 shadow_radius->setValue(shad->blurRadius());
726 shadow_x->setValue(shad->xOffset());
727 shadow_y->setValue(shad->yOffset());
728 effect_stack->setHidden(false);
732 effect_list->blockSignals(true);
733 effect_list->setCurrentIndex(effect_list->findData((int) NOEFFECT));
734 effect_list->blockSignals(false);
735 effect_stack->setHidden(true);
738 //if (l[0]->hasFocus())
739 //toolBox->setCurrentIndex(0);
740 //toolBox->setItemEnabled(2, true);
741 font_size->blockSignals(true);
742 font_family->blockSignals(true);
743 font_weight_box->blockSignals(true);
744 buttonItalic->blockSignals(true);
745 buttonUnder->blockSignals(true);
746 fontColorButton->blockSignals(true);
747 textAlpha->blockSignals(true);
748 buttonAlignLeft->blockSignals(true);
749 buttonAlignRight->blockSignals(true);
750 buttonAlignNone->blockSignals(true);
751 buttonAlignCenter->blockSignals(true);
753 QFont font = i->font();
754 font_family->setCurrentFont(font);
755 font_size->setValue(font.pixelSize());
756 buttonItalic->setChecked(font.italic());
757 buttonUnder->setChecked(font.underline());
758 setFontBoxWeight(font.weight());
760 QColor color = i->defaultTextColor();
761 fontColorButton->setColor(color);
762 textAlpha->setValue(color.alpha());
764 QTextCursor cur = i->textCursor();
765 QTextBlockFormat format = cur.blockFormat();
766 if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
767 else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
768 else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
769 else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
771 font_size->blockSignals(false);
772 font_family->blockSignals(false);
773 font_weight_box->blockSignals(false);
774 buttonItalic->blockSignals(false);
775 buttonUnder->blockSignals(false);
776 fontColorButton->blockSignals(false);
777 textAlpha->blockSignals(false);
778 buttonAlignLeft->blockSignals(false);
779 buttonAlignRight->blockSignals(false);
780 buttonAlignNone->blockSignals(false);
781 buttonAlignCenter->blockSignals(false);
783 updateAxisButtons(i);
784 updateCoordinates(i);
786 enableToolbars(TITLE_TEXT);
788 } else if ((l.at(0))->type() == RECTITEM) {
789 showToolbars(TITLE_RECTANGLE);
791 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
792 if (rec == m_startViewport || rec == m_endViewport) {
793 /*toolBox->setCurrentIndex(3);
794 toolBox->widget(0)->setEnabled(false);
795 toolBox->widget(1)->setEnabled(false);*/
796 enableToolbars(TITLE_SELECT);
798 /*toolBox->widget(0)->setEnabled(true);
799 toolBox->widget(1)->setEnabled(true);
800 toolBox->setCurrentIndex(0);*/
801 //toolBox->setItemEnabled(3, true);
802 rectFAlpha->setValue(rec->pen().color().alpha());
803 rectBAlpha->setValue(rec->brush().color().alpha());
804 //kDebug() << rec->brush().color().alpha();
805 QColor fcol = rec->pen().color();
806 QColor bcol = rec->brush().color();
807 //fcol.setAlpha(255);
808 //bcol.setAlpha(255);
809 rectFColor->setColor(fcol);
810 rectBColor->setColor(bcol);
812 rectLineWidth->setValue(rec->pen().width());
813 enableToolbars(TITLE_RECTANGLE);
816 updateAxisButtons(l.at(0));
817 updateCoordinates(rec);
818 updateDimension(rec);
820 } else if (l.at(0)->type() == IMAGEITEM) {
821 showToolbars(TITLE_IMAGE);
823 updateCoordinates(l.at(0));
824 updateDimension(l.at(0));
826 enableToolbars(TITLE_IMAGE);
829 //toolBox->setCurrentIndex(0);
830 showToolbars(TITLE_SELECT);
831 enableToolbars(TITLE_SELECT);
832 frame_properties->setEnabled(false);
834 zValue->setValue((int)l.at(0)->zValue());
835 itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
836 itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
837 value_x->blockSignals(false);
838 value_y->blockSignals(false);
839 value_w->blockSignals(false);
840 value_h->blockSignals(false);
841 itemzoom->blockSignals(false);
842 itemrotate->blockSignals(false);
846 void TitleWidget::slotValueChanged(int type)
848 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
849 if (l.size() > 0 && l.at(0)->type() == IMAGEITEM) {
854 val = value_w->value();
857 val = value_h->value();
861 QGraphicsItem *i = l.at(0);
862 Transform t = m_transformations.value(i);
864 // Ratio width:height
865 double phi = (double) i->boundingRect().width() / i->boundingRect().height();
866 double alpha = (double) t.rotate / 180.0 * M_PI;
876 // Add 0.5 because otherwise incrementing by 1 might have no effect
877 length = val / (cos(alpha) + 1 / phi * sin(alpha)) + 0.5;
878 scale = length / i->boundingRect().width();
881 length = val / (phi * sin(alpha) + cos(alpha)) + 0.5;
882 scale = length / i->boundingRect().height();
889 qtrans.scale(scale, scale);
890 qtrans.rotate(t.rotate);
891 i->setTransform(qtrans);
892 m_transformations[i] = t;
899 /** \brief Updates position/size of the selected item when a value
900 * of an item (coordinates, size) has changed */
901 void TitleWidget::slotAdjustSelectedItem()
903 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
905 if (l.at(0)->type() == RECTITEM) {
907 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
909 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
910 } else if (l.at(0)->type() == TEXTITEM) {
912 updatePosition(l.at(0));
913 } else if (l.at(0)->type() == IMAGEITEM) {
915 updatePosition(l.at(0));
920 /** \brief Updates width/height int the text fields, regarding transformation matrix */
921 void TitleWidget::updateDimension(QGraphicsItem *i)
923 value_w->blockSignals(true);
924 value_h->blockSignals(true);
927 if (i->type() == IMAGEITEM) {
928 // Get multipliers for rotation/scaling
930 /*Transform t = m_transformations.value(i);
931 QRectF r = i->boundingRect();
932 int width = (int) ( abs(r.width()*t.scalex * cos(t.rotate/180.0*M_PI))
933 + abs(r.height()*t.scaley * sin(t.rotate/180.0*M_PI)) );
934 int height = (int) ( abs(r.height()*t.scaley * cos(t.rotate/180*M_PI))
935 + abs(r.width()*t.scalex * sin(t.rotate/180*M_PI)) );*/
937 value_w->setValue(i->sceneBoundingRect().width());
938 value_h->setValue(i->sceneBoundingRect().height());
939 } else if (i->type() == RECTITEM) {
940 QGraphicsRectItem *r = static_cast <QGraphicsRectItem *>(i);
941 value_w->setValue((int) r->rect().width());
942 value_h->setValue((int) r->rect().height());
943 } else if (i->type() == TEXTITEM) {
944 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(i);
945 value_w->setValue((int) t->boundingRect().width());
946 value_h->setValue((int) t->boundingRect().height());
949 value_w->blockSignals(false);
950 value_h->blockSignals(false);
953 /** \brief Updates the coordinates in the text fields from the item */
954 void TitleWidget::updateCoordinates(QGraphicsItem *i)
956 // Block signals emitted by this method
957 value_x->blockSignals(true);
958 value_y->blockSignals(true);
960 if (i->type() == TEXTITEM) {
962 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
964 // Set the correct x coordinate value
965 if (origin_x_left->isChecked()) {
966 // Origin (0 point) is at m_frameWidth, coordinate axis is inverted
967 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->boundingRect().width()));
969 // Origin is at 0 (default)
970 value_x->setValue((int) rec->pos().x());
974 if (origin_y_top->isChecked()) {
975 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->boundingRect().height()));
977 value_y->setValue((int) rec->pos().y());
980 } else if (i->type() == RECTITEM) {
982 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
984 if (origin_x_left->isChecked()) {
985 // Origin (0 point) is at m_frameWidth
986 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->rect().width()));
988 // Origin is at 0 (default)
989 value_x->setValue((int) rec->pos().x());
992 if (origin_y_top->isChecked()) {
993 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->rect().height()));
995 value_y->setValue((int) rec->pos().y());
998 } else if (i->type() == IMAGEITEM) {
1000 if (origin_x_left->isChecked()) {
1001 value_x->setValue((int)(m_frameWidth - i->pos().x() - i->sceneBoundingRect().width()));
1003 value_x->setValue((int) i->pos().x());
1006 if (origin_y_top->isChecked()) {
1007 value_y->setValue((int)(m_frameHeight - i->pos().y() - i->sceneBoundingRect().height()));
1009 value_y->setValue((int) i->pos().y());
1014 // Stop blocking signals now
1015 value_x->blockSignals(false);
1016 value_y->blockSignals(false);
1019 void TitleWidget::updateRotZoom(QGraphicsItem *i)
1021 itemzoom->blockSignals(true);
1022 itemrotate->blockSignals(false);
1024 Transform t = m_transformations.value(i);
1025 itemzoom->setValue((int)(t.scalex * 100.0 + 0.5));
1026 itemrotate->setValue((int)(t.rotate));
1028 itemzoom->blockSignals(false);
1029 itemrotate->blockSignals(false);
1032 /** \brief Updates the position of an item by reading coordinates from the text fields */
1033 void TitleWidget::updatePosition(QGraphicsItem *i)
1035 if (i->type() == TEXTITEM) {
1036 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
1039 if (origin_x_left->isChecked()) {
1040 /* Origin of the x axis is at m_frameWidth,
1041 * and distance from right border of the item to the right
1042 * border of the frame is taken.
1043 * See comment to slotOriginXClicked().
1045 posX = m_frameWidth - value_x->value() - rec->boundingRect().width();
1047 posX = value_x->value();
1051 if (origin_y_top->isChecked()) {
1052 /* Same for y axis */
1053 posY = m_frameHeight - value_y->value() - rec->boundingRect().height();
1055 posY = value_y->value();
1058 rec->setPos(posX, posY);
1060 } else if (i->type() == RECTITEM) {
1062 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
1065 if (origin_x_left->isChecked()) {
1066 posX = m_frameWidth - value_x->value() - rec->rect().width();
1068 posX = value_x->value();
1072 if (origin_y_top->isChecked()) {
1073 posY = m_frameHeight - value_y->value() - rec->rect().height();
1075 posY = value_y->value();
1078 rec->setPos(posX, posY);
1080 } else if (i->type() == IMAGEITEM) {
1082 if (origin_x_left->isChecked()) {
1083 // Use the sceneBoundingRect because this also regards transformations like zoom
1084 posX = m_frameWidth - value_x->value() - i->sceneBoundingRect().width();
1086 posX = value_x->value();
1090 if (origin_y_top->isChecked()) {
1091 posY = m_frameHeight - value_y->value() - i->sceneBoundingRect().height();
1093 posY = value_y->value();
1096 i->setPos(posX, posY);
1102 void TitleWidget::updateTextOriginX()
1104 if (origin_x_left->isChecked()) {
1105 origin_x_left->setText(i18n("\u2212X"));
1107 origin_x_left->setText(i18n("+X"));
1111 void TitleWidget::slotOriginXClicked()
1113 // Update the text displayed on the button.
1114 updateTextOriginX();
1116 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1117 if (l.size() >= 1) {
1118 updateCoordinates(l.at(0));
1120 // Remember x axis setting
1121 l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked() ?
1122 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1124 graphicsView->setFocus();
1127 void TitleWidget::updateTextOriginY()
1129 if (origin_y_top->isChecked()) {
1130 origin_y_top->setText(i18n("\u2212Y"));
1132 origin_y_top->setText(i18n("+Y"));
1136 void TitleWidget::slotOriginYClicked()
1138 // Update the text displayed on the button.
1139 updateTextOriginY();
1141 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1142 if (l.size() >= 1) {
1143 updateCoordinates(l.at(0));
1145 l.at(0)->setData(TitleDocument::OriginYTop, origin_y_top->isChecked() ?
1146 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1149 graphicsView->setFocus();
1152 void TitleWidget::updateAxisButtons(QGraphicsItem *i)
1154 int xAxis = i->data(TitleDocument::OriginXLeft).toInt();
1155 int yAxis = i->data(TitleDocument::OriginYTop).toInt();
1156 origin_x_left->blockSignals(true);
1157 origin_y_top->blockSignals(true);
1159 if (xAxis == TitleDocument::AxisInverted) {
1160 origin_x_left->setChecked(true);
1162 origin_x_left->setChecked(false);
1164 updateTextOriginX();
1166 if (yAxis == TitleDocument::AxisInverted) {
1167 origin_y_top->setChecked(true);
1169 origin_y_top->setChecked(false);
1171 updateTextOriginY();
1173 origin_x_left->blockSignals(false);
1174 origin_y_top->blockSignals(false);
1177 void TitleWidget::slotChangeBackground()
1179 QColor color = kcolorbutton->color();
1180 color.setAlpha(horizontalSlider->value());
1181 m_frameBorder->setBrush(QBrush(color));
1185 * Something (yeah) has changed in our QGraphicsScene.
1187 void TitleWidget::slotChanged()
1189 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1190 if (l.size() >= 1 && l.at(0)->type() == TEXTITEM) {
1191 textChanged(static_cast <QGraphicsTextItem *>(l.at(0)));
1196 * If the user has set origin_x_left (everything also for y),
1197 * we need to look whether a text element has been selected. If yes,
1198 * we need to ensure that the right border of the text field
1199 * remains fixed also when some text has been entered.
1201 * This is also known as right-justified, with the difference that
1202 * it is not valid for text but for its boundingRect. Text may still
1203 * be left-justified.
1205 void TitleWidget::textChanged(QGraphicsTextItem *i)
1210 if (origin_x_left->isChecked() || origin_y_top->isChecked()) {
1212 if (!i->toPlainText().isEmpty()) {
1216 * Don't do anything if the string is empty. If the position
1217 * would be updated here, a newly created text field would
1218 * be set to the position of the last selected text field.
1224 void TitleWidget::slotInsertUnicode()
1226 m_unicodeDialog->exec();
1229 void TitleWidget::slotInsertUnicodeString(QString text)
1231 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
1233 if (l.at(0)->type() == TEXTITEM) {
1234 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(l.at(0));
1235 t->textCursor().insertText(text);
1240 void TitleWidget::slotUpdateText()
1242 QFont font = font_family->currentFont();
1243 font.setPixelSize(font_size->value());
1244 font.setItalic(buttonItalic->isChecked());
1245 font.setUnderline(buttonUnder->isChecked());
1246 font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1247 QColor color = fontColorButton->color();
1248 color.setAlpha(textAlpha->value());
1250 QGraphicsTextItem* item = NULL;
1251 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1252 if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
1253 item = static_cast <QGraphicsTextItem *>(l.at(0));
1256 //if (item->textCursor().selection ().isEmpty())
1257 QTextCursor cur = item->textCursor();
1258 QTextBlockFormat format = cur.blockFormat();
1259 if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
1260 item->setTextWidth(item->boundingRect().width());
1261 if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
1262 else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
1263 else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
1265 format.setAlignment(Qt::AlignLeft);
1266 item->setTextWidth(-1);
1270 item->setFont(font);
1271 item->setDefaultTextColor(color);
1272 cur.select(QTextCursor::Document);
1273 cur.setBlockFormat(format);
1274 item->setTextCursor(cur);
1275 cur.clearSelection();
1276 item->setTextCursor(cur);
1281 void TitleWidget::rectChanged()
1283 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1284 if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
1285 QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
1286 QColor f = rectFColor->color();
1287 f.setAlpha(rectFAlpha->value());
1289 penf.setWidth(rectLineWidth->value());
1291 QColor b = rectBColor->color();
1292 b.setAlpha(rectBAlpha->value());
1293 rec->setBrush(QBrush(b));
1297 void TitleWidget::itemScaled(int val)
1299 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1300 if (l.size() == 1) {
1301 Transform x = m_transformations.value(l.at(0));
1302 x.scalex = (double)val / 100.0;
1303 x.scaley = (double)val / 100.0;
1305 qtrans.scale(x.scalex, x.scaley);
1306 qtrans.rotate(x.rotate);
1307 l[0]->setTransform(qtrans);
1308 m_transformations[l.at(0)] = x;
1309 updateDimension(l.at(0));
1313 void TitleWidget::itemRotate(int val)
1315 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1316 if (l.size() == 1) {
1317 Transform x = m_transformations[l.at(0)];
1318 x.rotate = (double)val;
1320 qtrans.scale(x.scalex, x.scaley);
1321 qtrans.rotate(x.rotate);
1322 l[0]->setTransform(qtrans);
1323 m_transformations[l.at(0)] = x;
1324 updateDimension(l.at(0));
1328 void TitleWidget::itemHCenter()
1330 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1331 if (l.size() == 1) {
1332 QGraphicsItem *item = l.at(0);
1333 QRectF br = item->sceneBoundingRect();
1334 int width = (int)br.width();
1335 int newPos = (int)((m_frameWidth - width) / 2);
1336 newPos += item->pos().x() - br.left(); // Check item transformation
1337 item->setPos(newPos, item->pos().y());
1338 updateCoordinates(item);
1342 void TitleWidget::itemVCenter()
1344 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1345 if (l.size() == 1) {
1346 QGraphicsItem *item = l.at(0);
1347 QRectF br = item->sceneBoundingRect();
1348 int height = (int)br.height();
1349 int newPos = (int)((m_frameHeight - height) / 2);
1350 newPos += item->pos().y() - br.top(); // Check item transformation
1351 item->setPos(item->pos().x(), newPos);
1352 updateCoordinates(item);
1356 void TitleWidget::itemTop()
1358 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1359 if (l.size() == 1) {
1360 QGraphicsItem *item = l.at(0);
1361 QRectF br = item->sceneBoundingRect();
1363 if (br.top() > 0) diff = -br.top();
1364 else diff = -br.bottom();
1365 item->moveBy(0, diff);
1366 updateCoordinates(item);
1370 void TitleWidget::itemBottom()
1372 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1373 if (l.size() == 1) {
1374 QGraphicsItem *item = l.at(0);
1375 QRectF br = item->sceneBoundingRect();
1377 if (br.bottom() > m_frameHeight) diff = m_frameHeight - br.top();
1378 else diff = m_frameHeight - br.bottom();
1379 item->moveBy(0, diff);
1380 updateCoordinates(item);
1384 void TitleWidget::itemLeft()
1386 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1387 if (l.size() == 1) {
1388 QGraphicsItem *item = l.at(0);
1389 QRectF br = item->sceneBoundingRect();
1391 if (br.left() > 0) diff = -br.left();
1392 else diff = -br.right();
1393 item->moveBy(diff, 0);
1394 updateCoordinates(item);
1398 void TitleWidget::itemRight()
1400 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1401 if (l.size() == 1) {
1402 QGraphicsItem *item = l.at(0);
1403 QRectF br = item->sceneBoundingRect();
1405 if (br.right() < m_frameWidth) diff = m_frameWidth - br.right();
1406 else diff = m_frameWidth - br.left();
1407 item->moveBy(diff, 0);
1408 updateCoordinates(item);
1412 void TitleWidget::setupViewports()
1414 //double aspect_ratio = 4.0 / 3.0;//read from project
1415 //better zoom centered, but render uses only the created rect, so no problem to change the zoom function
1416 /*QRectF sp(0, 0, startViewportSize->value() * m_frameWidth / 100.0 , startViewportSize->value()* m_frameHeight / 100.0);
1417 QRectF ep(0, 0, endViewportSize->value() * m_frameWidth / 100.0, endViewportSize->value() * m_frameHeight / 100.0);
1418 // use a polygon thiat uses 16:9 and 4:3 rects forpreview the size in all aspect ratios ?
1419 QPolygonF spoly(sp);
1420 QPolygonF epoly(ep);
1421 spoly.translate(startViewportX->value(), startViewportY->value());
1422 epoly.translate(endViewportX->value(), endViewportY->value());
1423 m_startViewport->setPolygon(spoly);
1424 m_endViewport->setPolygon(epoly);
1425 if (! insertingValues) {
1426 m_startViewport->setData(0, startViewportX->value());
1427 m_startViewport->setData(1, startViewportY->value());
1428 m_startViewport->setData(2, startViewportSize->value());
1430 m_endViewport->setData(0, endViewportX->value());
1431 m_endViewport->setData(1, endViewportY->value());
1432 m_endViewport->setData(2, endViewportSize->value());
1436 void TitleWidget::loadTitle(KUrl url)
1438 if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this, i18n("Load Title"));
1439 if (!url.isEmpty()) {
1440 QList<QGraphicsItem *> items = m_scene->items();
1441 for (int i = 0; i < items.size(); i++) {
1442 if (items.at(i)->zValue() > -1000) delete items.at(i);
1444 m_scene->clearTextSelection();
1448 if (KIO::NetAccess::download(url, tmpfile, 0)) {
1449 QFile file(tmpfile);
1450 if (file.open(QIODevice::ReadOnly)) {
1451 doc.setContent(&file, false);
1454 KIO::NetAccess::removeTempFile(tmpfile);
1459 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport, &out) + 1;
1461 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1462 insertingValues = true;
1463 startViewportX->setValue(m_startViewport->data(0).toInt());
1464 startViewportY->setValue(m_startViewport->data(1).toInt());
1465 startViewportSize->setValue(m_startViewport->data(2).toInt());
1466 endViewportX->setValue(m_endViewport->data(0).toInt());
1467 endViewportY->setValue(m_endViewport->data(1).toInt());
1468 endViewportSize->setValue(m_endViewport->data(2).toInt());
1470 insertingValues = false;
1476 void TitleWidget::saveTitle(KUrl url)
1478 if (anim_start->isChecked()) slotAnimStart(false);
1479 if (anim_end->isChecked()) slotAnimEnd(false);
1480 if (url.isEmpty()) {
1481 KFileDialog *fs = new KFileDialog(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this);
1482 fs->setOperationMode(KFileDialog::Saving);
1483 fs->setMode(KFile::File);
1484 #if KDE_IS_VERSION(4,2,0)
1485 fs->setConfirmOverwrite(true);
1487 fs->setKeepLocation(true);
1489 url = fs->selectedUrl();
1492 if (!url.isEmpty()) {
1493 if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport, m_tc.getFrameCount(title_duration->text())) == false)
1494 KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
1498 QDomDocument TitleWidget::xml()
1500 QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
1501 doc.documentElement().setAttribute("out", m_tc.getFrameCount(title_duration->text()));
1505 int TitleWidget::duration() const
1507 return m_tc.getFrameCount(title_duration->text());
1510 void TitleWidget::setXml(QDomDocument doc)
1513 m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport, &out);
1515 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1516 /*if (doc.documentElement().hasAttribute("out")) {
1517 GenTime duration = GenTime(doc.documentElement().attribute("out").toDouble() / 1000.0);
1518 title_duration->setText(m_tc.getTimecode(duration));
1520 else title_duration->setText(m_tc.getTimecode(GenTime(5000)));*/
1522 QDomElement e = doc.documentElement();
1523 m_transformations.clear();
1524 QList <QGraphicsItem *> items = graphicsView->scene()->items();
1525 const double PI = 4.0 * atan(1.0);
1526 for (int i = 0; i < items.count(); i++) {
1527 QTransform t = items.at(i)->transform();
1531 x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
1532 m_transformations[items.at(i)] = x;
1534 // mbd: Update the GUI color selectors to match the stuff from the loaded document
1535 QColor background_color = m_titledocument.getBackgroundColor();
1536 horizontalSlider->blockSignals(true);
1537 kcolorbutton->blockSignals(true);
1538 horizontalSlider->setValue(background_color.alpha());
1539 background_color.setAlpha(255);
1540 kcolorbutton->setColor(background_color);
1541 horizontalSlider->blockSignals(false);
1542 kcolorbutton->blockSignals(false);
1544 /*startViewportX->setValue(m_startViewport->data(0).toInt());
1545 startViewportY->setValue(m_startViewport->data(1).toInt());
1546 startViewportSize->setValue(m_startViewport->data(2).toInt());
1547 endViewportX->setValue(m_endViewport->data(0).toInt());
1548 endViewportY->setValue(m_endViewport->data(1).toInt());
1549 endViewportSize->setValue(m_endViewport->data(2).toInt());*/
1551 QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
1555 /** \brief Connected to the accepted signal - calls writeChoices */
1556 void TitleWidget::slotAccepted()
1558 if (anim_start->isChecked()) slotAnimStart(false);
1559 if (anim_end->isChecked()) slotAnimEnd(false);
1563 /** \brief Store the current choices of font, background and rect values */
1564 void TitleWidget::writeChoices()
1566 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1567 KSharedConfigPtr config = KGlobal::config();
1568 KConfigGroup titleConfig(config, "TitleWidget");
1569 // Write the entries
1570 titleConfig.writeEntry("font_family", font_family->currentFont());
1571 //titleConfig.writeEntry("font_size", font_size->value());
1572 titleConfig.writeEntry("font_pixel_size", font_size->value());
1573 titleConfig.writeEntry("font_color", fontColorButton->color());
1574 titleConfig.writeEntry("font_alpha", textAlpha->value());
1575 titleConfig.writeEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1576 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
1577 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
1579 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
1580 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
1581 titleConfig.writeEntry("rect_background_color", rectBColor->color());
1582 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
1583 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
1585 titleConfig.writeEntry("background_color", kcolorbutton->color());
1586 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
1588 //! \todo Not sure if I should sync - it is probably safe to do it
1593 /** \brief Read the last stored choices into the dialog */
1594 void TitleWidget::readChoices()
1596 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1597 KSharedConfigPtr config = KGlobal::config();
1598 KConfigGroup titleConfig(config, "TitleWidget");
1600 font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
1601 font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
1602 fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
1603 textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
1605 if (titleConfig.readEntry("font_bold", false)) weight = QFont::Bold;
1606 else weight = titleConfig.readEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1607 setFontBoxWeight(weight);
1608 buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
1609 buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
1611 rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
1612 rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
1613 rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
1614 rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
1615 rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
1617 kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
1618 horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
1621 void TitleWidget::adjustFrameSize()
1623 m_frameWidth = m_titledocument.frameWidth();
1624 m_frameHeight = m_titledocument.frameHeight();
1625 m_frameBorder->setRect(0, 0, m_frameWidth, m_frameHeight);
1626 displayBackgroundFrame();
1629 void TitleWidget::slotAnimStart(bool anim)
1631 if (anim && anim_end->isChecked()) {
1632 anim_end->setChecked(false);
1633 m_endViewport->setZValue(-1000);
1634 m_endViewport->setBrush(QBrush());
1637 QList<QGraphicsItem *> list = m_scene->items();
1638 for (int i = 0; i < list.count(); i++) {
1639 if (list.at(i)->zValue() > -1000) {
1640 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1641 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1644 align_box->setEnabled(anim);
1645 itemzoom->setEnabled(!anim);
1646 itemrotate->setEnabled(!anim);
1647 frame_toolbar->setEnabled(!anim);
1648 toolbar_stack->setEnabled(!anim);
1650 keep_aspect->setChecked(!m_startViewport->data(0).isNull());
1651 m_startViewport->setZValue(1100);
1652 QColor col = m_startViewport->pen().color();
1654 m_startViewport->setBrush(col);
1655 m_startViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1656 m_startViewport->setSelected(true);
1659 if (m_startViewport->childItems().isEmpty()) addAnimInfoText();
1661 m_startViewport->setZValue(-1000);
1662 m_startViewport->setBrush(QBrush());
1663 m_startViewport->setFlags(0);
1664 if (!anim_end->isChecked()) deleteAnimInfoText();
1669 void TitleWidget::slotAnimEnd(bool anim)
1671 if (anim && anim_start->isChecked()) {
1672 anim_start->setChecked(false);
1673 m_startViewport->setZValue(-1000);
1674 m_startViewport->setBrush(QBrush());
1677 QList<QGraphicsItem *> list = m_scene->items();
1678 for (int i = 0; i < list.count(); i++) {
1679 if (list.at(i)->zValue() > -1000) {
1680 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1681 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1684 align_box->setEnabled(anim);
1685 itemzoom->setEnabled(!anim);
1686 itemrotate->setEnabled(!anim);
1687 frame_toolbar->setEnabled(!anim);
1688 toolbar_stack->setEnabled(!anim);
1690 keep_aspect->setChecked(!m_endViewport->data(0).isNull());
1691 m_endViewport->setZValue(1100);
1692 QColor col = m_endViewport->pen().color();
1694 m_endViewport->setBrush(col);
1695 m_endViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1696 m_endViewport->setSelected(true);
1699 if (m_endViewport->childItems().isEmpty()) addAnimInfoText();
1701 m_endViewport->setZValue(-1000);
1702 m_endViewport->setBrush(QBrush());
1703 m_endViewport->setFlags(0);
1704 if (!anim_start->isChecked()) deleteAnimInfoText();
1708 void TitleWidget::addAnimInfoText()
1710 // add text to anim viewport
1711 QGraphicsTextItem *t = new QGraphicsTextItem(i18n("Start"), m_startViewport);
1712 QGraphicsTextItem *t2 = new QGraphicsTextItem(i18n("End"), m_endViewport);
1713 QFont font = t->font();
1714 font.setPixelSize(m_startViewport->rect().width() / 10);
1715 QColor col = m_startViewport->pen().color();
1717 t->setDefaultTextColor(col);
1719 font.setPixelSize(m_endViewport->rect().width() / 10);
1720 col = m_endViewport->pen().color();
1722 t2->setDefaultTextColor(col);
1726 void TitleWidget::updateInfoText()
1728 // update info text font
1729 if (!m_startViewport->childItems().isEmpty()) {
1730 QGraphicsTextItem *item = static_cast <QGraphicsTextItem *>(m_startViewport->childItems().at(0));
1732 QFont font = item->font();
1733 font.setPixelSize(m_startViewport->rect().width() / 10);
1734 item->setFont(font);
1737 if (!m_endViewport->childItems().isEmpty()) {
1738 QGraphicsTextItem *item = static_cast <QGraphicsTextItem *>(m_endViewport->childItems().at(0));
1740 QFont font = item->font();
1741 font.setPixelSize(m_endViewport->rect().width() / 10);
1742 item->setFont(font);
1747 void TitleWidget::deleteAnimInfoText()
1749 // end animation editing, remove info text
1750 while (!m_startViewport->childItems().isEmpty()) {
1751 QGraphicsItem *item = m_startViewport->childItems().at(0);
1752 m_scene->removeItem(item);
1755 while (!m_endViewport->childItems().isEmpty()) {
1756 QGraphicsItem *item = m_endViewport->childItems().at(0);
1757 m_scene->removeItem(item);
1763 void TitleWidget::slotKeepAspect(bool keep)
1765 if (m_endViewport->zValue() == 1100) {
1766 m_endViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1767 m_endViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1769 m_startViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1770 m_startViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1774 void TitleWidget::slotResize50()
1776 if (m_endViewport->zValue() == 1100) {
1777 m_endViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1778 } else m_startViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1781 void TitleWidget::slotResize100()
1783 if (m_endViewport->zValue() == 1100) {
1784 m_endViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1785 } else m_startViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1788 void TitleWidget::slotResize200()
1790 if (m_endViewport->zValue() == 1100) {
1791 m_endViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);
1792 } else m_startViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);
1795 void TitleWidget::slotAddEffect(int ix)
1797 int effect = effect_list->itemData(ix).toInt();
1799 effect_stack->setHidden(true);
1802 effect_stack->setCurrentIndex(effect - 1);
1803 effect_stack->setHidden(false);
1804 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1805 if (effect == TYPEWRITEREFFECT) {
1806 if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
1807 QStringList effdata = QStringList() << "typewriter" << QString::number(typewriter_delay->value()) + ";" + QString::number(typewriter_start->value());
1808 l[0]->setData(100, effdata);
1811 #if QT_VERSION < 0x040600
1814 if (effect == BLUREFFECT) {
1816 if (l.size() == 1) {
1817 QGraphicsEffect *eff = new QGraphicsBlurEffect();
1818 l[0]->setGraphicsEffect(eff);
1820 } else if (effect == SHADOWEFFECT) {
1821 if (l.size() == 1) {
1822 QGraphicsEffect *eff = new QGraphicsDropShadowEffect();
1823 l[0]->setGraphicsEffect(eff);
1831 void TitleWidget::slotFontText(const QString& s)
1834 if (f.exactMatch()) {
1835 // Font really exists (could also just be a «d» if the user
1836 // starts typing «dejavu» for example).
1837 font_family->setCurrentFont(f);
1839 // TODO: typing dejavu serif does not recognize the font (takes sans).
1840 // upper/lowercase problem?
1843 void TitleWidget::slotEditTypewriter(int ix)
1845 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1846 if (l.size() == 1) {
1847 QStringList effdata = QStringList() << "typewriter" << QString::number(typewriter_delay->value()) + ";" + QString::number(typewriter_start->value());
1848 l[0]->setData(100, effdata);
1852 void TitleWidget::slotEditBlur(int ix)
1854 #if QT_VERSION < 0x040600
1857 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1858 if (l.size() == 1) {
1859 QGraphicsEffect *eff = l[0]->graphicsEffect();
1860 QGraphicsBlurEffect *blur = static_cast <QGraphicsBlurEffect *>(eff);
1861 if (blur) blur->setBlurRadius(ix);
1866 void TitleWidget::slotEditShadow()
1868 #if QT_VERSION < 0x040600
1871 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1872 if (l.size() == 1) {
1873 QGraphicsEffect *eff = l[0]->graphicsEffect();
1874 QGraphicsDropShadowEffect *shadow = static_cast <QGraphicsDropShadowEffect *>(eff);
1876 shadow->setBlurRadius(shadow_radius->value());
1877 shadow->setOffset(shadow_x->value(), shadow_y->value());