summaryrefslogtreecommitdiffstats
path: root/src/gvcore/dragpixmapgenerator.h
blob: d73d77623cb68853db332d1087de73872cce6bc3 (plain)
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
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
/*
Gwenview - A simple image viewer for KDE
Copyright 2000-2004 Aurélien Gâteau

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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/
#ifndef DRAGPIXMAPGENERATOR_H
#define DRAGPIXMAPGENERATOR_H

// Qt
#include <qapplication.h>
#include <qpixmap.h>
#include <qtooltip.h>
#include <qvaluelist.h>
#include <qvaluevector.h>

// KDE
#include <klocale.h>

namespace Gwenview {


template <class T>
class DragPixmapGenerator;


template <class T>
class DragPixmapItemDrawer {
public:
	DragPixmapItemDrawer()
	: mGenerator(0) {}
	virtual ~DragPixmapItemDrawer() {}
	virtual void setGenerator(DragPixmapGenerator<T>* generator) {
		mGenerator = generator;
	}
	virtual QSize itemSize(T)=0;
	virtual void drawItem(QPainter*, int left, int top, T)=0;
	virtual int spacing() const { 
		return 0;
	}

protected:
	DragPixmapGenerator<T>* mGenerator;
};


QPixmap dragPixmapGeneratorHelper(QValueVector<QPixmap> pixmapVector);


template <class T>
class DragPixmapGenerator {
public:
	/** Offset between cursor and dragged images */
	static const uint DRAG_OFFSET=16;


	/** Maximum width of an item painted by DragPixmapItemDrawer */
	static const int ITEM_MAX_WIDTH=128;

	static const int MAX_HEIGHT=200;

	static const int DRAG_MARGIN=4;


	DragPixmapGenerator()
	: mPixmapWidth(0) {}


    void addItem(const T& item) {
		mItemList << item;
	}


	int maxWidth() const {
		return ITEM_MAX_WIDTH;
	}


	/**
	 * Returns the width of the generated pixmap, not including the margin.
	 * To be used by DragPixmapItemDrawer<T>::drawItem. Should not be used
	 * anywhere else since this value is initialized in generate().
	 */
	int pixmapWidth() const {
		return mPixmapWidth;
	}

    void setItemDrawer(DragPixmapItemDrawer<T>* drawer) {
		mItemDrawer = drawer;
		drawer->setGenerator(this);
	}

    QPixmap generate() {
		int width = 0, height = 0;
		int dragCount = 0;
		int spacing = mItemDrawer->spacing();
		bool listCropped;
		QString bottomText;
		QFontMetrics fm = QApplication::fontMetrics();

		// Compute pixmap size and update dragCount
		QValueListIterator<T> it = mItemList.begin();
		QValueListIterator<T> end = mItemList.end();
		height = -spacing;
		for (; it!= end && height < MAX_HEIGHT; ++dragCount, ++it) {
			QSize itemSize = mItemDrawer->itemSize(*it);
			Q_ASSERT(itemSize.width() <= ITEM_MAX_WIDTH);
			
			width = QMAX(width, itemSize.width());
			height += itemSize.height() + spacing;
		}

		listCropped = it != end;
		if (listCropped) {
			// If list has been cropped, leave space for item count text
			height += fm.height();
			bottomText = i18n("%1 items").arg(mItemList.count());
			width = QMAX(width, fm.width("... " + bottomText));
		}
		
		mPixmapWidth = width;

		// Init pixmap
		QPixmap pixmap(width + 2*DRAG_MARGIN, height + 2*DRAG_MARGIN);
		QColorGroup cg = QToolTip::palette().active();

		pixmap.fill(cg.base());
		QPainter painter(&pixmap);
		
		// Draw border
		painter.setPen(cg.dark());
		painter.drawRect(pixmap.rect());
		
		// Draw items
		it = mItemList.begin();
		height = DRAG_MARGIN;
		for (int pos=0; pos < dragCount; ++pos, ++it) {
			mItemDrawer->drawItem(&painter, DRAG_MARGIN, height, *it);
			height += mItemDrawer->itemSize(*it).height() + spacing;
		}

		// Draw text if necessary
		if (listCropped) {
			int posY= height + fm.ascent();
			painter.drawText(DRAG_MARGIN, posY, "...");
			int offset = width - fm.width(bottomText);
			painter.drawText(DRAG_MARGIN + offset, posY, bottomText);
		}
		painter.end();

		return pixmap;
	}


private:
	QValueList<T> mItemList;
	DragPixmapItemDrawer<T>* mItemDrawer;
	int mPixmapWidth;
};


} // namespace


#endif /* DRAGPIXMAPGENERATOR_H */