kivio

kivio_icon_view.cpp
1/*
2 * Kivio - Visual Modelling and Flowcharting
3 * Copyright (C) 2000-2001 theKompany.com & Dave Marotti
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19#include "kivio_icon_view.h"
20
21#include "kivio_stencil_spawner.h"
22#include "kivio_stencil_spawner_set.h"
23#include "kivio_stencil_spawner_info.h"
24#include "kivio_spawner_drag.h"
25#include "kivio_common.h"
26#include "kivioglobal.h"
27#include "kivio_stencil.h"
28
29#include <tqbrush.h>
30#include <tqcursor.h>
31#include <tqpalette.h>
32#include <tdelocale.h>
33#include <kdebug.h>
34#include <tdeglobalsettings.h>
35
36KivioStencilSpawner* KivioIconView::m_pCurDrag = 0L;
37TQPtrList<KivioIconView> KivioIconView::objList;
38
39/**********************************************************************
40 *
41 * KivioIconViewItem
42 *
43 **********************************************************************/
44KivioIconViewItem::KivioIconViewItem( TQIconView *parent )
45 : TQIconViewItem( parent )
46{
47 m_pSpawner = NULL;
48 setText("stencil");
49}
50
51KivioIconViewItem::~KivioIconViewItem()
52{
53 m_pSpawner = NULL;
54}
55
56void KivioIconViewItem::setStencilSpawner( KivioStencilSpawner *pSpawn )
57{
58 KivioStencilSpawnerInfo *pInfo;
59
60 m_pSpawner = pSpawn;
61
62 if( !m_pSpawner )
63 {
64 setText( i18n("untitled stencil", "Untitled") );
65 }
66 else
67 {
68 pInfo = m_pSpawner->info();
69 setText( pInfo->title() );
70 KivioStencil* stencil = m_pSpawner->newStencil();
71 setPixmap(Kivio::generatePixmapFromStencil(32, 32, stencil));
72 delete stencil;
73 }
74
75}
76
77bool KivioIconViewItem::acceptDrop( const TQMimeSource * ) const
78{
79 return false;
80}
81
82
83/**********************************************************************
84 *
85 * KivioIconView
86 *
87 **********************************************************************/
88KivioIconView::KivioIconView( bool _readWrite,TQWidget *parent, const char *name )
89: TQIconView( parent, name )
90{
91 m_pSpawnerSet = NULL;
92 m_pCurDrag = NULL;
93 isReadWrite=_readWrite;
94 objList.append(this);
95
96 setGridX( 64 );
97 setGridY( 64 );
98 setResizeMode( Adjust );
99 setWordWrapIconText(true);
100 setHScrollBarMode( AlwaysOff );
101 setVScrollBarMode( Auto );
102 setAutoArrange(true);
103 setSorting(true);
104
105 setItemsMovable(false);
106
107 setArrangement(LeftToRight);
108 setAcceptDrops(false);
109 viewport()->setAcceptDrops(false);
110 if(isReadWrite) {
111 connect(this, TQ_SIGNAL(doubleClicked(TQIconViewItem*)), this, TQ_SLOT(slotDoubleClicked(TQIconViewItem*)));
112 connect(this, TQ_SIGNAL(clicked(TQIconViewItem*)), this, TQ_SLOT(slotClicked(TQIconViewItem*)));
113 }
114}
115
116KivioIconView::~KivioIconView()
117{
118 objList.remove(this);
119 m_pCurDrag = NULL;
120}
121
122void KivioIconView::setStencilSpawnerSet( KivioStencilSpawnerSet *pSet )
123{
124 m_pSpawnerSet = pSet;
125 m_pCurDrag = NULL;
126
127 KivioStencilSpawner *pSpawner;
128 KivioIconViewItem *pItem;
129
130 pSpawner = pSet->spawners()->first();
131 while( pSpawner )
132 {
133 pItem = new KivioIconViewItem( this );
134 pItem->setKey(pSpawner->info()->title());
135 pItem->setStencilSpawner(pSpawner);
136
137 pSpawner = pSet->spawners()->next();
138 }
139}
140
141TQDragObject *KivioIconView::dragObject()
142{
143 if( !currentItem() || !isReadWrite)
144 return 0;
145
146 TQPoint orig = viewportToContents( viewport()->mapFromGlobal( TQCursor::pos() ) );
147 KivioSpawnerDrag *drag = new KivioSpawnerDrag( this, viewport() );
148
149 const char*null_pix[]={
150 "1 1 1 1",
151 "# c None",
152 "#"};
153 drag->setPixmap(null_pix);
154
155 KivioIconViewItem *item = (KivioIconViewItem *)currentItem();
156
157 TQIconDragItem id;
158 TQString full;
159 full = item->spawner()->set()->dir() + "/" + item->spawner()->info()->title();
160 id.setData( TQCString(full.ascii()));
161
162 drag->append( id,
163 TQRect( item->pixmapRect(false).x() - orig.x(),
164 item->pixmapRect(false).y() - orig.y(),
165 item->pixmapRect().width(),
166 item->pixmapRect().height() ),
167 TQRect( item->textRect(false).x() - orig.x(),
168 item->textRect(false).y() - orig.y(),
169 item->textRect().width(),
170 item->textRect().height() ),
171 *(item->spawner()) );
172
173
174 // Set the current dragged KivioStencilSpawner for use
175 // when the drop occurs. I don't actually encode the
176 // data because it's a pain in the ass and I don't understand
177 // how to do it. So I store a pointer here and clear
178 // it on the drop.
179 m_pCurDrag = item->spawner();
180
181 return drag;
182}
183
184void KivioIconView::clearCurrentDrag()
185{
186 m_pCurDrag = 0;
187}
188
189
190void KivioIconView::slotDoubleClicked( TQIconViewItem *pTQtItem )
191{
192 KivioIconViewItem* pItem = dynamic_cast<KivioIconViewItem *>(pTQtItem);
193
194 if( !pItem )
195 {
196 kdDebug(43000) << "KivioIconView::slotDoubleClicked() - Clicked item is not a KivioIconViewItem!" << endl;
197 return;
198 }
199
200 KivioStencilSpawner* pSpawner = pItem->spawner();
201
202 emit createNewStencil(pSpawner);
203}
204
205void KivioIconView::slotClicked(TQIconViewItem* item)
206{
207 KivioIconViewItem* stencilItem = dynamic_cast<KivioIconViewItem *>(item);
208
209 if(!stencilItem)
210 {
211 kdDebug(43000) << "KivioIconView::slotClicked() - Clicked item is not a KivioIconViewItem!" << endl;
212 return;
213 }
214
215 KivioStencilSpawner* spawner = stencilItem->spawner();
216 emit stencilSelected(spawner);
217}
218
219#include "kivio_icon_view.moc"