kivio

kivio_layer_panel.cpp

00001 /*
00002  * Kivio - Visual Modelling and Flowcharting
00003  * Copyright (C) 2000-2001 theKompany.com & Dave Marotti
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  */
00019 #include "kivio_layer_panel.h"
00020 
00021 #include "kivio_view.h"
00022 #include "kivio_doc.h"
00023 #include "kivio_page.h"
00024 #include "kivio_layer.h"
00025 #include "kivio_factory.h"
00026 #include "kivio_command.h"
00027 #include "layerlisttooltip.h"
00028 
00029 #include <qheader.h>
00030 #include <qlayout.h>
00031 
00032 #include <ktoolbar.h>
00033 #include <klocale.h>
00034 #include <kaction.h>
00035 #include <kiconloader.h>
00036 #include <kinputdialog.h>
00037 
00038 KivioLayerItem::KivioLayerItem(QListView* parent, KivioLayer* d, int id)
00039 : QListViewItem(parent), data(d)
00040 {
00041   QString ids;
00042   QString space;
00043   ids.setNum(id);
00044   space.fill(' ',10-ids.length());
00045   ids.prepend(space);
00046   setText(5,ids);
00047 
00048   update();
00049 }
00050 
00051 KivioLayerItem::~KivioLayerItem()
00052 {
00053 }
00054 
00055 void KivioLayerItem::update()
00056 {
00057   setPixmap(0, data->visible() ? SmallIcon("layer_visible",KivioFactory::global()):SmallIcon("layer_novisible",KivioFactory::global()));
00058   setPixmap(1, data->printable() ? SmallIcon("layer_print",KivioFactory::global()):SmallIcon("layer_noprint",KivioFactory::global()));
00059   setPixmap(2, data->editable() ? SmallIcon("layer_editable",KivioFactory::global()):SmallIcon("layer_noeditable",KivioFactory::global()));
00060   setPixmap(3, data->connectable() ? SmallIcon("layer_connect",KivioFactory::global()):SmallIcon("layer_noconnect",KivioFactory::global()));
00061   setText(4,data->name());
00062 }
00063 
00064 
00065 /*********************************************************/
00066 KivioLayerPanel::KivioLayerPanel(KivioView* view, QWidget* parent, const char* name)
00067 : KivioLayerPanelBase(parent, name), m_pView(view)
00068 {
00069   list->header()->hide();
00070   list->addColumn(i18n("View"),15);
00071   list->addColumn(i18n("Print"),15);
00072   list->addColumn(i18n("Editable"),15);
00073   list->addColumn(i18n("Connect"),15);
00074   list->addColumn(i18n("Name"),-1);
00075   list->setSorting(5, false);
00076   list->installEventFilter(this);
00077 
00078   actNew = new KAction( i18n("New Layer"), BarIcon("layer_add",KivioFactory::global()), 0, this, SLOT(addItem()), this);
00079   actDel = new KAction( i18n("Remove Layer"), BarIcon("layer_remove",KivioFactory::global()), 0, this, SLOT(removeItem()), this);
00080   actRename = new KAction( i18n("Rename Layer"), BarIcon("item_rename",KivioFactory::global()), 0, this, SLOT(renameItem()), this);
00081   actUp = new KAction( i18n("Move Layer Up"), "up", 0, this, SLOT(upItem()), this);
00082   actDown = new KAction( i18n("Move Layer Down"), "down", 0, this, SLOT(downItem()), this);
00083 
00084   actNew->plug(bar);
00085   actDel->plug(bar);
00086   bar->insertSeparator();
00087   actRename->plug(bar);
00088   bar->insertSeparator();
00089   actUp->plug(bar);
00090   actDown->plug(bar);
00091 
00092   new Kivio::LayerListToolTip(list->viewport(), list);
00093 }
00094 
00095 KivioLayerPanel::~KivioLayerPanel()
00096 {
00097 }
00098 
00099 bool KivioLayerPanel::eventFilter(QObject* o, QEvent* ev)
00100 {
00101   if (o == list && (ev->type() == QEvent::LayoutHint || ev->type() == QEvent::Resize)) {
00102     int s = list->width() - 2*(list->margin() + list->lineWidth());
00103     s -= list->header()->sectionSize(0);
00104     s -= list->header()->sectionSize(1);
00105     s -= list->header()->sectionSize(2);
00106     s -= list->header()->sectionSize(3);
00107     list->header()->resizeSection(4,s);
00108     list->triggerUpdate();
00109   }
00110 
00111   return QWidget::eventFilter(o, ev);
00112 }
00113 
00114 void KivioLayerPanel::addItem()
00115 {
00116   KivioPage* page = m_pView->activePage();
00117   KivioLayer* layer = new KivioLayer(page);
00118 
00119   // Set the layer name to something a bit different. This isn't
00120   // guaranteed to be unique, but it's better than "Untitled"
00121   layer->setName(i18n("Layer %1").arg(id));
00122   page->addLayer(layer);
00123   page->setCurLayer(layer);
00124 
00125   m_pView->doc()->updateView(page);
00126 
00127   KivioAddLayerCommand * cmd = new KivioAddLayerCommand( i18n("Add Layer"), page, layer, id );
00128   m_pView->doc()->addCommand(cmd );
00129 
00130   KivioLayerItem* item = new KivioLayerItem(list, layer, id++);
00131   list->sort();
00132   list->setCurrentItem(item);
00133 }
00134 
00135 void KivioLayerPanel::removeItem()
00136 {
00137   KivioLayerItem* item = (KivioLayerItem*)list->currentItem();
00138   if (!item || (m_pView->activePage()->layers()->count() <= 1))
00139     return;
00140 
00141   itemActivated(item);
00142 
00143   m_pView->activePage()->removeCurrentLayer();
00144   m_pView->doc()->updateView(m_pView->activePage());
00145 
00146   delete item;
00147 }
00148 
00149 void KivioLayerPanel::renameItem()
00150 {
00151   KivioLayerItem* i = (KivioLayerItem*)list->currentItem();
00152   if (!i)
00153     return;
00154 
00155   KivioLayer* layer = i->data;
00156   QString oldText = layer->name();
00157 
00158   bool ok=false;
00159   QString newName = KInputDialog::getText(i18n("Rename Layer"),
00160     i18n("Layer name:"), oldText, &ok, this);
00161 
00162   if (ok) {
00163     layer->setName(newName);
00164     KivioRenameLayerCommand *cmd = new KivioRenameLayerCommand( i18n("Rename Layer"), layer, oldText, newName);
00165     m_pView->doc()->addCommand( cmd );
00166   }
00167 
00168   i->update();
00169 }
00170 
00171 void KivioLayerPanel::upItem()
00172 {
00173   KivioLayerItem* item = (KivioLayerItem*)list->currentItem();
00174   if (!item)
00175     return;
00176 
00177   QListViewItem* above = item->itemAbove();
00178   if (!above)
00179     return;
00180 
00181   KivioPage* page = m_pView->activePage();
00182   KivioLayer* layer = item->data;
00183 
00184   int pos = page->layers()->find(layer);
00185   // It's already the top layer... return
00186   if(pos == (static_cast<int>(page->layers()->count()) - 1))
00187     return;
00188 
00189   layer = page->layers()->take();
00190   if( !layer )
00191     return;
00192 
00193   page->layers()->insert(pos + 1, layer);
00194 
00195   QString t = above->text(5);
00196   above->setText(5,item->text(5));
00197   item->setText(5,t);
00198 
00199   list->sort();
00200 
00201   updateButtons(item);
00202 
00203   page->setCurLayer(layer);
00204   m_pView->doc()->updateView(page);
00205 }
00206 
00207 void KivioLayerPanel::downItem()
00208 {
00209   KivioLayerItem* item = (KivioLayerItem*)list->currentItem();
00210   if (!item)
00211     return;
00212 
00213   QListViewItem* below = item->itemBelow();
00214   if (!below)
00215     return;
00216 
00217   KivioPage* page = m_pView->activePage();
00218   KivioLayer* layer = item->data;
00219 
00220   int pos = page->layers()->find(layer);
00221   // It's already the bottom layer... return
00222   if (pos == 0)
00223     return;
00224 
00225   layer = page->layers()->take();
00226   if( !layer )
00227     return;
00228 
00229   page->layers()->insert(pos - 1, layer);
00230 
00231   QString t = below->text(5);
00232   below->setText(5,item->text(5));
00233   item->setText(5,t);
00234 
00235   list->sort();
00236 
00237   updateButtons(item);
00238 
00239   page->setCurLayer(layer);
00240   m_pView->doc()->updateView(page);
00241 }
00242 
00243 void KivioLayerPanel::updateButtons(QListViewItem* i)
00244 {
00245   if (!i) {
00246     actDel->setEnabled(false);
00247     actRename->setEnabled(false);
00248     actUp->setEnabled(false);
00249     actDown->setEnabled(false);
00250   } else {
00251     if(m_pView->activePage()->layers()->count() > 1) {
00252       actDel->setEnabled(true);
00253     } else {
00254       actDel->setEnabled(false);
00255     }
00256 
00257     actRename->setEnabled(true);
00258     actUp->setEnabled(i->itemAbove());
00259     actDown->setEnabled(i->itemBelow());
00260   }
00261 }
00262 
00263 void KivioLayerPanel::itemClicked(QListViewItem* i, const QPoint&, int c)
00264 {
00265   if(!i) {
00266     return;
00267   }
00268 
00269   KivioLayerItem* vi = static_cast<KivioLayerItem*>(i);
00270   KivioLayer* layer = vi->data;
00271 
00272   switch(c)
00273   {
00274     case 0:
00275       layer->setVisible(!layer->visible());
00276       break;
00277     case 1:
00278       layer->setPrintable(!layer->printable());
00279       break;
00280     case 2:
00281       layer->setEditable(!layer->editable());
00282       break;
00283     case 3:
00284       layer->setConnectable(!layer->connectable());
00285       break;
00286     default:
00287       break;
00288   }
00289 
00290   vi->update();
00291   m_pView->doc()->updateView(m_pView->activePage());
00292 }
00293 
00294 void KivioLayerPanel::itemActivated(QListViewItem* i)
00295 {
00296   if (!i)
00297     return;
00298 
00299   KivioLayerItem* vi = (KivioLayerItem*)i;
00300 
00301   KivioPage* page = m_pView->activePage();
00302   page->setCurLayer(vi->data);
00303 
00304   // Switching layers unselects all stencils.  We REALLY don't want multiple
00305   // selections between layers.  If you don't do this, and multiple stencils
00306   // on various layers do occur and a group operation occurs, I have NO idea
00307   // what will happen since I didn't code it with multi-layer-group in mind...
00308   // or did I?  Anyway, if you get rid of this, you have to go check all the
00309   // multi-select operations.
00310   page->unselectAllStencils();
00311   m_pView->doc()->updateView(page);
00312 
00313   updateButtons(i);
00314 }
00315 
00316 void KivioLayerPanel::reset()
00317 {
00318   id = 1;
00319   list->clear();
00320 
00321   KivioPage* page = m_pView->activePage();
00322   KivioLayer* layer = page->firstLayer();
00323   QListViewItem* ci = 0;
00324 
00325   while (layer) {
00326     KivioLayerItem* i = new KivioLayerItem(list, layer, id++);
00327     if (layer == page->curLayer())
00328       ci = i;
00329     layer = page->nextLayer();
00330   }
00331 
00332   if(!ci) {
00333     ci = list->firstChild();
00334   }
00335 
00336   list->setSelected(ci, true);
00337   list->sort();
00338   updateButtons(list->currentItem());
00339 }
00340 
00341 #include "kivio_layer_panel.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys