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);
715 if (i->graphicsEffect()) {
716 QGraphicsBlurEffect *blur = static_cast <QGraphicsBlurEffect *> (i->graphicsEffect());
718 effect_list->setCurrentIndex(effect_list->findData((int) BLUREFFECT));
719 int rad = (int) blur->blurRadius();
720 blur_radius->setValue(rad);
721 effect_stack->setHidden(false);
724 QGraphicsDropShadowEffect *shad = static_cast <QGraphicsDropShadowEffect *> (i->graphicsEffect());
726 effect_list->setCurrentIndex(effect_list->findData((int) SHADOWEFFECT));
727 shadow_radius->setValue(shad->blurRadius());
728 shadow_x->setValue(shad->xOffset());
729 shadow_y->setValue(shad->yOffset());
730 effect_stack->setHidden(false);
734 else effect_stack->setHidden(true);
736 //if (l[0]->hasFocus())
737 //toolBox->setCurrentIndex(0);
738 //toolBox->setItemEnabled(2, true);
739 font_size->blockSignals(true);
740 font_family->blockSignals(true);
741 font_weight_box->blockSignals(true);
742 buttonItalic->blockSignals(true);
743 buttonUnder->blockSignals(true);
744 fontColorButton->blockSignals(true);
745 textAlpha->blockSignals(true);
746 buttonAlignLeft->blockSignals(true);
747 buttonAlignRight->blockSignals(true);
748 buttonAlignNone->blockSignals(true);
749 buttonAlignCenter->blockSignals(true);
751 QFont font = i->font();
752 font_family->setCurrentFont(font);
753 font_size->setValue(font.pixelSize());
754 buttonItalic->setChecked(font.italic());
755 buttonUnder->setChecked(font.underline());
756 setFontBoxWeight(font.weight());
758 QColor color = i->defaultTextColor();
759 fontColorButton->setColor(color);
760 textAlpha->setValue(color.alpha());
762 QTextCursor cur = i->textCursor();
763 QTextBlockFormat format = cur.blockFormat();
764 if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
765 else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
766 else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
767 else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
769 font_size->blockSignals(false);
770 font_family->blockSignals(false);
771 font_weight_box->blockSignals(false);
772 buttonItalic->blockSignals(false);
773 buttonUnder->blockSignals(false);
774 fontColorButton->blockSignals(false);
775 textAlpha->blockSignals(false);
776 buttonAlignLeft->blockSignals(false);
777 buttonAlignRight->blockSignals(false);
778 buttonAlignNone->blockSignals(false);
779 buttonAlignCenter->blockSignals(false);
781 updateAxisButtons(i);
782 updateCoordinates(i);
784 enableToolbars(TITLE_TEXT);
786 } else if ((l.at(0))->type() == RECTITEM) {
787 showToolbars(TITLE_RECTANGLE);
789 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
790 if (rec == m_startViewport || rec == m_endViewport) {
791 /*toolBox->setCurrentIndex(3);
792 toolBox->widget(0)->setEnabled(false);
793 toolBox->widget(1)->setEnabled(false);*/
794 enableToolbars(TITLE_SELECT);
796 /*toolBox->widget(0)->setEnabled(true);
797 toolBox->widget(1)->setEnabled(true);
798 toolBox->setCurrentIndex(0);*/
799 //toolBox->setItemEnabled(3, true);
800 rectFAlpha->setValue(rec->pen().color().alpha());
801 rectBAlpha->setValue(rec->brush().color().alpha());
802 //kDebug() << rec->brush().color().alpha();
803 QColor fcol = rec->pen().color();
804 QColor bcol = rec->brush().color();
805 //fcol.setAlpha(255);
806 //bcol.setAlpha(255);
807 rectFColor->setColor(fcol);
808 rectBColor->setColor(bcol);
810 rectLineWidth->setValue(rec->pen().width());
811 enableToolbars(TITLE_RECTANGLE);
814 updateAxisButtons(l.at(0));
815 updateCoordinates(rec);
816 updateDimension(rec);
818 } else if (l.at(0)->type() == IMAGEITEM) {
819 showToolbars(TITLE_IMAGE);
821 updateCoordinates(l.at(0));
822 updateDimension(l.at(0));
824 enableToolbars(TITLE_IMAGE);
827 //toolBox->setCurrentIndex(0);
828 showToolbars(TITLE_SELECT);
829 enableToolbars(TITLE_SELECT);
830 frame_properties->setEnabled(false);
832 zValue->setValue((int)l.at(0)->zValue());
833 itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
834 itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
835 value_x->blockSignals(false);
836 value_y->blockSignals(false);
837 value_w->blockSignals(false);
838 value_h->blockSignals(false);
839 itemzoom->blockSignals(false);
840 itemrotate->blockSignals(false);
844 void TitleWidget::slotValueChanged(int type)
846 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
847 if (l.size() > 0 && l.at(0)->type() == IMAGEITEM) {
852 val = value_w->value();
855 val = value_h->value();
859 QGraphicsItem *i = l.at(0);
860 Transform t = m_transformations.value(i);
862 // Ratio width:height
863 double phi = (double) i->boundingRect().width() / i->boundingRect().height();
864 double alpha = (double) t.rotate / 180.0 * M_PI;
874 // Add 0.5 because otherwise incrementing by 1 might have no effect
875 length = val / (cos(alpha) + 1 / phi * sin(alpha)) + 0.5;
876 scale = length / i->boundingRect().width();
879 length = val / (phi * sin(alpha) + cos(alpha)) + 0.5;
880 scale = length / i->boundingRect().height();
887 qtrans.scale(scale, scale);
888 qtrans.rotate(t.rotate);
889 i->setTransform(qtrans);
890 m_transformations[i] = t;
897 /** \brief Updates position/size of the selected item when a value
898 * of an item (coordinates, size) has changed */
899 void TitleWidget::slotAdjustSelectedItem()
901 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
903 if (l.at(0)->type() == RECTITEM) {
905 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
907 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
908 } else if (l.at(0)->type() == TEXTITEM) {
910 updatePosition(l.at(0));
911 } else if (l.at(0)->type() == IMAGEITEM) {
913 updatePosition(l.at(0));
918 /** \brief Updates width/height int the text fields, regarding transformation matrix */
919 void TitleWidget::updateDimension(QGraphicsItem *i)
921 value_w->blockSignals(true);
922 value_h->blockSignals(true);
925 if (i->type() == IMAGEITEM) {
926 // Get multipliers for rotation/scaling
928 /*Transform t = m_transformations.value(i);
929 QRectF r = i->boundingRect();
930 int width = (int) ( abs(r.width()*t.scalex * cos(t.rotate/180.0*M_PI))
931 + abs(r.height()*t.scaley * sin(t.rotate/180.0*M_PI)) );
932 int height = (int) ( abs(r.height()*t.scaley * cos(t.rotate/180*M_PI))
933 + abs(r.width()*t.scalex * sin(t.rotate/180*M_PI)) );*/
935 value_w->setValue(i->sceneBoundingRect().width());
936 value_h->setValue(i->sceneBoundingRect().height());
937 } else if (i->type() == RECTITEM) {
938 QGraphicsRectItem *r = static_cast <QGraphicsRectItem *>(i);
939 value_w->setValue((int) r->rect().width());
940 value_h->setValue((int) r->rect().height());
941 } else if (i->type() == TEXTITEM) {
942 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(i);
943 value_w->setValue((int) t->boundingRect().width());
944 value_h->setValue((int) t->boundingRect().height());
947 value_w->blockSignals(false);
948 value_h->blockSignals(false);
951 /** \brief Updates the coordinates in the text fields from the item */
952 void TitleWidget::updateCoordinates(QGraphicsItem *i)
954 // Block signals emitted by this method
955 value_x->blockSignals(true);
956 value_y->blockSignals(true);
958 if (i->type() == TEXTITEM) {
960 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
962 // Set the correct x coordinate value
963 if (origin_x_left->isChecked()) {
964 // Origin (0 point) is at m_frameWidth, coordinate axis is inverted
965 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->boundingRect().width()));
967 // Origin is at 0 (default)
968 value_x->setValue((int) rec->pos().x());
972 if (origin_y_top->isChecked()) {
973 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->boundingRect().height()));
975 value_y->setValue((int) rec->pos().y());
978 } else if (i->type() == RECTITEM) {
980 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
982 if (origin_x_left->isChecked()) {
983 // Origin (0 point) is at m_frameWidth
984 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->rect().width()));
986 // Origin is at 0 (default)
987 value_x->setValue((int) rec->pos().x());
990 if (origin_y_top->isChecked()) {
991 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->rect().height()));
993 value_y->setValue((int) rec->pos().y());
996 } else if (i->type() == IMAGEITEM) {
998 if (origin_x_left->isChecked()) {
999 value_x->setValue((int)(m_frameWidth - i->pos().x() - i->sceneBoundingRect().width()));
1001 value_x->setValue((int) i->pos().x());
1004 if (origin_y_top->isChecked()) {
1005 value_y->setValue((int)(m_frameHeight - i->pos().y() - i->sceneBoundingRect().height()));
1007 value_y->setValue((int) i->pos().y());
1012 // Stop blocking signals now
1013 value_x->blockSignals(false);
1014 value_y->blockSignals(false);
1017 void TitleWidget::updateRotZoom(QGraphicsItem *i)
1019 itemzoom->blockSignals(true);
1020 itemrotate->blockSignals(false);
1022 Transform t = m_transformations.value(i);
1023 itemzoom->setValue((int)(t.scalex * 100.0 + 0.5));
1024 itemrotate->setValue((int)(t.rotate));
1026 itemzoom->blockSignals(false);
1027 itemrotate->blockSignals(false);
1030 /** \brief Updates the position of an item by reading coordinates from the text fields */
1031 void TitleWidget::updatePosition(QGraphicsItem *i)
1033 if (i->type() == TEXTITEM) {
1034 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
1037 if (origin_x_left->isChecked()) {
1038 /* Origin of the x axis is at m_frameWidth,
1039 * and distance from right border of the item to the right
1040 * border of the frame is taken.
1041 * See comment to slotOriginXClicked().
1043 posX = m_frameWidth - value_x->value() - rec->boundingRect().width();
1045 posX = value_x->value();
1049 if (origin_y_top->isChecked()) {
1050 /* Same for y axis */
1051 posY = m_frameHeight - value_y->value() - rec->boundingRect().height();
1053 posY = value_y->value();
1056 rec->setPos(posX, posY);
1058 } else if (i->type() == RECTITEM) {
1060 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
1063 if (origin_x_left->isChecked()) {
1064 posX = m_frameWidth - value_x->value() - rec->rect().width();
1066 posX = value_x->value();
1070 if (origin_y_top->isChecked()) {
1071 posY = m_frameHeight - value_y->value() - rec->rect().height();
1073 posY = value_y->value();
1076 rec->setPos(posX, posY);
1078 } else if (i->type() == IMAGEITEM) {
1080 if (origin_x_left->isChecked()) {
1081 // Use the sceneBoundingRect because this also regards transformations like zoom
1082 posX = m_frameWidth - value_x->value() - i->sceneBoundingRect().width();
1084 posX = value_x->value();
1088 if (origin_y_top->isChecked()) {
1089 posY = m_frameHeight - value_y->value() - i->sceneBoundingRect().height();
1091 posY = value_y->value();
1094 i->setPos(posX, posY);
1100 void TitleWidget::updateTextOriginX()
1102 if (origin_x_left->isChecked()) {
1103 origin_x_left->setText(i18n("\u2212X"));
1105 origin_x_left->setText(i18n("+X"));
1109 void TitleWidget::slotOriginXClicked()
1111 // Update the text displayed on the button.
1112 updateTextOriginX();
1114 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1115 if (l.size() >= 1) {
1116 updateCoordinates(l.at(0));
1118 // Remember x axis setting
1119 l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked() ?
1120 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1122 graphicsView->setFocus();
1125 void TitleWidget::updateTextOriginY()
1127 if (origin_y_top->isChecked()) {
1128 origin_y_top->setText(i18n("\u2212Y"));
1130 origin_y_top->setText(i18n("+Y"));
1134 void TitleWidget::slotOriginYClicked()
1136 // Update the text displayed on the button.
1137 updateTextOriginY();
1139 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1140 if (l.size() >= 1) {
1141 updateCoordinates(l.at(0));
1143 l.at(0)->setData(TitleDocument::OriginYTop, origin_y_top->isChecked() ?
1144 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1147 graphicsView->setFocus();
1150 void TitleWidget::updateAxisButtons(QGraphicsItem *i)
1152 int xAxis = i->data(TitleDocument::OriginXLeft).toInt();
1153 int yAxis = i->data(TitleDocument::OriginYTop).toInt();
1154 origin_x_left->blockSignals(true);
1155 origin_y_top->blockSignals(true);
1157 if (xAxis == TitleDocument::AxisInverted) {
1158 origin_x_left->setChecked(true);
1160 origin_x_left->setChecked(false);
1162 updateTextOriginX();
1164 if (yAxis == TitleDocument::AxisInverted) {
1165 origin_y_top->setChecked(true);
1167 origin_y_top->setChecked(false);
1169 updateTextOriginY();
1171 origin_x_left->blockSignals(false);
1172 origin_y_top->blockSignals(false);
1175 void TitleWidget::slotChangeBackground()
1177 QColor color = kcolorbutton->color();
1178 color.setAlpha(horizontalSlider->value());
1179 m_frameBorder->setBrush(QBrush(color));
1183 * Something (yeah) has changed in our QGraphicsScene.
1185 void TitleWidget::slotChanged()
1187 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1188 if (l.size() >= 1 && l.at(0)->type() == TEXTITEM) {
1189 textChanged(static_cast <QGraphicsTextItem *>(l.at(0)));
1194 * If the user has set origin_x_left (everything also for y),
1195 * we need to look whether a text element has been selected. If yes,
1196 * we need to ensure that the right border of the text field
1197 * remains fixed also when some text has been entered.
1199 * This is also known as right-justified, with the difference that
1200 * it is not valid for text but for its boundingRect. Text may still
1201 * be left-justified.
1203 void TitleWidget::textChanged(QGraphicsTextItem *i)
1208 if (origin_x_left->isChecked() || origin_y_top->isChecked()) {
1210 if (!i->toPlainText().isEmpty()) {
1214 * Don't do anything if the string is empty. If the position
1215 * would be updated here, a newly created text field would
1216 * be set to the position of the last selected text field.
1222 void TitleWidget::slotInsertUnicode()
1224 m_unicodeDialog->exec();
1227 void TitleWidget::slotInsertUnicodeString(QString text)
1229 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
1231 if (l.at(0)->type() == TEXTITEM) {
1232 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(l.at(0));
1233 t->textCursor().insertText(text);
1238 void TitleWidget::slotUpdateText()
1240 QFont font = font_family->currentFont();
1241 font.setPixelSize(font_size->value());
1242 font.setItalic(buttonItalic->isChecked());
1243 font.setUnderline(buttonUnder->isChecked());
1244 font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1245 QColor color = fontColorButton->color();
1246 color.setAlpha(textAlpha->value());
1248 QGraphicsTextItem* item = NULL;
1249 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1250 if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
1251 item = static_cast <QGraphicsTextItem *>(l.at(0));
1254 //if (item->textCursor().selection ().isEmpty())
1255 QTextCursor cur = item->textCursor();
1256 QTextBlockFormat format = cur.blockFormat();
1257 if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
1258 item->setTextWidth(item->boundingRect().width());
1259 if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
1260 else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
1261 else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
1263 format.setAlignment(Qt::AlignLeft);
1264 item->setTextWidth(-1);
1268 item->setFont(font);
1269 item->setDefaultTextColor(color);
1270 cur.select(QTextCursor::Document);
1271 cur.setBlockFormat(format);
1272 item->setTextCursor(cur);
1273 cur.clearSelection();
1274 item->setTextCursor(cur);
1279 void TitleWidget::rectChanged()
1281 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1282 if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
1283 QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
1284 QColor f = rectFColor->color();
1285 f.setAlpha(rectFAlpha->value());
1287 penf.setWidth(rectLineWidth->value());
1289 QColor b = rectBColor->color();
1290 b.setAlpha(rectBAlpha->value());
1291 rec->setBrush(QBrush(b));
1295 void TitleWidget::itemScaled(int val)
1297 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1298 if (l.size() == 1) {
1299 Transform x = m_transformations.value(l.at(0));
1300 x.scalex = (double)val / 100.0;
1301 x.scaley = (double)val / 100.0;
1303 qtrans.scale(x.scalex, x.scaley);
1304 qtrans.rotate(x.rotate);
1305 l[0]->setTransform(qtrans);
1306 m_transformations[l.at(0)] = x;
1307 updateDimension(l.at(0));
1311 void TitleWidget::itemRotate(int val)
1313 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1314 if (l.size() == 1) {
1315 Transform x = m_transformations[l.at(0)];
1316 x.rotate = (double)val;
1318 qtrans.scale(x.scalex, x.scaley);
1319 qtrans.rotate(x.rotate);
1320 l[0]->setTransform(qtrans);
1321 m_transformations[l.at(0)] = x;
1322 updateDimension(l.at(0));
1326 void TitleWidget::itemHCenter()
1328 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1329 if (l.size() == 1) {
1330 QGraphicsItem *item = l.at(0);
1331 QRectF br = item->sceneBoundingRect();
1332 int width = (int)br.width();
1333 int newPos = (int)((m_frameWidth - width) / 2);
1334 newPos += item->pos().x() - br.left(); // Check item transformation
1335 item->setPos(newPos, item->pos().y());
1336 updateCoordinates(item);
1340 void TitleWidget::itemVCenter()
1342 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1343 if (l.size() == 1) {
1344 QGraphicsItem *item = l.at(0);
1345 QRectF br = item->sceneBoundingRect();
1346 int height = (int)br.height();
1347 int newPos = (int)((m_frameHeight - height) / 2);
1348 newPos += item->pos().y() - br.top(); // Check item transformation
1349 item->setPos(item->pos().x(), newPos);
1350 updateCoordinates(item);
1354 void TitleWidget::itemTop()
1356 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1357 if (l.size() == 1) {
1358 QGraphicsItem *item = l.at(0);
1359 QRectF br = item->sceneBoundingRect();
1361 if (br.top() > 0) diff = -br.top();
1362 else diff = -br.bottom();
1363 item->moveBy(0, diff);
1364 updateCoordinates(item);
1368 void TitleWidget::itemBottom()
1370 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1371 if (l.size() == 1) {
1372 QGraphicsItem *item = l.at(0);
1373 QRectF br = item->sceneBoundingRect();
1375 if (br.bottom() > m_frameHeight) diff = m_frameHeight - br.top();
1376 else diff = m_frameHeight - br.bottom();
1377 item->moveBy(0, diff);
1378 updateCoordinates(item);
1382 void TitleWidget::itemLeft()
1384 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1385 if (l.size() == 1) {
1386 QGraphicsItem *item = l.at(0);
1387 QRectF br = item->sceneBoundingRect();
1389 if (br.left() > 0) diff = -br.left();
1390 else diff = -br.right();
1391 item->moveBy(diff, 0);
1392 updateCoordinates(item);
1396 void TitleWidget::itemRight()
1398 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1399 if (l.size() == 1) {
1400 QGraphicsItem *item = l.at(0);
1401 QRectF br = item->sceneBoundingRect();
1403 if (br.right() < m_frameWidth) diff = m_frameWidth - br.right();
1404 else diff = m_frameWidth - br.left();
1405 item->moveBy(diff, 0);
1406 updateCoordinates(item);
1410 void TitleWidget::setupViewports()
1412 //double aspect_ratio = 4.0 / 3.0;//read from project
1413 //better zoom centered, but render uses only the created rect, so no problem to change the zoom function
1414 /*QRectF sp(0, 0, startViewportSize->value() * m_frameWidth / 100.0 , startViewportSize->value()* m_frameHeight / 100.0);
1415 QRectF ep(0, 0, endViewportSize->value() * m_frameWidth / 100.0, endViewportSize->value() * m_frameHeight / 100.0);
1416 // use a polygon thiat uses 16:9 and 4:3 rects forpreview the size in all aspect ratios ?
1417 QPolygonF spoly(sp);
1418 QPolygonF epoly(ep);
1419 spoly.translate(startViewportX->value(), startViewportY->value());
1420 epoly.translate(endViewportX->value(), endViewportY->value());
1421 m_startViewport->setPolygon(spoly);
1422 m_endViewport->setPolygon(epoly);
1423 if (! insertingValues) {
1424 m_startViewport->setData(0, startViewportX->value());
1425 m_startViewport->setData(1, startViewportY->value());
1426 m_startViewport->setData(2, startViewportSize->value());
1428 m_endViewport->setData(0, endViewportX->value());
1429 m_endViewport->setData(1, endViewportY->value());
1430 m_endViewport->setData(2, endViewportSize->value());
1434 void TitleWidget::loadTitle(KUrl url)
1436 if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this, i18n("Load Title"));
1437 if (!url.isEmpty()) {
1438 QList<QGraphicsItem *> items = m_scene->items();
1439 for (int i = 0; i < items.size(); i++) {
1440 if (items.at(i)->zValue() > -1000) delete items.at(i);
1442 m_scene->clearTextSelection();
1446 if (KIO::NetAccess::download(url, tmpfile, 0)) {
1447 QFile file(tmpfile);
1448 if (file.open(QIODevice::ReadOnly)) {
1449 doc.setContent(&file, false);
1452 KIO::NetAccess::removeTempFile(tmpfile);
1457 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport, &out) + 1;
1459 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1460 insertingValues = true;
1461 startViewportX->setValue(m_startViewport->data(0).toInt());
1462 startViewportY->setValue(m_startViewport->data(1).toInt());
1463 startViewportSize->setValue(m_startViewport->data(2).toInt());
1464 endViewportX->setValue(m_endViewport->data(0).toInt());
1465 endViewportY->setValue(m_endViewport->data(1).toInt());
1466 endViewportSize->setValue(m_endViewport->data(2).toInt());
1468 insertingValues = false;
1474 void TitleWidget::saveTitle(KUrl url)
1476 if (anim_start->isChecked()) slotAnimStart(false);
1477 if (anim_end->isChecked()) slotAnimEnd(false);
1478 if (url.isEmpty()) {
1479 KFileDialog *fs = new KFileDialog(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this);
1480 fs->setOperationMode(KFileDialog::Saving);
1481 fs->setMode(KFile::File);
1482 #if KDE_IS_VERSION(4,2,0)
1483 fs->setConfirmOverwrite(true);
1485 fs->setKeepLocation(true);
1487 url = fs->selectedUrl();
1490 if (!url.isEmpty()) {
1491 if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport, m_tc.getFrameCount(title_duration->text())) == false)
1492 KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
1496 QDomDocument TitleWidget::xml()
1498 QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
1499 doc.documentElement().setAttribute("out", m_tc.getFrameCount(title_duration->text()));
1503 int TitleWidget::duration() const
1505 return m_tc.getFrameCount(title_duration->text());
1508 void TitleWidget::setXml(QDomDocument doc)
1511 m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport, &out);
1513 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1514 /*if (doc.documentElement().hasAttribute("out")) {
1515 GenTime duration = GenTime(doc.documentElement().attribute("out").toDouble() / 1000.0);
1516 title_duration->setText(m_tc.getTimecode(duration));
1518 else title_duration->setText(m_tc.getTimecode(GenTime(5000)));*/
1520 QDomElement e = doc.documentElement();
1521 m_transformations.clear();
1522 QList <QGraphicsItem *> items = graphicsView->scene()->items();
1523 const double PI = 4.0 * atan(1.0);
1524 for (int i = 0; i < items.count(); i++) {
1525 QTransform t = items.at(i)->transform();
1529 x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
1530 m_transformations[items.at(i)] = x;
1532 // mbd: Update the GUI color selectors to match the stuff from the loaded document
1533 QColor background_color = m_titledocument.getBackgroundColor();
1534 horizontalSlider->blockSignals(true);
1535 kcolorbutton->blockSignals(true);
1536 horizontalSlider->setValue(background_color.alpha());
1537 background_color.setAlpha(255);
1538 kcolorbutton->setColor(background_color);
1539 horizontalSlider->blockSignals(false);
1540 kcolorbutton->blockSignals(false);
1542 /*startViewportX->setValue(m_startViewport->data(0).toInt());
1543 startViewportY->setValue(m_startViewport->data(1).toInt());
1544 startViewportSize->setValue(m_startViewport->data(2).toInt());
1545 endViewportX->setValue(m_endViewport->data(0).toInt());
1546 endViewportY->setValue(m_endViewport->data(1).toInt());
1547 endViewportSize->setValue(m_endViewport->data(2).toInt());*/
1549 QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
1553 /** \brief Connected to the accepted signal - calls writeChoices */
1554 void TitleWidget::slotAccepted()
1556 if (anim_start->isChecked()) slotAnimStart(false);
1557 if (anim_end->isChecked()) slotAnimEnd(false);
1561 /** \brief Store the current choices of font, background and rect values */
1562 void TitleWidget::writeChoices()
1564 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1565 KSharedConfigPtr config = KGlobal::config();
1566 KConfigGroup titleConfig(config, "TitleWidget");
1567 // Write the entries
1568 titleConfig.writeEntry("font_family", font_family->currentFont());
1569 //titleConfig.writeEntry("font_size", font_size->value());
1570 titleConfig.writeEntry("font_pixel_size", font_size->value());
1571 titleConfig.writeEntry("font_color", fontColorButton->color());
1572 titleConfig.writeEntry("font_alpha", textAlpha->value());
1573 titleConfig.writeEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1574 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
1575 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
1577 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
1578 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
1579 titleConfig.writeEntry("rect_background_color", rectBColor->color());
1580 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
1581 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
1583 titleConfig.writeEntry("background_color", kcolorbutton->color());
1584 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
1586 //! \todo Not sure if I should sync - it is probably safe to do it
1591 /** \brief Read the last stored choices into the dialog */
1592 void TitleWidget::readChoices()
1594 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1595 KSharedConfigPtr config = KGlobal::config();
1596 KConfigGroup titleConfig(config, "TitleWidget");
1598 font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
1599 font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
1600 fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
1601 textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
1603 if (titleConfig.readEntry("font_bold", false)) weight = QFont::Bold;
1604 else weight = titleConfig.readEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1605 setFontBoxWeight(weight);
1606 buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
1607 buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
1609 rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
1610 rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
1611 rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
1612 rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
1613 rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
1615 kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
1616 horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
1619 void TitleWidget::adjustFrameSize()
1621 m_frameWidth = m_titledocument.frameWidth();
1622 m_frameHeight = m_titledocument.frameHeight();
1623 m_frameBorder->setRect(0, 0, m_frameWidth, m_frameHeight);
1624 displayBackgroundFrame();
1627 void TitleWidget::slotAnimStart(bool anim)
1629 if (anim && anim_end->isChecked()) {
1630 anim_end->setChecked(false);
1631 m_endViewport->setZValue(-1000);
1632 m_endViewport->setBrush(QBrush());
1635 QList<QGraphicsItem *> list = m_scene->items();
1636 for (int i = 0; i < list.count(); i++) {
1637 if (list.at(i)->zValue() > -1000) {
1638 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1639 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1642 align_box->setEnabled(anim);
1643 itemzoom->setEnabled(!anim);
1644 itemrotate->setEnabled(!anim);
1645 frame_toolbar->setEnabled(!anim);
1646 toolbar_stack->setEnabled(!anim);
1648 keep_aspect->setChecked(!m_startViewport->data(0).isNull());
1649 m_startViewport->setZValue(1100);
1650 QColor col = m_startViewport->pen().color();
1652 m_startViewport->setBrush(col);
1653 m_startViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1654 m_startViewport->setSelected(true);
1657 if (m_startViewport->childItems().isEmpty()) addAnimInfoText();
1659 m_startViewport->setZValue(-1000);
1660 m_startViewport->setBrush(QBrush());
1661 m_startViewport->setFlags(0);
1662 if (!anim_end->isChecked()) deleteAnimInfoText();
1667 void TitleWidget::slotAnimEnd(bool anim)
1669 if (anim && anim_start->isChecked()) {
1670 anim_start->setChecked(false);
1671 m_startViewport->setZValue(-1000);
1672 m_startViewport->setBrush(QBrush());
1675 QList<QGraphicsItem *> list = m_scene->items();
1676 for (int i = 0; i < list.count(); i++) {
1677 if (list.at(i)->zValue() > -1000) {
1678 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1679 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1682 align_box->setEnabled(anim);
1683 itemzoom->setEnabled(!anim);
1684 itemrotate->setEnabled(!anim);
1685 frame_toolbar->setEnabled(!anim);
1686 toolbar_stack->setEnabled(!anim);
1688 keep_aspect->setChecked(!m_endViewport->data(0).isNull());
1689 m_endViewport->setZValue(1100);
1690 QColor col = m_endViewport->pen().color();
1692 m_endViewport->setBrush(col);
1693 m_endViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1694 m_endViewport->setSelected(true);
1697 if (m_endViewport->childItems().isEmpty()) addAnimInfoText();
1699 m_endViewport->setZValue(-1000);
1700 m_endViewport->setBrush(QBrush());
1701 m_endViewport->setFlags(0);
1702 if (!anim_start->isChecked()) deleteAnimInfoText();
1706 void TitleWidget::addAnimInfoText()
1708 // add text to anim viewport
1709 QGraphicsTextItem *t = new QGraphicsTextItem(i18n("Start"), m_startViewport);
1710 QGraphicsTextItem *t2 = new QGraphicsTextItem(i18n("End"), m_endViewport);
1711 QFont font = t->font();
1712 font.setPixelSize(m_startViewport->rect().width() / 10);
1713 QColor col = m_startViewport->pen().color();
1715 t->setDefaultTextColor(col);
1717 font.setPixelSize(m_endViewport->rect().width() / 10);
1718 col = m_endViewport->pen().color();
1720 t2->setDefaultTextColor(col);
1724 void TitleWidget::updateInfoText()
1726 // update info text font
1727 if (!m_startViewport->childItems().isEmpty()) {
1728 QGraphicsTextItem *item = static_cast <QGraphicsTextItem *>(m_startViewport->childItems().at(0));
1730 QFont font = item->font();
1731 font.setPixelSize(m_startViewport->rect().width() / 10);
1732 item->setFont(font);
1735 if (!m_endViewport->childItems().isEmpty()) {
1736 QGraphicsTextItem *item = static_cast <QGraphicsTextItem *>(m_endViewport->childItems().at(0));
1738 QFont font = item->font();
1739 font.setPixelSize(m_endViewport->rect().width() / 10);
1740 item->setFont(font);
1745 void TitleWidget::deleteAnimInfoText()
1747 // end animation editing, remove info text
1748 while (!m_startViewport->childItems().isEmpty()) {
1749 QGraphicsItem *item = m_startViewport->childItems().at(0);
1750 m_scene->removeItem(item);
1753 while (!m_endViewport->childItems().isEmpty()) {
1754 QGraphicsItem *item = m_endViewport->childItems().at(0);
1755 m_scene->removeItem(item);
1761 void TitleWidget::slotKeepAspect(bool keep)
1763 if (m_endViewport->zValue() == 1100) {
1764 m_endViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1765 m_endViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1767 m_startViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1768 m_startViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1772 void TitleWidget::slotResize50()
1774 if (m_endViewport->zValue() == 1100) {
1775 m_endViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1776 } else m_startViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1779 void TitleWidget::slotResize100()
1781 if (m_endViewport->zValue() == 1100) {
1782 m_endViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1783 } else m_startViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1786 void TitleWidget::slotResize200()
1788 if (m_endViewport->zValue() == 1100) {
1789 m_endViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);
1790 } else m_startViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);
1793 void TitleWidget::slotAddEffect(int ix)
1795 int effect = effect_list->itemData(ix).toInt();
1797 effect_stack->setHidden(true);
1800 effect_stack->setCurrentIndex(effect - 1);
1801 effect_stack->setHidden(false);
1802 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1803 if (effect == TYPEWRITEREFFECT) {
1804 if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
1805 QStringList effdata = QStringList() << "typewriter" << QString::number(typewriter_delay->value()) + ";" + QString::number(typewriter_start->value());
1806 l[0]->setData(100, effdata);
1809 #if QT_VERSION < 0x040600
1812 if (effect == BLUREFFECT) {
1814 if (l.size() == 1) {
1815 QGraphicsEffect *eff = new QGraphicsBlurEffect();
1816 l[0]->setGraphicsEffect(eff);
1818 } else if (effect == SHADOWEFFECT) {
1819 if (l.size() == 1) {
1820 QGraphicsEffect *eff = new QGraphicsDropShadowEffect();
1821 l[0]->setGraphicsEffect(eff);
1829 void TitleWidget::slotFontText(const QString& s)
1832 if (f.exactMatch()) {
1833 // Font really exists (could also just be a «d» if the user
1834 // starts typing «dejavu» for example).
1835 font_family->setCurrentFont(f);
1837 // TODO: typing dejavu serif does not recognize the font (takes sans).
1838 // upper/lowercase problem?
1841 void TitleWidget::slotEditTypewriter(int ix)
1843 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1844 if (l.size() == 1) {
1845 QStringList effdata = QStringList() << "typewriter" << QString::number(typewriter_delay->value()) + ";" + QString::number(typewriter_start->value());
1846 l[0]->setData(100, effdata);
1850 void TitleWidget::slotEditBlur(int ix)
1852 #if QT_VERSION < 0x040600
1855 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1856 if (l.size() == 1) {
1857 QGraphicsEffect *eff = l[0]->graphicsEffect();
1858 QGraphicsBlurEffect *blur = static_cast <QGraphicsBlurEffect *>(eff);
1859 if (blur) blur->setBlurRadius(ix);
1864 void TitleWidget::slotEditShadow()
1866 #if QT_VERSION < 0x040600
1869 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1870 if (l.size() == 1) {
1871 QGraphicsEffect *eff = l[0]->graphicsEffect();
1872 QGraphicsDropShadowEffect *shadow = static_cast <QGraphicsDropShadowEffect *>(eff);
1874 shadow->setBlurRadius(shadow_radius->value());
1875 shadow->setOffset(shadow_x->value(), shadow_y->value());