QgsExternalResourceWidget::QgsExternalResourceWidget( QWidget *parent )
  : QWidget( parent )
{
  setBackgroundRole( QPalette::Window );
  setAutoFillBackground( true );

  QGridLayout *layout = new QGridLayout();
  layout->setMargin( 0 );

  mFileWidget = new QgsFileWidget( this );
  layout->addWidget( mFileWidget, 0, 0 );
  mFileWidget->setVisible( mFileWidgetVisible );

  mPixmapLabel = new QgsPixmapLabel( this );
  layout->addWidget( mPixmapLabel, 1, 0 );

#ifdef WITH_QTWEBKIT
  mWebView = new QWebView( this );
  layout->addWidget( mWebView, 2, 0 );
#endif

  updateDocumentViewer();

  setLayout( layout );

  connect( mFileWidget, &QgsFileWidget::fileChanged, this, &QgsExternalResourceWidget::loadDocument );
  connect( mFileWidget, &QgsFileWidget::fileChanged, this, &QgsExternalResourceWidget::valueChanged );
}
QgsExternalResourceWidget::QgsExternalResourceWidget( QWidget *parent )
    : QWidget( parent )
    , mFileWidgetVisible( true )
    , mDocumentViewerContent( NoContent )
    , mDocumentViewerHeight( 0 )
    , mDocumentViewerWidth( 0 )
    , mRelativeStorage( QgsFileWidget::Absolute )

{
  setBackgroundRole( QPalette::Window );
  setAutoFillBackground( true );

  QGridLayout* layout = new QGridLayout();
  layout->setMargin( 0 );

  mFileWidget = new QgsFileWidget( this );
  layout->addWidget( mFileWidget, 0, 0 );
  mFileWidget->setVisible( mFileWidgetVisible );

  mPixmapLabel = new QgsPixmapLabel( this );
  layout->addWidget( mPixmapLabel, 1, 0 );

#ifdef WITH_QTWEBKIT
  mWebView = new QWebView( this );
  layout->addWidget( mWebView, 2, 0 );
#endif

  updateDocumentViewer();

  setLayout( layout );

  connect( mFileWidget, SIGNAL( fileChanged( QString ) ), this, SLOT( loadDocument( QString ) ) );
}
void QgsExternalResourceWidget::loadDocument( const QString &path )
{
  QString resolvedPath;

  if ( path.isEmpty() )
  {
#ifdef WITH_QTWEBKIT
    if ( mDocumentViewerContent == Web )
    {
      mWebView->setUrl( QUrl( QStringLiteral( "about:blank" ) ) );
    }
#endif
    if ( mDocumentViewerContent == Image )
    {
      mPixmapLabel->clear();
      updateDocumentViewer();
    }
  }
  else
  {
    resolvedPath = resolvePath( path );

#ifdef WITH_QTWEBKIT
    if ( mDocumentViewerContent == Web )
    {
      mWebView->setUrl( QUrl( resolvedPath ) );
    }
#endif

    if ( mDocumentViewerContent == Image )
    {
      QPixmap pm( resolvedPath );
      mPixmapLabel->setPixmap( pm );
      updateDocumentViewer();
    }
  }
}
void QgsExternalResourceWidget::setDocumentViewerContent( QgsExternalResourceWidget::DocumentViewerContent content )
{
  mDocumentViewerContent = content;
  updateDocumentViewer();
}
void QgsExternalResourceWidget::setDocumentViewerWidth( int width )
{
  mDocumentViewerWidth = width;
  updateDocumentViewer();
}
void QgsExternalResourceWidget::setDocumentViewerHeight( int height )
{
  mDocumentViewerHeight = height;
  updateDocumentViewer();
}