Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

clipboard.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 
00021 #include "clipboard.h"
00022 
00023 #include <opie2/otaskbarapplet.h>
00024 #include <opie2/oresource.h>
00025 
00026 #include <qpe/applnk.h>
00027 
00028 #include <qpainter.h>
00029 #include <qpopupmenu.h>
00030 #include <qwindowsystem_qws.h>
00031 #include <qapplication.h>
00032 #include <qclipboard.h>
00033 #include <qtimer.h>
00034 
00035 //===========================================================================
00036 
00037 
00038 using namespace Opie::Ui;
00039 ClipboardApplet::ClipboardApplet( QWidget *parent, const char *name ) : QWidget( parent, name )
00040 {
00041         setFixedWidth ( AppLnk::smallIconSize()  );
00042         setFixedHeight ( AppLnk::smallIconSize()  );
00043 
00044     m_clipboardPixmap = Opie::Core::OResource::loadPixmap( "paste", Opie::Core::OResource::SmallIcon );
00045 
00046         m_timer = new QTimer ( this );
00047 
00048         connect ( QApplication::clipboard ( ), SIGNAL( dataChanged()), this, SLOT( newData()));
00049         connect ( m_timer, SIGNAL( timeout()), this, SLOT( newData()));
00050         connect ( qApp, SIGNAL( aboutToQuit()), this, SLOT( shutdown()));
00051 
00052         m_menu = 0;
00053         m_dirty = true;
00054         m_lasttext = QString::null;
00055 
00056         m_timer-> start ( 0, true );
00057 }
00058 
00059 ClipboardApplet::~ClipboardApplet ( )
00060 {
00061 }
00062 
00063 int ClipboardApplet::position()
00064 {
00065     return 6;
00066 }
00067 
00068 void ClipboardApplet::shutdown ( )
00069 {
00070         // the timer has to be stopped, or Qt/E will hang on quit()
00071         // see launcher/desktop.cpp
00072 
00073         m_timer-> stop ( );
00074 }
00075 
00076 void ClipboardApplet::mousePressEvent ( QMouseEvent *)
00077 {
00078         if ( m_dirty ) {
00079                 delete m_menu;
00080 
00081                 m_menu = new QPopupMenu ( this );
00082                 m_menu-> setCheckable ( true );
00083 
00084                 if ( m_history. count ( )) {
00085                         for ( unsigned int i = 0; i < m_history. count ( ); i++ ) {
00086                                 QString str = m_history [i];
00087 
00088                                 if ( str. length ( ) > 20 )
00089                                         str = str. left ( 20 ) + "...";
00090 
00091                                 m_menu-> insertItem ( QString ( "%1: %2" ). arg ( i + 1 ). arg ( str ), i );
00092                                 m_menu-> setItemChecked ( i, false );
00093                         }
00094                         m_menu-> setItemChecked ( m_history. count ( ) - 1, true );
00095                         m_menu-> insertSeparator ( );
00096                 }
00097     m_menu-> insertItem ( Opie::Core::OResource::loadPixmap( "cut", Opie::Core::OResource::SmallIcon ), tr( "Cut" ), 100 );
00098     m_menu-> insertItem ( Opie::Core::OResource::loadPixmap( "copy", Opie::Core::OResource::SmallIcon ), tr( "Copy" ), 101 );
00099     m_menu-> insertItem ( Opie::Core::OResource::loadPixmap( "paste", Opie::Core::OResource::SmallIcon ), tr( "Paste" ), 102 );
00100 
00101                 connect ( m_menu, SIGNAL( activated(int)), this, SLOT( action(int)));
00102 
00103                 m_dirty = false;
00104         }
00105         QPoint p = mapToGlobal ( QPoint ( 0, 0 ));
00106         QSize s = m_menu-> sizeHint ( );
00107 
00108         m_menu-> popup ( QPoint ( p. x ( ) + ( width ( ) / 2 ) - ( s. width ( ) / 2 ), p. y ( ) - s. height ( )));
00109 }
00110 
00111 void ClipboardApplet::action(int id)
00112 {
00113         ushort unicode = 0;
00114         int scan = 0;
00115 
00116         switch ( id ) {
00117                 case 100:
00118                         unicode = 'X' - '@';
00119                         scan    = Key_X; // Cut
00120                         break;
00121                 case 101:
00122                         unicode = 'C' - '@';
00123                         scan    = Key_C; // Copy
00124                         break;
00125                 case 102:
00126                         unicode = 'V' - '@';
00127                         scan    = Key_V; // Paste
00128                         break;
00129 
00130                 default:
00131                         if (( id >= 0 ) && ( uint( id ) < m_history. count ( ))) {
00132                                 QApplication::clipboard ( )-> setText ( m_history [id] );
00133 
00134                                 for ( uint i = 0; i < m_history. count ( ); i++ )
00135                                         m_menu-> setItemChecked ( i, i == uint( id ));
00136 
00137                                 unicode = 'V' - '@';
00138                                 scan    = Key_V;
00139                         }
00140                         break;
00141         }
00142 
00143         if ( scan ) {
00144                 qwsServer-> sendKeyEvent ( unicode, scan, ControlButton, true, false );
00145                 qwsServer-> sendKeyEvent ( unicode, scan, ControlButton, false, false );
00146         }
00147 }
00148 
00149 void ClipboardApplet::paintEvent ( QPaintEvent* )
00150 {
00151         QPainter p ( this );
00152         /* center the height but our pixmap is as big as the height ;)*/
00153         p. drawPixmap( 0, 0,
00154                        m_clipboardPixmap );
00155 }
00156 
00157 void ClipboardApplet::newData ( )
00158 {
00159         static bool excllock = false;
00160 
00161         if ( excllock )
00162                 return;
00163         else
00164                 excllock = true;
00165 
00166         m_timer-> stop ( );
00167 
00168         QCString type = "plain";
00169         QString txt = QApplication::clipboard ( )-> text ( type );
00170 
00171         if ( !txt. isEmpty ( ) && !m_history. contains ( txt )) {
00172                 m_history. append ( txt );
00173 
00174                 if ( m_history. count ( ) > 5 )
00175                         m_history. remove ( m_history. begin ( ));
00176 
00177                 m_dirty = true;
00178         }
00179 
00180         m_timer-> start ( 1500, true );
00181 
00182         excllock = false;
00183 }
00184 
00185 EXPORT_OPIE_APPLET_v1( ClipboardApplet )
00186 

Generated on Sat Nov 5 16:15:24 2005 for OPIE by  doxygen 1.4.2