kivioarrowheadaction.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003 Peter Simonsson <psn@linux.se>,
3 theKompany.com & Dave Marotti
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include "kivioarrowheadaction.h"
22#include "kivioglobal.h"
23
24#include <tqbitmap.h>
25#include <tqpixmap.h>
26#include <tqimage.h>
27#include <tqpainter.h>
28#include <tqmenubar.h>
29#include <tqwhatsthis.h>
30
31#include <tdepopupmenu.h>
32#include <tdetoolbar.h>
33#include <tdelocale.h>
34#include <kdebug.h>
35#include <tdeapplication.h>
36#include <tdetoolbarbutton.h>
37#include <kstandarddirs.h>
38#include <kiconloader.h>
39
40KivioArrowHeadAction::KivioArrowHeadAction(const TQString &text, const TQString &pix,
41 TQObject* parent, const char *name)
42 : TDEActionMenu(text, pix, parent, name)
43{
44 m_emitSignals = true;
45 setShortcutConfigurable( false );
46 m_popup = new TDEPopupMenu(0L,"KivioArrowHeadAction::popup");
47 m_startPopup = new TDEPopupMenu;
48 m_endPopup = new TDEPopupMenu;
49 m_startPopup->setCheckable(true);
50 m_endPopup->setCheckable(true);
51 m_popup->insertItem(SmallIconSet("start_arrowhead", 16), i18n("Arrowhead at Origin"), m_startPopup);
52 m_popup->insertItem(SmallIconSet("end_arrowhead", 16), i18n("Arrowhead at End"), m_endPopup);
53 loadArrowHeads(m_startPopup);
54 loadArrowHeads(m_endPopup);
55 m_currentStart = m_currentEnd = 0;
56 m_startPopup->setItemChecked(0, true);
57 m_endPopup->setItemChecked(0, true);
58 connect(m_startPopup, TQ_SIGNAL(activated(int)), TQ_SLOT(setCurrentStartArrow(int)));
59 connect(m_endPopup, TQ_SIGNAL(activated(int)), TQ_SLOT(setCurrentEndArrow(int)));
60}
61
62KivioArrowHeadAction::~KivioArrowHeadAction()
63{
64 delete m_startPopup;
65 m_startPopup = 0;
66 delete m_endPopup;
67 m_endPopup = 0;
68 delete m_popup;
69 m_popup = 0;
70}
71
72TDEPopupMenu* KivioArrowHeadAction::popupMenu() const
73{
74 return m_popup;
75}
76
77void KivioArrowHeadAction::popup( const TQPoint& global )
78{
79 popupMenu()->popup(global);
80}
81
82int KivioArrowHeadAction::plug( TQWidget* widget, int index)
83{
84 // This function is copied from TDEActionMenu::plug
85 if (kapp && !kapp->authorizeTDEAction(name()))
86 return -1;
87 kdDebug(129) << "TDEAction::plug( " << widget << ", " << index << " )" << endl; // remove -- ellis
88 if ( widget->inherits("TQPopupMenu") )
89 {
90 TQPopupMenu* menu = static_cast<TQPopupMenu*>( widget );
91 int id;
92
93 if ( hasIconSet() )
94 id = menu->insertItem( iconSet(), text(), popupMenu(), -1, index );
95 else
96 id = menu->insertItem( kapp->iconLoader()->loadIcon(icon(), TDEIcon::Small),
97 text(), popupMenu(), -1, index );
98
99 if ( !isEnabled() )
100 menu->setItemEnabled( id, false );
101
102 addContainer( menu, id );
103 connect( menu, TQ_SIGNAL( destroyed() ), this, TQ_SLOT( slotDestroyed() ) );
104
105 return containerCount() - 1;
106 }
107 else if ( widget->inherits( "TDEToolBar" ) )
108 {
109 TDEToolBar *bar = static_cast<TDEToolBar *>( widget );
110
111 int id_ = TDEAction::getToolButtonID();
112
113 if ( icon().isEmpty() && !iconSet().isNull() )
114 bar->insertButton( iconSet().pixmap(), id_, TQ_SIGNAL( clicked() ), this,
115 TQ_SLOT( slotActivated() ), isEnabled(), plainText(),
116 index );
117 else
118 {
119 TDEInstance *instance;
120
121 if ( m_parentCollection )
122 instance = m_parentCollection->instance();
123 else
124 instance = TDEGlobal::instance();
125
126 bar->insertButton( icon(), id_, TQ_SIGNAL( clicked() ), this,
127 TQ_SLOT( slotActivated() ), isEnabled(), plainText(),
128 index, instance );
129 }
130
131 addContainer( bar, id_ );
132
133 if (!whatsThis().isEmpty())
134 TQWhatsThis::add( bar->getButton(id_), whatsThis() );
135
136 connect( bar, TQ_SIGNAL( destroyed() ), this, TQ_SLOT( slotDestroyed() ) );
137
138 bar->getButton(id_)->setPopup(popupMenu(), true );
139
140 return containerCount() - 1;
141 }
142 else if ( widget->inherits( "TQMenuBar" ) )
143 {
144 TQMenuBar *bar = static_cast<TQMenuBar *>( widget );
145
146 int id;
147
148 id = bar->insertItem( text(), popupMenu(), -1, index );
149
150 if ( !isEnabled() )
151 bar->setItemEnabled( id, false );
152
153 addContainer( bar, id );
154 connect( bar, TQ_SIGNAL( destroyed() ), this, TQ_SLOT( slotDestroyed() ) );
155
156 return containerCount() - 1;
157 }
158
159 return -1;
160}
161
162void KivioArrowHeadAction::loadArrowHeads(TDEPopupMenu* popup)
163{
164 TQBitmap mask;
165 TQPixmap pixAll = Kivio::arrowHeadPixmap();
166 TQPixmap pix(pixAll.width(), 17);
167 TQPainter p(&pix, popup);
168 int cindex = 0;
169
170 // insert item "None"
171 popup->insertItem(i18n("no line end", "None"),cindex++);
172
173 for (int y = 0; y < pixAll.height(); y += 17 ) {
174 pix.fill(white);
175 p.drawPixmap(0, 0, pixAll, 0, y, pix.width(), pix.height());
176 popup->insertItem(pix, cindex++);
177 }
178
179 p.end();
180}
181
182int KivioArrowHeadAction::currentStartArrow()
183{
184 return m_currentStart;
185}
186
187int KivioArrowHeadAction::currentEndArrow()
188{
189 return m_currentEnd;
190}
191
192void KivioArrowHeadAction::setCurrentStartArrow(int c)
193{
194 m_startPopup->setItemChecked(m_currentStart, false);
195 m_currentStart = c;
196 m_startPopup->setItemChecked(m_currentStart, true);
197
198 if(m_emitSignals) {
199 emit startChanged(m_currentStart);
200 }
201}
202
203void KivioArrowHeadAction::setCurrentEndArrow(int c)
204{
205 m_endPopup->setItemChecked(m_currentEnd, false);
206 m_currentEnd = c;
207 m_endPopup->setItemChecked(m_currentEnd, true);
208
209 if(m_emitSignals) {
210 emit endChanged(m_currentEnd);
211 }
212}
213
214#include "kivioarrowheadaction.moc"