summaryrefslogtreecommitdiff
path: root/core/applets/clipboardapplet/clipboard.cpp
blob: 4fc8076b073da4b948ef0b30016e359b6a6116fc (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
/**********************************************************************
** Copyright (C) 2000 Trolltech AS.  All rights reserved.
**
** This file is part of Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#include "clipboard.h"

#include <opie2/otaskbarapplet.h>
#include <qpe/resource.h>
#include <qpe/applnk.h>

#include <qpainter.h>
#include <qpopupmenu.h>
#include <qwindowsystem_qws.h>
#include <qapplication.h>
#include <qclipboard.h>
#include <qtimer.h>

//===========================================================================


ClipboardApplet::ClipboardApplet( QWidget *parent, const char *name ) : QWidget( parent, name )
{
        setFixedWidth ( AppLnk::smallIconSize()  );
        setFixedHeight ( AppLnk::smallIconSize()  );

	QImage img = Resource::loadImage( "paste");
	img = img.smoothScale( AppLnk::smallIconSize(), AppLnk::smallIconSize() );

	m_clipboardPixmap.convertFromImage( img );

	m_timer = new QTimer ( this );

	connect ( QApplication::clipboard ( ), SIGNAL( dataChanged()), this, SLOT( newData()));
	connect ( m_timer, SIGNAL( timeout()), this, SLOT( newData()));
	connect ( qApp, SIGNAL( aboutToQuit()), this, SLOT( shutdown()));

	m_menu = 0;
	m_dirty = true;
	m_lasttext = QString::null;

	m_timer-> start ( 0, true );
}

ClipboardApplet::~ClipboardApplet ( )
{
}

int ClipboardApplet::position()
{
    return 6;
}

void ClipboardApplet::shutdown ( )
{
	// the timer has to be stopped, or Qt/E will hang on quit()
	// see launcher/desktop.cpp

	m_timer-> stop ( );
}

void ClipboardApplet::mousePressEvent ( QMouseEvent *)
{
	if ( m_dirty ) {
		delete m_menu;

		m_menu = new QPopupMenu ( this );
		m_menu-> setCheckable ( true );

		if ( m_history. count ( )) {
			for ( unsigned int i = 0; i < m_history. count ( ); i++ ) {
				QString str = m_history [i];

				if ( str. length ( ) > 20 )
					str = str. left ( 20 ) + "...";

				m_menu-> insertItem ( QString ( "%1: %2" ). arg ( i + 1 ). arg ( str ), i );
				m_menu-> setItemChecked ( i, false );
			}
			m_menu-> setItemChecked ( m_history. count ( ) - 1, true );
			m_menu-> insertSeparator ( );
		}
		m_menu-> insertItem ( QIconSet ( Resource::loadPixmap ( "cut" )), tr( "Cut" ), 100 );
		m_menu-> insertItem ( QIconSet ( Resource::loadPixmap ( "copy" )), tr( "Copy" ), 101 );
		m_menu-> insertItem ( QIconSet ( Resource::loadPixmap ( "paste" )), tr( "Paste" ), 102 );

		connect ( m_menu, SIGNAL( activated(int)), this, SLOT( action(int)));

		m_dirty = false;
	}
	QPoint p = mapToGlobal ( QPoint ( 0, 0 ));
	QSize s = m_menu-> sizeHint ( );

	m_menu-> popup ( QPoint ( p. x ( ) + ( width ( ) / 2 ) - ( s. width ( ) / 2 ), p. y ( ) - s. height ( )));
}

void ClipboardApplet::action(int id)
{
	ushort unicode = 0;
	int scan = 0;

	switch ( id ) {
		case 100:
			unicode = 'X' - '@';
			scan    = Key_X; // Cut
			break;
		case 101:
			unicode = 'C' - '@';
			scan    = Key_C; // Copy
			break;
		case 102:
			unicode = 'V' - '@';
			scan    = Key_V; // Paste
			break;

		default:
			if (( id >= 0 ) && ( uint( id ) < m_history. count ( ))) {
				QApplication::clipboard ( )-> setText ( m_history [id] );

				for ( uint i = 0; i < m_history. count ( ); i++ )
					m_menu-> setItemChecked ( i, i == uint( id ));

				unicode = 'V' - '@';
				scan    = Key_V;
			}
			break;
	}

	if ( scan ) {
		qwsServer-> sendKeyEvent ( unicode, scan, ControlButton, true, false );
		qwsServer-> sendKeyEvent ( unicode, scan, ControlButton, false, false );
	}
}

void ClipboardApplet::paintEvent ( QPaintEvent* )
{
	QPainter p ( this );
        /* center the height but our pixmap is as big as the height ;)*/
      	p. drawPixmap( 0, 0,
                       m_clipboardPixmap );
}

void ClipboardApplet::newData ( )
{
	static bool excllock = false;

	if ( excllock )
		return;
	else
		excllock = true;

	m_timer-> stop ( );

	QCString type = "plain";
	QString txt = QApplication::clipboard ( )-> text ( type );

	if ( !txt. isEmpty ( ) && !m_history. contains ( txt )) {
		m_history. append ( txt );

		if ( m_history. count ( ) > 5 )
			m_history. remove ( m_history. begin ( ));

		m_dirty = true;
	}

	m_timer-> start ( 1500, true );

	excllock = false;
}

Q_EXPORT_INTERFACE()
{
    Q_CREATE_INSTANCE( OTaskbarAppletWrapper<ClipboardApplet> );
}