kword

KWMailMergeLabelAction.cpp

00001 /* This file is part of the KDE project
00002 
00003    Copyright (C) 2002 Joseph Wenninger <jowenn@kde.org>
00004 
00005    Large parts are taken from kdebase/konqueror/konq_actions.*
00006    Copyright (C) 2000 Simon Hausmann <hausmann@kde.org>
00007 
00008    This library is free software; you can redistribute it and/or
00009    modify it under the terms of the GNU Library General Public
00010    License as published by the Free Software Foundation; either
00011    version 2 of the License, or (at your option) any later version.
00012 
00013    This library is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016    Library General Public License for more details.
00017 
00018    You should have received a copy of the GNU Library General Public License
00019    along with this library; see the file COPYING.LIB.  If not, write to
00020    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021  * Boston, MA 02110-1301, USA.
00022 */
00023 
00024 #include "KWMailMergeLabelAction.h"
00025 #include "KWMailMergeLabelAction.moc"
00026 #include <KoMainWindow.h>
00027 #include <kstyle.h>
00028 #include <kpopupmenu.h>
00029 
00030 class MailMergeDraggableLabel : public QToolButton
00031 {
00032 public:
00033     MailMergeDraggableLabel( KoMainWindow * mw, const QString & text, QWidget * parent = 0, const char * name = 0 )
00034         : QToolButton( parent, name ), m_mw(mw)
00035     {
00036         setText(text);
00037         setAcceptDrops(true);
00038         validDrag = false;
00039     }
00040 protected:
00041     void mousePressEvent( QMouseEvent * ev )
00042     {
00043         validDrag = true;
00044         startDragPos = ev->pos();
00045     }
00046     void mouseMoveEvent( QMouseEvent * ev )
00047     {
00048         if ((startDragPos - ev->pos()).manhattanLength() > QApplication::startDragDistance())
00049         {
00050             validDrag = false;
00051 
00052 #if 0             // what was the goal here?
00053         KWTextDrag *drag=new KWTextDrag(m_mw);
00054 //      drag->setKWord(" <!DOCTYPE PARAGRAPHS> <PARAGRAPHS> <PARAGRAPH>  <TEXT>fsfsd</TEXT>  <LAYOUT>   <NAME value=\"Standard\" />   <FLOW align=\"left\" />   <FORMAT id=\"1\" pos=\"0\" len=\"5\" >    <WEIGHT value=\"50\" />    <FONT name=\"helvetica\" />    <SIZE value=\"12\" />    <ITALIC value=\"0\" />    <UNDERLINE value=\"0\" />    <STRIKEOUT value=\"0\" />    <VERTALIGN value=\"0\" />   </FORMAT>  </LAYOUT> </PARAGRAPH></PARAGRAPHS>");
00055         drag->setKWord(" <!DOCTYPE PARAGRAPHS> <PARAGRAPHS> <PARAGRAPH>  <TEXT>fsfsd</TEXT> </PARAGRAPH> </PARAGRAPHS>");
00056 //      drag->setKWord("<TEXT>fsfsd</TEXT>");
00057 
00058         drag->dragCopy();
00059 #endif
00060         }
00061     }
00062     void mouseReleaseEvent( QMouseEvent * )
00063     {
00064         validDrag = false;
00065     }
00066     QSize sizeHint() const
00067     {
00068         int w = fontMetrics().width( text() );
00069         int h = fontMetrics().height();
00070         return QSize( w, h );
00071     }
00072     void drawButton( QPainter * p )
00073     {
00074         // Draw the background
00075         style().drawComplexControl( QStyle::CC_ToolButton, p, this, rect(), colorGroup(),
00076                                     QStyle::Style_Enabled, QStyle::SC_ToolButton );
00077         // Draw the label
00078         style().drawControl( QStyle::CE_ToolButtonLabel, p, this, rect(), colorGroup(),
00079                              QStyle::Style_Enabled );
00080     }
00081     void enterEvent( QEvent* ) {};
00082     void leaveEvent( QEvent* ) {};
00083 #if 0
00084     void dragEnterEvent( QDragEnterEvent *ev ) {
00085         if ( KURLDrag::canDecode( ev ) )
00086             ev->acceptAction();
00087     }
00088     void dropEvent( QDropEvent* ev ) {
00089         KURL::List lst;
00090         if ( KURLDrag::decode( ev, lst ) ) {
00091             m_mw->openURL( 0L, lst.first() );
00092         }
00093     }
00094 #endif
00095 private:
00096     QPoint startDragPos;
00097     bool validDrag;
00098     KoMainWindow * m_mw;
00099 };
00100 
00101 
00102 
00103 
00104 
00105 KWMailMergeLabelAction::KWMailMergeLabelAction( const QString &text, int accel,
00106                     QObject* receiver, const char* slot, QObject *parent, const char *name )
00107     : KAction( text, accel, receiver, slot, parent, name ), m_label( 0L )
00108 {
00109 }
00110 
00111 int KWMailMergeLabelAction::plug( QWidget *widget, int index )
00112 {
00113   //do not call the previous implementation here
00114 
00115   if ( widget->inherits( "KToolBar" ) )
00116   {
00117     KToolBar *tb = (KToolBar *)widget;
00118 
00119     int id = KAction::getToolButtonID();
00120 
00121     m_label = new MailMergeDraggableLabel( static_cast<KoMainWindow*>(tb->mainWindow()), text(), widget );
00122     tb->insertWidget( id, m_label->width(), m_label, index );
00123 
00124     addContainer( tb, id );
00125 
00126     connect( tb, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
00127 
00128     return containerCount() - 1;
00129   }
00130 
00131   return -1;
00132 }
00133 
00134 void KWMailMergeLabelAction::unplug( QWidget *widget )
00135 {
00136   if ( widget->inherits( "KToolBar" ) )
00137   {
00138     KToolBar *bar = (KToolBar *)widget;
00139 
00140     int idx = findContainer( bar );
00141 
00142     if ( idx != -1 )
00143     {
00144       bar->removeItem( itemId( idx ) );
00145       removeContainer( idx );
00146     }
00147 
00148     m_label = 0;
00149     return;
00150   }
00151 }
00152 
KDE Home | KDE Accessibility Home | Description of Access Keys