view widgets/RelatedMediaWidget.cpp @ 258:14e8a46d506d

support "file://"
author lbajardsilogic
date Fri, 20 Jun 2008 07:26:03 +0000
parents 057856cf81a2
children
line wrap: on
line source
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*	Sound Access	
		EASAIER client application.	
		Silogic 2007. Laure Bajard. 
	
	This program is free software; you can redistribute it and/or    
	modify it under the terms of the GNU General Public License as    
	published by the Free Software Foundation; either version 2 of the    
	License, or (at your option) any later version.  See the file    
	COPYING included with this distribution for more information .
*/

#include "RelatedMediaWidget.h"

#include <QLayoutItem>
#include <QLinearGradient>
#include <QScrollArea>
#include <QVBoxLayout>
#include <QTextEdit>

#include <iostream>

#include "sv/main/MainWindow.h"

RelatedMediaWidget::RelatedMediaWidget() : QWidget() ,
	m_nbRelMedia(0),
	m_painter(0),
	m_relMediaMaxHeight(150)
{
	m_relMediaLayout = new FlowLayout; 
	//m_relMediaLayout->setSizeConstraint(QLayout::SetMinimumSize);

	setLayout(m_relMediaLayout);
}

RelatedMediaWidget::~RelatedMediaWidget()	
{}

void RelatedMediaWidget::reset()
{
	m_nbRelMedia = 0;

	QLayoutItem *child;
	while ((child = m_relMediaLayout->takeAt(0)) != 0) {
		delete child->widget();
	}
}

void RelatedMediaWidget::addRelatedMedia(const QString &filename, std::map<QString, QString> relMediaList)
{
	QString imageSource;
	QString extension = filename.right(filename.length() - (filename.lastIndexOf('.') + 1));

	int width;
	int height;
	
	if (extension == "pdf")
	{
		imageSource = ":icons/icon_pdf.png";
	} else
	{
		imageSource = filename;
	}

	QPixmap image(imageSource);
	if (image.height() > m_relMediaMaxHeight)
	{
		height	= m_relMediaMaxHeight;
		width	= image.width() * m_relMediaMaxHeight / image.height();
	} else 
	{
		height	= image.height();
		width	= image.width();
	}

	QLabel *mediaLabel = new QLabel();
	mediaLabel->setText("<html><header><style type=\"text/css\">a {text-decoration: none;}</style></header><body><a href=\"" + filename + 
		"\"><img src=\"" + imageSource + "\" width=\"" + QString::number(width) + "\" height=\"" + QString::number(height) + "\">&nbsp;</a></body></html>");
	
	connect(mediaLabel, SIGNAL(linkActivated(QString)), MainWindow::instance(), SLOT(runExternProcess(QString)));

	QLabel *textLabel = new QLabel();
	QString uri = "file:/" + filename.right(filename.length() - filename.lastIndexOf("/data"));
	std::map<QString, QString>::iterator iter = relMediaList.find(uri);
	if (iter != relMediaList.end())
	{
		textLabel->setText("<html><a >" + (iter->second).left(50) + "</a><a href=\"" + iter->second + "\">...</a></html>");
		connect(textLabel, SIGNAL(linkActivated(QString)), this, SLOT(showLabel(QString)));
	} else {
		textLabel->setText("<html><a >Info may be added here</a><a href=\"" + filename + "\">...</a></html>");
	}

	QVBoxLayout *mediaLayout = new QVBoxLayout;
	mediaLayout->addWidget(mediaLabel);
	mediaLayout->addWidget(textLabel);

	QWidget *mediaWidget = new QWidget;
	mediaWidget->setLayout(mediaLayout);

	m_relMediaLayout->addWidget(mediaWidget);

	m_nbRelMedia++;
}

void RelatedMediaWidget::showLabel(QString label)
{
	QTextEdit *showLabel = new QTextEdit;
	showLabel->setReadOnly(true);
	showLabel->setHtml("<html><br><br>" + label + "</br></br></html>");
	showLabel->show();
}