00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "kivioarrowheadaction.h"
00022 #include "kivioglobal.h"
00023
00024 #include <qbitmap.h>
00025 #include <qpixmap.h>
00026 #include <qimage.h>
00027 #include <qpainter.h>
00028 #include <qmenubar.h>
00029 #include <qwhatsthis.h>
00030
00031 #include <kpopupmenu.h>
00032 #include <ktoolbar.h>
00033 #include <klocale.h>
00034 #include <kdebug.h>
00035 #include <kapplication.h>
00036 #include <ktoolbarbutton.h>
00037 #include <kstandarddirs.h>
00038 #include <kiconloader.h>
00039
00040 KivioArrowHeadAction::KivioArrowHeadAction(const QString &text, const QString &pix,
00041 QObject* parent, const char *name)
00042 : KActionMenu(text, pix, parent, name)
00043 {
00044 m_emitSignals = true;
00045 setShortcutConfigurable( false );
00046 m_popup = new KPopupMenu(0L,"KivioArrowHeadAction::popup");
00047 m_startPopup = new KPopupMenu;
00048 m_endPopup = new KPopupMenu;
00049 m_startPopup->setCheckable(true);
00050 m_endPopup->setCheckable(true);
00051 m_popup->insertItem(SmallIconSet("start_arrowhead", 16), i18n("Arrowhead at Origin"), m_startPopup);
00052 m_popup->insertItem(SmallIconSet("end_arrowhead", 16), i18n("Arrowhead at End"), m_endPopup);
00053 loadArrowHeads(m_startPopup);
00054 loadArrowHeads(m_endPopup);
00055 m_currentStart = m_currentEnd = 0;
00056 m_startPopup->setItemChecked(0, true);
00057 m_endPopup->setItemChecked(0, true);
00058 connect(m_startPopup, SIGNAL(activated(int)), SLOT(setCurrentStartArrow(int)));
00059 connect(m_endPopup, SIGNAL(activated(int)), SLOT(setCurrentEndArrow(int)));
00060 }
00061
00062 KivioArrowHeadAction::~KivioArrowHeadAction()
00063 {
00064 delete m_startPopup;
00065 m_startPopup = 0;
00066 delete m_endPopup;
00067 m_endPopup = 0;
00068 delete m_popup;
00069 m_popup = 0;
00070 }
00071
00072 KPopupMenu* KivioArrowHeadAction::popupMenu() const
00073 {
00074 return m_popup;
00075 }
00076
00077 void KivioArrowHeadAction::popup( const QPoint& global )
00078 {
00079 popupMenu()->popup(global);
00080 }
00081
00082 int KivioArrowHeadAction::plug( QWidget* widget, int index)
00083 {
00084
00085 if (kapp && !kapp->authorizeKAction(name()))
00086 return -1;
00087 kdDebug(129) << "KAction::plug( " << widget << ", " << index << " )" << endl;
00088 if ( widget->inherits("QPopupMenu") )
00089 {
00090 QPopupMenu* menu = static_cast<QPopupMenu*>( widget );
00091 int id;
00092
00093 if ( hasIconSet() )
00094 id = menu->insertItem( iconSet(), text(), popupMenu(), -1, index );
00095 else
00096 id = menu->insertItem( kapp->iconLoader()->loadIcon(icon(), KIcon::Small),
00097 text(), popupMenu(), -1, index );
00098
00099 if ( !isEnabled() )
00100 menu->setItemEnabled( id, false );
00101
00102 addContainer( menu, id );
00103 connect( menu, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
00104
00105 return containerCount() - 1;
00106 }
00107 else if ( widget->inherits( "KToolBar" ) )
00108 {
00109 KToolBar *bar = static_cast<KToolBar *>( widget );
00110
00111 int id_ = KAction::getToolButtonID();
00112
00113 if ( icon().isEmpty() && !iconSet().isNull() )
00114 bar->insertButton( iconSet().pixmap(), id_, SIGNAL( clicked() ), this,
00115 SLOT( slotActivated() ), isEnabled(), plainText(),
00116 index );
00117 else
00118 {
00119 KInstance *instance;
00120
00121 if ( m_parentCollection )
00122 instance = m_parentCollection->instance();
00123 else
00124 instance = KGlobal::instance();
00125
00126 bar->insertButton( icon(), id_, SIGNAL( clicked() ), this,
00127 SLOT( slotActivated() ), isEnabled(), plainText(),
00128 index, instance );
00129 }
00130
00131 addContainer( bar, id_ );
00132
00133 if (!whatsThis().isEmpty())
00134 QWhatsThis::add( bar->getButton(id_), whatsThis() );
00135
00136 connect( bar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
00137
00138 bar->getButton(id_)->setPopup(popupMenu(), true );
00139
00140 return containerCount() - 1;
00141 }
00142 else if ( widget->inherits( "QMenuBar" ) )
00143 {
00144 QMenuBar *bar = static_cast<QMenuBar *>( widget );
00145
00146 int id;
00147
00148 id = bar->insertItem( text(), popupMenu(), -1, index );
00149
00150 if ( !isEnabled() )
00151 bar->setItemEnabled( id, false );
00152
00153 addContainer( bar, id );
00154 connect( bar, SIGNAL( destroyed() ), this, SLOT( slotDestroyed() ) );
00155
00156 return containerCount() - 1;
00157 }
00158
00159 return -1;
00160 }
00161
00162 void KivioArrowHeadAction::loadArrowHeads(KPopupMenu* popup)
00163 {
00164 QBitmap mask;
00165 QPixmap pixAll = Kivio::arrowHeadPixmap();
00166 QPixmap pix(pixAll.width(), 17);
00167 QPainter p(&pix, popup);
00168 int cindex = 0;
00169
00170
00171 popup->insertItem(i18n("no line end", "None"),cindex++);
00172
00173 for (int y = 0; y < pixAll.height(); y += 17 ) {
00174 pix.fill(white);
00175 p.drawPixmap(0, 0, pixAll, 0, y, pix.width(), pix.height());
00176 popup->insertItem(pix, cindex++);
00177 }
00178
00179 p.end();
00180 }
00181
00182 int KivioArrowHeadAction::currentStartArrow()
00183 {
00184 return m_currentStart;
00185 }
00186
00187 int KivioArrowHeadAction::currentEndArrow()
00188 {
00189 return m_currentEnd;
00190 }
00191
00192 void KivioArrowHeadAction::setCurrentStartArrow(int c)
00193 {
00194 m_startPopup->setItemChecked(m_currentStart, false);
00195 m_currentStart = c;
00196 m_startPopup->setItemChecked(m_currentStart, true);
00197
00198 if(m_emitSignals) {
00199 emit startChanged(m_currentStart);
00200 }
00201 }
00202
00203 void KivioArrowHeadAction::setCurrentEndArrow(int c)
00204 {
00205 m_endPopup->setItemChecked(m_currentEnd, false);
00206 m_currentEnd = c;
00207 m_endPopup->setItemChecked(m_currentEnd, true);
00208
00209 if(m_emitSignals) {
00210 emit endChanged(m_currentEnd);
00211 }
00212 }
00213
00214 #include "kivioarrowheadaction.moc"