#endif
BookView::BookView(QWidget *parent):
- QWebView(parent), contentIndex(-1), mBook(0), restore(true), restorePos(0),
- loadFinished(false)
+ QWebView(parent), contentIndex(-1), mBook(0),
+ restore(true), restorePos(0), loadFinished(false)
{
settings()->setAttribute(QWebSettings::AutoLoadImages, true);
settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
settings()->setAttribute(QWebSettings::PluginsEnabled, false);
settings()->setAttribute(QWebSettings::ZoomTextOnly, true);
- settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, false);
+ settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls,
+ false);
page()->setContentEditable(false);
#if defined(Q_WS_MAEMO_5)
#include "book.h"
+class QModelIndex;
+
/** Visual representation of a book. */
class BookView: public QWebView
{
void InfoDialog::onReadBook()
{
- Library::instance()->setCurrent(Library::instance()->find(book));
+ Library::instance()->setNowReading(Library::instance()->find(book));
close();
}
void InfoDialog::onRemoveBook()
{
- QString title = book->title;
+ QString title = book->name();
if (QMessageBox::Yes ==
QMessageBox::question(this, "Delete book", "Delete book " + title,
QMessageBox::Yes
Library *Library::mInstance = 0;
-Library::Library(QObject *parent): QAbstractListModel(parent), mCurrent(0)
+Library::Library(QObject *parent): QAbstractListModel(parent), mNowReading(0)
{
load();
}
switch (role) {
case Qt::DisplayRole:
return mBooks[index.row()]->name();
- case BookRole:
- return QVariant::fromValue<Book>(*mBooks[index.row()]);
default:
return QVariant();
}
}
+Book *Library::book(const QModelIndex &index)
+{
+ if (index.isValid()) {
+ return mBooks[index.row()];
+ } else {
+ return 0;
+ }
+}
+
void Library::close()
{
delete mInstance;
qDebug() << "Library::load: Add" << book->title << "from" << path;
mBooks.append(book);
}
- QString currentPath = settings.value("lib/current").toString();
+ QString currentPath = settings.value("lib/nowreading").toString();
QModelIndex index = find(currentPath);
if (index.isValid()) {
- mCurrent = mBooks[index.row()];
- qDebug() << "Library::load: Current book is" << mCurrent->path();
+ mNowReading = mBooks[index.row()];
+ qDebug() << "Library::load: Now reading" << mNowReading->path();
}
}
QString key = "lib/book" + QString::number(i);
settings.setValue(key, mBooks[i]->path());
}
- settings.setValue("lib/current", mCurrent? mCurrent->path(): QString());
+ settings.setValue("lib/nowreading",
+ mNowReading? mNowReading->path(): QString());
}
bool Library::add(QString path)
mBooks.removeAt(row);
save();
endRemoveRows();
- if (book == mCurrent) {
- mCurrent = 0;
- emit currentBookChanged();
+ if (book == mNowReading) {
+ mNowReading = 0;
+ emit nowReadingChanged();
}
delete book;
}
-Book *Library::current() const
+QModelIndex Library::nowReading() const
{
- return mCurrent;
+ return find(mNowReading);
}
-void Library::setCurrent(const QModelIndex index)
+void Library::setNowReading(const QModelIndex index)
{
- int row = index.row();
- qDebug() << "Library::setCurrent" << row;
- if ((row >= 0) && (row < mBooks.size())) {
- mCurrent = mBooks[row];
- save();
- emit currentBookChanged();
+ if (index.isValid()) {
+ int row = index.row();
+ if ((row >= 0) && (row < mBooks.size())) {
+ mNowReading = mBooks[row];
+ }
+ } else {
+ mNowReading = 0;
}
+ save();
+ emit nowReadingChanged();
}
void Library::clear()
delete mBooks[i];
}
mBooks.clear();
- mCurrent = 0;
+ mNowReading = 0;
}
QModelIndex Library::find(QString path) const
Q_OBJECT
public:
- enum {
- BookRole = Qt::UserRole + 1,
- };
-
static Library *instance();
static void close();
int rowCount(const QModelIndex &parent = QModelIndex()) const;
void save();
QModelIndex find(QString path) const;
QModelIndex find(const Book *book) const;
- Book *current() const;
bool add(QString path);
void remove(const QModelIndex &index);
- void setCurrent(const QModelIndex index);
+ void setNowReading(const QModelIndex index);
+ QModelIndex nowReading() const;
+ Book *book(const QModelIndex &index);
signals:
- void currentBookChanged();
+ void nowReadingChanged();
private:
explicit Library(QObject *parent = 0);
void clear();
static Library *mInstance;
QList<Book *> mBooks;
- Book *mCurrent;
+ Book *mNowReading;
};
#endif // LIBRARY_H
if (lastDir == "") {
if (library->rowCount()) {
QModelIndex lastIndex = library->index(library->rowCount() - 1);
- Book lastBook = library->data(lastIndex,
- Library::BookRole).value<Book>();
- QFileInfo info(lastBook.path());
+ Book *lastBook = library->book(lastIndex);
+ QFileInfo info(lastBook->path());
lastDir = info.absolutePath();
}
}
void LibraryDialog::onBookAdded()
{
-#if 0 // FIXME
+#if 0
Library *library = Library::instance();
int index = library->size() - 1;
Book *book = library->at(index);
qDebug() << "LibraryDialog::onRemove";
QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
if (current.isValid()) {
- Book currentBook =
- Library::instance()->data(current, Library::BookRole).value<Book>();
- QString title = currentBook.name();
+ Book *currentBook = Library::instance()->book(current);
+ QString title = currentBook->name();
if (QMessageBox::Yes ==
QMessageBox::question(this, "Delete book",
"Delete book \"" + title + "\"?",
qDebug() << "LibraryDialog::onRead";
QModelIndex current = sortedLibrary->mapToSource(list->currentIndex());
if (current.isValid()) {
- Library::instance()->setCurrent(current);
+ Library::instance()->setNowReading(current);
}
}
"view-fullscreen");
// Handle model changes
- connect(Library::instance(), SIGNAL(currentBookChanged()),
+ connect(Library::instance(), SIGNAL(nowReadingChanged()),
this, SLOT(onCurrentBookChanged()));
normalFlags = windowFlags();
library->add(path);
QModelIndex index = library->find(path);
if (index.isValid()) {
- library->setCurrent(index);
+ library->setNowReading(index);
}
}
else {
- Book *current = library->current();
- if (current) {
- setCurrentBook(current);
+ QModelIndex index = library->nowReading();
+ if (index.isValid()) {
+ library->setNowReading(index);
}
else {
if (!library->rowCount()) {
library->add(":/books/2 B R 0 2 B.epub");
}
- library->setCurrent(library->index(0));
+ library->setNowReading(library->index(0));
}
}
void MainWindow::onCurrentBookChanged()
{
- setCurrentBook(Library::instance()->current());
+ setCurrentBook(Library::instance()->nowReading());
}
void MainWindow::showNormal()
restoreButton->flash();
}
-void MainWindow::setCurrentBook(Book *current)
+void MainWindow::setCurrentBook(const QModelIndex ¤t)
{
- book = current;
- view->setBook(current);
- setWindowTitle(current? current->title: "Dorian");
+ if (current.isValid()) {
+ Book *book = Library::instance()->book(current);
+ view->setBook(book);
+ setWindowTitle(book->name());
+ } else {
+ view->setBook(0);
+ setWindowTitle("Dorian");
+ }
}
-QAction *MainWindow::addToolBarAction(const QObject *receiver, const char *member,
+QAction *MainWindow::addToolBarAction(const QObject *receiver,
+ const char *member,
const QString &name)
{
return toolBar->
#include <QtGui>
class QString;
+class QModelIndex;
class DevTools;
class BookView;
class Book;
virtual void closeEvent(QCloseEvent *event);
private:
- void setCurrentBook(Book *book);
+ void setCurrentBook(const QModelIndex ¤t);
QAction *addToolBarAction(const QObject *receiver, const char *member,
const QString &name);
QRect fullScreenZone() const;
bool SortedLibrary::lessThan(const QModelIndex &left,
const QModelIndex &right) const
{
- Book leftBook = sourceModel()->data(left, Library::BookRole).value<Book>();
- Book rightBook = sourceModel()->data(right, Library::BookRole).value<Book>();
+ Book *leftBook = Library::instance()->book(left);
+ Book *rightBook = Library::instance()->book(right);
QString leftString;
QString rightString;
switch (mSortBy) {
case SortByTitle:
- leftString = leftBook.title;
- rightString = rightBook.title;
+ leftString = leftBook->name();
+ rightString = rightBook->name();
break;
default:
- leftString = leftBook.creators[0];
- rightString = rightBook.creators[0];
+ leftString = leftBook->creators[0];
+ rightString = rightBook->creators[0];
break;
}