]> git.sesse.net Git - kdenlive/blobdiff - src/titlewidget.cpp
[PATCH by Ray Lehtiniemi] Do not return references to temporaries.
[kdenlive] / src / titlewidget.cpp
index 19cc14c96d0011f7d9f509d8e05ccd247d66504f..ed1b6801d9e5a8dc1e0c43a2c5d9d777eb5997dd 100644 (file)
  *                                                                         *
  ***************************************************************************/
 
+#include "titlewidget.h"
+#include "kdenlivesettings.h"
 
+#include <KDebug>
+#include <KGlobalSettings>
+#include <KFileDialog>
+#include <KStandardDirs>
 
-#include <QGraphicsView>
 #include <QDomDocument>
 #include <QGraphicsItem>
 #include <QGraphicsSvgItem>
 #include <QTimer>
-
 #include <QToolBar>
 #include <QMenu>
 
-#include <KDebug>
-#include <KGlobalSettings>
-#include <KFileDialog>
-
-#include "titlewidget.h"
-#include "kdenlivesettings.h"
-
 int settingUp = false;
 
-TitleWidget::TitleWidget(KUrl url, QString projectPath, Render *render, QWidget *parent): QDialog(parent), m_render(render), m_count(0), m_projectPath(projectPath) {
+TitleWidget::TitleWidget(KUrl url, QString projectPath, Render *render, QWidget *parent): QDialog(parent), m_render(render), m_count(0), m_projectPath(projectPath), startViewport(NULL), endViewport(NULL) {
     setupUi(this);
-    //frame_properties->
     setFont(KGlobalSettings::toolBarFont());
     //toolBox->setFont(KGlobalSettings::toolBarFont());
     frame_properties->setEnabled(false);
+    rect_properties->setFixedHeight(frame_properties->height() + 4);
+    text_properties->setFixedHeight(frame_properties->height() + 4);
     m_frameWidth = render->renderWidth();
     m_frameHeight = render->renderHeight();
     //connect(newTextButton, SIGNAL(clicked()), this, SLOT(slotNewText()));
     //connect(newRectButton, SIGNAL(clicked()), this, SLOT(slotNewRect()));
+    // kcolorbutton == The color of the background
     connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
+    // horizontalslider == The alpha of the background
     connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
     //connect(ktextedit, SIGNAL(textChanged()), this , SLOT(textChanged()));
     //connect (fontBold, SIGNAL ( clicked()), this, SLOT( setBold()) ) ;
@@ -88,6 +88,9 @@ TitleWidget::TitleWidget(KUrl url, QString projectPath, Render *render, QWidget
     connect(buttonUnder, SIGNAL(clicked()), this, SLOT(slotUpdateText()));
     connect(displayBg, SIGNAL(stateChanged(int)), this, SLOT(displayBackgroundFrame()));
 
+    // mbd
+    connect(this, SIGNAL(accepted()), this, SLOT(slotAccepted()));
+
     buttonFitZoom->setIcon(KIcon("zoom-fit-best"));
     buttonRealSize->setIcon(KIcon("zoom-original"));
     buttonBold->setIcon(KIcon("format-text-bold"));
@@ -101,7 +104,7 @@ TitleWidget::TitleWidget(KUrl url, QString projectPath, Render *render, QWidget
 
     QHBoxLayout *layout = new QHBoxLayout;
     frame_toolbar->setLayout(layout);
-
+    layout->setContentsMargins(2, 2, 2, 2);
     QToolBar *m_toolbar = new QToolBar("titleToolBar", this);
 
     m_buttonRect = m_toolbar->addAction(KIcon("kdenlive-insert-rect"), i18n("Add Rectangle"));
@@ -169,21 +172,63 @@ TitleWidget::TitleWidget(KUrl url, QString projectPath, Render *render, QWidget
     m_frameBorder->setFlags(QGraphicsItem::ItemClipsToShape);
     graphicsView->scene()->addItem(m_frameBorder);
 
-    initViewports();
+    // mbd: load saved settings
+    readChoices();
 
+    initViewports();
+    QTimer::singleShot(500, this, SLOT(slotAdjustZoom()));
     graphicsView->show();
     //graphicsView->setRenderHint(QPainter::Antialiasing);
     graphicsView->setInteractive(true);
-    QTimer::singleShot(500, this, SLOT(slotAdjustZoom()));
     //graphicsView->resize(400, 300);
     kDebug() << "// TITLE WIDGWT: " << graphicsView->viewport()->width() << "x" << graphicsView->viewport()->height();
     toolBox->setItemEnabled(2, false);
     if (!url.isEmpty()) {
         m_count = m_titledocument.loadDocument(url, startViewport, endViewport) + 1;
         slotSelectTool();
-    } else slotRectTool();
+    } else {
+        slotRectTool();
+    }
 }
 
+TitleWidget::~TitleWidget() {
+    delete m_buttonRect;
+    delete m_buttonText;
+    delete m_buttonImage;
+    delete m_buttonCursor;
+    delete m_buttonSave;
+    delete m_buttonLoad;
+
+    delete m_frameBorder;
+    delete m_frameImage;
+    if (startViewport) delete startViewport;
+    if (endViewport) delete endViewport;
+    delete m_scene;
+}
+
+//static
+QStringList TitleWidget::getFreeTitleInfo(const KUrl &projectUrl) {
+    QStringList result;
+    QString titlePath = projectUrl.path() + "/titles/";
+    KStandardDirs::makeDir(titlePath);
+    QString titleName = "title";
+    int counter = 0;
+    QString path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
+    while (QFile::exists(path + ".png")) {
+        counter++;
+        path = titlePath + titleName + QString::number(counter).rightJustified(3, '0', false);
+    }
+    result.append(titleName + QString::number(counter).rightJustified(3, '0', false));
+    result.append(path + ".png");
+    return result;
+}
+
+QString TitleWidget::getTitleResourceFromName(const KUrl &projectUrl, const QString &titleName) {
+    QStringList result;
+    QString titlePath = projectUrl.path() + "/titles/";
+    KStandardDirs::makeDir(titlePath);
+    return titlePath + titleName + ".png";
+}
 
 //virtual
 void TitleWidget::resizeEvent(QResizeEvent * event) {
@@ -286,8 +331,8 @@ void TitleWidget::initViewports() {
 }
 
 void TitleWidget::slotUpdateZoom(int pos) {
-    m_scene->setZoom((double) pos / 7);
-    zoom_label->setText("x" + QString::number((double) pos / 7, 'g', 2));
+    m_scene->setZoom((double) pos / 100);
+    zoom_label->setText(QString::number(pos) + '%');
 }
 
 void TitleWidget::slotZoom(bool up) {
@@ -298,16 +343,18 @@ void TitleWidget::slotZoom(bool up) {
 }
 
 void TitleWidget::slotAdjustZoom() {
-    double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
+    /*double scalex = graphicsView->width() / (double)(m_frameWidth * 1.2);
     double scaley = graphicsView->height() / (double)(m_frameHeight * 1.2);
     if (scalex > scaley) scalex = scaley;
-    int zoompos = (int)(scalex * 7 + 0.5);
+    int zoompos = (int)(scalex * 7 + 0.5);*/
+    graphicsView->fitInView(m_frameBorder, Qt::KeepAspectRatio);
+    int zoompos = graphicsView->matrix().m11() * 100;
     zoom_slider->setValue(zoompos);
     graphicsView->centerOn(m_frameBorder);
 }
 
 void TitleWidget::slotZoomOneToOne() {
-    zoom_slider->setValue(7);
+    zoom_slider->setValue(100);
     graphicsView->centerOn(m_frameBorder);
 }
 
@@ -328,6 +375,11 @@ void TitleWidget::slotNewRect(QGraphicsRectItem * rect) {
 void TitleWidget::slotNewText(QGraphicsTextItem *tt) {
     QFont font = font_family->currentFont();
     font.setPointSize(font_size->value());
+    // mbd: issue 551:
+    font.setBold(buttonBold->isChecked());
+    font.setItalic(buttonItalic->isChecked());
+    font.setUnderline(buttonUnder->isChecked());
+
     tt->setFont(font);
     QColor color = fontColorButton->color();
     color.setAlpha(textAlpha->value());
@@ -356,7 +408,7 @@ void TitleWidget::selectionChanged() {
     value_y->blockSignals(true);
     value_w->blockSignals(true);
     value_h->blockSignals(true);
-    kDebug() << "////////  SELECTION CHANGED; ITEMS: " << l.size();
+    //kDebug() << "////////  SELECTION CHANGED; ITEMS: " << l.size();
     if (l.size() == 1) {
         if ((l[0])->type() == 8) {
             rect_properties->setHidden(true);
@@ -409,7 +461,7 @@ void TitleWidget::selectionChanged() {
             //toolBox->setItemEnabled(3, true);
             rectFAlpha->setValue(rec->pen().color().alpha());
             rectBAlpha->setValue(rec->brush().color().alpha());
-            kDebug() << rec->brush().color().alpha();
+            //kDebug() << rec->brush().color().alpha();
             QColor fcol = rec->pen().color();
             QColor bcol = rec->brush().color();
             //fcol.setAlpha(255);
@@ -622,16 +674,26 @@ QDomDocument TitleWidget::xml() {
 }
 
 void TitleWidget::setXml(QDomDocument doc) {
-    m_titledocument.loadFromXml(doc, startViewport, endViewport);
+    m_count = m_titledocument.loadFromXml(doc, startViewport, endViewport);
+    // mbd: Update the GUI color selectors to match the stuff from the loaded document
+    QColor background_color = m_titledocument.getBackgroundColor();
+    horizontalSlider->blockSignals(true);
+    kcolorbutton->blockSignals(true);
+    horizontalSlider->setValue(background_color.alpha());
+    background_color.setAlpha(255);
+    kcolorbutton->setColor(background_color);
+    horizontalSlider->blockSignals(false);
+    kcolorbutton->blockSignals(false);
+
     slotSelectTool();
 }
 
-QPixmap TitleWidget::renderedPixmap() {
-    QPixmap pix(m_frameWidth, m_frameHeight);
+QImage TitleWidget::renderedPixmap() {
+    QImage pix(m_frameWidth, m_frameHeight, QImage::Format_ARGB32);
     pix.fill(Qt::transparent);
     QPainter painter(&pix);
-    painter.setRenderHint(QPainter::Antialiasing);
-    m_scene->clearSelection();
+    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
+    m_scene->clearTextSelection();
     QPen framepen = m_frameBorder->pen();
     m_frameBorder->setPen(Qt::NoPen);
     startViewport->setVisible(false);
@@ -639,6 +701,7 @@ QPixmap TitleWidget::renderedPixmap() {
     m_frameImage->setVisible(false);
 
     m_scene->render(&painter, QRectF(), QRectF(0, 0, m_frameWidth, m_frameHeight));
+    painter.end();
     m_frameBorder->setPen(framepen);
     startViewport->setVisible(true);
     endViewport->setVisible(true);
@@ -646,5 +709,59 @@ QPixmap TitleWidget::renderedPixmap() {
     return pix;
 }
 
-#include "moc_titlewidget.cpp"
+/** \brief Connected to the accepted signal - calls writeChoices */
+void TitleWidget::slotAccepted() {
+    writeChoices();
+}
+
+/** \brief Store the current choices of font, background and rect values */
+void TitleWidget::writeChoices() {
+    // Get a pointer to a shared configuration instance, then get the TitleWidget group.
+    KSharedConfigPtr config = KGlobal::config();
+    KConfigGroup titleConfig(config, "TitleWidget");
+    // Write the entries
+    titleConfig.writeEntry("font_family", font_family->currentFont());
+    titleConfig.writeEntry("font_size", font_size->value());
+    titleConfig.writeEntry("font_color", fontColorButton->color());
+    titleConfig.writeEntry("font_alpha", textAlpha->value());
+    titleConfig.writeEntry("font_bold", buttonBold->isChecked());
+    titleConfig.writeEntry("font_italic", buttonItalic->isChecked());
+    titleConfig.writeEntry("font_underlined", buttonUnder->isChecked());
+
+    titleConfig.writeEntry("rect_foreground_color", rectFColor->color());
+    titleConfig.writeEntry("rect_foreground_alpha", rectFAlpha->value());
+    titleConfig.writeEntry("rect_background_color", rectBColor->color());
+    titleConfig.writeEntry("rect_background_alpha", rectBAlpha->value());
+    titleConfig.writeEntry("rect_line_width", rectLineWidth->value());
+
+    titleConfig.writeEntry("background_color", kcolorbutton->color());
+    titleConfig.writeEntry("background_alpha", horizontalSlider->value());
+    //! \todo Not sure if I should sync - it is probably safe to do it
+    config->sync();
+
+}
+
+/** \brief Read the last stored choices into the dialog */
+void TitleWidget::readChoices() {
+    // Get a pointer to a shared configuration instance, then get the TitleWidget group.
+    KSharedConfigPtr config = KGlobal::config();
+    KConfigGroup titleConfig(config, "TitleWidget");
+    // read the entries
+    font_family->setCurrentFont(titleConfig.readEntry("font_family", font_family->currentFont()));
+    font_size->setValue(titleConfig.readEntry("font_size", font_size->value()));
+    fontColorButton->setColor(titleConfig.readEntry("font_color", fontColorButton->color()));
+    textAlpha->setValue(titleConfig.readEntry("font_alpha", textAlpha->value()));
+    buttonBold->setChecked(titleConfig.readEntry("font_bold", buttonBold->isChecked()));
+    buttonItalic->setChecked(titleConfig.readEntry("font_italic", buttonItalic->isChecked()));
+    buttonUnder->setChecked(titleConfig.readEntry("font_underlined", buttonUnder->isChecked()));
+
+    rectFColor->setColor(titleConfig.readEntry("rect_foreground_color", rectFColor->color()));
+    rectFAlpha->setValue(titleConfig.readEntry("rect_foreground_alpha", rectFAlpha->value()));
+    rectBColor->setColor(titleConfig.readEntry("rect_background_color", rectBColor->color()));
+    rectBAlpha->setValue(titleConfig.readEntry("rect_background_alpha", rectBAlpha->value()));
+    rectLineWidth->setValue(titleConfig.readEntry("rect_line_width", rectLineWidth->value()));
+
+    kcolorbutton->setColor(titleConfig.readEntry("background_color", kcolorbutton->color()));
+    horizontalSlider->setValue(titleConfig.readEntry("background_alpha", horizontalSlider->value()));
+}