summaryrefslogtreecommitdiffstats
path: root/src/indexwidget.cpp
blob: 023867ead38d4180df1767cbed8ae4a412839633 (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
/***************************************************************************
 *                                                                         *
 *   Copyright (C) 2005, 2006 by Kevin Gilbert                             *
 *   [email protected]                                                *
 *                                                                         *
 *   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 <ntqdragobject.h>
#include <ntqfontmetrics.h>
#include <ntqpainter.h>
#include <ntqpopupmenu.h>
#include <ntqtimer.h>

#include <kapplication.h>
#include <tdeconfig.h>
#include <kdebug.h>
#include <tdeglobal.h>
#include <kglobalsettings.h>
#include <kiconloader.h>
#include <tdelocale.h>

#include "global.h"
#include "indexwidget.h"
#include "mainwidget.h"
#include "scanstack.h"
#include "scanwidget.h"

//	constructor
//	===========

IndexWidget::IndexWidget( ScanStack* scanStack, MainWidget* parent, const char* name )
: TQScrollView( parent, name ),
  m_blinkTimer( NULL ),
  m_scanCount( 0 ),
  m_scanStack( scanStack )
{	setDragAutoScroll( true );
	viewport( )->setAcceptDrops( true );
}

//	calculateIndex
//	==============

short IndexWidget::calculateIndex( const ushort y )
{	TQFontMetrics metrics = fontMetrics( );
	byte         height  = metrics.height( );
	ushort       index   = y / height;

	return index > m_scanStack->count( ) - 1 ? -1 : index;
}

//	contentsContextMenuEvent
//	========================

void IndexWidget::contentsContextMenuEvent( TQContextMenuEvent* event )
{	short index = calculateIndex( event->y( ));

	if( index < 0 )
		return;

	ScanWidget* scanWidget = m_scanStack->makeScanWidgetVisible( index );
	emit( visibleScanWidgetChanged( scanWidget ));

	TQPopupMenu*  contextMenu   = new TQPopupMenu( this, "context menu" );
	TDEIconLoader* iconLoader    = TDEGlobal::iconLoader( );
	TQIconSet     profileLoad   = iconLoader->loadIconSet( "profileload",   TDEIcon::Small );
	TQIconSet     profileSave   = iconLoader->loadIconSet( "profilesave",   TDEIcon::Small );
	TQIconSet     profileSaveAs = iconLoader->loadIconSet( "profilesaveas", TDEIcon::Small );
	TQIconSet     scanClose     = iconLoader->loadIconSet( "scanclose",     TDEIcon::Small );
	TQIconSet     scanDuplicate = iconLoader->loadIconSet( "scanduplicate", TDEIcon::Small );
	TQIconSet     scanNew       = iconLoader->loadIconSet( "scannew",       TDEIcon::Small );
	TQIconSet     scanRename    = iconLoader->loadIconSet( "scanrename",    TDEIcon::Small );

	contextMenu->insertItem( scanClose,     i18n( "&Close scan"      ), parent( ), SLOT( slotScanClose( )),     CTRL+Key_W );
	contextMenu->insertItem( scanDuplicate, i18n( "&Duplicate scan"  ), parent( ), SLOT( slotScanDuplicate( )), CTRL+Key_C );
	contextMenu->insertItem( scanNew,       i18n( "&New scan..."     ), parent( ), SLOT( slotScanNew( )),       CTRL+Key_N );
	contextMenu->insertItem( scanRename,    i18n( "&Rename scan..."  ), parent( ), SLOT( slotScanRename( )),    Key_F2     );
	contextMenu->insertItem( i18n( "&Use target host name" ), parent( ), SLOT( slotUseTargetHost( )));
	contextMenu->insertSeparator( );
	contextMenu->insertItem( profileLoad,   i18n( "&Load profile..."    ), parent( ), SLOT( slotProfileLoad( )));
	contextMenu->insertItem( profileSave,   i18n( "&Save profile..."    ), parent( ), SLOT( slotProfileSave( )));
	contextMenu->insertItem( profileSaveAs, i18n( "&Save profile as..." ), parent( ), SLOT( slotProfileSaveAs( )));

	contextMenu->exec( event->globalPos( ));
}

//	contentsDragMoveEvent
//	=====================

void IndexWidget::contentsDragMoveEvent( TQDragMoveEvent* event )
{	short index = calculateIndex( event->pos( ).y( ));
	index = index < 0 ? m_scanStack->count( ) - 1 : index;

	ScanWidget* scanWidget = m_scanStack->makeScanWidgetVisible( index );
	emit( visibleScanWidgetChanged( scanWidget ));
}

//	contentsMouseMoveEvent
//	======================

void IndexWidget::contentsMouseMoveEvent( TQMouseEvent* event )
{	if( event->stateAfter( ) != TQt::LeftButton )
		return;

	short index = calculateIndex( event->y( ));

	if( index < 0 )
		return;

	TQStoredDrag* drag = new TQStoredDrag( "application/octet-stream", this );
	drag->dragMove( );
	ScanWidget* scanWidget = m_scanStack->moveScanWidget( index );

	scanWidget->ignoreTabChanges( true );
	m_scanStack->raiseWidget( scanWidget );
	scanWidget->ignoreTabChanges( false );

	emit( visibleScanWidgetChanged( scanWidget ));
}

//	contentsMousePressEvent
//	=======================

void IndexWidget::contentsMousePressEvent( TQMouseEvent* event )
{	if( event->stateAfter( ) != TQt::LeftButton )
		return;

	short index = calculateIndex( event->y( ));

	if( index < 0 )
		return;

	ScanWidget* scanWidget = m_scanStack->makeScanWidgetVisible( index );
	emit( visibleScanWidgetChanged( scanWidget ));

	if( scanWidget->state( ) == ScanWidget::finished )
		scanWidget->state( ScanWidget::dormant );
}

//	drawContents
//	============

void IndexWidget::drawContents( TQPainter* painter, int clipx, int clipy, int clipw, int cliph )
{	painter->fillRect( clipx, clipy, clipw, cliph, TDEGlobalSettings::baseColor( ));
	painter->setBackgroundMode( TQt::OpaqueMode );

	TQFontMetrics metrics = fontMetrics( );
	byte   height  = metrics.height( );
	byte   aWidth2 = 2 * metrics.width( "*" );
	ushort x       = height / 2;
	ushort xMax    = 0;
	ushort y       = 0;
	ushort i;

	for( m_scanStack->resetScanWidgets( i ); m_scanStack->moreScanWidgets( i ); y += height, m_scanStack->nextScanWidget( i ))
	{	ScanWidget* scanWidget = m_scanStack->currentScanWidget( i );

		switch( scanWidget->state( ))
		{	case ScanWidget::dormant:
				if( m_scanStack->isVisibleWidget( scanWidget ))
				{	painter->fillRect( 0, y, visibleWidth( ), height, TDEGlobalSettings::alternateBackgroundColor( ));
					painter->setBackgroundColor( TDEGlobalSettings::alternateBackgroundColor( ));
					painter->setPen( TDEGlobalSettings::buttonTextColor( ));
					break;
				}

				painter->setBackgroundColor( TDEGlobalSettings::baseColor( ));
				painter->setPen( TDEGlobalSettings::buttonTextColor( ));
				break;

			case ScanWidget::finished:
				painter->fillRect( 0, y, visibleWidth( ), height, TDEGlobalSettings::highlightColor( ));
				painter->setBackgroundColor( TDEGlobalSettings::highlightColor( ));
				painter->setPen( TDEGlobalSettings::activeTextColor( ));
				break;

			case ScanWidget::running:
				if( m_highlight )
				{	painter->fillRect( 0, y, visibleWidth( ), height, TDEGlobalSettings::highlightColor( ));
					painter->setBackgroundColor( TDEGlobalSettings::highlightColor( ));
					painter->setPen( TDEGlobalSettings::activeTextColor( ));
					break;
				}

				if( m_scanStack->isVisibleWidget( scanWidget ))
				{	painter->fillRect( 0, y, visibleWidth( ), height, TDEGlobalSettings::alternateBackgroundColor( ));
					painter->setBackgroundColor( TDEGlobalSettings::alternateBackgroundColor( ));
					painter->setPen( TDEGlobalSettings::buttonTextColor( ));
					break;
				}

				painter->fillRect( 0, y, visibleWidth( ), height, TDEGlobalSettings::baseColor( ));
				painter->setBackgroundColor( TDEGlobalSettings::baseColor( ));
				painter->setPen( TDEGlobalSettings::buttonTextColor( ));
				break;
		}

		if( scanWidget->useTargetHost( ))
			painter->drawText( x, y + metrics.ascent( ), "*" );

		painter->drawText( x + aWidth2, y + metrics.ascent( ), scanWidget->scanName( ));
		xMax = _max_( xMax, x + aWidth2 + metrics.width( scanWidget->scanName( )));
	}

	if( m_scanCount )
		m_highlight = !m_highlight;

	resizeContents( xMax + height / 2, y );
}

//	ensureCurrentVisible
//	====================

void IndexWidget::ensureCurrentVisible( )
{	short index = m_scanStack->indexOfVisibleScanWidget( );

	if( index < 0 )
		return;

	TQFontMetrics metrics = fontMetrics( );
	short y = index * metrics.height( ) - height( ) / 2;
	setContentsPos( 0, y );
	updateContents( );
}

//	ensureCurrentVisible
//	====================

void IndexWidget::slotRepaint( )
{	repaintContents( false );
}

//	slotScanStarted
//	===============

void IndexWidget::slotScanStarted( )
{	if( m_scanCount++ )
		return;

	ASSERT( m_blinkTimer == NULL );
	m_blinkTimer = new TQTimer( this );
	connect( m_blinkTimer, SIGNAL( timeout( )), SLOT( slotRepaint( )));
	m_blinkTimer->start( 500 );
	m_highlight = true;
}

//	slotScanStopped
//	===============

void IndexWidget::slotScanStopped( ScanWidget* scanWidget )
{	scanWidget->state( scanWidget == m_scanStack->visibleWidget( ) ? ScanWidget::dormant : ScanWidget::finished );
	updateContents( );

	if( --m_scanCount )
		return;

	delete m_blinkTimer;
	m_blinkTimer = NULL;
}