summaryrefslogtreecommitdiffstats
path: root/src/gvcore/imageviewtools.cpp
blob: 789ca5546f029d9867b056e55ec6076086ea8e9a (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
// vim: set tabstop=4 shiftwidth=4 noexpandtab
/*
Gwenview - A simple image viewer for TDE
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.

*/
// Our header
#include "imageviewtools.h"

// KDE 
#include <kaction.h>
#include <kdebug.h>
#include <klocale.h>
#include <kstandarddirs.h>

// Local
#include "imageviewconfig.h"

namespace Gwenview {


// Helper function
static TQCursor loadCursor(const TQString& name) {
	TQString path;
	path=locate("data", TQString("gwenview/cursors/%1.png").arg(name));
	return TQCursor(TQPixmap(path));
}


//----------------------------------------------------------------------------
//
// ImageView::ToolBase
//
//----------------------------------------------------------------------------
ImageView::ToolBase::ToolBase(ImageView* view)
: mView(view) {}


ImageView::ToolBase::~ToolBase() {}

void ImageView::ToolBase::mouseMoveEvent(TQMouseEvent*) {}
void ImageView::ToolBase::leftButtonPressEvent(TQMouseEvent*) {}
void ImageView::ToolBase::leftButtonReleaseEvent(TQMouseEvent*) {}

void ImageView::ToolBase::midButtonReleaseEvent(TQMouseEvent*) {
	mView->zoomToFit()->activate();
}

void ImageView::ToolBase::rightButtonPressEvent(TQMouseEvent* event) {
	emit mView->requestContextMenu(event->globalPos());
}

void ImageView::ToolBase::rightButtonReleaseEvent(TQMouseEvent*) {
}

void ImageView::ToolBase::wheelEvent(TQWheelEvent* event) {
	event->accept();
}

void ImageView::ToolBase::updateCursor() {
	mView->viewport()->setCursor(ArrowCursor);
}


//----------------------------------------------------------------------------
//
// ImageView::ZoomTool
//
//----------------------------------------------------------------------------
ImageView::ZoomTool::ZoomTool(ImageView* view)
: ImageView::ToolBase(view) {
	mZoomCursor=loadCursor("zoom");
}


void ImageView::ZoomTool::zoomTo(const TQPoint& pos, bool in) {
	if (!mView->canZoom(in)) return;

	TQPoint centerPos=TQPoint(mView->visibleWidth(), mView->visibleHeight())/2;
	// Compute image position
	TQPoint imgPos=mView->viewportToContents(pos) - mView->offset();
	double newZoom=mView->computeZoom(in);

	imgPos*=newZoom/mView->zoom();
	imgPos=imgPos-pos+centerPos;
	mView->setZoom(newZoom, imgPos.x(), imgPos.y());
}


void ImageView::ZoomTool::leftButtonReleaseEvent(TQMouseEvent* event) {
	zoomTo(event->pos(), true);
}


void ImageView::ZoomTool::wheelEvent(TQWheelEvent* event) {
	zoomTo(event->pos(), event->delta()>0);
	event->accept();
}


void ImageView::ZoomTool::rightButtonPressEvent(TQMouseEvent*) {
}


void ImageView::ZoomTool::rightButtonReleaseEvent(TQMouseEvent* event) {
	zoomTo(event->pos(), false);
}


void ImageView::ZoomTool::updateCursor() {
	mView->viewport()->setCursor(mZoomCursor);
}


TQString ImageView::ZoomTool::hint() const {
    return i18n("Left click to zoom in, right click to zoom out. You can also use the mouse wheel.");
}


//----------------------------------------------------------------------------
//
// ImageView::ScrollTool
//
//----------------------------------------------------------------------------
ImageView::ScrollTool::ScrollTool(ImageView* view)
: ImageView::ToolBase(view)
, mScrollStartX(0), mScrollStartY(0)
, mDragStarted(false) {
}


void ImageView::ScrollTool::leftButtonPressEvent(TQMouseEvent* event) {
	mScrollStartX=event->x();
	mScrollStartY=event->y();
	mView->viewport()->setCursor(SizeAllCursor);
	mDragStarted=true;
}


void ImageView::ScrollTool::mouseMoveEvent(TQMouseEvent* event) {
	if (!mDragStarted) return;

	int deltaX,deltaY;

	deltaX=mScrollStartX - event->x();
	deltaY=mScrollStartY - event->y();

	mScrollStartX=event->x();
	mScrollStartY=event->y();
	mView->scrollBy(deltaX,deltaY);
}


void ImageView::ScrollTool::leftButtonReleaseEvent(TQMouseEvent*) {
	if (!mDragStarted) return;

	mDragStarted=false;
	mView->viewport()->setCursor(ArrowCursor);
}


void ImageView::ScrollTool::wheelEvent(TQWheelEvent* event) {
	if (ImageViewConfig::mouseWheelScroll()) {
		int deltaX, deltaY;

		if (event->state() & AltButton || event->orientation()==Horizontal) {
			deltaX = event->delta();
			deltaY = 0;
		} else {
			deltaX = 0;
			deltaY = event->delta();
		}
		mView->scrollBy(-deltaX, -deltaY);
	} else {
		if (event->delta()<0) {
			mView->emitSelectNext();
		} else {
			mView->emitSelectPrevious();
		}
	}
	event->accept();
}


void ImageView::ScrollTool::updateCursor() {
	if (mDragStarted) {
		mView->viewport()->setCursor(SizeAllCursor);
	} else {
		mView->viewport()->setCursor(ArrowCursor);
	}
}


TQString ImageView::ScrollTool::hint() const {
    return i18n("Drag to move the image, middle-click to toggle auto-zoom. Hold the Control key to switch to the zoom tool.");
}


} // namespace