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 int settingUp = false;
43 const int IMAGEITEM = 7;
44 const int RECTITEM = 3;
45 const int TEXTITEM = 8;
46 static bool insertingValues = false;
48 TitleWidget::TitleWidget(KUrl url, Timecode tc, QString projectTitlePath, Render *render, QWidget *parent) :
51 m_startViewport(NULL),
55 m_unicodeDialog(new UnicodeDialog(UnicodeDialog::InputHex)),
56 m_projectTitlePath(projectTitlePath),
60 setFont(KGlobalSettings::toolBarFont());
61 frame_properties->setEnabled(false);
62 rect_properties->setFixedHeight(frame_properties->height() + 4);
63 no_properties->setFixedHeight(frame_properties->height() + 4);
64 image_properties->setFixedHeight(frame_properties->height() + 4);
65 text_properties->setFixedHeight(frame_properties->height() + 4);
66 frame_properties->setFixedHeight(frame_toolbar->height());
68 itemzoom->setSuffix(i18n("%"));
69 m_frameWidth = render->renderWidth();
70 m_frameHeight = render->renderHeight();
71 showToolbars(TITLE_NONE);
73 //TODO: get default title duration instead of hardcoded one
74 title_duration->setText(m_tc.getTimecode(GenTime(5000 / 1000.0)));
76 connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
77 connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
79 connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
80 connect(font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(slotUpdateText())) ;
81 connect(font_size, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText())) ;
82 connect(textAlpha, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText()));
83 connect(font_weight_box, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateText()));
85 connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
86 connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
87 connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
88 connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
89 connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
91 /*connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
92 connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
93 connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
94 connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
95 connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
96 connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));*/
98 connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
99 connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
100 connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
101 connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
102 connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
103 connect(itemtop, SIGNAL(clicked()), this, SLOT(itemTop()));
104 connect(itembottom, SIGNAL(clicked()), this, SLOT(itemBottom()));
105 connect(itemleft, SIGNAL(clicked()), this, SLOT(itemLeft()));
106 connect(itemright, SIGNAL(clicked()), this, SLOT(itemRight()));
108 connect(origin_x_left, SIGNAL(clicked()), this, SLOT(slotOriginXClicked()));
109 connect(origin_y_top, SIGNAL(clicked()), this, SLOT(slotOriginYClicked()));
111 m_signalMapper = new QSignalMapper(this);
112 m_signalMapper->setMapping(value_w, ValueWidth);
113 m_signalMapper->setMapping(value_h, ValueHeight);
114 connect(value_w, SIGNAL(valueChanged(int)), m_signalMapper, SLOT(map()));
115 connect(value_h, SIGNAL(valueChanged(int)), m_signalMapper, SLOT(map()));
116 connect(m_signalMapper, SIGNAL(mapped(int)), this, SLOT(slotValueChanged(int)));
118 connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
119 connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
120 connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
121 connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
122 connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
123 connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
124 connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
125 connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
126 connect(buttonAlignLeft, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
127 connect(buttonAlignRight, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
128 connect(buttonAlignCenter, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
129 connect(buttonAlignNone, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
130 //connect(buttonInsertUnicode, SIGNAL(clicked()), this, SLOT(slotInsertUnicode()));
131 connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
133 connect(m_unicodeDialog, SIGNAL(charSelected(QString)), this, SLOT(slotInsertUnicodeString(QString)));
136 connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
138 font_weight_box->blockSignals(true);
139 font_weight_box->addItem(i18nc("Font style", "Light"), QFont::Light);
140 font_weight_box->addItem(i18nc("Font style", "Normal"), QFont::Normal);
141 font_weight_box->addItem(i18nc("Font style", "Demi-Bold"), QFont::DemiBold);
142 font_weight_box->addItem(i18nc("Font style", "Bold"), QFont::Bold);
143 font_weight_box->addItem(i18nc("Font style", "Black"), QFont::Black);
144 font_weight_box->setToolTip(i18n("Font weight"));
145 font_weight_box->setCurrentIndex(1);
146 font_weight_box->blockSignals(false);
148 buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
149 buttonRealSize->setIcon(KIcon("zoom-original"));
150 buttonItalic->setIcon(KIcon("format-text-italic"));
151 buttonUnder->setIcon(KIcon("format-text-underline"));
152 buttonAlignCenter->setIcon(KIcon("format-justify-center"));
153 buttonAlignLeft->setIcon(KIcon("format-justify-left"));
154 buttonAlignRight->setIcon(KIcon("format-justify-right"));
155 buttonAlignNone->setIcon(KIcon("kdenlive-align-none"));
157 buttonAlignNone->setToolTip(i18n("No alignment"));
158 buttonAlignRight->setToolTip(i18n("Align right"));
159 buttonAlignLeft->setToolTip(i18n("Align left"));
160 buttonAlignCenter->setToolTip(i18n("Align center"));
162 m_unicodeAction = new QAction(KIcon("kdenlive-insert-unicode"), QString(), this);
163 m_unicodeAction->setShortcut(Qt::SHIFT + Qt::CTRL + Qt::Key_U);
164 m_unicodeAction->setToolTip(i18n("Insert Unicode character") + ' ' + m_unicodeAction->shortcut().toString());
165 connect(m_unicodeAction, SIGNAL(triggered()), this, SLOT(slotInsertUnicode()));
166 buttonInsertUnicode->setDefaultAction(m_unicodeAction);
168 origin_x_left->setToolTip(i18n("Invert x axis and change 0 point"));
169 origin_y_top->setToolTip(i18n("Invert y axis and change 0 point"));
170 rectBColor->setToolTip(i18n("Select fill color"));
171 rectFColor->setToolTip(i18n("Select border color"));
172 rectBAlpha->setToolTip(i18n("Fill transparency"));
173 rectFAlpha->setToolTip(i18n("Border transparency"));
174 zoom_slider->setToolTip(i18n("Zoom"));
175 buttonRealSize->setToolTip(i18n("Original size (1:1)"));
176 buttonFitZoom->setToolTip(i18n("Fit zoom"));
177 kcolorbutton->setToolTip(i18n("Select background color"));
178 horizontalSlider->setToolTip(i18n("Background Transparency"));
180 itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
181 itemhcenter->setToolTip(i18n("Align item horizontally"));
182 itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
183 itemvcenter->setToolTip(i18n("Align item vertically"));
184 itemtop->setIcon(KIcon("kdenlive-align-top"));
185 itemtop->setToolTip(i18n("Align item to top"));
186 itembottom->setIcon(KIcon("kdenlive-align-bottom"));
187 itembottom->setToolTip(i18n("Align item to bottom"));
188 itemright->setIcon(KIcon("kdenlive-align-right"));
189 itemright->setToolTip(i18n("Align item to right"));
190 itemleft->setIcon(KIcon("kdenlive-align-left"));
191 itemleft->setToolTip(i18n("Align item to left"));
194 QHBoxLayout *layout = new QHBoxLayout;
195 frame_toolbar->setLayout(layout);
196 layout->setContentsMargins(2, 2, 2, 2);
197 QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
199 m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), QString());
200 m_buttonCursor->setCheckable(true);
201 m_buttonCursor->setShortcut(Qt::ALT + Qt::Key_S);
202 m_buttonCursor->setToolTip(i18n("Selection Tool") + ' ' + m_buttonCursor->shortcut().toString());
203 connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
205 m_buttonText = m_toolbar->addAction(KIcon("insert-text"), QString());
206 m_buttonText->setCheckable(true);
207 m_buttonText->setShortcut(Qt::ALT + Qt::Key_T);
208 m_buttonText->setToolTip(i18n("Add Text") + ' ' + m_buttonText->shortcut().toString());
209 connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
211 m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), QString());
212 m_buttonRect->setCheckable(true);
213 m_buttonRect->setShortcut(Qt::ALT + Qt::Key_R);
214 m_buttonRect->setToolTip(i18n("Add Rectangle") + ' ' + m_buttonRect->shortcut().toString());
215 connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
217 m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), QString());
218 m_buttonImage->setCheckable(false);
219 m_buttonImage->setShortcut(Qt::ALT + Qt::Key_I);
220 m_buttonImage->setToolTip(i18n("Add Image") + ' ' + m_buttonImage->shortcut().toString());
221 connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
223 m_toolbar->addSeparator();
225 m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
226 m_buttonLoad->setCheckable(false);
227 m_buttonLoad->setShortcut(Qt::CTRL + Qt::Key_O);
228 connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
230 m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
231 m_buttonSave->setCheckable(false);
232 m_buttonSave->setShortcut(Qt::CTRL + Qt::Key_S);
233 connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
235 layout->addWidget(m_toolbar);
237 // initialize graphic scene
238 m_scene = new GraphicsSceneRectMove(this);
239 graphicsView->setScene(m_scene);
240 m_titledocument.setScene(m_scene, m_frameWidth, m_frameHeight);
241 connect(m_scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(slotChanged()));
243 // a gradient background
244 /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
245 gradient->setSpread(QGradient::ReflectSpread);
246 scene->setBackgroundBrush(*gradient);*/
248 m_frameImage = new QGraphicsPixmapItem();
250 qtrans.scale(2.0, 2.0);
251 m_frameImage->setTransform(qtrans);
252 m_frameImage->setZValue(-1200);
253 m_frameImage->setFlags(0);
254 displayBackgroundFrame();
255 graphicsView->scene()->addItem(m_frameImage);
257 connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
258 connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
259 connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
260 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
261 //connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
262 connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
263 connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
264 connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
266 QPen framepen(Qt::DotLine);
267 framepen.setColor(Qt::red);
269 m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
270 m_frameBorder->setPen(framepen);
271 m_frameBorder->setZValue(-1100);
272 m_frameBorder->setBrush(Qt::transparent);
273 m_frameBorder->setFlags(0);
274 graphicsView->scene()->addItem(m_frameBorder);
276 // mbd: load saved settings
279 graphicsView->show();
280 //graphicsView->setRenderHint(QPainter::Antialiasing);
281 graphicsView->setInteractive(true);
282 //graphicsView->resize(400, 300);
283 kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
284 //toolBox->setItemEnabled(2, false);
285 m_startViewport = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
286 m_endViewport = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
287 m_startViewport->setData(0, m_frameWidth);
288 m_startViewport->setData(1, m_frameHeight);
289 m_endViewport->setData(0, m_frameWidth);
290 m_endViewport->setData(1, m_frameHeight);
292 if (!url.isEmpty()) loadTitle(url);
295 QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
298 connect(anim_start, SIGNAL(toggled(bool)), this, SLOT(slotAnimStart(bool)));
299 connect(anim_end, SIGNAL(toggled(bool)), this, SLOT(slotAnimEnd(bool)));
302 TitleWidget::~TitleWidget()
306 delete m_buttonImage;
307 delete m_buttonCursor;
310 delete m_unicodeAction;
312 delete m_unicodeDialog;
313 delete m_frameBorder;
315 delete m_startViewport;
316 delete m_endViewport;
318 delete m_signalMapper;
322 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl, bool isClone)
325 QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
326 KStandardDirs::makeDir(titlePath);
327 titlePath.append((isClone == false) ? "title" : "clone");
330 while (path.isEmpty() || QFile::exists(path)) {
332 path = titlePath + QString::number(counter).rightJustified(3, '0', false) + ".png";
334 result.append(((isClone == false) ? i18n("Title") : i18n("Clone")) + ' ' + QString::number(counter).rightJustified(3, '0', false));
340 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
343 QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
344 KStandardDirs::makeDir(titlePath);
345 return titlePath + titleName + ".png";
349 QStringList TitleWidget::extractImageList(QString xml)
352 if (xml.isEmpty()) return result;
355 QDomNodeList images = doc.elementsByTagName("content");
356 for (int i = 0; i < images.count(); i++) {
357 if (images.at(i).toElement().hasAttribute("url"))
358 result.append(images.at(i).toElement().attribute("url"));
365 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
370 void TitleWidget::slotTextTool()
372 m_scene->setTool(TITLE_TEXT);
373 showToolbars(TITLE_TEXT);
374 checkButton(TITLE_TEXT);
377 void TitleWidget::slotRectTool()
379 m_scene->setTool(TITLE_RECTANGLE);
380 showToolbars(TITLE_RECTANGLE);
381 checkButton(TITLE_RECTANGLE);
384 void TitleWidget::slotSelectTool()
386 m_scene->setTool(TITLE_SELECT);
388 // Find out which toolbars need to be shown, depending on selected item
389 TITLETOOL t = TITLE_SELECT;
390 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
392 switch (l.at(0)->type()) {
406 if (t == TITLE_RECTANGLE && (l.at(0) == m_endViewport || l.at(0) == m_startViewport)) {
407 //graphicsView->centerOn(l.at(0));
413 updateCoordinates(l.at(0));
414 updateDimension(l.at(0));
415 updateRotZoom(l.at(0));
418 checkButton(TITLE_SELECT);
421 void TitleWidget::slotImageTool()
423 // TODO: find a way to get a list of all supported image types...
424 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";
425 KUrl url = KFileDialog::getOpenUrl(KUrl(), allExtensions, this, i18n("Load Image")); //"*.svg *.png *.jpg *.jpeg *.gif *.raw"
426 if (!url.isEmpty()) {
427 if (url.path().endsWith(".svg")) {
428 QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
429 svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
430 svg->setZValue(m_count++);
431 svg->setData(Qt::UserRole, url.path());
432 graphicsView->scene()->addItem(svg);
434 QPixmap pix(url.path());
435 QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
436 image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
437 image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
438 image->setData(Qt::UserRole, url.path());
439 image->setZValue(m_count++);
440 graphicsView->scene()->addItem(image);
443 m_scene->setTool(TITLE_SELECT);
444 showToolbars(TITLE_SELECT);
445 checkButton(TITLE_NONE);
448 void TitleWidget::showToolbars(TITLETOOL toolType)
452 rect_properties->setHidden(true);
453 image_properties->setHidden(true);
454 no_properties->setHidden(true);
455 text_properties->setHidden(false);
457 case TITLE_RECTANGLE:
458 image_properties->setHidden(true);
459 no_properties->setHidden(true);
460 text_properties->setHidden(true);
461 rect_properties->setHidden(false);
464 no_properties->setHidden(true);
465 text_properties->setHidden(true);
466 rect_properties->setHidden(true);
467 image_properties->setHidden(false);
470 text_properties->setHidden(true);
471 rect_properties->setHidden(true);
472 image_properties->setHidden(true);
473 no_properties->setHidden(false);
478 void TitleWidget::enableToolbars(TITLETOOL toolType)
480 // TITLETOOL is defined in graphicsscenerectmove.h
485 bool bValue_w = false;
486 bool bValue_h = false;
495 case TITLE_RECTANGLE:
510 frame_properties->setEnabled(bFrame);
511 text_properties->setEnabled(bText);
512 rect_properties->setEnabled(bRect);
513 image_properties->setEnabled(bImage);
514 value_w->setEnabled(bValue_w);
515 value_h->setEnabled(bValue_h);
518 void TitleWidget::checkButton(TITLETOOL toolType)
520 bool bSelect = false;
532 case TITLE_RECTANGLE:
542 m_buttonCursor->setChecked(bSelect);
543 m_buttonText->setChecked(bText);
544 m_buttonRect->setChecked(bRect);
545 m_buttonImage->setChecked(bImage);
548 void TitleWidget::displayBackgroundFrame()
550 if (!displayBg->isChecked()) {
551 QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
552 QPixmap pattern(20, 20);
554 QColor bgcolor(210, 210, 210);
557 p.fillRect(QRect(0, 0, 10, 10), bgcolor);
558 p.fillRect(QRect(10, 10, 20, 20), bgcolor);
563 p.fillRect(bg.rect(), br);
565 m_frameImage->setPixmap(bg);
567 m_frameImage->setPixmap(QPixmap::fromImage(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2)));
571 void TitleWidget::initAnimation()
573 align_box->setEnabled(false);
574 QPen startpen(Qt::DotLine);
575 QPen endpen(Qt::DashDotLine);
576 startpen.setColor(QColor(100, 200, 100, 140));
577 endpen.setColor(QColor(200, 100, 100, 140));
579 m_startViewport->setPen(startpen);
580 m_endViewport->setPen(endpen);
582 m_startViewport->setZValue(-1000);
583 m_endViewport->setZValue(-1000);
585 m_startViewport->setFlags(0);
586 m_endViewport->setFlags(0);
588 graphicsView->scene()->addItem(m_startViewport);
589 graphicsView->scene()->addItem(m_endViewport);
591 connect(keep_aspect, SIGNAL(toggled(bool)), this, SLOT(slotKeepAspect(bool)));
592 connect(resize50, SIGNAL(clicked()), this, SLOT(slotResize50()));
593 connect(resize100, SIGNAL(clicked()), this, SLOT(slotResize100()));
594 connect(resize200, SIGNAL(clicked()), this, SLOT(slotResize200()));
597 void TitleWidget::slotUpdateZoom(int pos)
599 m_scene->setZoom((double) pos / 100);
600 zoom_label->setText(QString::number(pos) + '%');
603 void TitleWidget::slotZoom(bool up)
605 int pos = zoom_slider->value();
608 zoom_slider->setValue(pos);
611 void TitleWidget::slotAdjustZoom()
613 /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
614 double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
615 if (scalex > scaley) scalex = scaley;
616 int zoompos = (int)(scalex * 7 + 0.5);*/
617 graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
618 int zoompos = graphicsView->matrix().m11() * 100;
619 zoom_slider->setValue(zoompos);
620 graphicsView->centerOn(m_frameBorder);
623 void TitleWidget::slotZoomOneToOne()
625 zoom_slider->setValue(100);
626 graphicsView->centerOn(m_frameBorder);
629 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
631 updateAxisButtons(rect); // back to default
633 QColor f = rectFColor->color();
634 f.setAlpha(rectFAlpha->value());
636 penf.setWidth(rectLineWidth->value());
638 QColor b = rectBColor->color();
639 b.setAlpha(rectBAlpha->value());
640 rect->setBrush(QBrush(b));
641 rect->setZValue(m_count++);
642 //setCurrentItem(rect);
643 //graphicsView->setFocus();
646 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
648 updateAxisButtons(tt); // back to default
650 QFont font = font_family->currentFont();
651 font.setPixelSize(font_size->value());
653 font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
654 font.setItalic(buttonItalic->isChecked());
655 font.setUnderline(buttonUnder->isChecked());
658 QColor color = fontColorButton->color();
659 color.setAlpha(textAlpha->value());
660 tt->setDefaultTextColor(color);
661 tt->setZValue(m_count++);
665 void TitleWidget::setFontBoxWeight(int weight)
667 int index = font_weight_box->findData(weight);
669 index = font_weight_box->findData(QFont::Normal);
671 font_weight_box->setCurrentIndex(index);
674 void TitleWidget::setCurrentItem(QGraphicsItem *item)
676 m_scene->setSelectedItem(item);
679 void TitleWidget::zIndexChanged(int v)
681 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
687 void TitleWidget::selectionChanged()
689 if (m_scene->tool() != TITLE_SELECT) return;
690 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
691 //toolBox->setItemEnabled(2, false);
692 //toolBox->setItemEnabled(3, false);
693 value_x->blockSignals(true);
694 value_y->blockSignals(true);
695 value_w->blockSignals(true);
696 value_h->blockSignals(true);
697 itemzoom->blockSignals(true);
698 itemrotate->blockSignals(true);
700 bool blockX = !origin_x_left->signalsBlocked();
701 bool blockY = !origin_y_top->signalsBlocked();
702 if (blockX) origin_x_left->blockSignals(true);
703 if (blockY) origin_y_top->blockSignals(true);
704 origin_x_left->setChecked(false);
705 origin_y_top->setChecked(false);
708 enableToolbars(TITLE_NONE);
709 if (blockX) origin_x_left->blockSignals(false);
710 if (blockY) origin_y_top->blockSignals(false);
711 itemzoom->setEnabled(false);
712 itemrotate->setEnabled(false);
713 } else if (l.size() == 1) {
714 if (l.at(0) != m_startViewport && l.at(0) != m_endViewport) {
715 itemzoom->setEnabled(true);
716 itemrotate->setEnabled(true);
718 itemzoom->setEnabled(false);
719 itemrotate->setEnabled(false);
722 if (l.at(0)->type() == TEXTITEM) {
723 showToolbars(TITLE_TEXT);
724 QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
725 //if (l[0]->hasFocus())
726 //toolBox->setCurrentIndex(0);
727 //toolBox->setItemEnabled(2, true);
728 font_size->blockSignals(true);
729 font_family->blockSignals(true);
730 font_weight_box->blockSignals(true);
731 buttonItalic->blockSignals(true);
732 buttonUnder->blockSignals(true);
733 fontColorButton->blockSignals(true);
734 textAlpha->blockSignals(true);
735 buttonAlignLeft->blockSignals(true);
736 buttonAlignRight->blockSignals(true);
737 buttonAlignNone->blockSignals(true);
738 buttonAlignCenter->blockSignals(true);
740 QFont font = i->font();
741 font_family->setCurrentFont(font);
742 font_size->setValue(font.pixelSize());
743 buttonItalic->setChecked(font.italic());
744 buttonUnder->setChecked(font.underline());
745 setFontBoxWeight(font.weight());
747 QColor color = i->defaultTextColor();
748 fontColorButton->setColor(color);
749 textAlpha->setValue(color.alpha());
751 QTextCursor cur = i->textCursor();
752 QTextBlockFormat format = cur.blockFormat();
753 if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
754 else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
755 else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
756 else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
758 font_size->blockSignals(false);
759 font_family->blockSignals(false);
760 font_weight_box->blockSignals(false);
761 buttonItalic->blockSignals(false);
762 buttonUnder->blockSignals(false);
763 fontColorButton->blockSignals(false);
764 textAlpha->blockSignals(false);
765 buttonAlignLeft->blockSignals(false);
766 buttonAlignRight->blockSignals(false);
767 buttonAlignNone->blockSignals(false);
768 buttonAlignCenter->blockSignals(false);
770 updateAxisButtons(i);
771 updateCoordinates(i);
773 enableToolbars(TITLE_TEXT);
775 } else if ((l.at(0))->type() == RECTITEM) {
776 showToolbars(TITLE_RECTANGLE);
778 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
779 if (rec == m_startViewport || rec == m_endViewport) {
780 /*toolBox->setCurrentIndex(3);
781 toolBox->widget(0)->setEnabled(false);
782 toolBox->widget(1)->setEnabled(false);*/
783 enableToolbars(TITLE_NONE);
785 /*toolBox->widget(0)->setEnabled(true);
786 toolBox->widget(1)->setEnabled(true);
787 toolBox->setCurrentIndex(0);*/
788 //toolBox->setItemEnabled(3, true);
789 rectFAlpha->setValue(rec->pen().color().alpha());
790 rectBAlpha->setValue(rec->brush().color().alpha());
791 //kDebug() << rec->brush().color().alpha();
792 QColor fcol = rec->pen().color();
793 QColor bcol = rec->brush().color();
794 //fcol.setAlpha(255);
795 //bcol.setAlpha(255);
796 rectFColor->setColor(fcol);
797 rectBColor->setColor(bcol);
799 rectLineWidth->setValue(rec->pen().width());
800 enableToolbars(TITLE_RECTANGLE);
803 updateAxisButtons(l.at(0));
804 updateCoordinates(rec);
805 updateDimension(rec);
807 } else if (l.at(0)->type() == IMAGEITEM) {
808 showToolbars(TITLE_IMAGE);
810 updateCoordinates(l.at(0));
811 updateDimension(l.at(0));
813 enableToolbars(TITLE_IMAGE);
816 //toolBox->setCurrentIndex(0);
817 showToolbars(TITLE_NONE);
818 enableToolbars(TITLE_NONE);
819 /*frame_properties->setEnabled(false);
820 text_properties->setEnabled(false);
821 rect_properties->setEnabled(false);*/
823 zValue->setValue((int)l.at(0)->zValue());
824 itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
825 itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
826 value_x->blockSignals(false);
827 value_y->blockSignals(false);
828 value_w->blockSignals(false);
829 value_h->blockSignals(false);
830 itemzoom->blockSignals(false);
831 itemrotate->blockSignals(false);
835 void TitleWidget::slotValueChanged(int type)
837 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
838 if (l.size() > 0 && l.at(0)->type() == IMAGEITEM) {
843 val = value_w->value();
846 val = value_h->value();
850 QGraphicsItem *i = l.at(0);
851 Transform t = m_transformations.value(i);
853 // Ratio width:height
854 double phi = (double) i->boundingRect().width() / i->boundingRect().height();
855 double alpha = (double) t.rotate / 180.0 * M_PI;
865 // Add 0.5 because otherwise incrementing by 1 might have no effect
866 length = val / (cos(alpha) + 1 / phi * sin(alpha)) + 0.5;
867 scale = length / i->boundingRect().width();
870 length = val / (phi * sin(alpha) + cos(alpha)) + 0.5;
871 scale = length / i->boundingRect().height();
878 qtrans.scale(scale, scale);
879 qtrans.rotate(t.rotate);
880 i->setTransform(qtrans);
881 m_transformations[i] = t;
888 /** \brief Updates position/size of the selected item when a value
889 * of an item (coordinates, size) has changed */
890 void TitleWidget::slotAdjustSelectedItem()
892 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
894 if (l.at(0)->type() == RECTITEM) {
896 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
898 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
899 } else if (l.at(0)->type() == TEXTITEM) {
901 updatePosition(l.at(0));
902 } else if (l.at(0)->type() == IMAGEITEM) {
904 updatePosition(l.at(0));
909 /** \brief Updates width/height int the text fields, regarding transformation matrix */
910 void TitleWidget::updateDimension(QGraphicsItem *i)
912 value_w->blockSignals(true);
913 value_h->blockSignals(true);
916 if (i->type() == IMAGEITEM) {
917 // Get multipliers for rotation/scaling
919 /*Transform t = m_transformations.value(i);
920 QRectF r = i->boundingRect();
921 int width = (int) ( abs(r.width()*t.scalex * cos(t.rotate/180.0*M_PI))
922 + abs(r.height()*t.scaley * sin(t.rotate/180.0*M_PI)) );
923 int height = (int) ( abs(r.height()*t.scaley * cos(t.rotate/180*M_PI))
924 + abs(r.width()*t.scalex * sin(t.rotate/180*M_PI)) );*/
926 value_w->setValue(i->sceneBoundingRect().width());
927 value_h->setValue(i->sceneBoundingRect().height());
928 } else if (i->type() == RECTITEM) {
929 QGraphicsRectItem *r = static_cast <QGraphicsRectItem *>(i);
930 value_w->setValue((int) r->rect().width());
931 value_h->setValue((int) r->rect().height());
932 } else if (i->type() == TEXTITEM) {
933 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(i);
934 value_w->setValue((int) t->boundingRect().width());
935 value_h->setValue((int) t->boundingRect().height());
938 value_w->blockSignals(false);
939 value_h->blockSignals(false);
942 /** \brief Updates the coordinates in the text fields from the item */
943 void TitleWidget::updateCoordinates(QGraphicsItem *i)
945 // Block signals emitted by this method
946 value_x->blockSignals(true);
947 value_y->blockSignals(true);
949 if (i->type() == TEXTITEM) {
951 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
953 // Set the correct x coordinate value
954 if (origin_x_left->isChecked()) {
955 // Origin (0 point) is at m_frameWidth, coordinate axis is inverted
956 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->boundingRect().width()));
958 // Origin is at 0 (default)
959 value_x->setValue((int) rec->pos().x());
963 if (origin_y_top->isChecked()) {
964 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->boundingRect().height()));
966 value_y->setValue((int) rec->pos().y());
969 } else if (i->type() == RECTITEM) {
971 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
973 if (origin_x_left->isChecked()) {
974 // Origin (0 point) is at m_frameWidth
975 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->rect().width()));
977 // Origin is at 0 (default)
978 value_x->setValue((int) rec->pos().x());
981 if (origin_y_top->isChecked()) {
982 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->rect().height()));
984 value_y->setValue((int) rec->pos().y());
987 } else if (i->type() == IMAGEITEM) {
989 if (origin_x_left->isChecked()) {
990 value_x->setValue((int)(m_frameWidth - i->pos().x() - i->sceneBoundingRect().width()));
992 value_x->setValue((int) i->pos().x());
995 if (origin_y_top->isChecked()) {
996 value_y->setValue((int)(m_frameHeight - i->pos().y() - i->sceneBoundingRect().height()));
998 value_y->setValue((int) i->pos().y());
1003 // Stop blocking signals now
1004 value_x->blockSignals(false);
1005 value_y->blockSignals(false);
1008 void TitleWidget::updateRotZoom(QGraphicsItem *i)
1010 itemzoom->blockSignals(true);
1011 itemrotate->blockSignals(false);
1013 Transform t = m_transformations.value(i);
1014 itemzoom->setValue((int)(t.scalex * 100.0 + 0.5));
1015 itemrotate->setValue((int)(t.rotate));
1017 itemzoom->blockSignals(false);
1018 itemrotate->blockSignals(false);
1021 /** \brief Updates the position of an item by reading coordinates from the text fields */
1022 void TitleWidget::updatePosition(QGraphicsItem *i)
1024 if (i->type() == TEXTITEM) {
1025 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
1028 if (origin_x_left->isChecked()) {
1029 /* Origin of the x axis is at m_frameWidth,
1030 * and distance from right border of the item to the right
1031 * border of the frame is taken.
1032 * See comment to slotOriginXClicked().
1034 posX = m_frameWidth - value_x->value() - rec->boundingRect().width();
1036 posX = value_x->value();
1040 if (origin_y_top->isChecked()) {
1041 /* Same for y axis */
1042 posY = m_frameHeight - value_y->value() - rec->boundingRect().height();
1044 posY = value_y->value();
1047 rec->setPos(posX, posY);
1049 } else if (i->type() == RECTITEM) {
1051 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
1054 if (origin_x_left->isChecked()) {
1055 posX = m_frameWidth - value_x->value() - rec->rect().width();
1057 posX = value_x->value();
1061 if (origin_y_top->isChecked()) {
1062 posY = m_frameHeight - value_y->value() - rec->rect().height();
1064 posY = value_y->value();
1067 rec->setPos(posX, posY);
1069 } else if (i->type() == IMAGEITEM) {
1071 if (origin_x_left->isChecked()) {
1072 // Use the sceneBoundingRect because this also regards transformations like zoom
1073 posX = m_frameWidth - value_x->value() - i->sceneBoundingRect().width();
1075 posX = value_x->value();
1079 if (origin_y_top->isChecked()) {
1080 posY = m_frameHeight - value_y->value() - i->sceneBoundingRect().height();
1082 posY = value_y->value();
1085 i->setPos(posX, posY);
1091 void TitleWidget::updateTextOriginX()
1093 if (origin_x_left->isChecked()) {
1094 origin_x_left->setText(i18n("\u2212X"));
1096 origin_x_left->setText(i18n("+X"));
1100 void TitleWidget::slotOriginXClicked()
1102 // Update the text displayed on the button.
1103 updateTextOriginX();
1105 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1106 if (l.size() >= 1) {
1107 updateCoordinates(l.at(0));
1109 // Remember x axis setting
1110 l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked() ?
1111 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1113 graphicsView->setFocus();
1116 void TitleWidget::updateTextOriginY()
1118 if (origin_y_top->isChecked()) {
1119 origin_y_top->setText(i18n("\u2212Y"));
1121 origin_y_top->setText(i18n("+Y"));
1125 void TitleWidget::slotOriginYClicked()
1127 // Update the text displayed on the button.
1128 updateTextOriginY();
1130 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1131 if (l.size() >= 1) {
1132 updateCoordinates(l.at(0));
1134 l.at(0)->setData(TitleDocument::OriginYTop, origin_y_top->isChecked() ?
1135 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1138 graphicsView->setFocus();
1141 void TitleWidget::updateAxisButtons(QGraphicsItem *i)
1143 int xAxis = i->data(TitleDocument::OriginXLeft).toInt();
1144 int yAxis = i->data(TitleDocument::OriginYTop).toInt();
1145 origin_x_left->blockSignals(true);
1146 origin_y_top->blockSignals(true);
1148 if (xAxis == TitleDocument::AxisInverted) {
1149 origin_x_left->setChecked(true);
1151 origin_x_left->setChecked(false);
1153 updateTextOriginX();
1155 if (yAxis == TitleDocument::AxisInverted) {
1156 origin_y_top->setChecked(true);
1158 origin_y_top->setChecked(false);
1160 updateTextOriginY();
1162 origin_x_left->blockSignals(false);
1163 origin_y_top->blockSignals(false);
1166 void TitleWidget::slotChangeBackground()
1168 QColor color = kcolorbutton->color();
1169 color.setAlpha(horizontalSlider->value());
1170 m_frameBorder->setBrush(QBrush(color));
1174 * Something (yeah) has changed in our QGraphicsScene.
1176 void TitleWidget::slotChanged()
1178 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1179 if (l.size() >= 1 && l.at(0)->type() == TEXTITEM) {
1180 textChanged(static_cast <QGraphicsTextItem *>(l.at(0)));
1185 * If the user has set origin_x_left (everything also for y),
1186 * we need to look whether a text element has been selected. If yes,
1187 * we need to ensure that the right border of the text field
1188 * remains fixed also when some text has been entered.
1190 * This is also known as right-justified, with the difference that
1191 * it is not valid for text but for its boundingRect. Text may still
1192 * be left-justified.
1194 void TitleWidget::textChanged(QGraphicsTextItem *i)
1199 if (origin_x_left->isChecked() || origin_y_top->isChecked()) {
1201 if (!i->toPlainText().isEmpty()) {
1205 * Don't do anything if the string is empty. If the position
1206 * would be updated here, a newly created text field would
1207 * be set to the position of the last selected text field.
1213 void TitleWidget::slotInsertUnicode()
1215 m_unicodeDialog->exec();
1218 void TitleWidget::slotInsertUnicodeString(QString text)
1220 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
1222 if (l.at(0)->type() == TEXTITEM) {
1223 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(l.at(0));
1224 t->textCursor().insertText(text);
1229 void TitleWidget::slotUpdateText()
1231 QFont font = font_family->currentFont();
1232 font.setPixelSize(font_size->value());
1233 font.setItalic(buttonItalic->isChecked());
1234 font.setUnderline(buttonUnder->isChecked());
1235 font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1236 QColor color = fontColorButton->color();
1237 color.setAlpha(textAlpha->value());
1239 QGraphicsTextItem* item = NULL;
1240 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1241 if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
1242 item = static_cast <QGraphicsTextItem *>(l.at(0));
1245 //if (item->textCursor().selection ().isEmpty())
1246 QTextCursor cur = item->textCursor();
1247 QTextBlockFormat format = cur.blockFormat();
1248 if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
1249 item->setTextWidth(item->boundingRect().width());
1250 if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
1251 else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
1252 else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
1254 format.setAlignment(Qt::AlignLeft);
1255 item->setTextWidth(-1);
1259 item->setFont(font);
1260 item->setDefaultTextColor(color);
1261 cur.select(QTextCursor::Document);
1262 cur.setBlockFormat(format);
1263 item->setTextCursor(cur);
1264 cur.clearSelection();
1265 item->setTextCursor(cur);
1270 void TitleWidget::rectChanged()
1272 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1273 if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
1274 QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
1275 QColor f = rectFColor->color();
1276 f.setAlpha(rectFAlpha->value());
1278 penf.setWidth(rectLineWidth->value());
1280 QColor b = rectBColor->color();
1281 b.setAlpha(rectBAlpha->value());
1282 rec->setBrush(QBrush(b));
1286 void TitleWidget::itemScaled(int val)
1288 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1289 if (l.size() == 1) {
1290 Transform x = m_transformations.value(l.at(0));
1291 x.scalex = (double)val / 100.0;
1292 x.scaley = (double)val / 100.0;
1294 qtrans.scale(x.scalex, x.scaley);
1295 qtrans.rotate(x.rotate);
1296 l[0]->setTransform(qtrans);
1297 m_transformations[l.at(0)] = x;
1298 updateDimension(l.at(0));
1302 void TitleWidget::itemRotate(int val)
1304 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1305 if (l.size() == 1) {
1306 Transform x = m_transformations[l.at(0)];
1307 x.rotate = (double)val;
1309 qtrans.scale(x.scalex, x.scaley);
1310 qtrans.rotate(x.rotate);
1311 l[0]->setTransform(qtrans);
1312 m_transformations[l.at(0)] = x;
1313 updateDimension(l.at(0));
1317 void TitleWidget::itemHCenter()
1319 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1320 if (l.size() == 1) {
1321 QGraphicsItem *item = l.at(0);
1322 QRectF br = item->sceneBoundingRect();
1323 int width = (int)br.width();
1324 int newPos = (int)((m_frameWidth - width) / 2);
1325 newPos += item->pos().x() - br.left(); // Check item transformation
1326 item->setPos(newPos, item->pos().y());
1327 updateCoordinates(item);
1331 void TitleWidget::itemVCenter()
1333 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1334 if (l.size() == 1) {
1335 QGraphicsItem *item = l.at(0);
1336 QRectF br = item->sceneBoundingRect();
1337 int height = (int)br.height();
1338 int newPos = (int)((m_frameHeight - height) / 2);
1339 newPos += item->pos().y() - br.top(); // Check item transformation
1340 item->setPos(item->pos().x(), newPos);
1341 updateCoordinates(item);
1345 void TitleWidget::itemTop()
1347 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1348 if (l.size() == 1) {
1349 QGraphicsItem *item = l.at(0);
1350 QRectF br = item->sceneBoundingRect();
1352 if (br.top() > 0) diff = -br.top();
1353 else diff = -br.bottom();
1354 item->moveBy(0, diff);
1355 updateCoordinates(item);
1359 void TitleWidget::itemBottom()
1361 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1362 if (l.size() == 1) {
1363 QGraphicsItem *item = l.at(0);
1364 QRectF br = item->sceneBoundingRect();
1366 if (br.bottom() > m_frameHeight) diff = m_frameHeight - br.top();
1367 else diff = m_frameHeight - br.bottom();
1368 item->moveBy(0, diff);
1369 updateCoordinates(item);
1373 void TitleWidget::itemLeft()
1375 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1376 if (l.size() == 1) {
1377 QGraphicsItem *item = l.at(0);
1378 QRectF br = item->sceneBoundingRect();
1380 if (br.left() > 0) diff = -br.left();
1381 else diff = -br.right();
1382 item->moveBy(diff, 0);
1383 updateCoordinates(item);
1387 void TitleWidget::itemRight()
1389 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1390 if (l.size() == 1) {
1391 QGraphicsItem *item = l.at(0);
1392 QRectF br = item->sceneBoundingRect();
1394 if (br.right() < m_frameWidth) diff = m_frameWidth - br.right();
1395 else diff = m_frameWidth - br.left();
1396 item->moveBy(diff, 0);
1397 updateCoordinates(item);
1401 void TitleWidget::setupViewports()
1403 //double aspect_ratio = 4.0 / 3.0;//read from project
1404 //better zoom centered, but render uses only the created rect, so no problem to change the zoom function
1405 /*QRectF sp(0, 0, startViewportSize->value() * m_frameWidth / 100.0 , startViewportSize->value()* m_frameHeight / 100.0);
1406 QRectF ep(0, 0, endViewportSize->value() * m_frameWidth / 100.0, endViewportSize->value() * m_frameHeight / 100.0);
1407 // use a polygon thiat uses 16:9 and 4:3 rects forpreview the size in all aspect ratios ?
1408 QPolygonF spoly(sp);
1409 QPolygonF epoly(ep);
1410 spoly.translate(startViewportX->value(), startViewportY->value());
1411 epoly.translate(endViewportX->value(), endViewportY->value());
1412 m_startViewport->setPolygon(spoly);
1413 m_endViewport->setPolygon(epoly);
1414 if (! insertingValues) {
1415 m_startViewport->setData(0, startViewportX->value());
1416 m_startViewport->setData(1, startViewportY->value());
1417 m_startViewport->setData(2, startViewportSize->value());
1419 m_endViewport->setData(0, endViewportX->value());
1420 m_endViewport->setData(1, endViewportY->value());
1421 m_endViewport->setData(2, endViewportSize->value());
1425 void TitleWidget::loadTitle(KUrl url)
1427 if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this, i18n("Load Title"));
1428 if (!url.isEmpty()) {
1429 QList<QGraphicsItem *> items = m_scene->items();
1430 for (int i = 0; i < items.size(); i++) {
1431 if (items.at(i)->zValue() > -1000) delete items.at(i);
1433 m_scene->clearTextSelection();
1437 if (KIO::NetAccess::download(url, tmpfile, 0)) {
1438 QFile file(tmpfile);
1439 if (file.open(QIODevice::ReadOnly)) {
1440 doc.setContent(&file, false);
1443 KIO::NetAccess::removeTempFile(tmpfile);
1448 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport, &out) + 1;
1450 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1451 insertingValues = true;
1452 startViewportX->setValue(m_startViewport->data(0).toInt());
1453 startViewportY->setValue(m_startViewport->data(1).toInt());
1454 startViewportSize->setValue(m_startViewport->data(2).toInt());
1455 endViewportX->setValue(m_endViewport->data(0).toInt());
1456 endViewportY->setValue(m_endViewport->data(1).toInt());
1457 endViewportSize->setValue(m_endViewport->data(2).toInt());
1459 insertingValues = false;
1465 void TitleWidget::saveTitle(KUrl url)
1467 if (anim_start->isChecked()) slotAnimStart(false);
1468 if (anim_end->isChecked()) slotAnimEnd(false);
1469 if (url.isEmpty()) {
1470 KFileDialog *fs = new KFileDialog(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this);
1471 fs->setOperationMode(KFileDialog::Saving);
1472 fs->setMode(KFile::File);
1473 #if KDE_IS_VERSION(4,2,0)
1474 fs->setConfirmOverwrite(true);
1476 fs->setKeepLocation(true);
1478 url = fs->selectedUrl();
1481 if (!url.isEmpty()) {
1482 if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport, m_tc.getFrameCount(title_duration->text())) == false)
1483 KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
1487 QDomDocument TitleWidget::xml()
1489 QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
1490 doc.documentElement().setAttribute("out", m_tc.getFrameCount(title_duration->text()));
1494 int TitleWidget::duration() const
1496 return m_tc.getFrameCount(title_duration->text());
1499 void TitleWidget::setXml(QDomDocument doc)
1502 m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport, &out);
1504 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1505 /*if (doc.documentElement().hasAttribute("out")) {
1506 GenTime duration = GenTime(doc.documentElement().attribute("out").toDouble() / 1000.0);
1507 title_duration->setText(m_tc.getTimecode(duration));
1509 else title_duration->setText(m_tc.getTimecode(GenTime(5000)));*/
1511 QDomElement e = doc.documentElement();
1512 m_transformations.clear();
1513 QList <QGraphicsItem *> items = graphicsView->scene()->items();
1514 const double PI = 4.0 * atan(1.0);
1515 for (int i = 0; i < items.count(); i++) {
1516 QTransform t = items.at(i)->transform();
1520 x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
1521 m_transformations[items.at(i)] = x;
1523 // mbd: Update the GUI color selectors to match the stuff from the loaded document
1524 QColor background_color = m_titledocument.getBackgroundColor();
1525 horizontalSlider->blockSignals(true);
1526 kcolorbutton->blockSignals(true);
1527 horizontalSlider->setValue(background_color.alpha());
1528 background_color.setAlpha(255);
1529 kcolorbutton->setColor(background_color);
1530 horizontalSlider->blockSignals(false);
1531 kcolorbutton->blockSignals(false);
1533 /*startViewportX->setValue(m_startViewport->data(0).toInt());
1534 startViewportY->setValue(m_startViewport->data(1).toInt());
1535 startViewportSize->setValue(m_startViewport->data(2).toInt());
1536 endViewportX->setValue(m_endViewport->data(0).toInt());
1537 endViewportY->setValue(m_endViewport->data(1).toInt());
1538 endViewportSize->setValue(m_endViewport->data(2).toInt());*/
1540 QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
1544 /** \brief Connected to the accepted signal - calls writeChoices */
1545 void TitleWidget::slotAccepted()
1547 if (anim_start->isChecked()) slotAnimStart(false);
1548 if (anim_end->isChecked()) slotAnimEnd(false);
1552 /** \brief Store the current choices of font, background and rect values */
1553 void TitleWidget::writeChoices()
1555 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1556 KSharedConfigPtr config = KGlobal::config();
1557 KConfigGroup titleConfig(config, "TitleWidget");
1558 // Write the entries
1559 titleConfig.writeEntry("font_family", font_family->currentFont());
1560 //titleConfig.writeEntry("font_size", font_size->value());
1561 titleConfig.writeEntry("font_pixel_size", font_size->value());
1562 titleConfig.writeEntry("font_color", fontColorButton->color());
1563 titleConfig.writeEntry("font_alpha", textAlpha->value());
1564 titleConfig.writeEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1565 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
1566 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
1568 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
1569 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
1570 titleConfig.writeEntry("rect_background_color", rectBColor->color());
1571 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
1572 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
1574 titleConfig.writeEntry("background_color", kcolorbutton->color());
1575 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
1577 //! \todo Not sure if I should sync - it is probably safe to do it
1582 /** \brief Read the last stored choices into the dialog */
1583 void TitleWidget::readChoices()
1585 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1586 KSharedConfigPtr config = KGlobal::config();
1587 KConfigGroup titleConfig(config, "TitleWidget");
1589 font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
1590 font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
1591 fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
1592 textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
1594 if (titleConfig.readEntry("font_bold", false)) weight = QFont::Bold;
1595 else weight = titleConfig.readEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1596 setFontBoxWeight(weight);
1597 buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
1598 buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
1600 rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
1601 rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
1602 rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
1603 rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
1604 rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
1606 kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
1607 horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
1610 void TitleWidget::adjustFrameSize()
1612 m_frameWidth = m_titledocument.frameWidth();
1613 m_frameHeight = m_titledocument.frameHeight();
1614 m_frameBorder->setRect(0, 0, m_frameWidth, m_frameHeight);
1615 displayBackgroundFrame();
1618 void TitleWidget::slotAnimStart(bool anim)
1620 if (anim && anim_end->isChecked()) {
1621 anim_end->setChecked(false);
1622 m_endViewport->setZValue(-1000);
1623 m_endViewport->setBrush(QBrush());
1626 QList<QGraphicsItem *> list = m_scene->items();
1627 for (int i = 0; i < list.count(); i++) {
1628 if (list.at(i)->zValue() > -1000) {
1629 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1630 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1633 align_box->setEnabled(anim);
1634 itemzoom->setEnabled(!anim);
1635 itemrotate->setEnabled(!anim);
1636 frame_toolbar->setEnabled(!anim);
1637 rect_properties->setEnabled(!anim);
1639 keep_aspect->setChecked(!m_startViewport->data(0).isNull());
1640 m_startViewport->setZValue(1100);
1641 QColor col = m_startViewport->pen().color();
1643 m_startViewport->setBrush(col);
1644 m_startViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1645 m_startViewport->setSelected(true);
1648 if (m_startViewport->childItems().isEmpty()) addAnimInfoText();
1650 m_startViewport->setZValue(-1000);
1651 m_startViewport->setBrush(QBrush());
1652 m_startViewport->setFlags(0);
1653 if (!anim_end->isChecked()) deleteAnimInfoText();
1658 void TitleWidget::slotAnimEnd(bool anim)
1660 if (anim && anim_start->isChecked()) {
1661 anim_start->setChecked(false);
1662 m_startViewport->setZValue(-1000);
1663 m_startViewport->setBrush(QBrush());
1666 QList<QGraphicsItem *> list = m_scene->items();
1667 for (int i = 0; i < list.count(); i++) {
1668 if (list.at(i)->zValue() > -1000) {
1669 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1670 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1673 align_box->setEnabled(anim);
1674 itemzoom->setEnabled(!anim);
1675 itemrotate->setEnabled(!anim);
1676 frame_toolbar->setEnabled(!anim);
1677 rect_properties->setEnabled(!anim);
1679 keep_aspect->setChecked(!m_endViewport->data(0).isNull());
1680 m_endViewport->setZValue(1100);
1681 QColor col = m_endViewport->pen().color();
1683 m_endViewport->setBrush(col);
1684 m_endViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1685 m_endViewport->setSelected(true);
1688 if (m_endViewport->childItems().isEmpty()) addAnimInfoText();
1690 m_endViewport->setZValue(-1000);
1691 m_endViewport->setBrush(QBrush());
1692 m_endViewport->setFlags(0);
1693 if (!anim_start->isChecked()) deleteAnimInfoText();
1697 void TitleWidget::addAnimInfoText()
1699 // add text to anim viewport
1700 QGraphicsTextItem *t = new QGraphicsTextItem(i18n("Start"), m_startViewport);
1701 QGraphicsTextItem *t2 = new QGraphicsTextItem(i18n("End"), m_endViewport);
1702 QFont font = t->font();
1703 font.setPixelSize(m_startViewport->rect().width() / 10);
1704 QColor col = m_startViewport->pen().color();
1706 t->setDefaultTextColor(col);
1708 font.setPixelSize(m_endViewport->rect().width() / 10);
1709 col = m_endViewport->pen().color();
1711 t2->setDefaultTextColor(col);
1715 void TitleWidget::updateInfoText()
1717 // update info text font
1718 if (!m_startViewport->childItems().isEmpty()) {
1719 QGraphicsTextItem *item = static_cast <QGraphicsTextItem *>(m_startViewport->childItems().at(0));
1721 QFont font = item->font();
1722 font.setPixelSize(m_startViewport->rect().width() / 10);
1723 item->setFont(font);
1726 if (!m_endViewport->childItems().isEmpty()) {
1727 QGraphicsTextItem *item = static_cast <QGraphicsTextItem *>(m_endViewport->childItems().at(0));
1729 QFont font = item->font();
1730 font.setPixelSize(m_endViewport->rect().width() / 10);
1731 item->setFont(font);
1736 void TitleWidget::deleteAnimInfoText()
1738 // end animation editing, remove info text
1739 while (!m_startViewport->childItems().isEmpty()) {
1740 QGraphicsItem *item = m_startViewport->childItems().at(0);
1741 m_scene->removeItem(item);
1744 while (!m_endViewport->childItems().isEmpty()) {
1745 QGraphicsItem *item = m_endViewport->childItems().at(0);
1746 m_scene->removeItem(item);
1752 void TitleWidget::slotKeepAspect(bool keep)
1754 if (m_endViewport->zValue() == 1100) {
1755 m_endViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1756 m_endViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1758 m_startViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1759 m_startViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1763 void TitleWidget::slotResize50()
1765 if (m_endViewport->zValue() == 1100) {
1766 m_endViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1767 } else m_startViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1770 void TitleWidget::slotResize100()
1772 if (m_endViewport->zValue() == 1100) {
1773 m_endViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1774 } else m_startViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1777 void TitleWidget::slotResize200()
1779 if (m_endViewport->zValue() == 1100) {
1780 m_endViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);
1781 } else m_startViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);