driveselectiondelegate.cpp 7.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/***************************************************************************
 *   Copyright Simon Persson                                               *
 *   simonpersson1@gmail.com                                               *
 *                                                                         *
 *   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.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
20 21 22

#include "backupplan.h"
#include "driveselectiondelegate.h"
23
#include "driveselection.h"
24 25 26 27 28 29

#include <QApplication>
#include <QPainter>
#include <QStyle>

#include <kcapacitybar.h>
30 31
#include <KIcon>
#include <KIconLoader>
32
#include <KLocale>
33
#include <kio/global.h>
34 35 36

static const int cMargin = 6;

37 38
DriveSelectionDelegate::DriveSelectionDelegate(QListView *pParent)
   : QStyledItemDelegate(pParent), mListView(pParent)
39 40 41
{
	mCapacityBar = new KCapacityBar(KCapacityBar::DrawTextInline);
}
42

43 44 45 46
void DriveSelectionDelegate::paint(QPainter* pPainter, const QStyleOptionViewItem& pOption,
                                   const QModelIndex& pIndex) const {
	pPainter->save();
	pPainter->setRenderHint(QPainter::Antialiasing);
47

48
	QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &pOption, pPainter);
49

50 51
	KIO::filesize_t lTotalSize = pIndex.data(DriveSelection::TotalSpace).value<KIO::filesize_t>();
	KIO::filesize_t lUsedSize = pIndex.data(DriveSelection::UsedSpace).value<KIO::filesize_t>();
52

53
	bool lIsDisconnected = pIndex.data(DriveSelection::UDI).toString().isEmpty();
54

55 56 57 58 59 60
	if(lTotalSize == 0 || lIsDisconnected) {
		mCapacityBar->setValue(0);
	} else {
		mCapacityBar->setValue((lUsedSize * 100) / lTotalSize);
	}
	mCapacityBar->drawCapacityBar(pPainter, pOption.rect.adjusted(cMargin,
61
	                                                              cMargin+QApplication::fontMetrics().height()+cMargin,
62 63 64
	                                                              -cMargin,
	                                                              4*cMargin + QApplication::fontMetrics().height() -
	                                                              pOption.rect.height()));
65 66 67 68 69 70 71 72 73 74 75

	if (pOption.state & QStyle::State_Selected)
		pPainter->setPen(pOption.palette.color(QPalette::HighlightedText));
	else
		pPainter->setPen(pOption.palette.color(QPalette::Text));

	KLocale *lLocale = KGlobal::locale();
	QString lDisplayLabel, lPartitionLabel, lDisconnectedLabel;
	if(lIsDisconnected) {
		lDisconnectedLabel = i18nc("@item:inlistbox this text is added if selected drive is disconnected", " (disconnected)");
	} else {
76
		lDisconnectedLabel = QString();
77
		QString lFreeSpace = i18nc("@label %1 is amount of free storage space of hard drive","%1 free",
78
		                           lLocale->formatByteSize(lTotalSize - lUsedSize, 1));
79
		pPainter->drawText(pOption.rect.topRight() + QPoint(-(cMargin+QApplication::fontMetrics().width(lFreeSpace)),
80
		                                                    cMargin+QApplication::fontMetrics().height()), lFreeSpace);
81 82 83 84 85 86 87 88 89 90 91 92
	}

	QString lDeviceDescription = pIndex.data(DriveSelection::DeviceDescription).toString();
	QString lLabel = pIndex.data(DriveSelection::Label).toString();
	int lPartitionNumber = pIndex.data(DriveSelection::PartitionNumber).toInt();
	if(lLabel.isEmpty() || lLabel == lDeviceDescription) {
		if(pIndex.data(DriveSelection::PartitionsOnDrive).toInt() > 1) {
			lPartitionLabel = i18nc("@item:inlistbox used for unnamed filesystems, more than one filesystem on device. %1 is partition number, %2 is device description, %3 is either empty or the \" (disconnected)\" text",
			                        "Partition %1 on %2%3", lPartitionNumber, lDeviceDescription, lDisconnectedLabel);
		} else {
			lPartitionLabel = i18nc("@item:inlistbox used when there is only one unnamed filesystem on device. %1 is device description, %2 is either empty or the \" (disconnected)\" text",
			                        "%1%2", lDeviceDescription, lDisconnectedLabel);
93
		}
94 95 96
	} else {
		lPartitionLabel = i18nc("@item:inlistbox %1 is filesystem label, %2 is the device description, %3 is either empty or the \" (disconnected)\" text",
		                        "%1 on %2%3", lLabel, lDeviceDescription, lDisconnectedLabel);
97
	}
98 99 100 101 102

	if(lTotalSize == 0) {
		lDisplayLabel = lPartitionLabel;
	} else {
		lDisplayLabel = i18nc("@item:inlistbox %1 is drive(partition) label, %2 is storage capacity",
103
		                      "%1: %2 total capacity", lPartitionLabel, lLocale->formatByteSize(lTotalSize, 1));
104
	}
105
	pPainter->drawText(pOption.rect.topLeft() + QPoint(cMargin, cMargin+QApplication::fontMetrics().height()), lDisplayLabel);
106

107 108 109 110 111 112 113 114
	int lIconSize = KIconLoader::SizeLarge;
	QRect lWarningRect = warningRect(pOption.rect.adjusted(lIconSize + cMargin, 0, 0, 0), pIndex);
	if(!lWarningRect.isEmpty()) {
		KIcon lIcon(QLatin1String("dialog-warning"));
		lIcon.paint(pPainter, lWarningRect.left() - cMargin - lIconSize, lWarningRect.top(), lIconSize, lIconSize);
		pPainter->drawText(lWarningRect, Qt::AlignVCenter | Qt::TextWordWrap, warningText(pIndex));
	}

115
	pPainter->restore();
116 117
}

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
QSize DriveSelectionDelegate::sizeHint(const QStyleOptionViewItem& pOption, const QModelIndex& pIndex) const {
	Q_UNUSED(pOption)
	QSize lSize;
	lSize.setWidth(cMargin*2 + QApplication::fontMetrics().width(pIndex.data().toString()));
	lSize.setHeight(cMargin*5 + QApplication::fontMetrics().height());
	QRect lWarningRect = warningRect(mListView->rect().adjusted(KIconLoader::SizeLarge + cMargin, 0, 0, 0), pIndex);
	if(!lWarningRect.isEmpty()) {
		lSize.setHeight(lSize.height() + 2*cMargin + lWarningRect.height());
	}
	return lSize;
}

QRect DriveSelectionDelegate::warningRect(const QRect &pRect, const QModelIndex &pIndex) const {
	QRect lTextLocation = pRect.adjusted(cMargin, 5*cMargin + QApplication::fontMetrics().height(), -cMargin, -cMargin);
	QString lWarningText = warningText(pIndex);
	if(lWarningText.isEmpty()) {
		return QRect();
	}
	QRect lTextBoundary = QApplication::fontMetrics().boundingRect(lTextLocation, Qt::TextWordWrap, lWarningText);
	if(lTextBoundary.height() < KIconLoader::SizeLarge) {
		lTextBoundary.setHeight(KIconLoader::SizeLarge);
	}
	return lTextBoundary;
}

QString DriveSelectionDelegate::warningText(const QModelIndex &pIndex) const {
	bool lPermissionWarning = pIndex.data(DriveSelection::PermissionLossWarning).toBool();
	bool lSymlinkWarning = pIndex.data(DriveSelection::SymlinkLossWarning).toBool();
	if(lPermissionWarning && lSymlinkWarning) {
		return i18nc("@item:inlistbox", "Warning: Symbolic links and file permissions can not be saved "
		             "to this file system. File permissions only matters if there is more than one "
		             "user of this computer or if you are backing up executable program files.");
	} else if(lPermissionWarning) {
		return i18nc("@item:inlistbox", "Warning: File permissions can not be saved to this file "
		             "system. File permissions only matters if there is more than one "
		             "user of this computer or if you are backing up executable program files.");
	}
	return QString();
156
}