summaryrefslogtreecommitdiffstats
path: root/kword/KWMailMergeLabelAction.cpp
blob: 79d06378338ae1bcb7500491faa4393f077ea787 (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
/* This file is part of the KDE project

   Copyright (C) 2002 Joseph Wenninger <jowenn@kde.org>

   Large parts are taken from kdebase/konqueror/konq_actions.*
   Copyright (C) 2000 Simon Hausmann <hausmann@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "KWMailMergeLabelAction.h"
#include "KWMailMergeLabelAction.moc"
#include <KoMainWindow.h>
#include <kstyle.h>
#include <kpopupmenu.h>

class MailMergeDraggableLabel : public QToolButton
{
public:
    MailMergeDraggableLabel( KoMainWindow * mw, const QString & text, QWidget * parent = 0, const char * name = 0 )
        : QToolButton( parent, name ), m_mw(mw)
    {
        setText(text);
        setAcceptDrops(true);
        validDrag = false;
    }
protected:
    void mousePressEvent( QMouseEvent * ev )
    {
        validDrag = true;
        startDragPos = ev->pos();
    }
    void mouseMoveEvent( QMouseEvent * ev )
    {
        if ((startDragPos - ev->pos()).manhattanLength() > QApplication::startDragDistance())
        {
            validDrag = false;

#if 0             // what was the goal here?
        KWTextDrag *drag=new KWTextDrag(m_mw);
//      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>");
        drag->setKWord(" <!DOCTYPE PARAGRAPHS> <PARAGRAPHS> <PARAGRAPH>  <TEXT>fsfsd</TEXT> </PARAGRAPH> </PARAGRAPHS>");
//      drag->setKWord("<TEXT>fsfsd</TEXT>");

        drag->dragCopy();
#endif
        }
    }
    void mouseReleaseEvent( QMouseEvent * )
    {
        validDrag = false;
    }
    QSize sizeHint() const
    {
        int w = fontMetrics().width( text() );
        int h = fontMetrics().height();
        return QSize( w, h );
    }
    void drawButton( QPainter * p )
    {
        // Draw the background
        style().drawComplexControl( QStyle::CC_ToolButton, p, this, rect(), colorGroup(),
                                    QStyle::Style_Enabled, QStyle::SC_ToolButton );
        // Draw the label
        style().drawControl( QStyle::CE_ToolButtonLabel, p, this, rect(), colorGroup(),
                             QStyle::Style_Enabled );
    }
    void enterEvent( QEvent* ) {};
    void leaveEvent( QEvent* ) {};
#if 0
    void dragEnterEvent( QDragEnterEvent *ev ) {
        if ( KURLDrag::canDecode( ev ) )
            ev->acceptAction();
    }
    void dropEvent( QDropEvent* ev ) {
        KURL::List lst;
        if ( KURLDrag::decode( ev, lst ) ) {
            m_mw->openURL( 0L, lst.first() );
        }
    }
#endif
private:
    QPoint startDragPos;
    bool validDrag;
    KoMainWindow * m_mw;
};





KWMailMergeLabelAction::KWMailMergeLabelAction( const QString &text, int accel,
                    QObject* receiver, const char* slot, QObject *parent, const char *name )
    : KAction( text, accel, receiver, slot, parent, name ), m_label( 0L )
{
}

int KWMailMergeLabelAction::plug( QWidget *widget, int index )
{
  //do not call the previous implementation here

  if ( widget->inherits( "KToolBar" ) )
  {
    KToolBar *tb = (KToolBar *)widget;

    int id = KAction::getToolButtonID();

    m_label = new MailMergeDraggableLabel( static_cast<KoMainWindow*>(tb->mainWindow()), text(), widget );
    tb->insertWidget( id, m_label->width(), m_label, index );

    addContainer( tb, id );

    connect( tb, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );

    return containerCount() - 1;
  }

  return -1;
}

void KWMailMergeLabelAction::unplug( QWidget *widget )
{
  if ( widget->inherits( "KToolBar" ) )
  {
    KToolBar *bar = (KToolBar *)widget;

    int idx = findContainer( bar );

    if ( idx != -1 )
    {
      bar->removeItem( itemId( idx ) );
      removeContainer( idx );
    }

    m_label = 0;
    return;
  }
}