2013-07-04 21:59:40 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Klaas Freitag <freitag@kde.org>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mirall/folderstatusmodel.h"
|
2013-07-19 12:04:09 +04:00
|
|
|
#include "mirall/utility.h"
|
2013-07-04 21:59:40 +04:00
|
|
|
|
|
|
|
#include <QtCore>
|
|
|
|
#include <QtGui>
|
2013-08-28 22:58:22 +04:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
|
|
|
|
#include <QtWidgets>
|
|
|
|
#endif
|
2013-07-04 21:59:40 +04:00
|
|
|
|
|
|
|
namespace Mirall {
|
|
|
|
|
|
|
|
FolderStatusModel::FolderStatusModel()
|
|
|
|
:QStandardItemModel()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-30 16:53:42 +04:00
|
|
|
Qt::ItemFlags FolderStatusModel::flags ( const QModelIndex& ) const
|
2013-07-04 21:59:40 +04:00
|
|
|
{
|
2013-08-01 21:11:05 +04:00
|
|
|
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
|
2013-07-04 21:59:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant FolderStatusModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
if (role == Qt::EditRole)
|
|
|
|
return QVariant();
|
|
|
|
else
|
|
|
|
return QStandardItemModel::data(index,role);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ====================================================================================
|
|
|
|
|
|
|
|
FolderStatusDelegate::FolderStatusDelegate()
|
|
|
|
:QStyledItemDelegate()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
FolderStatusDelegate::~FolderStatusDelegate()
|
|
|
|
{
|
|
|
|
// TODO Auto-generated destructor stub
|
|
|
|
}
|
|
|
|
|
|
|
|
//alocate each item size in listview.
|
|
|
|
QSize FolderStatusDelegate::sizeHint(const QStyleOptionViewItem & option ,
|
|
|
|
const QModelIndex & index) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(option)
|
|
|
|
QFont aliasFont = option.font;
|
|
|
|
QFont font = option.font;
|
|
|
|
aliasFont.setPointSize( font.pointSize() +2 );
|
|
|
|
|
|
|
|
QFontMetrics fm(font);
|
|
|
|
QFontMetrics aliasFm(aliasFont);
|
|
|
|
|
|
|
|
int aliasMargin = aliasFm.height()/2;
|
|
|
|
int margin = fm.height()/4;
|
|
|
|
|
|
|
|
// calc height
|
|
|
|
|
|
|
|
int h = aliasMargin; // margin to top
|
|
|
|
h += aliasFm.height(); // alias
|
|
|
|
h += margin; // between alias and local path
|
|
|
|
h += fm.height(); // local path
|
|
|
|
h += margin; // between local and remote path
|
|
|
|
h += fm.height(); // remote path
|
|
|
|
h += aliasMargin; // bottom margin
|
|
|
|
|
|
|
|
// add some space to show an error condition.
|
2013-09-05 21:25:59 +04:00
|
|
|
if( ! qvariant_cast<QStringList>(index.data(FolderErrorMsg)).isEmpty() ) {
|
|
|
|
QStringList errMsgs = qvariant_cast<QStringList>(index.data(FolderErrorMsg));
|
|
|
|
h += aliasMargin*2 + errMsgs.count()*fm.height();
|
2013-07-04 21:59:40 +04:00
|
|
|
}
|
|
|
|
|
2013-07-18 00:32:47 +04:00
|
|
|
if( qvariant_cast<bool>(index.data(AddProgressSpace)) ) {
|
|
|
|
int margin = fm.height()/4;
|
|
|
|
h += (5 * margin); // All the margins
|
|
|
|
h += 2* fm.boundingRect(tr("File")).height();
|
|
|
|
}
|
|
|
|
|
2013-07-04 21:59:40 +04:00
|
|
|
return QSize( 0, h);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FolderStatusDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
|
2013-07-18 00:32:47 +04:00
|
|
|
const QModelIndex &index) const
|
2013-07-04 21:59:40 +04:00
|
|
|
{
|
|
|
|
QStyledItemDelegate::paint(painter,option,index);
|
|
|
|
|
|
|
|
painter->save();
|
|
|
|
|
|
|
|
QFont aliasFont = option.font;
|
|
|
|
QFont subFont = option.font;
|
|
|
|
QFont errorFont = subFont;
|
2013-07-18 00:32:47 +04:00
|
|
|
QFont progressFont = subFont;
|
2013-07-04 21:59:40 +04:00
|
|
|
|
2013-07-18 00:32:47 +04:00
|
|
|
progressFont.setPointSize( subFont.pointSize()-1);
|
2013-07-04 21:59:40 +04:00
|
|
|
//font.setPixelSize(font.weight()+);
|
|
|
|
aliasFont.setBold(true);
|
|
|
|
aliasFont.setPointSize( subFont.pointSize()+2 );
|
|
|
|
|
|
|
|
QFontMetrics subFm( subFont );
|
|
|
|
QFontMetrics aliasFm( aliasFont );
|
2013-07-22 14:57:58 +04:00
|
|
|
QFontMetrics progressFm( progressFont );
|
2013-07-04 21:59:40 +04:00
|
|
|
|
|
|
|
int aliasMargin = aliasFm.height()/2;
|
|
|
|
int margin = subFm.height()/4;
|
|
|
|
|
2013-08-02 16:22:01 +04:00
|
|
|
QIcon statusIcon = qvariant_cast<QIcon>(index.data(FolderStatusIconRole));
|
|
|
|
QString aliasText = qvariant_cast<QString>(index.data(FolderAliasRole));
|
|
|
|
QString pathText = qvariant_cast<QString>(index.data(FolderPathRole));
|
|
|
|
QString remotePath = qvariant_cast<QString>(index.data(FolderSecondPathRole));
|
2013-09-05 21:25:59 +04:00
|
|
|
QStringList errorTexts= qvariant_cast<QStringList>(index.data(FolderErrorMsg));
|
2013-07-22 14:57:58 +04:00
|
|
|
|
2013-07-24 16:40:34 +04:00
|
|
|
int overallPercent = qvariant_cast<int>(index.data(SyncProgressOverallPercent));
|
|
|
|
QString overallString = qvariant_cast<QString>(index.data(SyncProgressOverallString));
|
|
|
|
QString itemString = qvariant_cast<QString>(index.data(SyncProgressItemString));
|
2013-08-02 16:22:01 +04:00
|
|
|
int warningCount = qvariant_cast<int>(index.data(WarningCount));
|
|
|
|
bool syncOngoing = qvariant_cast<bool>(index.data(SyncRunning));
|
2013-07-04 21:59:40 +04:00
|
|
|
// QString statusText = qvariant_cast<QString>(index.data(FolderStatus));
|
|
|
|
bool syncEnabled = index.data(FolderSyncEnabled).toBool();
|
|
|
|
// QString syncStatus = syncEnabled? tr( "Enabled" ) : tr( "Disabled" );
|
|
|
|
|
|
|
|
QRect iconRect = option.rect;
|
|
|
|
QRect aliasRect = option.rect;
|
|
|
|
|
|
|
|
iconRect.setLeft( aliasMargin );
|
|
|
|
iconRect.setTop( iconRect.top() + aliasMargin ); // (iconRect.height()-iconsize.height())/2);
|
|
|
|
|
2013-07-07 03:24:42 +04:00
|
|
|
// alias box
|
2013-07-04 21:59:40 +04:00
|
|
|
aliasRect.setTop(aliasRect.top() + aliasMargin );
|
|
|
|
aliasRect.setBottom(aliasRect.top() + aliasFm.height());
|
|
|
|
aliasRect.setRight(aliasRect.right() - aliasMargin );
|
|
|
|
|
|
|
|
// remote directory box
|
2013-07-07 03:24:42 +04:00
|
|
|
QRect remotePathRect = aliasRect;
|
|
|
|
remotePathRect.setTop(aliasRect.bottom() + margin );
|
|
|
|
remotePathRect.setBottom(remotePathRect.top() + subFm.height());
|
2013-07-04 21:59:40 +04:00
|
|
|
|
2013-07-07 03:24:42 +04:00
|
|
|
// local directory box
|
|
|
|
QRect localPathRect = remotePathRect;
|
|
|
|
localPathRect.setTop( remotePathRect.bottom() + margin );
|
|
|
|
localPathRect.setBottom( localPathRect.top() + subFm.height());
|
|
|
|
|
|
|
|
iconRect.setBottom(localPathRect.bottom());
|
2013-07-04 21:59:40 +04:00
|
|
|
iconRect.setWidth(iconRect.height());
|
|
|
|
|
|
|
|
int nextToIcon = iconRect.right()+aliasMargin;
|
|
|
|
aliasRect.setLeft(nextToIcon);
|
|
|
|
localPathRect.setLeft(nextToIcon);
|
|
|
|
remotePathRect.setLeft(nextToIcon);
|
|
|
|
|
|
|
|
int iconSize = iconRect.width();
|
|
|
|
|
|
|
|
QPixmap pm = statusIcon.pixmap(iconSize, iconSize, syncEnabled ? QIcon::Normal : QIcon::Disabled );
|
|
|
|
painter->drawPixmap(QPoint(iconRect.left(), iconRect.top()), pm);
|
|
|
|
|
2013-08-02 16:22:01 +04:00
|
|
|
// only show the warning icon if the sync is running. Otherwise its
|
|
|
|
// encoded in the status icon.
|
|
|
|
if( warningCount > 0 && syncOngoing) {
|
|
|
|
QRect warnRect;
|
|
|
|
warnRect.setLeft(iconRect.left());
|
|
|
|
warnRect.setTop(iconRect.bottom()-17);
|
|
|
|
warnRect.setWidth(16);
|
|
|
|
warnRect.setHeight(16);
|
|
|
|
|
|
|
|
QIcon warnIcon(":/mirall/resources/warning-16");
|
|
|
|
QPixmap pm = warnIcon.pixmap(16,16, syncEnabled ? QIcon::Normal : QIcon::Disabled );
|
|
|
|
painter->drawPixmap(QPoint(warnRect.left(), warnRect.top()),pm );
|
|
|
|
}
|
|
|
|
|
2013-07-04 21:59:40 +04:00
|
|
|
if ((option.state & QStyle::State_Selected)
|
|
|
|
&& (option.state & QStyle::State_Active)
|
|
|
|
// Hack: Windows Vista's light blue is not contrasting enough for white
|
|
|
|
&& !qApp->style()->inherits("QWindowsVistaStyle")) {
|
|
|
|
painter->setPen(option.palette.color(QPalette::HighlightedText));
|
|
|
|
} else {
|
|
|
|
painter->setPen(option.palette.color(QPalette::Text));
|
|
|
|
}
|
|
|
|
QString elidedAlias = aliasFm.elidedText(aliasText, Qt::ElideRight, aliasRect.width());
|
|
|
|
painter->setFont(aliasFont);
|
|
|
|
painter->drawText(aliasRect, elidedAlias);
|
|
|
|
|
|
|
|
painter->setFont(subFont);
|
2013-07-07 03:24:42 +04:00
|
|
|
QString elidedRemotePathText;
|
|
|
|
|
|
|
|
if (remotePath.isEmpty() || remotePath == QLatin1String("/")) {
|
2013-07-22 20:06:27 +04:00
|
|
|
elidedRemotePathText = subFm.elidedText(tr("Syncing all files in your account with"),
|
2013-07-07 03:24:42 +04:00
|
|
|
Qt::ElideRight, remotePathRect.width());
|
|
|
|
} else {
|
|
|
|
elidedRemotePathText = subFm.elidedText(tr("Remote path: %1").arg(remotePath),
|
|
|
|
Qt::ElideMiddle, remotePathRect.width());
|
|
|
|
}
|
|
|
|
painter->drawText(remotePathRect, elidedRemotePathText);
|
|
|
|
|
2013-07-04 21:59:40 +04:00
|
|
|
QString elidedPathText = subFm.elidedText(pathText, Qt::ElideMiddle, localPathRect.width());
|
|
|
|
painter->drawText(localPathRect, elidedPathText);
|
|
|
|
|
|
|
|
// paint an error overlay if there is an error string
|
2013-07-19 19:05:35 +04:00
|
|
|
|
|
|
|
int h = iconRect.bottom();
|
2013-09-05 21:25:59 +04:00
|
|
|
if( !errorTexts.isEmpty() ) {
|
2013-08-15 13:30:39 +04:00
|
|
|
h += aliasMargin;
|
2013-07-04 21:59:40 +04:00
|
|
|
QRect errorRect = localPathRect;
|
|
|
|
errorRect.setLeft( iconRect.left());
|
2013-07-19 19:05:35 +04:00
|
|
|
errorRect.setTop( h );
|
2013-09-05 21:25:59 +04:00
|
|
|
errorRect.setHeight(errorTexts.count() * subFm.height()+aliasMargin);
|
2013-07-04 21:59:40 +04:00
|
|
|
errorRect.setRight( option.rect.right()-aliasMargin );
|
|
|
|
|
|
|
|
painter->setBrush( QColor(0xbb, 0x4d, 0x4d) );
|
|
|
|
painter->setPen( QColor(0xaa, 0xaa, 0xaa));
|
|
|
|
painter->drawRoundedRect( errorRect, 4, 4 );
|
|
|
|
|
|
|
|
painter->setPen( Qt::white );
|
|
|
|
painter->setFont(errorFont);
|
|
|
|
QRect errorTextRect = errorRect;
|
2013-09-05 21:25:59 +04:00
|
|
|
errorTextRect.setLeft( errorTextRect.left()+aliasMargin );
|
2013-07-04 21:59:40 +04:00
|
|
|
errorTextRect.setTop( errorTextRect.top()+aliasMargin/2 );
|
|
|
|
|
2013-09-05 21:25:59 +04:00
|
|
|
int x = errorTextRect.left();
|
|
|
|
int y = errorTextRect.top()+aliasMargin/2 + subFm.height()/2;
|
|
|
|
|
|
|
|
foreach( QString eText, errorTexts ) {
|
|
|
|
painter->drawText(x, y, subFm.elidedText( eText, Qt::ElideLeft, errorTextRect.width()-2*aliasMargin));
|
|
|
|
y += subFm.height();
|
2013-07-04 21:59:40 +04:00
|
|
|
}
|
2013-07-19 19:05:35 +04:00
|
|
|
|
|
|
|
h = errorRect.bottom();
|
2013-07-04 21:59:40 +04:00
|
|
|
}
|
2013-07-19 19:05:35 +04:00
|
|
|
h += aliasMargin;
|
|
|
|
|
2013-07-22 14:57:58 +04:00
|
|
|
// Sync File Progress Bar: Show it if syncFile is not empty.
|
2013-07-24 16:40:34 +04:00
|
|
|
if( !overallString.isEmpty()) {
|
2013-07-19 19:05:35 +04:00
|
|
|
int fileNameTextHeight = subFm.boundingRect(tr("File")).height();
|
2013-08-18 00:56:02 +04:00
|
|
|
int barHeight = qMax(fileNameTextHeight, aliasFm.height()+2); ;
|
2013-07-19 19:05:35 +04:00
|
|
|
int overallWidth = option.rect.width()-2*aliasMargin;
|
|
|
|
|
|
|
|
painter->save();
|
2013-07-04 21:59:40 +04:00
|
|
|
|
2013-07-19 19:05:35 +04:00
|
|
|
// Sizes-Text
|
2013-10-15 16:57:14 +04:00
|
|
|
QRect octetRect = progressFm.boundingRect( overallString );
|
|
|
|
int progressTextWidth = octetRect.width() + 2;
|
2013-07-19 19:05:35 +04:00
|
|
|
|
2013-07-22 14:57:58 +04:00
|
|
|
// Overall Progress Bar.
|
2013-07-19 19:05:35 +04:00
|
|
|
QRect pBRect;
|
|
|
|
pBRect.setTop( h );
|
|
|
|
pBRect.setLeft( iconRect.left());
|
|
|
|
pBRect.setHeight(barHeight);
|
|
|
|
pBRect.setWidth( overallWidth - progressTextWidth - margin );
|
|
|
|
|
|
|
|
QStyleOptionProgressBarV2 pBarOpt;
|
2013-08-18 00:56:02 +04:00
|
|
|
|
2013-07-19 19:05:35 +04:00
|
|
|
pBarOpt.state = option.state | QStyle::State_Horizontal;
|
|
|
|
pBarOpt.minimum = 0;
|
|
|
|
pBarOpt.maximum = 100;
|
|
|
|
pBarOpt.progress = overallPercent;
|
|
|
|
pBarOpt.orientation = Qt::Horizontal;
|
|
|
|
pBarOpt.palette = option.palette;
|
|
|
|
pBarOpt.rect = pBRect;
|
|
|
|
|
|
|
|
QApplication::style()->drawControl( QStyle::CE_ProgressBar, &pBarOpt, painter );
|
|
|
|
|
|
|
|
// Overall Progress Text
|
|
|
|
QRect overallProgressRect;
|
|
|
|
overallProgressRect.setTop( pBRect.top() );
|
|
|
|
overallProgressRect.setHeight( pBRect.height() );
|
|
|
|
overallProgressRect.setLeft( pBRect.right()+margin);
|
|
|
|
overallProgressRect.setWidth( progressTextWidth );
|
|
|
|
painter->setFont(progressFont);
|
2013-08-08 12:42:46 +04:00
|
|
|
|
2013-10-15 16:57:14 +04:00
|
|
|
painter->drawText( overallProgressRect, Qt::AlignRight+Qt::AlignVCenter, overallString);
|
2013-08-18 00:56:02 +04:00
|
|
|
// painter->drawRect(overallProgressRect);
|
2013-07-19 19:05:35 +04:00
|
|
|
|
2013-07-22 14:57:58 +04:00
|
|
|
// Individual File Progress
|
2013-07-19 19:05:35 +04:00
|
|
|
QRect fileRect;
|
|
|
|
fileRect.setTop( pBRect.bottom() + margin);
|
|
|
|
fileRect.setLeft( iconRect.left());
|
2013-07-22 14:57:58 +04:00
|
|
|
fileRect.setWidth(overallWidth);
|
2013-07-19 19:05:35 +04:00
|
|
|
fileRect.setHeight(fileNameTextHeight);
|
2013-10-15 16:57:14 +04:00
|
|
|
QString elidedText = progressFm.elidedText(itemString, Qt::ElideLeft, fileRect.width());
|
2013-07-18 00:32:47 +04:00
|
|
|
|
2013-08-08 12:42:46 +04:00
|
|
|
painter->drawText( fileRect, Qt::AlignLeft+Qt::AlignVCenter, elidedText);
|
2013-07-18 00:32:47 +04:00
|
|
|
|
2013-07-19 13:07:47 +04:00
|
|
|
painter->restore();
|
2013-07-18 00:32:47 +04:00
|
|
|
}
|
2013-07-24 16:40:34 +04:00
|
|
|
painter->restore();
|
2013-07-04 21:59:40 +04:00
|
|
|
}
|
|
|
|
|
2013-07-30 16:37:46 +04:00
|
|
|
bool FolderStatusDelegate::editorEvent ( QEvent * /*event*/, QAbstractItemModel * /*model*/, const QStyleOptionViewItem & /*option*/, const QModelIndex & /*index*/ )
|
2013-07-04 21:59:40 +04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Mirall
|