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;
47 TitleWidget::TitleWidget(KUrl url, Timecode tc, QString projectTitlePath, Render *render, QWidget *parent) :
50 m_startViewport(NULL),
54 m_unicodeDialog(new UnicodeDialog(UnicodeDialog::InputHex)),
55 m_projectTitlePath(projectTitlePath),
59 setFont(KGlobalSettings::toolBarFont());
60 frame_properties->setEnabled(false);
61 rect_properties->setFixedHeight(frame_properties->height() + 4);
62 no_properties->setFixedHeight(frame_properties->height() + 4);
63 image_properties->setFixedHeight(frame_properties->height() + 4);
64 text_properties->setFixedHeight(frame_properties->height() + 4);
65 frame_properties->setFixedHeight(frame_toolbar->height());
67 itemzoom->setSuffix(i18n("%"));
68 m_frameWidth = render->renderWidth();
69 m_frameHeight = render->renderHeight();
70 showToolbars(TITLE_NONE);
72 //TODO: get default title duration instead of hardcoded one
73 title_duration->setText(m_tc.getTimecode(GenTime(5000 / 1000.0)));
75 connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
76 connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
78 connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
79 connect(font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(slotUpdateText())) ;
80 connect(font_size, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText())) ;
81 connect(textAlpha, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateText()));
82 connect(font_weight_box, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateText()));
84 connect(rectFAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
85 connect(rectBAlpha, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
86 connect(rectFColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
87 connect(rectBColor, SIGNAL(clicked()), this, SLOT(rectChanged()));
88 connect(rectLineWidth, SIGNAL(valueChanged(int)), this, SLOT(rectChanged()));
90 /*connect(startViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
91 connect(startViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
92 connect(startViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
93 connect(endViewportX, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
94 connect(endViewportY, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));
95 connect(endViewportSize, SIGNAL(valueChanged(int)), this, SLOT(setupViewports()));*/
97 connect(zValue, SIGNAL(valueChanged(int)), this, SLOT(zIndexChanged(int)));
98 connect(itemzoom, SIGNAL(valueChanged(int)), this, SLOT(itemScaled(int)));
99 connect(itemrotate, SIGNAL(valueChanged(int)), this, SLOT(itemRotate(int)));
100 connect(itemhcenter, SIGNAL(clicked()), this, SLOT(itemHCenter()));
101 connect(itemvcenter, SIGNAL(clicked()), this, SLOT(itemVCenter()));
102 connect(itemtop, SIGNAL(clicked()), this, SLOT(itemTop()));
103 connect(itembottom, SIGNAL(clicked()), this, SLOT(itemBottom()));
104 connect(itemleft, SIGNAL(clicked()), this, SLOT(itemLeft()));
105 connect(itemright, SIGNAL(clicked()), this, SLOT(itemRight()));
107 connect(origin_x_left, SIGNAL(clicked()), this, SLOT(slotOriginXClicked()));
108 connect(origin_y_top, SIGNAL(clicked()), this, SLOT(slotOriginYClicked()));
110 m_signalMapper = new QSignalMapper(this);
111 m_signalMapper->setMapping(value_w, ValueWidth);
112 m_signalMapper->setMapping(value_h, ValueHeight);
113 connect(value_w, SIGNAL(valueChanged(int)), m_signalMapper, SLOT(map()));
114 connect(value_h, SIGNAL(valueChanged(int)), m_signalMapper, SLOT(map()));
115 connect(m_signalMapper, SIGNAL(mapped(int)), this, SLOT(slotValueChanged(int)));
117 connect(value_x, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
118 connect(value_y, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
119 connect(value_w, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
120 connect(value_h, SIGNAL(valueChanged(int)), this, SLOT(slotAdjustSelectedItem()));
121 connect(buttonFitZoom, SIGNAL(clicked()), this, SLOT(slotAdjustZoom()));
122 connect(buttonRealSize, SIGNAL(clicked()), this, SLOT(slotZoomOneToOne()));
123 connect(buttonItalic, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
124 connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
125 connect(buttonAlignLeft, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
126 connect(buttonAlignRight, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
127 connect(buttonAlignCenter, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
128 connect(buttonAlignNone, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
129 //connect(buttonInsertUnicode, SIGNAL(clicked()), this, SLOT(slotInsertUnicode()));
130 connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
132 connect(m_unicodeDialog, SIGNAL(charSelected(QString)), this, SLOT(slotInsertUnicodeString(QString)));
135 connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
137 font_weight_box->blockSignals(true);
138 font_weight_box->addItem(i18nc("Font style", "Light"), QFont::Light);
139 font_weight_box->addItem(i18nc("Font style", "Normal"), QFont::Normal);
140 font_weight_box->addItem(i18nc("Font style", "Demi-Bold"), QFont::DemiBold);
141 font_weight_box->addItem(i18nc("Font style", "Bold"), QFont::Bold);
142 font_weight_box->addItem(i18nc("Font style", "Black"), QFont::Black);
143 font_weight_box->setToolTip(i18n("Font weight"));
144 font_weight_box->setCurrentIndex(1);
145 font_weight_box->blockSignals(false);
147 buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
148 buttonRealSize->setIcon(KIcon("zoom-original"));
149 buttonItalic->setIcon(KIcon("format-text-italic"));
150 buttonUnder->setIcon(KIcon("format-text-underline"));
151 buttonAlignCenter->setIcon(KIcon("format-justify-center"));
152 buttonAlignLeft->setIcon(KIcon("format-justify-left"));
153 buttonAlignRight->setIcon(KIcon("format-justify-right"));
154 buttonAlignNone->setIcon(KIcon("kdenlive-align-none"));
156 buttonAlignNone->setToolTip(i18n("No alignment"));
157 buttonAlignRight->setToolTip(i18n("Align right"));
158 buttonAlignLeft->setToolTip(i18n("Align left"));
159 buttonAlignCenter->setToolTip(i18n("Align center"));
161 m_unicodeAction = new QAction(KIcon("kdenlive-insert-unicode"), QString(), this);
162 m_unicodeAction->setShortcut(Qt::SHIFT + Qt::CTRL + Qt::Key_U);
163 m_unicodeAction->setToolTip(i18n("Insert Unicode character") + ' ' + m_unicodeAction->shortcut().toString());
164 connect(m_unicodeAction, SIGNAL(triggered()), this, SLOT(slotInsertUnicode()));
165 buttonInsertUnicode->setDefaultAction(m_unicodeAction);
167 origin_x_left->setToolTip(i18n("Invert x axis and change 0 point"));
168 origin_y_top->setToolTip(i18n("Invert y axis and change 0 point"));
169 rectBColor->setToolTip(i18n("Select fill color"));
170 rectFColor->setToolTip(i18n("Select border color"));
171 rectBAlpha->setToolTip(i18n("Fill transparency"));
172 rectFAlpha->setToolTip(i18n("Border transparency"));
173 zoom_slider->setToolTip(i18n("Zoom"));
174 buttonRealSize->setToolTip(i18n("Original size (1:1)"));
175 buttonFitZoom->setToolTip(i18n("Fit zoom"));
176 kcolorbutton->setToolTip(i18n("Select background color"));
177 horizontalSlider->setToolTip(i18n("Background Transparency"));
179 itemhcenter->setIcon(KIcon("kdenlive-align-hor"));
180 itemhcenter->setToolTip(i18n("Align item horizontally"));
181 itemvcenter->setIcon(KIcon("kdenlive-align-vert"));
182 itemvcenter->setToolTip(i18n("Align item vertically"));
183 itemtop->setIcon(KIcon("kdenlive-align-top"));
184 itemtop->setToolTip(i18n("Align item to top"));
185 itembottom->setIcon(KIcon("kdenlive-align-bottom"));
186 itembottom->setToolTip(i18n("Align item to bottom"));
187 itemright->setIcon(KIcon("kdenlive-align-right"));
188 itemright->setToolTip(i18n("Align item to right"));
189 itemleft->setIcon(KIcon("kdenlive-align-left"));
190 itemleft->setToolTip(i18n("Align item to left"));
193 QHBoxLayout *layout = new QHBoxLayout;
194 frame_toolbar->setLayout(layout);
195 layout->setContentsMargins(2, 2, 2, 2);
196 QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
198 m_buttonCursor = m_toolbar->addAction(KIcon("transform-move"), QString());
199 m_buttonCursor->setCheckable(true);
200 m_buttonCursor->setShortcut(Qt::ALT + Qt::Key_S);
201 m_buttonCursor->setToolTip(i18n("Selection Tool") + ' ' + m_buttonCursor->shortcut().toString());
202 connect(m_buttonCursor, SIGNAL(triggered()), this, SLOT(slotSelectTool()));
204 m_buttonText = m_toolbar->addAction(KIcon("insert-text"), QString());
205 m_buttonText->setCheckable(true);
206 m_buttonText->setShortcut(Qt::ALT + Qt::Key_T);
207 m_buttonText->setToolTip(i18n("Add Text") + ' ' + m_buttonText->shortcut().toString());
208 connect(m_buttonText, SIGNAL(triggered()), this, SLOT(slotTextTool()));
210 m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), QString());
211 m_buttonRect->setCheckable(true);
212 m_buttonRect->setShortcut(Qt::ALT + Qt::Key_R);
213 m_buttonRect->setToolTip(i18n("Add Rectangle") + ' ' + m_buttonRect->shortcut().toString());
214 connect(m_buttonRect, SIGNAL(triggered()), this, SLOT(slotRectTool()));
216 m_buttonImage = m_toolbar->addAction(KIcon("insert-image"), QString());
217 m_buttonImage->setCheckable(false);
218 m_buttonImage->setShortcut(Qt::ALT + Qt::Key_I);
219 m_buttonImage->setToolTip(i18n("Add Image") + ' ' + m_buttonImage->shortcut().toString());
220 connect(m_buttonImage, SIGNAL(triggered()), this, SLOT(slotImageTool()));
222 m_toolbar->addSeparator();
224 m_buttonLoad = m_toolbar->addAction(KIcon("document-open"), i18n("Open Document"));
225 m_buttonLoad->setCheckable(false);
226 m_buttonLoad->setShortcut(Qt::CTRL + Qt::Key_O);
227 connect(m_buttonLoad, SIGNAL(triggered()), this, SLOT(loadTitle()));
229 m_buttonSave = m_toolbar->addAction(KIcon("document-save-as"), i18n("Save As"));
230 m_buttonSave->setCheckable(false);
231 m_buttonSave->setShortcut(Qt::CTRL + Qt::Key_S);
232 connect(m_buttonSave, SIGNAL(triggered()), this, SLOT(saveTitle()));
234 layout->addWidget(m_toolbar);
236 // initialize graphic scene
237 m_scene = new GraphicsSceneRectMove(this);
238 graphicsView->setScene(m_scene);
239 m_titledocument.setScene(m_scene, m_frameWidth, m_frameHeight);
240 connect(m_scene, SIGNAL(changed(QList<QRectF>)), this, SLOT(slotChanged()));
242 // a gradient background
243 /*QRadialGradient *gradient = new QRadialGradient(0, 0, 10);
244 gradient->setSpread(QGradient::ReflectSpread);
245 scene->setBackgroundBrush(*gradient);*/
247 m_frameImage = new QGraphicsPixmapItem();
249 qtrans.scale(2.0, 2.0);
250 m_frameImage->setTransform(qtrans);
251 m_frameImage->setZValue(-1200);
252 m_frameImage->setFlags(0);
253 displayBackgroundFrame();
254 graphicsView->scene()->addItem(m_frameImage);
256 connect(m_scene, SIGNAL(selectionChanged()), this , SLOT(selectionChanged()));
257 connect(m_scene, SIGNAL(itemMoved()), this , SLOT(selectionChanged()));
258 connect(m_scene, SIGNAL(sceneZoom(bool)), this , SLOT(slotZoom(bool)));
259 connect(m_scene, SIGNAL(actionFinished()), this , SLOT(slotSelectTool()));
260 //connect(m_scene, SIGNAL(actionFinished()), this , SLOT(selectionChanged()));
261 connect(m_scene, SIGNAL(newRect(QGraphicsRectItem *)), this , SLOT(slotNewRect(QGraphicsRectItem *)));
262 connect(m_scene, SIGNAL(newText(QGraphicsTextItem *)), this , SLOT(slotNewText(QGraphicsTextItem *)));
263 connect(zoom_slider, SIGNAL(valueChanged(int)), this , SLOT(slotUpdateZoom(int)));
265 QPen framepen(Qt::DotLine);
266 framepen.setColor(Qt::red);
268 m_frameBorder = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
269 m_frameBorder->setPen(framepen);
270 m_frameBorder->setZValue(-1100);
271 m_frameBorder->setBrush(Qt::transparent);
272 m_frameBorder->setFlags(0);
273 graphicsView->scene()->addItem(m_frameBorder);
275 // mbd: load saved settings
278 graphicsView->show();
279 //graphicsView->setRenderHint(QPainter::Antialiasing);
280 graphicsView->setInteractive(true);
281 //graphicsView->resize(400, 300);
282 kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
283 //toolBox->setItemEnabled(2, false);
284 m_startViewport = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
285 m_endViewport = new QGraphicsRectItem(QRectF(0, 0, m_frameWidth, m_frameHeight));
286 m_startViewport->setData(0, m_frameWidth);
287 m_startViewport->setData(1, m_frameHeight);
288 m_endViewport->setData(0, m_frameWidth);
289 m_endViewport->setData(1, m_frameHeight);
291 if (!url.isEmpty()) loadTitle(url);
294 QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
297 connect(anim_start, SIGNAL(toggled(bool)), this, SLOT(slotAnimStart(bool)));
298 connect(anim_end, SIGNAL(toggled(bool)), this, SLOT(slotAnimEnd(bool)));
301 TitleWidget::~TitleWidget()
305 delete m_buttonImage;
306 delete m_buttonCursor;
309 delete m_unicodeAction;
311 delete m_unicodeDialog;
312 delete m_frameBorder;
314 delete m_startViewport;
315 delete m_endViewport;
317 delete m_signalMapper;
321 QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl, bool isClone)
324 QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
325 KStandardDirs::makeDir(titlePath);
326 titlePath.append((isClone == false) ? "title" : "clone");
329 while (path.isEmpty() || QFile::exists(path)) {
331 path = titlePath + QString::number(counter).rightJustified(3, '0', false) + ".png";
333 result.append(((isClone == false) ? i18n("Title") : i18n("Clone")) + ' ' + QString::number(counter).rightJustified(3, '0', false));
339 QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName)
342 QString titlePath = projectUrl.path(KUrl::AddTrailingSlash) + "titles/";
343 KStandardDirs::makeDir(titlePath);
344 return titlePath + titleName + ".png";
348 QStringList TitleWidget::extractImageList(QString xml)
351 if (xml.isEmpty()) return result;
354 QDomNodeList images = doc.elementsByTagName("content");
355 for (int i = 0; i < images.count(); i++) {
356 if (images.at(i).toElement().hasAttribute("url"))
357 result.append(images.at(i).toElement().attribute("url"));
364 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
369 void TitleWidget::slotTextTool()
371 m_scene->setTool(TITLE_TEXT);
372 showToolbars(TITLE_TEXT);
373 checkButton(TITLE_TEXT);
376 void TitleWidget::slotRectTool()
378 m_scene->setTool(TITLE_RECTANGLE);
379 showToolbars(TITLE_RECTANGLE);
380 checkButton(TITLE_RECTANGLE);
383 void TitleWidget::slotSelectTool()
385 m_scene->setTool(TITLE_SELECT);
387 // Find out which toolbars need to be shown, depending on selected item
388 TITLETOOL t = TITLE_SELECT;
389 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
391 switch (l.at(0)->type()) {
405 if (t == TITLE_RECTANGLE && (l.at(0) == m_endViewport || l.at(0) == m_startViewport)) {
406 //graphicsView->centerOn(l.at(0));
412 updateCoordinates(l.at(0));
413 updateDimension(l.at(0));
414 updateRotZoom(l.at(0));
417 checkButton(TITLE_SELECT);
420 void TitleWidget::slotImageTool()
422 // TODO: find a way to get a list of all supported image types...
423 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";
424 KUrl url = KFileDialog::getOpenUrl(KUrl(), allExtensions, this, i18n("Load Image")); //"*.svg *.png *.jpg *.jpeg *.gif *.raw"
425 if (!url.isEmpty()) {
426 if (url.path().endsWith(".svg")) {
427 QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
428 svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
429 svg->setZValue(m_count++);
430 svg->setData(Qt::UserRole, url.path());
431 graphicsView->scene()->addItem(svg);
433 QPixmap pix(url.path());
434 QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
435 image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
436 image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
437 image->setData(Qt::UserRole, url.path());
438 image->setZValue(m_count++);
439 graphicsView->scene()->addItem(image);
442 m_scene->setTool(TITLE_SELECT);
443 showToolbars(TITLE_SELECT);
444 checkButton(TITLE_NONE);
447 void TitleWidget::showToolbars(TITLETOOL toolType)
451 rect_properties->setHidden(true);
452 image_properties->setHidden(true);
453 no_properties->setHidden(true);
454 text_properties->setHidden(false);
456 case TITLE_RECTANGLE:
457 image_properties->setHidden(true);
458 no_properties->setHidden(true);
459 text_properties->setHidden(true);
460 rect_properties->setHidden(false);
463 no_properties->setHidden(true);
464 text_properties->setHidden(true);
465 rect_properties->setHidden(true);
466 image_properties->setHidden(false);
469 text_properties->setHidden(true);
470 rect_properties->setHidden(true);
471 image_properties->setHidden(true);
472 no_properties->setHidden(false);
477 void TitleWidget::enableToolbars(TITLETOOL toolType)
479 // TITLETOOL is defined in graphicsscenerectmove.h
484 bool bValue_w = false;
485 bool bValue_h = false;
494 case TITLE_RECTANGLE:
509 frame_properties->setEnabled(bFrame);
510 text_properties->setEnabled(bText);
511 rect_properties->setEnabled(bRect);
512 image_properties->setEnabled(bImage);
513 value_w->setEnabled(bValue_w);
514 value_h->setEnabled(bValue_h);
517 void TitleWidget::checkButton(TITLETOOL toolType)
519 bool bSelect = false;
531 case TITLE_RECTANGLE:
541 m_buttonCursor->setChecked(bSelect);
542 m_buttonText->setChecked(bText);
543 m_buttonRect->setChecked(bRect);
544 m_buttonImage->setChecked(bImage);
547 void TitleWidget::displayBackgroundFrame()
549 if (!displayBg->isChecked()) {
550 QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
551 QPixmap pattern(20, 20);
553 QColor bgcolor(210, 210, 210);
556 p.fillRect(QRect(0, 0, 10, 10), bgcolor);
557 p.fillRect(QRect(10, 10, 20, 20), bgcolor);
562 p.fillRect(bg.rect(), br);
564 m_frameImage->setPixmap(bg);
566 m_frameImage->setPixmap(QPixmap::fromImage(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2)));
570 void TitleWidget::initAnimation()
572 align_box->setEnabled(false);
573 QPen startpen(Qt::DotLine);
574 QPen endpen(Qt::DashDotLine);
575 startpen.setColor(QColor(100, 200, 100, 140));
576 endpen.setColor(QColor(200, 100, 100, 140));
578 m_startViewport->setPen(startpen);
579 m_endViewport->setPen(endpen);
581 m_startViewport->setZValue(-1000);
582 m_endViewport->setZValue(-1000);
584 m_startViewport->setFlags(0);
585 m_endViewport->setFlags(0);
587 graphicsView->scene()->addItem(m_startViewport);
588 graphicsView->scene()->addItem(m_endViewport);
590 connect(keep_aspect, SIGNAL(toggled(bool)), this, SLOT(slotKeepAspect(bool)));
591 connect(resize50, SIGNAL(clicked()), this, SLOT(slotResize50()));
592 connect(resize100, SIGNAL(clicked()), this, SLOT(slotResize100()));
593 connect(resize200, SIGNAL(clicked()), this, SLOT(slotResize200()));
596 void TitleWidget::slotUpdateZoom(int pos)
598 m_scene->setZoom((double) pos / 100);
599 zoom_label->setText(QString::number(pos) + '%');
602 void TitleWidget::slotZoom(bool up)
604 int pos = zoom_slider->value();
607 zoom_slider->setValue(pos);
610 void TitleWidget::slotAdjustZoom()
612 /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
613 double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
614 if (scalex > scaley) scalex = scaley;
615 int zoompos = (int)(scalex * 7 + 0.5);*/
616 graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
617 int zoompos = graphicsView->matrix().m11() * 100;
618 zoom_slider->setValue(zoompos);
619 graphicsView->centerOn(m_frameBorder);
622 void TitleWidget::slotZoomOneToOne()
624 zoom_slider->setValue(100);
625 graphicsView->centerOn(m_frameBorder);
628 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
630 updateAxisButtons(rect); // back to default
632 QColor f = rectFColor->color();
633 f.setAlpha(rectFAlpha->value());
635 penf.setWidth(rectLineWidth->value());
637 QColor b = rectBColor->color();
638 b.setAlpha(rectBAlpha->value());
639 rect->setBrush(QBrush(b));
640 rect->setZValue(m_count++);
641 //setCurrentItem(rect);
642 //graphicsView->setFocus();
645 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
647 updateAxisButtons(tt); // back to default
649 QFont font = font_family->currentFont();
650 font.setPixelSize(font_size->value());
652 font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
653 font.setItalic(buttonItalic->isChecked());
654 font.setUnderline(buttonUnder->isChecked());
657 QColor color = fontColorButton->color();
658 color.setAlpha(textAlpha->value());
659 tt->setDefaultTextColor(color);
660 tt->setZValue(m_count++);
664 void TitleWidget::setFontBoxWeight(int weight)
666 int index = font_weight_box->findData(weight);
668 index = font_weight_box->findData(QFont::Normal);
670 font_weight_box->setCurrentIndex(index);
673 void TitleWidget::setCurrentItem(QGraphicsItem *item)
675 m_scene->setSelectedItem(item);
678 void TitleWidget::zIndexChanged(int v)
680 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
686 void TitleWidget::selectionChanged()
688 if (m_scene->tool() != TITLE_SELECT) return;
689 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
690 //toolBox->setItemEnabled(2, false);
691 //toolBox->setItemEnabled(3, false);
692 value_x->blockSignals(true);
693 value_y->blockSignals(true);
694 value_w->blockSignals(true);
695 value_h->blockSignals(true);
696 itemzoom->blockSignals(true);
697 itemrotate->blockSignals(true);
699 bool blockX = !origin_x_left->signalsBlocked();
700 bool blockY = !origin_y_top->signalsBlocked();
701 if (blockX) origin_x_left->blockSignals(true);
702 if (blockY) origin_y_top->blockSignals(true);
703 origin_x_left->setChecked(false);
704 origin_y_top->setChecked(false);
707 enableToolbars(TITLE_NONE);
708 if (blockX) origin_x_left->blockSignals(false);
709 if (blockY) origin_y_top->blockSignals(false);
710 itemzoom->setEnabled(false);
711 itemrotate->setEnabled(false);
712 } else if (l.size() == 1) {
713 if (l.at(0) != m_startViewport && l.at(0) != m_endViewport) {
714 itemzoom->setEnabled(true);
715 itemrotate->setEnabled(true);
717 itemzoom->setEnabled(false);
718 itemrotate->setEnabled(false);
721 if (l.at(0)->type() == TEXTITEM) {
722 showToolbars(TITLE_TEXT);
723 QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
724 //if (l[0]->hasFocus())
725 //toolBox->setCurrentIndex(0);
726 //toolBox->setItemEnabled(2, true);
727 font_size->blockSignals(true);
728 font_family->blockSignals(true);
729 font_weight_box->blockSignals(true);
730 buttonItalic->blockSignals(true);
731 buttonUnder->blockSignals(true);
732 fontColorButton->blockSignals(true);
733 textAlpha->blockSignals(true);
734 buttonAlignLeft->blockSignals(true);
735 buttonAlignRight->blockSignals(true);
736 buttonAlignNone->blockSignals(true);
737 buttonAlignCenter->blockSignals(true);
739 QFont font = i->font();
740 font_family->setCurrentFont(font);
741 font_size->setValue(font.pixelSize());
742 buttonItalic->setChecked(font.italic());
743 buttonUnder->setChecked(font.underline());
744 setFontBoxWeight(font.weight());
746 QColor color = i->defaultTextColor();
747 fontColorButton->setColor(color);
748 textAlpha->setValue(color.alpha());
750 QTextCursor cur = i->textCursor();
751 QTextBlockFormat format = cur.blockFormat();
752 if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
753 else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
754 else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
755 else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
757 font_size->blockSignals(false);
758 font_family->blockSignals(false);
759 font_weight_box->blockSignals(false);
760 buttonItalic->blockSignals(false);
761 buttonUnder->blockSignals(false);
762 fontColorButton->blockSignals(false);
763 textAlpha->blockSignals(false);
764 buttonAlignLeft->blockSignals(false);
765 buttonAlignRight->blockSignals(false);
766 buttonAlignNone->blockSignals(false);
767 buttonAlignCenter->blockSignals(false);
769 updateAxisButtons(i);
770 updateCoordinates(i);
772 enableToolbars(TITLE_TEXT);
774 } else if ((l.at(0))->type() == RECTITEM) {
775 showToolbars(TITLE_RECTANGLE);
777 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
778 if (rec == m_startViewport || rec == m_endViewport) {
779 /*toolBox->setCurrentIndex(3);
780 toolBox->widget(0)->setEnabled(false);
781 toolBox->widget(1)->setEnabled(false);*/
782 enableToolbars(TITLE_NONE);
784 /*toolBox->widget(0)->setEnabled(true);
785 toolBox->widget(1)->setEnabled(true);
786 toolBox->setCurrentIndex(0);*/
787 //toolBox->setItemEnabled(3, true);
788 rectFAlpha->setValue(rec->pen().color().alpha());
789 rectBAlpha->setValue(rec->brush().color().alpha());
790 //kDebug() << rec->brush().color().alpha();
791 QColor fcol = rec->pen().color();
792 QColor bcol = rec->brush().color();
793 //fcol.setAlpha(255);
794 //bcol.setAlpha(255);
795 rectFColor->setColor(fcol);
796 rectBColor->setColor(bcol);
798 rectLineWidth->setValue(rec->pen().width());
799 enableToolbars(TITLE_RECTANGLE);
802 updateAxisButtons(l.at(0));
803 updateCoordinates(rec);
804 updateDimension(rec);
806 } else if (l.at(0)->type() == IMAGEITEM) {
807 showToolbars(TITLE_IMAGE);
809 updateCoordinates(l.at(0));
810 updateDimension(l.at(0));
812 enableToolbars(TITLE_IMAGE);
815 //toolBox->setCurrentIndex(0);
816 showToolbars(TITLE_NONE);
817 enableToolbars(TITLE_NONE);
818 /*frame_properties->setEnabled(false);
819 text_properties->setEnabled(false);
820 rect_properties->setEnabled(false);*/
822 zValue->setValue((int)l.at(0)->zValue());
823 itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
824 itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
825 value_x->blockSignals(false);
826 value_y->blockSignals(false);
827 value_w->blockSignals(false);
828 value_h->blockSignals(false);
829 itemzoom->blockSignals(false);
830 itemrotate->blockSignals(false);
834 void TitleWidget::slotValueChanged(int type)
836 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
837 if (l.size() > 0 && l.at(0)->type() == IMAGEITEM) {
842 val = value_w->value();
845 val = value_h->value();
849 QGraphicsItem *i = l.at(0);
850 Transform t = m_transformations.value(i);
852 // Ratio width:height
853 double phi = (double) i->boundingRect().width() / i->boundingRect().height();
854 double alpha = (double) t.rotate / 180.0 * M_PI;
864 // Add 0.5 because otherwise incrementing by 1 might have no effect
865 length = val / (cos(alpha) + 1 / phi * sin(alpha)) + 0.5;
866 scale = length / i->boundingRect().width();
869 length = val / (phi * sin(alpha) + cos(alpha)) + 0.5;
870 scale = length / i->boundingRect().height();
877 qtrans.scale(scale, scale);
878 qtrans.rotate(t.rotate);
879 i->setTransform(qtrans);
880 m_transformations[i] = t;
887 /** \brief Updates position/size of the selected item when a value
888 * of an item (coordinates, size) has changed */
889 void TitleWidget::slotAdjustSelectedItem()
891 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
893 if (l.at(0)->type() == RECTITEM) {
895 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
897 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
898 } else if (l.at(0)->type() == TEXTITEM) {
900 updatePosition(l.at(0));
901 } else if (l.at(0)->type() == IMAGEITEM) {
903 updatePosition(l.at(0));
908 /** \brief Updates width/height int the text fields, regarding transformation matrix */
909 void TitleWidget::updateDimension(QGraphicsItem *i)
911 value_w->blockSignals(true);
912 value_h->blockSignals(true);
915 if (i->type() == IMAGEITEM) {
916 // Get multipliers for rotation/scaling
918 /*Transform t = m_transformations.value(i);
919 QRectF r = i->boundingRect();
920 int width = (int) ( abs(r.width()*t.scalex * cos(t.rotate/180.0*M_PI))
921 + abs(r.height()*t.scaley * sin(t.rotate/180.0*M_PI)) );
922 int height = (int) ( abs(r.height()*t.scaley * cos(t.rotate/180*M_PI))
923 + abs(r.width()*t.scalex * sin(t.rotate/180*M_PI)) );*/
925 value_w->setValue(i->sceneBoundingRect().width());
926 value_h->setValue(i->sceneBoundingRect().height());
927 } else if (i->type() == RECTITEM) {
928 QGraphicsRectItem *r = static_cast <QGraphicsRectItem *>(i);
929 value_w->setValue((int) r->rect().width());
930 value_h->setValue((int) r->rect().height());
931 } else if (i->type() == TEXTITEM) {
932 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(i);
933 value_w->setValue((int) t->boundingRect().width());
934 value_h->setValue((int) t->boundingRect().height());
937 value_w->blockSignals(false);
938 value_h->blockSignals(false);
941 /** \brief Updates the coordinates in the text fields from the item */
942 void TitleWidget::updateCoordinates(QGraphicsItem *i)
944 // Block signals emitted by this method
945 value_x->blockSignals(true);
946 value_y->blockSignals(true);
948 if (i->type() == TEXTITEM) {
950 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
952 // Set the correct x coordinate value
953 if (origin_x_left->isChecked()) {
954 // Origin (0 point) is at m_frameWidth, coordinate axis is inverted
955 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->boundingRect().width()));
957 // Origin is at 0 (default)
958 value_x->setValue((int) rec->pos().x());
962 if (origin_y_top->isChecked()) {
963 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->boundingRect().height()));
965 value_y->setValue((int) rec->pos().y());
968 } else if (i->type() == RECTITEM) {
970 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
972 if (origin_x_left->isChecked()) {
973 // Origin (0 point) is at m_frameWidth
974 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->rect().width()));
976 // Origin is at 0 (default)
977 value_x->setValue((int) rec->pos().x());
980 if (origin_y_top->isChecked()) {
981 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->rect().height()));
983 value_y->setValue((int) rec->pos().y());
986 } else if (i->type() == IMAGEITEM) {
988 if (origin_x_left->isChecked()) {
989 value_x->setValue((int)(m_frameWidth - i->pos().x() - i->sceneBoundingRect().width()));
991 value_x->setValue((int) i->pos().x());
994 if (origin_y_top->isChecked()) {
995 value_y->setValue((int)(m_frameHeight - i->pos().y() - i->sceneBoundingRect().height()));
997 value_y->setValue((int) i->pos().y());
1002 // Stop blocking signals now
1003 value_x->blockSignals(false);
1004 value_y->blockSignals(false);
1007 void TitleWidget::updateRotZoom(QGraphicsItem *i)
1009 itemzoom->blockSignals(true);
1010 itemrotate->blockSignals(false);
1012 Transform t = m_transformations.value(i);
1013 itemzoom->setValue((int)(t.scalex * 100.0 + 0.5));
1014 itemrotate->setValue((int)(t.rotate));
1016 itemzoom->blockSignals(false);
1017 itemrotate->blockSignals(false);
1020 /** \brief Updates the position of an item by reading coordinates from the text fields */
1021 void TitleWidget::updatePosition(QGraphicsItem *i)
1023 if (i->type() == TEXTITEM) {
1024 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
1027 if (origin_x_left->isChecked()) {
1028 /* Origin of the x axis is at m_frameWidth,
1029 * and distance from right border of the item to the right
1030 * border of the frame is taken.
1031 * See comment to slotOriginXClicked().
1033 posX = m_frameWidth - value_x->value() - rec->boundingRect().width();
1035 posX = value_x->value();
1039 if (origin_y_top->isChecked()) {
1040 /* Same for y axis */
1041 posY = m_frameHeight - value_y->value() - rec->boundingRect().height();
1043 posY = value_y->value();
1046 rec->setPos(posX, posY);
1048 } else if (i->type() == RECTITEM) {
1050 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
1053 if (origin_x_left->isChecked()) {
1054 posX = m_frameWidth - value_x->value() - rec->rect().width();
1056 posX = value_x->value();
1060 if (origin_y_top->isChecked()) {
1061 posY = m_frameHeight - value_y->value() - rec->rect().height();
1063 posY = value_y->value();
1066 rec->setPos(posX, posY);
1068 } else if (i->type() == IMAGEITEM) {
1070 if (origin_x_left->isChecked()) {
1071 // Use the sceneBoundingRect because this also regards transformations like zoom
1072 posX = m_frameWidth - value_x->value() - i->sceneBoundingRect().width();
1074 posX = value_x->value();
1078 if (origin_y_top->isChecked()) {
1079 posY = m_frameHeight - value_y->value() - i->sceneBoundingRect().height();
1081 posY = value_y->value();
1084 i->setPos(posX, posY);
1090 void TitleWidget::updateTextOriginX()
1092 if (origin_x_left->isChecked()) {
1093 origin_x_left->setText(i18n("\u2212X"));
1095 origin_x_left->setText(i18n("+X"));
1099 void TitleWidget::slotOriginXClicked()
1101 // Update the text displayed on the button.
1102 updateTextOriginX();
1104 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1105 if (l.size() >= 1) {
1106 updateCoordinates(l.at(0));
1108 // Remember x axis setting
1109 l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked() ?
1110 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1112 graphicsView->setFocus();
1115 void TitleWidget::updateTextOriginY()
1117 if (origin_y_top->isChecked()) {
1118 origin_y_top->setText(i18n("\u2212Y"));
1120 origin_y_top->setText(i18n("+Y"));
1124 void TitleWidget::slotOriginYClicked()
1126 // Update the text displayed on the button.
1127 updateTextOriginY();
1129 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1130 if (l.size() >= 1) {
1131 updateCoordinates(l.at(0));
1133 l.at(0)->setData(TitleDocument::OriginYTop, origin_y_top->isChecked() ?
1134 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1137 graphicsView->setFocus();
1140 void TitleWidget::updateAxisButtons(QGraphicsItem *i)
1142 int xAxis = i->data(TitleDocument::OriginXLeft).toInt();
1143 int yAxis = i->data(TitleDocument::OriginYTop).toInt();
1144 origin_x_left->blockSignals(true);
1145 origin_y_top->blockSignals(true);
1147 if (xAxis == TitleDocument::AxisInverted) {
1148 origin_x_left->setChecked(true);
1150 origin_x_left->setChecked(false);
1152 updateTextOriginX();
1154 if (yAxis == TitleDocument::AxisInverted) {
1155 origin_y_top->setChecked(true);
1157 origin_y_top->setChecked(false);
1159 updateTextOriginY();
1161 origin_x_left->blockSignals(false);
1162 origin_y_top->blockSignals(false);
1165 void TitleWidget::slotChangeBackground()
1167 QColor color = kcolorbutton->color();
1168 color.setAlpha(horizontalSlider->value());
1169 m_frameBorder->setBrush(QBrush(color));
1173 * Something (yeah) has changed in our QGraphicsScene.
1175 void TitleWidget::slotChanged()
1177 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1178 if (l.size() >= 1 && l.at(0)->type() == TEXTITEM) {
1179 textChanged(static_cast <QGraphicsTextItem *>(l.at(0)));
1184 * If the user has set origin_x_left (everything also for y),
1185 * we need to look whether a text element has been selected. If yes,
1186 * we need to ensure that the right border of the text field
1187 * remains fixed also when some text has been entered.
1189 * This is also known as right-justified, with the difference that
1190 * it is not valid for text but for its boundingRect. Text may still
1191 * be left-justified.
1193 void TitleWidget::textChanged(QGraphicsTextItem *i)
1198 if (origin_x_left->isChecked() || origin_y_top->isChecked()) {
1200 if (!i->toPlainText().isEmpty()) {
1204 * Don't do anything if the string is empty. If the position
1205 * would be updated here, a newly created text field would
1206 * be set to the position of the last selected text field.
1212 void TitleWidget::slotInsertUnicode()
1214 m_unicodeDialog->exec();
1217 void TitleWidget::slotInsertUnicodeString(QString text)
1219 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
1221 if (l.at(0)->type() == TEXTITEM) {
1222 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(l.at(0));
1223 t->textCursor().insertText(text);
1228 void TitleWidget::slotUpdateText()
1230 QFont font = font_family->currentFont();
1231 font.setPixelSize(font_size->value());
1232 font.setItalic(buttonItalic->isChecked());
1233 font.setUnderline(buttonUnder->isChecked());
1234 font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1235 QColor color = fontColorButton->color();
1236 color.setAlpha(textAlpha->value());
1238 QGraphicsTextItem* item = NULL;
1239 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1240 if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
1241 item = static_cast <QGraphicsTextItem *>(l.at(0));
1244 //if (item->textCursor().selection ().isEmpty())
1245 QTextCursor cur = item->textCursor();
1246 QTextBlockFormat format = cur.blockFormat();
1247 if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
1248 item->setTextWidth(item->boundingRect().width());
1249 if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
1250 else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
1251 else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
1253 format.setAlignment(Qt::AlignLeft);
1254 item->setTextWidth(-1);
1258 item->setFont(font);
1259 item->setDefaultTextColor(color);
1260 cur.select(QTextCursor::Document);
1261 cur.setBlockFormat(format);
1262 item->setTextCursor(cur);
1263 cur.clearSelection();
1264 item->setTextCursor(cur);
1269 void TitleWidget::rectChanged()
1271 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1272 if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
1273 QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
1274 QColor f = rectFColor->color();
1275 f.setAlpha(rectFAlpha->value());
1277 penf.setWidth(rectLineWidth->value());
1279 QColor b = rectBColor->color();
1280 b.setAlpha(rectBAlpha->value());
1281 rec->setBrush(QBrush(b));
1285 void TitleWidget::itemScaled(int val)
1287 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1288 if (l.size() == 1) {
1289 Transform x = m_transformations.value(l.at(0));
1290 x.scalex = (double)val / 100.0;
1291 x.scaley = (double)val / 100.0;
1293 qtrans.scale(x.scalex, x.scaley);
1294 qtrans.rotate(x.rotate);
1295 l[0]->setTransform(qtrans);
1296 m_transformations[l.at(0)] = x;
1297 updateDimension(l.at(0));
1301 void TitleWidget::itemRotate(int val)
1303 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1304 if (l.size() == 1) {
1305 Transform x = m_transformations[l.at(0)];
1306 x.rotate = (double)val;
1308 qtrans.scale(x.scalex, x.scaley);
1309 qtrans.rotate(x.rotate);
1310 l[0]->setTransform(qtrans);
1311 m_transformations[l.at(0)] = x;
1312 updateDimension(l.at(0));
1316 void TitleWidget::itemHCenter()
1318 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1319 if (l.size() == 1) {
1320 QGraphicsItem *item = l.at(0);
1321 QRectF br = item->sceneBoundingRect();
1322 int width = (int)br.width();
1323 int newPos = (int)((m_frameWidth - width) / 2);
1324 newPos += item->pos().x() - br.left(); // Check item transformation
1325 item->setPos(newPos, item->pos().y());
1326 updateCoordinates(item);
1330 void TitleWidget::itemVCenter()
1332 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1333 if (l.size() == 1) {
1334 QGraphicsItem *item = l.at(0);
1335 QRectF br = item->sceneBoundingRect();
1336 int height = (int)br.height();
1337 int newPos = (int)((m_frameHeight - height) / 2);
1338 newPos += item->pos().y() - br.top(); // Check item transformation
1339 item->setPos(item->pos().x(), newPos);
1340 updateCoordinates(item);
1344 void TitleWidget::itemTop()
1346 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1347 if (l.size() == 1) {
1348 QGraphicsItem *item = l.at(0);
1349 QRectF br = item->sceneBoundingRect();
1351 if (br.top() > 0) diff = -br.top();
1352 else diff = -br.bottom();
1353 item->moveBy(0, diff);
1354 updateCoordinates(item);
1358 void TitleWidget::itemBottom()
1360 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1361 if (l.size() == 1) {
1362 QGraphicsItem *item = l.at(0);
1363 QRectF br = item->sceneBoundingRect();
1365 if (br.bottom() > m_frameHeight) diff = m_frameHeight - br.top();
1366 else diff = m_frameHeight - br.bottom();
1367 item->moveBy(0, diff);
1368 updateCoordinates(item);
1372 void TitleWidget::itemLeft()
1374 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1375 if (l.size() == 1) {
1376 QGraphicsItem *item = l.at(0);
1377 QRectF br = item->sceneBoundingRect();
1379 if (br.left() > 0) diff = -br.left();
1380 else diff = -br.right();
1381 item->moveBy(diff, 0);
1382 updateCoordinates(item);
1386 void TitleWidget::itemRight()
1388 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1389 if (l.size() == 1) {
1390 QGraphicsItem *item = l.at(0);
1391 QRectF br = item->sceneBoundingRect();
1393 if (br.right() < m_frameWidth) diff = m_frameWidth - br.right();
1394 else diff = m_frameWidth - br.left();
1395 item->moveBy(diff, 0);
1396 updateCoordinates(item);
1400 void TitleWidget::setupViewports()
1402 //double aspect_ratio = 4.0 / 3.0;//read from project
1403 //better zoom centered, but render uses only the created rect, so no problem to change the zoom function
1404 /*QRectF sp(0, 0, startViewportSize->value() * m_frameWidth / 100.0 , startViewportSize->value()* m_frameHeight / 100.0);
1405 QRectF ep(0, 0, endViewportSize->value() * m_frameWidth / 100.0, endViewportSize->value() * m_frameHeight / 100.0);
1406 // use a polygon thiat uses 16:9 and 4:3 rects forpreview the size in all aspect ratios ?
1407 QPolygonF spoly(sp);
1408 QPolygonF epoly(ep);
1409 spoly.translate(startViewportX->value(), startViewportY->value());
1410 epoly.translate(endViewportX->value(), endViewportY->value());
1411 m_startViewport->setPolygon(spoly);
1412 m_endViewport->setPolygon(epoly);
1413 if (! insertingValues) {
1414 m_startViewport->setData(0, startViewportX->value());
1415 m_startViewport->setData(1, startViewportY->value());
1416 m_startViewport->setData(2, startViewportSize->value());
1418 m_endViewport->setData(0, endViewportX->value());
1419 m_endViewport->setData(1, endViewportY->value());
1420 m_endViewport->setData(2, endViewportSize->value());
1424 void TitleWidget::loadTitle(KUrl url)
1426 if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this, i18n("Load Title"));
1427 if (!url.isEmpty()) {
1428 QList<QGraphicsItem *> items = m_scene->items();
1429 for (int i = 0; i < items.size(); i++) {
1430 if (items.at(i)->zValue() > -1000) delete items.at(i);
1432 m_scene->clearTextSelection();
1436 if (KIO::NetAccess::download(url, tmpfile, 0)) {
1437 QFile file(tmpfile);
1438 if (file.open(QIODevice::ReadOnly)) {
1439 doc.setContent(&file, false);
1442 KIO::NetAccess::removeTempFile(tmpfile);
1447 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport, &out) + 1;
1449 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1450 insertingValues = true;
1451 startViewportX->setValue(m_startViewport->data(0).toInt());
1452 startViewportY->setValue(m_startViewport->data(1).toInt());
1453 startViewportSize->setValue(m_startViewport->data(2).toInt());
1454 endViewportX->setValue(m_endViewport->data(0).toInt());
1455 endViewportY->setValue(m_endViewport->data(1).toInt());
1456 endViewportSize->setValue(m_endViewport->data(2).toInt());
1458 insertingValues = false;
1464 void TitleWidget::saveTitle(KUrl url)
1466 if (anim_start->isChecked()) slotAnimStart(false);
1467 if (anim_end->isChecked()) slotAnimEnd(false);
1468 if (url.isEmpty()) {
1469 KFileDialog *fs = new KFileDialog(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this);
1470 fs->setOperationMode(KFileDialog::Saving);
1471 fs->setMode(KFile::File);
1472 #if KDE_IS_VERSION(4,2,0)
1473 fs->setConfirmOverwrite(true);
1475 fs->setKeepLocation(true);
1477 url = fs->selectedUrl();
1480 if (!url.isEmpty()) {
1481 if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport, m_tc.getFrameCount(title_duration->text())) == false)
1482 KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
1486 QDomDocument TitleWidget::xml()
1488 QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
1489 doc.documentElement().setAttribute("out", m_tc.getFrameCount(title_duration->text()));
1493 int TitleWidget::duration() const
1495 return m_tc.getFrameCount(title_duration->text());
1498 void TitleWidget::setXml(QDomDocument doc)
1501 m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport, &out);
1503 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1504 /*if (doc.documentElement().hasAttribute("out")) {
1505 GenTime duration = GenTime(doc.documentElement().attribute("out").toDouble() / 1000.0);
1506 title_duration->setText(m_tc.getTimecode(duration));
1508 else title_duration->setText(m_tc.getTimecode(GenTime(5000)));*/
1510 QDomElement e = doc.documentElement();
1511 m_transformations.clear();
1512 QList <QGraphicsItem *> items = graphicsView->scene()->items();
1513 const double PI = 4.0 * atan(1.0);
1514 for (int i = 0; i < items.count(); i++) {
1515 QTransform t = items.at(i)->transform();
1519 x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
1520 m_transformations[items.at(i)] = x;
1522 // mbd: Update the GUI color selectors to match the stuff from the loaded document
1523 QColor background_color = m_titledocument.getBackgroundColor();
1524 horizontalSlider->blockSignals(true);
1525 kcolorbutton->blockSignals(true);
1526 horizontalSlider->setValue(background_color.alpha());
1527 background_color.setAlpha(255);
1528 kcolorbutton->setColor(background_color);
1529 horizontalSlider->blockSignals(false);
1530 kcolorbutton->blockSignals(false);
1532 /*startViewportX->setValue(m_startViewport->data(0).toInt());
1533 startViewportY->setValue(m_startViewport->data(1).toInt());
1534 startViewportSize->setValue(m_startViewport->data(2).toInt());
1535 endViewportX->setValue(m_endViewport->data(0).toInt());
1536 endViewportY->setValue(m_endViewport->data(1).toInt());
1537 endViewportSize->setValue(m_endViewport->data(2).toInt());*/
1539 QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
1543 /** \brief Connected to the accepted signal - calls writeChoices */
1544 void TitleWidget::slotAccepted()
1546 if (anim_start->isChecked()) slotAnimStart(false);
1547 if (anim_end->isChecked()) slotAnimEnd(false);
1551 /** \brief Store the current choices of font, background and rect values */
1552 void TitleWidget::writeChoices()
1554 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1555 KSharedConfigPtr config = KGlobal::config();
1556 KConfigGroup titleConfig(config, "TitleWidget");
1557 // Write the entries
1558 titleConfig.writeEntry("font_family", font_family->currentFont());
1559 //titleConfig.writeEntry("font_size", font_size->value());
1560 titleConfig.writeEntry("font_pixel_size", font_size->value());
1561 titleConfig.writeEntry("font_color", fontColorButton->color());
1562 titleConfig.writeEntry("font_alpha", textAlpha->value());
1563 titleConfig.writeEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1564 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
1565 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
1567 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
1568 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
1569 titleConfig.writeEntry("rect_background_color", rectBColor->color());
1570 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
1571 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
1573 titleConfig.writeEntry("background_color", kcolorbutton->color());
1574 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
1576 //! \todo Not sure if I should sync - it is probably safe to do it
1581 /** \brief Read the last stored choices into the dialog */
1582 void TitleWidget::readChoices()
1584 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1585 KSharedConfigPtr config = KGlobal::config();
1586 KConfigGroup titleConfig(config, "TitleWidget");
1588 font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
1589 font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
1590 fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
1591 textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
1593 if (titleConfig.readEntry("font_bold", false)) weight = QFont::Bold;
1594 else weight = titleConfig.readEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1595 setFontBoxWeight(weight);
1596 buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
1597 buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
1599 rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
1600 rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
1601 rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
1602 rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
1603 rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
1605 kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
1606 horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
1609 void TitleWidget::adjustFrameSize()
1611 m_frameWidth = m_titledocument.frameWidth();
1612 m_frameHeight = m_titledocument.frameHeight();
1613 m_frameBorder->setRect(0, 0, m_frameWidth, m_frameHeight);
1614 displayBackgroundFrame();
1617 void TitleWidget::slotAnimStart(bool anim)
1619 if (anim && anim_end->isChecked()) {
1620 anim_end->setChecked(false);
1621 m_endViewport->setZValue(-1000);
1622 m_endViewport->setBrush(QBrush());
1625 QList<QGraphicsItem *> list = m_scene->items();
1626 for (int i = 0; i < list.count(); i++) {
1627 if (list.at(i)->zValue() > -1000) {
1628 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1629 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1632 align_box->setEnabled(anim);
1633 itemzoom->setEnabled(!anim);
1634 itemrotate->setEnabled(!anim);
1635 frame_toolbar->setEnabled(!anim);
1636 rect_properties->setEnabled(!anim);
1638 keep_aspect->setChecked(!m_startViewport->data(0).isNull());
1639 m_startViewport->setZValue(1100);
1640 QColor col = m_startViewport->pen().color();
1642 m_startViewport->setBrush(col);
1643 m_startViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1644 m_startViewport->setSelected(true);
1647 if (m_startViewport->childItems().isEmpty()) addAnimInfoText();
1649 m_startViewport->setZValue(-1000);
1650 m_startViewport->setBrush(QBrush());
1651 m_startViewport->setFlags(0);
1652 if (!anim_end->isChecked()) deleteAnimInfoText();
1657 void TitleWidget::slotAnimEnd(bool anim)
1659 if (anim && anim_start->isChecked()) {
1660 anim_start->setChecked(false);
1661 m_startViewport->setZValue(-1000);
1662 m_startViewport->setBrush(QBrush());
1665 QList<QGraphicsItem *> list = m_scene->items();
1666 for (int i = 0; i < list.count(); i++) {
1667 if (list.at(i)->zValue() > -1000) {
1668 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1669 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1672 align_box->setEnabled(anim);
1673 itemzoom->setEnabled(!anim);
1674 itemrotate->setEnabled(!anim);
1675 frame_toolbar->setEnabled(!anim);
1676 rect_properties->setEnabled(!anim);
1678 keep_aspect->setChecked(!m_endViewport->data(0).isNull());
1679 m_endViewport->setZValue(1100);
1680 QColor col = m_endViewport->pen().color();
1682 m_endViewport->setBrush(col);
1683 m_endViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1684 m_endViewport->setSelected(true);
1687 if (m_endViewport->childItems().isEmpty()) addAnimInfoText();
1689 m_endViewport->setZValue(-1000);
1690 m_endViewport->setBrush(QBrush());
1691 m_endViewport->setFlags(0);
1692 if (!anim_start->isChecked()) deleteAnimInfoText();
1696 void TitleWidget::addAnimInfoText()
1698 // add text to anim viewport
1699 QGraphicsTextItem *t = new QGraphicsTextItem(i18n("Start"), m_startViewport);
1700 QGraphicsTextItem *t2 = new QGraphicsTextItem(i18n("End"), m_endViewport);
1701 QFont font = t->font();
1702 font.setPixelSize(m_startViewport->rect().width() / 10);
1703 QColor col = m_startViewport->pen().color();
1705 t->setDefaultTextColor(col);
1707 font.setPixelSize(m_endViewport->rect().width() / 10);
1708 col = m_endViewport->pen().color();
1710 t2->setDefaultTextColor(col);
1714 void TitleWidget::updateInfoText()
1716 // update info text font
1717 if (!m_startViewport->childItems().isEmpty()) {
1718 QGraphicsTextItem *item = static_cast <QGraphicsTextItem *>(m_startViewport->childItems().at(0));
1720 QFont font = item->font();
1721 font.setPixelSize(m_startViewport->rect().width() / 10);
1722 item->setFont(font);
1725 if (!m_endViewport->childItems().isEmpty()) {
1726 QGraphicsTextItem *item = static_cast <QGraphicsTextItem *>(m_endViewport->childItems().at(0));
1728 QFont font = item->font();
1729 font.setPixelSize(m_endViewport->rect().width() / 10);
1730 item->setFont(font);
1735 void TitleWidget::deleteAnimInfoText()
1737 // end animation editing, remove info text
1738 while (!m_startViewport->childItems().isEmpty()) {
1739 QGraphicsItem *item = m_startViewport->childItems().at(0);
1740 m_scene->removeItem(item);
1743 while (!m_endViewport->childItems().isEmpty()) {
1744 QGraphicsItem *item = m_endViewport->childItems().at(0);
1745 m_scene->removeItem(item);
1751 void TitleWidget::slotKeepAspect(bool keep)
1753 if (m_endViewport->zValue() == 1100) {
1754 m_endViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1755 m_endViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1757 m_startViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1758 m_startViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1762 void TitleWidget::slotResize50()
1764 if (m_endViewport->zValue() == 1100) {
1765 m_endViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1766 } else m_startViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1769 void TitleWidget::slotResize100()
1771 if (m_endViewport->zValue() == 1100) {
1772 m_endViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1773 } else m_startViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1776 void TitleWidget::slotResize200()
1778 if (m_endViewport->zValue() == 1100) {
1779 m_endViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);
1780 } else m_startViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);