Scribus Developer Documentation - API
logo-pen

Scribus Developer Documentation - API

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 #include <QApplication>
00024 #include <QHeaderView>
00025 
00026 #include "bookmwin.h"
00027 #include "util_icon.h"
00028 
00029 
00030 BookMItem::BookMItem(QTreeWidgetItem* parent, struct ScribusDoc::BookMa *Bm)
00031     : QTreeWidgetItem(parent)
00032 {
00033     SetUp(Bm);
00034 }
00035 
00036 BookMItem::BookMItem(QTreeWidgetItem* parent, QTreeWidgetItem* after, struct ScribusDoc::BookMa *Bm)
00037     : QTreeWidgetItem(parent, after)
00038 {
00039     SetUp(Bm);
00040 }
00041 
00042 BookMItem::BookMItem(QTreeWidget* parent, QTreeWidgetItem* after, struct ScribusDoc::BookMa *Bm)
00043     : QTreeWidgetItem(parent, after)
00044 {
00045     SetUp(Bm);
00046 }
00047 
00048 BookMItem::BookMItem(QTreeWidget* parent, struct ScribusDoc::BookMa *Bm)
00049     : QTreeWidgetItem(parent)
00050 {
00051     SetUp(Bm);
00052 }
00053 
00054 BookMItem::BookMItem(QTreeWidget* parent, QTreeWidgetItem* after, int nr, PageItem* PObject)
00055     : QTreeWidgetItem(parent, after)
00056 {
00057     ItemNr = nr;
00058     PageObject = PObject;
00059     PdfObj = 0;
00060     Action = "";
00061     First = 0;
00062     Last = 0;
00063     Prev = 0;
00064     Next = 0;
00065     Pare = 0;
00066 }
00067 
00068 BookMItem::BookMItem(QTreeWidget* parent, int nr, PageItem* PObject)
00069     : QTreeWidgetItem(parent)
00070 {
00071     ItemNr = nr;
00072     PageObject = PObject;
00073     PdfObj = 0;
00074     Action = "";
00075     First = 0;
00076     Last = 0;
00077     Prev = 0;
00078     Next = 0;
00079     Pare = 0;
00080 }
00081 
00082 void BookMItem::SetUp(struct ScribusDoc::BookMa *Bm)
00083 {
00084     ItemNr = Bm->ItemNr;
00085     PdfObj = 0;
00086     Action = Bm->Aktion;
00087     First = Bm->First;
00088     Last = Bm->Last;
00089     Prev = Bm->Prev;
00090     Next = Bm->Next;
00091     Pare = Bm->Parent;
00092     Titel = Bm->Title;
00093     PageObject = Bm->PageObject;
00094     setText(0, Bm->Text);
00095 }
00096 
00097 QString BookMItem::key(int, bool) const
00098 {
00099     QString tmp;
00100     tmp.sprintf("%5d", ItemNr);
00101     return tmp;
00102 }
00103 
00104 int BookMItem::level()
00105 {
00106     int ret = 0; // 0 = top level
00107     QTreeWidgetItem * p = parent();
00108     while (p)
00109     {
00110         ++ret;
00111         p = p->parent();
00112     }
00113     return ret;
00114 }
00115 
00116 
00117 BookMView::BookMView(QWidget* parent) : QTreeWidget(parent)
00118 {
00119     NrItems = 0;
00120     First = 1;
00121     Last = 0;
00122 
00123     setAcceptDrops(true);
00124     setSelectionMode(QAbstractItemView::SingleSelection);
00125     setDragEnabled(true);
00126     setDropIndicatorShown(true);
00127     setDragDropMode(QAbstractItemView::InternalMove);
00128     setRootIsDecorated(true);
00129     header()->hide();
00130 
00131     connect(this, SIGNAL(currentItemChanged( QTreeWidgetItem *, QTreeWidgetItem *)),
00132             this, SLOT(setPageItem(QTreeWidgetItem *, QTreeWidgetItem *)));
00133 }
00134 
00135 void BookMView::AddPageItem(PageItem* ite)
00136 {
00137     QString bm = "";
00138     QString bm2 = "";
00139     QString cc;
00140     for (int d = 0; d < ite->itemText.length(); ++d)
00141     {
00142         cc = ite->itemText.text(d);
00143         if ((cc == QChar(13)) || (cc == QChar(10)))
00144             break;
00145         if (cc == QChar(29))
00146             cc = " ";
00147         if ((cc == "(") || (cc == ")") || (cc == "\\"))
00148             bm2 += "\\";
00149         bm += cc;
00150         bm2 += cc;
00151     }
00152     AddItem(bm, bm2, ite);
00153     Last = NrItems;
00154 }
00155 
00156 void BookMView::setPageItem(QTreeWidgetItem * /*old*/, QTreeWidgetItem * newItem)
00157 {
00158     BookMItem * i = dynamic_cast<BookMItem*>(newItem);
00159     // something weird here at 1st call...
00160     if (i)
00161         emit SelectElement(i->PageObject);
00162 }
00163 
00164 void BookMView::dropEvent(QDropEvent *e)
00165 {
00166     QTreeWidget::dropEvent(e);
00167     QApplication::restoreOverrideCursor();
00168     rebuildTree();
00169 }
00170 
00171 // #include <QtDebug>
00172 void BookMView::rebuildTree()
00173 {
00174     QTreeWidgetItemIterator it(this, QTreeWidgetItemIterator::All);
00175     // level - parent : children
00176     QMap<int,QMap<int,QList<BookMItem*> > > levels;
00177     QMap<int,BookMItem*> items;
00178 
00179     Last = 0;
00180 
00181     while (*it)
00182     {
00183         BookMItem * currItem = (BookMItem*)(*it);
00184         // handle parents
00185         BookMItem * parent = (BookMItem*)currItem->parent();
00186         currItem->Pare = parent ? parent->ItemNr : 0;
00187 
00188         levels[currItem->level()][currItem->Pare].append(currItem);
00189         items[Last] = currItem;
00190         
00191         currItem->ItemNr = Last;
00192         ++Last;
00193         ++it;
00194     }
00195 
00196     NrItems = Last;
00197 
00198     foreach (int level, levels.keys())
00199     {
00200 //      qDebug() << "LEVEL: " << level;
00201         QMapIterator<int,QList<BookMItem*> > parent(levels[level]);
00202         while (parent.hasNext())
00203         {
00204             parent.next();
00205 //          qDebug() << "PARENT: " << parent.key();
00206             int cnt = parent.value().count();
00207             items[parent.key()]->First = cnt > 0 ? parent.value().at(0)->ItemNr : 0;
00208             items[parent.key()]->Last = cnt > 0 ? parent.value().at(cnt-1)->ItemNr : 0;
00209             int prev = parent.key();
00210             int ix = 0;
00211             foreach (BookMItem* item, parent.value())
00212             {
00213 //              qDebug() << "CHILD: " << item->ItemNr;
00214                 item->Prev = prev;
00215                 prev = item->ItemNr;
00216                 if (ix < (cnt - 1))
00217                     item->Next = parent.value()[ix+1]->ItemNr;
00218                 ++ix;
00219             }
00220         }
00221     }
00222 /*
00223     foreach (BookMItem* i, items.values())
00224     {
00225         qDebug() << i->ItemNr << " " << i->text(0).left(10) << " " << i->Prev << " " << i->Next << " " << i->Pare << " " << i->First << " " << i->Last;
00226     }*/
00227 
00228     emit MarkMoved();
00229     emit changed();
00230 }
00231 
00232 void BookMView::AddItem(QString text, QString Tit, PageItem *PageObject)
00233 {
00234     BookMItem * ite = new BookMItem(this, NrItems+1, PageObject);
00235     ite->setText(0, text);
00236     ite->Titel = Tit;
00237     rebuildTree();
00238 }
00239 
00240 void BookMView::DeleteItem(PageItem *pObject)
00241 {
00242     QTreeWidgetItemIterator it(this);
00243     BookMItem * item;
00244     while (*it)
00245     {
00246         item = (BookMItem*)(*it);
00247         if (item->PageObject == pObject)
00248         {
00249             delete (*it);
00250             break;
00251         }
00252     }
00253     rebuildTree();
00254 }
00255 
00256 void BookMView::SetAction(PageItem *currItem, QString Act)
00257 {
00258     BookMItem *ite;
00259     QTreeWidgetItemIterator it(this);
00260     while (*it)
00261     {
00262         ite = (BookMItem*)(*it);
00263         if (ite->PageObject == currItem)
00264         {
00265             ite->Action = Act;
00266             break;
00267         }
00268         ++it;
00269     }
00270 }
00271 
00272 void BookMView::ChangeText(PageItem *currItem)
00273 {
00274     BookMItem *ite;
00275     QString bm = "";
00276     QString bm2 = "";
00277     QString cc;
00278     for (int d = 0; d < currItem->itemText.length(); ++d)
00279     {
00280         cc = currItem->itemText.text(d);
00281         if ((cc == QChar(13)) || (cc == QChar(10)))
00282             break;
00283         if ((cc == "(") || (cc == ")") || (cc == "\\"))
00284             bm2 += "\\";
00285         bm += cc;
00286         bm2 += cc;
00287     }
00288     QTreeWidgetItemIterator it(this);
00289     while (*it)
00290     {
00291         ite = (BookMItem*)(*it);
00292         if (ite->PageObject == currItem)
00293         {
00294             ite->setText(0, bm);
00295             ite->Titel = bm2;
00296             break;
00297         }
00298         ++it;
00299     }
00300     rebuildTree();
00301 }
Created with personalized Doxygen. This is documentation for GPL software Scribus - Open Source Desktop Publishing.