]> git.sesse.net Git - vlc/blobdiff - modules/gui/qt4/dialogs/external.cpp
Qt: forgotten include
[vlc] / modules / gui / qt4 / dialogs / external.cpp
index 4baeda6b683278cb17c2b67271dcf7b96b4a160f..e78eaa048da908c4f9c40fcec09ade920d791eb0 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * external.hpp : Dialogs from other LibVLC core and other plugins
+ * external.cpp : Dialogs from other LibVLC core and other plugins
  ****************************************************************************
  * Copyright (C) 2009 RĂ©mi Denis-Courmont
  * Copyright (C) 2006 the VideoLAN team
 #include <QLabel>
 #include <QLineEdit>
 #include <QMessageBox>
+#include <QProgressDialog>
+#include <QMutex>
+#include <QPushButton>
+#include <QTimer>
 
-QVLCVariable::QVLCVariable (vlc_object_t *obj, const char *varname, int type)
-    : object (obj), name (qfu(varname))
+DialogHandler::DialogHandler (intf_thread_t *intf, QObject *_parent)
+    : QObject( _parent ), intf (intf),
+      critical (VLC_OBJECT(intf), "dialog-critical"),
+      login (VLC_OBJECT(intf), "dialog-login"),
+      question (VLC_OBJECT(intf), "dialog-question"),
+      progressBar (VLC_OBJECT(intf), "dialog-progress-bar")
 {
-    var_Create (object, qtu(name), type);
-    var_AddCallback (object, qtu(name), callback, this);
-}
-
-QVLCVariable::~QVLCVariable (void)
-{
-    var_DelCallback (object, qtu(name), callback, this);
-    var_Destroy (object, qtu(name));
-}
-
-int QVLCVariable::callback (vlc_object_t *object, const char *,
-                            vlc_value_t, vlc_value_t cur, void *data)
-{
-    QVLCVariable *self = (QVLCVariable *)data;
-    emit self->pointerChanged (object, cur.p_address);
-}
+    var_Create (intf, "dialog-error", VLC_VAR_ADDRESS);
+    var_AddCallback (intf, "dialog-error", error, this);
+    connect (this, SIGNAL(error(const QString &, const QString &)),
+             SLOT(displayError(const QString &, const QString &)));
 
-
-DialogHandler::DialogHandler (intf_thread_t *intf)
-    : intf (intf),
-      message (VLC_OBJECT(intf), "dialog-fatal", VLC_VAR_ADDRESS),
-      login (VLC_OBJECT(intf), "dialog-login", VLC_VAR_ADDRESS),
-      question (VLC_OBJECT(intf), "dialog-question", VLC_VAR_ADDRESS)
-{
-    connect (&message, SIGNAL(pointerChanged(vlc_object_t *, void *)),
-             SLOT(displayMessage(vlc_object_t *, void *)),
+    connect (&critical, SIGNAL(pointerChanged(vlc_object_t *, void *)),
+             SLOT(displayCritical(vlc_object_t *, void *)),
              Qt::BlockingQueuedConnection);
     connect (&login, SIGNAL(pointerChanged(vlc_object_t *, void *)),
              SLOT(requestLogin(vlc_object_t *, void *)),
@@ -70,6 +59,12 @@ DialogHandler::DialogHandler (intf_thread_t *intf)
     connect (&question, SIGNAL(pointerChanged(vlc_object_t *, void *)),
              SLOT(requestAnswer(vlc_object_t *, void *)),
              Qt::BlockingQueuedConnection);
+    connect (&progressBar, SIGNAL(pointerChanged(vlc_object_t *, void *)),
+             SLOT(startProgressBar(vlc_object_t *, void *)),
+             Qt::BlockingQueuedConnection);
+    connect (this,
+             SIGNAL(progressBarDestroyed(QWidget *)),
+             SLOT(stopProgressBar(QWidget *)));
 
     dialog_Register (intf);
 }
@@ -77,19 +72,33 @@ DialogHandler::DialogHandler (intf_thread_t *intf)
 DialogHandler::~DialogHandler (void)
 {
     dialog_Unregister (intf);
+
+    var_DelCallback (intf, "dialog-error", error, this);
+    var_Destroy (intf, "dialog-error");
 }
 
-void DialogHandler::displayMessage (vlc_object_t *, void *value)
+int DialogHandler::error (vlc_object_t *obj, const char *,
+                          vlc_value_t, vlc_value_t value, void *data)
 {
-     const dialog_fatal_t *dialog = (const dialog_fatal_t *)value;
+    const dialog_fatal_t *dialog = (const dialog_fatal_t *)value.p_address;
+    DialogHandler *self = static_cast<DialogHandler *>(data);
 
-    if (dialog->modal)
-        QMessageBox::critical (NULL, qfu(dialog->title), qfu(dialog->message),
-                               QMessageBox::Ok);
-    else
-    if (config_GetInt (intf, "qt-error-dialogs"))
-        ErrorsDialog::getInstance (intf)->addError(qfu(dialog->title),
-                                                   qfu(dialog->message));
+    if (var_InheritBool (obj, "qt-error-dialogs"))
+        emit self->error (qfu(dialog->title), qfu(dialog->message));
+    return VLC_SUCCESS;
+}
+
+void DialogHandler::displayError (const QString& title, const QString& message)
+{
+    ErrorsDialog::getInstance (intf)->addError(title, message);
+}
+
+void DialogHandler::displayCritical (vlc_object_t *, void *value)
+{
+    const dialog_fatal_t *dialog = (const dialog_fatal_t *)value;
+
+    QMessageBox::critical (NULL, qfu(dialog->title), qfu(dialog->message),
+                           QMessageBox::Ok);
 }
 
 void DialogHandler::requestLogin (vlc_object_t *, void *value)
@@ -99,6 +108,7 @@ void DialogHandler::requestLogin (vlc_object_t *, void *value)
     QLayout *layout = new QVBoxLayout (dialog);
 
     dialog->setWindowTitle (qfu(data->title));
+    dialog->setWindowRole ("vlc-login");
     layout->setMargin (2);
 
     /* User name and password fields */
@@ -119,11 +129,14 @@ void DialogHandler::requestLogin (vlc_object_t *, void *value)
     layout->addWidget (panel);
 
     /* OK, Cancel buttons */
-    QDialogButtonBox *buttonBox;
-    buttonBox = new QDialogButtonBox (QDialogButtonBox::Ok
-                                       | QDialogButtonBox::Cancel);
-    connect (buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
-    connect (buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
+    QDialogButtonBox *buttonBox = new QDialogButtonBox;
+    QPushButton *okButton = new QPushButton( "&Ok" );
+    QPushButton *cancelButton = new QPushButton( "&Cancel" );
+    buttonBox->addButton( okButton, QDialogButtonBox::AcceptRole );
+    buttonBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
+
+    CONNECT( buttonBox, accepted(), dialog, accept() );
+    CONNECT( buttonBox, rejected(), dialog, reject() );
     layout->addWidget (buttonBox);
 
     /* Run the dialog */
@@ -150,9 +163,8 @@ void DialogHandler::requestAnswer (vlc_object_t *, void *value)
         ? box->addButton ("&" + qfu(data->yes), QMessageBox::YesRole) : NULL;
     QAbstractButton *no = (data->no != NULL)
         ? box->addButton ("&" + qfu(data->no), QMessageBox::NoRole) : NULL;
-    QAbstractButton *cancel = (data->cancel != NULL)
-        ? box->addButton ("&" + qfu(data->cancel), QMessageBox::RejectRole)
-        : NULL;
+    if (data->cancel != NULL)
+        box->addButton ("&" + qfu(data->cancel), QMessageBox::RejectRole);
 
     box->exec ();
 
@@ -168,3 +180,80 @@ void DialogHandler::requestAnswer (vlc_object_t *, void *value)
     delete box;
     data->answer = answer;
 }
+
+
+QVLCProgressDialog::QVLCProgressDialog (DialogHandler *parent,
+                                        struct dialog_progress_bar_t *data)
+    : QProgressDialog ( ),
+      handler (parent),
+      cancelled (false)
+{
+    setLabelText( qfu(data->message) );
+    setRange( 0, 0 );
+
+    if( data->cancel )
+        setCancelButton( new QPushButton( "&" + qfu(data->cancel) ) );
+    if (data->title != NULL)
+        setWindowTitle (qfu(data->title));
+
+    setWindowRole ("vlc-progress");
+    setMinimumDuration (300);
+    setValue( 0 );
+
+    connect (this, SIGNAL(progressed(int)), SLOT(setValue(int)));
+    connect (this, SIGNAL(described(const QString&)),
+                   SLOT(setLabelText(const QString&)));
+    connect (this, SIGNAL(canceled(void)), SLOT(saveCancel(void)));
+
+    data->pf_update = update;
+    data->pf_check = check;
+    data->pf_destroy = destroy;
+    data->p_sys = this;
+}
+
+
+void QVLCProgressDialog::update (void *priv, const char *text, float value)
+{
+    QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
+    if( value > 0 )
+        self->setRange( 0, 1000 );
+    if (text != NULL)
+        emit self->described (qfu(text));
+    emit self->progressed ((int)(value * 1000.));
+}
+
+static QMutex cancel_mutex;
+
+bool QVLCProgressDialog::check (void *priv)
+{
+    QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
+    QMutexLocker locker (&cancel_mutex);
+    return self->cancelled;
+}
+
+void QVLCProgressDialog::destroy (void *priv)
+{
+    QVLCProgressDialog *self = static_cast<QVLCProgressDialog *>(priv);
+
+    emit self->handler->progressBarDestroyed (self);
+}
+
+void QVLCProgressDialog::saveCancel (void)
+{
+    QMutexLocker locker (&cancel_mutex);
+    cancelled = true;
+}
+
+void DialogHandler::startProgressBar (vlc_object_t *, void *value)
+{
+    dialog_progress_bar_t *data = (dialog_progress_bar_t *)value;
+    QWidget *dlg = new QVLCProgressDialog (this, data);
+
+    QTimer::singleShot( 300, dlg, SLOT( show() ) );
+//    dlg->show ();
+}
+
+void DialogHandler::stopProgressBar (QWidget *dlg)
+{
+    delete dlg;
+}