• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • kdeutils
  • Sitemap
  • Contact Us
 

kjots

bookshelf.cpp

Go to the documentation of this file.
00001 //
00002 //  Copyright (C) 1997 Christoph Neerfeld <Christoph.Neerfeld@home.ivm.de>
00003 //  Copyright (C) 2002, 2003 Aaron J. Seigo <aseigo@kde.org>
00004 //  Copyright (C) 2003 Stanislav Kljuhhin <crz@hot.ee>
00005 //  Copyright (C) 2005-2006 Jaison Lee <lee.jaison@gmail.com>
00006 //  Copyright (C) 2007 Stephen Kelly <steveire@gmail.com>
00007 //
00008 //  This program is free software; you can redistribute it and/or modify
00009 //  it under the terms of the GNU General Public License as published by
00010 //  the Free Software Foundation; either version 2 of the License, or
00011 //  (at your option) any later version.
00012 //
00013 //  This program is distributed in the hope that it will be useful,
00014 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 //  GNU General Public License for more details.
00017 //
00018 //  You should have received a copy of the GNU General Public License
00019 //  along with this program; if not, write to the Free Software
00020 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00021 //
00022 
00023 //Own Header
00024 #include "bookshelf.h"
00025 
00026 #include <QAction>
00027 #include <QMimeData>
00028 #include <QDir>
00029 #include <QApplication>
00030 #include <QClipboard>
00031 #include <QHeaderView>
00032 #include <QDragMoveEvent>
00033 
00034 #include <kaction.h>
00035 #include <kmessagebox.h>
00036 #include <klocale.h>
00037 #include <kactioncollection.h>
00038 #include <kmenu.h>
00039 #include <kstandardaction.h>
00040 #include <kstandarddirs.h>
00041 #include <kcolordialog.h>
00042 
00043 #include "kjotsentry.h"
00044 #include "KJotsSettings.h"
00045 
00046 QString mimeType = "kjots/dropdata";
00047 
00048 Bookshelf::Bookshelf ( QWidget *parent ) : QTreeWidget(parent)
00049 {
00050     bookActionCollection = pageActionCollection = 0;
00051     sortOrder = Qt::DescendingOrder;
00052 
00053     setObjectName( "bookshelf" );
00054     setColumnCount(1);
00055     setRootIsDecorated(true);
00056     setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding));
00057     setMinimumWidth(fontMetrics().maxWidth() * 10 + 5);
00058     setSelectionMode(QAbstractItemView::ExtendedSelection);
00059     headerItem()->setText(0, i18n("Bookshelf"));
00060     header()->setClickable(true);
00061     setDragEnabled(true);
00062     setAcceptDrops(true);
00063     setDropIndicatorShown(true);
00064     setContextMenuPolicy(Qt::ActionsContextMenu);
00065 
00066     connect(this, SIGNAL(itemChanged(QTreeWidgetItem*, int)), SLOT(entryRenamed(QTreeWidgetItem*, int)));
00067 
00068 }
00069 
00070 void Bookshelf::DelayedInitialization ( KActionCollection *actionCollection ) {
00071     loadBooks();
00072 
00073     QAction *sep = new QAction(this);
00074     sep->setSeparator(true);
00075 
00076     QAction *action;
00077     action = actionCollection->addAction("copy_link_address");
00078     action->setText(i18n("Copy Link Address"));
00079     connect(action, SIGNAL(triggered()), SLOT(copyLinkAddress()));
00080 
00081     pageActionCollection = new KActionCollection(this);
00082     bookActionCollection = new KActionCollection(this);
00083     multiActionCollection = new KActionCollection(this);
00084 
00085     pageActionCollection->addAction("new_page", actionCollection->action("new_page"));
00086     pageActionCollection->addAction("rename_entry", actionCollection->action("rename_entry"));
00087     pageActionCollection->addAction("save_to", actionCollection->action("save_to"));
00088     pageActionCollection->addAction("copy_link_address", actionCollection->action("copy_link_address"));
00089     bookActionCollection->addAction("del_folder", actionCollection->action("del_folder"));
00090     pageActionCollection->addAction("del_page", actionCollection->action("del_page"));
00091 
00092     bookActionCollection->addAction("new_page", actionCollection->action("new_page"));
00093     bookActionCollection->addAction("rename_entry", actionCollection->action("rename_entry"));
00094     bookActionCollection->addAction("save_to", actionCollection->action("save_to"));
00095     bookActionCollection->addAction("copy_link_address", actionCollection->action("copy_link_address"));
00096     bookActionCollection->addAction("del_folder", actionCollection->action("del_folder"));
00097 
00098     multiActionCollection->addAction("save_to", actionCollection->action("save_to"));
00099     multiActionCollection->addAction("del_mult", actionCollection->action("del_mult"));
00100 
00101     addAction(actionCollection->action("new_book"));
00102     addAction(actionCollection->action("new_page"));
00103     addAction(actionCollection->action("rename_entry"));
00104     addAction(actionCollection->action("save_to"));
00105     addAction(actionCollection->action("copy_link_address"));
00106     addAction(actionCollection->action("change_color"));
00107     addAction(sep);
00108     addAction(actionCollection->action("del_folder"));
00109     addAction(actionCollection->action("del_page"));
00110     addAction(actionCollection->action("del_mult"));
00111 
00112     // Do this now or else the slot will called before DelayedInitialization().
00113     connect(this, SIGNAL(itemSelectionChanged()), SLOT(setContextMenuOptions()));
00114 
00115     //These need to be connected after books are loaded.
00116     connect(this, SIGNAL(itemExpanded(QTreeWidgetItem*)), SLOT(itemWasExpanded(QTreeWidgetItem*)));
00117     connect(this, SIGNAL(itemCollapsed(QTreeWidgetItem*)), SLOT(itemWasCollapsed(QTreeWidgetItem*)));
00118 
00119     connect(actionCollection->action("change_color"), SIGNAL(triggered()),
00120         this, SLOT(changeColor()));
00121     connect(header(), SIGNAL(sectionClicked(int)), this, SLOT(onHeaderClick(int)));
00122 
00123     return;
00124 }
00125 
00135 void Bookshelf::loadBooks ( void )
00136 {
00137     QDir dir(KStandardDirs::locateLocal("appdata",""));
00138     QList<KJotsBook*> books;
00139 
00140     QStringList filter;
00141     filter << "*.book";
00142 
00143     setUpdatesEnabled(false);
00144 
00145     //Read in books from disk
00146     QStringList files = dir.entryList(filter, QDir::Files|QDir::Readable);
00147     foreach ( QString file, files ) {
00148         QString filepath = dir.absoluteFilePath(file);
00149         KJotsBook* book = new KJotsBook();
00150         book->openBook(filepath);
00151         books << book;
00152     }
00153 
00154     //Sort books and add them in order. KConfig doesn't have a built-in
00155     //ULongLongList so we do our own thing for now.
00156     QString orderString = KJotsSettings::bookshelfOrder();
00157     QStringList idStrings = orderString.split(",");
00158     foreach ( QString idString, idStrings ) {
00159         quint64 id = idString.toLongLong();
00160         foreach ( KJotsBook *book, books ) {
00161             if ( book->id() == id ) {
00162                 addTopLevelItem(book);
00163                 books.removeAll(book);
00164                 break;
00165             }
00166         }
00167     }
00168 
00169     //If we missed anything someone must by playing around with the data
00170     //files. Just add it to the end.
00171     foreach ( KJotsBook *book, books ) {
00172         addTopLevelItem(book);
00173     }
00174 
00175     // Open any books that need opening.
00176     if ( topLevelItemCount() ) {
00177         QTreeWidgetItemIterator it ( this, QTreeWidgetItemIterator::HasChildren );
00178         for ( ; *it ; it++ ) {
00179             KJotsBook *book = static_cast<KJotsBook*>(*it);
00180 
00181             if ( book->shouldBeOpened() ) {
00182                 setItemExpanded(book, true);
00183             }
00184         }
00185     }
00186 
00187     setUpdatesEnabled(true);
00188 
00189     return;
00190 }
00191 
00195 void Bookshelf::prepareForExit ( void )
00196 {
00197     QList<QTreeWidgetItem*> selection = selectedItems();
00198     if ( selection.size() == 1 ) {
00199         KJotsEntry *entry = static_cast<KJotsBook*>(selection[0]);
00200         KJotsSettings::setCurrentSelection(entry->id());
00201     } else {
00202         KJotsSettings::setCurrentSelection(0);
00203     }
00204 
00205     QStringList idList;
00206     int tops = topLevelItemCount();
00207     for ( int i=0; i<tops; i++ ) {
00208         KJotsBook *book = static_cast<KJotsBook*>(topLevelItem(i));
00209         idList << QString::number(book->id());
00210     }
00211     KJotsSettings::setBookshelfOrder( idList.join(",") );
00212 
00213 }
00214 
00221 KJotsEntry* Bookshelf::entryFromId(quint64 id)
00222 {
00223     KJotsEntry *entry = 0;
00224     if ( !id ) return entry; //why waste time?
00225 
00226     for ( QTreeWidgetItemIterator it( this ); *it; it++ ) {
00227         entry = dynamic_cast<KJotsEntry*>(*it);
00228         if ( entry && entry->id() == id ) {
00229             break;
00230         }
00231     }
00232 
00233     return entry;
00234 }
00235 
00240 void Bookshelf::jumpToId(quint64 id)
00241 {
00242     KJotsEntry *entry = entryFromId(id);
00243     jumpToEntry(entry);
00244 
00245     return;
00246 }
00247 
00252 void Bookshelf::jumpToEntry(QTreeWidgetItem *item)
00253 {
00254     if ( item ) {
00255         clearSelection();
00256         scrollToItem(item);
00257         setItemSelected(item, true);
00258     }
00259 
00260     return;
00261 }
00262 
00266 void Bookshelf::remove(QTreeWidgetItem *item)
00267 {
00268     KJotsEntry *entry = dynamic_cast<KJotsEntry*>(item);
00269     Q_ASSERT(entry);
00270 
00271     if ( entry->parentBook() ) {
00272         entry->parentBook()->takeChild(entry->parentBook()->indexOfChild(entry));
00273     } else {
00274         takeTopLevelItem(indexOfTopLevelItem(entry));
00275     }
00276 
00277     if ( entry->isBook() ) {
00278         dynamic_cast<KJotsBook*>(entry)->deleteBook();
00279     }
00280 
00281     delete entry;
00282 }
00283 
00287 Qt::DropActions Bookshelf::supportedDropActions() const
00288 {
00289     return Qt::CopyAction;
00290 }
00291 
00295 QStringList Bookshelf::mimeTypes() const
00296 {
00297     QStringList types;
00298     types << mimeType;
00299     return types;
00300 }
00301 
00302 void Bookshelf::dragEnterEvent ( QDragEnterEvent *event )
00303 {
00304     QTreeWidget::dragEnterEvent(event);
00305 
00306     //Don't accept multi-drags in the bookshelf
00307     QByteArray incomingData = event->mimeData()->data(mimeType);
00308     if ( incomingData.count('|') ) {
00309         event->setAccepted(false);
00310     }
00311 }
00312 
00313 void Bookshelf::dragMoveEvent ( QDragMoveEvent *event )
00314 {
00315     QTreeWidget::dragMoveEvent(event);
00316 
00317     QByteArray incomingData = event->mimeData()->data(mimeType);
00318     quint64 id = incomingData.toULongLong();
00319     KJotsEntry *entry = entryFromId(id);
00320 
00321     //Don't allow a book to be dragged into itself
00322     if ( entry->isBook() ) {
00323         KJotsBook *book = static_cast<KJotsBook*>(entry);
00324         QTreeWidgetItem *item = itemAt(event->pos());
00325         KJotsEntry *target = static_cast<KJotsEntry*>(item);
00326         if ( book->contents().contains(target) ) {
00327             event->setAccepted(false);
00328         }
00329     }
00330 }
00331 
00335 QMimeData* Bookshelf::mimeData(const QList<QTreeWidgetItem*> items) const
00336 {
00337     QMimeData *mimeData = new QMimeData();
00338     QStringList ids;
00339     QString textData, htmlData;
00340 
00341     foreach ( QTreeWidgetItem *item, items ) {
00342         KJotsEntry *entry = dynamic_cast<KJotsEntry*>(item);
00343 
00344         if ( entry ) {
00345             ids << QString::number(entry->id());
00346 
00347             if ( entry->isPage() ) {
00348                 textData += static_cast<KJotsPage*>(entry)->body()->toPlainText();
00349                 htmlData += static_cast<KJotsPage*>(entry)->body()->toHtml();
00350             }
00351         }
00352     }
00353 
00354     mimeData->setData(mimeType, ids.join(QString('|')).toAscii());
00355     mimeData->setText(textData);
00356     mimeData->setHtml(htmlData);
00357     return mimeData;
00358 }
00359 
00363 bool Bookshelf::dropMimeData ( QTreeWidgetItem *parent, int index,
00364     const QMimeData *data, Qt::DropAction action )
00365 {
00366     QList<KJotsEntry*> movedItems;
00367     KJotsBook *newParent = dynamic_cast<KJotsBook*>(parent);
00368     bool success = false;
00369 
00370     if (action == Qt::IgnoreAction) return true; //Sanity Check
00371 
00372     QList<QByteArray> ids = data->data(mimeType).split('|');
00373     foreach ( QByteArray id, ids ) {
00374         movedItems << entryFromId(id.toULongLong());
00375     }
00376 
00377     foreach ( KJotsEntry *item, movedItems ) {
00378         bool wasOpened = isItemExpanded(item);
00379 
00380         // We don't accept pages in the root. Create a new book?
00381         if ( !newParent && item->isPage() ) {
00382             //Disable ourselves to prevent user from doing anything.
00383             QWidget::setEnabled(false);
00384 
00385             if (KMessageBox::questionYesNo(this,
00386                 i18n("All pages must be inside a book. "
00387                 "Would you like to create a new book to put the page in, "
00388                 "or would you prefer to not move the page at all?"),
00389                 QString(), KGuiItem(i18n("Create New Book")), KGuiItem(i18n("Do Not Move Page"))) ==
00390                 KMessageBox::Yes )
00391             {
00392                 //TODO: create the book at the place where the drop occurred instead of
00393                 //appending it to the end.
00394                 KJotsBook* book = new KJotsBook();
00395                 addTopLevelItem(book);
00396                 //TODO: Ask for Book name.
00397                 book->setTitle(i18n("New Book"));
00398                 item->parentBook()->setDirty(true);
00399                 item->parentBook()->takeChild(index);
00400                 book->setDirty(true);
00401                 book->addChild(item);
00402             }
00403 
00404             QWidget::setEnabled(true);
00405         } else {
00406             int oldIndex = (item->parentBook()) ?
00407                 item->parentBook()->indexOfChild(item) : indexOfTopLevelItem(item);
00408 
00409             //Proceed with caution when keeping the same parent...
00410             if ( newParent == item->parentBook() ) {
00411 
00412                 //Make sure we are actually moving somewhere.
00413                 if ( index == (oldIndex + 1) || index == oldIndex ) {
00414                     return false;
00415                 }
00416 
00417                 if ( index > oldIndex ) {
00418                     --index; //Don't count ourselves.
00419                 }
00420 
00421                 if ( newParent ) {
00422                     newParent->setDirty(true);
00423                     newParent->takeChild(oldIndex);
00424                     parent->insertChild(index, item);
00425                 } else {
00426                     item->topLevelBook()->setDirty(true);
00427                     takeTopLevelItem(oldIndex);
00428                     insertTopLevelItem(index, item);
00429                 }
00430             } else {
00431                 if ( !item->parentBook() ) {
00432                     takeTopLevelItem(oldIndex);
00433                 } else {
00434                     item->parentBook()->setDirty(true);
00435                     item->parentBook()->takeChild(oldIndex);
00436                 }
00437 
00438                 if ( !parent ) {
00439                     insertTopLevelItem(index, item);
00440                     // Nested book moved to the root.
00441                     // Open the book so it can be marked dirty and saved.
00442                     KJotsBook *book = static_cast<KJotsBook*>(item);
00443                     book->openBook(QString());
00444                 } else {
00445                     parent->insertChild(index, item);
00446                 }
00447 
00448                 item->topLevelBook()->setDirty(true);
00449                 success = true;
00450             }
00451 
00452             if ( wasOpened ) {
00453                 setItemExpanded(item, true);
00454             }
00455         }
00456     }
00457 
00458     return success;
00459 }
00460 
00461 void Bookshelf::entryRenamed(QTreeWidgetItem* item, int  /*col*/)
00462 {
00463     KJotsEntry* entry = dynamic_cast<KJotsEntry*>(item);
00464 
00465     if (entry) {
00466         entry->topLevelBook()->setDirty(true);
00467         jumpToEntry(entry);
00468     }
00469 }
00470 
00471 void Bookshelf::setContextMenuOptions()
00472 {
00473     QList<QTreeWidgetItem*> selection = selectedItems();
00474     int selectionSize = selection.size();
00475 
00476     // Reset all menu entries
00477     foreach ( QAction* action, multiActionCollection->actions() )
00478         action->setEnabled(false);
00479     foreach ( QAction* action, pageActionCollection->actions() )
00480         action->setEnabled(false);
00481     foreach ( QAction* action, bookActionCollection->actions() )
00482         action->setEnabled(false);
00483 
00484     if ( !selectionSize  ) {
00485         // No item is selected
00486     } else if ( selectionSize == 1 ) {
00487         KJotsEntry *entry = static_cast<KJotsEntry*>(selection.at(0));
00488         if ( entry->isBook() ) {
00489         foreach ( QAction* action, bookActionCollection->actions() )
00490             action->setEnabled(true);
00491         } else {
00492         foreach ( QAction* action, pageActionCollection->actions() )
00493             action->setEnabled(true);
00494         }
00495     }
00496     else if ( selectionSize > 1 ) {
00497         foreach ( QAction* action, multiActionCollection->actions() )
00498             action->setEnabled(true);
00499     }
00500 
00501 
00502     return;
00503 }
00504 
00508 KJotsEntry* Bookshelf::currentEntry ( void )
00509 {
00510     KJotsEntry *entry = 0;
00511     QList<QTreeWidgetItem*> selection = selectedItems();
00512 
00513     if ( selection.size() == 1 ) {
00514         entry = dynamic_cast<KJotsEntry*>(selection.at(0));
00515     }
00516 
00517     return entry;
00518 }
00522 KJotsPage* Bookshelf::currentPage ( void )
00523 {
00524     KJotsPage *page = 0;
00525     QList<QTreeWidgetItem*> selection = selectedItems();
00526 
00527     if ( selection.size() == 1 ) {
00528         page = dynamic_cast<KJotsPage*>(selection.at(0));
00529     }
00530 
00531     return page;
00532 }
00533 
00538 KJotsBook* Bookshelf::currentBook ( void )
00539 {
00540     KJotsBook *book = 0;
00541     QList<QTreeWidgetItem*> selection = selectedItems();
00542 
00543     if ( selection.size() == 1 ) {
00544         KJotsEntry *entry = dynamic_cast<KJotsEntry*>(selection.at(0));
00545         Q_ASSERT(entry);
00546 
00547         if ( entry->isPage() ) {
00548             book = entry->parentBook();
00549         } else {
00550             book = dynamic_cast<KJotsBook*>(entry);
00551         }
00552     }
00553 
00554     return book;
00555 }
00556 
00561 KJotsBook* Bookshelf::currentTopLevelBook ( void )
00562 {
00563     KJotsBook *book = currentBook();
00564 
00565     while ( book && book->parentBook() ) {
00566         book = book->parentBook();
00567     }
00568 
00569     return book;
00570 }
00571 
00575 void Bookshelf::itemWasExpanded(QTreeWidgetItem *item)
00576 {
00577     KJotsEntry *entry = dynamic_cast<KJotsEntry*>(item);
00578     if ( entry ) {
00579         entry->topLevelBook()->setDirty(true);
00580     }
00581 }
00582 
00586 void Bookshelf::itemWasCollapsed(QTreeWidgetItem *item)
00587 {
00588     KJotsEntry *entry = dynamic_cast<KJotsEntry*>(item);
00589     if ( entry ) {
00590         entry->topLevelBook()->setDirty(true);
00591     }
00592 }
00593 
00597 void Bookshelf::copyLinkAddress()
00598 {
00599     QList<QTreeWidgetItem*> selection = selectedItems();
00600     if ( selection.size() != 1 ) return;
00601 
00602     KJotsEntry *entry = static_cast<KJotsEntry*>(selection.at(0));
00603     QMimeData *mimeData = new QMimeData();
00604 
00605     QString link = QString("<a href=\"kjots://0.0.0.0/%1\">%2</a>").arg(entry->id()).arg(entry->title());
00606 
00607     mimeData->setData("kjots/internal_link", link.toUtf8());
00608     mimeData->setText(entry->title());
00609     QApplication::clipboard()->setMimeData(mimeData);
00610     return;
00611 }
00612 
00617 QList<KJotsEntry*> Bookshelf::selected(void)
00618 {
00619     QList<KJotsEntry*> entries;
00620 
00621     foreach ( QTreeWidgetItem *item, selectedItems() ) {
00622         entries << static_cast<KJotsEntry*>(item);
00623     }
00624 
00625     //The book itself overrides its pages.
00626     foreach ( KJotsEntry *entry, entries ) {
00627         if ( entry->isBook() ) {
00628             KJotsBook *book = static_cast<KJotsBook*>(entry);
00629             foreach ( KJotsEntry *content, book->contents() ) {
00630                 entries.removeAll(content);
00631             }
00632         }
00633     }
00634 
00635     return entries;
00636 }
00637 
00641 QString Bookshelf::currentCaption(const QString &sep)
00642 {
00643     QString caption;
00644 
00645     QList<QTreeWidgetItem*> selection = selectedItems();
00646     int selectionSize = selection.size();
00647     if ( selectionSize > 1 ) {
00648         caption = i18n("Multiple selections");
00649     } else if ( selectionSize == 1 ) {
00650         QTreeWidgetItem *item = selection[0];
00651         while ( item ) {
00652             QTreeWidgetItem *parentBook = item->parent();
00653 
00654             if ( parentBook ) {
00655                 caption = sep + item->text(0) + caption;
00656             } else {
00657                 caption = item->text(0) + caption;
00658             }
00659 
00660             item=parentBook;
00661         }
00662     }
00663 
00664     return caption;
00665 }
00666 
00667 void Bookshelf::changeColor()
00668 {
00669     QColor myColor;
00670     int result = KColorDialog::getColor( myColor );
00671     if ( result == KColorDialog::Accepted ) {
00672         foreach( KJotsEntry *entry, selected() ) {
00673             entry->setBackgroundColor(0, myColor);
00674             entry->topLevelBook()->setDirty(true);
00675             if ( entry->isBook() ) {
00676                 KJotsBook *book = static_cast<KJotsBook*>(entry);
00677                 foreach( KJotsEntry *entry2, book->contents() ) {
00678                     entry2->setBackgroundColor(0, myColor);
00679                 }
00680             }
00681         }
00682     }
00683 }
00684 
00688 void Bookshelf::nextBook()
00689 {
00690     QTreeWidgetItem *item = currentEntry();
00691     if ( !item ) return;
00692 
00693     QTreeWidgetItemIterator it ( item, QTreeWidgetItemIterator::HasChildren );
00694     if ( *it == item ) it++;
00695 
00696     if ( *it == 0 ) {
00697         // Just set to first book.
00698         it = QTreeWidgetItemIterator ( topLevelItem(0) );
00699     }
00700 
00701     if ( *it && *it != item ) {
00702         jumpToEntry(*it);
00703     }
00704 
00705     return;
00706 }
00707 
00711 void Bookshelf::prevBook()
00712 {
00713     KJotsEntry *entry = currentEntry();
00714     QTreeWidgetItem *item = entry;
00715     if ( !item ) return;
00716 
00717     QTreeWidgetItemIterator it ( item );
00718     while ( *it ) {
00719         --it;
00720 
00721         if ( *it == 0 ) {
00722             QModelIndex index = moveCursor(QAbstractItemView::MoveEnd, Qt::NoModifier);
00723             it = QTreeWidgetItemIterator ( itemFromIndex(index) );
00724         }
00725 
00726         if ( static_cast<KJotsEntry*>(*it)->isBook() ) {
00727             break;
00728         }
00729     }
00730 
00731     if ( *it && *it != item ) {
00732         jumpToEntry(*it);
00733     }
00734 
00735     return;
00736 }
00737 
00741 void Bookshelf::nextPage()
00742 {
00743     QTreeWidgetItem *item = currentEntry();
00744     if ( !item ) return;
00745 
00746     QTreeWidgetItemIterator it ( item, QTreeWidgetItemIterator::NoChildren );
00747     if ( *it == item ) it++;
00748 
00749     if ( *it == 0 ) {
00750         it = QTreeWidgetItemIterator ( topLevelItem(0),
00751             QTreeWidgetItemIterator::NoChildren );
00752     }
00753 
00754     if ( *it && *it != item ) {
00755         jumpToEntry(*it);
00756     }
00757 
00758     return;
00759 }
00760 
00764 void Bookshelf::prevPage()
00765 {
00766     KJotsEntry *entry = currentEntry();
00767     QTreeWidgetItem *item = entry;
00768     if ( !item ) return;
00769 
00770     QTreeWidgetItemIterator it ( item );
00771     while ( *it ) {
00772         --it;
00773 
00774         if ( *it == 0 ) {
00775             QModelIndex index = moveCursor(QAbstractItemView::MoveEnd, Qt::NoModifier);
00776             it = QTreeWidgetItemIterator ( itemFromIndex(index) );
00777         }
00778 
00779         if ( static_cast<KJotsEntry*>(*it)->isBook() == false ) {
00780             break;
00781         }
00782     }
00783 
00784     if ( *it && *it != item ) {
00785         jumpToEntry(*it);
00786     }
00787 
00788     return;
00789 }
00790 
00791 void Bookshelf::onHeaderClick(int)
00792 {
00793     sortBook(invisibleRootItem());
00794     if ( !header()->isSortIndicatorShown() ) {
00795         header()->setSortIndicatorShown(true);
00796     }
00797     header()->setSortIndicator(0, sortOrder);
00798     header()->update();
00799     return;
00800 }
00801 
00802 // Sorting with the Qt widgets sucks, so we make our own. Believe it
00803 // or not but yes; all this crap is actually necessary to get it to
00804 // work. Things fail spectaculalry if you don't make sure all the
00805 // books are closed before sorting them.
00806 void Bookshelf::sortBook(QTreeWidgetItem *book )
00807 {
00808     setUpdatesEnabled(false);
00809 
00810     QList<QTreeWidgetItem*> openedBooks;
00811     QTreeWidgetItemIterator it ( book, QTreeWidgetItemIterator::HasChildren );
00812     while ( *it ) {
00813         if ( (*it)->isExpanded() ) {
00814             openedBooks << *it;
00815             (*it)->setExpanded(false);
00816         }
00817         it++;
00818     }
00819 
00820     sortOrder = (sortOrder==Qt::DescendingOrder) ?
00821         Qt::AscendingOrder : Qt::DescendingOrder;
00822     book->sortChildren(0, sortOrder);
00823 
00824     foreach ( QTreeWidgetItem *item, openedBooks ) {
00825         item->setExpanded(true);
00826     }
00827 
00828     setUpdatesEnabled(true);
00829 
00830     return;
00831 }
00832 
00833 #include "bookshelf.moc"
00834 /* ex: set tabstop=4 softtabstop=4 shiftwidth=4 expandtab: */
00835 /* kate: tab-indents off; replace-tabs on; tab-width 4; remove-trailing-space on; encoding utf-8;*/

kjots

Skip menu "kjots"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kdeutils

Skip menu "kdeutils"
  • ark
  • kcalc
  • kcharselect
  • kdelirc
  • kdessh
  • kdf
  • kfloppy
  • kgpg
  • kjots
  • klaptopdaemon
  • kmilo
  • ksim
  • ktimer
  • kwallet
  • superkaramba
Generated for kdeutils by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal