kivio

kivio_layer_panel.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_layer_panel.h"
20
21#include "kivio_view.h"
22#include "kivio_doc.h"
23#include "kivio_page.h"
24#include "kivio_layer.h"
25#include "kivio_factory.h"
26#include "kivio_command.h"
27#include "layerlisttooltip.h"
28
29#include <tqheader.h>
30#include <tqlayout.h>
31
32#include <tdetoolbar.h>
33#include <tdelocale.h>
34#include <tdeaction.h>
35#include <kiconloader.h>
36#include <kinputdialog.h>
37
38KivioLayerItem::KivioLayerItem(TQListView* parent, KivioLayer* d, int id)
39: TQListViewItem(parent), data(d)
40{
41 TQString ids;
42 TQString space;
43 ids.setNum(id);
44 space.fill(' ',10-ids.length());
45 ids.prepend(space);
46 setText(5,ids);
47
48 update();
49}
50
51KivioLayerItem::~KivioLayerItem()
52{
53}
54
55void KivioLayerItem::update()
56{
57 setPixmap(0, data->visible() ? SmallIcon("layer_visible",KivioFactory::global()):SmallIcon("layer_novisible",KivioFactory::global()));
58 setPixmap(1, data->printable() ? SmallIcon("layer_print",KivioFactory::global()):SmallIcon("layer_noprint",KivioFactory::global()));
59 setPixmap(2, data->editable() ? SmallIcon("layer_editable",KivioFactory::global()):SmallIcon("layer_noeditable",KivioFactory::global()));
60 setPixmap(3, data->connectable() ? SmallIcon("layer_connect",KivioFactory::global()):SmallIcon("layer_noconnect",KivioFactory::global()));
61 setText(4,data->name());
62}
63
64
65/*********************************************************/
66KivioLayerPanel::KivioLayerPanel(KivioView* view, TQWidget* parent, const char* name)
67: KivioLayerPanelBase(parent, name), m_pView(view)
68{
69 list->header()->hide();
70 list->addColumn(i18n("View"),15);
71 list->addColumn(i18n("Print"),15);
72 list->addColumn(i18n("Editable"),15);
73 list->addColumn(i18n("Connect"),15);
74 list->addColumn(i18n("Name"),-1);
75 list->setSorting(5, false);
76 list->installEventFilter(this);
77
78 actNew = new TDEAction( i18n("New Layer"), BarIcon("layer_add",KivioFactory::global()), 0, this, TQ_SLOT(addItem()), this);
79 actDel = new TDEAction( i18n("Remove Layer"), BarIcon("layer_remove",KivioFactory::global()), 0, this, TQ_SLOT(removeItem()), this);
80 actRename = new TDEAction( i18n("Rename Layer"), BarIcon("item_rename",KivioFactory::global()), 0, this, TQ_SLOT(renameItem()), this);
81 actUp = new TDEAction( i18n("Move Layer Up"), "go-up", 0, this, TQ_SLOT(upItem()), this);
82 actDown = new TDEAction( i18n("Move Layer Down"), "go-down", 0, this, TQ_SLOT(downItem()), this);
83
84 actNew->plug(bar);
85 actDel->plug(bar);
86 bar->insertSeparator();
87 actRename->plug(bar);
88 bar->insertSeparator();
89 actUp->plug(bar);
90 actDown->plug(bar);
91
92 new Kivio::LayerListToolTip(list->viewport(), list);
93}
94
95KivioLayerPanel::~KivioLayerPanel()
96{
97}
98
99bool KivioLayerPanel::eventFilter(TQObject* o, TQEvent* ev)
100{
101 if (o == list && (ev->type() == TQEvent::LayoutHint || ev->type() == TQEvent::Resize)) {
102 int s = list->width() - 2*(list->margin() + list->lineWidth());
103 s -= list->header()->sectionSize(0);
104 s -= list->header()->sectionSize(1);
105 s -= list->header()->sectionSize(2);
106 s -= list->header()->sectionSize(3);
107 list->header()->resizeSection(4,s);
108 list->triggerUpdate();
109 }
110
111 return TQWidget::eventFilter(o, ev);
112}
113
114void KivioLayerPanel::addItem()
115{
116 KivioPage* page = m_pView->activePage();
117 KivioLayer* layer = new KivioLayer(page);
118
119 // Set the layer name to something a bit different. This isn't
120 // guaranteed to be unique, but it's better than "Untitled"
121 layer->setName(i18n("Layer %1").arg(id));
122 page->addLayer(layer);
123 page->setCurLayer(layer);
124
125 m_pView->doc()->updateView(page);
126
127 KivioAddLayerCommand * cmd = new KivioAddLayerCommand( i18n("Add Layer"), page, layer, id );
128 m_pView->doc()->addCommand(cmd );
129
130 KivioLayerItem* item = new KivioLayerItem(list, layer, id++);
131 list->sort();
132 list->setCurrentItem(item);
133}
134
135void KivioLayerPanel::removeItem()
136{
137 KivioLayerItem* item = (KivioLayerItem*)list->currentItem();
138 if (!item || (m_pView->activePage()->layers()->count() <= 1))
139 return;
140
141 itemActivated(item);
142
143 m_pView->activePage()->removeCurrentLayer();
144 m_pView->doc()->updateView(m_pView->activePage());
145
146 delete item;
147}
148
149void KivioLayerPanel::renameItem()
150{
151 KivioLayerItem* i = (KivioLayerItem*)list->currentItem();
152 if (!i)
153 return;
154
155 KivioLayer* layer = i->data;
156 TQString oldText = layer->name();
157
158 bool ok=false;
159 TQString newName = KInputDialog::getText(i18n("Rename Layer"),
160 i18n("Layer name:"), oldText, &ok, this);
161
162 if (ok) {
163 layer->setName(newName);
164 KivioRenameLayerCommand *cmd = new KivioRenameLayerCommand( i18n("Rename Layer"), layer, oldText, newName);
165 m_pView->doc()->addCommand( cmd );
166 }
167
168 i->update();
169}
170
171void KivioLayerPanel::upItem()
172{
173 KivioLayerItem* item = (KivioLayerItem*)list->currentItem();
174 if (!item)
175 return;
176
177 TQListViewItem* above = item->itemAbove();
178 if (!above)
179 return;
180
181 KivioPage* page = m_pView->activePage();
182 KivioLayer* layer = item->data;
183
184 int pos = page->layers()->find(layer);
185 // It's already the top layer... return
186 if(pos == (static_cast<int>(page->layers()->count()) - 1))
187 return;
188
189 layer = page->layers()->take();
190 if( !layer )
191 return;
192
193 page->layers()->insert(pos + 1, layer);
194
195 TQString t = above->text(5);
196 above->setText(5,item->text(5));
197 item->setText(5,t);
198
199 list->sort();
200
201 updateButtons(item);
202
203 page->setCurLayer(layer);
204 m_pView->doc()->updateView(page);
205}
206
207void KivioLayerPanel::downItem()
208{
209 KivioLayerItem* item = (KivioLayerItem*)list->currentItem();
210 if (!item)
211 return;
212
213 TQListViewItem* below = item->itemBelow();
214 if (!below)
215 return;
216
217 KivioPage* page = m_pView->activePage();
218 KivioLayer* layer = item->data;
219
220 int pos = page->layers()->find(layer);
221 // It's already the bottom layer... return
222 if (pos == 0)
223 return;
224
225 layer = page->layers()->take();
226 if( !layer )
227 return;
228
229 page->layers()->insert(pos - 1, layer);
230
231 TQString t = below->text(5);
232 below->setText(5,item->text(5));
233 item->setText(5,t);
234
235 list->sort();
236
237 updateButtons(item);
238
239 page->setCurLayer(layer);
240 m_pView->doc()->updateView(page);
241}
242
243void KivioLayerPanel::updateButtons(TQListViewItem* i)
244{
245 if (!i) {
246 actDel->setEnabled(false);
247 actRename->setEnabled(false);
248 actUp->setEnabled(false);
249 actDown->setEnabled(false);
250 } else {
251 if(m_pView->activePage()->layers()->count() > 1) {
252 actDel->setEnabled(true);
253 } else {
254 actDel->setEnabled(false);
255 }
256
257 actRename->setEnabled(true);
258 actUp->setEnabled(i->itemAbove());
259 actDown->setEnabled(i->itemBelow());
260 }
261}
262
263void KivioLayerPanel::itemClicked(TQListViewItem* i, const TQPoint&, int c)
264{
265 if(!i) {
266 return;
267 }
268
269 KivioLayerItem* vi = static_cast<KivioLayerItem*>(i);
270 KivioLayer* layer = vi->data;
271
272 switch(c)
273 {
274 case 0:
275 layer->setVisible(!layer->visible());
276 break;
277 case 1:
278 layer->setPrintable(!layer->printable());
279 break;
280 case 2:
281 layer->setEditable(!layer->editable());
282 break;
283 case 3:
284 layer->setConnectable(!layer->connectable());
285 break;
286 default:
287 break;
288 }
289
290 vi->update();
291 m_pView->doc()->updateView(m_pView->activePage());
292}
293
294void KivioLayerPanel::itemActivated(TQListViewItem* i)
295{
296 if (!i)
297 return;
298
299 KivioLayerItem* vi = (KivioLayerItem*)i;
300
301 KivioPage* page = m_pView->activePage();
302 page->setCurLayer(vi->data);
303
304 // Switching layers unselects all stencils. We REALLY don't want multiple
305 // selections between layers. If you don't do this, and multiple stencils
306 // on various layers do occur and a group operation occurs, I have NO idea
307 // what will happen since I didn't code it with multi-layer-group in mind...
308 // or did I? Anyway, if you get rid of this, you have to go check all the
309 // multi-select operations.
310 page->unselectAllStencils();
311 m_pView->doc()->updateView(page);
312
313 updateButtons(i);
314}
315
316void KivioLayerPanel::reset()
317{
318 id = 1;
319 list->clear();
320
321 KivioPage* page = m_pView->activePage();
322 KivioLayer* layer = page->firstLayer();
323 TQListViewItem* ci = 0;
324
325 while (layer) {
326 KivioLayerItem* i = new KivioLayerItem(list, layer, id++);
327 if (layer == page->curLayer())
328 ci = i;
329 layer = page->nextLayer();
330 }
331
332 if(!ci) {
333 ci = list->firstChild();
334 }
335
336 list->setSelected(ci, true);
337 list->sort();
338 updateButtons(list->currentItem());
339}
340
341#include "kivio_layer_panel.moc"
Shows the tooltips in the layer list.