QFrame *frame = new QFrame(this);
setCentralWidget(frame);
- QHBoxLayout *horizontalLayout = new QHBoxLayout(this);
+ QHBoxLayout *horizontalLayout = new QHBoxLayout(frame);
frame->setLayout(horizontalLayout);
list = new QListWidget(this);
#endif
BookView::BookView(QWidget *parent):
- QWebView(parent), contentIndex(-1), mBook(0), restore(true),
+ QWebView(parent), contentIndex(-1), mBook(0), restorePositionAfterLoad(false),
positionAfterLoad(0), loaded(false), contentsHeight(0), decorated(false)
{
Trace t("BookView::BookView");
void BookView::setBook(Book *book)
{
Trace t("BookView::setBook");
+
+ // Save position in current book
setLastBookmark();
+
+ // Open new book, restore last position
if (book != mBook) {
mBook = book;
if (book) {
contentIndex = -1;
book->open();
- goToBookmark(book->lastBookmark());
+ restoreLastBookmark();
}
else {
contentIndex = 0;
void BookView::setLastBookmark()
{
- Trace t("BookView::saveLastBookmark");
+ Trace t("BookView::setLastBookmark");
if (mBook) {
- int height = contentsHeight; // page()->mainFrame()->contentsSize().height();
+ int height = contentsHeight;
int pos = page()->mainFrame()->scrollPosition().y();
t.trace(QString("At %1 (%2%, height %3)").
arg(pos).arg((qreal)pos / (qreal)height * 100).arg(height));
}
}
+void BookView::restoreLastBookmark()
+{
+ Trace t("BookView::restoreLastBookmark");
+ if (mBook) {
+ goToBookmark(mBook->lastBookmark());
+ }
+}
+
void BookView::goToBookmark(const Book::Bookmark &bookmark)
{
Trace t("BookView::goToBookmark");
if (mBook) {
- restore = true;
- positionAfterLoad = bookmark.pos;
if (bookmark.chapter != contentIndex) {
+ t.trace(QString("Loading new chapter %1").arg(bookmark.chapter));
+ restorePositionAfterLoad = true;
+ positionAfterLoad = bookmark.pos;
loadContent(bookmark.chapter);
} else {
- onLoadFinished(true);
+ goToPosition(bookmark.pos);
}
}
}
if (b.chapter != contentIndex) {
continue;
}
- int height = contentsHeight; // page()->mainFrame()->contentsSize().height();
+ int height = contentsHeight;
int bookmarkPos = (qreal)height * (qreal)b.pos;
painter.drawPixmap(2, bookmarkPos - scrollPos.y(), bookmarkPixmap);
}
QDir().rmpath(tmpPath());
}
-bool BookView::eventFilter(QObject *, QEvent *e)
+bool BookView::eventFilter(QObject *o, QEvent *e)
{
+#if 0
if (e->type() != QEvent::Paint && e->type() != QEvent::MouseMove) {
- Trace::debug(QString("BookView::eventFilter %1").
- arg(Trace::event(e->type())));
+ if (e->type() == QEvent::Resize) {
+ Trace::debug(QString("BookView::eventFilter QEvent::Resize to %1").
+ arg(page()->mainFrame()->contentsSize().height()));
+ } else {
+ Trace::debug(QString("BookView::eventFilter %1").
+ arg(Trace::event(e->type())));
+ }
}
+#endif
+
switch (e->type()) {
case QEvent::MouseButtonPress:
emit suppressedMouseButtonPress();
default:
break;
}
- return false;
+
+ return QObject::eventFilter(o, e);
}
void BookView::addJavaScriptObjects()
t.trace(QString("To %1").arg(size.height()));
contentsHeight = size.height();
if (decorated) {
- decorated = false;
- if (restore) {
- restore = false;
- if (mBook) {
- QWebPage *webPage = page();
- QWebFrame *mainFrame = webPage->mainFrame();
- int height = contentsHeight;
- int scrollPos = (qreal)height * positionAfterLoad;
- mainFrame->setScrollPosition(QPoint(0, scrollPos));
- t.trace(QString("Restoring positon to %1 (%2%, height %3)").
- arg(scrollPos).arg(positionAfterLoad * 100).arg(height));
- foreach (QString key, mainFrame->metaData().keys()) {
- QString value = mainFrame->metaData().value(key);
- t.trace(key + ": " + value);
- }
- }
+ if (restorePositionAfterLoad) {
+ restorePositionAfterLoad = false;
+ goToPosition(positionAfterLoad);
}
}
}
+
+void BookView::leaveEvent(QEvent *e)
+{
+ Trace t("BookView::leaveEvent");
+ setLastBookmark();
+ QWebView::leaveEvent(e);
+}
+
+void BookView::enterEvent(QEvent *e)
+{
+ Trace t("BookView::enterEvent");
+ restoreLastBookmark();
+ QWebView::enterEvent(e);
+}
+
+void BookView::goToPosition(qreal position)
+{
+ int scrollPos = (qreal)contentsHeight * position;
+ page()->mainFrame()->setScrollPosition(QPoint(0, scrollPos));
+ // FIXME: update();
+ Trace::debug(QString("BookView::goToPosition: To %1 (%2%, height %3)").
+ arg(scrollPos).arg(position * 100).arg(contentsHeight));
+}
void goToBookmark(const Book::Bookmark &bookmark);
void addBookmark();
void setLastBookmark();
+ void restoreLastBookmark();
signals:
void chapterLoadStart(int index);
virtual void paintEvent(QPaintEvent *e);
virtual void mousePressEvent(QMouseEvent *e);
bool eventFilter(QObject *o, QEvent *e);
+ virtual void leaveEvent(QEvent *);
+ virtual void enterEvent(QEvent *);
private:
/** Save navigation icons from resource to the file system. */
/** Get temporary directory for extracting book contents. */
QString tmpPath();
+ /** Go to a given (relative) position in current chapter. */
+ void goToPosition(qreal position);
+
int contentIndex; /**< Current chapter in book. */
Book *mBook; /**< Book to show. */
- bool restore; /**< Restoring position after load is needed. */
+ bool restorePositionAfterLoad;
+ /**< If true, restoring position after load is needed. */
qreal positionAfterLoad;
/**< Position to be restored after load. */
QImage bookmarkImage;
if (fullScreenZone().contains(event->x(), event->y())) {
emit restore();
} else {
- restoreButton->flash();
+ restoreButton->flash(700);
}
QMainWindow::MOUSE_ACTIVATE_EVENT(event);
}
#include <QtGui>
#include <QtDebug>
#include <QDir>
-#include <QCoreApplication>
+#include <QApplication>
#include <QFileInfo>
#ifdef Q_WS_MAEMO_5
# include <QtMaemo5/QMaemo5InformationBox>
Trace t("MainWindow::onSettingsChanged");
#ifdef Q_WS_MAEMO_5
if (key == "orientation") {
+ // view->setLastBookmark();
QString value = Settings::instance()->value(key).toString();
if (value == "portrait") {
setAttribute(Qt::WA_Maemo5PortraitOrientation, true);
- setAttribute(Qt::WA_Maemo5LandscapeOrientation, false);
}
else {
- setAttribute(Qt::WA_Maemo5PortraitOrientation, false);
setAttribute(Qt::WA_Maemo5LandscapeOrientation, true);
}
+
+ // view->restoreLastBookmark();
+ // view->setFocus();
+ // raise();
+
+ // QApplication::setActiveWindow(this);
+ // activateWindow();
+ // QEvent *enter = new QEvent(QEvent::Enter);
+ // QApplication::postEvent(view, enter);
+
+ // view->grabKeyboard();
+ // showNormal();
+
+ // QTestEventList events;
+ // events.addMouseClick(Qt::LeftButton);
+ // events.simulate(view);
}
#else
Q_UNUSED(key);
* Make returning from full screen even more obvious
* Add more restrictions to JavaScript handling
* Restore bookmarks to the correct position
+ * Save and restore positon when application changes orientation temporarily
-- Akos Polster <akos@pipacs.com> Sat, 31 Jul 2010 20:00:00 +0200
QHBoxLayout *orientationLayout = new QHBoxLayout(orientationBox);
orientationLayout->setMargin(0);
orientationBox->setLayout(orientationLayout);
- QButtonGroup *orientationGroup = new QButtonGroup(this);
+ orientationGroup = new QButtonGroup(this);
QToolButton *portraitButton = new QToolButton(box);
portraitButton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
void SettingsWindow::onOrientationButtonClicked(int id)
{
+#ifdef Q_WS_MAEMO_5
+ Q_UNUSED(id);
+#else
QString orientation;
switch (id) {
- case OrientationLandscape: orientation = "landscape"; break;
- default: orientation = "portrait"; break;
+ case OrientationLandscape:
+ orientation = "landscape";
+ break;
+ default:
+ orientation = "portrait";
+ break;
}
Settings::instance()->setValue("orientation", orientation);
+#endif // Q_WS_MAEMO_5
}
#ifdef Q_WS_MAEMO_5
void SettingsWindow::closeEvent(QCloseEvent *e)
{
- Settings::instance()->setValue("zoom", zoomSlider->value());
- Settings::instance()->setValue("font", fontButton->currentFont().family());
+ Settings *settings = Settings::instance();
+ settings->setValue("zoom", zoomSlider->value());
+ settings->setValue("font", fontButton->currentFont().family());
+ settings->setValue("orientation",
+ (orientationGroup->checkedId() == OrientationLandscape)?
+ "landscape": "portrait");
e->accept();
}
class QFont;
class QSlider;
class QFontComboBox;
+class QButtonGroup;
class SettingsWindow: public QMainWindow
{
private:
QSlider *zoomSlider;
QFontComboBox *fontButton;
+ QButtonGroup *orientationGroup;
};
#endif // SETTINGSWINDOW_H
}
}
-void TranslucentButton::flash()
+void TranslucentButton::flash(int duration)
{
QPropertyAnimation *ani = new QPropertyAnimation(this, "opacity", 0);
- ani->setDuration(3000);
+ ani->setDuration(duration);
ani->setStartValue(0.);
ani->setEndValue(1.);
ani->setEasingCurve(QEasingCurve::OutQuart);
void setOpacity(qreal opacity);
public slots:
- void flash();
+ void flash(int duration = 3000);
protected:
virtual void paintEvent(QPaintEvent *);