Beispiel #1
0
QVariant DocumentModel::data( const QModelIndex &index, int role ) const
{
	if( !hasIndex( index.row(), index.column() ) )
		return QVariant();

	DataFile file = d->b->dataFiles().at( index.row() );
	switch( role )
	{
	case Qt::ForegroundRole:
		switch( index.column() )
		{
		case Size: return QColor(Qt::gray);
		default: return QVariant();
		}
	case Qt::DisplayRole:
		switch( index.column() )
		{
		case Id: return from( file.id() );
		case Name: return from( file.fileName() );
		case Mime: return from( file.mediaType() );
		case Size: return FileDialog::fileSize( file.fileSize() );
		default: return QVariant();
		}
	case Qt::TextAlignmentRole:
		switch( index.column() )
		{
		case Name:
		case Mime: return int(Qt::AlignLeft|Qt::AlignVCenter);
		case Size: return int(Qt::AlignRight|Qt::AlignVCenter);
		default: return Qt::AlignCenter;
		}
	case Qt::ToolTipRole:
		switch( index.column() )
		{
		case Save: return tr("Save");
		case Remove: return tr("Remove");
		default: return tr("Filename: %1\nFilesize: %2\nMedia type: %3")
			.arg( from( file.fileName() ) )
			.arg( FileDialog::fileSize( file.fileSize() ) )
			.arg( from( file.mediaType() ) );
		}
	case Qt::DecorationRole:
		switch( index.column() )
		{
		case Save: return QPixmap(":/images/ico_save.png");
		case Remove: return QPixmap(":/images/ico_delete.png");
		default: return QVariant();
		}
	case Qt::SizeHintRole:
		switch( index.column() )
		{
		case Save:
		case Remove: return QSize( 20, 20 );
		default: return QVariant();
		}
	default: return QVariant();
	}
}
Beispiel #2
0
QString DocumentModel::save( const QModelIndex &index, const QString &path ) const
{
	if( !hasIndex( index.row(), index.column() ) )
		return QString();
	DataFile file = d->b->dataFiles().at( index.row() );

	QFileInfo info( from( file.fileName() ) );
	QString dst = path;
	if( dst.isEmpty() )
	{
		dst = QDir::tempPath() + "/" + info.fileName();
		if( QFile::exists( dst ) )
		{
			for( unsigned int i = 1; i < 100; ++i )
			{
				dst = QString( "%1/%2_%3.%4").arg( QDir::tempPath() ).arg( info.baseName() ).arg( i ).arg( info.completeSuffix() );
				if( !QFile::exists( dst ) )
					break;
			}
		}
	}

	if( QFileInfo( path ).isDir() )
	{
		QString filename = info.fileName();
#if defined(Q_OS_WIN)
		filename.replace( QRegExp( "[\\\\/*:?\"<>|]" ), "_" );
#else
		filename.replace( QRegExp( "[\\\\]"), "_" );
#endif
		dst += path.isEmpty() ? filename : QDir::toNativeSeparators( "/" + filename );
	}
	QFile::remove( dst );
	file.saveAs( dst.toUtf8().constData() );
	return dst;
}