summaryrefslogtreecommitdiffstats
path: root/kcoloredit/paletteviewscrolledarea.cpp
blob: a00606c898d900f99ee52538f401b2231586eb38 (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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/***************************************************************************
                         paletteviewscrolledarea.cpp
                             -------------------
    begin                : Sun Jul 17 2000
    copyright            : (C) 2000 by Artur Rataj
    email                : art@zeus.polsl.gliwice.pl
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <stdlib.h>
#include <tqptrlist.h>
#include <tqcolor.h>
#include <tqcursor.h>
#include <tqbrush.h>
#include <tqpainter.h>
#include <tqpixmap.h>
#include <tqpen.h>
#include <tqfontmetrics.h>
#include <tqtimer.h>
#include <tdeglobal.h>
#include <kcolordrag.h>
#include <tqscrollbar.h>

#include "palette.h"
#include "palettehistory.h"
#include "kcoloreditdoc.h"
#include "kcoloreditview.h"
#include "paletteviewscrolledarea.h"
#include "paletteviewscrolledarea.moc"

PaletteViewScrolledArea::PaletteViewScrolledArea(const int defaultCellWidth,
	const int defaultCellHeight, const int cellSpacing, TQScrollBar* scrollBar,
	TQScrollBar* hScrollBar, KColorEditView* view, TQWidget* parent, const char* name)
	: TQFrame(parent, name) {
	this->defaultCellWidth = defaultCellWidth;
	this->defaultCellHeight = defaultCellHeight;
	this->cellSpacing = cellSpacing;
	this->scrollBar = scrollBar;
	this->hScrollBar = hScrollBar;
	this->view = view;
	setBackgroundMode(NoBackground);
	scrollTimeoutTimer = new TQTimer(this);
	connect(scrollTimeoutTimer, TQ_SIGNAL( timeout() ), TQ_SLOT( slotScrollTimeout() ));
	scrollTimeout = true;
	mousePressed = false;
	cursorPositioning = false;
	colorChosen = false;
	setCellsSizes();
	slotCursorFollowsChosenColor(false);
	this->scrollBar->setValue(0);
	slotViewColorNames(false);
	setMinimumSize(1, 1);
}

PaletteViewScrolledArea::~PaletteViewScrolledArea() {
}

void PaletteViewScrolledArea::slotCursorFollowsChosenColor(bool follows) {
	cursorFollowsChosenColor = follows;
}

void PaletteViewScrolledArea::slotViewColorNames(bool viewColorNames) {
	this->viewColorNames = viewColorNames;
}

void PaletteViewScrolledArea::redraw() {
	//setCellsSizes();
	repaintPalette();
}

void PaletteViewScrolledArea::repaintPalette() {
	repaint(false);
}

void PaletteViewScrolledArea::checkSelectionAutoScroll(const int mousePosY) {
	if(mousePosY < 0)
		scrollPalette(-8, 50);
	else if(mousePosY > height() - 1)
		scrollPalette(8, 50);
}

void PaletteViewScrolledArea::scrollPalette(const int amount, const int timeout) {
	if(scrollTimeout) {
		scrollBy(amount);
		scrollTimeout = false;
		scrollTimeoutTimer->start(timeout, true);
	}
}

void PaletteViewScrolledArea::slotScrollTimeout() {
	scrollTimeout = true;
	if(mousePressed) {
		TQPoint cursorPoint = mapFromGlobal(TQCursor::pos());
		setCursorPos(cursorPoint.x(), cursorPoint.y());
		selectionEnd = cursorPos();
		if(selectionEnd >= selectionBegin)
			setSelection(selectionBegin, selectionEnd);
		else
			setSelection(selectionEnd, selectionBegin);
		checkSelectionAutoScroll(cursorPoint.y());
		repaintPalette();
	}
}

void PaletteViewScrolledArea::setCursorPos(const int pos) {
	int oldCursorPos = document()->paletteCursorPos();
	document()->setPaletteCursorPos(pos);
	if(pos != oldCursorPos)
		view->slotCursorPosChanged(pos);
}

bool PaletteViewScrolledArea::setCursorPos(const int x, const int y) {
	int shiftedY = y + scrollBar->value();
	int shiftedX = x + hScrollBar->value();
	int cursorColumn = (shiftedX + rowWidth/cellsInRow/2)*cellsInRow/rowWidth;
	int cursorRow;
	if(shiftedY >= 0)
		cursorRow = (shiftedY + cellSpacing)/rowHeight;
	else
		cursorRow = -1;
	bool atCursorLocation = abs(cursorColumn*rowWidth/cellsInRow - shiftedX) < cellSpacing + 1;
	if(!atCursorLocation) {
		atCursorLocation = abs(cursorRow*rowHeight - shiftedY) < cellSpacing + 1;
		if(atCursorLocation)
			cursorColumn = 0;
	}
	if(atCursorLocation || cursorPositioning) {
		if(cursorColumn > cellsInRow - 1) {
			cursorColumn = 0;
			++cursorRow;
		}
		int tmpCursorPos = cursorRow*cellsInRow + cursorColumn;
		if(tmpCursorPos < 0)
			tmpCursorPos = 0;
		else if(tmpCursorPos >= palette()->length()) {
			if(tmpCursorPos == palette()->length() &&
				cursorColumn == 0 &&
				selectionMin() == selectionMax())
				/* if cursor has been set after the last color and the color is
				 * also the last in a row, and there is no selection, scroll the
				 * palette to ensure the cursor is visible
				 */
				scrollBy(rowHeight*2);
			if(tmpCursorPos > palette()->length())
				tmpCursorPos = palette()->length();
		}
		setCursorPos(tmpCursorPos);
	}
	return atCursorLocation;
}

void PaletteViewScrolledArea::setSelection(const int min, const int max) {
	document()->setPaletteSelection(min, max);
}

void PaletteViewScrolledArea::setCellsSizes() {
	rowWidth = width();
	cellWidth = defaultCellWidth;
	cellHeight = defaultCellHeight;
	if(viewColorNames) {
		TQPainter painter;
		painter.begin(this);
		if(cellHeight < painter.fontMetrics().height()) {
			int prevCellHeight = cellHeight;
			cellHeight = painter.fontMetrics().height();
			cellWidth = cellHeight*cellWidth/prevCellHeight;
		}
		painter.end();
	}
	if(viewColorNames)
		cellsInRow = 1;
	else
		cellsInRow = rowWidth/(cellWidth + cellSpacing*2);
	if(viewColorNames)
		rowHeight = cellHeight + cellSpacing*2;
	else
		rowHeight = (int)(rowWidth*1.0/cellsInRow/
			( cellWidth + cellSpacing*2 )*( cellHeight + cellSpacing*2 ) + 0.5);
	cellHeight = rowHeight - cellSpacing*2;
	rowsNum = (palette()->length() + cellsInRow - 1)/cellsInRow;
	cellTableHeight = rowsNum*rowHeight;
	int contentsHeight;
	if(palette()->length() != 0 &&
		( palette()->length()%cellsInRow ) == 0)
		contentsHeight = cellTableHeight + rowHeight;
	else
		contentsHeight = cellTableHeight;
	int scrollBarRange = contentsHeight - 1 - height();
	if(scrollBarRange < 0)
		scrollBarRange = 0;
	scrollBar->setRange(0, scrollBarRange);
	scrollBar->setSteps(rowHeight, height());
}

Palette* PaletteViewScrolledArea::palette() const {
	return document()->paletteHistory()->editableStream();
}

int PaletteViewScrolledArea::cursorPos() const {
	return document()->paletteCursorPos();
}

int PaletteViewScrolledArea::selectionMin() const{
	return document()->paletteSelectionBegin();
}

int PaletteViewScrolledArea::selectionMax() const {
	return document()->paletteSelectionEnd();
}

void PaletteViewScrolledArea::paintEvent(TQPaintEvent* /*event*/) {
	setCellsSizes();
	TQPixmap pixmap(size());
	TQPainter painter;
	painter.begin(&pixmap, this);
	TQFontMetrics fontMetrics = painter.fontMetrics();
	int maxLineWidth;
	if(viewColorNames) {
		int maxTextLength = 0;
		for(int index = 0; index < palette()->length(); ++index) {
			int currTextLength = fontMetrics.width(
				palette()->color(index)->name());
			if(currTextLength > maxTextLength)
				maxTextLength = currTextLength;
		}
		maxLineWidth = cellWidth + cellSpacing*2 +
			cellSpacing*3 + maxTextLength + 1;
	} else
		maxLineWidth = rowWidth;
	int width = rowWidth;
	if(maxLineWidth > width) {
		hScrollBar->setRange(0, maxLineWidth - width);
		hScrollBar->setSteps(8, width);
		hScrollBar->show();
	} else {
		hScrollBar->setValue(0);
		hScrollBar->hide();
	}
	int posY = scrollBar->value();
	int firstRow = posY/rowHeight;
	int lastRow = (posY + height() - 1 + rowHeight - 1)/rowHeight;
	if(viewColorNames)
		painter.fillRect(0, 0, rowWidth, height(),
        TQBrush( TQFrame::palette().active().base() ));
	TQBrush normalBackgroundBrush(TQFrame::palette().active().background());
	TQBrush selectedBackgroundBrush(TQFrame::palette().active().highlight());
	TQBrush foregroundBrush;
	TQBrush cursorBrush(TQFrame::palette().active().foreground());
	TQPen backgroundPen(TQFrame::palette().active().foreground());
	int min = selectionMin();
	int max = selectionMax();
	int fontAscent = fontMetrics.ascent();
	int xBegin = -hScrollBar->value();
	for(int x = 0; x < cellsInRow; ++x) {
		int xEnd = -hScrollBar->value();
		if(viewColorNames)
			xEnd += cellWidth + cellSpacing*2;
		else
			xEnd += (x + 1)*(width - 1)/cellsInRow;
		int cellWithSpacingWidth = xEnd - xBegin + 1;
		int cellWidth = cellWithSpacingWidth - 2*cellSpacing;
		for(int y = firstRow; y <= lastRow; ++y) {
			int yBegin = y*rowHeight - posY;
			int currCellNum = y*cellsInRow + x;
			TQBrush* backgroundBrush;
			if(currCellNum >= min && currCellNum < max)
				backgroundBrush = &selectedBackgroundBrush;
			else
				backgroundBrush = &normalBackgroundBrush;
			if(currCellNum < palette()->length()) {
				Color* color = palette()->color(currCellNum);
				TQBrush foregroundBrush(TQColor(
					color->component(Color::RED_INDEX),
					color->component(Color::GREEN_INDEX),
					color->component(Color::BLUE_INDEX) ));
				painter.fillRect(xBegin, yBegin, cellWithSpacingWidth, cellSpacing,
					*backgroundBrush);
				painter.fillRect(xBegin, yBegin + rowHeight - cellSpacing, cellWithSpacingWidth, cellSpacing,
					*backgroundBrush);
				TQBrush* backgroundOrCursorBrush;
				if(cursorPos() == currCellNum)
					backgroundOrCursorBrush = &cursorBrush;
				else
					backgroundOrCursorBrush = backgroundBrush;
				painter.fillRect(xBegin, yBegin + cellSpacing, cellSpacing, cellHeight,
					*backgroundOrCursorBrush);
				painter.fillRect(xBegin + cellWithSpacingWidth - cellSpacing, yBegin + cellSpacing,
					cellSpacing, cellHeight,
					*backgroundBrush);
				painter.fillRect(xBegin + cellSpacing, yBegin + cellSpacing, cellWidth, cellHeight,
					foregroundBrush);
				if(viewColorNames) {
					painter.setPen(backgroundPen);
					painter.drawText(xBegin + cellWithSpacingWidth + cellSpacing*3,
						yBegin + rowHeight/2 + fontAscent/2, color->name());
				}
			} else {
				if(cursorPos() == currCellNum) {
					painter.fillRect(xBegin, yBegin + cellSpacing, cellSpacing, cellHeight,
						cursorBrush);
					painter.fillRect(xBegin, yBegin, cellSpacing, cellSpacing,
						*backgroundBrush);
					painter.fillRect(xBegin, yBegin + rowHeight - cellSpacing, cellSpacing, cellSpacing,
						*backgroundBrush);
					painter.fillRect(xBegin + cellSpacing, yBegin, cellWithSpacingWidth - cellSpacing, rowHeight,
						*backgroundBrush);
				} else
					painter.fillRect(xBegin, yBegin, cellWithSpacingWidth, rowHeight,
						*backgroundBrush);
			}
		}
		xBegin = xEnd + 1;
	}
	painter.end();
	painter.begin(this);
	painter.drawPixmap(0, 0, pixmap);
	painter.end();
}

int PaletteViewScrolledArea::colorIndex(const TQPoint& point) const {
	int colorColumn = point.x()*cellsInRow/rowWidth;
	int colorRow = (point.y() + scrollBar->value())/rowHeight;
	int colorIndex = colorRow*cellsInRow + colorColumn;
	if(colorIndex > palette()->length() - 1 ||
		colorIndex < 0)
		colorIndex = -1;
	return colorIndex;
}

TQColor PaletteViewScrolledArea::color(const TQPoint& point) const {
	Color* color = palette()->color(colorIndex( point ));
	return TQColor(color->component( Color::RED_INDEX ),
		color->component( Color::GREEN_INDEX ),
		color->component( Color::BLUE_INDEX ));
}

void PaletteViewScrolledArea::mousePressEvent(TQMouseEvent* event) {
	cursorPositioning = false;
	if(( cursorPositioning = setCursorPos(event->x(), event->y()) )) {
		selectionBegin = cursorPos();
		setSelection(selectionBegin, selectionBegin);
		redraw();
		colorChosen = false;
	} else {
		colorDragPoint = event->pos();
		colorChosen = true;
	}
	mousePressed = true;
}

void PaletteViewScrolledArea::mouseMoveEvent(TQMouseEvent* event) {
	if(cursorPositioning) {
		setCursorPos(event->x(), event->y());
		selectionEnd = cursorPos();
		if(selectionEnd >= selectionBegin)
			setSelection(selectionBegin, selectionEnd);
		else
			setSelection(selectionEnd, selectionBegin);
		checkSelectionAutoScroll(event->y());
		redraw();
	} else {
		/* check if it is a color drag */
		if(colorIndex( colorDragPoint ) != -1) {
			if(abs( event->x() - colorDragPoint.x() ) > 2 ||
				abs( event->y() - colorDragPoint.y() ) > 2) {
				TQColor draggedColor = color(colorDragPoint);
				KColorDrag* colorDrag = KColorDrag::makeDrag(draggedColor, this);
				colorDrag->dragCopy();
			} else
				colorChosen = true;
		}
	}
}

void PaletteViewScrolledArea::mouseReleaseEvent(TQMouseEvent* /*event*/) {
	if(colorChosen) {
		if(colorIndex( colorDragPoint ) != -1) {
			int index = colorIndex(colorDragPoint);
			chooseColor(palette()->color( index ));
			if(cursorFollowsChosenColor) {
				setCursorPos(index);
				setSelection(cursorPos(), cursorPos());
				redraw();
			}
		}
		colorChosen = false;
	}
	mousePressed = false;
}

void PaletteViewScrolledArea::chooseColor(Color* const color) {
	view->chooseColor(color);
}

KColorEditDoc* PaletteViewScrolledArea::document() const {
	return view->document();
}

void PaletteViewScrolledArea::scrollBy(const int y) {
	scrollBar->setValue(scrollBar->value() + y);
}