File: versionlistdelegate.cpp

package info (click to toggle)
kup-backup 0.7.1+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,480 kB
  • sloc: cpp: 7,263; sh: 51; makefile: 6
file content (302 lines) | stat: -rw-r--r-- 11,817 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/***************************************************************************
 *   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.             *
 ***************************************************************************/

#include "versionlistdelegate.h"
#include "versionlistmodel.h"

#include <KFormat>
#include <KLocalizedString>

#include <QAbstractItemView>
#include <QAbstractItemModel>
#include <QApplication>
#include <QMouseEvent>
#include <QParallelAnimationGroup>
#include <QPainter>
#include <QGraphicsOpacityEffect>
#include <QPropertyAnimation>
#include <QPushButton>

#define cMargin 4

Button::Button(QString pText, QWidget *pParent)
   : QObject() // don't make it owned by pParent
{
	mStyleOption.initFrom(pParent);
	mStyleOption.features = QStyleOptionButton::None;
	mStyleOption.state = QStyle::State_Enabled;
	mStyleOption.text = pText;

	const QSize lContentsSize = mStyleOption.fontMetrics.size(Qt::TextSingleLine, mStyleOption.text);
	mStyleOption.rect = QRect(QPoint(0, 0),
	                          QApplication::style()->sizeFromContents(QStyle::CT_PushButton,
	                                                                  &mStyleOption, lContentsSize));
	mPushed = false;
	mParent = pParent;
}

void Button::setPosition(const QPoint &pTopRight) {
	mStyleOption.rect.moveTopRight(pTopRight);
}

void Button::paint(QPainter *pPainter, float pOpacity) {
	pPainter->setOpacity(pOpacity);
	QApplication::style()->drawControl(QStyle::CE_PushButton, &mStyleOption, pPainter);
}

bool Button::event(QEvent *pEvent) {
	QMouseEvent *lMouseEvent = static_cast<QMouseEvent *>(pEvent);
	bool lActivated = false;
	switch(pEvent->type()) {
		case QEvent::MouseMove:
			if(mStyleOption.rect.contains(lMouseEvent->pos())) {
				if(!(mStyleOption.state & QStyle::State_MouseOver)) {
					mStyleOption.state |= QStyle::State_MouseOver;
					if(mPushed) {
						mStyleOption.state |= QStyle::State_Sunken;
						mStyleOption.state &= ~QStyle::State_Raised;
					}
					mParent->update(mStyleOption.rect);
				}
			} else {
				if(mStyleOption.state & QStyle::State_MouseOver) {
					mStyleOption.state &= ~QStyle::State_MouseOver;
					if(mPushed) {
						mStyleOption.state &= ~QStyle::State_Sunken;
						mStyleOption.state |= QStyle::State_Raised;
					}
					mParent->update(mStyleOption.rect);
				}
			}
		break;
	case QEvent::MouseButtonPress:
		if(lMouseEvent->button() == Qt::LeftButton && !mPushed &&
		      (mStyleOption.state & QStyle::State_MouseOver)) {
			mPushed = true;
			mStyleOption.state |= QStyle::State_Sunken;
			mStyleOption.state &= ~QStyle::State_Raised;
			mParent->update(mStyleOption.rect);
		}
		break;
	case QEvent::MouseButtonRelease:
		if(lMouseEvent->button() == Qt::LeftButton) {
			if(mPushed && (mStyleOption.state & QStyle::State_MouseOver)) {
				lActivated = true;
			}
			mPushed = false;
			mStyleOption.state &= ~QStyle::State_Sunken;
			mStyleOption.state |= QStyle::State_Raised;
			mParent->update(mStyleOption.rect);
		}
		break;
	case QEvent::KeyPress: {
		QKeyEvent *lKeyEvent = static_cast<QKeyEvent *>(pEvent);
		if((mStyleOption.state & QStyle::State_HasFocus)) {
			if(lKeyEvent->key() == Qt::Key_Left || lKeyEvent->key() == Qt::Key_Right) {
				mStyleOption.state &= ~QStyle::State_HasFocus;
				emit focusChangeRequested(lKeyEvent->key() == Qt::Key_Right);
				mParent->update(mStyleOption.rect);
			} else if(lKeyEvent->key() == Qt::Key_Space || lKeyEvent->key() == Qt::Key_Return
			          || lKeyEvent->key() == Qt::Key_Enter) {
				lActivated = true;
			}
		}
	}
	default:
		break;
	}
	return lActivated;
}


VersionItemAnimation::VersionItemAnimation(QWidget *pParent)
   : QParallelAnimationGroup(pParent)
{
	mParent = pParent;
	mExtraHeight = 0;
	mOpacity = 0;
	mOpenButton = new Button(xi18nc("@action:button", "Open"), pParent);
	connect(mOpenButton, SIGNAL(focusChangeRequested(bool)), SLOT(changeFocus(bool)), Qt::QueuedConnection);
	mRestoreButton = new Button(xi18nc("@action:button", "Restore"), pParent);
	connect(mRestoreButton, SIGNAL(focusChangeRequested(bool)), SLOT(changeFocus(bool)), Qt::QueuedConnection);
	QPropertyAnimation *lHeightAnimation = new QPropertyAnimation(this, "extraHeight", this);
	lHeightAnimation->setStartValue(0.0);
	lHeightAnimation->setEndValue(1.0);
	lHeightAnimation->setDuration(300);
	lHeightAnimation->setEasingCurve(QEasingCurve::InOutBack);
	addAnimation(lHeightAnimation);

	QPropertyAnimation *lWidgetOpacityAnimation = new QPropertyAnimation(this, "opacity", this);
	lWidgetOpacityAnimation->setStartValue(0.0);
	lWidgetOpacityAnimation->setEndValue(1.0);
	lWidgetOpacityAnimation->setDuration(300);
	addAnimation(lWidgetOpacityAnimation);
}

void VersionItemAnimation::setExtraHeight(float pExtraHeight) {
	mExtraHeight = pExtraHeight;
	emit sizeChanged(mIndex);
}

void VersionItemAnimation::changeFocus(bool pForward) {
	Q_UNUSED(pForward)
	if(sender() == mOpenButton) {
		mRestoreButton->mStyleOption.state |= QStyle::State_HasFocus;
		mParent->update(mRestoreButton->mStyleOption.rect);
	} else if(sender() == mRestoreButton) {
		mOpenButton->mStyleOption.state |= QStyle::State_HasFocus;
		mParent->update(mOpenButton->mStyleOption.rect);
	}
}

void VersionItemAnimation::setFocus(bool pFocused) {
	if(!pFocused) {
		mOpenButton->mStyleOption.state &= ~QStyle::State_HasFocus;
		mRestoreButton->mStyleOption.state &= ~QStyle::State_HasFocus;
	} else {
		mOpenButton->mStyleOption.state |= QStyle::State_HasFocus;
		mRestoreButton->mStyleOption.state &= ~QStyle::State_HasFocus;
	}
	mParent->update(mOpenButton->mStyleOption.rect);
	mParent->update(mRestoreButton->mStyleOption.rect);
}

VersionListDelegate::VersionListDelegate(QAbstractItemView *pItemView, QObject *pParent) :
   QAbstractItemDelegate(pParent)
{
	mView = pItemView;
	mModel = pItemView->model();
	connect(pItemView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
	        SLOT(updateCurrent(QModelIndex,QModelIndex)));
	connect(pItemView->model(), SIGNAL(modelReset()), SLOT(reset()));
	pItemView->viewport()->installEventFilter(this); //mouse events
	pItemView->installEventFilter(this); //keyboard events
	pItemView->viewport()->setMouseTracking(true);
}

VersionListDelegate::~VersionListDelegate() {
}

void VersionListDelegate::paint(QPainter *pPainter, const QStyleOptionViewItem &pOption, const QModelIndex &pIndex) const {
	QStyle * lStyle = QApplication::style();
	lStyle->drawPrimitive(QStyle::PE_PanelItemViewItem, &pOption, pPainter);
	pPainter->save();
	pPainter->setPen(pOption.palette.color(pOption.state & QStyle::State_Selected
	                                       ? QPalette::HighlightedText: QPalette::Text));
	QRect lMarginRect = pOption.rect.adjusted(cMargin, cMargin, -cMargin, -cMargin);

	QRect lSizeDisplayBounds;
	if(!pIndex.data(VersionIsDirectoryRole).toBool()) {
		QString lSizeText = KFormat().formatByteSize((double)pIndex.data(VersionSizeRole).toULongLong());
		pPainter->drawText(lMarginRect, Qt::AlignRight | Qt::AlignTop, lSizeText, &lSizeDisplayBounds);
	}
	QString lDateText = pOption.fontMetrics.elidedText(pIndex.data().toString(), Qt::ElideRight,
	                                                   lMarginRect.width() - lSizeDisplayBounds.width());
	pPainter->drawText(lMarginRect, Qt::AlignLeft | Qt::AlignTop, lDateText);
	pPainter->restore();

	VersionItemAnimation *lAnimation = mActiveAnimations.value(pIndex);
	if(lAnimation != NULL) {
		pPainter->save();
		pPainter->setClipRect(pOption.rect);

		lAnimation->mRestoreButton->setPosition(pOption.rect.topRight() +
		                                       QPoint(-cMargin,
		                                              pOption.fontMetrics.height() + 2*cMargin));
		lAnimation->mRestoreButton->paint(pPainter, lAnimation->opacity());

		lAnimation->mOpenButton->setPosition(lAnimation->mRestoreButton->mStyleOption.rect.topLeft() +
		                                    QPoint(-cMargin , 0));
		lAnimation->mOpenButton->paint(pPainter, lAnimation->opacity());
		pPainter->restore();
	}
}

QSize VersionListDelegate::sizeHint(const QStyleOptionViewItem &pOption, const QModelIndex &pIndex) const {
	int lExtraHeight = 0;
	int lExtraWidth = 0;
	VersionItemAnimation *lAnimation = mActiveAnimations.value(pIndex);
	if(lAnimation != NULL) {
		int lButtonHeight = lAnimation->mOpenButton->mStyleOption.rect.height();
		lExtraHeight = lAnimation->extraHeight() * (lButtonHeight + cMargin);
		lExtraWidth = lAnimation->mOpenButton->mStyleOption.rect.width() +
		              lAnimation->mRestoreButton->mStyleOption.rect.width();
	}
	return QSize(lExtraWidth, cMargin*2 + pOption.fontMetrics.height() + lExtraHeight);
}

bool VersionListDelegate::eventFilter(QObject *pObject, QEvent *pEvent) {
	foreach (VersionItemAnimation *lAnimation, mActiveAnimations) {
		if(lAnimation->mOpenButton->event(pEvent)) {
			emit openRequested(lAnimation->mIndex);
		}
		if(lAnimation->mRestoreButton->event(pEvent)) {
			emit restoreRequested(lAnimation->mIndex);
		}
	}
	return QAbstractItemDelegate::eventFilter(pObject, pEvent);
}


void VersionListDelegate::updateCurrent(const QModelIndex &pCurrent, const QModelIndex &pPrevious) {
	if(pPrevious.isValid()) {
		VersionItemAnimation *lPrevAnim = mActiveAnimations.value(pPrevious);
		if(lPrevAnim != NULL) {
			lPrevAnim->setDirection(QAbstractAnimation::Backward);
			lPrevAnim->start();
			lPrevAnim->setFocus(false);
		}
	}
	if(pCurrent.isValid()) {
		VersionItemAnimation *lCurAnim = mActiveAnimations.value(pCurrent);
		if(lCurAnim == NULL) {
			if(!mInactiveAnimations.isEmpty()) {
				lCurAnim = mInactiveAnimations.takeFirst();
			} else {
				lCurAnim = new VersionItemAnimation(mView->viewport());
				connect(lCurAnim, SIGNAL(sizeChanged(QModelIndex)), SIGNAL(sizeHintChanged(QModelIndex)));
				connect(lCurAnim, SIGNAL(finished()), SLOT(reclaimAnimation()));
			}
			lCurAnim->mIndex = pCurrent;
			mActiveAnimations.insert(pCurrent, lCurAnim);
		}
		lCurAnim->setDirection(QAbstractAnimation::Forward);
		lCurAnim->start();
		lCurAnim->setFocus(true);
	}
}

void VersionListDelegate::reset() {
	mInactiveAnimations.append(mActiveAnimations.values());
	mActiveAnimations.clear();
}

void VersionListDelegate::reclaimAnimation() {
	VersionItemAnimation *lAnimation = static_cast<VersionItemAnimation *>(sender());
	if(lAnimation->direction() == QAbstractAnimation::Backward) {
		mInactiveAnimations.append(lAnimation);
		foreach(VersionItemAnimation *lActiveAnimation, mActiveAnimations) {
			if(lActiveAnimation == lAnimation) {
				mActiveAnimations.remove(lAnimation->mIndex);
				break;
			}
		}
	}
}