#include "library.h"
#include "book.h"
+#include "trace.h"
Library *Library::mInstance = 0;
-Library::Library(QObject *parent): QAbstractListModel(parent), mNowReading(0)
+Library::Library(QObject *parent): QAbstractListModel(parent)
{
load();
}
QVariant Library::data(const QModelIndex &index, int role) const
{
- qDebug() << "Library::data, row" << index.row() << "role" << role;
-
if (!index.isValid()) {
return QVariant();
}
switch (role) {
case Qt::DisplayRole:
return mBooks[index.row()]->name();
+ case Qt::DecorationRole:
+ return QPixmap::fromImage(mBooks[index.row()]->cover);
default:
return QVariant();
}
Book *Library::book(const QModelIndex &index)
{
if (index.isValid()) {
- return mBooks[index.row()];
- } else {
- return 0;
+ if ((index.row() >= 0) && (index.row() < mBooks.size())) {
+ return mBooks[index.row()];
+ } else {
+ qCritical() << "*** Library::book: Bad index" << index.row();
+ }
}
+ return 0;
}
void Library::close()
QString key = "lib/book" + QString::number(i);
QString path = settings.value(key).toString();
Book *book = new Book(path);
+ connect(book, SIGNAL(opened(const QString &)),
+ this, SLOT(onBookOpened(const QString &)));
book->load();
- qDebug() << "Library::load: Add" << book->title << "from" << path;
mBooks.append(book);
}
QString currentPath = settings.value("lib/nowreading").toString();
- QModelIndex index = find(currentPath);
- if (index.isValid()) {
- mNowReading = mBooks[index.row()];
- qDebug() << "Library::load: Now reading" << mNowReading->path();
- }
+ mNowReading = find(currentPath);
}
void Library::save()
{
- qDebug() << "Library::save";
-
QSettings settings;
settings.setValue("lib/size", mBooks.size());
for (int i = 0; i < mBooks.size(); i++) {
QString key = "lib/book" + QString::number(i);
settings.setValue(key, mBooks[i]->path());
}
+ Book *currentBook = book(mNowReading);
settings.setValue("lib/nowreading",
- mNowReading? mNowReading->path(): QString());
+ currentBook? currentBook->path(): QString());
}
bool Library::add(QString path)
{
- qDebug() << "Library::add" << path;
+ Trace t("Library::add " + path);
if (path == "") {
- qWarning() << "Library::add: Empty path";
+ qCritical() << "*** Library::add: Empty path";
return false;
}
if (find(path).isValid()) {
- qDebug() << " Book already exists in library";
+ t.trace("Book already exists in library");
return false;
}
int size = mBooks.size();
void Library::remove(const QModelIndex &index)
{
- if (!index.isValid()) {
+ Book *toRemove = book(index);
+ if (!toRemove) {
return;
}
int row = index.row();
- if ((row < 0) || (row >= mBooks.size())) {
- return;
- }
beginRemoveRows(QModelIndex(), row, row);
- Book *book = mBooks[row];
mBooks.removeAt(row);
save();
endRemoveRows();
- if (book == mNowReading) {
- mNowReading = 0;
+ if (index == mNowReading) {
+ mNowReading = QModelIndex();
emit nowReadingChanged();
}
- delete book;
+ delete toRemove;
}
QModelIndex Library::nowReading() const
{
- return find(mNowReading);
+ return mNowReading;
}
-void Library::setNowReading(const QModelIndex index)
+void Library::setNowReading(const QModelIndex &index)
{
- if (index.isValid()) {
- int row = index.row();
- if ((row >= 0) && (row < mBooks.size())) {
- mNowReading = mBooks[row];
- }
- } else {
- mNowReading = 0;
- }
+ mNowReading = index;
save();
emit nowReadingChanged();
}
delete mBooks[i];
}
mBooks.clear();
- mNowReading = 0;
+ mNowReading = QModelIndex();
}
QModelIndex Library::find(QString path) const
QModelIndex Library::find(const Book *book) const
{
- for (int i = 0; i < mBooks.size(); i++) {
- if (book == mBooks[i]) {
- return index(i);
+ if (book) {
+ for (int i = 0; i < mBooks.size(); i++) {
+ if (book == mBooks[i]) {
+ return index(i);
+ }
}
}
return QModelIndex();
}
+
+void Library::onBookOpened(const QString &path)
+{
+ Trace t("Library::onBookOpened " + path);
+ QModelIndex index = find(path);
+ if (index.isValid()) {
+ emit dataChanged(index, index);
+ }
+}