Back to index

scribus-ng  1.3.4.dfsg+svn20071115
bookmwin.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 /***************************************************************************
00008                           bookmwin.cpp  -  description
00009                              -------------------
00010     begin                : Mon Feb 11 2002
00011     copyright            : (C) 2002 by Franz Schmid
00012     email                : Franz.Schmid@altmuehlnet.de
00013  ***************************************************************************/
00014 
00015 /***************************************************************************
00016  *                                                                         *
00017  *   This program is free software; you can redistribute it and/or modify  *
00018  *   it under the terms of the GNU General Public License as published by  *
00019  *   the Free Software Foundation; either version 2 of the License, or     *
00020  *   (at your option) any later version.                                   *
00021  *                                                                         *
00022  ***************************************************************************/
00023 
00024 #include "bookmwin.h"
00025 #include "bookmwin.moc"
00026 #include <qpixmap.h>
00027 #include <qdragobject.h>
00028 #include <qpopupmenu.h>
00029 #include <qcursor.h>
00030 #include <qheader.h>
00031 
00032 extern QPixmap loadIcon(QString nam);
00033 
00034 BookMItem::BookMItem(QListViewItem* parent, struct ScribusDoc::BookMa *Bm) : QListViewItem(parent)
00035 {
00036        SetUp(Bm);
00037 }
00038 
00039 BookMItem::BookMItem(QListViewItem* parent, QListViewItem* after, struct ScribusDoc::BookMa *Bm) : QListViewItem(parent, after)
00040 {
00041        SetUp(Bm);
00042 }
00043 
00044 BookMItem::BookMItem(QListView* parent, QListViewItem* after, struct ScribusDoc::BookMa *Bm) : QListViewItem(parent, after)
00045 {
00046        SetUp(Bm);
00047 }
00048 
00049 BookMItem::BookMItem(QListView* parent, struct ScribusDoc::BookMa *Bm) : QListViewItem(parent)
00050 {
00051        SetUp(Bm);
00052 }
00053 
00054 BookMItem::BookMItem(QListView* parent, QListViewItem* after, int nr, PageItem* PObject) : QListViewItem(parent, after)
00055 {
00056        ItemNr = nr;
00057        PageObject = PObject;
00058        PdfObj = 0;
00059        Action = "";
00060        First = 0;
00061        Last = 0;
00062        Prev = 0;
00063        Next = 0;
00064        Pare = 0;
00065 }
00066 
00067 BookMItem::BookMItem(QListView* parent, int nr, PageItem* PObject) : QListViewItem(parent)
00068 {
00069        ItemNr = nr;
00070        PageObject = PObject;
00071        PdfObj = 0;
00072        Action = "";
00073        First = 0;
00074        Last = 0;
00075        Prev = 0;
00076        Next = 0;
00077        Pare = 0;
00078 }
00079 
00080 void BookMItem::SetUp(struct ScribusDoc::BookMa *Bm)
00081 {
00082        ItemNr = Bm->ItemNr;
00083        PdfObj = 0;
00084        Action = Bm->Aktion;
00085        First = Bm->First;
00086        Last = Bm->Last;
00087        Prev = Bm->Prev;
00088        Next = Bm->Next;
00089        Pare = Bm->Parent;
00090        Titel = Bm->Title;
00091        PageObject = Bm->PageObject;
00092        setText(0, Bm->Text);
00093 }
00094 
00095 QString BookMItem::key(int, bool) const
00096 {
00097        QString tmp;
00098        tmp.sprintf("%5d", ItemNr);
00099        return tmp;
00100 }
00101 
00102 
00103 BookMView::BookMView(QWidget* parent) : QListView(parent)
00104 {
00105        NrItems = 0;
00106        Mpressed = false;
00107        DraggedI = 0;
00108        First = 1;
00109        Last = 0;
00110        setAcceptDrops(true);
00111        viewport()->setAcceptDrops(true);
00112        setRootIsDecorated(true);
00113        idBookMarkCol=addColumn("Bookmarks");
00114        setResizeMode(QListView::AllColumns);
00115        setSelectionMode(QListView::NoSelection);
00116        setSorting(-1,1);
00117        languageChange();
00118 }
00119 
00120 void BookMView::AddPageItem(PageItem* ite)
00121 {
00122        QString bm = "";
00123        QString bm2 = "";
00124        QString cc;
00125        for (int d = 0; d < ite->itemText.length(); ++d)
00126        {
00127               cc = ite->itemText.text(d);
00128               if ((cc == QChar(13)) || (cc == QChar(10)))
00129                      break;
00130               if (cc == QChar(29))
00131                      cc = " ";
00132               if ((cc == "(") || (cc == ")") || (cc == "\\"))
00133                      bm2 += "\\";
00134               bm += cc;
00135               bm2 += cc;
00136        }
00137        AddItem(bm, bm2, ite);
00138        Last = NrItems;
00139 }
00140 
00141 void BookMView::contentsMouseReleaseEvent(QMouseEvent *e)
00142 {
00143        if (Mpressed)
00144        {
00145               QListViewItem *i = itemAt(contentsToViewport(e->pos()));
00146               QPoint p = contentsToViewport(e->pos());
00147               if ( i ) 
00148               {
00149                      if ( p.x() > header()->cellPos( header()->mapToActual( 0 ) ) + treeStepSize() * ( i->depth() + ( rootIsDecorated() ? 1 : 0) ) + itemMargin() ||
00150                                    p.x() < header()->cellPos( header()->mapToActual( 0 ) ) ) 
00151                      {
00152                             BookMItem *ip;
00153                             ip = (BookMItem*)i;
00154                             emit SelectElement(ip->PageObject);
00155                      }
00156               }
00157        }
00158        Mpressed = false;
00159 }
00160 
00161 void BookMView::contentsMousePressEvent(QMouseEvent* e)
00162 {
00163        QListView::contentsMousePressEvent(e);
00164        QListViewItem *i = itemAt(contentsToViewport(e->pos()));
00165        if (i)
00166        {
00167               Mpos = e->pos();
00168               Mpressed = true;
00169        }
00170 }
00171 
00172 void BookMView::contentsMouseMoveEvent(QMouseEvent* e)
00173 {
00174        if ((Mpressed) && ((Mpos - e->pos()).manhattanLength() > 4))
00175        {
00176               Mpressed = false;
00177               QListViewItem *i = itemAt(contentsToViewport(Mpos));
00178               if (i)
00179               {
00180                      DraggedI = (BookMItem*)i;
00181                      QDragObject *dr = new QTextDrag(i->text(0), this, "BMD");
00182                      dr->drag();
00183               }
00184        }
00185 }
00186 
00187 void BookMView::contentsDropEvent(QDropEvent *e)
00188 {
00189        QString text;
00190        if (!QTextDrag::decode(e, text))
00191        {
00192               e->ignore();
00193               return;
00194     }
00195        int ins, mov, mret, can;
00196        QListViewItem *pp;
00197        QListViewItem *lv;
00198        BookMItem *ip;
00199        BookMItem *ip2;
00200        BookMItem *ite;
00201        QListViewItem *item = itemAt(contentsToViewport(e->pos()));
00202        BookMItem *Bite = (BookMItem*)item;
00203        if ((item) && (DraggedI != 0))
00204        {
00205               if ((BookMItem*)item != DraggedI)
00206               {
00207                      pp = item->parent();
00208                      while (pp)
00209                      {
00210                             if (pp == DraggedI)
00211                                    return;
00212                             pp = pp->parent();
00213                      }
00214                      QPopupMenu *pmenu = new QPopupMenu();
00215                      mov = pmenu->insertItem( tr("Move Bookmark"));
00216                      ins = pmenu->insertItem( tr("Insert Bookmark"));
00217                      can = pmenu->insertItem( tr("Cancel"));
00218                      mret = pmenu->exec(QCursor::pos());
00219                      if (mret != can)
00220                      {
00221                             if (mret == ins)
00222                             {
00223                                    lv = DraggedI->parent();
00224                                    lv = lv ? lv->firstChild() : firstChild();
00225                                    if (!DraggedI->Prev == 0)
00226                                    {
00227                                           while (lv)
00228                                           {
00229                                                  ip = (BookMItem*)lv;
00230                                                  if (ip->ItemNr == DraggedI->Prev)
00231                                                         break;
00232                                                  lv = lv->nextSibling();
00233                                           }
00234                                           ip = (BookMItem*)lv;
00235                                           lv = DraggedI->nextSibling();
00236                                           if (lv)
00237                                           {
00238                                                  ip2 = (BookMItem*)lv;
00239                                                  ip2->Prev = ip->ItemNr;
00240                                                  ip->Next = ip2->ItemNr;
00241                                           }
00242                                           else
00243                                                  ip->Next = 0;
00244                                    }
00245                                    lv = DraggedI->parent();
00246                                    item->insertItem(DraggedI);
00247                                    lv ? lv = lv->firstChild() : lv = firstChild();
00248                                    if (DraggedI->Prev == 0)
00249                                    {
00250                                           ip = (BookMItem*)lv;
00251                                           if (ip)
00252                                                  ip->Prev = 0;
00253                                    }
00254                                    QListViewItemIterator it(this);
00255                                    for ( ; it.current(); ++it)
00256                                    {
00257                                           ite = (BookMItem*)it.current();
00258                                           if (ite->ItemNr == DraggedI->ItemNr)
00259                                                  DraggedI = ite;
00260                                    }
00261                                    DraggedI->Pare = Bite->ItemNr;
00262                                    ip = (BookMItem*)item->firstChild();
00263                                    Bite->First = ip->ItemNr;
00264                                    lv = item->firstChild();
00265                                    while (lv)
00266                                    {
00267                                           if (!lv->nextSibling())
00268                                           {
00269                                                  ip = (BookMItem*)lv;
00270                                                  Bite->Last = ip->ItemNr;
00271                                                  break;
00272                                           }
00273                                           lv = lv->nextSibling();
00274                                    }
00275                                    if (DraggedI->nextSibling())
00276                                    {
00277                                           ip = (BookMItem*)DraggedI->nextSibling();
00278                                           DraggedI->Next = ip->ItemNr;
00279                                           ip->Prev = DraggedI->ItemNr;
00280                                    }
00281                                    else
00282                                           DraggedI->Next = 0;
00283                                    if (DraggedI != item->firstChild())
00284                                    {
00285                                           ip = (BookMItem*)DraggedI->itemAbove();
00286                                           DraggedI->Prev = ip->ItemNr;
00287                                           ip->Next = DraggedI->ItemNr;
00288                                    }
00289                                    else
00290                                           DraggedI->Prev = 0;
00291                                    emit changed();
00292                             }
00293                             if (mret == mov)
00294                             {
00295                                    lv = DraggedI->parent();
00296                                    lv ? lv = lv->firstChild() : lv = firstChild();
00297                                    if (DraggedI->Prev == 0)
00298                                    {
00299                                           ip = (BookMItem*)lv->nextSibling();
00300                                           if (ip)
00301                                                  ip->Prev = 0;
00302                                    }
00303                                    else
00304                                    {
00305                                           while (lv)
00306                                           {
00307                                                  ip = (BookMItem*)lv;
00308                                                  if (ip->ItemNr == DraggedI->Prev)
00309                                                         break;
00310                                                  lv = lv->nextSibling();
00311                                           }
00312                                    }
00313                                    ip = (BookMItem*)lv;
00314                                    lv = DraggedI->nextSibling();
00315                                    if (lv)
00316                                    {
00317                                           ip2 = (BookMItem*)lv;
00318                                           ip2->Prev = ip->ItemNr;
00319                                           ip->Next = ip2->ItemNr;
00320                                    }
00321                                    else
00322                                           ip->Next = 0;
00323                                    lv = DraggedI->parent();
00324                                    DraggedI->moveItem(item);
00325                                    lv ? lv = lv->firstChild() : lv = firstChild();
00326                                    if (DraggedI->Prev == 0)
00327                                    {
00328                                           ip = (BookMItem*)lv;
00329                                           if (ip)
00330                                                  ip->Prev = 0;
00331                                    }
00332                                    QListViewItemIterator it2(this);
00333                                    for ( ; it2.current(); ++it2)
00334                                    {
00335                                           ite = (BookMItem*)it2.current();
00336                                           if (ite->ItemNr == DraggedI->ItemNr)
00337                                                  DraggedI = ite;
00338                                    }
00339                                    ip = (BookMItem*)item;
00340                                    DraggedI->Pare = ip->Pare;
00341                                    DraggedI->Prev = ip->ItemNr;
00342                                    ip->Next = DraggedI->ItemNr;
00343                                    if (DraggedI->nextSibling())
00344                                    {
00345                                           ip = (BookMItem*)DraggedI->nextSibling();
00346                                           DraggedI->Next = ip->ItemNr;
00347                                           ip->Prev = DraggedI->ItemNr;
00348                                    }
00349                                    else
00350                                           DraggedI->Next = 0;
00351                                    lv = item->parent();
00352                                    if (lv)
00353                                    {
00354                                           ip = (BookMItem*)lv->firstChild();
00355                                           ip2 = (BookMItem*)lv;
00356                                           ip2->First = ip->ItemNr;
00357                                           lv = lv->firstChild();
00358                                           while (lv)
00359                                           {
00360                                                  if (!lv->nextSibling())
00361                                                  {
00362                                                         ip = (BookMItem*)lv;
00363                                                         ip2->Last = ip->ItemNr;
00364                                                         break;
00365                                                  }
00366                                                  lv = lv->nextSibling();
00367                                           }
00368                                    }
00369                                    emit changed();
00370                             }
00371                             emit MarkMoved();
00372                      }
00373                      delete pmenu; 
00374                      DraggedI = 0;
00375               }
00376        }
00377 }
00378 
00379 void BookMView::contentsDragMoveEvent(QDragMoveEvent *e)
00380 {
00381        QString text;
00382        if (!QTextDrag::decode(e, text))
00383        {
00384               e->ignore();
00385               return;
00386     }
00387        QListViewItem *item = itemAt(contentsToViewport(e->pos()));
00388        if (item)
00389        {
00390               setSelected(item, true);
00391               e->accept();
00392        }
00393        else
00394               e->ignore();
00395 }
00396 
00397 void BookMView::AddItem(QString text, QString Tit, PageItem *PageObject)
00398 {
00399        QListViewItem *lv = firstChild();
00400        while (lv)
00401        {
00402               if (!lv->nextSibling())
00403                      break;
00404               lv = lv->nextSibling();
00405        }
00406        BookMItem *ip;
00407        BookMItem *ite;
00408        if (lv)
00409               ite = new BookMItem(this, lv, NrItems+1, PageObject);
00410        else
00411               ite = new BookMItem(this, NrItems+1, PageObject);
00412        ite->setText(0, text);
00413        ite->Titel = Tit;
00414        ite->Next = 0;
00415        if (lv)
00416        {
00417               ip = (BookMItem*)lv;
00418               ip->Next = ite->ItemNr;
00419               ite->Prev = ip->ItemNr;
00420        }
00421        NrItems++;
00422 }
00423 
00424 void BookMView::DeleteItem(PageItem *pObject)
00425 {
00426        BookMItem *ite;
00427        int nr = 0;
00428        QListViewItemIterator itx(this);
00429        for ( ; itx.current(); ++itx)
00430        {
00431               ite = (BookMItem*)itx.current();
00432               if (ite->PageObject == pObject)
00433               {
00434                      nr = ite->ItemNr;
00435                      break;
00436               }
00437        }
00438        BookMItem *ite2 = 0;
00439        BookMItem *ite3;
00440        BookMItem *ite4;
00441        QListViewItemIterator it(this);
00442        for ( ; it.current(); ++it)
00443        {
00444               ite = (BookMItem*)it.current();
00445               if (ite->Next == nr)
00446                      ite2 = ite;
00447               if (ite->ItemNr == nr)
00448               {
00449                      QListViewItem *pp = ite->firstChild();
00450                      QListViewItem *ppn = ite->nextSibling();
00451                      if ((ite->parent()) && (ite2 == 0) && (ppn))
00452                      {
00453                             ite3 = (BookMItem*)ite->parent();
00454                             ite4 = (BookMItem*)ppn;
00455                             ite3->First = ite4->ItemNr;
00456                      }
00457                      while (pp)
00458                      {
00459                      ite->parent() ? ite->parent()->insertItem(pp) :  pp->moveItem(ite);
00460                             pp = ite->firstChild();
00461                      }
00462                      if (ppn)
00463                      {
00464                             ite3 = (BookMItem*)ppn;
00465                             if (ite2)
00466                             {
00467                                    ite2->Next = ite3->ItemNr;
00468                                    ite3->Prev = ite2->ItemNr;
00469                             }
00470                             else
00471                                    ite3->Prev = 0;
00472                      }
00473                      else
00474                             if (ite2)
00475                                    ite2->Next = 0;
00476                      delete ite;
00477                      NrItems--;
00478               }
00479        }
00480        QMap<int,int> Tabl;
00481        Tabl.clear();
00482        Tabl[0] = 0;
00483        int Counter = 1;
00484        QListViewItemIterator itn(this);
00485        for ( ; itn.current(); ++itn)
00486        {
00487               ite = (BookMItem*)itn.current();
00488               Tabl[ite->ItemNr] = Counter;
00489               Counter++;
00490        }
00491        QListViewItemIterator itnf(this);
00492        for ( ; itnf.current(); ++itnf)
00493        {
00494               ite = (BookMItem*)itnf.current();
00495               ite->ItemNr = Tabl[ite->ItemNr];
00496               ite->Pare = Tabl[ite->Pare];
00497               ite->Next = Tabl[ite->Next];
00498               ite->Prev = Tabl[ite->Prev];
00499               ite->First = Tabl[ite->First];
00500               ite->Last = Tabl[ite->Last];
00501        }
00502 }
00503 
00504 void BookMView::SetAction(PageItem *currItem, QString Act)
00505 {
00506        BookMItem *ite;
00507        QListViewItemIterator it(this);
00508        for ( ; it.current(); ++it)
00509        {
00510               ite = (BookMItem*)it.current();
00511               if (ite->PageObject == currItem)
00512               {
00513                      ite->Action = Act;
00514                      break;
00515               }
00516        }
00517 }
00518 
00519 void BookMView::ChangeText(PageItem *currItem)
00520 {
00521        BookMItem *ite;
00522        QString bm = "";
00523        QString bm2 = "";
00524        QString cc;
00525        for (int d = 0; d < currItem->itemText.length(); ++d)
00526        {
00527               cc = currItem->itemText.text(d);
00528               if ((cc == QChar(13)) || (cc == QChar(10)))
00529                      break;
00530               if ((cc == "(") || (cc == ")") || (cc == "\\"))
00531                      bm2 += "\\";
00532               bm += cc;
00533               bm2 += cc;
00534        }
00535        QListViewItemIterator it(this);
00536        for ( ; it.current(); ++it)
00537        {
00538               ite = (BookMItem*)it.current();
00539               if (ite->PageObject == currItem)
00540               {
00541                      ite->setText(0, bm);
00542                      ite->Titel = bm2;
00543                      break;
00544               }
00545        }
00546 }
00547 
00548 void BookMView::languageChange()
00549 {
00550        setColumnText(idBookMarkCol, tr("Bookmarks"));
00551 }