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>
30 #include <QDomDocument>
31 #include <QGraphicsItem>
32 #include <QGraphicsSvgItem>
36 #include <QSignalMapper>
37 #include <QTextBlockFormat>
38 #include <QTextCursor>
40 int settingUp = false;
42 const int IMAGEITEM = 7;
43 const int RECTITEM = 3;
44 const int TEXTITEM = 8;
45 static bool insertingValues = false;
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 //toolBox->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));
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 void TitleWidget::resizeEvent(QResizeEvent * /*event*/)
353 void TitleWidget::slotTextTool()
355 m_scene->setTool(TITLE_TEXT);
356 showToolbars(TITLE_TEXT);
357 checkButton(TITLE_TEXT);
360 void TitleWidget::slotRectTool()
362 m_scene->setTool(TITLE_RECTANGLE);
363 showToolbars(TITLE_RECTANGLE);
364 checkButton(TITLE_RECTANGLE);
367 void TitleWidget::slotSelectTool()
369 m_scene->setTool(TITLE_SELECT);
371 // Find out which toolbars need to be shown, depending on selected item
372 TITLETOOL t = TITLE_SELECT;
373 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
375 switch (l.at(0)->type()) {
389 if (t == TITLE_RECTANGLE && (l.at(0) == m_endViewport || l.at(0) == m_startViewport)) {
390 //graphicsView->centerOn(l.at(0));
396 updateCoordinates(l.at(0));
397 updateDimension(l.at(0));
398 updateRotZoom(l.at(0));
401 checkButton(TITLE_SELECT);
404 void TitleWidget::slotImageTool()
406 KUrl url = KFileDialog::getOpenUrl(KUrl(), "*.svg *.png *.jpg *.jpeg *.gif *.raw", this, i18n("Load Image"));
407 if (!url.isEmpty()) {
408 if (url.path().endsWith(".svg")) {
409 QGraphicsSvgItem *svg = new QGraphicsSvgItem(url.toLocalFile());
410 svg->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
411 svg->setZValue(m_count++);
412 svg->setData(Qt::UserRole, url.path());
413 graphicsView->scene()->addItem(svg);
415 QPixmap pix(url.path());
416 QGraphicsPixmapItem *image = new QGraphicsPixmapItem(pix);
417 image->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);
418 image->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
419 image->setData(Qt::UserRole, url.path());
420 image->setZValue(m_count++);
421 graphicsView->scene()->addItem(image);
424 m_scene->setTool(TITLE_SELECT);
425 showToolbars(TITLE_SELECT);
426 checkButton(TITLE_NONE);
429 void TitleWidget::showToolbars(TITLETOOL toolType)
433 rect_properties->setHidden(true);
434 image_properties->setHidden(true);
435 no_properties->setHidden(true);
436 text_properties->setHidden(false);
438 case TITLE_RECTANGLE:
439 image_properties->setHidden(true);
440 no_properties->setHidden(true);
441 text_properties->setHidden(true);
442 rect_properties->setHidden(false);
445 no_properties->setHidden(true);
446 text_properties->setHidden(true);
447 rect_properties->setHidden(true);
448 image_properties->setHidden(false);
451 text_properties->setHidden(true);
452 rect_properties->setHidden(true);
453 image_properties->setHidden(true);
454 no_properties->setHidden(false);
459 void TitleWidget::enableToolbars(TITLETOOL toolType)
461 // TITLETOOL is defined in graphicsscenerectmove.h
466 bool bValue_w = false;
467 bool bValue_h = false;
476 case TITLE_RECTANGLE:
491 frame_properties->setEnabled(bFrame);
492 text_properties->setEnabled(bText);
493 rect_properties->setEnabled(bRect);
494 image_properties->setEnabled(bImage);
495 value_w->setEnabled(bValue_w);
496 value_h->setEnabled(bValue_h);
499 void TitleWidget::checkButton(TITLETOOL toolType)
501 bool bSelect = false;
513 case TITLE_RECTANGLE:
523 m_buttonCursor->setChecked(bSelect);
524 m_buttonText->setChecked(bText);
525 m_buttonRect->setChecked(bRect);
526 m_buttonImage->setChecked(bImage);
529 void TitleWidget::displayBackgroundFrame()
531 if (!displayBg->isChecked()) {
532 QPixmap bg(m_frameWidth / 2, m_frameHeight / 2);
533 QPixmap pattern(20, 20);
535 QColor bgcolor(210, 210, 210);
538 p.fillRect(QRect(0, 0, 10, 10), bgcolor);
539 p.fillRect(QRect(10, 10, 20, 20), bgcolor);
544 p.fillRect(bg.rect(), br);
546 m_frameImage->setPixmap(bg);
548 m_frameImage->setPixmap(QPixmap::fromImage(m_render->extractFrame((int) m_render->seekPosition().frames(m_render->fps()), m_frameWidth / 2, m_frameHeight / 2)));
552 void TitleWidget::initAnimation()
554 align_box->setEnabled(false);
555 QPen startpen(Qt::DotLine);
556 QPen endpen(Qt::DashDotLine);
557 startpen.setColor(QColor(100, 200, 100, 140));
558 endpen.setColor(QColor(200, 100, 100, 140));
560 m_startViewport->setPen(startpen);
561 m_endViewport->setPen(endpen);
563 m_startViewport->setZValue(-1000);
564 m_endViewport->setZValue(-1000);
566 m_startViewport->setFlags(0);
567 m_endViewport->setFlags(0);
569 graphicsView->scene()->addItem(m_startViewport);
570 graphicsView->scene()->addItem(m_endViewport);
572 connect(keep_aspect, SIGNAL(toggled(bool)), this, SLOT(slotKeepAspect(bool)));
573 connect(resize50, SIGNAL(clicked()), this, SLOT(slotResize50()));
574 connect(resize100, SIGNAL(clicked()), this, SLOT(slotResize100()));
575 connect(resize200, SIGNAL(clicked()), this, SLOT(slotResize200()));
578 void TitleWidget::slotUpdateZoom(int pos)
580 m_scene->setZoom((double) pos / 100);
581 zoom_label->setText(QString::number(pos) + '%');
584 void TitleWidget::slotZoom(bool up)
586 int pos = zoom_slider->value();
589 zoom_slider->setValue(pos);
592 void TitleWidget::slotAdjustZoom()
594 /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
595 double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
596 if (scalex > scaley) scalex = scaley;
597 int zoompos = (int)(scalex * 7 + 0.5);*/
598 graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
599 int zoompos = graphicsView->matrix().m11() * 100;
600 zoom_slider->setValue(zoompos);
601 graphicsView->centerOn(m_frameBorder);
604 void TitleWidget::slotZoomOneToOne()
606 zoom_slider->setValue(100);
607 graphicsView->centerOn(m_frameBorder);
610 void TitleWidget::slotNewRect(QGraphicsRectItem * rect)
612 updateAxisButtons(rect); // back to default
614 QColor f = rectFColor->color();
615 f.setAlpha(rectFAlpha->value());
617 penf.setWidth(rectLineWidth->value());
619 QColor b = rectBColor->color();
620 b.setAlpha(rectBAlpha->value());
621 rect->setBrush(QBrush(b));
622 rect->setZValue(m_count++);
623 //setCurrentItem(rect);
624 //graphicsView->setFocus();
627 void TitleWidget::slotNewText(QGraphicsTextItem *tt)
629 updateAxisButtons(tt); // back to default
631 QFont font = font_family->currentFont();
632 font.setPixelSize(font_size->value());
634 font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
635 font.setItalic(buttonItalic->isChecked());
636 font.setUnderline(buttonUnder->isChecked());
639 QColor color = fontColorButton->color();
640 color.setAlpha(textAlpha->value());
641 tt->setDefaultTextColor(color);
642 tt->setZValue(m_count++);
646 void TitleWidget::setFontBoxWeight(int weight)
648 int index = font_weight_box->findData(weight);
650 index = font_weight_box->findData(QFont::Normal);
652 font_weight_box->setCurrentIndex(index);
655 void TitleWidget::setCurrentItem(QGraphicsItem *item)
657 m_scene->setSelectedItem(item);
660 void TitleWidget::zIndexChanged(int v)
662 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
668 void TitleWidget::selectionChanged()
670 if (m_scene->tool() != TITLE_SELECT) return;
671 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
672 //toolBox->setItemEnabled(2, false);
673 //toolBox->setItemEnabled(3, false);
674 value_x->blockSignals(true);
675 value_y->blockSignals(true);
676 value_w->blockSignals(true);
677 value_h->blockSignals(true);
678 itemzoom->blockSignals(true);
679 itemrotate->blockSignals(true);
681 bool blockX = !origin_x_left->signalsBlocked();
682 bool blockY = !origin_y_top->signalsBlocked();
683 if (blockX) origin_x_left->blockSignals(true);
684 if (blockY) origin_y_top->blockSignals(true);
685 origin_x_left->setChecked(false);
686 origin_y_top->setChecked(false);
689 enableToolbars(TITLE_NONE);
690 if (blockX) origin_x_left->blockSignals(false);
691 if (blockY) origin_y_top->blockSignals(false);
692 itemzoom->setEnabled(false);
693 itemrotate->setEnabled(false);
694 } else if (l.size() == 1) {
695 if (l.at(0) != m_startViewport && l.at(0) != m_endViewport) {
696 itemzoom->setEnabled(true);
697 itemrotate->setEnabled(true);
699 itemzoom->setEnabled(false);
700 itemrotate->setEnabled(false);
702 if (l.at(0)->type() == TEXTITEM) {
703 showToolbars(TITLE_TEXT);
704 QGraphicsTextItem* i = static_cast <QGraphicsTextItem *>(l.at(0));
705 //if (l[0]->hasFocus())
706 //toolBox->setCurrentIndex(0);
707 //toolBox->setItemEnabled(2, true);
708 font_size->blockSignals(true);
709 font_family->blockSignals(true);
710 font_weight_box->blockSignals(true);
711 buttonItalic->blockSignals(true);
712 buttonUnder->blockSignals(true);
713 fontColorButton->blockSignals(true);
714 textAlpha->blockSignals(true);
715 buttonAlignLeft->blockSignals(true);
716 buttonAlignRight->blockSignals(true);
717 buttonAlignNone->blockSignals(true);
718 buttonAlignCenter->blockSignals(true);
720 QFont font = i->font();
721 font_family->setCurrentFont(font);
722 font_size->setValue(font.pixelSize());
723 buttonItalic->setChecked(font.italic());
724 buttonUnder->setChecked(font.underline());
725 setFontBoxWeight(font.weight());
727 QColor color = i->defaultTextColor();
728 fontColorButton->setColor(color);
729 textAlpha->setValue(color.alpha());
731 QTextCursor cur = i->textCursor();
732 QTextBlockFormat format = cur.blockFormat();
733 if (i->textWidth() == -1) buttonAlignNone->setChecked(true);
734 else if (format.alignment() == Qt::AlignHCenter) buttonAlignCenter->setChecked(true);
735 else if (format.alignment() == Qt::AlignRight) buttonAlignRight->setChecked(true);
736 else if (format.alignment() == Qt::AlignLeft) buttonAlignLeft->setChecked(true);
738 font_size->blockSignals(false);
739 font_family->blockSignals(false);
740 font_weight_box->blockSignals(false);
741 buttonItalic->blockSignals(false);
742 buttonUnder->blockSignals(false);
743 fontColorButton->blockSignals(false);
744 textAlpha->blockSignals(false);
745 buttonAlignLeft->blockSignals(false);
746 buttonAlignRight->blockSignals(false);
747 buttonAlignNone->blockSignals(false);
748 buttonAlignCenter->blockSignals(false);
750 updateAxisButtons(i);
751 updateCoordinates(i);
753 enableToolbars(TITLE_TEXT);
755 } else if ((l.at(0))->type() == RECTITEM) {
756 showToolbars(TITLE_RECTANGLE);
758 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
759 if (rec == m_startViewport || rec == m_endViewport) {
760 /*toolBox->setCurrentIndex(3);
761 toolBox->widget(0)->setEnabled(false);
762 toolBox->widget(1)->setEnabled(false);*/
763 enableToolbars(TITLE_NONE);
765 /*toolBox->widget(0)->setEnabled(true);
766 toolBox->widget(1)->setEnabled(true);
767 toolBox->setCurrentIndex(0);*/
768 //toolBox->setItemEnabled(3, true);
769 rectFAlpha->setValue(rec->pen().color().alpha());
770 rectBAlpha->setValue(rec->brush().color().alpha());
771 //kDebug() << rec->brush().color().alpha();
772 QColor fcol = rec->pen().color();
773 QColor bcol = rec->brush().color();
774 //fcol.setAlpha(255);
775 //bcol.setAlpha(255);
776 rectFColor->setColor(fcol);
777 rectBColor->setColor(bcol);
779 rectLineWidth->setValue(rec->pen().width());
780 enableToolbars(TITLE_RECTANGLE);
783 updateAxisButtons(l.at(0));
784 updateCoordinates(rec);
785 updateDimension(rec);
787 } else if (l.at(0)->type() == IMAGEITEM) {
788 showToolbars(TITLE_IMAGE);
790 updateCoordinates(l.at(0));
791 updateDimension(l.at(0));
793 enableToolbars(TITLE_IMAGE);
796 //toolBox->setCurrentIndex(0);
797 showToolbars(TITLE_NONE);
798 enableToolbars(TITLE_NONE);
799 /*frame_properties->setEnabled(false);
800 text_properties->setEnabled(false);
801 rect_properties->setEnabled(false);*/
803 zValue->setValue((int)l.at(0)->zValue());
804 itemzoom->setValue((int)(m_transformations.value(l.at(0)).scalex * 100.0 + 0.5));
805 itemrotate->setValue((int)(m_transformations.value(l.at(0)).rotate));
806 value_x->blockSignals(false);
807 value_y->blockSignals(false);
808 value_w->blockSignals(false);
809 value_h->blockSignals(false);
810 itemzoom->blockSignals(false);
811 itemrotate->blockSignals(false);
815 void TitleWidget::slotValueChanged(int type)
817 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
818 if (l.size() > 0 && l.at(0)->type() == IMAGEITEM) {
823 val = value_w->value();
826 val = value_h->value();
830 QGraphicsItem *i = l.at(0);
831 Transform t = m_transformations.value(i);
833 // Ratio width:height
834 double phi = (double) i->boundingRect().width() / i->boundingRect().height();
835 double alpha = (double) t.rotate / 180.0 * M_PI;
845 // Add 0.5 because otherwise incrementing by 1 might have no effect
846 length = val / (cos(alpha) + 1 / phi * sin(alpha)) + 0.5;
847 scale = length / i->boundingRect().width();
850 length = val / (phi * sin(alpha) + cos(alpha)) + 0.5;
851 scale = length / i->boundingRect().height();
858 qtrans.scale(scale, scale);
859 qtrans.rotate(t.rotate);
860 i->setTransform(qtrans);
861 m_transformations[i] = t;
868 /** \brief Updates position/size of the selected item when a value
869 * of an item (coordinates, size) has changed */
870 void TitleWidget::slotAdjustSelectedItem()
872 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
874 if (l.at(0)->type() == RECTITEM) {
876 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(l.at(0));
878 rec->setRect(QRect(0, 0, value_w->value(), value_h->value()));
879 } else if (l.at(0)->type() == TEXTITEM) {
881 updatePosition(l.at(0));
882 } else if (l.at(0)->type() == IMAGEITEM) {
884 updatePosition(l.at(0));
889 /** \brief Updates width/height int the text fields, regarding transformation matrix */
890 void TitleWidget::updateDimension(QGraphicsItem *i)
892 value_w->blockSignals(true);
893 value_h->blockSignals(true);
896 if (i->type() == IMAGEITEM) {
897 // Get multipliers for rotation/scaling
899 /*Transform t = m_transformations.value(i);
900 QRectF r = i->boundingRect();
901 int width = (int) ( abs(r.width()*t.scalex * cos(t.rotate/180.0*M_PI))
902 + abs(r.height()*t.scaley * sin(t.rotate/180.0*M_PI)) );
903 int height = (int) ( abs(r.height()*t.scaley * cos(t.rotate/180*M_PI))
904 + abs(r.width()*t.scalex * sin(t.rotate/180*M_PI)) );*/
906 value_w->setValue(i->sceneBoundingRect().width());
907 value_h->setValue(i->sceneBoundingRect().height());
908 } else if (i->type() == RECTITEM) {
909 QGraphicsRectItem *r = static_cast <QGraphicsRectItem *>(i);
910 value_w->setValue((int) r->rect().width());
911 value_h->setValue((int) r->rect().height());
912 } else if (i->type() == TEXTITEM) {
913 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(i);
914 value_w->setValue((int) t->boundingRect().width());
915 value_h->setValue((int) t->boundingRect().height());
918 value_w->blockSignals(false);
919 value_h->blockSignals(false);
922 /** \brief Updates the coordinates in the text fields from the item */
923 void TitleWidget::updateCoordinates(QGraphicsItem *i)
925 // Block signals emitted by this method
926 value_x->blockSignals(true);
927 value_y->blockSignals(true);
929 if (i->type() == TEXTITEM) {
931 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
933 // Set the correct x coordinate value
934 if (origin_x_left->isChecked()) {
935 // Origin (0 point) is at m_frameWidth, coordinate axis is inverted
936 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->boundingRect().width()));
938 // Origin is at 0 (default)
939 value_x->setValue((int) rec->pos().x());
943 if (origin_y_top->isChecked()) {
944 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->boundingRect().height()));
946 value_y->setValue((int) rec->pos().y());
949 } else if (i->type() == RECTITEM) {
951 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
953 if (origin_x_left->isChecked()) {
954 // Origin (0 point) is at m_frameWidth
955 value_x->setValue((int)(m_frameWidth - rec->pos().x() - rec->rect().width()));
957 // Origin is at 0 (default)
958 value_x->setValue((int) rec->pos().x());
961 if (origin_y_top->isChecked()) {
962 value_y->setValue((int)(m_frameHeight - rec->pos().y() - rec->rect().height()));
964 value_y->setValue((int) rec->pos().y());
967 } else if (i->type() == IMAGEITEM) {
969 if (origin_x_left->isChecked()) {
970 value_x->setValue((int)(m_frameWidth - i->pos().x() - i->sceneBoundingRect().width()));
972 value_x->setValue((int) i->pos().x());
975 if (origin_y_top->isChecked()) {
976 value_y->setValue((int)(m_frameHeight - i->pos().y() - i->sceneBoundingRect().height()));
978 value_y->setValue((int) i->pos().y());
983 // Stop blocking signals now
984 value_x->blockSignals(false);
985 value_y->blockSignals(false);
988 void TitleWidget::updateRotZoom(QGraphicsItem *i)
990 itemzoom->blockSignals(true);
991 itemrotate->blockSignals(false);
993 Transform t = m_transformations.value(i);
994 itemzoom->setValue((int)(t.scalex * 100.0 + 0.5));
995 itemrotate->setValue((int)(t.rotate));
997 itemzoom->blockSignals(false);
998 itemrotate->blockSignals(false);
1001 /** \brief Updates the position of an item by reading coordinates from the text fields */
1002 void TitleWidget::updatePosition(QGraphicsItem *i)
1004 if (i->type() == TEXTITEM) {
1005 QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
1008 if (origin_x_left->isChecked()) {
1009 /* Origin of the x axis is at m_frameWidth,
1010 * and distance from right border of the item to the right
1011 * border of the frame is taken.
1012 * See comment to slotOriginXClicked().
1014 posX = m_frameWidth - value_x->value() - rec->boundingRect().width();
1016 posX = value_x->value();
1020 if (origin_y_top->isChecked()) {
1021 /* Same for y axis */
1022 posY = m_frameHeight - value_y->value() - rec->boundingRect().height();
1024 posY = value_y->value();
1027 rec->setPos(posX, posY);
1029 } else if (i->type() == RECTITEM) {
1031 QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *>(i);
1034 if (origin_x_left->isChecked()) {
1035 posX = m_frameWidth - value_x->value() - rec->rect().width();
1037 posX = value_x->value();
1041 if (origin_y_top->isChecked()) {
1042 posY = m_frameHeight - value_y->value() - rec->rect().height();
1044 posY = value_y->value();
1047 rec->setPos(posX, posY);
1049 } else if (i->type() == IMAGEITEM) {
1051 if (origin_x_left->isChecked()) {
1052 // Use the sceneBoundingRect because this also regards transformations like zoom
1053 posX = m_frameWidth - value_x->value() - i->sceneBoundingRect().width();
1055 posX = value_x->value();
1059 if (origin_y_top->isChecked()) {
1060 posY = m_frameHeight - value_y->value() - i->sceneBoundingRect().height();
1062 posY = value_y->value();
1065 i->setPos(posX, posY);
1071 void TitleWidget::updateTextOriginX()
1073 if (origin_x_left->isChecked()) {
1074 origin_x_left->setText(i18n("\u2212X"));
1076 origin_x_left->setText(i18n("+X"));
1080 void TitleWidget::slotOriginXClicked()
1082 // Update the text displayed on the button.
1083 updateTextOriginX();
1085 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1086 if (l.size() >= 1) {
1087 updateCoordinates(l.at(0));
1089 // Remember x axis setting
1090 l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked() ?
1091 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1093 graphicsView->setFocus();
1096 void TitleWidget::updateTextOriginY()
1098 if (origin_y_top->isChecked()) {
1099 origin_y_top->setText(i18n("\u2212Y"));
1101 origin_y_top->setText(i18n("+Y"));
1105 void TitleWidget::slotOriginYClicked()
1107 // Update the text displayed on the button.
1108 updateTextOriginY();
1110 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1111 if (l.size() >= 1) {
1112 updateCoordinates(l.at(0));
1114 l.at(0)->setData(TitleDocument::OriginYTop, origin_y_top->isChecked() ?
1115 TitleDocument::AxisInverted : TitleDocument::AxisDefault);
1118 graphicsView->setFocus();
1121 void TitleWidget::updateAxisButtons(QGraphicsItem *i)
1123 int xAxis = i->data(TitleDocument::OriginXLeft).toInt();
1124 int yAxis = i->data(TitleDocument::OriginYTop).toInt();
1125 origin_x_left->blockSignals(true);
1126 origin_y_top->blockSignals(true);
1128 if (xAxis == TitleDocument::AxisInverted) {
1129 origin_x_left->setChecked(true);
1131 origin_x_left->setChecked(false);
1133 updateTextOriginX();
1135 if (yAxis == TitleDocument::AxisInverted) {
1136 origin_y_top->setChecked(true);
1138 origin_y_top->setChecked(false);
1140 updateTextOriginY();
1142 origin_x_left->blockSignals(false);
1143 origin_y_top->blockSignals(false);
1146 void TitleWidget::slotChangeBackground()
1148 QColor color = kcolorbutton->color();
1149 color.setAlpha(horizontalSlider->value());
1150 m_frameBorder->setBrush(QBrush(color));
1154 * Something (yeah) has changed in our QGraphicsScene.
1156 void TitleWidget::slotChanged()
1158 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1159 if (l.size() >= 1 && l.at(0)->type() == TEXTITEM) {
1160 textChanged(static_cast <QGraphicsTextItem *>(l.at(0)));
1165 * If the user has set origin_x_left (everything also for y),
1166 * we need to look whether a text element has been selected. If yes,
1167 * we need to ensure that the right border of the text field
1168 * remains fixed also when some text has been entered.
1170 * This is also known as right-justified, with the difference that
1171 * it is not valid for text but for its boundingRect. Text may still
1172 * be left-justified.
1174 void TitleWidget::textChanged(QGraphicsTextItem *i)
1179 if (origin_x_left->isChecked() || origin_y_top->isChecked()) {
1181 if (!i->toPlainText().isEmpty()) {
1185 * Don't do anything if the string is empty. If the position
1186 * would be updated here, a newly created text field would
1187 * be set to the position of the last selected text field.
1193 void TitleWidget::slotInsertUnicode()
1195 m_unicodeDialog->exec();
1198 void TitleWidget::slotInsertUnicodeString(QString text)
1200 QList<QGraphicsItem *> l = graphicsView->scene()->selectedItems();
1202 if (l.at(0)->type() == TEXTITEM) {
1203 QGraphicsTextItem *t = static_cast <QGraphicsTextItem *>(l.at(0));
1204 t->textCursor().insertText(text);
1209 void TitleWidget::slotUpdateText()
1211 QFont font = font_family->currentFont();
1212 font.setPixelSize(font_size->value());
1213 font.setItalic(buttonItalic->isChecked());
1214 font.setUnderline(buttonUnder->isChecked());
1215 font.setWeight(font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1216 QColor color = fontColorButton->color();
1217 color.setAlpha(textAlpha->value());
1219 QGraphicsTextItem* item = NULL;
1220 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1221 if (l.size() == 1 && l.at(0)->type() == TEXTITEM) {
1222 item = static_cast <QGraphicsTextItem *>(l.at(0));
1225 //if (item->textCursor().selection ().isEmpty())
1226 QTextCursor cur = item->textCursor();
1227 QTextBlockFormat format = cur.blockFormat();
1228 if (buttonAlignLeft->isChecked() || buttonAlignCenter->isChecked() || buttonAlignRight->isChecked()) {
1229 item->setTextWidth(item->boundingRect().width());
1230 if (buttonAlignCenter->isChecked()) format.setAlignment(Qt::AlignHCenter);
1231 else if (buttonAlignRight->isChecked()) format.setAlignment(Qt::AlignRight);
1232 else if (buttonAlignLeft->isChecked()) format.setAlignment(Qt::AlignLeft);
1234 format.setAlignment(Qt::AlignLeft);
1235 item->setTextWidth(-1);
1239 item->setFont(font);
1240 item->setDefaultTextColor(color);
1241 cur.select(QTextCursor::Document);
1242 cur.setBlockFormat(format);
1243 item->setTextCursor(cur);
1244 cur.clearSelection();
1245 item->setTextCursor(cur);
1250 void TitleWidget::rectChanged()
1252 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1253 if (l.size() == 1 && l.at(0)->type() == RECTITEM && !settingUp) {
1254 QGraphicsRectItem *rec = static_cast<QGraphicsRectItem *>(l.at(0));
1255 QColor f = rectFColor->color();
1256 f.setAlpha(rectFAlpha->value());
1258 penf.setWidth(rectLineWidth->value());
1260 QColor b = rectBColor->color();
1261 b.setAlpha(rectBAlpha->value());
1262 rec->setBrush(QBrush(b));
1266 void TitleWidget::itemScaled(int val)
1268 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1269 if (l.size() == 1) {
1270 Transform x = m_transformations.value(l.at(0));
1271 x.scalex = (double)val / 100.0;
1272 x.scaley = (double)val / 100.0;
1274 qtrans.scale(x.scalex, x.scaley);
1275 qtrans.rotate(x.rotate);
1276 l[0]->setTransform(qtrans);
1277 m_transformations[l.at(0)] = x;
1278 updateDimension(l.at(0));
1282 void TitleWidget::itemRotate(int val)
1284 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1285 if (l.size() == 1) {
1286 Transform x = m_transformations[l.at(0)];
1287 x.rotate = (double)val;
1289 qtrans.scale(x.scalex, x.scaley);
1290 qtrans.rotate(x.rotate);
1291 l[0]->setTransform(qtrans);
1292 m_transformations[l.at(0)] = x;
1293 updateDimension(l.at(0));
1297 void TitleWidget::itemHCenter()
1299 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1300 if (l.size() == 1) {
1301 QGraphicsItem *item = l.at(0);
1302 QRectF br = item->sceneBoundingRect();
1303 int width = (int)br.width();
1304 int newPos = (int)((m_frameWidth - width) / 2);
1305 newPos += item->pos().x() - br.left(); // Check item transformation
1306 item->setPos(newPos, item->pos().y());
1307 updateCoordinates(item);
1311 void TitleWidget::itemVCenter()
1313 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1314 if (l.size() == 1) {
1315 QGraphicsItem *item = l.at(0);
1316 QRectF br = item->sceneBoundingRect();
1317 int height = (int)br.height();
1318 int newPos = (int)((m_frameHeight - height) / 2);
1319 newPos += item->pos().y() - br.top(); // Check item transformation
1320 item->setPos(item->pos().x(), newPos);
1321 updateCoordinates(item);
1325 void TitleWidget::itemTop()
1327 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1328 if (l.size() == 1) {
1329 QGraphicsItem *item = l.at(0);
1330 QRectF br = item->sceneBoundingRect();
1332 if (br.top() > 0) diff = -br.top();
1333 else diff = -br.bottom();
1334 item->moveBy(0, diff);
1335 updateCoordinates(item);
1339 void TitleWidget::itemBottom()
1341 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1342 if (l.size() == 1) {
1343 QGraphicsItem *item = l.at(0);
1344 QRectF br = item->sceneBoundingRect();
1346 if (br.bottom() > m_frameHeight) diff = m_frameHeight - br.top();
1347 else diff = m_frameHeight - br.bottom();
1348 item->moveBy(0, diff);
1349 updateCoordinates(item);
1353 void TitleWidget::itemLeft()
1355 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1356 if (l.size() == 1) {
1357 QGraphicsItem *item = l.at(0);
1358 QRectF br = item->sceneBoundingRect();
1360 if (br.left() > 0) diff = -br.left();
1361 else diff = -br.right();
1362 item->moveBy(diff, 0);
1363 updateCoordinates(item);
1367 void TitleWidget::itemRight()
1369 QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
1370 if (l.size() == 1) {
1371 QGraphicsItem *item = l.at(0);
1372 QRectF br = item->sceneBoundingRect();
1374 if (br.right() < m_frameWidth) diff = m_frameWidth - br.right();
1375 else diff = m_frameWidth - br.left();
1376 item->moveBy(diff, 0);
1377 updateCoordinates(item);
1381 void TitleWidget::setupViewports()
1383 //double aspect_ratio = 4.0 / 3.0;//read from project
1384 //better zoom centered, but render uses only the created rect, so no problem to change the zoom function
1385 /*QRectF sp(0, 0, startViewportSize->value() * m_frameWidth / 100.0 , startViewportSize->value()* m_frameHeight / 100.0);
1386 QRectF ep(0, 0, endViewportSize->value() * m_frameWidth / 100.0, endViewportSize->value() * m_frameHeight / 100.0);
1387 // use a polygon thiat uses 16:9 and 4:3 rects forpreview the size in all aspect ratios ?
1388 QPolygonF spoly(sp);
1389 QPolygonF epoly(ep);
1390 spoly.translate(startViewportX->value(), startViewportY->value());
1391 epoly.translate(endViewportX->value(), endViewportY->value());
1392 m_startViewport->setPolygon(spoly);
1393 m_endViewport->setPolygon(epoly);
1394 if (! insertingValues) {
1395 m_startViewport->setData(0, startViewportX->value());
1396 m_startViewport->setData(1, startViewportY->value());
1397 m_startViewport->setData(2, startViewportSize->value());
1399 m_endViewport->setData(0, endViewportX->value());
1400 m_endViewport->setData(1, endViewportY->value());
1401 m_endViewport->setData(2, endViewportSize->value());
1405 void TitleWidget::loadTitle(KUrl url)
1407 if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl(m_projectTitlePath), "application/x-kdenlivetitle", this, i18n("Load Title"));
1408 if (!url.isEmpty()) {
1409 QList<QGraphicsItem *> items = m_scene->items();
1410 for (int i = 0; i < items.size(); i++) {
1411 if (items.at(i)->zValue() > -1000) delete items.at(i);
1413 m_scene->clearTextSelection();
1417 if (KIO::NetAccess::download(url, tmpfile, 0)) {
1418 QFile file(tmpfile);
1419 if (file.open(QIODevice::ReadOnly)) {
1420 doc.setContent(&file, false);
1423 KIO::NetAccess::removeTempFile(tmpfile);
1428 m_count = m_titledocument.loadDocument(url, m_startViewport, m_endViewport, &out) + 1;
1430 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1431 insertingValues = true;
1432 startViewportX->setValue(m_startViewport->data(0).toInt());
1433 startViewportY->setValue(m_startViewport->data(1).toInt());
1434 startViewportSize->setValue(m_startViewport->data(2).toInt());
1435 endViewportX->setValue(m_endViewport->data(0).toInt());
1436 endViewportY->setValue(m_endViewport->data(1).toInt());
1437 endViewportSize->setValue(m_endViewport->data(2).toInt());
1439 insertingValues = false;
1445 void TitleWidget::saveTitle(KUrl url)
1447 if (anim_start->isChecked()) slotAnimStart(false);
1448 if (anim_end->isChecked()) slotAnimEnd(false);
1449 if (url.isEmpty()) {
1450 KFileDialog *fs = new KFileDialog(KUrl(m_projectTitlePath), "application/x-kdenlivetitle",this);
1451 fs->setOperationMode(KFileDialog::Saving);
1452 fs->setMode(KFile::File);
1453 fs->setConfirmOverwrite(true);
1454 fs->setKeepLocation(true);
1456 url = fs->selectedUrl();
1459 if (!url.isEmpty()) {
1460 if (m_titledocument.saveDocument(url, m_startViewport, m_endViewport, m_tc.getFrameCount(title_duration->text())) == false)
1461 KMessageBox::error(this, i18n("Cannot write to file %1", url.path()));
1465 QDomDocument TitleWidget::xml()
1467 QDomDocument doc = m_titledocument.xml(m_startViewport, m_endViewport);
1468 doc.documentElement().setAttribute("out", m_tc.getFrameCount(title_duration->text()));
1472 int TitleWidget::duration() const
1474 return m_tc.getFrameCount(title_duration->text());
1477 void TitleWidget::setXml(QDomDocument doc)
1480 m_count = m_titledocument.loadFromXml(doc, m_startViewport, m_endViewport, &out);
1482 title_duration->setText(m_tc.getTimecode(GenTime(out, m_render->fps())));
1483 /*if (doc.documentElement().hasAttribute("out")) {
1484 GenTime duration = GenTime(doc.documentElement().attribute("out").toDouble() / 1000.0);
1485 title_duration->setText(m_tc.getTimecode(duration));
1487 else title_duration->setText(m_tc.getTimecode(GenTime(5000)));*/
1489 QDomElement e = doc.documentElement();
1490 m_transformations.clear();
1491 QList <QGraphicsItem *> items = graphicsView->scene()->items();
1492 const double PI = 4.0 * atan(1.0);
1493 for (int i = 0; i < items.count(); i++) {
1494 QTransform t = items.at(i)->transform();
1498 x.rotate = 180. / PI * atan2(-t.m21(), t.m11());
1499 m_transformations[items.at(i)] = x;
1501 // mbd: Update the GUI color selectors to match the stuff from the loaded document
1502 QColor background_color = m_titledocument.getBackgroundColor();
1503 horizontalSlider->blockSignals(true);
1504 kcolorbutton->blockSignals(true);
1505 horizontalSlider->setValue(background_color.alpha());
1506 background_color.setAlpha(255);
1507 kcolorbutton->setColor(background_color);
1508 horizontalSlider->blockSignals(false);
1509 kcolorbutton->blockSignals(false);
1511 /*startViewportX->setValue(m_startViewport->data(0).toInt());
1512 startViewportY->setValue(m_startViewport->data(1).toInt());
1513 startViewportSize->setValue(m_startViewport->data(2).toInt());
1514 endViewportX->setValue(m_endViewport->data(0).toInt());
1515 endViewportY->setValue(m_endViewport->data(1).toInt());
1516 endViewportSize->setValue(m_endViewport->data(2).toInt());*/
1518 QTimer::singleShot(200, this, SLOT(slotAdjustZoom()));
1522 /** \brief Connected to the accepted signal - calls writeChoices */
1523 void TitleWidget::slotAccepted()
1525 if (anim_start->isChecked()) slotAnimStart(false);
1526 if (anim_end->isChecked()) slotAnimEnd(false);
1530 /** \brief Store the current choices of font, background and rect values */
1531 void TitleWidget::writeChoices()
1533 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1534 KSharedConfigPtr config = KGlobal::config();
1535 KConfigGroup titleConfig(config, "TitleWidget");
1536 // Write the entries
1537 titleConfig.writeEntry("font_family", font_family->currentFont());
1538 //titleConfig.writeEntry("font_size", font_size->value());
1539 titleConfig.writeEntry("font_pixel_size", font_size->value());
1540 titleConfig.writeEntry("font_color", fontColorButton->color());
1541 titleConfig.writeEntry("font_alpha", textAlpha->value());
1542 titleConfig.writeEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1543 titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
1544 titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
1546 titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
1547 titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
1548 titleConfig.writeEntry("rect_background_color", rectBColor->color());
1549 titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
1550 titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
1552 titleConfig.writeEntry("background_color", kcolorbutton->color());
1553 titleConfig.writeEntry("background_alpha", horizontalSlider->value());
1555 //! \todo Not sure if I should sync - it is probably safe to do it
1560 /** \brief Read the last stored choices into the dialog */
1561 void TitleWidget::readChoices()
1563 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
1564 KSharedConfigPtr config = KGlobal::config();
1565 KConfigGroup titleConfig(config, "TitleWidget");
1567 font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
1568 font_size->setValue(titleConfig.readEntry("font_pixel_size", font_size->value()));
1569 fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
1570 textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
1572 if (titleConfig.readEntry("font_bold", false)) weight = QFont::Bold;
1573 else weight = titleConfig.readEntry("font_weight", font_weight_box->itemData(font_weight_box->currentIndex()).toInt());
1574 setFontBoxWeight(weight);
1575 buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
1576 buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
1578 rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
1579 rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
1580 rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
1581 rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
1582 rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
1584 kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
1585 horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
1588 void TitleWidget::adjustFrameSize()
1590 m_frameWidth = m_titledocument.frameWidth();
1591 m_frameHeight = m_titledocument.frameHeight();
1592 m_frameBorder->setRect(0, 0, m_frameWidth, m_frameHeight);
1593 displayBackgroundFrame();
1596 void TitleWidget::slotAnimStart(bool anim)
1598 if (anim && anim_end->isChecked()) {
1599 anim_end->setChecked(false);
1600 m_endViewport->setZValue(-1000);
1601 m_endViewport->setBrush(QBrush());
1604 QList<QGraphicsItem *> list = m_scene->items();
1605 for (int i = 0; i < list.count(); i++) {
1606 if (list.at(i)->zValue() > -1000) {
1607 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1608 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1611 align_box->setEnabled(anim);
1612 itemzoom->setEnabled(!anim);
1613 itemrotate->setEnabled(!anim);
1614 frame_toolbar->setEnabled(!anim);
1615 rect_properties->setEnabled(!anim);
1617 keep_aspect->setChecked(!m_startViewport->data(0).isNull());
1618 m_startViewport->setZValue(1100);
1619 QColor col = m_startViewport->pen().color();
1621 m_startViewport->setBrush(col);
1622 m_startViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1623 m_startViewport->setSelected(true);
1627 m_startViewport->setZValue(-1000);
1628 m_startViewport->setBrush(QBrush());
1629 m_startViewport->setFlags(0);
1634 void TitleWidget::slotAnimEnd(bool anim)
1636 if (anim && anim_start->isChecked()) {
1637 anim_start->setChecked(false);
1638 m_startViewport->setZValue(-1000);
1639 m_startViewport->setBrush(QBrush());
1642 QList<QGraphicsItem *> list = m_scene->items();
1643 for (int i = 0; i < list.count(); i++) {
1644 if (list.at(i)->zValue() > -1000) {
1645 list.at(i)->setFlag(QGraphicsItem::ItemIsMovable, !anim);
1646 list.at(i)->setFlag(QGraphicsItem::ItemIsSelectable, !anim);
1649 align_box->setEnabled(anim);
1650 itemzoom->setEnabled(!anim);
1651 itemrotate->setEnabled(!anim);
1652 frame_toolbar->setEnabled(!anim);
1653 rect_properties->setEnabled(!anim);
1655 keep_aspect->setChecked(!m_endViewport->data(0).isNull());
1656 m_endViewport->setZValue(1100);
1657 QColor col = m_endViewport->pen().color();
1659 m_endViewport->setBrush(col);
1660 m_endViewport->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
1661 m_endViewport->setSelected(true);
1665 m_endViewport->setZValue(-1000);
1666 m_endViewport->setBrush(QBrush());
1667 m_endViewport->setFlags(0);
1671 void TitleWidget::slotKeepAspect(bool keep)
1673 if (m_endViewport->zValue() == 1100) {
1674 m_endViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1675 m_endViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1677 m_startViewport->setData(0, keep == true ? m_frameWidth : QVariant());
1678 m_startViewport->setData(1, keep == true ? m_frameHeight : QVariant());
1682 void TitleWidget::slotResize50()
1684 if (m_endViewport->zValue() == 1100) {
1685 m_endViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1686 } else m_startViewport->setRect(0, 0, m_frameWidth / 2, m_frameHeight / 2);
1689 void TitleWidget::slotResize100()
1691 if (m_endViewport->zValue() == 1100) {
1692 m_endViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1693 } else m_startViewport->setRect(0, 0, m_frameWidth, m_frameHeight);
1696 void TitleWidget::slotResize200()
1698 if (m_endViewport->zValue() == 1100) {
1699 m_endViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);
1700 } else m_startViewport->setRect(0, 0, m_frameWidth * 2, m_frameHeight * 2);