]> git.sesse.net Git - kdenlive/blobdiff - src/statusbarmessagelabel.cpp
reindent + nice (working) progress bar for document loading
[kdenlive] / src / statusbarmessagelabel.cpp
index 2e1c591c47a0b961f4d8cb93f3c55652f6a9b2de..814d6ef0d05779fdbf45fad33a99b8042eb4ea43 100644 (file)
 #include <QKeyEvent>
 #include <QPushButton>
 #include <QPixmap>
-#include <QTimer>
+
 
 StatusBarMessageLabel::StatusBarMessageLabel(QWidget* parent) :
         QWidget(parent),
         m_type(DefaultMessage),
         m_state(Default),
-        m_illumination(0),
+        m_illumination(-64),
         m_minTextHeight(-1),
-        m_timer(0),
-        m_closeButton(0) {
+        m_closeButton(0)
+{
     setMinimumHeight(KIconLoader::SizeSmall);
 
     QPalette palette;
     palette.setColor(QPalette::Background, Qt::transparent);
     setPalette(palette);
 
-    m_timer = new QTimer(this);
-    connect(m_timer, SIGNAL(timeout()),
-            this, SLOT(timerDone()));
+    connect(&m_timer, SIGNAL(timeout()), this, SLOT(timerDone()));
 
     m_closeButton = new QPushButton(i18nc("@action:button", "Close"), this);
     m_closeButton->hide();
-    connect(m_closeButton, SIGNAL(clicked()),
-            this, SLOT(closeErrorMessage()));
+    connect(m_closeButton, SIGNAL(clicked()), this, SLOT(closeErrorMessage()));
 }
 
-StatusBarMessageLabel::~StatusBarMessageLabel() {
+StatusBarMessageLabel::~StatusBarMessageLabel()
+{
 }
 
 void StatusBarMessageLabel::setMessage(const QString& text,
-                                       MessageType type) {
+                                       MessageType type)
+{
     if ((text == m_text) && (type == m_type)) {
         return;
     }
@@ -80,9 +79,9 @@ void StatusBarMessageLabel::setMessage(const QString& text,
     m_text = text;
     m_type = type;
 
-    m_timer->stop();
-    m_illumination = 0;
+    m_illumination = -64;
     m_state = Default;
+    m_timer.stop();
 
     const char* iconName = 0;
     QPixmap pixmap;
@@ -100,11 +99,17 @@ void StatusBarMessageLabel::setMessage(const QString& text,
 
     case ErrorMessage:
         iconName = "dialog-warning";
-        m_timer->start(100);
+        m_timer.start(100);
         m_state = Illuminate;
+        m_closeButton->hide();
+        break;
 
-        //updateCloseButtonPosition();
-        //m_closeButton->show();
+    case MltError:
+        iconName = "dialog-close";
+        m_timer.start(100);
+        m_state = Illuminate;
+        updateCloseButtonPosition();
+        m_closeButton->show();
         break;
 
     case DefaultMessage:
@@ -114,11 +119,17 @@ void StatusBarMessageLabel::setMessage(const QString& text,
     }
 
     m_pixmap = (iconName == 0) ? QPixmap() : SmallIcon(iconName);
-    QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
-    show(); //update();
+
+    /*QFontMetrics fontMetrics(font());
+    setMaximumWidth(fontMetrics.boundingRect(m_text).width() + m_pixmap.width() + (BorderGap * 4));
+    updateGeometry();*/
+
+    //QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
+    update();
 }
 
-void StatusBarMessageLabel::setMinimumTextHeight(int min) {
+void StatusBarMessageLabel::setMinimumTextHeight(int min)
+{
     if (min != m_minTextHeight) {
         m_minTextHeight = min;
         setMinimumHeight(min);
@@ -128,36 +139,29 @@ void StatusBarMessageLabel::setMinimumTextHeight(int min) {
     }
 }
 
-int StatusBarMessageLabel::widthGap() const {
-    QFontMetrics fontMetrics(font());
-    const int defaultGap = 10;
-    return fontMetrics.width(m_text) - availableTextWidth() + defaultGap;
-}
-
-void StatusBarMessageLabel::paintEvent(QPaintEvent* /* event */) {
+void StatusBarMessageLabel::paintEvent(QPaintEvent* /* event */)
+{
     QPainter painter(this);
 
     // draw background
-    QColor backgroundColor = palette().window().color();
-    if (m_illumination > 0) {
-        // at this point, a: we are a second label being drawn over the already
-        // painted status area, so we can be translucent, and b: our palette's
-        // window color (bg only) seems to be wrong (always black)
+    QColor backgroundColor;
+    if (m_state == Default || m_illumination < 0) backgroundColor = palette().window().color();
+    else {
         KColorScheme scheme(palette().currentColorGroup(), KColorScheme::Window);
         backgroundColor = scheme.background(KColorScheme::NegativeBackground).color();
-        backgroundColor.setAlpha(qMin(255, m_illumination*2));
     }
-    painter.setBrush(backgroundColor);
-    painter.setPen(Qt::NoPen);
-    painter.drawRect(QRect(0, 0, width(), height()));
+    if (m_state == Desaturate && m_illumination > 0) {
+        backgroundColor.setAlpha(m_illumination * 2);
+    }
+    painter.fillRect(0, 0, width(), height(), backgroundColor);
 
     // draw pixmap
     int x = BorderGap;
-    int y = (m_minTextHeight - m_pixmap.height()) / 2;
+    int y = (height() - m_pixmap.height()) / 2;
 
     if (!m_pixmap.isNull()) {
         painter.drawPixmap(x, y, m_pixmap);
-        x += m_pixmap.width() + BorderGap;
+        x += m_pixmap.width() + BorderGap * 2;
     }
 
     // draw text
@@ -170,17 +174,19 @@ void StatusBarMessageLabel::paintEvent(QPaintEvent* /* event */) {
     painter.end();
 }
 
-void StatusBarMessageLabel::resizeEvent(QResizeEvent* event) {
+void StatusBarMessageLabel::resizeEvent(QResizeEvent* event)
+{
     QWidget::resizeEvent(event);
-    //updateCloseButtonPosition();
-    QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
+    updateCloseButtonPosition();
+    //QTimer::singleShot(GeometryTimeout, this, SLOT(assureVisibleText()));
 }
 
-void StatusBarMessageLabel::timerDone() {
+void StatusBarMessageLabel::timerDone()
+{
     switch (m_state) {
     case Illuminate: {
         // increase the illumination
-        const int illumination_max = 224;
+        const int illumination_max = 128;
         if (m_illumination < illumination_max) {
             m_illumination += 32;
             if (m_illumination > illumination_max) {
@@ -189,27 +195,30 @@ void StatusBarMessageLabel::timerDone() {
             update();
         } else {
             m_state = Illuminated;
-            m_timer->start(5000);
+            m_timer.start(1500);
         }
         break;
     }
 
     case Illuminated: {
         // start desaturation
-        m_state = Desaturate;
-        m_timer->start(100);
+        if (m_type != MltError) {
+            m_state = Desaturate;
+            m_timer.start(80);
+        }
         break;
     }
 
     case Desaturate: {
         // desaturate
-        if (m_illumination > 0) {
+        if (m_illumination < -128) {
+            m_illumination = 0;
+            m_state = Default;
+            m_timer.stop();
+            setMessage(QString(), DefaultMessage);
+        } else {
             m_illumination -= 5;
             update();
-        } else {
-            m_state = Default;
-            m_timer->stop();
-            reset();
         }
         break;
     }
@@ -219,7 +228,8 @@ void StatusBarMessageLabel::timerDone() {
     }
 }
 
-void StatusBarMessageLabel::assureVisibleText() {
+void StatusBarMessageLabel::assureVisibleText()
+{
     if (m_text.isEmpty()) {
         return;
     }
@@ -262,42 +272,38 @@ void StatusBarMessageLabel::assureVisibleText() {
         updateGeometry();
     }
 
-    //updateCloseButtonPosition();
+    updateCloseButtonPosition();
 }
 
-int StatusBarMessageLabel::availableTextWidth() const {
+int StatusBarMessageLabel::availableTextWidth() const
+{
     const int buttonWidth = 0; /*(m_type == ErrorMessage) ?
                             m_closeButton->width() + BorderGap : 0;*/
     return width() - m_pixmap.width() - (BorderGap * 4) - buttonWidth;
 }
 
-void StatusBarMessageLabel::updateCloseButtonPosition() {
+void StatusBarMessageLabel::updateCloseButtonPosition()
+{
     const int x = width() - m_closeButton->width() - BorderGap;
     const int y = (height() - m_closeButton->height()) / 2;
     m_closeButton->move(x, y);
 }
 
-void StatusBarMessageLabel::closeErrorMessage() {
+void StatusBarMessageLabel::closeErrorMessage()
+{
     if (!showPendingMessage()) {
-        reset();
-        setMessage(m_defaultText, DefaultMessage);
+        setMessage(QString(), DefaultMessage);
     }
 }
 
-bool StatusBarMessageLabel::showPendingMessage() {
+bool StatusBarMessageLabel::showPendingMessage()
+{
     if (!m_pendingMessages.isEmpty()) {
-        reset();
         setMessage(m_pendingMessages.takeFirst(), ErrorMessage);
         return true;
     }
     return false;
 }
 
-void StatusBarMessageLabel::reset() {
-    m_text.clear();
-    m_pixmap = QPixmap();
-    m_type = DefaultMessage;
-    update();
-}
 
 #include "statusbarmessagelabel.moc"